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
|
/*
* 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 "dft/dft.h"
typedef struct {
solver super;
} S;
typedef struct {
plan_dft super;
twid *td;
INT n, is, os;
} P;
static void cdot(INT n, const E *x, const R *w,
R *or0, R *oi0, R *or1, R *oi1)
{
INT i;
E rr = x[0], ri = 0, ir = x[1], ii = 0;
x += 2;
for (i = 1; i + i < n; ++i) {
rr += x[0] * w[0];
ir += x[1] * w[0];
ri += x[2] * w[1];
ii += x[3] * w[1];
x += 4; w += 2;
}
*or0 = rr + ii;
*oi0 = ir - ri;
*or1 = rr - ii;
*oi1 = ir + ri;
}
static void hartley(INT n, const R *xr, const R *xi, INT xs, E *o,
R *pr, R *pi)
{
INT i;
E sr, si;
o[0] = sr = xr[0]; o[1] = si = xi[0]; o += 2;
for (i = 1; i + i < n; ++i) {
sr += (o[0] = xr[i * xs] + xr[(n - i) * xs]);
si += (o[1] = xi[i * xs] + xi[(n - i) * xs]);
o[2] = xr[i * xs] - xr[(n - i) * xs];
o[3] = xi[i * xs] - xi[(n - i) * xs];
o += 4;
}
*pr = sr;
*pi = si;
}
static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io)
{
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 * 2 * sizeof(E);
BUF_ALLOC(E *, buf, bufsz);
hartley(n, ri, ii, is, buf, ro, io);
for (i = 1; i + i < n; ++i) {
cdot(n, buf, W,
ro + i * os, io + i * os,
ro + (n - i) * os, io + (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, "(dft-generic-%D)", ego->n);
}
static int applicable(const solver *ego, const problem *p_,
const planner *plnr)
{
const problem_dft *p = (const problem_dft *) p_;
UNUSED(ego);
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)
);
}
static plan *mkplan(const solver *ego, const problem *p_, planner *plnr)
{
const problem_dft *p;
P *pln;
INT n;
static const plan_adt padt = {
X(dft_solve), awake, print, X(plan_null_destroy)
};
if (!applicable(ego, p_, plnr))
return (plan *)0;
pln = MKPLAN_DFT(P, &padt, apply);
p = (const problem_dft *) p_;
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->super.super.ops.add = (n-1) * 5;
pln->super.super.ops.mul = 0;
pln->super.super.ops.fma = (n-1) * (n-1) ;
#if 0 /* these are nice pipelined sequential loads and should cost nothing */
pln->super.super.ops.other = (n-1)*(4 + 1 + 2 * (n-1)); /* approximate */
#endif
return &(pln->super.super);
}
static solver *mksolver(void)
{
static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 };
S *slv = MKSOLVER(S, &sadt);
return &(slv->super);
}
void X(dft_generic_register)(planner *p)
{
REGISTER_SOLVER(p, mksolver());
}
|