fft.h 32.2 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
/*
This file is part of pocketfft.

Martin Reinecke's avatar
Martin Reinecke committed
4
Copyright (C) 2010-2020 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
5
6
7
8
9
10
11
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
38
39
40
41
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

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.
*/

#ifndef MRUTIL_FFT_H
#define MRUTIL_FFT_H

42
#include "mr_util/math/fft1d.h"
Martin Reinecke's avatar
Martin Reinecke committed
43

Martin Reinecke's avatar
Martin Reinecke committed
44
45
46
47
48
49
50
51
52
53
#ifndef POCKETFFT_CACHE_SIZE
#define POCKETFFT_CACHE_SIZE 16
#endif

#include <cmath>
#include <cstdlib>
#include <stdexcept>
#include <memory>
#include <vector>
#include <complex>
54
#include <algorithm>
Martin Reinecke's avatar
Martin Reinecke committed
55
56
57
#if POCKETFFT_CACHE_SIZE!=0
#include <array>
#endif
58
59
60
#include "mr_util/infra/threading.h"
#include "mr_util/infra/simd.h"
#include "mr_util/infra/mav.h"
Martin Reinecke's avatar
Martin Reinecke committed
61
62
63
#ifndef MRUTIL_NO_THREADING
#include <mutex>
#endif
Martin Reinecke's avatar
Martin Reinecke committed
64
65
66
67
68
69
70
71
72
73

