pocketfft_hdronly.h 108 KB
Newer Older
1
/*
2 3 4
This file is part of pocketfft.

Copyright (C) 2010-2019 Max-Planck-Society
5 6 7 8 9 10 11
Copyright (C) 2019 Peter Bell

For the odd-sized DCT-IV transforms:
  Copyright (C) 2003, 2007-14 Matteo Frigo
  Copyright (C) 2003, 2007-14 Massachusetts Institute of Technology

Authors: Martin Reinecke, Peter Bell
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors may
  be used to endorse or promote products derived from this software without
  specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
38 39 40 41

#ifndef POCKETFFT_HDRONLY_H
#define POCKETFFT_HDRONLY_H

42
#ifndef __cplusplus
43
#error This file is C++ and requires a C++ compiler.
44 45
#endif

Martin Reinecke's avatar
sync  
Martin Reinecke committed
46
#if !(__cplusplus >= 201103L || _MSVC_LANG+0L >= 201103L)
47
#error This file requires at least C++11 support.
48 49
#endif

50 51 52 53
#ifndef POCKETFFT_CACHE_SIZE
#define POCKETFFT_CACHE_SIZE 16
#endif

54 55 56 57 58 59
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdexcept>
#include <memory>
#include <vector>
Martin Reinecke's avatar
sync  
Martin Reinecke committed
60
#include <complex>
61 62
#if POCKETFFT_CACHE_SIZE!=0
#include <array>
Martin Reinecke's avatar
Martin Reinecke committed
63
#include <mutex>
64
#endif
Peter Bell's avatar
Peter Bell committed
65

66
#ifndef POCKETFFT_NO_MULTITHREADING
Peter Bell's avatar
Peter Bell committed
67 68 69 70 71 72
#include <mutex>
#include <condition_variable>
#include <thread>
#include <queue>
#include <atomic>
#include <functional>
Martin Reinecke's avatar
Martin Reinecke committed
73

74 75
#ifdef POCKETFFT_PTHREADS
#  include <pthread.h>
Martin Reinecke's avatar
Martin Reinecke committed
76
#endif
77
#endif
78

Martin Reinecke's avatar
Martin Reinecke committed
79
#if defined(__GNUC__)
80 81
#define POCKETFFT_NOINLINE __attribute__((noinline))
#define POCKETFFT_RESTRICT __restrict__
Martin Reinecke's avatar
Martin Reinecke committed
82
#elif defined(_MSC_VER)
83 84
#define POCKETFFT_NOINLINE __declspec(noinline)
#define POCKETFFT_RESTRICT __restrict
85
#else
86 87
#define POCKETFFT_NOINLINE
#define POCKETFFT_RESTRICT
88 89 90 91 92 93
#endif

namespace pocketfft {

namespace detail {

Martin Reinecke's avatar
Martin Reinecke committed
94 95
using namespace std;

Martin Reinecke's avatar
Martin Reinecke committed
96 97 98 99 100 101
using shape_t = vector<size_t>;
using stride_t = vector<ptrdiff_t>;

constexpr bool FORWARD  = true,
               BACKWARD = false;

Martin Reinecke's avatar
sync  
Martin Reinecke committed
102 103 104 105
// only enable vector support for gcc>=5.0 and clang>=5.0
#ifndef POCKETFFT_NO_VECTORS
#define POCKETFFT_NO_VECTORS
#if defined(__INTEL_COMPILER)
106
// do nothing. This is necessary because this compiler also sets __GNUC__.
Martin Reinecke's avatar
sync  
Martin Reinecke committed
107 108 109 110 111 112 113 114 115 116 117
#elif defined(__clang__)
#if __clang__>=5
#undef POCKETFFT_NO_VECTORS
#endif
#elif defined(__GNUC__)
#if __GNUC__>=5
#undef POCKETFFT_NO_VECTORS
#endif
#endif
#endif

Martin Reinecke's avatar
Martin Reinecke committed
118 119
template<typename T> struct VLEN { static constexpr size_t val=1; };

Martin Reinecke's avatar
Martin Reinecke committed
120 121
#ifndef POCKETFFT_NO_VECTORS
#if (defined(__AVX512F__))
Martin Reinecke's avatar
Martin Reinecke committed
122 123
template<> struct VLEN<float> { static constexpr size_t val=16; };
template<> struct VLEN<double> { static constexpr size_t val=8; };
Martin Reinecke's avatar
Martin Reinecke committed
124
#elif (defined(__AVX__))
Martin Reinecke's avatar
Martin Reinecke committed
125 126
template<> struct VLEN<float> { static constexpr size_t val=8; };
template<> struct VLEN<double> { static constexpr size_t val=4; };
Martin Reinecke's avatar
Martin Reinecke committed
127
#elif (defined(__SSE2__))
Martin Reinecke's avatar
Martin Reinecke committed
128 129
template<> struct VLEN<float> { static constexpr size_t val=4; };
template<> struct VLEN<double> { static constexpr size_t val=2; };
Martin Reinecke's avatar
sync  
Martin Reinecke committed
130
#elif (defined(__VSX__))
Martin Reinecke's avatar
Martin Reinecke committed
131 132
template<> struct VLEN<float> { static constexpr size_t val=4; };
template<> struct VLEN<double> { static constexpr size_t val=2; };
Martin Reinecke's avatar
Martin Reinecke committed
133 134 135 136 137
#else
#define POCKETFFT_NO_VECTORS
#endif
#endif

Martin Reinecke's avatar
Martin Reinecke committed
138
template<typename T> class arr
139 140 141 142 143
  {
  private:
    T *p;
    size_t sz;

Martin Reinecke's avatar
Martin Reinecke committed
144
#if defined(POCKETFFT_NO_VECTORS)
145 146 147 148 149
    static T *ralloc(size_t num)
      {
      if (num==0) return nullptr;
      void *res = malloc(num*sizeof(T));
      if (!res) throw bad_alloc();
Martin Reinecke's avatar
Martin Reinecke committed
150 151 152 153 154 155 156 157
      return reinterpret_cast<T *>(res);
      }
    static void dealloc(T *ptr)
      { free(ptr); }
#elif __cplusplus >= 201703L
    static T *ralloc(size_t num)
      {
      if (num==0) return nullptr;
158 159
      void *res = aligned_alloc(64,num*sizeof(T));
      if (!res) throw bad_alloc();
Martin Reinecke's avatar
Martin Reinecke committed
160 161 162 163
      return reinterpret_cast<T *>(res);
      }
    static void dealloc(T *ptr)
      { free(ptr); }
164
#else // portable emulation
Martin Reinecke's avatar
Martin Reinecke committed
165 166 167
    static T *ralloc(size_t num)
      {
      if (num==0) return nullptr;
168 169 170 171 172 173
      void *ptr = malloc(num*sizeof(T)+64);
      if (!ptr) throw bad_alloc();
      T *res = reinterpret_cast<T *>
        ((reinterpret_cast<size_t>(ptr) & ~(size_t(63))) + 64);
      (reinterpret_cast<void**>(res))[-1] = ptr;
      return res;
Martin Reinecke's avatar
Martin Reinecke committed
174 175
      }
    static void dealloc(T *ptr)
176
      { if (ptr) free((reinterpret_cast<void**>(ptr))[-1]); }
Martin Reinecke's avatar
Martin Reinecke committed
177
#endif
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

  public:
    arr() : p(0), sz(0) {}
    arr(size_t n) : p(ralloc(n)), sz(n) {}
    arr(arr &&other)
      : p(other.p), sz(other.sz)
      { other.p=nullptr; other.sz=0; }
    ~arr() { dealloc(p); }

    void resize(size_t n)
      {
      if (n==sz) return;
      dealloc(p);
      p = ralloc(n);
      sz = n;
      }

    T &operator[](size_t idx) { return p[idx]; }
    const T &operator[](size_t idx) const { return p[idx]; }

    T *data() { return p; }
    const T *data() const { return p; }

    size_t size() const { return sz; }
  };

template<typename T> struct cmplx {
  T r, i;
  cmplx() {}
  cmplx(T r_, T i_) : r(r_), i(i_) {}
  void Set(T r_, T i_) { r=r_; i=i_; }
  void Set(T r_) { r=r_; i=T(0); }
Martin Reinecke's avatar
Martin Reinecke committed
210 211
  void Split(T &r_, T &i_) const { r_=r; i_=i; }
  void SplitConj(T &r_, T &i_) const { r_=r; i_=-i; }
212 213 214 215
  cmplx &operator+= (const cmplx &other)
    { r+=other.r; i+=other.i; return *this; }
  template<typename T2>cmplx &operator*= (T2 other)
    { r*=other; i*=other; return *this; }
216 217 218 219 220 221 222
  template<typename T2>cmplx &operator*= (const cmplx<T2> &other)
    {
    T tmp = r*other.r - i*other.i;
    i = r*other.i + i*other.r;
    r = tmp;
    return *this;
    }
Martin Reinecke's avatar
Martin Reinecke committed
223 224 225 226
  template<typename T2>cmplx &operator+= (const cmplx<T2> &other)
    { r+=other.r; i+=other.i; return *this; }
  template<typename T2>cmplx &operator-= (const cmplx<T2> &other)
    { r-=other.r; i-=other.i; return *this; }
227 228 229
  template<typename T2> auto operator* (const T2 &other) const
    -> cmplx<decltype(r*other)>
    { return {r*other, i*other}; }
Martin Reinecke's avatar
Martin Reinecke committed
230 231 232 233 234 235
  template<typename T2> auto operator+ (const cmplx<T2> &other) const
    -> cmplx<decltype(r+other.r)>
    { return {r+other.r, i+other.i}; }
  template<typename T2> auto operator- (const cmplx<T2> &other) const
    -> cmplx<decltype(r+other.r)>
    { return {r-other.r, i-other.i}; }
236 237 238
  template<typename T2> auto operator* (const cmplx<T2> &other) const
    -> cmplx<decltype(r+other.r)>
    { return {r*other.r-i*other.i, r*other.i + i*other.r}; }
Martin Reinecke's avatar
Martin Reinecke committed
239
  template<bool fwd, typename T2> auto special_mul (const cmplx<T2> &other) const
240 241
    -> cmplx<decltype(r+other.r)>
    {
Martin Reinecke's avatar
sync  
Martin Reinecke committed
242
    using Tres = cmplx<decltype(r+other.r)>;
Martin Reinecke's avatar
Martin Reinecke committed
243 244
    return fwd ? Tres(r*other.r+i*other.i, i*other.r-r*other.i)
               : Tres(r*other.r-i*other.i, r*other.i+i*other.r);
245 246
    }
};
Martin Reinecke's avatar
Martin Reinecke committed
247 248
template<typename T> inline void PM(T &a, T &b, T c, T d)
  { a=c+d; b=c-d; }
Martin Reinecke's avatar
Martin Reinecke committed
249 250
template<typename T> inline void PMINPLACE(T &a, T &b)
  { T t = a; a+=b; b=t-b; }
251 252
template<typename T> inline void MPINPLACE(T &a, T &b)
  { T t = a; a-=b; b=t+b; }
253 254
template<typename T> cmplx<T> conj(const cmplx<T> &a)
  { return {a.r, -a.i}; }
Martin Reinecke's avatar
Martin Reinecke committed
255 256 257 258 259
template<bool fwd, typename T, typename T2> void special_mul (const cmplx<T> &v1, const cmplx<T2> &v2, cmplx<T> &res)
  {
  res = fwd ? cmplx<T>(v1.r*v2.r+v1.i*v2.i, v1.i*v2.r-v1.r*v2.i)
            : cmplx<T>(v1.r*v2.r-v1.i*v2.i, v1.r*v2.i+v1.i*v2.r);
  }
260 261 262

template<typename T> void ROT90(cmplx<T> &a)
  { auto tmp_=a.r; a.r=-a.i; a.i=tmp_; }
Martin Reinecke's avatar
Martin Reinecke committed
263 264
template<bool fwd, typename T> void ROTX90(cmplx<T> &a)
  { auto tmp_= fwd ? -a.r : a.r; a.r = fwd ? a.i : -a.i; a.i=tmp_; }
265 266 267 268 269

//
// twiddle factor section
//

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
/** Approximate sin(pi*a) within the range [-0.25, 0.25] */
inline float sinpi0(float a)
  {
  // adapted from https://stackoverflow.com/questions/42792939/
  float s = a * a;
  float r =      -5.957031250000000000e-01f;
  r = fma (r, s,  2.550399541854858398e+00f);
  r = fma (r, s, -5.167724132537841797e+00f);
  r = (a * s) * r;
  return fma (a, 3.141592741012573242e+00f, r);
  }

