fourstep.c 5.5 KB

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