annotate src/fftw-3.3.3/kernel/transpose.c @ 127:7867fa7e1b6b

Current fftw source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:40:26 +0100
parents 89f5e221ed7b
children
rev   line source
cannam@95 1 /*
cannam@95 2 * Copyright (c) 2003, 2007-11 Matteo Frigo
cannam@95 3 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
cannam@95 4 *
cannam@95 5 * This program is free software; you can redistribute it and/or modify
cannam@95 6 * it under the terms of the GNU General Public License as published by
cannam@95 7 * the Free Software Foundation; either version 2 of the License, or
cannam@95 8 * (at your option) any later version.
cannam@95 9 *
cannam@95 10 * This program is distributed in the hope that it will be useful,
cannam@95 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@95 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@95 13 * GNU General Public License for more details.
cannam@95 14 *
cannam@95 15 * You should have received a copy of the GNU General Public License
cannam@95 16 * along with this program; if not, write to the Free Software
cannam@95 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cannam@95 18 *
cannam@95 19 */
cannam@95 20
cannam@95 21 #include "ifftw.h"
cannam@95 22
cannam@95 23 /* in place square transposition, iterative */
cannam@95 24 void X(transpose)(R *I, INT n, INT s0, INT s1, INT vl)
cannam@95 25 {
cannam@95 26 INT i0, i1, v;
cannam@95 27
cannam@95 28 switch (vl) {
cannam@95 29 case 1:
cannam@95 30 for (i1 = 1; i1 < n; ++i1) {
cannam@95 31 for (i0 = 0; i0 < i1; ++i0) {
cannam@95 32 R x0 = I[i1 * s0 + i0 * s1];
cannam@95 33 R y0 = I[i1 * s1 + i0 * s0];
cannam@95 34 I[i1 * s1 + i0 * s0] = x0;
cannam@95 35 I[i1 * s0 + i0 * s1] = y0;
cannam@95 36 }
cannam@95 37 }
cannam@95 38 break;
cannam@95 39 case 2:
cannam@95 40 for (i1 = 1; i1 < n; ++i1) {
cannam@95 41 for (i0 = 0; i0 < i1; ++i0) {
cannam@95 42 R x0 = I[i1 * s0 + i0 * s1];
cannam@95 43 R x1 = I[i1 * s0 + i0 * s1 + 1];
cannam@95 44 R y0 = I[i1 * s1 + i0 * s0];
cannam@95 45 R y1 = I[i1 * s1 + i0 * s0 + 1];
cannam@95 46 I[i1 * s1 + i0 * s0] = x0;
cannam@95 47 I[i1 * s1 + i0 * s0 + 1] = x1;
cannam@95 48 I[i1 * s0 + i0 * s1] = y0;
cannam@95 49 I[i1 * s0 + i0 * s1 + 1] = y1;
cannam@95 50 }
cannam@95 51 }
cannam@95 52 break;
cannam@95 53 default:
cannam@95 54 for (i1 = 1; i1 < n; ++i1) {
cannam@95 55 for (i0 = 0; i0 < i1; ++i0) {
cannam@95 56 for (v = 0; v < vl; ++v) {
cannam@95 57 R x0 = I[i1 * s0 + i0 * s1 + v];
cannam@95 58 R y0 = I[i1 * s1 + i0 * s0 + v];
cannam@95 59 I[i1 * s1 + i0 * s0 + v] = x0;
cannam@95 60 I[i1 * s0 + i0 * s1 + v] = y0;
cannam@95 61 }
cannam@95 62 }
cannam@95 63 }
cannam@95 64 break;
cannam@95 65 }
cannam@95 66 }
cannam@95 67
cannam@95 68 struct transpose_closure {
cannam@95 69 R *I;
cannam@95 70 INT s0, s1, vl, tilesz;
cannam@95 71 R *buf0, *buf1;
cannam@95 72 };
cannam@95 73
cannam@95 74 static void dotile(INT n0l, INT n0u, INT n1l, INT n1u, void *args)
cannam@95 75 {
cannam@95 76 struct transpose_closure *k = (struct transpose_closure *)args;
cannam@95 77 R *I = k->I;
cannam@95 78 INT s0 = k->s0, s1 = k->s1, vl = k->vl;
cannam@95 79 INT i0, i1, v;
cannam@95 80
cannam@95 81 switch (vl) {
cannam@95 82 case 1:
cannam@95 83 for (i1 = n1l; i1 < n1u; ++i1) {
cannam@95 84 for (i0 = n0l; i0 < n0u; ++i0) {
cannam@95 85 R x0 = I[i1 * s0 + i0 * s1];
cannam@95 86 R y0 = I[i1 * s1 + i0 * s0];
cannam@95 87 I[i1 * s1 + i0 * s0] = x0;
cannam@95 88 I[i1 * s0 + i0 * s1] = y0;
cannam@95 89 }
cannam@95 90 }
cannam@95 91 break;
cannam@95 92 case 2:
cannam@95 93 for (i1 = n1l; i1 < n1u; ++i1) {
cannam@95 94 for (i0 = n0l; i0 < n0u; ++i0) {
cannam@95 95 R x0 = I[i1 * s0 + i0 * s1];
cannam@95 96 R x1 = I[i1 * s0 + i0 * s1 + 1];
cannam@95 97 R y0 = I[i1 * s1 + i0 * s0];
cannam@95 98 R y1 = I[i1 * s1 + i0 * s0 + 1];
cannam@95 99 I[i1 * s1 + i0 * s0] = x0;
cannam@95 100 I[i1 * s1 + i0 * s0 + 1] = x1;
cannam@95 101 I[i1 * s0 + i0 * s1] = y0;
cannam@95 102 I[i1 * s0 + i0 * s1 + 1] = y1;
cannam@95 103 }
cannam@95 104 }
cannam@95 105 break;
cannam@95 106 default:
cannam@95 107 for (i1 = n1l; i1 < n1u; ++i1) {
cannam@95 108 for (i0 = n0l; i0 < n0u; ++i0) {
cannam@95 109 for (v = 0; v < vl; ++v) {
cannam@95 110 R x0 = I[i1 * s0 + i0 * s1 + v];
cannam@95 111 R y0 = I[i1 * s1 + i0 * s0 + v];
cannam@95 112 I[i1 * s1 + i0 * s0 + v] = x0;
cannam@95 113 I[i1 * s0 + i0 * s1 + v] = y0;
cannam@95 114 }
cannam@95 115 }
cannam@95 116 }
cannam@95 117 }
cannam@95 118 }
cannam@95 119
cannam@95 120 static void dotile_buf(INT n0l, INT n0u, INT n1l, INT n1u, void *args)
cannam@95 121 {
cannam@95 122 struct transpose_closure *k = (struct transpose_closure *)args;
cannam@95 123 X(cpy2d_ci)(k->I + n0l * k->s0 + n1l * k->s1,
cannam@95 124 k->buf0,
cannam@95 125 n0u - n0l, k->s0, k->vl,
cannam@95 126 n1u - n1l, k->s1, k->vl * (n0u - n0l),
cannam@95 127 k->vl);
cannam@95 128 X(cpy2d_ci)(k->I + n0l * k->s1 + n1l * k->s0,
cannam@95 129 k->buf1,
cannam@95 130 n0u - n0l, k->s1, k->vl,
cannam@95 131 n1u - n1l, k->s0, k->vl * (n0u - n0l),
cannam@95 132 k->vl);
cannam@95 133 X(cpy2d_co)(k->buf1,
cannam@95 134 k->I + n0l * k->s0 + n1l * k->s1,
cannam@95 135 n0u - n0l, k->vl, k->s0,
cannam@95 136 n1u - n1l, k->vl * (n0u - n0l), k->s1,
cannam@95 137 k->vl);
cannam@95 138 X(cpy2d_co)(k->buf0,
cannam@95 139 k->I + n0l * k->s1 + n1l * k->s0,
cannam@95 140 n0u - n0l, k->vl, k->s1,
cannam@95 141 n1u - n1l, k->vl * (n0u - n0l), k->s0,
cannam@95 142 k->vl);
cannam@95 143 }
cannam@95 144
cannam@95 145 static void transpose_rec(R *I, INT n,
cannam@95 146 void (*f)(INT n0l, INT n0u, INT n1l, INT n1u,
cannam@95 147 void *args),
cannam@95 148 struct transpose_closure *k)
cannam@95 149 {
cannam@95 150 tail:
cannam@95 151 if (n > 1) {
cannam@95 152 INT n2 = n / 2;
cannam@95 153 k->I = I;
cannam@95 154 X(tile2d)(0, n2, n2, n, k->tilesz, f, k);
cannam@95 155 transpose_rec(I, n2, f, k);
cannam@95 156 I += n2 * (k->s0 + k->s1); n -= n2; goto tail;
cannam@95 157 }
cannam@95 158 }
cannam@95 159
cannam@95 160 void X(transpose_tiled)(R *I, INT n, INT s0, INT s1, INT vl)
cannam@95 161 {
cannam@95 162 struct transpose_closure k;
cannam@95 163 k.s0 = s0;
cannam@95 164 k.s1 = s1;
cannam@95 165 k.vl = vl;
cannam@95 166 /* two blocks must be in cache, to be swapped */
cannam@95 167 k.tilesz = X(compute_tilesz)(vl, 2);
cannam@95 168 k.buf0 = k.buf1 = 0; /* unused */
cannam@95 169 transpose_rec(I, n, dotile, &k);
cannam@95 170 }
cannam@95 171
cannam@95 172 void X(transpose_tiledbuf)(R *I, INT n, INT s0, INT s1, INT vl)
cannam@95 173 {
cannam@95 174 struct transpose_closure k;
cannam@95 175 /* Assume that the the rows of I conflict into the same cache
cannam@95 176 lines, and therefore we don't need to reserve cache space for
cannam@95 177 the input. If the rows don't conflict, there is no reason
cannam@95 178 to use tiledbuf at all.*/
cannam@95 179 R buf0[CACHESIZE / (2 * sizeof(R))];
cannam@95 180 R buf1[CACHESIZE / (2 * sizeof(R))];
cannam@95 181 k.s0 = s0;
cannam@95 182 k.s1 = s1;
cannam@95 183 k.vl = vl;
cannam@95 184 k.tilesz = X(compute_tilesz)(vl, 2);
cannam@95 185 k.buf0 = buf0;
cannam@95 186 k.buf1 = buf1;
cannam@95 187 A(k.tilesz * k.tilesz * vl * sizeof(R) <= sizeof(buf0));
cannam@95 188 A(k.tilesz * k.tilesz * vl * sizeof(R) <= sizeof(buf1));
cannam@95 189 transpose_rec(I, n, dotile_buf, &k);
cannam@95 190 }
cannam@95 191