/** Approximate sin(pi*a) within the range [-0.25, 0.25] */
inline double sinpi0(double a)
  {
  // adapted from https://stackoverflow.com/questions/42792939/
  double s = a * a;
  double r =      4.6151442520157035e-4;
  r = fma (r, s, -7.3700183130883555e-3);
  r = fma (r, s,  8.2145868949323936e-2);
  r = fma (r, s, -5.9926452893214921e-1);
  r = fma (r, s,  2.5501640398732688e+0);
  r = fma (r, s, -5.1677127800499516e+0);
  s = s * a;
  r = r * s;
  return fma (a, 3.1415926535897931e+0, r);
  }

/** Approximate cos(pi*x)-1 for x in [-0.25,0.25] */
inline float cosm1pi0(float a)
  {
  // adapted from https://stackoverflow.com/questions/42792939/
  float s = a * a;
  float r =        2.313842773437500000e-01f;
  r = fmaf (r, s, -1.335021972656250000e+00f);
  r = fmaf (r, s,  4.058703899383544922e+00f);
  r = fmaf (r, s, -4.934802055358886719e+00f);
  return r*s;
  }

/** Approximate cos(pi*x)-1 for x in [-0.25,0.25] */
inline double cosm1pi0(double a)
  {
  // adapted from https://stackoverflow.com/questions/42792939/
  double s = a * a;
  double r =     -1.0369917389758117e-4;
  r = fma (r, s,  1.9294935641298806e-3);
  r = fma (r, s, -2.5806887942825395e-2);
  r = fma (r, s,  2.3533063028328211e-1);
  r = fma (r, s, -1.3352627688538006e+0);
  r = fma (r, s,  4.0587121264167623e+0);
  r = fma (r, s, -4.9348022005446790e+0);
  return r*s;
  }

