fft.h 32.7 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
  }
Martin Reinecke's avatar
Martin Reinecke committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// template<typename T, typename T0> aligned_array<T> alloc_tmp
//   (const fmav_info &info, const shape_t &axes)
//   {
//   size_t fullsize=info.size();
//   size_t tmpsize=0;
//   for (size_t i=0; i<axes.size(); ++i)
//     {
//     auto axsize = info.shape(axes[i]);
//     auto othersize = fullsize/axsize;
//     constexpr auto vlen = native_simd<T0>::size();
//     auto sz = axsize*((othersize>=vlen) ? vlen : 1);
//     if (sz>tmpsize) tmpsize=sz;
//     }
//   return aligned_array<T>(tmpsize);
//   }
Martin Reinecke's avatar
Martin Reinecke committed
622
623

template <typename T, size_t vlen> void copy_input(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
624
  const fmav<Cmplx<T>> &src, Cmplx<native_simd<T>> *MRUTIL_RESTRICT dst)
Martin Reinecke's avatar
Martin Reinecke committed
625
626
627
628
  {
  for (size_t i=0; i<it.length_in(); ++i)
    for (size_t j=0; j<vlen; ++j)
      {
629
630
      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
631
632
633
634
      }
  }

template <typename T, size_t vlen> void copy_input(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
635
  const fmav<T> &src, native_simd<T> *MRUTIL_RESTRICT dst)
Martin Reinecke's avatar
Martin Reinecke committed
636
637
638
639
640
641
642
  {
  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
643
  const fmav<T> &src, T *MRUTIL_RESTRICT dst)
Martin Reinecke's avatar
Martin Reinecke committed
644
645
646
647
648
649
650
  {
  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
651
  const Cmplx<native_simd<T>> *MRUTIL_RESTRICT src, fmav<Cmplx<T>> &dst)
Martin Reinecke's avatar
Martin Reinecke committed
652
  {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
653
  auto ptr=dst.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
654
655
  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
656
      ptr[it.oofs(j,i)].Set(src[i].r[j],src[i].i[j]);
Martin Reinecke's avatar
Martin Reinecke committed
657
658
659
  }

template<typename T, size_t vlen> void copy_output(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
660
  const native_simd<T> *MRUTIL_RESTRICT src, fmav<T> &dst)
Martin Reinecke's avatar
Martin Reinecke committed
661
  {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
662
  auto ptr=dst.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
663
664
  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
665
      ptr[it.oofs(j,i)] = src[i][j];
Martin Reinecke's avatar
Martin Reinecke committed
666
667
668
  }

template<typename T, size_t vlen> void copy_output(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
669
  const T *MRUTIL_RESTRICT src, fmav<T> &dst)
Martin Reinecke's avatar
Martin Reinecke committed
670
  {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
671
  auto ptr=dst.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
672
673
  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
674
    ptr[it.oofs(i)] = src[i];
Martin Reinecke's avatar
Martin Reinecke committed
675
676
  }

Martin Reinecke's avatar
Martin Reinecke committed
677
template <typename T> struct add_vec { using type = native_simd<T>; };
Martin Reinecke's avatar
Martin Reinecke committed
678
template <typename T> struct add_vec<Cmplx<T>>
Martin Reinecke's avatar
Martin Reinecke committed
679
  { using type = Cmplx<native_simd<T>>; };
