numerics
Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1/// @file spectral/backends/stdsimd/impl.hpp
2/// @brief std::experimental::simd butterfly for FFT.
3///
4/// Uses the portable C++ SIMD abstraction (<experimental/simd>, GCC 11+).
5/// On AVX2 platforms vd::size() == 4 (4 doubles/register), on NEON == 2.
6///
7/// The gather uses the generator-lambda constructor:
8/// simd<double, abi> ur([](int k){ return a[j+k].real(); });
9/// which the compiler maps to SIMD gather instructions when possible.
10///
11/// The scatter-store back to AoS is the main cost difference vs. the
12/// handwritten backend -- the element-wise write loop is the comparison point.
13///
14/// Only compiled when NUMERICS_HAS_STD_SIMD is defined.
15#pragma once
16#ifdef NUMERICS_HAS_STD_SIMD
17#include "spectral/fft.hpp"
18#include "../seq/impl.hpp"
19#include <experimental/simd>
20#include <cmath>
21#include <stdexcept>
22#include <vector>
23
24namespace stdx = std::experimental;
25
26namespace backends {
27namespace stdsimd {
28
29static constexpr double TWO_PI = 6.283185307179586476925286766559;
30
31// ---- FFTPlanImpl -----------------------------------------------------------
32
33struct FFTPlanImpl {
34 int n;
35 bool invert;
36 std::vector<std::vector<num::cplx>> twiddles;
37
38 FFTPlanImpl(int n_, bool inv) : n(n_), invert(inv) {
39 if (n_ == 0 || (n_ & (n_ - 1)))
40 throw std::invalid_argument("FFTPlan: length must be a power of two");
41 for (int len = 2; len <= n_; len <<= 1) {
42 double ang = TWO_PI / static_cast<double>(len) * (inv ? 1.0 : -1.0);
43 num::cplx wlen{std::cos(ang), std::sin(ang)};
44 std::vector<num::cplx> tw(len / 2);
45 num::cplx w{1.0, 0.0};
46 for (int j = 0; j < len / 2; ++j) { tw[j] = w; w *= wlen; }
47 twiddles.push_back(std::move(tw));
48 }
49 }
50
51 void execute(num::CVector& a) const {
52 using vd = stdx::simd<double, stdx::simd_abi::native<double>>;
53 constexpr int W = static_cast<int>(vd::size());
54
56 num::cplx* data = a.data();
57
58 int stage = 0;
59 for (int len = 2; len <= n; len <<= 1, ++stage) {
60 int hlen = len / 2;
61 const num::cplx* tw = twiddles[stage].data();
62
63 for (int i = 0; i < n; i += len) {
64 num::cplx* up = data + i;
65 num::cplx* vp = data + i + hlen;
66
67 int j = 0;
68 for (; j + W <= hlen; j += W) {
69 // Gather: split AoS complex into separate real/imag vectors.
70 vd ur([&](int k) -> double { return up[j+k].real(); });
71 vd ui([&](int k) -> double { return up[j+k].imag(); });
72 vd vr([&](int k) -> double { return vp[j+k].real(); });
73 vd vi([&](int k) -> double { return vp[j+k].imag(); });
74 vd wr([&](int k) -> double { return tw[j+k].real(); });
75 vd wi([&](int k) -> double { return tw[j+k].imag(); });
76
77 // Complex multiply: t = v * w
78 vd tr = vr * wr - vi * wi;
79 vd ti = vr * wi + vi * wr;
80
81 // Butterfly + scatter store
82 for (int k = 0; k < W; ++k) {
83 up[j+k] = {ur[k] + tr[k], ui[k] + ti[k]};
84 vp[j+k] = {ur[k] - tr[k], ui[k] - ti[k]};
85 }
86 }
87 // scalar tail
88 for (; j < hlen; ++j) {
89 num::cplx t = vp[j] * tw[j];
90 num::cplx uu = up[j];
91 up[j] = uu + t;
92 vp[j] = uu - t;
93 }
94 }
95 }
96 }
97};
98
99// ---- one-shot functions ----------------------------------------------------
100
101inline void fft(const num::CVector& in, num::CVector& out) {
102 int n = static_cast<int>(in.size());
103 for (int i = 0; i < n; ++i) out[i] = in[i];
104 FFTPlanImpl plan(n, false);
105 plan.execute(out);
106}
107
108inline void ifft(const num::CVector& in, num::CVector& out) {
109 int n = static_cast<int>(in.size());
110 for (int i = 0; i < n; ++i) out[i] = in[i];
111 FFTPlanImpl plan(n, true);
112 plan.execute(out);
113}
114
115inline void rfft(const num::Vector& in, num::CVector& out) {
116 int n = static_cast<int>(in.size());
117 num::CVector tmp(static_cast<num::idx>(n), num::cplx{0, 0});
118 for (int i = 0; i < n; ++i) tmp[i] = {in[i], 0.0};
119 FFTPlanImpl plan(n, false);
120 plan.execute(tmp);
121 for (int k = 0; k < n / 2 + 1; ++k) out[k] = tmp[k];
122}
123
124inline void irfft(const num::CVector& in, int n, num::Vector& out) {
125 num::CVector tmp(static_cast<num::idx>(n), num::cplx{0, 0});
126 for (int k = 0; k < n / 2 + 1; ++k) tmp[k] = in[k];
127 for (int k = 1; k < (n - 1) / 2 + 1; ++k)
128 tmp[n - k] = std::conj(in[k]);
129 FFTPlanImpl plan(n, true);
130 plan.execute(tmp);
131 for (int i = 0; i < n; ++i) out[i] = tmp[i].real();
132}
133
134} // namespace stdsimd
135} // namespace backends
136
137#endif // NUMERICS_HAS_STD_SIMD
Dense vector with optional GPU storage, templated over scalar type T.
Definition vector.hpp:23
constexpr idx size() const noexcept
Definition vector.hpp:77
FFT interface with backend dispatch.
void bit_reverse(num::CVector &a)
Definition impl.hpp:15
void ifft(const CVector &in, CVector &out, FFTBackend b=default_fft_backend)
Inverse complex DFT (unnormalised: result = n * true_inverse).
Definition fft.cpp:31
void fft(const CVector &in, CVector &out, FFTBackend b=default_fft_backend)
Forward complex DFT. out must be pre-allocated to in.size().
Definition fft.cpp:15
@ stdsimd
std::experimental::simd butterfly – requires NUMERICS_HAS_STD_SIMD
void irfft(const CVector &in, int n, Vector &out, FFTBackend b=default_fft_backend)
Complex-to-real inverse DFT (unnormalised).
Definition fft.cpp:61
void rfft(const Vector &in, CVector &out, FFTBackend b=default_fft_backend)
Real-to-complex forward DFT. out must be pre-allocated to n/2+1.
Definition fft.cpp:46
double real
Definition types.hpp:10
std::size_t idx
Definition types.hpp:11
std::complex< real > cplx
Definition types.hpp:12