1 /*
2 * Copyright (c) 2008-2024 Stefan Krah. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27
28 #include <assert.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34
35 #include "bits.h"
36 #include "constants.h"
37 #include "mpdecimal.h"
38 #include "transpose.h"
39 #include "typearith.h"
40
41
42 #define BUFSIZE 4096
43 #define SIDE 128
44
45
46 /* Bignum: The transpose functions are used for very large transforms
47 in sixstep.c and fourstep.c. */
48
49
50 /* Definition of the matrix transpose */
51 void
std_trans(mpd_uint_t dest[],mpd_uint_t src[],mpd_size_t rows,mpd_size_t cols)52 std_trans(mpd_uint_t dest[], mpd_uint_t src[], mpd_size_t rows, mpd_size_t cols)
53 {
54 mpd_size_t idest, isrc;
55 mpd_size_t r, c;
56
57 for (r = 0; r < rows; r++) {
58 isrc = r * cols;
59 idest = r;
60 for (c = 0; c < cols; c++) {
61 dest[idest] = src[isrc];
62 isrc += 1;
63 idest += rows;
64 }
65 }
66 }
67
68 /*
69 * Swap half-rows of 2^n * (2*2^n) matrix.
70 * FORWARD_CYCLE: even/odd permutation of the halfrows.
71 * BACKWARD_CYCLE: reverse the even/odd permutation.
72 */
73 static int
swap_halfrows_pow2(mpd_uint_t * matrix,mpd_size_t rows,mpd_size_t cols,int dir)74 swap_halfrows_pow2(mpd_uint_t *matrix, mpd_size_t rows, mpd_size_t cols, int dir)
75 {
76 mpd_uint_t buf1[BUFSIZE];
77 mpd_uint_t buf2[BUFSIZE];
78 mpd_uint_t *readbuf, *writebuf, *hp;
79 mpd_size_t *done, dbits;
80 mpd_size_t b = BUFSIZE, stride;
81 mpd_size_t hn, hmax; /* halfrow number */
82 mpd_size_t m, r=0;
83 mpd_size_t offset;
84 mpd_size_t next;
85
86
87 assert(cols == mul_size_t(2, rows));
88
89 if (dir == FORWARD_CYCLE) {
90 r = rows;
91 }
92 else if (dir == BACKWARD_CYCLE) {
93 r = 2;
94 }
95 else {
96 abort(); /* GCOV_NOT_REACHED */
97 }
98
99 m = cols - 1;
100 hmax = rows; /* cycles start at odd halfrows */
101 dbits = 8 * sizeof *done;
102 if ((done = mpd_calloc(hmax/(sizeof *done) + 1, sizeof *done)) == NULL) {
103 return 0;
104 }
105
106 for (hn = 1; hn <= hmax; hn += 2) {
107
108 if (done[hn/dbits] & mpd_bits[hn%dbits]) {
109 continue;
110 }
111
112 readbuf = buf1; writebuf = buf2;
113
114 for (offset = 0; offset < cols/2; offset += b) {
115
116 stride = (offset + b < cols/2) ? b : cols/2-offset;
117
118 hp = matrix + hn*cols/2;
119 memcpy(readbuf, hp+offset, stride*(sizeof *readbuf));
120 pointerswap(&readbuf, &writebuf);
121
122 next = mulmod_size_t(hn, r, m);
123 hp = matrix + next*cols/2;
124
125 while (next != hn) {
126
127 memcpy(readbuf, hp+offset, stride*(sizeof *readbuf));
128 memcpy(hp+offset, writebuf, stride*(sizeof *writebuf));
129 pointerswap(&readbuf, &writebuf);
130
131 done[next/dbits] |= mpd_bits[next%dbits];
132
133 next = mulmod_size_t(next, r, m);
134 hp = matrix + next*cols/2;
135
136 }
137
138 memcpy(hp+offset, writebuf, stride*(sizeof *writebuf));
139
140 done[hn/dbits] |= mpd_bits[hn%dbits];
141 }
142 }
143
144 mpd_free(done);
145 return 1;
146 }
147
148 /* In-place transpose of a square matrix */
149 static inline void
squaretrans(mpd_uint_t * buf,mpd_size_t cols)150 squaretrans(mpd_uint_t *buf, mpd_size_t cols)
151 {
152 mpd_uint_t tmp;
153 mpd_size_t idest, isrc;
154 mpd_size_t r, c;
155
156 for (r = 0; r < cols; r++) {
157 c = r+1;
158 isrc = r*cols + c;
159 idest = c*cols + r;
160 for (c = r+1; c < cols; c++) {
161 tmp = buf[isrc];
162 buf[isrc] = buf[idest];
163 buf[idest] = tmp;
164 isrc += 1;
165 idest += cols;
166 }
167 }
168 }
169
170 /*
171 * Transpose 2^n * 2^n matrix. For cache efficiency, the matrix is split into
172 * square blocks with side length 'SIDE'. First, the blocks are transposed,
173 * then a square transposition is done on each individual block.
174 */
175 static void
squaretrans_pow2(mpd_uint_t * matrix,mpd_size_t size)176 squaretrans_pow2(mpd_uint_t *matrix, mpd_size_t size)
177 {
178 mpd_uint_t buf1[SIDE*SIDE];
179 mpd_uint_t buf2[SIDE*SIDE];
180 mpd_uint_t *to, *from;
181 mpd_size_t b = size;
182 mpd_size_t r, c;
183 mpd_size_t i;
184
185 while (b > SIDE) b >>= 1;
186
187 for (r = 0; r < size; r += b) {
188
189 for (c = r; c < size; c += b) {
190
191 from = matrix + r*size + c;
192 to = buf1;
193 for (i = 0; i < b; i++) {
194 memcpy(to, from, b*(sizeof *to));
195 from += size;
196 to += b;
197 }
198 squaretrans(buf1, b);
199
200 if (r == c) {
201 to = matrix + r*size + c;
202 from = buf1;
203 for (i = 0; i < b; i++) {
204 memcpy(to, from, b*(sizeof *to));
205 from += b;
206 to += size;
207 }
208 continue;
209 }
210 else {
211 from = matrix + c*size + r;
212 to = buf2;
213 for (i = 0; i < b; i++) {
214 memcpy(to, from, b*(sizeof *to));
215 from += size;
216 to += b;
217 }
218 squaretrans(buf2, b);
219
220 to = matrix + c*size + r;
221 from = buf1;
222 for (i = 0; i < b; i++) {
223 memcpy(to, from, b*(sizeof *to));
224 from += b;
225 to += size;
226 }
227
228 to = matrix + r*size + c;
229 from = buf2;
230 for (i = 0; i < b; i++) {
231 memcpy(to, from, b*(sizeof *to));
232 from += b;
233 to += size;
234 }
235 }
236 }
237 }
238
239 }
240
241 /*
242 * In-place transposition of a 2^n x 2^n or a 2^n x (2*2^n)
243 * or a (2*2^n) x 2^n matrix.
244 */
245 int
transpose_pow2(mpd_uint_t * matrix,mpd_size_t rows,mpd_size_t cols)246 transpose_pow2(mpd_uint_t *matrix, mpd_size_t rows, mpd_size_t cols)
247 {
248 mpd_size_t size = mul_size_t(rows, cols);
249
250 assert(ispower2(rows));
251 assert(ispower2(cols));
252
253 if (cols == rows) {
254 squaretrans_pow2(matrix, rows);
255 }
256 else if (cols == mul_size_t(2, rows)) {
257 if (!swap_halfrows_pow2(matrix, rows, cols, FORWARD_CYCLE)) {
258 return 0;
259 }
260 squaretrans_pow2(matrix, rows);
261 squaretrans_pow2(matrix+(size/2), rows);
262 }
263 else if (rows == mul_size_t(2, cols)) {
264 squaretrans_pow2(matrix, cols);
265 squaretrans_pow2(matrix+(size/2), cols);
266 if (!swap_halfrows_pow2(matrix, cols, rows, BACKWARD_CYCLE)) {
267 return 0;
268 }
269 }
270 else {
271 abort(); /* GCOV_NOT_REACHED */
272 }
273
274 return 1;
275 }
276