template <typename T> void sincosm1pi0(T a_, T *POCKETFFT_RESTRICT res)
  {
  if (sizeof(T)>sizeof(double)) // don't have the code for long double
    {
    constexpr T pi = T(3.141592653589793238462643383279502884197L);
    auto s = sin(pi*a_);
    res[1] = s;
    res[0] = (s*s)/(-sqrt((1-s)*(1+s))-1);
    return;
    }
  res[0] = T(cosm1pi0(double(a_)));
  res[1] = T(sinpi0(double(a_)));
  }

template <typename T> T sinpi(T a)
  {
  // reduce argument to primary approximation interval (-0.25, 0.25)
  auto r = nearbyint (a + a); // must use IEEE-754 "to nearest" rounding
  auto i = (int64_t)r;
  auto t = fma (T(-0.5), r, a);

  switch (i%4)
    {
    case 0:
      return sinpi0(t);
    case 1: case -3:
      return cosm1pi0(t) + T(1.);
    case 2: case -2:
      return T(0.) - sinpi0(t);
    case 3: case -1:
      return T(-1.) - cosm1pi0(t);
    }
357
  throw runtime_error("cannot happen");
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  }

template <typename T> T cospi(T a)
  {
  // reduce argument to primary approximation interval (-0.25, 0.25)
  auto r = nearbyint (a + a); // must use IEEE-754 "to nearest" rounding
  auto i = (int64_t)r;
  auto t = fma (T(-0.5), r, a);

  switch (i%4)
    {
    case 0:
      return cosm1pi0(t) + T(1.);
    case 1: case -3:
      return T(0.) - sinpi0(t);
    case 2: case -2:
      return T(-1.) - cosm1pi0(t);
    case 3: case -1:
      return sinpi0(t);
    }
378
  throw runtime_error("cannot happen");
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
  }

inline long double cospi(long double a)
  {
  constexpr auto pi = 3.141592653589793238462643383279502884197L;
  return sizeof(long double) > sizeof(double) ? cos(a * pi) :
    static_cast<long double>(cospi(static_cast<double>(a)));
  }

inline long double sinpi(long double a)
  {
  constexpr auto pi = 3.141592653589793238462643383279502884197L;
  return sizeof(long double) > sizeof(double) ? sin(a * pi) :
    static_cast<long double>(sinpi(static_cast<double>(a)));
  }

template <typename T> void sincospi(T a, T *POCKETFFT_RESTRICT res)
  {
  // reduce argument to primary approximation interval (-0.25, 0.25)
  auto r = nearbyint (a + a); // must use IEEE-754 "to nearest" rounding
  auto i = (int64_t)r;
  auto t = fma (T(-0.5), r, a);

  auto c=cosm1pi0(t)+T(1.);
  auto s=sinpi0(t);

  // map results according to quadrant
  if (i & 2)
    {
    s = T(0.)-s;
    c = T(0.)-c;
    }
  if (i & 1)
    {
    swap(s, c);
    c = T(0.)-c;
    }
  res[0]=c;
  res[1]=s;
  }

inline void sincospi(long double a, long double *POCKETFFT_RESTRICT res)
  {
  if (sizeof(long double) > sizeof(double))
    {
    constexpr auto pi = 3.141592653589793238462643383279502884197L;
    res[0] = cos(pi * a);
    res[1] = sin(pi * a);
    }
  else
    {
    double sincos[2];
    sincospi(static_cast<double>(a), sincos);
    res[0] = static_cast<long double>(sincos[0]);
    res[1] = static_cast<long double>(sincos[1]);
    }
  }

