fourstep.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2008-2020 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 <assert.h>
  29. #include "constants.h"
  30. #include "fourstep.h"
  31. #include "numbertheory.h"
  32. #include "sixstep.h"
  33. #include "umodarith.h"
  34. /* Bignum: Cache efficient Matrix Fourier Transform for arrays of the
  35. form 3 * 2**n (See literature/matrix-transform.txt). */
  36. #ifndef PPRO
  37. static inline void
  38. std_size3_ntt(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3,
  39. mpd_uint_t w3table[3], mpd_uint_t umod)
  40. {
  41. mpd_uint_t r1, r2;
  42. mpd_uint_t w;
  43. mpd_uint_t s, tmp;
  44. /* k = 0 -> w = 1 */
  45. s = *x1;
  46. s = addmod(s, *x2, umod);
  47. s = addmod(s, *x3, umod);
  48. r1 = s;
  49. /* k = 1 */
  50. s = *x1;
  51. w = w3table[1];
  52. tmp = MULMOD(*x2, w);
  53. s = addmod(s, tmp, umod);
  54. w = w3table[2];
  55. tmp = MULMOD(*x3, w);
  56. s = addmod(s, tmp, umod);
  57. r2 = s;
  58. /* k = 2 */
  59. s = *x1;
  60. w = w3table[2];
  61. tmp = MULMOD(*x2, w);
  62. s = addmod(s, tmp, umod);
  63. w = w3table[1];
  64. tmp = MULMOD(*x3, w);
  65. s = addmod(s, tmp, umod);
  66. *x3 = s;
  67. *x2 = r2;
  68. *x1 = r1;
  69. }
  70. #else /* PPRO */
  71. static inline void
  72. ppro_size3_ntt(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3, mpd_uint_t w3table[3],
  73. mpd_uint_t umod, double *dmod, uint32_t dinvmod[3])
  74. {
  75. mpd_uint_t r1, r2;
  76. mpd_uint_t w;
  77. mpd_uint_t s, tmp;
  78. /* k = 0 -> w = 1 */
  79. s = *x1;
  80. s = addmod(s, *x2, umod);
  81. s = addmod(s, *x3, umod);
  82. r1 = s;
  83. /* k = 1 */
  84. s = *x1;
  85. w = w3table[1];
  86. tmp = ppro_mulmod(*x2, w, dmod, dinvmod);
  87. s = addmod(s, tmp, umod);
  88. w = w3table[2];
  89. tmp = ppro_mulmod(*x3, w, dmod, dinvmod);
  90. s = addmod(s, tmp, umod);
  91. r2 = s;
  92. /* k = 2 */
  93. s = *x1;
  94. w = w3table[2];
  95. tmp = ppro_mulmod(*x2, w, dmod, dinvmod);
  96. s = addmod(s, tmp, umod);
  97. w = w3table[1];
  98. tmp = ppro_mulmod(*x3, w, dmod, dinvmod);
  99. s = addmod(s, tmp, umod);
  100. *x3 = s;
  101. *x2 = r2;
  102. *x1 = r1;
  103. }
  104. #endif
  105. /* forward transform, sign = -1; transform length = 3 * 2**n */
  106. int
  107. four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum)
  108. {
  109. mpd_size_t R = 3; /* number of rows */
  110. mpd_size_t C = n / 3; /* number of columns */
  111. mpd_uint_t w3table[3];
  112. mpd_uint_t kernel, w0, w1, wstep;
  113. mpd_uint_t *s, *p0, *p1, *p2;
  114. mpd_uint_t umod;
  115. #ifdef PPRO
  116. double dmod;
  117. uint32_t dinvmod[3];
  118. #endif
  119. mpd_size_t i, k;
  120. assert(n >= 48);
  121. assert(n <= 3*MPD_MAXTRANSFORM_2N);
  122. /* Length R transform on the columns. */
  123. SETMODULUS(modnum);
  124. _mpd_init_w3table(w3table, -1, modnum);
  125. for (p0=a, p1=p0+C, p2=p0+2*C; p0<a+C; p0++,p1++,p2++) {
  126. SIZE3_NTT(p0, p1, p2, w3table);
  127. }
  128. /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */
  129. kernel = _mpd_getkernel(n, -1, modnum);
  130. for (i = 1; i < R; i++) {
  131. w0 = 1; /* r**(i*0): initial value for k=0 */
  132. w1 = POWMOD(kernel, i); /* r**(i*1): initial value for k=1 */
  133. wstep = MULMOD(w1, w1); /* r**(2*i) */
  134. for (k = 0; k < C-1; k += 2) {
  135. mpd_uint_t x0 = a[i*C+k];
  136. mpd_uint_t x1 = a[i*C+k+1];
  137. MULMOD2(&x0, w0, &x1, w1);
  138. MULMOD2C(&w0, &w1, wstep); /* r**(i*(k+2)) = r**(i*k) * r**(2*i) */
  139. a[i*C+k] = x0;
  140. a[i*C+k+1] = x1;
  141. }
  142. }
  143. /* Length C transform on the rows. */
  144. for (s = a; s < a+n; s += C) {
  145. if (!six_step_fnt(s, C, modnum)) {
  146. return 0;
  147. }
  148. }
  149. #if 0
  150. /* An unordered transform is sufficient for convolution. */
  151. /* Transpose the matrix. */
  152. #include "transpose.h"
  153. transpose_3xpow2(a, R, C);
  154. #endif
  155. return 1;
  156. }
  157. /* backward transform, sign = 1; transform length = 3 * 2**n */
  158. int
  159. inv_four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum)
  160. {
  161. mpd_size_t R = 3; /* number of rows */
  162. mpd_size_t C = n / 3; /* number of columns */
  163. mpd_uint_t w3table[3];
  164. mpd_uint_t kernel, w0, w1, wstep;
  165. mpd_uint_t *s, *p0, *p1, *p2;
  166. mpd_uint_t umod;
  167. #ifdef PPRO
  168. double dmod;
  169. uint32_t dinvmod[3];
  170. #endif
  171. mpd_size_t i, k;
  172. assert(n >= 48);
  173. assert(n <= 3*MPD_MAXTRANSFORM_2N);
  174. #if 0
  175. /* An unordered transform is sufficient for convolution. */
  176. /* Transpose the matrix, producing an R*C matrix. */
  177. #include "transpose.h"
  178. transpose_3xpow2(a, C, R);
  179. #endif
  180. /* Length C transform on the rows. */
  181. for (s = a; s < a+n; s += C) {
  182. if (!inv_six_step_fnt(s, C, modnum)) {
  183. return 0;
  184. }
  185. }
  186. /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */
  187. SETMODULUS(modnum);
  188. kernel = _mpd_getkernel(n, 1, modnum);
  189. for (i = 1; i < R; i++) {
  190. w0 = 1;
  191. w1 = POWMOD(kernel, i);
  192. wstep = MULMOD(w1, w1);
  193. for (k = 0; k < C; k += 2) {
  194. mpd_uint_t x0 = a[i*C+k];
  195. mpd_uint_t x1 = a[i*C+k+1];
  196. MULMOD2(&x0, w0, &x1, w1);
  197. MULMOD2C(&w0, &w1, wstep);
  198. a[i*C+k] = x0;
  199. a[i*C+k+1] = x1;
  200. }
  201. }
  202. /* Length R transform on the columns. */
  203. _mpd_init_w3table(w3table, 1, modnum);
  204. for (p0=a, p1=p0+C, p2=p0+2*C; p0<a+C; p0++,p1++,p2++) {
  205. SIZE3_NTT(p0, p1, p2, w3table);
  206. }
  207. return 1;
  208. }