summaryrefslogtreecommitdiff
path: root/src/fftw3/rdft/hc2hc.c
blob: 926273c953a0735eb476ac7af79f0f5223c69905 (plain)
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
/*
 * Copyright (c) 2003 Matteo Frigo
 * Copyright (c) 2003 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/* $Id: hc2hc.c,v 1.1 2008/10/17 06:11:29 scuri Exp $ */

/* generic Cooley-Tukey routines */
#include "rdft.h"
#include "hc2hc.h"

static void destroy(plan *ego_)
{
     plan_hc2hc *ego = (plan_hc2hc *) ego_;

     X(plan_destroy_internal)(ego->cld);
     X(plan_destroy_internal)(ego->cld0);
     X(plan_destroy_internal)(ego->cldm);
     X(stride_destroy)(ego->ios);
     X(stride_destroy)(ego->vs);
}

static void awake(plan *ego_, int flg)
{
     plan_hc2hc *ego = (plan_hc2hc *) ego_;

     AWAKE(ego->cld, flg);
     AWAKE(ego->cld0, flg);
     AWAKE(ego->cldm, flg);

     if (flg) {
	  const tw_instr *tw = ego->slv->desc->tw;
	  X(mktwiddle)(&ego->td, tw, ego->n, ego->r, (ego->m + 1) / 2);
	  /* 0th twiddle is handled by cld0: */
	  ego->W = ego->td->W + X(twiddle_length)(ego->r, tw);
     } else {
	  X(twiddle_destroy)(&ego->td);
          ego->W = 0;
     }
}

static void print(const plan *ego_, printer *p)
{
     const plan_hc2hc *ego = (const plan_hc2hc *) ego_;
     const solver_hc2hc *slv = ego->slv;
     const hc2hc_desc *e = slv->desc;

     p->print(p, "(%s-%d/%d%v \"%s\"%(%p%)%(%p%)%(%p%))",
              slv->nam, ego->r, X(twiddle_length)(ego->r, e->tw),
	      ego->vl, e->nam, ego->cld0, ego->cldm, ego->cld);
}

#define divides(a, b) (((int)(b) % (int)(a)) == 0)

int X(rdft_hc2hc_applicable)(const solver_hc2hc *ego, const problem *p_)
{
     if (RDFTP(p_)) {
          const problem_rdft *p = (const problem_rdft *) p_;
          const hc2hc_desc *d = ego->desc;
          return (1
                  && p->sz->rnk == 1
                  && p->vecsz->rnk <= 1
		  && p->kind[0] == d->genus->kind
                  && divides(d->radix, p->sz->dims[0].n)
		  && d->radix < p->sz->dims[0].n /* avoid inf. loops in cld0 */
	       );
     }
     return 0;
}


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

plan *X(mkplan_rdft_hc2hc)(const solver_hc2hc *ego,
			   const problem *p_,
			   planner *plnr,
			   const hc2hcadt *adt)
{
     plan_hc2hc *pln;
     plan *cld = 0, *cld0 = 0, *cldm = 0;
     int n, r, m;
     problem *cldp = 0, *cld0p = 0, *cldmp = 0;
     iodim *d;
     const problem_rdft *p;
     const hc2hc_desc *e = ego->desc;

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

     p = (const problem_rdft *) p_;
     d = p->sz->dims;
     n = d[0].n;
     r = e->radix;
     m = n / r;

     adt->mkcldrn(ego, p, &cldp, &cld0p, &cldmp);

     cld = X(mkplan_d)(plnr, cldp); cldp = 0;
     if (!cld) goto nada;

     cld0 = X(mkplan_d)(plnr, cld0p); cld0p = 0;
     if (!cld0) goto nada;

     cldm = X(mkplan_d)(plnr, cldmp); cldmp = 0;
     if (!cldm) goto nada;

     A(adt->pln_size >= sizeof(plan_hc2hc));
     pln = (plan_hc2hc *) X(mkplan_rdft)(adt->pln_size, &padt, adt->apply);

     pln->slv = ego;
     pln->cld = cld;
     pln->cld0 = cld0;
     pln->cldm = cldm;
     pln->k = ego->k;
     pln->n = n;
     pln->r = r;
     pln->m = m;

     pln->is = d[0].is;
     pln->os = d[0].os;

     pln->ios = pln->vs = 0;
     X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
     pln->td = 0;
     adt->finish(pln);

     return &(pln->super.super);

 nada:
     X(problem_destroy)(cldmp);
     X(problem_destroy)(cld0p);
     X(problem_destroy)(cldp);
     X(plan_destroy_internal)(cldm);
     X(plan_destroy_internal)(cld0);
     X(plan_destroy_internal)(cld);
     return (plan *) 0;
}

solver *X(mksolver_rdft_hc2hc)(khc2hc k, const hc2hc_desc *desc,
			       const char *nam, const solver_adt *adt)
{
     solver_hc2hc *slv;

     slv = MKSOLVER(solver_hc2hc, adt);

     slv->desc = desc;
     slv->k = k;
     slv->nam = nam;
     return &(slv->super);
}

/* routines to create children are shared by many solvers */

void X(rdft_mkcldrn_dit)(const solver_hc2hc *ego, const problem_rdft *p,
                         problem **cldp, problem **cld0p, problem **cldmp)
{
     iodim *d = p->sz->dims;
     const hc2hc_desc *e = ego->desc;
     int m = d[0].n / e->radix;
     int omid = d[0].os * (m/2);

     tensor *null, *radix = X(mktensor_1d)(e->radix, d[0].is, m * d[0].os);
     tensor *cld_vec = X(tensor_append)(radix, p->vecsz);
     X(tensor_destroy)(radix);
     A(p->kind[0] == R2HC);

     *cldp = X(mkproblem_rdft_d)(X(mktensor_1d)(m, e->radix*d[0].is, d[0].os),
				 cld_vec, p->I, p->O, p->kind);

     radix = X(mktensor_1d)(e->radix, m * d[0].os, m * d[0].os);
     null = X(mktensor_0d)();
     *cld0p = X(mkproblem_rdft_1)(radix, null, p->O, p->O, R2HC);
     *cldmp = X(mkproblem_rdft_1)(m%2 ? null : radix, null,
				  p->O + omid, p->O + omid, R2HCII);
     X(tensor_destroy2)(null, radix);
}


void X(rdft_mkcldrn_dif)(const solver_hc2hc *ego, const problem_rdft *p,
                         problem **cldp, problem **cld0p, problem **cldmp)
{
     iodim *d = p->sz->dims;
     const hc2hc_desc *e = ego->desc;
     int m = d[0].n / e->radix;
     int imid = d[0].is * (m/2);

     tensor *null, *radix = X(mktensor_1d)(e->radix, m * d[0].is, d[0].os);
     tensor *cld_vec = X(tensor_append)(radix, p->vecsz);
     X(tensor_destroy)(radix);
     A(p->kind[0] == HC2R);

     *cldp = X(mkproblem_rdft_d)(X(mktensor_1d)(m, d[0].is, e->radix*d[0].os),
				 cld_vec, p->I, p->O, p->kind);

     radix = X(mktensor_1d)(e->radix, m * d[0].is, m * d[0].is);
     null = X(mktensor_0d)();
     *cld0p = X(mkproblem_rdft_1)(radix, null, p->I, p->I, HC2R);
     *cldmp = X(mkproblem_rdft_1)(m%2 ? null : radix, null, 
				  p->I + imid, p->I + imid, HC2RIII);
     X(tensor_destroy2)(null, radix);
}