437 438 439
template<typename T> class sincos_2pibyn
  {
  private:
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
440
    using Thigh = typename conditional<(sizeof(T)>sizeof(double)), T, double>::type;
441
    size_t sz;
442
    arr<cmplx<T>> data;
443

444
    POCKETFFT_NOINLINE void calc_first_octant(size_t den,
445
      cmplx<T> * POCKETFFT_RESTRICT res)
446 447 448
      {
      size_t n = (den+4)>>3;
      if (n==0) return;
449
      res[0].Set(1.,0.);
450
      if (n==1) return;
Martin Reinecke's avatar
Martin Reinecke committed
451
      size_t l1 = size_t(sqrt(n));
452
      arr<cmplx<Thigh>> tmp(l1);
453 454
      for (size_t i=1; i<l1; ++i)
        {
455 456
        sincosm1pi0(Thigh(2*i)/Thigh(den),&tmp[i].r);
        res[i].Set(T(tmp[i].r)+1,T(tmp[i].i));
457 458 459 460
        }
      size_t start=l1;
      while(start<n)
        {
461 462 463
        cmplx<Thigh> cs;
        sincosm1pi0((Thigh(2*start))/Thigh(den),&cs.r);
        res[start].Set(T(cs.r+1), T(cs.i));
464 465 466 467
        size_t end = l1;
        if (start+end>n) end = n-start;
        for (size_t i=1; i<end; ++i)
          {
468 469 470
          cmplx<Thigh> csx=tmp[i];
          res[start+i].Set(T(((cs.r*csx.r - cs.i*csx.i + cs.r) + csx.r) + 1),
                           T((cs.r*csx.i + cs.i*csx.r) + cs.i + csx.i));
471 472 473 474 475
          }
        start += l1;
        }
      }

476
    void calc_first_quadrant(size_t n, cmplx<T> * POCKETFFT_RESTRICT res)
477
      {
478 479
      arr<cmplx<T>> p(n/2); // n is always even here
      calc_first_octant(n<<1, p.data());
480
      size_t ndone=(n+2)>>2;
481 482
      size_t i=0, idx1=0, idx2=ndone-1;
      for (; i+1<ndone; i+=2, ++idx1, --idx2)
483
        {
484 485
        res[idx1] = p[i];
        res[idx2].Set(p[i+1].i, p[i+1].r);
486 487
        }
      if (i!=ndone)
488
        res[idx1] = p[i];
489 490
      }

491
    void calc_first_half(size_t n, cmplx<T> * POCKETFFT_RESTRICT res)
492
      {
Martin Reinecke's avatar
sync  
Martin Reinecke committed
493
      int ndone=int(n+1)>>1;
494 495
      arr<cmplx<T>> p((n+1)/2); // n is always odd here
      calc_first_octant(n<<2, p.data());
Martin Reinecke's avatar
sync  
Martin Reinecke committed
496
      int i4=0, in=int(n), i=0;
497
      for (; i4<=in-i4; ++i, i4+=4) // octant 0
498
        res[i] = p[i4];
499
      for (; i4-in <= 0; ++i, i4+=4) // octant 1
500
        { auto xm = in-i4; res[i].Set(p[xm].i, p[xm].r); }
501
      for (; i4<=3*in-i4; ++i, i4+=4) // octant 2
502
        { auto xm = i4-in; res[i].Set(-p[xm].i, p[xm].r); }
503
      for (; i<ndone; ++i, i4+=4) // octant 3
504
        { auto xm = 2*in-i4; res[i].Set(-p[xm].r, p[xm].i); }
505 506
      }

507
    void fill_first_quadrant(size_t n, cmplx<T> * POCKETFFT_RESTRICT res)
508
      {
Martin Reinecke's avatar
sync  
Martin Reinecke committed
509
      constexpr T hsqt2 = T(0.707106781186547524400844362104849L);
510 511
      size_t quart = n>>2;
      if ((n&7)==0)
512 513 514
        res[quart/2].Set(hsqt2, hsqt2);
      for (size_t i=1, j=quart-1; 2*i<quart; ++i, --j)
        { res[j].Set(res[i].i, res[i].r); }
515 516
      }

517
    POCKETFFT_NOINLINE void fill_first_half(size_t n, cmplx<T> * POCKETFFT_RESTRICT res)
518 519 520
      {
      size_t half = n>>1;
      if ((n&3)==0)
521 522
        for (size_t i=0; i<half/2; ++i)
          res[i+half/2].Set(-res[i].i, res[i].r);
523
      else
524 525
        for (size_t i=1, j=half-1; 2*i<half; ++i, --j)
          res[j].Set(-res[i].r, res[i].i);
526
      res[half].Set(T(-1),T(0));
527 528
      }

529
    POCKETFFT_NOINLINE void sincos_2pibyn_half(size_t n, cmplx<T> * POCKETFFT_RESTRICT res)
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
      {
      if ((n&3)==0)
        {
        calc_first_octant(n, res);
        fill_first_quadrant(n, res);
        fill_first_half(n, res);
        }
      else if ((n&1)==0)
        {
        calc_first_quadrant(n, res);
        fill_first_half(n, res);
        }
      else
        calc_first_half(n, res);
      }

  public:
547 548 549 550 551
    POCKETFFT_NOINLINE sincos_2pibyn(size_t n)
      : sz(n), data(n/2+1)
      { sincos_2pibyn_half(n, data.data()); }

    cmplx<T> operator[](size_t idx) const
552
      {
553 554 555 556 557
      if (idx<data.size())
        return data[idx];
      auto c = data[sz-idx];
      c.i = -c.i;
      return c;
558 559 560
      }
  };

