summaryrefslogtreecommitdiff
path: root/src/fftw3/rdft/rdft-dht.c
blob: f384fefeb5a3fabec9e701fd5b67dd497945787d (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
223
/*
 * 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: rdft-dht.c,v 1.1 2008/10/17 06:11:29 scuri Exp $ */

/* Solve an R2HC/HC2R problem via post/pre processing of a DHT.  This
   is mainly useful because we can use Rader to compute DHTs of prime
   sizes.  It also allows us to express hc2r problems in terms of r2hc
   (via dht-r2hc), and to do hc2r problems without destroying the input. */

#include "rdft.h"

typedef struct {
     solver super;
} S;

typedef struct {
     plan_rdft super;
     plan *cld;
     int is, os;
     int n;
} P;

static void apply_r2hc(const plan *ego_, R *I, R *O)
{
     const P *ego = (const P *) ego_;
     int os;
     int i, n;

     {
	  plan_rdft *cld = (plan_rdft *) ego->cld;
	  cld->apply((plan *) cld, I, O);
     }

     n = ego->n;
     os = ego->os;
     for (i = 1; i < n - i; ++i) {
	  E a, b;
	  a = K(0.5) * O[os * i];
	  b = K(0.5) * O[os * (n - i)];
	  O[os * i] = a + b;
#if FFT_SIGN == -1
	  O[os * (n - i)] = b - a;
#else
	  O[os * (n - i)] = a - b;
#endif
     }
}

/* hc2r, destroying input as usual */
static void apply_hc2r(const plan *ego_, R *I, R *O)
{
     const P *ego = (const P *) ego_;
     int is = ego->is;
     int i, n = ego->n;

     for (i = 1; i < n - i; ++i) {
	  E a, b;
	  a = I[is * i];
	  b = I[is * (n - i)];
#if FFT_SIGN == -1
	  I[is * i] = a - b;
	  I[is * (n - i)] = a + b;
#else
	  I[is * i] = a + b;
	  I[is * (n - i)] = a - b;
#endif
     }

     {
	  plan_rdft *cld = (plan_rdft *) ego->cld;
	  cld->apply((plan *) cld, I, O);
     }
}

/* hc2r, without destroying input */
static void apply_hc2r_save(const plan *ego_, R *I, R *O)
{
     const P *ego = (const P *) ego_;
     int is = ego->is, os = ego->os;
     int i, n = ego->n;

     O[0] = I[0];
     for (i = 1; i < n - i; ++i) {
	  E a, b;
	  a = I[is * i];
	  b = I[is * (n - i)];
#if FFT_SIGN == -1
	  O[os * i] = a - b;
	  O[os * (n - i)] = a + b;
#else
	  O[os * i] = a + b;
	  O[os * (n - i)] = a - b;
#endif
     }
     if (i == n - i)
	  O[os * i] = I[is * i];

     {
	  plan_rdft *cld = (plan_rdft *) ego->cld;
	  cld->apply((plan *) cld, O, O);
     }
}

static void awake(plan *ego_, int flg)
{
     P *ego = (P *) ego_;
     AWAKE(ego->cld, flg);
}

static void destroy(plan *ego_)
{
     P *ego = (P *) ego_;
     X(plan_destroy_internal)(ego->cld);
}

static void print(const plan *ego_, printer *p)
{
     const P *ego = (const P *) ego_;
     p->print(p, "(%s-dht-%d%(%p%))", 
	      ego->super.apply == apply_r2hc ? "r2hc" : "hc2r",
	      ego->n, ego->cld);
}

static int applicable0(const solver *ego_, const problem *p_)
{
     UNUSED(ego_);
     if (RDFTP(p_)) {
          const problem_rdft *p = (const problem_rdft *) p_;
          return (1
		  && p->sz->rnk == 1
		  && p->vecsz->rnk == 0
		  && (p->kind[0] == R2HC || p->kind[0] == HC2R)

		  /* hack: size-2 DHT etc. are defined as being equivalent
		     to size-2 R2HC in problem.c, so we need this to prevent
		     infinite loops for size 2 in EXHAUSTIVE mode: */
		  && p->sz->dims[0].n > 2
	       );
     }
     return 0;
}

static int applicable(const solver *ego, const problem *p_, 
		      const planner *plnr)
{
     return (!NO_UGLYP(plnr) && applicable0(ego, p_));
}

static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
{
     P *pln;
     const problem_rdft *p;
     problem *cldp;
     plan *cld;

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

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

     p = (const problem_rdft *) p_;

     if (p->kind[0] == R2HC || DESTROY_INPUTP(plnr))
	  cldp = X(mkproblem_rdft_1)(p->sz, p->vecsz, p->I, p->O, DHT);
     else {
	  tensor *sz = X(tensor_copy_inplace)(p->sz, INPLACE_OS);
	  cldp = X(mkproblem_rdft_1)(sz, p->vecsz, p->O, p->O, DHT);
	  X(tensor_destroy)(sz);
     }
     cld = X(mkplan_d)(plnr, cldp);
     if (!cld) return (plan *)0;

     pln = MKPLAN_RDFT(P, &padt, p->kind[0] == R2HC ? 
		       apply_r2hc : (DESTROY_INPUTP(plnr) ?
				     apply_hc2r : apply_hc2r_save));
     pln->n = p->sz->dims[0].n;
     pln->is = p->sz->dims[0].is;
     pln->os = p->sz->dims[0].os;
     pln->cld = cld;
     
     pln->super.super.ops = cld->ops;
     pln->super.super.ops.other += 4 * ((pln->n - 1)/2);
     pln->super.super.ops.add += 2 * ((pln->n - 1)/2);
     if (p->kind[0] == R2HC)
	  pln->super.super.ops.mul += 2 * ((pln->n - 1)/2);
     if (pln->super.apply == apply_hc2r_save)
	  pln->super.super.ops.other += 2 + (pln->n % 2 ? 0 : 2);

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

/* constructor */
static solver *mksolver(void)
{
     static const solver_adt sadt = { mkplan };
     S *slv = MKSOLVER(S, &sadt);
     return &(slv->super);
}

void X(rdft_dht_register)(planner *p)
{
     REGISTER_SOLVER(p, mksolver());
}