difradix2.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "bits.h"
  30. #include "constants.h"
  31. #include "difradix2.h"
  32. #include "numbertheory.h"
  33. #include "umodarith.h"
  34. /* Bignum: The actual transform routine (decimation in frequency). */
  35. /*
  36. * Generate index pairs (x, bitreverse(x)) and carry out the permutation.
  37. * n must be a power of two.
  38. * Algorithm due to Brent/Lehmann, see Joerg Arndt, "Matters Computational",
  39. * Chapter 1.14.4. [http://www.jjj.de/fxt/]
  40. */
  41. static inline void
  42. bitreverse_permute(mpd_uint_t a[], mpd_size_t n)
  43. {
  44. mpd_size_t x = 0;
  45. mpd_size_t r = 0;
  46. mpd_uint_t t;
  47. do { /* Invariant: r = bitreverse(x) */
  48. if (r > x) {
  49. t = a[x];
  50. a[x] = a[r];
  51. a[r] = t;
  52. }
  53. /* Flip trailing consecutive 1 bits and the first zero bit
  54. * that absorbs a possible carry. */
  55. x += 1;
  56. /* Mirror the operation on r: Flip n_trailing_zeros(x)+1
  57. high bits of r. */
  58. r ^= (n - (n >> (mpd_bsf(x)+1)));
  59. /* The loop invariant is preserved. */
  60. } while (x < n);
  61. }
  62. /* Fast Number Theoretic Transform, decimation in frequency. */
  63. void
  64. fnt_dif2(mpd_uint_t a[], mpd_size_t n, struct fnt_params *tparams)
  65. {
  66. mpd_uint_t *wtable = tparams->wtable;
  67. mpd_uint_t umod;
  68. #ifdef PPRO
  69. double dmod;
  70. uint32_t dinvmod[3];
  71. #endif
  72. mpd_uint_t u0, u1, v0, v1;
  73. mpd_uint_t w, w0, w1, wstep;
  74. mpd_size_t m, mhalf;
  75. mpd_size_t j, r;
  76. assert(ispower2(n));
  77. assert(n >= 4);
  78. SETMODULUS(tparams->modnum);
  79. /* m == n */
  80. mhalf = n / 2;
  81. for (j = 0; j < mhalf; j += 2) {
  82. w0 = wtable[j];
  83. w1 = wtable[j+1];
  84. u0 = a[j];
  85. v0 = a[j+mhalf];
  86. u1 = a[j+1];
  87. v1 = a[j+1+mhalf];
  88. a[j] = addmod(u0, v0, umod);
  89. v0 = submod(u0, v0, umod);
  90. a[j+1] = addmod(u1, v1, umod);
  91. v1 = submod(u1, v1, umod);
  92. MULMOD2(&v0, w0, &v1, w1);
  93. a[j+mhalf] = v0;
  94. a[j+1+mhalf] = v1;
  95. }
  96. wstep = 2;
  97. for (m = n/2; m >= 2; m>>=1, wstep<<=1) {
  98. mhalf = m / 2;
  99. /* j == 0 */
  100. for (r = 0; r < n; r += 2*m) {
  101. u0 = a[r];
  102. v0 = a[r+mhalf];
  103. u1 = a[m+r];
  104. v1 = a[m+r+mhalf];
  105. a[r] = addmod(u0, v0, umod);
  106. v0 = submod(u0, v0, umod);
  107. a[m+r] = addmod(u1, v1, umod);
  108. v1 = submod(u1, v1, umod);
  109. a[r+mhalf] = v0;
  110. a[m+r+mhalf] = v1;
  111. }
  112. for (j = 1; j < mhalf; j++) {
  113. w = wtable[j*wstep];
  114. for (r = 0; r < n; r += 2*m) {
  115. u0 = a[r+j];
  116. v0 = a[r+j+mhalf];
  117. u1 = a[m+r+j];
  118. v1 = a[m+r+j+mhalf];
  119. a[r+j] = addmod(u0, v0, umod);
  120. v0 = submod(u0, v0, umod);
  121. a[m+r+j] = addmod(u1, v1, umod);
  122. v1 = submod(u1, v1, umod);
  123. MULMOD2C(&v0, &v1, w);
  124. a[r+j+mhalf] = v0;
  125. a[m+r+j+mhalf] = v1;
  126. }
  127. }
  128. }
  129. bitreverse_permute(a, n);
  130. }