Martin Reinecke's avatar
Martin Reinecke committed
680
681
682
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
683
MRUTIL_NOINLINE void general_nd(const fmav<T> &in, fmav<T> &out,
Martin Reinecke's avatar
Martin Reinecke committed
684
685
686
  const shape_t &axes, T0 fct, size_t nthreads, const Exec & exec,
  const bool allow_inplace=true)
  {
Martin Reinecke's avatar
Martin Reinecke committed
687
  std::shared_ptr<Tplan> plan;
Martin Reinecke's avatar
Martin Reinecke committed
688
689
690
691
692
693
694
695

  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
696
      util::thread_count(nthreads, in, axes[iax], native_simd<T0>::size()),
Martin Reinecke's avatar
rework    
Martin Reinecke committed
697
      [&](Scheduler &sched) {
Martin Reinecke's avatar
Martin Reinecke committed
698
699
        constexpr auto vlen = native_simd<T0>::size();
        auto storage = alloc_tmp<T,T0>(in, len);
Martin Reinecke's avatar
Martin Reinecke committed
700
        const auto &tin(iax==0? in : out);
Martin Reinecke's avatar
rework    
Martin Reinecke committed
701
        multi_iter<vlen> it(tin, out, axes[iax], sched.num_threads(), sched.thread_num());
Martin Reinecke's avatar
Martin Reinecke committed
702
#ifndef MRUTIL_NO_SIMD
Martin Reinecke's avatar
Martin Reinecke committed
703
704
705
706
707
708
709
710
711
712
713
        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
714
          auto buf = allow_inplace && it.stride_out() == 1 ?
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
715
            &out.vraw(it.oofs(0)) : reinterpret_cast<T *>(storage.data());
Martin Reinecke's avatar
Martin Reinecke committed
716
717
718
719
720
721
722
723
724
725
726
          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
727
728
729
  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
730
731
732
733
734
735
736
737
    {
    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
738
  const native_simd<T> *MRUTIL_RESTRICT src, fmav<T> &dst)
Martin Reinecke's avatar
Martin Reinecke committed
739
  {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
740
  auto ptr = dst.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
741
  for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
742
    ptr[it.oofs(j,0)] = src[0][j];
Martin Reinecke's avatar
Martin Reinecke committed
743
744
745
746
  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
747
748
      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
749
750
751
      }
  if (i<it.length_out())
    for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
752
      ptr[it.oofs(j,i1)] = src[i][j];
Martin Reinecke's avatar
Martin Reinecke committed
753
754
755
  }

template <typename T, size_t vlen> void copy_hartley(const multi_iter<vlen> &it,
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
756
  const T *MRUTIL_RESTRICT src, fmav<T> &dst)
Martin Reinecke's avatar
Martin Reinecke committed
757
  {
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
758
759
  auto ptr = dst.vdata();
  ptr[it.oofs(0)] = src[0];
Martin Reinecke's avatar
Martin Reinecke committed
760
761
762
  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
763
764
    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
765
766
    }
  if (i<it.length_out())
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
767
    ptr[it.oofs(i1)] = src[i];
Martin Reinecke's avatar
Martin Reinecke committed
768
769
770
771
772
  }

struct ExecHartley
  {
  template <typename T0, typename T, size_t vlen> void operator () (
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
773
    const multi_iter<vlen> &it, const fmav<T0> &in, fmav<T0> &out,
Martin Reinecke's avatar
Martin Reinecke committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
    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
789
790
  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
791
792
793
794
795
796
797
    {
    copy_input(it, in, buf);
    plan.exec(buf, fct, ortho, type, cosine);
    copy_output(it, buf, out);
    }
  };

Martin Reinecke's avatar
Martin Reinecke committed
798
template<typename T> MRUTIL_NOINLINE void general_r2c(
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
799
  const fmav<T> &in, fmav<Cmplx<T>> &out, size_t axis, bool forward, T fct,
Martin Reinecke's avatar
Martin Reinecke committed
800
801
802
803
804
  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
805
    util::thread_count(nthreads, in, axis, native_simd<T>::size()),
Martin Reinecke's avatar
rework    
Martin Reinecke committed
806
    [&](Scheduler &sched) {
Martin Reinecke's avatar
Martin Reinecke committed
807
808
    constexpr auto vlen = native_simd<T>::size();
    auto storage = alloc_tmp<T,T>(in, len);
Martin Reinecke's avatar
rework    
Martin Reinecke committed
809
    multi_iter<vlen> it(in, out, axis, sched.num_threads(), sched.thread_num());
Martin Reinecke's avatar
Martin Reinecke committed
810
#ifndef MRUTIL_NO_SIMD
Martin Reinecke's avatar
Martin Reinecke committed
811
812
813
814
    if (vlen>1)
      while (it.remaining()>=vlen)
        {
        it.advance(vlen);
Martin Reinecke's avatar
Martin Reinecke committed
815
        auto tdatav = reinterpret_cast<native_simd<T> *>(storage.data());
Martin Reinecke's avatar
Martin Reinecke committed
816
817
        copy_input(it, in, tdatav);
        plan->exec(tdatav, fct, true);
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
818
        auto vout = out.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
819
        for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
820
          vout[it.oofs(j,0)].Set(tdatav[0][j]);
Martin Reinecke's avatar
Martin Reinecke committed
821
822
823
824
        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
825
              vout[it.oofs(j,ii)].Set(tdatav[i][j], tdatav[i+1][j]);
Martin Reinecke's avatar
Martin Reinecke committed
826
827
828
        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
829
              vout[it.oofs(j,ii)].Set(tdatav[i][j], -tdatav[i+1][j]);
Martin Reinecke's avatar
Martin Reinecke committed
830
831
        if (i<len)
          for (size_t j=0; j<vlen; ++j)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
832
            vout[it.oofs(j,ii)].Set(tdatav[i][j]);
Martin Reinecke's avatar
Martin Reinecke committed
833
834
835
836
837
838
839
840
        }
#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
841
842
      auto vout = out.vdata();
      vout[it.oofs(0)].Set(tdata[0]);
Martin Reinecke's avatar
Martin Reinecke committed
843
844
845
      size_t i=1, ii=1;
      if (forward)
        for (; i<len-1; i+=2, ++ii)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
846
          vout[it.oofs(ii)].Set(tdata[i], tdata[i+1]);
Martin Reinecke's avatar
Martin Reinecke committed
847
848
      else
        for (; i<len-1; i+=2, ++ii)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
849
          vout[it.oofs(ii)].Set(tdata[i], -tdata[i+1]);
Martin Reinecke's avatar
Martin Reinecke committed
850
      if (i<len)
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
851
        vout[it.oofs(ii)].Set(tdata[i]);
Martin Reinecke's avatar
Martin Reinecke committed
852
853
854
      }
    });  // end of parallel region
  }
