sixstep.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <stdio.h>
  30. #include "bits.h"
  31. #include "constants.h"
  32. #include "difradix2.h"
  33. #include "numbertheory.h"
  34. #include "sixstep.h"
  35. #include "transpose.h"
  36. #include "umodarith.h"
  37. /* Bignum: Cache efficient Matrix Fourier Transform for arrays of the
  38. form 2**n (See literature/six-step.txt). */
  39. /* forward transform with sign = -1 */
  40. int
  41. six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum)
  42. {
  43. struct fnt_params *tparams;
  44. mpd_size_t log2n, C, R;
  45. mpd_uint_t kernel;
  46. mpd_uint_t umod;
  47. #ifdef PPRO
  48. double dmod;
  49. uint32_t dinvmod[3];
  50. #endif
  51. mpd_uint_t *x, w0, w1, wstep;
  52. mpd_size_t i, k;
  53. assert(ispower2(n));
  54. assert(n >= 16);
  55. assert(n <= MPD_MAXTRANSFORM_2N);
  56. log2n = mpd_bsr(n);
  57. C = ((mpd_size_t)1) << (log2n / 2); /* number of columns */
  58. R = ((mpd_size_t)1) << (log2n - (log2n / 2)); /* number of rows */
  59. /* Transpose the matrix. */
  60. if (!transpose_pow2(a, R, C)) {
  61. return 0;
  62. }
  63. /* Length R transform on the rows. */
  64. if ((tparams = _mpd_init_fnt_params(R, -1, modnum)) == NULL) {
  65. return 0;
  66. }
  67. for (x = a; x < a+n; x += R) {
  68. fnt_dif2(x, R, tparams);
  69. }
  70. /* Transpose the matrix. */
  71. if (!transpose_pow2(a, C, R)) {
  72. mpd_free(tparams);
  73. return 0;
  74. }
  75. /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */
  76. SETMODULUS(modnum);
  77. kernel = _mpd_getkernel(n, -1, modnum);
  78. for (i = 1; i < R; i++) {
  79. w0 = 1; /* r**(i*0): initial value for k=0 */
  80. w1 = POWMOD(kernel, i); /* r**(i*1): initial value for k=1 */
  81. wstep = MULMOD(w1, w1); /* r**(2*i) */
  82. for (k = 0; k < C; k += 2) {
  83. mpd_uint_t x0 = a[i*C+k];
  84. mpd_uint_t x1 = a[i*C+k+1];
  85. MULMOD2(&x0, w0, &x1, w1);
  86. MULMOD2C(&w0, &w1, wstep); /* r**(i*(k+2)) = r**(i*k) * r**(2*i) */
  87. a[i*C+k] = x0;
  88. a[i*C+k+1] = x1;
  89. }
  90. }
  91. /* Length C transform on the rows. */
  92. if (C != R) {
  93. mpd_free(tparams);
  94. if ((tparams = _mpd_init_fnt_params(C, -1, modnum)) == NULL) {
  95. return 0;
  96. }
  97. }
  98. for (x = a; x < a+n; x += C) {
  99. fnt_dif2(x, C, tparams);
  100. }
  101. mpd_free(tparams);
  102. #if 0
  103. /* An unordered transform is sufficient for convolution. */
  104. /* Transpose the matrix. */
  105. if (!transpose_pow2(a, R, C)) {
  106. return 0;
  107. }
  108. #endif
  109. return 1;
  110. }
  111. /* reverse transform, sign = 1 */
  112. int
  113. inv_six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum)
  114. {
  115. struct fnt_params *tparams;
  116. mpd_size_t log2n, C, R;
  117. mpd_uint_t kernel;
  118. mpd_uint_t umod;
  119. #ifdef PPRO
  120. double dmod;
  121. uint32_t dinvmod[3];
  122. #endif
  123. mpd_uint_t *x, w0, w1, wstep;
  124. mpd_size_t i, k;
  125. assert(ispower2(n));
  126. assert(n >= 16);
  127. assert(n <= MPD_MAXTRANSFORM_2N);
  128. log2n = mpd_bsr(n);
  129. C = ((mpd_size_t)1) << (log2n / 2); /* number of columns */
  130. R = ((mpd_size_t)1) << (log2n - (log2n / 2)); /* number of rows */
  131. #if 0
  132. /* An unordered transform is sufficient for convolution. */
  133. /* Transpose the matrix, producing an R*C matrix. */
  134. if (!transpose_pow2(a, C, R)) {
  135. return 0;
  136. }
  137. #endif
  138. /* Length C transform on the rows. */
  139. if ((tparams = _mpd_init_fnt_params(C, 1, modnum)) == NULL) {
  140. return 0;
  141. }
  142. for (x = a; x < a+n; x += C) {
  143. fnt_dif2(x, C, tparams);
  144. }
  145. /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */
  146. SETMODULUS(modnum);
  147. kernel = _mpd_getkernel(n, 1, modnum);
  148. for (i = 1; i < R; i++) {
  149. w0 = 1;
  150. w1 = POWMOD(kernel, i);
  151. wstep = MULMOD(w1, w1);
  152. for (k = 0; k < C; k += 2) {
  153. mpd_uint_t x0 = a[i*C+k];
  154. mpd_uint_t x1 = a[i*C+k+1];
  155. MULMOD2(&x0, w0, &x1, w1);
  156. MULMOD2C(&w0, &w1, wstep);
  157. a[i*C+k] = x0;
  158. a[i*C+k+1] = x1;
  159. }
  160. }
  161. /* Transpose the matrix. */
  162. if (!transpose_pow2(a, R, C)) {
  163. mpd_free(tparams);
  164. return 0;
  165. }
  166. /* Length R transform on the rows. */
  167. if (R != C) {
  168. mpd_free(tparams);
  169. if ((tparams = _mpd_init_fnt_params(R, 1, modnum)) == NULL) {
  170. return 0;
  171. }
  172. }
  173. for (x = a; x < a+n; x += R) {
  174. fnt_dif2(x, R, tparams);
  175. }
  176. mpd_free(tparams);
  177. /* Transpose the matrix. */
  178. if (!transpose_pow2(a, C, R)) {
  179. return 0;
  180. }
  181. return 1;
  182. }