1 /*
2  * Copyright (c) 2003, 2007-14 Matteo Frigo
3  * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20 
21 
22 /* Solve a DHT problem (Discrete Hartley Transform) via post-processing
23    of an R2HC problem. */
24 
25 #include "rdft/rdft.h"
26 
27 typedef struct {
28      solver super;
29 } S;
30 
31 typedef struct {
32      plan_rdft super;
33      plan *cld;
34      INT os;
35      INT n;
36 } P;
37 
apply(const plan * ego_,R * I,R * O)38 static void apply(const plan *ego_, R *I, R *O)
39 {
40      const P *ego = (const P *) ego_;
41      INT os = ego->os;
42      INT i, n = ego->n;
43 
44      {
45 	  plan_rdft *cld = (plan_rdft *) ego->cld;
46 	  cld->apply((plan *) cld, I, O);
47      }
48 
49      for (i = 1; i < n - i; ++i) {
50 	  E a, b;
51 	  a = O[os * i];
52 	  b = O[os * (n - i)];
53 #if FFT_SIGN == -1
54 	  O[os * i] = a - b;
55 	  O[os * (n - i)] = a + b;
56 #else
57 	  O[os * i] = a + b;
58 	  O[os * (n - i)] = a - b;
59 #endif
60      }
61 }
62 
awake(plan * ego_,enum wakefulness wakefulness)63 static void awake(plan *ego_, enum wakefulness wakefulness)
64 {
65      P *ego = (P *) ego_;
66      X(plan_awake)(ego->cld, wakefulness);
67 }
68 
destroy(plan * ego_)69 static void destroy(plan *ego_)
70 {
71      P *ego = (P *) ego_;
72      X(plan_destroy_internal)(ego->cld);
73 }
74 
print(const plan * ego_,printer * p)75 static void print(const plan *ego_, printer *p)
76 {
77      const P *ego = (const P *) ego_;
78      p->print(p, "(dht-r2hc-%D%(%p%))", ego->n, ego->cld);
79 }
80 
applicable0(const problem * p_,const planner * plnr)81 static int applicable0(const problem *p_, const planner *plnr)
82 {
83      const problem_rdft *p = (const problem_rdft *) p_;
84      return (1
85 	     && !NO_DHT_R2HCP(plnr)
86 	     && p->sz->rnk == 1
87 	     && p->vecsz->rnk == 0
88 	     && p->kind[0] == DHT
89 	  );
90 }
91 
applicable(const solver * ego,const problem * p,const planner * plnr)92 static int applicable(const solver *ego, const problem *p, const planner *plnr)
93 {
94      UNUSED(ego);
95      return (!NO_SLOWP(plnr) && applicable0(p, plnr));
96 }
97 
mkplan(const solver * ego_,const problem * p_,planner * plnr)98 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
99 {
100      P *pln;
101      const problem_rdft *p;
102      plan *cld;
103 
104      static const plan_adt padt = {
105 	  X(rdft_solve), awake, print, destroy
106      };
107 
108      if (!applicable(ego_, p_, plnr))
109           return (plan *)0;
110 
111      p = (const problem_rdft *) p_;
112 
113      /* NO_DHT_R2HC stops infinite loops with rdft-dht.c */
114      cld = X(mkplan_f_d)(plnr,
115 			 X(mkproblem_rdft_1)(p->sz, p->vecsz,
116 					     p->I, p->O, R2HC),
117 			 NO_DHT_R2HC, 0, 0);
118      if (!cld) return (plan *)0;
119 
120      pln = MKPLAN_RDFT(P, &padt, apply);
121 
122      pln->n = p->sz->dims[0].n;
123      pln->os = p->sz->dims[0].os;
124      pln->cld = cld;
125 
126      pln->super.super.ops = cld->ops;
127      pln->super.super.ops.other += 4 * ((pln->n - 1)/2);
128      pln->super.super.ops.add += 2 * ((pln->n - 1)/2);
129 
130      return &(pln->super.super);
131 }
132 
133 /* constructor */
mksolver(void)134 static solver *mksolver(void)
135 {
136      static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
137      S *slv = MKSOLVER(S, &sadt);
138      return &(slv->super);
139 }
140 
X(dht_r2hc_register)141 void X(dht_r2hc_register)(planner *p)
142 {
143      REGISTER_SOLVER(p, mksolver());
144 }
145