namespace mr {

namespace detail_fft {

constexpr bool FORWARD  = true,
               BACKWARD = false;

struct util // hack to avoid duplicate symbols
  {
Martin Reinecke's avatar
Martin Reinecke committed
74
  static void sanity_check_axes(size_t ndim, const shape_t &axes)
Martin Reinecke's avatar
Martin Reinecke committed
75
76
    {
    shape_t tmp(ndim,0);
Martin Reinecke's avatar
Martin Reinecke committed
77
    if (axes.empty()) throw std::invalid_argument("no axes specified");
Martin Reinecke's avatar
Martin Reinecke committed
78
79
    for (auto ax : axes)
      {
Martin Reinecke's avatar
Martin Reinecke committed
80
81
      if (ax>=ndim) throw std::invalid_argument("bad axis number");
      if (++tmp[ax]>1) throw std::invalid_argument("axis specified repeatedly");
Martin Reinecke's avatar
Martin Reinecke committed
82
83
84
      }
    }

Martin Reinecke's avatar
Martin Reinecke committed
85
86
  static MRUTIL_NOINLINE void sanity_check_onetype(const fmav_info &a1,
    const fmav_info &a2, bool inplace, const shape_t &axes)
Martin Reinecke's avatar
Martin Reinecke committed
87
    {
Martin Reinecke's avatar
Martin Reinecke committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    sanity_check_axes(a1.ndim(), axes);
    MR_assert(a1.conformable(a2), "array sizes are not conformable");
    if (inplace) MR_assert(a1.stride()==a2.stride(), "stride mismatch");
    }
  static MRUTIL_NOINLINE void sanity_check_cr(const fmav_info &ac,
    const fmav_info &ar, const shape_t &axes)
    {
    sanity_check_axes(ac.ndim(), axes);
    MR_assert(ac.ndim()==ar.ndim(), "dimension mismatch");
    for (size_t i=0; i<ac.ndim(); ++i)
      MR_assert(ac.shape(i)== (i==axes.back()) ? (ar.shape(i)/2+1) : ar.shape(i),
        "axis length mismatch");
    }
  static MRUTIL_NOINLINE void sanity_check_cr(const fmav_info &ac,
    const fmav_info &ar, const size_t axis)
    {
    if (axis>=ac.ndim()) throw std::invalid_argument("bad axis number");
    MR_assert(ac.ndim()==ar.ndim(), "dimension mismatch");
    for (size_t i=0; i<ac.ndim(); ++i)
      MR_assert(ac.shape(i)== (i==axis) ? (ar.shape(i)/2+1) : ar.shape(i),
        "axis length mismatch");
Martin Reinecke's avatar
Martin Reinecke committed
109
110
111
    }

#ifdef MRUTIL_NO_THREADING
Martin Reinecke's avatar
Martin Reinecke committed
112
  static size_t thread_count (size_t /*nthreads*/, const fmav_info &/*info*/,
Martin Reinecke's avatar
Martin Reinecke committed
113
114
115
    size_t /*axis*/, size_t /*vlen*/)
    { return 1; }
#else
Martin Reinecke's avatar
Martin Reinecke committed
116
  static size_t thread_count (size_t nthreads, const fmav_info &info,
Martin Reinecke's avatar
Martin Reinecke committed
117
118
119
    size_t axis, size_t vlen)
    {
    if (nthreads==1) return 1;
Martin Reinecke's avatar
Martin Reinecke committed
120
121
122
    size_t size = info.size();
    size_t parallel = size / (info.shape(axis) * vlen);
    if (info.shape(axis) < 1000)
Martin Reinecke's avatar
Martin Reinecke committed
123
      parallel /= 4;
Martin Reinecke's avatar
Martin Reinecke committed
124
    size_t max_threads = (nthreads==0) ? mr::max_threads() : nthreads;
125
    return std::max(size_t(1), std::min(parallel, max_threads));
Martin Reinecke's avatar
Martin Reinecke committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    }
#endif
  };


//
// sine/cosine transforms
//

template<typename T0> class T_dct1
  {
  private:
    pocketfft_r<T0> fftplan;

  public:
Martin Reinecke's avatar
Martin Reinecke committed
141
    MRUTIL_NOINLINE T_dct1(size_t length)
Martin Reinecke's avatar
Martin Reinecke committed
142
143
      : fftplan(2*(length-1)) {}

Martin Reinecke's avatar
Martin Reinecke committed
144
    template<typename T> MRUTIL_NOINLINE void exec(T c[], T0 fct, bool ortho,
Martin Reinecke's avatar
Martin Reinecke committed
145
146
147
148
149
150
      int /*type*/, bool /*cosine*/) const
      {
      constexpr T0 sqrt2=T0(1.414213562373095048801688724209698L);
      size_t N=fftplan.length(), n=N/2+1;
      if (ortho)
        { c[0]*=sqrt2; c[n-1]*=sqrt2; }
151
      aligned_array<T> tmp(N);
Martin Reinecke's avatar
Martin Reinecke committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
      tmp[0] = c[0];
      for (size_t i=1; i<n; ++i)
        tmp[i] = tmp[N-i] = c[i];
      fftplan.exec(tmp.data(), fct, true);
      c[0] = tmp[0];
      for (size_t i=1; i<n; ++i)
        c[i] = tmp[2*i-1];
      if (ortho)
        { c[0]*=sqrt2*T0(0.5); c[n-1]*=sqrt2*T0(0.5); }
      }

    size_t length() const { return fftplan.length()/2+1; }
  };

template<typename T0> class T_dst1
  {
  private:
    pocketfft_r<T0> fftplan;

  public:
Martin Reinecke's avatar
Martin Reinecke committed
172
    MRUTIL_NOINLINE T_dst1(size_t length)
Martin Reinecke's avatar
Martin Reinecke committed
173
174
      : fftplan(2*(length+1)) {}

Martin Reinecke's avatar
Martin Reinecke committed
175
    template<typename T> MRUTIL_NOINLINE void exec(T c[], T0 fct,
Martin Reinecke's avatar
Martin Reinecke committed
176
177
178
      bool /*ortho*/, int /*type*/, bool /*cosine*/) const
      {
      size_t N=fftplan.length(), n=N/2-1;
179
      aligned_array<T> tmp(N);
Martin Reinecke's avatar
Martin Reinecke committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
      tmp[0] = tmp[n+1] = c[0]*0;
      for (size_t i=0; i<n; ++i)
        { tmp[i+1]=c[i]; tmp[N-1-i]=-c[i]; }
      fftplan.exec(tmp.data(), fct, true);
      for (size_t i=0; i<n; ++i)
        c[i] = -tmp[2*i+2];
      }

    size_t length() const { return fftplan.length()/2-1; }
  };

template<typename T0> class T_dcst23
  {
  private:
    pocketfft_r<T0> fftplan;
Martin Reinecke's avatar
Martin Reinecke committed
195
    std::vector<T0> twiddle;
Martin Reinecke's avatar
Martin Reinecke committed
196
197

  public:
Martin Reinecke's avatar
Martin Reinecke committed
198
    MRUTIL_NOINLINE T_dcst23(size_t length)
Martin Reinecke's avatar
Martin Reinecke committed
199
200
201
202
203
204
205
      : fftplan(length), twiddle(length)
      {
      UnityRoots<T0,Cmplx<T0>> tw(4*length);
      for (size_t i=0; i<length; ++i)
        twiddle[i] = tw[i+1].r;
      }

Martin Reinecke's avatar
Martin Reinecke committed
206
    template<typename T> MRUTIL_NOINLINE void exec(T c[], T0 fct, bool ortho,
Martin Reinecke's avatar
Martin Reinecke committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
      int type, bool cosine) const
      {
      constexpr T0 sqrt2=T0(1.414213562373095048801688724209698L);
      size_t N=length();
      size_t NS2 = (N+1)/2;
      if (type==2)
        {
        if (!cosine)
          for (size_t k=1; k<N; k+=2)
            c[k] = -c[k];
        c[0] *= 2;
        if ((N&1)==0) c[N-1]*=2;
        for (size_t k=1; k<N-1; k+=2)
          MPINPLACE(c[k+1], c[k]);
        fftplan.exec(c, fct, false);
        for (size_t k=1, kc=N-1; k<NS2; ++k, --kc)
          {
          T t1 = twiddle[k-1]*c[kc]+twiddle[kc-1]*c[k];
          T t2 = twiddle[k-1]*c[k]-twiddle[kc-1]*c[kc];
          c[k] = T0(0.5)*(t1+t2); c[kc]=T0(0.5)*(t1-t2);
          }
        if ((N&1)==0)
          c[NS2] *= twiddle[NS2-1];
        if (!cosine)
          for (size_t k=0, kc=N-1; k<kc; ++k, --kc)
Martin Reinecke's avatar
Martin Reinecke committed
232
            std::swap(c[k], c[kc]);
Martin Reinecke's avatar
Martin Reinecke committed
233
234
235
236
237
238
239
        if (ortho) c[0]*=sqrt2*T0(0.5);
        }
      else
        {
        if (ortho) c[0]*=sqrt2;
        if (!cosine)
          for (size_t k=0, kc=N-1; k<NS2; ++k, --kc)
Martin Reinecke's avatar
Martin Reinecke committed
240
            std::swap(c[k], c[kc]);
Martin Reinecke's avatar
Martin Reinecke committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
        for (size_t k=1, kc=N-1; k<NS2; ++k, --kc)
          {
          T t1=c[k]+c[kc], t2=c[k]-c[kc];
          c[k] = twiddle[k-1]*t2+twiddle[kc-1]*t1;
          c[kc]= twiddle[k-1]*t1-twiddle[kc-1]*t2;
          }
        if ((N&1)==0)
          c[NS2] *= 2*twiddle[NS2-1];
        fftplan.exec(c, fct, true);
        for (size_t k=1; k<N-1; k+=2)
          MPINPLACE(c[k], c[k+1]);
        if (!cosine)
          for (size_t k=1; k<N; k+=2)
            c[k] = -c[k];
        }
      }

    size_t length() const { return fftplan.length(); }
  };

template<typename T0> class T_dcst4
  {
  private:
    size_t N;
Martin Reinecke's avatar
Martin Reinecke committed
265
266
    std::unique_ptr<pocketfft_c<T0>> fft;
    std::unique_ptr<pocketfft_r<T0>> rfft;
267
    aligned_array<Cmplx<T0>> C2;
Martin Reinecke's avatar
Martin Reinecke committed
268
269

  public:
Martin Reinecke's avatar
Martin Reinecke committed
270
    MRUTIL_NOINLINE T_dcst4(size_t length)
Martin Reinecke's avatar
Martin Reinecke committed
271
272
273
274
275
276
277
278
279
280
281
282
283
      : N(length),
        fft((N&1) ? nullptr : new pocketfft_c<T0>(N/2)),
        rfft((N&1)? new pocketfft_r<T0>(N) : nullptr),
        C2((N&1) ? 0 : N/2)
      {
      if ((N&1)==0)
        {
        UnityRoots<T0,Cmplx<T0>> tw(16*N);
        for (size_t i=0; i<N/2; ++i)
          C2[i] = conj(tw[8*i+1]);
        }
      }

Martin Reinecke's avatar
Martin Reinecke committed
284
    template<typename T> MRUTIL_NOINLINE void exec(T c[], T0 fct,
Martin Reinecke's avatar
Martin Reinecke committed
285
286
287
288
289
      bool /*ortho*/, int /*type*/, bool cosine) const
      {
      size_t n2 = N/2;
      if (!cosine)
        for (size_t k=0, kc=N-1; k<n2; ++k, --kc)
Martin Reinecke's avatar
Martin Reinecke committed
290
          std::swap(c[k], c[kc]);
Martin Reinecke's avatar
Martin Reinecke committed
291
292
293
294
295
296
      if (N&1)
        {
        // The following code is derived from the FFTW3 function apply_re11()
        // and is released under the 3-clause BSD license with friendly
        // permission of Matteo Frigo and Steven G. Johnson.

297
        aligned_array<T> y(N);
Martin Reinecke's avatar
Martin Reinecke committed
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
        {
        size_t i=0, m=n2;
        for (; m<N; ++i, m+=4)
          y[i] = c[m];
        for (; m<2*N; ++i, m+=4)
          y[i] = -c[2*N-m-1];
        for (; m<3*N; ++i, m+=4)
          y[i] = -c[m-2*N];
        for (; m<4*N; ++i, m+=4)
          y[i] = c[4*N-m-1];
        for (; i<N; ++i, m+=4)
          y[i] = c[m-4*N];
        }
        rfft->exec(y.data(), fct, true);
        {
        auto SGN = [](size_t i)
           {
           constexpr T0 sqrt2=T0(1.414213562373095048801688724209698L);
           return (i&2) ? -sqrt2 : sqrt2;
           };
        c[n2] = y[0]*SGN(n2+1);
        size_t i=0, i1=1, k=1;
        for (; k<n2; ++i, ++i1, k+=2)
          {
          c[i    ] = y[2*k-1]*SGN(i1)     + y[2*k  ]*SGN(i);
          c[N -i1] = y[2*k-1]*SGN(N -i)   - y[2*k  ]*SGN(N -i1);
          c[n2-i1] = y[2*k+1]*SGN(n2-i)   - y[2*k+2]*SGN(n2-i1);
          c[n2+i1] = y[2*k+1]*SGN(n2+i+2) + y[2*k+2]*SGN(n2+i1);
          }
        if (k == n2)
          {
          c[i   ] = y[2*k-1]*SGN(i+1) + y[2*k]*SGN(i);
          c[N-i1] = y[2*k-1]*SGN(i+2) + y[2*k]*SGN(i1);
          }
        }

        // FFTW-derived code ends here
        }
      else
        {
        // even length algorithm from
        // https://www.appletonaudio.com/blog/2013/derivation-of-fast-dct-4-algorithm-based-on-dft/
340
        aligned_array<Cmplx<T>> y(n2);
Martin Reinecke's avatar
Martin Reinecke committed
341
342
343
344
345
346
347
348
        for(size_t i=0; i<n2; ++i)
          {
          y[i].Set(c[2*i],c[N-1-2*i]);
          y[i] *= C2[i];
          }
        fft->exec(y.data(), fct, true);
        for(size_t i=0, ic=n2-1; i<n2; ++i, --ic)
          {
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
349
350
          c[2*i  ] = T0( 2)*(y[i ].r*C2[i ].r-y[i ].i*C2[i ].i);
          c[2*i+1] = T0(-2)*(y[ic].i*C2[ic].r+y[ic].r*C2[ic].i);
Martin Reinecke's avatar
Martin Reinecke committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
          }
        }
      if (!cosine)
        for (size_t k=1; k<N; k+=2)
          c[k] = -c[k];
      }

    size_t length() const { return N; }
  };


//
// multi-D infrastructure
//

Martin Reinecke's avatar
Martin Reinecke committed
366
template<typename T> std::shared_ptr<T> get_plan(size_t length)
Martin Reinecke's avatar
Martin Reinecke committed
367
368
  {
#if POCKETFFT_CACHE_SIZE==0
Martin Reinecke's avatar
Martin Reinecke committed
369
  return std::make_shared<T>(length);
Martin Reinecke's avatar
Martin Reinecke committed
370
371
#else
  constexpr size_t nmax=POCKETFFT_CACHE_SIZE;
Martin Reinecke's avatar
Martin Reinecke committed
372
373
  static std::array<std::shared_ptr<T>, nmax> cache;
  static std::array<size_t, nmax> last_access{{0}};
Martin Reinecke's avatar
Martin Reinecke committed
374
375
  static size_t access_counter = 0;

Martin Reinecke's avatar
Martin Reinecke committed
376
  auto find_in_cache = [&]() -> std::shared_ptr<T>
Martin Reinecke's avatar
Martin Reinecke committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
    {
    for (size_t i=0; i<nmax; ++i)
      if (cache[i] && (cache[i]->length()==length))
        {
        // no need to update if this is already the most recent entry
        if (last_access[i]!=access_counter)
          {
          last_access[i] = ++access_counter;
          // Guard against overflow
          if (access_counter == 0)
            last_access.fill(0);
          }
        return cache[i];
        }

    return nullptr;
    };
Martin Reinecke's avatar
Martin Reinecke committed
394
#ifdef MRUTIL_NO_THREADING
Martin Reinecke's avatar
Martin Reinecke committed
395
396
  auto p = find_in_cache();
  if (p) return p;
Martin Reinecke's avatar
Martin Reinecke committed
397
  auto plan = std::make_shared<T>(length);
Martin Reinecke's avatar
Martin Reinecke committed
398
399
400
401
402
403
404
405
406
  size_t lru = 0;
  for (size_t i=1; i<nmax; ++i)
    if (last_access[i] < last_access[lru])
      lru = i;

  cache[lru] = plan;
  last_access[lru] = ++access_counter;
  return plan;
#else
Martin Reinecke's avatar
Martin Reinecke committed
407
  static std::mutex mut;
Martin Reinecke's avatar
Martin Reinecke committed
408
409

  {
Martin Reinecke's avatar
Martin Reinecke committed
410
  std::lock_guard<std::mutex> lock(mut);
Martin Reinecke's avatar
Martin Reinecke committed
411
412
413
  auto p = find_in_cache();
  if (p) return p;
  }
Martin Reinecke's avatar
Martin Reinecke committed
414
  auto plan = std::make_shared<T>(length);
Martin Reinecke's avatar
Martin Reinecke committed
415
  {
Martin Reinecke's avatar
Martin Reinecke committed
416
  std::lock_guard<std::mutex> lock(mut);
Martin Reinecke's avatar
Martin Reinecke committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
  auto p = find_in_cache();
  if (p) return p;

  size_t lru = 0;
  for (size_t i=1; i<nmax; ++i)
    if (last_access[i] < last_access[lru])
      lru = i;

  cache[lru] = plan;
  last_access[lru] = ++access_counter;
  }
  return plan;
#endif
#endif
  }

template<size_t N> class multi_iter
  {
  private:
    shape_t pos;
Martin Reinecke's avatar
Martin Reinecke committed
437
    fmav_info iarr, oarr;
Martin Reinecke's avatar
Martin Reinecke committed
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
    ptrdiff_t p_ii, p_i[N], str_i, p_oi, p_o[N], str_o;
    size_t idim, rem;

    void advance_i()
      {
      for (int i_=int(pos.size())-1; i_>=0; --i_)
        {
        auto i = size_t(i_);
        if (i==idim) continue;
        p_ii += iarr.stride(i);
        p_oi += oarr.stride(i);
        if (++pos[i] < iarr.shape(i))
          return;
        pos[i] = 0;
        p_ii -= ptrdiff_t(iarr.shape(i))*iarr.stride(i);
        p_oi -= ptrdiff_t(oarr.shape(i))*oarr.stride(i);
        }
      }

  public:
Martin Reinecke's avatar
Martin Reinecke committed
458
    multi_iter(const fmav_info &iarr_, const fmav_info &oarr_, size_t idim_,
Martin Reinecke's avatar
rework    
Martin Reinecke committed
459
        size_t nshares, size_t myshare)
Martin Reinecke's avatar
Martin Reinecke committed
460
461
462
463
464
      : pos(iarr_.ndim(), 0), iarr(iarr_), oarr(oarr_), p_ii(0),
        str_i(iarr.stride(idim_)), p_oi(0), str_o(oarr.stride(idim_)),
        idim(idim_), rem(iarr.size()/iarr.shape(idim))
      {
      if (nshares==1) return;
Martin Reinecke's avatar
Martin Reinecke committed
465
466
      if (nshares==0) throw std::runtime_error("can't run with zero threads");
      if (myshare>=nshares) throw std::runtime_error("impossible share requested");
Martin Reinecke's avatar
Martin Reinecke committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
      size_t nbase = rem/nshares;
      size_t additional = rem%nshares;
      size_t lo = myshare*nbase + ((myshare<additional) ? myshare : additional);
      size_t hi = lo+nbase+(myshare<additional);
      size_t todo = hi-lo;

      size_t chunk = rem;
      for (size_t i=0; i<pos.size(); ++i)
        {
        if (i==idim) continue;
        chunk /= iarr.shape(i);
        size_t n_advance = lo/chunk;
        pos[i] += n_advance;
        p_ii += ptrdiff_t(n_advance)*iarr.stride(i);
        p_oi += ptrdiff_t(n_advance)*oarr.stride(i);
        lo -= n_advance*chunk;
        }
      rem = todo;
      }
    void advance(size_t n)
      {
Martin Reinecke's avatar
Martin Reinecke committed
488
      if (rem<n) throw std::runtime_error("underrun");
Martin Reinecke's avatar
Martin Reinecke committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
      for (size_t i=0; i<n; ++i)
        {
        p_i[i] = p_ii;
        p_o[i] = p_oi;
        advance_i();
        }
      rem -= n;
      }
    ptrdiff_t iofs(size_t i) const { return p_i[0] + ptrdiff_t(i)*str_i; }
    ptrdiff_t iofs(size_t j, size_t i) const { return p_i[j] + ptrdiff_t(i)*str_i; }
    ptrdiff_t oofs(size_t i) const { return p_o[0] + ptrdiff_t(i)*str_o; }
    ptrdiff_t oofs(size_t j, size_t i) const { return p_o[j] + ptrdiff_t(i)*str_o; }
    size_t length_in() const { return iarr.shape(idim); }
    size_t length_out() const { return oarr.shape(idim); }
    ptrdiff_t stride_in() const { return str_i; }
    ptrdiff_t stride_out() const { return str_o; }
    size_t remaining() const { return rem; }
  };

class simple_iter
  {
  private:
    shape_t pos;
Martin Reinecke's avatar
Martin Reinecke committed
512
    fmav_info arr;
Martin Reinecke's avatar
Martin Reinecke committed
513
514
515
516
    ptrdiff_t p;
    size_t rem;

  public:
Martin Reinecke's avatar
Martin Reinecke committed
517
    simple_iter(const fmav_info &arr_)
Martin Reinecke's avatar
Martin Reinecke committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
      : pos(arr_.ndim(), 0), arr(arr_), p(0), rem(arr_.size()) {}
    void advance()
      {
      --rem;
      for (int i_=int(pos.size())-1; i_>=0; --i_)
        {
        auto i = size_t(i_);
        p += arr.stride(i);
        if (++pos[i] < arr.shape(i))
          return;
        pos[i] = 0;
        p -= ptrdiff_t(arr.shape(i))*arr.stride(i);
        }
      }
    ptrdiff_t ofs() const { return p; }
    size_t remaining() const { return rem; }
  };

class rev_iter
  {
  private:
    shape_t pos;
Martin Reinecke's avatar
Martin Reinecke committed
540
    fmav_info arr;
Martin Reinecke's avatar
Martin Reinecke committed
541
542
    std::vector<char> rev_axis;
    std::vector<char> rev_jump;
Martin Reinecke's avatar
Martin Reinecke committed
543
544
545
546
547
548
    size_t last_axis, last_size;
    shape_t shp;
    ptrdiff_t p, rp;
    size_t rem;

  public:
Martin Reinecke's avatar
Martin Reinecke committed
549
    rev_iter(const fmav_info &arr_, const shape_t &axes)
Martin Reinecke's avatar
Martin Reinecke committed
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
      : pos(arr_.ndim(), 0), arr(arr_), rev_axis(arr_.ndim(), 0),
        rev_jump(arr_.ndim(), 1), p(0), rp(0)
      {
      for (auto ax: axes)
        rev_axis[ax]=1;
      last_axis = axes.back();
      last_size = arr.shape(last_axis)/2 + 1;
      shp = arr.shape();
      shp[last_axis] = last_size;
      rem=1;
      for (auto i: shp)
        rem *= i;
      }
    void advance()
      {
      --rem;
      for (int i_=int(pos.size())-1; i_>=0; --i_)
        {
        auto i = size_t(i_);
        p += arr.stride(i);
        if (!rev_axis[i])
          rp += arr.stride(i);
        else
          {
          rp -= arr.stride(i);
          if (rev_jump[i])
            {
            rp += ptrdiff_t(arr.shape(i))*arr.stride(i);
            rev_jump[i] = 0;
            }
          }
        if (++pos[i] < shp[i])
          return;
        pos[i] = 0;
        p -= ptrdiff_t(shp[i])*arr.stride(i);
        if (rev_axis[i])
          {
          rp -= ptrdiff_t(arr.shape(i)-shp[i])*arr.stride(i);
          rev_jump[i] = 1;
          }
        else
          rp -= ptrdiff_t(shp[i])*arr.stride(i);
        }
      }
    ptrdiff_t ofs() const { return p; }
    ptrdiff_t rev_ofs() const { return rp; }
    size_t remaining() const { return rem; }
  };

Martin Reinecke's avatar
Martin Reinecke committed
599
600
template<typename T, typename T0> aligned_array<T> alloc_tmp
  (const fmav_info &info, size_t axsize)
Martin Reinecke's avatar
Martin Reinecke committed
601
  {
Martin Reinecke's avatar
Martin Reinecke committed
602
603
604
  auto othersize = info.size()/axsize;
  constexpr auto vlen = native_simd<T0>::size();
  auto tmpsize = axsize*((othersize>=vlen) ? vlen : 1);
Martin Reinecke's avatar
Martin Reinecke committed
605
  return aligned_array<T>(tmpsize);
Martin Reinecke's avatar
Martin Reinecke committed
606
607
608
  }

template <typename T, size_t vlen> void copy_input(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
609
  const fmav<Cmplx<T>> &src, Cmplx<native_simd<T>> *MRUTIL_RESTRICT dst)
Martin Reinecke's avatar
Martin Reinecke committed
610
611
612
613
  {
  for (size_t i=0; i<it.length_in(); ++i)
    for (size_t j=0; j<vlen; ++j)
      {
614
615
      dst[i].r[j] = src[it.iofs(j,i)].r;
      dst[i].i[j] = src[it.iofs(j,i)].i;
Martin Reinecke's avatar
Martin Reinecke committed
616
617
618
619
      }
  }

template <typename T, size_t vlen> void copy_input(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
620
  const fmav<T> &src, native_simd<T> *MRUTIL_RESTRICT dst)
Martin Reinecke's avatar
Martin Reinecke committed
621
622
623
624
625
626
627
  {
  for (size_t i=0; i<it.length_in(); ++i)
    for (size_t j=0; j<vlen; ++j)
      dst[i][j] = src[it.iofs(j,i)];
  }

template <typename T, size_t vlen> void copy_input(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
628
  const fmav<T> &src, T *MRUTIL_RESTRICT dst)
Martin Reinecke's avatar
Martin Reinecke committed
629
630
631
632
633
634
635
  {
  if (dst == &src[it.iofs(0)]) return;  // in-place
  for (size_t i=0; i<it.length_in(); ++i)
    dst[i] = src[it.iofs(i)];
  }

template<typename T, size_t vlen> void copy_output(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
636
  const Cmplx<native_simd<T>> *MRUTIL_RESTRICT src, fmav<Cmplx<T>> &dst)
Martin Reinecke's avatar
Martin Reinecke committed
637
  {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
638
  auto ptr=dst.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
639
640
  for (size_t i=0; i<it.length_out(); ++i)
    for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
641
      ptr[it.oofs(j,i)].Set(src[i].r[j],src[i].i[j]);
Martin Reinecke's avatar
Martin Reinecke committed
642
643
644
  }

template<typename T, size_t vlen> void copy_output(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
645
  const native_simd<T> *MRUTIL_RESTRICT src, fmav<T> &dst)
Martin Reinecke's avatar
Martin Reinecke committed
646
  {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
647
  auto ptr=dst.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
648
649
  for (size_t i=0; i<it.length_out(); ++i)
    for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
650
      ptr[it.oofs(j,i)] = src[i][j];
Martin Reinecke's avatar
Martin Reinecke committed
651
652
653
  }

template<typename T, size_t vlen> void copy_output(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
654
  const T *MRUTIL_RESTRICT src, fmav<T> &dst)
Martin Reinecke's avatar
Martin Reinecke committed
655
  {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
656
  auto ptr=dst.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
657
658
  if (src == &dst[it.oofs(0)]) return;  // in-place
  for (size_t i=0; i<it.length_out(); ++i)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
659
    ptr[it.oofs(i)] = src[i];
Martin Reinecke's avatar
Martin Reinecke committed
660
661
  }

Martin Reinecke's avatar
Martin Reinecke committed
662
template <typename T> struct add_vec { using type = native_simd<T>; };
Martin Reinecke's avatar
Martin Reinecke committed
663
template <typename T> struct add_vec<Cmplx<T>>
Martin Reinecke's avatar
Martin Reinecke committed
664
  { using type = Cmplx<native_simd<T>>; };
Martin Reinecke's avatar
Martin Reinecke committed
665
666
667
template <typename T> using add_vec_t = typename add_vec<T>::type;

template<typename Tplan, typename T, typename T0, typename Exec>
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
668
MRUTIL_NOINLINE void general_nd(const fmav<T> &in, fmav<T> &out,
Martin Reinecke's avatar
Martin Reinecke committed
669
670
671
  const shape_t &axes, T0 fct, size_t nthreads, const Exec & exec,
  const bool allow_inplace=true)
  {
Martin Reinecke's avatar
Martin Reinecke committed
672
  std::shared_ptr<Tplan> plan;
Martin Reinecke's avatar
Martin Reinecke committed
673
674
675
676
677
678
679
680

  for (size_t iax=0; iax<axes.size(); ++iax)
    {
    size_t len=in.shape(axes[iax]);
    if ((!plan) || (len!=plan->length()))
      plan = get_plan<Tplan>(len);

    execParallel(
Martin Reinecke's avatar
Martin Reinecke committed
681
      util::thread_count(nthreads, in, axes[iax], native_simd<T0>::size()),
Martin Reinecke's avatar
rework    
Martin Reinecke committed
682
      [&](Scheduler &sched) {
Martin Reinecke's avatar
Martin Reinecke committed
683
684
        constexpr auto vlen = native_simd<T0>::size();
        auto storage = alloc_tmp<T,T0>(in, len);
Martin Reinecke's avatar
Martin Reinecke committed
685
        const auto &tin(iax==0? in : out);
Martin Reinecke's avatar
rework    
Martin Reinecke committed
686
        multi_iter<vlen> it(tin, out, axes[iax], sched.num_threads(), sched.thread_num());
Martin Reinecke's avatar
Martin Reinecke committed
687
#ifndef MRUTIL_NO_SIMD
Martin Reinecke's avatar
Martin Reinecke committed
688
689
690
691
692
693
694
695
696
697
698
        if (vlen>1)
          while (it.remaining()>=vlen)
            {
            it.advance(vlen);
            auto tdatav = reinterpret_cast<add_vec_t<T> *>(storage.data());
            exec(it, tin, out, tdatav, *plan, fct);
            }
#endif
        while (it.remaining()>0)
          {
          it.advance(1);
Martin Reinecke's avatar
Martin Reinecke committed
699
          auto buf = allow_inplace && it.stride_out() == 1 ?
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
700
            &out.vraw(it.oofs(0)) : reinterpret_cast<T *>(storage.data());
Martin Reinecke's avatar
Martin Reinecke committed
701
702
703
704
705
706
707
708
709
710
711
          exec(it, tin, out, buf, *plan, fct);
          }
      });  // end of parallel region
    fct = T0(1); // factor has been applied, use 1 for remaining axes
    }
  }

struct ExecC2C
  {
  bool forward;

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
712
713
714
  template <typename T0, typename T, size_t vlen> void operator() (
    const multi_iter<vlen> &it, const fmav<Cmplx<T0>> &in,
    fmav<Cmplx<T0>> &out, T *buf, const pocketfft_c<T0> &plan, T0 fct) const
Martin Reinecke's avatar
Martin Reinecke committed
715
716
717
718
719
720
721
722
    {
    copy_input(it, in, buf);
    plan.exec(buf, fct, forward);
    copy_output(it, buf, out);
    }
  };

template <typename T, size_t vlen> void copy_hartley(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
723
  const native_simd<T> *MRUTIL_RESTRICT src, fmav<T> &dst)
Martin Reinecke's avatar
Martin Reinecke committed
724
  {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
725
  auto ptr = dst.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
726
  for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
727
    ptr[it.oofs(j,0)] = src[0][j];
Martin Reinecke's avatar
Martin Reinecke committed
728
729
730
731
  size_t i=1, i1=1, i2=it.length_out()-1;
  for (i=1; i<it.length_out()-1; i+=2, ++i1, --i2)
    for (size_t j=0; j<vlen; ++j)
      {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
732
733
      ptr[it.oofs(j,i1)] = src[i][j]+src[i+1][j];
      ptr[it.oofs(j,i2)] = src[i][j]-src[i+1][j];
Martin Reinecke's avatar
Martin Reinecke committed
734
735
736
      }
  if (i<it.length_out())
    for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
737
      ptr[it.oofs(j,i1)] = src[i][j];
Martin Reinecke's avatar
Martin Reinecke committed
738
739
740
  }

template <typename T, size_t vlen> void copy_hartley(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
741
  const T *MRUTIL_RESTRICT src, fmav<T> &dst)
Martin Reinecke's avatar
Martin Reinecke committed
742
  {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
743
744
  auto ptr = dst.vdata();
  ptr[it.oofs(0)] = src[0];
Martin Reinecke's avatar
Martin Reinecke committed
745
746
747
  size_t i=1, i1=1, i2=it.length_out()-1;
  for (i=1; i<it.length_out()-1; i+=2, ++i1, --i2)
    {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
748
749
    ptr[it.oofs(i1)] = src[i]+src[i+1];
    ptr[it.oofs(i2)] = src[i]-src[i+1];
Martin Reinecke's avatar
Martin Reinecke committed
750
751
    }
  if (i<it.length_out())
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
752
    ptr[it.oofs(i1)] = src[i];
Martin Reinecke's avatar
Martin Reinecke committed
753
754
755
756
757
  }

struct ExecHartley
  {
  template <typename T0, typename T, size_t vlen> void operator () (
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
758
    const multi_iter<vlen> &it, const fmav<T0> &in, fmav<T0> &out,
Martin Reinecke's avatar
Martin Reinecke committed
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
    T * buf, const pocketfft_r<T0> &plan, T0 fct) const
    {
    copy_input(it, in, buf);
    plan.exec(buf, fct, true);
    copy_hartley(it, buf, out);
    }
  };

struct ExecDcst
  {
  bool ortho;
  int type;
  bool cosine;