Martin Reinecke's avatar
Martin Reinecke committed
561
struct util // hack to avoid duplicate symbols
562
  {
563
  static POCKETFFT_NOINLINE size_t largest_prime_factor (size_t n)
564
    {
Martin Reinecke's avatar
Martin Reinecke committed
565 566 567
    size_t res=1;
    while ((n&1)==0)
      { res=2; n>>=1; }
Martin Reinecke's avatar
Martin Reinecke committed
568 569 570
    for (size_t x=3; x*x<=n; x+=2)
      while ((n%x)==0)
        { res=x; n/=x; }
Martin Reinecke's avatar
Martin Reinecke committed
571 572 573 574
    if (n>1) res=n;
    return res;
    }

575
  static POCKETFFT_NOINLINE double cost_guess (size_t n)
576
    {
Martin Reinecke's avatar
sync  
Martin Reinecke committed
577
    constexpr double lfp=1.1; // penalty for non-hardcoded larger factors
Martin Reinecke's avatar
Martin Reinecke committed
578 579 580 581
    size_t ni=n;
    double result=0.;
    while ((n&1)==0)
      { result+=2; n>>=1; }
Martin Reinecke's avatar
Martin Reinecke committed
582 583 584 585 586 587
    for (size_t x=3; x*x<=n; x+=2)
      while ((n%x)==0)
        {
        result+= (x<=5) ? double(x) : lfp*double(x); // penalize larger prime factors
        n/=x;
        }
Martin Reinecke's avatar
sync  
Martin Reinecke committed
588 589
    if (n>1) result+=(n<=5) ? double(n) : lfp*double(n);
    return result*double(ni);
590 591
    }

Martin Reinecke's avatar
Martin Reinecke committed
592
  /* returns the smallest composite of 2, 3, 5, 7 and 11 which is >= n */
593
  static POCKETFFT_NOINLINE size_t good_size_cmplx(size_t n)
Martin Reinecke's avatar
Martin Reinecke committed
594 595 596 597
    {
    if (n<=12) return n;

    size_t bestfac=2*n;
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
    for (size_t f11=1; f11<bestfac; f11*=11)
      for (size_t f117=f11; f117<bestfac; f117*=7)
        for (size_t f1175=f117; f1175<bestfac; f1175*=5)
          {
          size_t x=f1175;
          while (x<n) x*=2;
          for (;;)
            {
            if (x<n)
              x*=3;
            else if (x>n)
              {
              if (x<bestfac) bestfac=x;
              if (x&1) break;
              x>>=1;
              }
            else
              return n;
            }
          }
    return bestfac;
    }

  /* returns the smallest composite of 2, 3, 5 which is >= n */
  static POCKETFFT_NOINLINE size_t good_size_real(size_t n)
    {
    if (n<=6) return n;

    size_t bestfac=2*n;
    for (size_t f5=1; f5<bestfac; f5*=5)
      {
      size_t x = f5;
      while (x<n) x *= 2;
      for (;;)
        {
        if (x<n)
          x*=3;
        else if (x>n)
          {
          if (x<bestfac) bestfac=x;
          if (x&1) break;
          x>>=1;
          }
        else
          return n;
        }
      }
Martin Reinecke's avatar
Martin Reinecke committed
645 646
    return bestfac;
    }
647

Martin Reinecke's avatar
Martin Reinecke committed
648 649 650 651 652 653 654
  static size_t prod(const shape_t &shape)
    {
    size_t res=1;
    for (auto sz: shape)
      res*=sz;
    return res;
    }
Martin Reinecke's avatar
Martin Reinecke committed
655

656
  static POCKETFFT_NOINLINE void sanity_check(const shape_t &shape,
Martin Reinecke's avatar
Martin Reinecke committed
657 658 659 660 661 662
    const stride_t &stride_in, const stride_t &stride_out, bool inplace)
    {
    auto ndim = shape.size();
    if (ndim<1) throw runtime_error("ndim must be >= 1");
    if ((stride_in.size()!=ndim) || (stride_out.size()!=ndim))
      throw runtime_error("stride dimension mismatch");
Martin Reinecke's avatar
sync  
Martin Reinecke committed
663 664
    if (inplace && (stride_in!=stride_out))
      throw runtime_error("stride mismatch");
Martin Reinecke's avatar
Martin Reinecke committed
665 666
    }

667
  static POCKETFFT_NOINLINE void sanity_check(const shape_t &shape,
Martin Reinecke's avatar
Martin Reinecke committed
668 669 670 671 672 673 674 675
    const stride_t &stride_in, const stride_t &stride_out, bool inplace,
    const shape_t &axes)
    {
    sanity_check(shape, stride_in, stride_out, inplace);
    auto ndim = shape.size();
    shape_t tmp(ndim,0);
    for (auto ax : axes)
      {
676 677
      if (ax>=ndim) throw invalid_argument("bad axis number");
      if (++tmp[ax]>1) throw invalid_argument("axis specified repeatedly");
Martin Reinecke's avatar
Martin Reinecke committed
678 679 680
      }
    }

681
  static POCKETFFT_NOINLINE void sanity_check(const shape_t &shape,
Martin Reinecke's avatar
Martin Reinecke committed
682 683 684 685
    const stride_t &stride_in, const stride_t &stride_out, bool inplace,
    size_t axis)
    {
    sanity_check(shape, stride_in, stride_out, inplace);
686
    if (axis>=shape.size()) throw invalid_argument("bad axis number");
Martin Reinecke's avatar
Martin Reinecke committed
687
    }
Martin Reinecke's avatar
Martin Reinecke committed
688

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
#ifdef POCKETFFT_NO_MULTITHREADING
  static size_t thread_count (size_t /*nthreads*/, const shape_t &/*shape*/,
    size_t /*axis*/, size_t /*vlen*/)
    { return 1; }
#else
  static size_t thread_count (size_t nthreads, const shape_t &shape,
    size_t axis, size_t vlen)
    {
    if (nthreads==1) return 1;
    size_t size = prod(shape);
    size_t parallel = size / (shape[axis] * vlen);
    if (shape[axis] < 1000)
      parallel /= 4;
    size_t max_threads = nthreads == 0 ?
      thread::hardware_concurrency() : nthreads;
    return max(size_t(1), min(parallel, max_threads));
    }
#endif
Martin Reinecke's avatar
Martin Reinecke committed
707
  };
708

Peter Bell's avatar
Peter Bell committed
709
namespace threading {
710 711 712 713 714

#ifdef POCKETFFT_NO_MULTITHREADING

constexpr size_t thread_id = 0;
constexpr size_t num_threads = 1;
715
constexpr size_t max_threads = 1;
716 717 718 719 720 721 722

template <typename Func>
void thread_map(size_t /* nthreads */, Func f)
  { f(); }

#else

Peter Bell's avatar
Peter Bell committed
723 724
thread_local size_t thread_id = 0;
thread_local size_t num_threads = 1;
725
static const size_t max_threads = max(1u, thread::hardware_concurrency());
Peter Bell's avatar
Peter Bell committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

class latch
  {
    atomic<size_t> num_left_;
    mutex mut_;
    condition_variable completed_;
    using lock_t = unique_lock<mutex>;

  public:
    latch(size_t n): num_left_(n) {}

    void count_down()
      {
      lock_t lock(mut_);
      if (--num_left_)
        return;
      completed_.notify_all();
      }

