sixstep.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <assert.h>
  31. #include "bits.h"
  32. #include "difradix2.h"
  33. #include "mptypes.h"
  34. #include "numbertheory.h"
  35. #include "transpose.h"
  36. #include "umodarith.h"
  37. #include "sixstep.h"
  38. /*
  39. * A variant of the six-step algorithm from:
  40. *
  41. * David H. Bailey: FFTs in External or Hierarchical Memory, Journal of
  42. * Supercomputing, vol. 4, no. 1 (March 1990), p. 23-35.
  43. *
  44. * URL: http://crd.lbl.gov/~dhbailey/dhbpapers/
  45. */
  46. /* forward transform with sign = -1 */
  47. int
  48. six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum)
  49. {
  50. struct fnt_params *tparams;
  51. mpd_size_t log2n, C, R;
  52. mpd_uint_t kernel;
  53. mpd_uint_t umod;
  54. #ifdef PPRO
  55. double dmod;
  56. uint32_t dinvmod[3];
  57. #endif
  58. mpd_uint_t *x, w0, w1, wstep;
  59. mpd_size_t i, k;
  60. assert(ispower2(n));
  61. assert(n >= 16);
  62. assert(n <= MPD_MAXTRANSFORM_2N);
  63. log2n = mpd_bsr(n);
  64. C = (ONE_UM << (log2n / 2)); /* number of columns */
  65. R = (ONE_UM << (log2n - (log2n / 2))); /* number of rows */
  66. /* view 'a' as an RxC matrix, transpose */
  67. if (!transpose_pow2(a, R, C)) {
  68. return 0;
  69. }
  70. if ((tparams = _mpd_init_fnt_params(R, -1, modnum)) == NULL) {
  71. return 0;
  72. }
  73. for (x = a; x < a+n; x += R) {
  74. fnt_dif2(x, R, tparams);
  75. }
  76. if (!transpose_pow2(a, C, R)) {
  77. mpd_free(tparams);
  78. return 0;
  79. }
  80. SETMODULUS(modnum);
  81. kernel = _mpd_getkernel(n, -1, modnum);
  82. for (i = 1; i < R; i++) {
  83. w0 = 1;
  84. w1 = POWMOD(kernel, i);
  85. wstep = MULMOD(w1, w1);
  86. for (k = 0; k < C; k += 2) {
  87. mpd_uint_t x0 = a[i*C+k];
  88. mpd_uint_t x1 = a[i*C+k+1];
  89. MULMOD2(&x0, w0, &x1, w1);
  90. MULMOD2C(&w0, &w1, wstep);
  91. a[i*C+k] = x0;
  92. a[i*C+k+1] = x1;
  93. }
  94. }
  95. if (C != R) {
  96. mpd_free(tparams);
  97. if ((tparams = _mpd_init_fnt_params(C, -1, modnum)) == NULL) {
  98. return 0;
  99. }
  100. }
  101. for (x = a; x < a+n; x += C) {
  102. fnt_dif2(x, C, tparams);
  103. }
  104. mpd_free(tparams);
  105. #if 0
  106. /* An unordered transform is sufficient for convolution. */
  107. if (ordered) {
  108. if (!transpose_pow2(a, R, C)) {
  109. return 0;
  110. }
  111. }
  112. #endif
  113. return 1;
  114. }
  115. /* reverse transform, sign = 1 */
  116. int
  117. inv_six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum)
  118. {
  119. struct fnt_params *tparams;
  120. mpd_size_t log2n, C, R;
  121. mpd_uint_t kernel;
  122. mpd_uint_t umod;
  123. #ifdef PPRO
  124. double dmod;
  125. uint32_t dinvmod[3];
  126. #endif
  127. mpd_uint_t *x, w0, w1, wstep;
  128. mpd_size_t i, k;
  129. assert(ispower2(n));
  130. assert(n >= 16);
  131. assert(n <= MPD_MAXTRANSFORM_2N);
  132. log2n = mpd_bsr(n);
  133. C = (ONE_UM << (log2n / 2)); /* number of columns */
  134. R = (ONE_UM << (log2n - (log2n / 2))); /* number of rows */
  135. #if 0
  136. /* An unordered transform is sufficient for convolution. */
  137. if (ordered) {
  138. if (!transpose_pow2(a, C, R)) {
  139. return 0;
  140. }
  141. }
  142. #endif
  143. if ((tparams = _mpd_init_fnt_params(C, 1, modnum)) == NULL) {
  144. return 0;
  145. }
  146. for (x = a; x < a+n; x += C) {
  147. fnt_dif2(x, C, tparams);
  148. }
  149. if (!transpose_pow2(a, R, C)) {
  150. mpd_free(tparams);
  151. return 0;
  152. }
  153. SETMODULUS(modnum);
  154. kernel = _mpd_getkernel(n, 1, modnum);
  155. for (i = 1; i < C; i++) {
  156. w0 = 1;
  157. w1 = POWMOD(kernel, i);
  158. wstep = MULMOD(w1, w1);
  159. for (k = 0; k < R; k += 2) {
  160. mpd_uint_t x0 = a[i*R+k];
  161. mpd_uint_t x1 = a[i*R+k+1];
  162. MULMOD2(&x0, w0, &x1, w1);
  163. MULMOD2C(&w0, &w1, wstep);
  164. a[i*R+k] = x0;
  165. a[i*R+k+1] = x1;
  166. }
  167. }
  168. if (R != C) {
  169. mpd_free(tparams);
  170. if ((tparams = _mpd_init_fnt_params(R, 1, modnum)) == NULL) {
  171. return 0;
  172. }
  173. }
  174. for (x = a; x < a+n; x += R) {
  175. fnt_dif2(x, R, tparams);
  176. }
  177. mpd_free(tparams);
  178. if (!transpose_pow2(a, C, R)) {
  179. return 0;
  180. }
  181. return 1;
  182. }