  template <typename T0, typename T, typename Tplan, size_t vlen>
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
774
775
  void operator () (const multi_iter<vlen> &it, const fmav<T0> &in,
    fmav <T0> &out, T * buf, const Tplan &plan, T0 fct) const
Martin Reinecke's avatar
Martin Reinecke committed
776
777
778
779
780
781
782
    {
    copy_input(it, in, buf);
    plan.exec(buf, fct, ortho, type, cosine);
    copy_output(it, buf, out);
    }
  };

Martin Reinecke's avatar
Martin Reinecke committed
783
template<typename T> MRUTIL_NOINLINE void general_r2c(
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
784
  const fmav<T> &in, fmav<Cmplx<T>> &out, size_t axis, bool forward, T fct,
Martin Reinecke's avatar
Martin Reinecke committed
785
786
787
788
789
  size_t nthreads)
  {
  auto plan = get_plan<pocketfft_r<T>>(in.shape(axis));
  size_t len=in.shape(axis);
  execParallel(
Martin Reinecke's avatar
Martin Reinecke committed
790
    util::thread_count(nthreads, in, axis, native_simd<T>::size()),
Martin Reinecke's avatar
rework    
Martin Reinecke committed
791
    [&](Scheduler &sched) {
Martin Reinecke's avatar
Martin Reinecke committed
792
793
    constexpr auto vlen = native_simd<T>::size();
    auto storage = alloc_tmp<T,T>(in, len);
Martin Reinecke's avatar
rework    
Martin Reinecke committed
794
    multi_iter<vlen> it(in, out, axis, sched.num_threads(), sched.thread_num());
Martin Reinecke's avatar
Martin Reinecke committed
795
#ifndef MRUTIL_NO_SIMD
Martin Reinecke's avatar
Martin Reinecke committed
796
797
798
799
    if (vlen>1)
      while (it.remaining()>=vlen)
        {
        it.advance(vlen);
Martin Reinecke's avatar
Martin Reinecke committed
800
        auto tdatav = reinterpret_cast<native_simd<T> *>(storage.data());
Martin Reinecke's avatar
Martin Reinecke committed
801
802
        copy_input(it, in, tdatav);
        plan->exec(tdatav, fct, true);
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
803
        auto vout = out.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
804
        for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
805
          vout[it.oofs(j,0)].Set(tdatav[0][j]);
Martin Reinecke's avatar
Martin Reinecke committed
806
807
808
809
        size_t i=1, ii=1;
        if (forward)
          for (; i<len-1; i+=2, ++ii)
            for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
810
              vout[it.oofs(j,ii)].Set(tdatav[i][j], tdatav[i+1][j]);
Martin Reinecke's avatar
Martin Reinecke committed
811
812
813
        else
          for (; i<len-1; i+=2, ++ii)
            for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
814
              vout[it.oofs(j,ii)].Set(tdatav[i][j], -tdatav[i+1][j]);
Martin Reinecke's avatar
Martin Reinecke committed
815
816
        if (i<len)
          for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
817
            vout[it.oofs(j,ii)].Set(tdatav[i][j]);
Martin Reinecke's avatar
Martin Reinecke committed
818
819
820
821
822
823
824
825
        }
#endif
    while (it.remaining()>0)
      {
      it.advance(1);
      auto tdata = reinterpret_cast<T *>(storage.data());
      copy_input(it, in, tdata);
      plan->exec(tdata, fct, true);
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
826
827
      auto vout = out.vdata();
      vout[it.oofs(0)].Set(tdata[0]);
Martin Reinecke's avatar
Martin Reinecke committed
828
829
830
      size_t i=1, ii=1;
      if (forward)
        for (; i<len-1; i+=2, ++ii)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
831
          vout[it.oofs(ii)].Set(tdata[i], tdata[i+1]);
Martin Reinecke's avatar
Martin Reinecke committed
832
833
      else
        for (; i<len-1; i+=2, ++ii)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
834
          vout[it.oofs(ii)].Set(tdata[i], -tdata[i+1]);
Martin Reinecke's avatar
Martin Reinecke committed
835
      if (i<len)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
836
        vout[it.oofs(ii)].Set(tdata[i]);
Martin Reinecke's avatar
Martin Reinecke committed
837
838
839
      }
    });  // end of parallel region
  }
Martin Reinecke's avatar
Martin Reinecke committed
840
template<typename T> MRUTIL_NOINLINE void general_c2r(
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
841
  const fmav<Cmplx<T>> &in, fmav<T> &out, size_t axis, bool forward, T fct,
Martin Reinecke's avatar
Martin Reinecke committed
842
843
844
845
846
  size_t nthreads)
  {
  auto plan = get_plan<pocketfft_r<T>>(out.shape(axis));
  size_t len=out.shape(axis);
  execParallel(
Martin Reinecke's avatar
Martin Reinecke committed
847
    util::thread_count(nthreads, in, axis, native_simd<T>::size()),
Martin Reinecke's avatar
rework    
Martin Reinecke committed
848
    [&](Scheduler &sched) {
Martin Reinecke's avatar
Martin Reinecke committed
849
850
      constexpr auto vlen = native_simd<T>::size();
      auto storage = alloc_tmp<T,T>(out, len);
Martin Reinecke's avatar
rework    
Martin Reinecke committed
851
      multi_iter<vlen> it(in, out, axis, sched.num_threads(), sched.thread_num());
Martin Reinecke's avatar
Martin Reinecke committed
852
#ifndef MRUTIL_NO_SIMD
Martin Reinecke's avatar
Martin Reinecke committed
853
854
855
856
      if (vlen>1)
        while (it.remaining()>=vlen)
          {
          it.advance(vlen);
Martin Reinecke's avatar
Martin Reinecke committed
857
          auto tdatav = reinterpret_cast<native_simd<T> *>(storage.data());
Martin Reinecke's avatar
Martin Reinecke committed
858
859
860
861
862
863
864
          for (size_t j=0; j<vlen; ++j)
            tdatav[0][j]=in[it.iofs(j,0)].r;
          {
          size_t i=1, ii=1;
          if (forward)
            for (; i<len-1; i+=2, ++ii)
              for (size_t j=0; j<vlen; ++j)
865
866
867
868
                {
                tdatav[i  ][j] =  in[it.iofs(j,ii)].r;
                tdatav[i+1][j] = -in[it.iofs(j,ii)].i;
                }
Martin Reinecke's avatar
Martin Reinecke committed
869
870
871
          else
            for (; i<len-1; i+=2, ++ii)
              for (size_t j=0; j<vlen; ++j)
872
873
874
875
                {
                tdatav[i  ][j] = in[it.iofs(j,ii)].r;
                tdatav[i+1][j] = in[it.iofs(j,ii)].i;
                }
Martin Reinecke's avatar
Martin Reinecke committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
          if (i<len)
            for (size_t j=0; j<vlen; ++j)
              tdatav[i][j] = in[it.iofs(j,ii)].r;
          }
          plan->exec(tdatav, fct, false);
          copy_output(it, tdatav, out);
          }
#endif
      while (it.remaining()>0)
        {
        it.advance(1);
        auto tdata = reinterpret_cast<T *>(storage.data());
        tdata[0]=in[it.iofs(0)].r;
        {
        size_t i=1, ii=1;
        if (forward)
          for (; i<len-1; i+=2, ++ii)
893
894
895
896
            {
            tdata[i  ] =  in[it.iofs(ii)].r;
            tdata[i+1] = -in[it.iofs(ii)].i;
            }
Martin Reinecke's avatar
Martin Reinecke committed
897
898
        else
          for (; i<len-1; i+=2, ++ii)
899
900
901
902
            {
            tdata[i  ] = in[it.iofs(ii)].r;
            tdata[i+1] = in[it.iofs(ii)].i;
            }
Martin Reinecke's avatar
Martin Reinecke committed
903
904
905
906
907
908
909
910
911
912
913
914
915
916
        if (i<len)
          tdata[i] = in[it.iofs(ii)].r;
        }
        plan->exec(tdata, fct, false);
        copy_output(it, tdata, out);
        }
    });  // end of parallel region
  }

struct ExecR2R
  {
  bool r2c, forward;