    void wait()
      {
      lock_t lock(mut_);
      completed_.wait(lock, [this]{ return is_ready(); });
      }
    bool is_ready() { return num_left_ == 0; }
  };

template <typename T> class concurrent_queue
  {
755 756 757 758 759
    queue<T> q_;
    mutex mut_;
    condition_variable item_added_;
    bool shutdown_;
    using lock_t = unique_lock<mutex>;
Peter Bell's avatar
Peter Bell committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794

  public:
    concurrent_queue(): shutdown_(false) {}

    void push(T val)
      {
      {
      lock_t lock(mut_);
      if (shutdown_)
        throw runtime_error("Item added to queue after shutdown");
      q_.push(move(val));
      }
      item_added_.notify_one();
      }

    bool pop(T & val)
      {
      lock_t lock(mut_);
      item_added_.wait(lock, [this] { return (!q_.empty() || shutdown_); });
      if (q_.empty())
        return false;  // We are shutting down

      val = std::move(q_.front());
      q_.pop();
      return true;
      }

    void shutdown()
      {
      {
      lock_t lock(mut_);
      shutdown_ = true;
      }
      item_added_.notify_all();
      }
795 796

    void restart() { shutdown_ = false; }
Peter Bell's avatar
Peter Bell committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810
  };

class thread_pool
  {
    concurrent_queue<function<void()>> work_queue_;
    vector<thread> threads_;

    void worker_main()
      {
      function<void()> work;
      while (work_queue_.pop(work))
        work();
      }

811
    void create_threads()
Peter Bell's avatar
Peter Bell committed
812
      {
813
      size_t nthreads = threads_.size();
Peter Bell's avatar
Peter Bell committed
814 815 816 817 818
      for (size_t i=0; i<nthreads; ++i)
        {
        try { threads_[i] = thread([this]{ worker_main(); }); }
        catch (...)
          {
819
          shutdown();
Peter Bell's avatar
Peter Bell committed
820 821 822 823 824
          throw;
          }
        }
      }

825 826 827 828 829
  public:
    explicit thread_pool(size_t nthreads):
      threads_(nthreads)
      { create_threads(); }

830
    thread_pool(): thread_pool(max_threads) {}
Peter Bell's avatar
Peter Bell committed
831

832 833 834 835 836 837 838 839
    ~thread_pool() { shutdown(); }

    void submit(function<void()> work)
      {
      work_queue_.push(move(work));
      }

    void shutdown()
Peter Bell's avatar
Peter Bell committed
840 841
      {
      work_queue_.shutdown();
842 843 844
      for (auto &thread : threads_)
        if (thread.joinable())
          thread.join();
Peter Bell's avatar
Peter Bell committed
845 846
      }

847
    void restart()
Peter Bell's avatar
Peter Bell committed
848
      {
849 850
      work_queue_.restart();
      create_threads();
Martin Reinecke's avatar
Martin Reinecke committed
851
      }
Martin Reinecke's avatar
Martin Reinecke committed
852
  };
853

Peter Bell's avatar
Peter Bell committed
854 855 856
thread_pool & get_pool()
  {
  static thread_pool pool;
857 858 859 860 861 862 863 864 865 866 867 868
#ifdef POCKETFFT_PTHREADS
  static once_flag f;
  call_once(f,
    []{
    pthread_atfork(
      +[]{ get_pool().shutdown(); },  // prepare
      +[]{ get_pool().restart(); },   // parent
      +[]{ get_pool().restart(); }    // child
      );
    });
#endif

Peter Bell's avatar
Peter Bell committed
869 870 871 872 873 874 875 876
  return pool;
  }

/** Map a function f over nthreads */
template <typename Func>
void thread_map(size_t nthreads, Func f)
  {
  if (nthreads == 0)
877
    nthreads = max_threads;
Peter Bell's avatar
Peter Bell committed
878 879 880 881

  if (nthreads == 1)
    { f(); return; }

882
  auto & pool = get_pool();
Peter Bell's avatar
Peter Bell committed
883
  latch counter(nthreads);
884 885
  exception_ptr ex;
  mutex ex_mut;
Peter Bell's avatar
Peter Bell committed
886 887 888
  for (size_t i=0; i<nthreads; ++i)
    {
    pool.submit(
889
      [&f, &counter, &ex, &ex_mut, i, nthreads] {
Peter Bell's avatar
Peter Bell committed
890 891
      thread_id = i;
      num_threads = nthreads;
892 893 894 895 896 897
      try { f(); }
      catch (...)
        {
        lock_guard<mutex> lock(ex_mut);
        ex = current_exception();
        }
Peter Bell's avatar
Peter Bell committed
898 899 900 901
      counter.count_down();
      });
    }
  counter.wait();
902 903
  if (ex)
    rethrow_exception(ex);
Peter Bell's avatar
Peter Bell committed
904
  }
905 906 907

#endif

Peter Bell's avatar
Peter Bell committed
908 909
}

910 911 912 913 914 915 916 917 918 919 920 921 922
//
// complex FFTPACK transforms
//

