// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #include "CpuFFT.h" // Under a name of its own: gemmi includes the same header with POCKETFFT_NO_MULTITHREADING, and its // templates and ours must not be merged by the linker into one (single-threaded or not) copy. #define pocketfft jfjoch_pocketfft // MSVC: the scalar code (pocketfft vectorises only with the GCC/Clang vector extension, and decides so // itself; said here so that it stays so). #ifdef _MSC_VER #define POCKETFFT_NO_VECTORS #endif #include "../gemmi_gph/gemmi/third_party/pocketfft_hdronly.h" namespace cpu_fft { namespace { // pocketfft counts strides in bytes. pocketfft::stride_t Bytes(const Strides &s, size_t element) { pocketfft::stride_t out(s.size()); for (size_t i = 0; i < s.size(); i++) out[i] = s[i] * static_cast(element); return out; } Strides Contiguous(const Shape &shape) { Strides s(shape.size()); std::ptrdiff_t step = 1; for (size_t i = shape.size(); i-- > 0;) { s[i] = step; step *= static_cast(shape[i]); } return s; } Shape Halved(Shape shape, size_t axis) { shape[axis] = shape[axis] / 2 + 1; return shape; } } // namespace void R2C(const Shape &shape, const Strides &stride_in, const Strides &stride_out, const Shape &axes, const float *in, std::complex *out, size_t nthreads) { pocketfft::r2c(shape, Bytes(stride_in, sizeof(float)), Bytes(stride_out, sizeof(std::complex)), axes, pocketfft::FORWARD, in, out, 1.0f, nthreads); } void R2C(const Shape &shape, const Shape &axes, const float *in, std::complex *out, size_t nthreads) { R2C(shape, Contiguous(shape), Contiguous(Halved(shape, axes.back())), axes, in, out, nthreads); } void C2R(const Shape &shape, const Shape &axes, std::complex *in, float *out, size_t nthreads) { const Shape half = Halved(shape, axes.back()); const pocketfft::stride_t stride_in = Bytes(Contiguous(half), sizeof(std::complex)); if (axes.size() > 1) pocketfft::c2c(half, stride_in, stride_in, Shape(axes.begin(), axes.end() - 1), pocketfft::BACKWARD, in, in, 1.0f, nthreads); pocketfft::c2r(shape, stride_in, Bytes(Contiguous(shape), sizeof(float)), axes.back(), pocketfft::BACKWARD, in, out, 1.0f, nthreads); } void C2C(const Shape &shape, const Strides &stride, const Shape &axes, bool forward, std::complex *data, size_t nthreads) { const pocketfft::stride_t s = Bytes(stride, sizeof(std::complex)); pocketfft::c2c(shape, s, s, axes, forward, data, data, 1.0f, nthreads); } void C2C(const Shape &shape, const Shape &axes, bool forward, std::complex *data, size_t nthreads) { C2C(shape, Contiguous(shape), axes, forward, data, nthreads); } } // namespace cpu_fft