/src/fftw3/kernel/pickdim.c
Line | Count | Source |
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 | | #include "kernel/ifftw.h" |
22 | | |
23 | | |
24 | | /* Given a solver which_dim, a vector sz, and whether or not the |
25 | | transform is out-of-place, return the actual dimension index that |
26 | | it corresponds to. The basic idea here is that we return the |
27 | | which_dim'th valid dimension, starting from the end if |
28 | | which_dim < 0. */ |
29 | | static int really_pickdim(int which_dim, const tensor *sz, int oop, int *dp) |
30 | 1.98k | { |
31 | 1.98k | int i; |
32 | 1.98k | int count_ok = 0; |
33 | 1.98k | if (which_dim > 0) { |
34 | 2.09k | for (i = 0; i < sz->rnk; ++i) { |
35 | 1.93k | if (oop || sz->dims[i].is == sz->dims[i].os) |
36 | 1.16k | if (++count_ok == which_dim) { |
37 | 1.16k | *dp = i; |
38 | 1.16k | return 1; |
39 | 1.16k | } |
40 | 1.93k | } |
41 | 1.32k | } |
42 | 659 | else if (which_dim < 0) { |
43 | 824 | for (i = sz->rnk - 1; i >= 0; --i) { |
44 | 670 | if (oop || sz->dims[i].is == sz->dims[i].os) |
45 | 505 | if (++count_ok == -which_dim) { |
46 | 505 | *dp = i; |
47 | 505 | return 1; |
48 | 505 | } |
49 | 670 | } |
50 | 659 | } |
51 | 0 | else { /* zero: pick the middle, if valid */ |
52 | 0 | i = (sz->rnk - 1) / 2; |
53 | 0 | if (i >= 0 && (oop || sz->dims[i].is == sz->dims[i].os)) { |
54 | 0 | *dp = i; |
55 | 0 | return 1; |
56 | 0 | } |
57 | 0 | } |
58 | 308 | return 0; |
59 | 1.98k | } |
60 | | |
61 | | /* Like really_pickdim, but only returns 1 if no previous "buddy" |
62 | | which_dim in the buddies list would give the same dim. */ |
63 | | int X(pickdim)(int which_dim, const int *buddies, size_t nbuddies, |
64 | | const tensor *sz, int oop, int *dp) |
65 | 1.47k | { |
66 | 1.47k | size_t i; |
67 | 1.47k | int d1; |
68 | | |
69 | 1.47k | if (!really_pickdim(which_dim, sz, oop, dp)) |
70 | 308 | return 0; |
71 | | |
72 | | /* check whether some buddy solver would produce the same dim. |
73 | | If so, consider this solver unapplicable and let the buddy |
74 | | take care of it. The smallest-indexed buddy is applicable. */ |
75 | 1.33k | for (i = 0; i < nbuddies; ++i) { |
76 | 1.33k | if (buddies[i] == which_dim) |
77 | 833 | break; /* found self */ |
78 | 505 | if (really_pickdim(buddies[i], sz, oop, &d1) && *dp == d1) |
79 | 335 | return 0; /* found equivalent buddy */ |
80 | 505 | } |
81 | 833 | return 1; |
82 | 1.16k | } |