template<typename T0> class cfftp
  {
  private:
    struct fctdata
      {
      size_t fct;
      cmplx<T0> *tw, *tws;
      };

Martin Reinecke's avatar
Martin Reinecke committed
923
    size_t length;
924
    arr<cmplx<T0>> mem;
Martin Reinecke's avatar
Martin Reinecke committed
925
    vector<fctdata> fact;
926 927

    void add_factor(size_t factor)
Martin Reinecke's avatar
Martin Reinecke committed
928
      { fact.push_back({factor, nullptr, nullptr}); }
929

Martin Reinecke's avatar
Martin Reinecke committed
930
template<bool fwd, typename T> void pass2 (size_t ido, size_t l1,
931
  const T * POCKETFFT_RESTRICT cc, T * POCKETFFT_RESTRICT ch,
Martin Reinecke's avatar
Martin Reinecke committed
932
  const cmplx<T0> * POCKETFFT_RESTRICT wa) const
933 934 935
  {
  constexpr size_t cdim=2;

936 937
  auto CH = [ch,ido,l1](size_t a, size_t b, size_t c) -> T&
    { return ch[a+ido*(b+l1*c)]; };
938
  auto CC = [cc,ido,cdim](size_t a, size_t b, size_t c) -> const T&
939 940 941 942
    { return cc[a+ido*(b+cdim*c)]; };
  auto WA = [wa, ido](size_t x, size_t i)
    { return wa[i-1+x*(ido-1)]; };

943 944 945 946 947 948 949 950 951 952 953 954 955 956
  if (ido==1)
    for (size_t k=0; k<l1; ++k)
      {
      CH(0,k,0) = CC(0,0,k)+CC(0,1,k);
      CH(0,k,1) = CC(0,0,k)-CC(0,1,k);
      }
  else
    for (size_t k=0; k<l1; ++k)
      {
      CH(0,k,0) = CC(0,0,k)+CC(0,1,k);
      CH(0,k,1) = CC(0,0,k)-CC(0,1,k);
      for (size_t i=1; i<ido; ++i)
        {
        CH(i,k,0) = CC(i,0,k)+CC(i,1,k);
Martin Reinecke's avatar
Martin Reinecke committed
957
        special_mul<fwd>(CC(i,0,k)-CC(i,1,k),WA(0,i),CH(i,k,1));
958 959 960 961
        }
      }
  }

962
#define POCKETFFT_PREP3(idx) \
963
        T t0 = CC(idx,0,k), t1, t2; \
Martin Reinecke's avatar
Martin Reinecke committed
964
        PM (t1,t2,CC(idx,1,k),CC(idx,2,k)); \
965
        CH(idx,k,0)=t0+t1;
966
#define POCKETFFT_PARTSTEP3a(u1,u2,twr,twi) \
967
        { \
Martin Reinecke's avatar
Martin Reinecke committed
968 969 970
        T ca=t0+t1*twr; \
        T cb{-t2.i*twi, t2.r*twi}; \
        PM(CH(0,k,u1),CH(0,k,u2),ca,cb) ;\
971
        }
972
#define POCKETFFT_PARTSTEP3b(u1,u2,twr,twi) \
973
        { \
Martin Reinecke's avatar
Martin Reinecke committed
974 975 976 977
        T ca=t0+t1*twr; \
        T cb{-t2.i*twi, t2.r*twi}; \
        special_mul<fwd>(ca+cb,WA(u1-1,i),CH(i,k,u1)); \
        special_mul<fwd>(ca-cb,WA(u2-1,i),CH(i,k,u2)); \
978
        }
Martin Reinecke's avatar
Martin Reinecke committed
979
template<bool fwd, typename T> void pass3 (size_t ido, size_t l1,
980
  const T * POCKETFFT_RESTRICT cc, T * POCKETFFT_RESTRICT ch,
Martin Reinecke's avatar
Martin Reinecke committed
981
  const cmplx<T0> * POCKETFFT_RESTRICT wa) const
982 983
  {
  constexpr size_t cdim=3;
Martin Reinecke's avatar
sync  
Martin Reinecke committed
984
  constexpr T0 tw1r=-0.5,
Martin Reinecke's avatar
Martin Reinecke committed
985
               tw1i= (fwd ? -1: 1) * T0(0.8660254037844386467637231707529362L);
986

987 988
  auto CH = [ch,ido,l1](size_t a, size_t b, size_t c) -> T&
    { return ch[a+ido*(b+l1*c)]; };
989
  auto CC = [cc,ido,cdim](size_t a, size_t b, size_t c) -> const T&
990 991 992 993
    { return cc[a+ido*(b+cdim*c)]; };
  auto WA = [wa, ido](size_t x, size_t i)
    { return wa[i-1+x*(ido-1)]; };

994 995 996
  if (ido==1)
    for (size_t k=0; k<l1; ++k)
      {
997 998
      POCKETFFT_PREP3(0)
      POCKETFFT_PARTSTEP3a(1,2,tw1r,tw1i)
999 1000 1001 1002 1003
      }
  else
    for (size_t k=0; k<l1; ++k)
      {
      {
1004 1005
      POCKETFFT_PREP3(0)
      POCKETFFT_PARTSTEP3a(1,2,tw1r,tw1i)
1006 1007 1008
      }
      for (size_t i=1; i<ido; ++i)
        {
1009 1010
        POCKETFFT_PREP3(i)
        POCKETFFT_PARTSTEP3b(1,2,tw1r,tw1i)
1011 1012 1013 1014
        }
      }
  }

1015 1016 1017
#undef POCKETFFT_PARTSTEP3b
#undef POCKETFFT_PARTSTEP3a
#undef POCKETFFT_PREP3
1018

Martin Reinecke's avatar
Martin Reinecke committed
1019
template<bool fwd, typename T> void pass4 (size_t ido, size_t l1,
1020
  const T * POCKETFFT_RESTRICT cc, T * POCKETFFT_RESTRICT ch,
Martin Reinecke's avatar
Martin Reinecke committed
1021
  const cmplx<T0> * POCKETFFT_RESTRICT wa) const
1022 1023 1024
  {
  constexpr size_t cdim=4;

1025 1026
  auto CH = [ch,ido,l1](size_t a, size_t b, size_t c) -> T&
    { return ch[a+ido*(b+l1*c)]; };
1027
  auto CC = [cc,ido,cdim](size_t a, size_t b, size_t c) -> const T&
1028 1029 1030 1031
    { return cc[a+ido*(b+cdim*c)]; };
  auto WA = [wa, ido](size_t x, size_t i)
    { return wa[i-1+x*(ido-1)]; };

1032 1033 1034 1035
  if (ido==1)
    for (size_t k=0; k<l1; ++k)
      {
      T t1, t2, t3, t4;
Martin Reinecke's avatar
Martin Reinecke committed
1036 1037
      PM(t2,t1,CC(0,0,k),CC(0,2,k));
      PM(t3,t4,CC(0,1,k),CC(0,3,k));
Martin Reinecke's avatar
Martin Reinecke committed
1038
      ROTX90<fwd>(t4);
Martin Reinecke's avatar
Martin Reinecke committed
1039 1040
      PM(CH(0,k,0),CH(0,k,2),t2,t3);
      PM(CH(0,k,1),CH(0,k,3),t1,t4);
1041 1042 1043 1044 1045 1046
      }
  else
    for (size_t k=0; k<l1; ++k)
      {
      {
      T t1, t2, t3, t4;
Martin Reinecke's avatar
Martin Reinecke committed
1047 1048
      PM(t2,t1,CC(0,0,k),CC(0,2,k));
      PM(t3,t4,CC(0,1,k),CC(0,3,k));
Martin Reinecke's avatar
Martin Reinecke committed
1049
      ROTX90<fwd>(t4);
Martin Reinecke's avatar
Martin Reinecke committed
1050 1051
      PM(CH(0,k,0),CH(0,k,2),t2,t3);
      PM(CH(0,k,1),CH(0,k,3),t1,t4);
1052 1053 1054
      }
      for (size_t i=1; i<ido; ++i)
        {
Martin Reinecke's avatar
Martin Reinecke committed
1055
        T t1, t2, t3, t4;
1056
        T cc0=CC(i,0,k), cc1=CC(i,1,k),cc2=CC(i,2,k),cc3=CC(i,3,k);
Martin Reinecke's avatar
Martin Reinecke committed
1057 1058
        PM(t2,t1,cc0,cc2);
        PM(t3,t4,cc1,cc3);
Martin Reinecke's avatar
Martin Reinecke committed
1059
        ROTX90<fwd>(t4);
Martin Reinecke's avatar
Martin Reinecke committed
1060 1061 1062 1063
        CH(i,k,0) = t2+t3;
        special_mul<fwd>(t1+t4,WA(0,i),CH(i,k,1));
        special_mul<fwd>(t2-t3,WA(1,i),CH(i,k,2));
        special_mul<fwd>(t1-t4,WA(2,i),CH(i,k,3));
1064 1065 1066 1067
        }
      }
  }

1068
#define POCKETFFT_PREP5(idx) \
1069
        T t0 = CC(idx,0,k), t1, t2, t3, t4; \
Martin Reinecke's avatar
Martin Reinecke committed
1070 1071
        PM (t1,t4,CC(idx,1,k),CC(idx,4,k)); \
        PM (t2,t3,CC(idx,2,k),CC(idx,3,k)); \
1072 1073 1074
        CH(idx,k,0).r=t0.r+t1.r+t2.r; \
        CH(idx,k,0).i=t0.i+t1.i+t2.i;

1075
#define POCKETFFT_PARTSTEP5a(u1,u2,twar,twbr,twai,twbi) \
1076 1077 1078 1079 1080 1081
        { \
        T ca,cb; \
        ca.r=t0.r+twar*t1.r+twbr*t2.r; \
        ca.i=t0.i+twar*t1.i+twbr*t2.i; \
        cb.i=twai*t4.r twbi*t3.r; \
        cb.r=-(twai*t4.i twbi*t3.i); \
Martin Reinecke's avatar
Martin Reinecke committed
1082
        PM(CH(0,k,u1),CH(0,k,u2),ca,cb); \
1083 1084
        }

1085
#define POCKETFFT_PARTSTEP5b(u1,u2,twar,twbr,twai,twbi) \
1086 1087 1088 1089 1090 1091
        { \
        T ca,cb,da,db; \
        ca.r=t0.r+twar*t1.r+twbr*t2.r; \
        ca.i=t0.i+twar*t1.i+twbr*t2.i; \
        cb.i=twai*t4.r twbi*t3.r; \
        cb.r=-(twai*t4.i twbi*t3.i); \
Martin Reinecke's avatar
Martin Reinecke committed
1092 1093
        special_mul<fwd>(ca+cb,WA(u1-1,i),CH(i,k,u1)); \
        special_mul<fwd>(ca-cb,WA(u2-1,i),CH(i,k,u2)); \
1094
        }
Martin Reinecke's avatar
Martin Reinecke committed
1095
template<bool fwd, typename T> void pass5 (size_t ido, size_t l1,
1096
  const T * POCKETFFT_RESTRICT cc, T * POCKETFFT_RESTRICT ch,
Martin Reinecke's avatar
Martin Reinecke committed
1097
  const cmplx<T0> * POCKETFFT_RESTRICT wa) const
1098 1099
  {
  constexpr size_t cdim=5;
Martin Reinecke's avatar
sync  
Martin Reinecke committed
1100
  constexpr T0 tw1r= T0(0.3090169943749474241022934171828191L),
Martin Reinecke's avatar
Martin Reinecke committed
1101
               tw1i= (fwd ? -1: 1) * T0(0.9510565162951535721164393333793821L),
Martin Reinecke's avatar
sync  
Martin Reinecke committed
1102
               tw2r= T0(-0.8090169943749474241022934171828191L),
Martin Reinecke's avatar
Martin Reinecke committed
1103
               tw2i= (fwd ? -1: 1) * T0(0.5877852522924731291687059546390728L);
1104

1105 1106
  auto CH = [ch,ido,l1](size_t a, size_t b, size_t c) -> T&
    { return ch[a+ido*(b+l1*c)]; };
1107
  auto CC = [cc,ido,cdim](size_t a, size_t b, size_t c) -> const T&
1108 1109 1110 1111
    { return cc[a+ido*(b+cdim*c)]; };
  auto WA = [wa, ido](size_t x, size_t i)
    { return wa[i-1+x*(ido-1)]; };

1112 1113 1114
  if (ido==1)
    for (size_t k=0; k<l1; ++k)
      {
1115 1116 1117
      POCKETFFT_PREP5(0)
      POCKETFFT_PARTSTEP5a(1,4,tw1r,tw2r,+tw1i,+tw2i)
      POCKETFFT_PARTSTEP5a(2,3,tw2r,tw1r,+tw2i,-tw1i)
1118 1119 1120 1121 1122
      }
  else
    for (size_t k=0; k<l1; ++k)
      {
      {
1123 1124 1125
      POCKETFFT_PREP5(0)
      POCKETFFT_PARTSTEP5a(1,4,tw1r,tw2r,+tw1i,+tw2i)
      POCKETFFT_PARTSTEP5a(2,3,tw2r,tw1r,+tw2i,-tw1i)
1126 1127 1128
      }
      for (size_t i=1; i<ido; ++i)
        {
1129 1130 1131
        POCKETFFT_PREP5(i)
        POCKETFFT_PARTSTEP5b(1,4,tw1r,tw2r,+tw1i,+tw2i)
        POCKETFFT_PARTSTEP5b(2,3,tw2r,tw1r,+tw2i,-tw1i)
1132 1133 1134 1135
        }
      }
  }

1136 1137 1138
#undef POCKETFFT_PARTSTEP5b
#undef POCKETFFT_PARTSTEP5a
#undef POCKETFFT_PREP5
1139

1140
#define POCKETFFT_PREP7(idx) \