cannam@127: /* cannam@127: * Copyright (c) 2003, 2007-14 Matteo Frigo cannam@127: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology cannam@127: * cannam@127: * This program is free software; you can redistribute it and/or modify cannam@127: * it under the terms of the GNU General Public License as published by cannam@127: * the Free Software Foundation; either version 2 of the License, or cannam@127: * (at your option) any later version. cannam@127: * cannam@127: * This program is distributed in the hope that it will be useful, cannam@127: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@127: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@127: * GNU General Public License for more details. cannam@127: * cannam@127: * You should have received a copy of the GNU General Public License cannam@127: * along with this program; if not, write to the Free Software cannam@127: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@127: * cannam@127: */ cannam@127: cannam@127: cannam@127: #include "dft.h" cannam@127: #include "rdft.h" cannam@127: #include cannam@127: cannam@127: static void destroy(problem *ego_) cannam@127: { cannam@127: problem_rdft2 *ego = (problem_rdft2 *) ego_; cannam@127: X(tensor_destroy2)(ego->vecsz, ego->sz); cannam@127: X(ifree)(ego_); cannam@127: } cannam@127: cannam@127: static void hash(const problem *p_, md5 *m) cannam@127: { cannam@127: const problem_rdft2 *p = (const problem_rdft2 *) p_; cannam@127: X(md5puts)(m, "rdft2"); cannam@127: X(md5int)(m, p->r0 == p->cr); cannam@127: X(md5INT)(m, p->r1 - p->r0); cannam@127: X(md5INT)(m, p->ci - p->cr); cannam@127: X(md5int)(m, X(ialignment_of)(p->r0)); cannam@127: X(md5int)(m, X(ialignment_of)(p->r1)); cannam@127: X(md5int)(m, X(ialignment_of)(p->cr)); cannam@127: X(md5int)(m, X(ialignment_of)(p->ci)); cannam@127: X(md5int)(m, p->kind); cannam@127: X(tensor_md5)(m, p->sz); cannam@127: X(tensor_md5)(m, p->vecsz); cannam@127: } cannam@127: cannam@127: static void print(const problem *ego_, printer *p) cannam@127: { cannam@127: const problem_rdft2 *ego = (const problem_rdft2 *) ego_; cannam@127: p->print(p, "(rdft2 %d %d %T %T)", cannam@127: (int)(ego->cr == ego->r0), cannam@127: (int)(ego->kind), cannam@127: ego->sz, cannam@127: ego->vecsz); cannam@127: } cannam@127: cannam@127: static void recur(const iodim *dims, int rnk, R *I0, R *I1) cannam@127: { cannam@127: if (rnk == RNK_MINFTY) cannam@127: return; cannam@127: else if (rnk == 0) cannam@127: I0[0] = K(0.0); cannam@127: else if (rnk > 0) { cannam@127: INT i, n = dims[0].n, is = dims[0].is; cannam@127: cannam@127: if (rnk == 1) { cannam@127: for (i = 0; i < n - 1; i += 2) { cannam@127: *I0 = *I1 = K(0.0); cannam@127: I0 += is; I1 += is; cannam@127: } cannam@127: if (i < n) cannam@127: *I0 = K(0.0); cannam@127: } else { cannam@127: for (i = 0; i < n; ++i) cannam@127: recur(dims + 1, rnk - 1, I0 + i * is, I1 + i * is); cannam@127: } cannam@127: } cannam@127: } cannam@127: cannam@127: static void vrecur(const iodim *vdims, int vrnk, cannam@127: const iodim *dims, int rnk, R *I0, R *I1) cannam@127: { cannam@127: if (vrnk == RNK_MINFTY) cannam@127: return; cannam@127: else if (vrnk == 0) cannam@127: recur(dims, rnk, I0, I1); cannam@127: else if (vrnk > 0) { cannam@127: INT i, n = vdims[0].n, is = vdims[0].is; cannam@127: cannam@127: for (i = 0; i < n; ++i) cannam@127: vrecur(vdims + 1, vrnk - 1, cannam@127: dims, rnk, I0 + i * is, I1 + i * is); cannam@127: } cannam@127: } cannam@127: cannam@127: INT X(rdft2_complex_n)(INT real_n, rdft_kind kind) cannam@127: { cannam@127: switch (kind) { cannam@127: case R2HC: cannam@127: case HC2R: cannam@127: return (real_n / 2) + 1; cannam@127: case R2HCII: cannam@127: case HC2RIII: cannam@127: return (real_n + 1) / 2; cannam@127: default: cannam@127: /* can't happen */ cannam@127: A(0); cannam@127: return 0; cannam@127: } cannam@127: } cannam@127: cannam@127: static void zero(const problem *ego_) cannam@127: { cannam@127: const problem_rdft2 *ego = (const problem_rdft2 *) ego_; cannam@127: if (R2HC_KINDP(ego->kind)) { cannam@127: /* FIXME: can we avoid the double recursion somehow? */ cannam@127: vrecur(ego->vecsz->dims, ego->vecsz->rnk, cannam@127: ego->sz->dims, ego->sz->rnk, cannam@127: UNTAINT(ego->r0), UNTAINT(ego->r1)); cannam@127: } else { cannam@127: tensor *sz; cannam@127: tensor *sz2 = X(tensor_copy)(ego->sz); cannam@127: int rnk = sz2->rnk; cannam@127: if (rnk > 0) /* ~half as many complex outputs */ cannam@127: sz2->dims[rnk-1].n = cannam@127: X(rdft2_complex_n)(sz2->dims[rnk-1].n, ego->kind); cannam@127: sz = X(tensor_append)(ego->vecsz, sz2); cannam@127: X(tensor_destroy)(sz2); cannam@127: X(dft_zerotens)(sz, UNTAINT(ego->cr), UNTAINT(ego->ci)); cannam@127: X(tensor_destroy)(sz); cannam@127: } cannam@127: } cannam@127: cannam@127: static const problem_adt padt = cannam@127: { cannam@127: PROBLEM_RDFT2, cannam@127: hash, cannam@127: zero, cannam@127: print, cannam@127: destroy cannam@127: }; cannam@127: cannam@127: problem *X(mkproblem_rdft2)(const tensor *sz, const tensor *vecsz, cannam@127: R *r0, R *r1, R *cr, R *ci, cannam@127: rdft_kind kind) cannam@127: { cannam@127: problem_rdft2 *ego; cannam@127: cannam@127: A(kind == R2HC || kind == R2HCII || kind == HC2R || kind == HC2RIII); cannam@127: A(X(tensor_kosherp)(sz)); cannam@127: A(X(tensor_kosherp)(vecsz)); cannam@127: A(FINITE_RNK(sz->rnk)); cannam@127: cannam@127: /* require in-place problems to use r0 == cr */ cannam@127: if (UNTAINT(r0) == UNTAINT(ci)) cannam@127: return X(mkproblem_unsolvable)(); cannam@127: cannam@127: /* FIXME: should check UNTAINT(r1) == UNTAINT(cr) but cannam@127: only if odd elements exist, which requires compressing the cannam@127: tensors first */ cannam@127: cannam@127: if (UNTAINT(r0) == UNTAINT(cr)) cannam@127: r0 = cr = JOIN_TAINT(r0, cr); cannam@127: cannam@127: ego = (problem_rdft2 *)X(mkproblem)(sizeof(problem_rdft2), &padt); cannam@127: cannam@127: if (sz->rnk > 1) { /* have to compress rnk-1 dims separately, ugh */ cannam@127: tensor *szc = X(tensor_copy_except)(sz, sz->rnk - 1); cannam@127: tensor *szr = X(tensor_copy_sub)(sz, sz->rnk - 1, 1); cannam@127: tensor *szcc = X(tensor_compress)(szc); cannam@127: if (szcc->rnk > 0) cannam@127: ego->sz = X(tensor_append)(szcc, szr); cannam@127: else cannam@127: ego->sz = X(tensor_compress)(szr); cannam@127: X(tensor_destroy2)(szc, szr); X(tensor_destroy)(szcc); cannam@127: } else { cannam@127: ego->sz = X(tensor_compress)(sz); cannam@127: } cannam@127: ego->vecsz = X(tensor_compress_contiguous)(vecsz); cannam@127: ego->r0 = r0; cannam@127: ego->r1 = r1; cannam@127: ego->cr = cr; cannam@127: ego->ci = ci; cannam@127: ego->kind = kind; cannam@127: cannam@127: A(FINITE_RNK(ego->sz->rnk)); cannam@127: return &(ego->super); cannam@127: cannam@127: } cannam@127: cannam@127: /* Same as X(mkproblem_rdft2), but also destroy input tensors. */ cannam@127: problem *X(mkproblem_rdft2_d)(tensor *sz, tensor *vecsz, cannam@127: R *r0, R *r1, R *cr, R *ci, rdft_kind kind) cannam@127: { cannam@127: problem *p = X(mkproblem_rdft2)(sz, vecsz, r0, r1, cr, ci, kind); cannam@127: X(tensor_destroy2)(vecsz, sz); cannam@127: return p; cannam@127: } cannam@127: cannam@127: /* Same as X(mkproblem_rdft2_d), but with only one R pointer. cannam@127: Used by the API. */ cannam@127: problem *X(mkproblem_rdft2_d_3pointers)(tensor *sz, tensor *vecsz, cannam@127: R *r0, R *cr, R *ci, rdft_kind kind) cannam@127: { cannam@127: problem *p; cannam@127: int rnk = sz->rnk; cannam@127: R *r1; cannam@127: cannam@127: if (rnk == 0) cannam@127: r1 = r0; cannam@127: else if (R2HC_KINDP(kind)) { cannam@127: r1 = r0 + sz->dims[rnk-1].is; cannam@127: sz->dims[rnk-1].is *= 2; cannam@127: } else { cannam@127: r1 = r0 + sz->dims[rnk-1].os; cannam@127: sz->dims[rnk-1].os *= 2; cannam@127: } cannam@127: cannam@127: p = X(mkproblem_rdft2)(sz, vecsz, r0, r1, cr, ci, kind); cannam@127: X(tensor_destroy2)(vecsz, sz); cannam@127: return p; cannam@127: }