Chris@82: /* Chris@82: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@82: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@82: * Chris@82: * This program is free software; you can redistribute it and/or modify Chris@82: * it under the terms of the GNU General Public License as published by Chris@82: * the Free Software Foundation; either version 2 of the License, or Chris@82: * (at your option) any later version. Chris@82: * Chris@82: * This program is distributed in the hope that it will be useful, Chris@82: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@82: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@82: * GNU General Public License for more details. Chris@82: * Chris@82: * You should have received a copy of the GNU General Public License Chris@82: * along with this program; if not, write to the Free Software Chris@82: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@82: * Chris@82: */ Chris@82: Chris@82: #include "kernel/ifftw.h" Chris@82: Chris@82: /* in place square transposition, iterative */ Chris@82: void X(transpose)(R *I, INT n, INT s0, INT s1, INT vl) Chris@82: { Chris@82: INT i0, i1, v; Chris@82: Chris@82: switch (vl) { Chris@82: case 1: Chris@82: for (i1 = 1; i1 < n; ++i1) { Chris@82: for (i0 = 0; i0 < i1; ++i0) { Chris@82: R x0 = I[i1 * s0 + i0 * s1]; Chris@82: R y0 = I[i1 * s1 + i0 * s0]; Chris@82: I[i1 * s1 + i0 * s0] = x0; Chris@82: I[i1 * s0 + i0 * s1] = y0; Chris@82: } Chris@82: } Chris@82: break; Chris@82: case 2: Chris@82: for (i1 = 1; i1 < n; ++i1) { Chris@82: for (i0 = 0; i0 < i1; ++i0) { Chris@82: R x0 = I[i1 * s0 + i0 * s1]; Chris@82: R x1 = I[i1 * s0 + i0 * s1 + 1]; Chris@82: R y0 = I[i1 * s1 + i0 * s0]; Chris@82: R y1 = I[i1 * s1 + i0 * s0 + 1]; Chris@82: I[i1 * s1 + i0 * s0] = x0; Chris@82: I[i1 * s1 + i0 * s0 + 1] = x1; Chris@82: I[i1 * s0 + i0 * s1] = y0; Chris@82: I[i1 * s0 + i0 * s1 + 1] = y1; Chris@82: } Chris@82: } Chris@82: break; Chris@82: default: Chris@82: for (i1 = 1; i1 < n; ++i1) { Chris@82: for (i0 = 0; i0 < i1; ++i0) { Chris@82: for (v = 0; v < vl; ++v) { Chris@82: R x0 = I[i1 * s0 + i0 * s1 + v]; Chris@82: R y0 = I[i1 * s1 + i0 * s0 + v]; Chris@82: I[i1 * s1 + i0 * s0 + v] = x0; Chris@82: I[i1 * s0 + i0 * s1 + v] = y0; Chris@82: } Chris@82: } Chris@82: } Chris@82: break; Chris@82: } Chris@82: } Chris@82: Chris@82: struct transpose_closure { Chris@82: R *I; Chris@82: INT s0, s1, vl, tilesz; Chris@82: R *buf0, *buf1; Chris@82: }; Chris@82: Chris@82: static void dotile(INT n0l, INT n0u, INT n1l, INT n1u, void *args) Chris@82: { Chris@82: struct transpose_closure *k = (struct transpose_closure *)args; Chris@82: R *I = k->I; Chris@82: INT s0 = k->s0, s1 = k->s1, vl = k->vl; Chris@82: INT i0, i1, v; Chris@82: Chris@82: switch (vl) { Chris@82: case 1: Chris@82: for (i1 = n1l; i1 < n1u; ++i1) { Chris@82: for (i0 = n0l; i0 < n0u; ++i0) { Chris@82: R x0 = I[i1 * s0 + i0 * s1]; Chris@82: R y0 = I[i1 * s1 + i0 * s0]; Chris@82: I[i1 * s1 + i0 * s0] = x0; Chris@82: I[i1 * s0 + i0 * s1] = y0; Chris@82: } Chris@82: } Chris@82: break; Chris@82: case 2: Chris@82: for (i1 = n1l; i1 < n1u; ++i1) { Chris@82: for (i0 = n0l; i0 < n0u; ++i0) { Chris@82: R x0 = I[i1 * s0 + i0 * s1]; Chris@82: R x1 = I[i1 * s0 + i0 * s1 + 1]; Chris@82: R y0 = I[i1 * s1 + i0 * s0]; Chris@82: R y1 = I[i1 * s1 + i0 * s0 + 1]; Chris@82: I[i1 * s1 + i0 * s0] = x0; Chris@82: I[i1 * s1 + i0 * s0 + 1] = x1; Chris@82: I[i1 * s0 + i0 * s1] = y0; Chris@82: I[i1 * s0 + i0 * s1 + 1] = y1; Chris@82: } Chris@82: } Chris@82: break; Chris@82: default: Chris@82: for (i1 = n1l; i1 < n1u; ++i1) { Chris@82: for (i0 = n0l; i0 < n0u; ++i0) { Chris@82: for (v = 0; v < vl; ++v) { Chris@82: R x0 = I[i1 * s0 + i0 * s1 + v]; Chris@82: R y0 = I[i1 * s1 + i0 * s0 + v]; Chris@82: I[i1 * s1 + i0 * s0 + v] = x0; Chris@82: I[i1 * s0 + i0 * s1 + v] = y0; Chris@82: } Chris@82: } Chris@82: } Chris@82: } Chris@82: } Chris@82: Chris@82: static void dotile_buf(INT n0l, INT n0u, INT n1l, INT n1u, void *args) Chris@82: { Chris@82: struct transpose_closure *k = (struct transpose_closure *)args; Chris@82: X(cpy2d_ci)(k->I + n0l * k->s0 + n1l * k->s1, Chris@82: k->buf0, Chris@82: n0u - n0l, k->s0, k->vl, Chris@82: n1u - n1l, k->s1, k->vl * (n0u - n0l), Chris@82: k->vl); Chris@82: X(cpy2d_ci)(k->I + n0l * k->s1 + n1l * k->s0, Chris@82: k->buf1, Chris@82: n0u - n0l, k->s1, k->vl, Chris@82: n1u - n1l, k->s0, k->vl * (n0u - n0l), Chris@82: k->vl); Chris@82: X(cpy2d_co)(k->buf1, Chris@82: k->I + n0l * k->s0 + n1l * k->s1, Chris@82: n0u - n0l, k->vl, k->s0, Chris@82: n1u - n1l, k->vl * (n0u - n0l), k->s1, Chris@82: k->vl); Chris@82: X(cpy2d_co)(k->buf0, Chris@82: k->I + n0l * k->s1 + n1l * k->s0, Chris@82: n0u - n0l, k->vl, k->s1, Chris@82: n1u - n1l, k->vl * (n0u - n0l), k->s0, Chris@82: k->vl); Chris@82: } Chris@82: Chris@82: static void transpose_rec(R *I, INT n, Chris@82: void (*f)(INT n0l, INT n0u, INT n1l, INT n1u, Chris@82: void *args), Chris@82: struct transpose_closure *k) Chris@82: { Chris@82: tail: Chris@82: if (n > 1) { Chris@82: INT n2 = n / 2; Chris@82: k->I = I; Chris@82: X(tile2d)(0, n2, n2, n, k->tilesz, f, k); Chris@82: transpose_rec(I, n2, f, k); Chris@82: I += n2 * (k->s0 + k->s1); n -= n2; goto tail; Chris@82: } Chris@82: } Chris@82: Chris@82: void X(transpose_tiled)(R *I, INT n, INT s0, INT s1, INT vl) Chris@82: { Chris@82: struct transpose_closure k; Chris@82: k.s0 = s0; Chris@82: k.s1 = s1; Chris@82: k.vl = vl; Chris@82: /* two blocks must be in cache, to be swapped */ Chris@82: k.tilesz = X(compute_tilesz)(vl, 2); Chris@82: k.buf0 = k.buf1 = 0; /* unused */ Chris@82: transpose_rec(I, n, dotile, &k); Chris@82: } Chris@82: Chris@82: void X(transpose_tiledbuf)(R *I, INT n, INT s0, INT s1, INT vl) Chris@82: { Chris@82: struct transpose_closure k; Chris@82: /* Assume that the the rows of I conflict into the same cache Chris@82: lines, and therefore we don't need to reserve cache space for Chris@82: the input. If the rows don't conflict, there is no reason Chris@82: to use tiledbuf at all.*/ Chris@82: R buf0[CACHESIZE / (2 * sizeof(R))]; Chris@82: R buf1[CACHESIZE / (2 * sizeof(R))]; Chris@82: k.s0 = s0; Chris@82: k.s1 = s1; Chris@82: k.vl = vl; Chris@82: k.tilesz = X(compute_tilesz)(vl, 2); Chris@82: k.buf0 = buf0; Chris@82: k.buf1 = buf1; Chris@82: A(k.tilesz * k.tilesz * vl * sizeof(R) <= sizeof(buf0)); Chris@82: A(k.tilesz * k.tilesz * vl * sizeof(R) <= sizeof(buf1)); Chris@82: transpose_rec(I, n, dotile_buf, &k); Chris@82: } Chris@82: