| 1 |
/* PR target/112824 */ |
| 2 |
/* { dg-do compile } */ |
| 3 |
/* { dg-options "-std=c++23 -O3 -march=skylake-avx512 -mprefer-vector-width=512" } */ |
| 4 |
/* { dg-final { scan-assembler-not "vmov(?:dqu|apd)\[ \\t\]+\[^\n\]*%ymm" } } */ |
| 5 |
|
| 6 |
#include <bit> |
| 7 |
#include <concepts> |
| 8 |
#include <cstddef> |
| 9 |
#include <cstdint> |
| 10 |
|
| 11 |
template <ptrdiff_t W, typename T> |
| 12 |
using Vec [[gnu::vector_size(W * sizeof(T))]] = T; |
| 13 |
|
| 14 |
// Omitted: 16 without AVX, 32 without AVX512F, |
| 15 |
// or for forward compatibility some AVX10 may also mean 32-only |
| 16 |
static constexpr ptrdiff_t VectorBytes = 64; |
| 17 |
template<typename T> |
| 18 |
static constexpr ptrdiff_t VecWidth = 64 <= sizeof(T) ? 1 : 64/sizeof(T); |
| 19 |
|
| 20 |
template <typename T, ptrdiff_t N> struct Vector{ |
| 21 |
static constexpr ptrdiff_t L = N; |
| 22 |
T data[L]; |
| 23 |
static constexpr auto size()->ptrdiff_t{return N;} |
| 24 |
}; |
| 25 |
template <std::floating_point T, ptrdiff_t N> struct Vector<T,N>{ |
| 26 |
static constexpr ptrdiff_t W = N >= VecWidth<T> ? VecWidth<T> : ptrdiff_t(std::bit_ceil(size_t(N))); |
| 27 |
static constexpr ptrdiff_t L = (N/W) + ((N%W)!=0); |
| 28 |
using V = Vec<W,T>; |
| 29 |
V data[L]; |
| 30 |
static constexpr auto size()->ptrdiff_t{return N;} |
| 31 |
}; |
| 32 |
/// should be trivially copyable |
| 33 |
/// codegen is worse when passing by value, even though it seems like it should make |
| 34 |
/// aliasing simpler to analyze? |
| 35 |
template<typename T, ptrdiff_t N> |
| 36 |
[[gnu::always_inline]] constexpr auto operator+(Vector<T,N> x, Vector<T,N> y) -> Vector<T,N> { |
| 37 |
Vector<T,N> z; |
| 38 |
for (ptrdiff_t n = 0; n < Vector<T,N>::L; ++n) z.data[n] = x.data[n] + y.data[n]; |
| 39 |
return z; |
| 40 |
} |
| 41 |
template<typename T, ptrdiff_t N> |
| 42 |
[[gnu::always_inline]] constexpr auto operator*(Vector<T,N> x, Vector<T,N> y) -> Vector<T,N> { |
| 43 |
Vector<T,N> z; |
| 44 |
for (ptrdiff_t n = 0; n < Vector<T,N>::L; ++n) z.data[n] = x.data[n] * y.data[n]; |
| 45 |
return z; |
| 46 |
} |
| 47 |
template<typename T, ptrdiff_t N> |
| 48 |
[[gnu::always_inline]] constexpr auto operator+(T x, Vector<T,N> y) -> Vector<T,N> { |
| 49 |
Vector<T,N> z; |
| 50 |
for (ptrdiff_t n = 0; n < Vector<T,N>::L; ++n) z.data[n] = x + y.data[n]; |
| 51 |
return z; |
| 52 |
} |
| 53 |
template<typename T, ptrdiff_t N> |
| 54 |
[[gnu::always_inline]] constexpr auto operator*(T x, Vector<T,N> y) -> Vector<T,N> { |
| 55 |
Vector<T,N> z; |
| 56 |
for (ptrdiff_t n = 0; n < Vector<T,N>::L; ++n) z.data[n] = x * y.data[n]; |
| 57 |
return z; |
| 58 |
} |
| 59 |
|
| 60 |
template <typename T, ptrdiff_t N> struct Dual { |
| 61 |
T value; |
| 62 |
Vector<T, N> partials; |
| 63 |
}; |
| 64 |
// Here we have a specialization for non-power-of-2 `N` |
| 65 |
template <typename T, ptrdiff_t N> |
| 66 |
requires(std::floating_point<T> && (std::popcount(size_t(N))>1)) |
| 67 |
struct Dual<T,N> { |
| 68 |
Vector<T, N+1> data; |
| 69 |
}; |
| 70 |
|
| 71 |
template<ptrdiff_t W, typename T> |
| 72 |
consteval auto firstoff(){ |
| 73 |
static_assert(std::same_as<T,double>, "type not implemented"); |
| 74 |
if constexpr (W==2) return Vec<2,int64_t>{0,1} != 0; |
| 75 |
else if constexpr (W == 4) return Vec<4,int64_t>{0,1,2,3} != 0; |
| 76 |
else if constexpr (W == 8) return Vec<8,int64_t>{0,1,2,3,4,5,6,7} != 0; |
| 77 |
else static_assert(false, "vector width not implemented"); |
| 78 |
} |
| 79 |
|
| 80 |
template <typename T, ptrdiff_t N> |
| 81 |
[[gnu::always_inline]] constexpr auto operator+(Dual<T, N> a, |
| 82 |
Dual<T, N> b) |
| 83 |
-> Dual<T, N> { |
| 84 |
if constexpr (std::floating_point<T> && (std::popcount(size_t(N))>1)){ |
| 85 |
Dual<T,N> c; |
| 86 |
for (ptrdiff_t l = 0; l < Vector<T,N>::L; ++l) |
| 87 |
c.data.data[l] = a.data.data[l] + b.data.data[l]; |
| 88 |
return c; |
| 89 |
} else return {a.value + b.value, a.partials + b.partials}; |
| 90 |
} |
| 91 |
|
| 92 |
template <typename T, ptrdiff_t N> |
| 93 |
[[gnu::always_inline]] constexpr auto operator*(Dual<T, N> a, |
| 94 |
Dual<T, N> b) |
| 95 |
-> Dual<T, N> { |
| 96 |
if constexpr (std::floating_point<T> && (std::popcount(size_t(N))>1)){ |
| 97 |
using V = typename Vector<T,N>::V; |
| 98 |
V va = V{}+a.data.data[0][0], vb = V{}+b.data.data[0][0]; |
| 99 |
V x = va * b.data.data[0]; |
| 100 |
Dual<T,N> c; |
| 101 |
c.data.data[0] = firstoff<Vector<T,N>::W,T>() ? x + vb*a.data.data[0] : x; |
| 102 |
for (ptrdiff_t l = 1; l < Vector<T,N>::L; ++l) |
| 103 |
c.data.data[l] = va*b.data.data[l] + vb*a.data.data[l]; |
| 104 |
return c; |
| 105 |
} else return {a.value * b.value, a.value * b.partials + b.value * a.partials}; |
| 106 |
} |
| 107 |
|
| 108 |
void prod(Dual<Dual<double,7>,2> &c, const Dual<Dual<double,7>,2> &a, const Dual<Dual<double,7>,2>&b){ |
| 109 |
c = a*b; |
| 110 |
} |