Martin Reinecke's avatar
Martin Reinecke committed
855
template<typename T> MRUTIL_NOINLINE void general_c2r(
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
856
  const fmav<Cmplx<T>> &in, fmav<T> &out, size_t axis, bool forward, T fct,
Martin Reinecke's avatar
Martin Reinecke committed
857
858
859
860
861
  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
862
    util::thread_count(nthreads, in, axis, native_simd<T>::size()),
Martin Reinecke's avatar
rework    
Martin Reinecke committed
863
    [&](Scheduler &sched) {
Martin Reinecke's avatar
Martin Reinecke committed
864
865
      constexpr auto vlen = native_simd<T>::size();
      auto storage = alloc_tmp<T,T>(out, len);
Martin Reinecke's avatar
rework    
Martin Reinecke committed
866
      multi_iter<vlen> it(in, out, axis, sched.num_threads(), sched.thread_num());
Martin Reinecke's avatar
Martin Reinecke committed
867
#ifndef MRUTIL_NO_SIMD
Martin Reinecke's avatar
Martin Reinecke committed
868
869
870
871
      if (vlen>1)
        while (it.remaining()>=vlen)
          {
          it.advance(vlen);
Martin Reinecke's avatar
Martin Reinecke committed
872
          auto tdatav = reinterpret_cast<native_simd<T> *>(storage.data());
Martin Reinecke's avatar
Martin Reinecke committed
873
874
875
876
877
878
879
          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)
880
881
882
883
                {
                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
884
885
886
          else
            for (; i<len-1; i+=2, ++ii)
              for (size_t j=0; j<vlen; ++j)
887
888
889
890
                {
                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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
          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)
908
909
910
911
            {
            tdata[i  ] =  in[it.iofs(ii)].r;
            tdata[i+1] = -in[it.iofs(ii)].i;
            }
Martin Reinecke's avatar
Martin Reinecke committed
912
913
        else
          for (; i<len-1; i+=2, ++ii)
914
915
916
917
            {
            tdata[i  ] = in[it.iofs(ii)].r;
            tdata[i+1] = in[it.iofs(ii)].i;
            }
Martin Reinecke's avatar
Martin Reinecke committed
918
919
920
921
922
923
924
925
926
927
928
929
930
931
        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
932
    const multi_iter<vlen> &it, const fmav<T0> &in, fmav<T0> &out, T * buf,
Martin Reinecke's avatar
Martin Reinecke committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
    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
947
948
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
949
  T fct, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
950
  {
Martin Reinecke's avatar
Martin Reinecke committed
951
952
  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
953
  fmav<Cmplx<T>> in2(reinterpret_cast<const Cmplx<T> *>(in.data()), in);
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
954
  fmav<Cmplx<T>> out2(reinterpret_cast<Cmplx<T> *>(out.vdata()), out, out.writable());
Martin Reinecke's avatar
Martin Reinecke committed
955
  general_nd<pocketfft_c<T>>(in2, out2, axes, fct, nthreads, ExecC2C{forward});
Martin Reinecke's avatar
Martin Reinecke committed
956
957
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
958
template<typename T> void dct(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 DCT type");
Martin Reinecke's avatar
Martin Reinecke committed
962
963
  util::sanity_check_onetype(in, out, in.data()==out.data(), axes);
  if (in.size()==0) return;
Martin Reinecke's avatar
Martin Reinecke committed
964
965
  const ExecDcst exec{ortho, type, true};
  if (type==1)
Martin Reinecke's avatar
Martin Reinecke committed
966
    general_nd<T_dct1<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
967
  else if (type==4)
Martin Reinecke's avatar
Martin Reinecke committed
968
    general_nd<T_dcst4<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
969
  else
Martin Reinecke's avatar
Martin Reinecke committed
970
    general_nd<T_dcst23<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
971
972
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
973
template<typename T> void dst(const fmav<T> &in, fmav<T> &out,
Martin Reinecke's avatar
Martin Reinecke committed
974
  const shape_t &axes, int type, T fct, bool ortho, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
975
  {
Martin Reinecke's avatar
Martin Reinecke committed
976
  if ((type<1) || (type>4)) throw std::invalid_argument("invalid DST type");
Martin Reinecke's avatar
Martin Reinecke committed
977
  util::sanity_check_onetype(in, out, in.data()==out.data(), axes);
Martin Reinecke's avatar
Martin Reinecke committed
978
979
  const ExecDcst exec{ortho, type, false};
  if (type==1)
Martin Reinecke's avatar
Martin Reinecke committed
980
    general_nd<T_dst1<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
981
  else if (type==4)
Martin Reinecke's avatar
Martin Reinecke committed
982
    general_nd<T_dcst4<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
983
  else
Martin Reinecke's avatar
Martin Reinecke committed
984
    general_nd<T_dcst23<T>>(in, out, axes, fct, nthreads, exec);
Martin Reinecke's avatar
Martin Reinecke committed
985
986
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
987
988
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
989
990
  size_t nthreads=1)
  {
Martin Reinecke's avatar
Martin Reinecke committed
991
992
  util::sanity_check_cr(out, in, axis);
  if (in.size()==0) return;
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
993
  fmav<Cmplx<T>> out2(reinterpret_cast<Cmplx<T> *>(out.vdata()), out, out.writable());
Martin Reinecke's avatar
Martin Reinecke committed
994
  general_r2c(in, out2, axis, forward, fct, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
995
996
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
997
998
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
999
  bool forward, T fct, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
1000
  {
Martin Reinecke's avatar
Martin Reinecke committed
1001
1002
1003
  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
1004
1005
1006
  if (axes.size()==1) return;

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

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1010
1011
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
1012
  {
Martin Reinecke's avatar
Martin Reinecke committed
1013
1014
  util::sanity_check_cr(in, out, axis);
  if (in.size()==0) return;
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1015
  fmav<Cmplx<T>> in2(reinterpret_cast<const Cmplx<T> *>(in.data()), in);
Martin Reinecke's avatar
Martin Reinecke committed
1016
  general_c2r(in2, out, axis, forward, fct, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
1017
1018
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1019
1020
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
1021
1022
1023
  size_t nthreads=1)
  {
  if (axes.size()==1)
Martin Reinecke's avatar
Martin Reinecke committed
1024
1025
1026
    return c2r(in, out, axes[0], forward, fct, nthreads);
  util::sanity_check_cr(in, out, axes);
  if (in.size()==0) return;
1027
  fmav<std::complex<T>> atmp(in.shape());
1028
  auto newaxes = shape_t{axes.begin(), --axes.end()};
Martin Reinecke's avatar
Martin Reinecke committed
1029
1030
  c2c(in, atmp, newaxes, forward, T(1), nthreads);
  c2r(atmp, out, axes.back(), forward, fct, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
1031
1032
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1033
1034
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
1035
  T fct, size_t nthreads=1)
Martin Reinecke's avatar
Martin Reinecke committed
1036
  {
Martin Reinecke's avatar
Martin Reinecke committed
1037
1038
1039
  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
1040
1041
1042
    ExecR2R{real2hermitian, forward});
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1043
1044
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
1045
  {
Martin Reinecke's avatar
Martin Reinecke committed
1046
1047
1048
  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
1049
1050
1051
    false);
  }

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
1052
1053
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
1054
1055
  {
  if (axes.size()==1)
Martin Reinecke's avatar
Martin Reinecke committed
1056
1057
1058
1059
    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
1060
  tshp[axes.back()] = tshp[axes.back()]/2+1;
1061
  fmav<std::complex<T>> atmp(tshp);
Martin Reinecke's avatar
Martin Reinecke committed
1062
  r2c(in, atmp, axes, true, fct, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
1063
  simple_iter iin(atmp);
Martin Reinecke's avatar
Martin Reinecke committed
1064
  rev_iter iout(out, axes);
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
1065
  auto vout = out.vdata();
Martin Reinecke's avatar
Martin Reinecke committed
1066
1067
1068
  while(iin.remaining()>0)
    {
    auto v = atmp[iin.ofs()];
Martin Reinecke's avatar
stage 2    
Martin Reinecke committed
1069
1070
    vout[iout.ofs()] = v.real()+v.imag();
    vout[iout.rev_ofs()] = v.real()-v.imag();
Martin Reinecke's avatar
Martin Reinecke committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
    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