transpose.c 6.2 KB

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