File: generic.c

package info (click to toggle)
fftw3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 28,428 kB
  • sloc: ansic: 259,592; ml: 5,474; sh: 4,442; perl: 1,648; makefile: 1,156; fortran: 110
file content (232 lines) | stat: -rw-r--r-- 5,553 bytes parent folder | download
1
2
3
4
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (c) 2003, 2007-14 Matteo Frigo
 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "rdft/rdft.h"

typedef struct {
     solver super;
     rdft_kind kind;
} S;

typedef struct {
     plan_rdft super;
     twid *td;
     INT n, is, os;
     rdft_kind kind;
} P;

/***************************************************************************/

static void cdot_r2hc(INT n, const E *x, const R *w, R *or0, R *oi1)
{
     INT i;

     E rr = x[0], ri = 0;
     x += 1;
     for (i = 1; i + i < n; ++i) {
	  rr += x[0] * w[0];
	  ri += x[1] * w[1];
	  x += 2; w += 2;
     }
     *or0 = rr;
     *oi1 = ri;
}

static void hartley_r2hc(INT n, const R *xr, INT xs, E *o, R *pr)
{
     INT i;
     E sr;
     o[0] = sr = xr[0]; o += 1;
     for (i = 1; i + i < n; ++i) {
	  R a, b;
	  a = xr[i * xs];
	  b =  xr[(n - i) * xs];
	  sr += (o[0] = a + b);
#if FFT_SIGN == -1
	  o[1] = b - a;
#else
	  o[1] = a - b;
#endif
	  o += 2;
     }
     *pr = sr;
}
		    
static void apply_r2hc(const plan *ego_, R *I, R *O)
{
     const P *ego = (const P *) ego_;
     INT i;
     INT n = ego->n, is = ego->is, os = ego->os;
     const R *W = ego->td->W;
     E *buf;
     size_t bufsz = n * sizeof(E);

     BUF_ALLOC(E *, buf, bufsz);
     hartley_r2hc(n, I, is, buf, O);

     for (i = 1; i + i < n; ++i) {
	  cdot_r2hc(n, buf, W, O + i * os, O + (n - i) * os);
	  W += n - 1;
     }

     BUF_FREE(buf, bufsz);
}


static void cdot_hc2r(INT n, const E *x, const R *w, R *or0, R *or1)
{
     INT i;

     E rr = x[0], ii = 0; 
     x += 1;
     for (i = 1; i + i < n; ++i) {
	  rr += x[0] * w[0];
	  ii += x[1] * w[1];
	  x += 2; w += 2;
     }
#if FFT_SIGN == -1
     *or0 = rr - ii;
     *or1 = rr + ii;
#else
     *or0 = rr + ii;
     *or1 = rr - ii;
#endif
}

static void hartley_hc2r(INT n, const R *x, INT xs, E *o, R *pr)
{
     INT i;
     E sr;

     o[0] = sr = x[0]; o += 1;
     for (i = 1; i + i < n; ++i) {
	  sr += (o[0] = x[i * xs] + x[i * xs]);
	  o[1] = x[(n - i) * xs] + x[(n - i) * xs];
	  o += 2;
     }
     *pr = sr;
}

static void apply_hc2r(const plan *ego_, R *I, R *O)		    
{
     const P *ego = (const P *) ego_;
     INT i;
     INT n = ego->n, is = ego->is, os = ego->os;
     const R *W = ego->td->W;
     E *buf;
     size_t bufsz = n * sizeof(E);

     BUF_ALLOC(E *, buf, bufsz);
     hartley_hc2r(n, I, is, buf, O);

     for (i = 1; i + i < n; ++i) {
	  cdot_hc2r(n, buf, W, O + i * os, O + (n - i) * os);
	  W += n - 1;
     }

     BUF_FREE(buf, bufsz);
}


/***************************************************************************/

static void awake(plan *ego_, enum wakefulness wakefulness)
{
     P *ego = (P *) ego_;
     static const tw_instr half_tw[] = {
	  { TW_HALF, 1, 0 },
	  { TW_NEXT, 1, 0 }
     };

     X(twiddle_awake)(wakefulness, &ego->td, half_tw, ego->n, ego->n,
		      (ego->n - 1) / 2);
}

static void print(const plan *ego_, printer *p)
{
     const P *ego = (const P *) ego_;

     p->print(p, "(rdft-generic-%s-%D)", 
	      ego->kind == R2HC ? "r2hc" : "hc2r", 
	      ego->n);
}

static int applicable(const S *ego, const problem *p_, 
		      const planner *plnr)
{
     const problem_rdft *p = (const problem_rdft *) p_;
     return (1
	     && p->sz->rnk == 1
	     && p->vecsz->rnk == 0
	     && (p->sz->dims[0].n % 2) == 1 
	     && CIMPLIES(NO_LARGE_GENERICP(plnr), p->sz->dims[0].n < GENERIC_MIN_BAD)
	     && CIMPLIES(NO_SLOWP(plnr), p->sz->dims[0].n > GENERIC_MAX_SLOW)
	     && X(is_prime)(p->sz->dims[0].n)
	     && p->kind[0] == ego->kind
	  );
}

static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
{
     const S *ego = (const S *)ego_;
     const problem_rdft *p;
     P *pln;
     INT n;

     static const plan_adt padt = {
	  X(rdft_solve), awake, print, X(plan_null_destroy)
     };

     if (!applicable(ego, p_, plnr))
          return (plan *)0;

     p = (const problem_rdft *) p_;
     pln = MKPLAN_RDFT(P, &padt, 
		       R2HC_KINDP(p->kind[0]) ? apply_r2hc : apply_hc2r);

     pln->n = n = p->sz->dims[0].n;
     pln->is = p->sz->dims[0].is;
     pln->os = p->sz->dims[0].os;
     pln->td = 0;
     pln->kind = ego->kind;

     pln->super.super.ops.add = (n-1) * 2.5;
     pln->super.super.ops.mul = 0;
     pln->super.super.ops.fma = 0.5 * (n-1) * (n-1) ;
#if 0 /* these are nice pipelined sequential loads and should cost nothing */
     pln->super.super.ops.other = (n-1)*(2 + 1 + (n-1));  /* approximate */
#endif

     return &(pln->super.super);
}

static solver *mksolver(rdft_kind kind)
{
     static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
     S *slv = MKSOLVER(S, &sadt);
     slv->kind = kind;
     return &(slv->super);
}

void X(rdft_generic_register)(planner *p)
{
     REGISTER_SOLVER(p, mksolver(R2HC));
     REGISTER_SOLVER(p, mksolver(HC2R));
}