  template <typename T0, typename T, size_t vlen> void operator () (
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
917
    const multi_iter<vlen> &it, const fmav<T0> &in, fmav<T0> &out, T * buf,
Martin Reinecke's avatar
Martin Reinecke committed
918
919
920
921
922
923
924
925
926
927
928
929
930
931
    const pocketfft_r<T0> &plan, T0 fct) const
    {
    copy_input(it, in, buf);
    if ((!r2c) && forward)
      for (size_t i=2; i<it.length_out(); i+=2)
        buf[i] = -buf[i];
    plan.exec(buf, fct, forward);
    if (r2c && (!forward))
      for (size_t i=2; i<it.length_out(); i+=2)
        buf[i] = -buf[i];
    copy_output(it, buf, out);
    }
  };

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
932
933
template<typename T> void c2c(const fmav<std::complex<T>> &in,
  fmav<std::complex<T>> &out, const shape_t &axes, bool forward,
Martin Reinecke's avatar
Martin Reinecke committed
934
  T fct, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
935
  {
Martin Reinecke's avatar
Martin Reinecke committed
936
937
  util::sanity_check_onetype(in, out, in.data()==out.data(), axes);
  if (in.size()==0) return;
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
938
  fmav<Cmplx<T>> in2(reinterpret_cast<const Cmplx<T> *>(in.data()), in);
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
939
  fmav<Cmplx<T>> out2(reinterpret_cast<Cmplx<T> *>(out.vdata()), out, out.writable());
Martin Reinecke's avatar
Martin Reinecke committed
940
  general_nd<pocketfft_c<T>>(in2, out2, axes, fct, nthreads, ExecC2C{forward});
Martin Reinecke's avatar
Martin Reinecke committed
941
942
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
943
template<typename T> void dct(const fmav<T> &in, fmav<T> &out,
Martin Reinecke's avatar
Martin Reinecke committed
944
  const shape_t &axes, int type, T fct, bool ortho, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
945
  {
Martin Reinecke's avatar
Martin Reinecke committed
946
  if ((type<1) || (type>4)) throw std::invalid_argument("invalid DCT type");
Martin Reinecke's avatar
Martin Reinecke committed
947
948
  util::sanity_check_onetype(in, out, in.data()==out.data(), axes);
  if (in.size()==0) return;
Martin Reinecke's avatar
Martin Reinecke committed
949
950
  const ExecDcst exec{ortho, type, true};
  if (type==1)
Martin Reinecke's avatar
Martin Reinecke committed
951
    general_nd<T_dct1<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
952
  else if (type==4)
Martin Reinecke's avatar
Martin Reinecke committed
953
    general_nd<T_dcst4<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
954
  else
Martin Reinecke's avatar
Martin Reinecke committed
955
    general_nd<T_dcst23<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
956
957
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
958
template<typename T> void dst(const fmav<T> &in, fmav<T> &out,
Martin Reinecke's avatar
Martin Reinecke committed
959
  const shape_t &axes, int type, T fct, bool ortho, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
960
  {
Martin Reinecke's avatar
Martin Reinecke committed
961
  if ((type<1) || (type>4)) throw std::invalid_argument("invalid DST type");
Martin Reinecke's avatar
Martin Reinecke committed
962
  util::sanity_check_onetype(in, out, in.data()==out.data(), axes);
Martin Reinecke's avatar
Martin Reinecke committed
963
964
  const ExecDcst exec{ortho, type, false};
  if (type==1)
Martin Reinecke's avatar
Martin Reinecke committed
965
    general_nd<T_dst1<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
966
  else if (type==4)
Martin Reinecke's avatar
Martin Reinecke committed
967
    general_nd<T_dcst4<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
968
  else
Martin Reinecke's avatar
Martin Reinecke committed
969
    general_nd<T_dcst23<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
970
971
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
972
973
template<typename T> void r2c(const fmav<T> &in,
  fmav<std::complex<T>> &out, size_t axis, bool forward, T fct,
Martin Reinecke's avatar
Martin Reinecke committed
974
975
  size_t nthreads=1)
  {
Martin Reinecke's avatar
Martin Reinecke committed
976
977
  util::sanity_check_cr(out, in, axis);
  if (in.size()==0) return;
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
978
  fmav<Cmplx<T>> out2(reinterpret_cast<Cmplx<T> *>(out.vdata()), out, out.writable());
Martin Reinecke's avatar
Martin Reinecke committed
979
  general_r2c(in, out2, axis, forward, fct, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
980
981
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
982
983
template<typename T> void r2c(const fmav<T> &in,
  fmav<std::complex<T>> &out, const shape_t &axes,
Martin Reinecke's avatar
Martin Reinecke committed
984
  bool forward, T fct, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
985
  {
Martin Reinecke's avatar
Martin Reinecke committed
986
987
988
  util::sanity_check_cr(out, in, axes);
  if (in.size()==0) return;
  r2c(in, out, axes.back(), forward, fct, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
989
990
991
  if (axes.size()==1) return;

  auto newaxes = shape_t{axes.begin(), --axes.end()};
Martin Reinecke's avatar
Martin Reinecke committed
992
  c2c(out, out, newaxes, forward, T(1), nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
993
994
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
995
996
template<typename T> void c2r(const fmav<std::complex<T>> &in,
  fmav<T> &out,  size_t axis, bool forward, T fct, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
997
  {
Martin Reinecke's avatar
Martin Reinecke committed
998
999
  util::sanity_check_cr(in, out, axis);
  if (in.size()==0) return;
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1000
  fmav<Cmplx<T>> in2(reinterpret_cast<const Cmplx<T> *>(in.data()), in);
Martin Reinecke's avatar
Martin Reinecke committed
1001
  general_c2r(in2, out, axis, forward, fct, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
1002
1003
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1004
1005
template<typename T> void c2r(const fmav<std::complex<T>> &in,
  fmav<T> &out, const shape_t &axes, bool forward, T fct,
Martin Reinecke's avatar
Martin Reinecke committed
1006
1007
1008
  size_t nthreads=1)
  {
  if (axes.size()==1)
Martin Reinecke's avatar
Martin Reinecke committed
1009
1010
1011
    return c2r(in, out, axes[0], forward, fct, nthreads);
  util::sanity_check_cr(in, out, axes);
  if (in.size()==0) return;
1012
  fmav<std::complex<T>> atmp(in.shape());
1013
  auto newaxes = shape_t{axes.begin(), --axes.end()};
Martin Reinecke's avatar
Martin Reinecke committed
1014
1015
  c2c(in, atmp, newaxes, forward, T(1), nthreads);
  c2r(atmp, out, axes.back(), forward, fct, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
1016
1017
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1018
1019
template<typename T> void r2r_fftpack(const fmav<T> &in,
  fmav<T> &out, const shape_t &axes, bool real2hermitian, bool forward,
Martin Reinecke's avatar
Martin Reinecke committed
1020
  T fct, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
1021
  {
Martin Reinecke's avatar
Martin Reinecke committed
1022
1023
1024
  util::sanity_check_onetype(in, out, in.data()==out.data(), axes);
  if (in.size()==0) return;
  general_nd<pocketfft_r<T>>(in, out, axes, fct, nthreads,
Martin Reinecke's avatar
Martin Reinecke committed
1025
1026
1027
    ExecR2R{real2hermitian, forward});
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1028
1029
template<typename T> void r2r_separable_hartley(const fmav<T> &in,
  fmav<T> &out, const shape_t &axes, T fct, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
1030
  {
Martin Reinecke's avatar
Martin Reinecke committed
1031
1032
1033
  util::sanity_check_onetype(in, out, in.data()==out.data(), axes);
  if (in.size()==0) return;
  general_nd<pocketfft_r<T>>(in, out, axes, fct, nthreads, ExecHartley{},
Martin Reinecke's avatar
Martin Reinecke committed
1034
1035
1036
    false);
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1037
1038
template<typename T> void r2r_genuine_hartley(const fmav<T> &in,
  fmav<T> &out, const shape_t &axes, T fct, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
1039
1040
  {
  if (axes.size()==1)
Martin Reinecke's avatar
Martin Reinecke committed
1041
1042
1043
1044
    return r2r_separable_hartley(in, out, axes, fct, nthreads);
  util::sanity_check_onetype(in, out, in.data()==out.data(), axes);
  if (in.size()==0) return;
  shape_t tshp(in.shape());
Martin Reinecke's avatar
Martin Reinecke committed
1045
  tshp[axes.back()] = tshp[axes.back()]/2+1;
1046
  fmav<std::complex<T>> atmp(tshp);
Martin Reinecke's avatar
Martin Reinecke committed
1047
  r2c(in, atmp, axes, true, fct, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
1048
  simple_iter iin(atmp);
Martin Reinecke's avatar
Martin Reinecke committed
1049
  rev_iter iout(out, axes);
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
1050
  auto vout = out.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
1051
1052
1053
  while(iin.remaining()>0)
    {
    auto v = atmp[iin.ofs()];
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
1054
1055
    vout[iout.ofs()] = v.real()+v.imag();
    vout[iout.rev_ofs()] = v.real()-v.imag();
Martin Reinecke's avatar
Martin Reinecke committed
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
    iin.advance(); iout.advance();
    }
  }

} // namespace detail_fft

using detail_fft::FORWARD;
using detail_fft::BACKWARD;
using detail_fft::c2c;
using detail_fft::c2r;
using detail_fft::r2c;
using detail_fft::r2r_fftpack;
using detail_fft::r2r_separable_hartley;
using detail_fft::r2r_genuine_hartley;
using detail_fft::dct;
using detail_fft::dst;

} // namespace mr

#endif // POCKETFFT_HDRONLY_H