/src/fftw3/kernel/tensor.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 | | |
22 | | #include "kernel/ifftw.h" |
23 | | |
24 | | tensor *X(mktensor)(int rnk) |
25 | 21.3k | { |
26 | 21.3k | tensor *x; |
27 | | |
28 | 21.3k | A(rnk >= 0); |
29 | | |
30 | 21.3k | #if defined(STRUCT_HACK_KR) |
31 | 21.3k | if (FINITE_RNK(rnk) && rnk > 1) |
32 | 8.31k | x = (tensor *)MALLOC(sizeof(tensor) + (unsigned)(rnk - 1) * sizeof(iodim), |
33 | 21.3k | TENSORS); |
34 | 13.0k | else |
35 | 13.0k | x = (tensor *)MALLOC(sizeof(tensor), TENSORS); |
36 | | #elif defined(STRUCT_HACK_C99) |
37 | | if (FINITE_RNK(rnk)) |
38 | | x = (tensor *)MALLOC(sizeof(tensor) + (unsigned)rnk * sizeof(iodim), |
39 | | TENSORS); |
40 | | else |
41 | | x = (tensor *)MALLOC(sizeof(tensor), TENSORS); |
42 | | #else |
43 | | x = (tensor *)MALLOC(sizeof(tensor), TENSORS); |
44 | | if (FINITE_RNK(rnk) && rnk > 0) |
45 | | x->dims = (iodim *)MALLOC(sizeof(iodim) * (unsigned)rnk, TENSORS); |
46 | | else |
47 | | x->dims = 0; |
48 | | #endif |
49 | | |
50 | 21.3k | x->rnk = rnk; |
51 | 21.3k | return x; |
52 | 21.3k | } |
53 | | |
54 | | void X(tensor_destroy)(tensor *sz) |
55 | 21.3k | { |
56 | | #if !defined(STRUCT_HACK_C99) && !defined(STRUCT_HACK_KR) |
57 | | X(ifree0)(sz->dims); |
58 | | #endif |
59 | 21.3k | X(ifree)(sz); |
60 | 21.3k | } |
61 | | |
62 | | INT X(tensor_sz)(const tensor *sz) |
63 | 6.21k | { |
64 | 6.21k | int i; |
65 | 6.21k | INT n = 1; |
66 | | |
67 | 6.21k | if (!FINITE_RNK(sz->rnk)) |
68 | 0 | return 0; |
69 | | |
70 | 18.3k | for (i = 0; i < sz->rnk; ++i) |
71 | 12.1k | n *= sz->dims[i].n; |
72 | 6.21k | return n; |
73 | 6.21k | } |
74 | | |
75 | | void X(tensor_md5)(md5 *p, const tensor *t) |
76 | 7.56k | { |
77 | 7.56k | int i; |
78 | 7.56k | X(md5int)(p, t->rnk); |
79 | 7.56k | if (FINITE_RNK(t->rnk)) { |
80 | 13.6k | for (i = 0; i < t->rnk; ++i) { |
81 | 6.27k | const iodim *q = t->dims + i; |
82 | 6.27k | X(md5INT)(p, q->n); |
83 | 6.27k | X(md5INT)(p, q->is); |
84 | 6.27k | X(md5INT)(p, q->os); |
85 | 6.27k | } |
86 | 7.35k | } |
87 | 7.56k | } |
88 | | |
89 | | /* treat a (rank <= 1)-tensor as a rank-1 tensor, extracting |
90 | | appropriate n, is, and os components */ |
91 | | int X(tensor_tornk1)(const tensor *t, INT *n, INT *is, INT *os) |
92 | 8.03k | { |
93 | 8.03k | A(t->rnk <= 1); |
94 | 8.03k | if (t->rnk == 1) { |
95 | 4.77k | const iodim *vd = t->dims; |
96 | 4.77k | *n = vd[0].n; |
97 | 4.77k | *is = vd[0].is; |
98 | 4.77k | *os = vd[0].os; |
99 | 4.77k | } else { |
100 | 3.25k | *n = 1; |
101 | 3.25k | *is = *os = 0; |
102 | 3.25k | } |
103 | 8.03k | return 1; |
104 | 8.03k | } |
105 | | |
106 | | void X(tensor_print)(const tensor *x, printer *p) |
107 | 0 | { |
108 | 0 | if (FINITE_RNK(x->rnk)) { |
109 | 0 | int i; |
110 | 0 | int first = 1; |
111 | 0 | p->print(p, "("); |
112 | 0 | for (i = 0; i < x->rnk; ++i) { |
113 | 0 | const iodim *d = x->dims + i; |
114 | 0 | p->print(p, "%s(%D %D %D)", |
115 | 0 | first ? "" : " ", |
116 | 0 | d->n, d->is, d->os); |
117 | 0 | first = 0; |
118 | 0 | } |
119 | 0 | p->print(p, ")"); |
120 | 0 | } else { |
121 | 0 | p->print(p, "rank-minfty"); |
122 | 0 | } |
123 | 0 | } |