umodarith.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. #ifndef LIBMPDEC_UMODARITH_H_
  28. #define LIBMPDEC_UMODARITH_H_
  29. #include "mpdecimal.h"
  30. #include "constants.h"
  31. #include "typearith.h"
  32. /* Bignum: Low level routines for unsigned modular arithmetic. These are
  33. used in the fast convolution functions for very large coefficients. */
  34. /**************************************************************************/
  35. /* ANSI modular arithmetic */
  36. /**************************************************************************/
  37. /*
  38. * Restrictions: a < m and b < m
  39. * ACL2 proof: umodarith.lisp: addmod-correct
  40. */
  41. static inline mpd_uint_t
  42. addmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m)
  43. {
  44. mpd_uint_t s;
  45. s = a + b;
  46. s = (s < a) ? s - m : s;
  47. s = (s >= m) ? s - m : s;
  48. return s;
  49. }
  50. /*
  51. * Restrictions: a < m and b < m
  52. * ACL2 proof: umodarith.lisp: submod-2-correct
  53. */
  54. static inline mpd_uint_t
  55. submod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m)
  56. {
  57. mpd_uint_t d;
  58. d = a - b;
  59. d = (a < b) ? d + m : d;
  60. return d;
  61. }
  62. /*
  63. * Restrictions: a < 2m and b < 2m
  64. * ACL2 proof: umodarith.lisp: section ext-submod
  65. */
  66. static inline mpd_uint_t
  67. ext_submod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m)
  68. {
  69. mpd_uint_t d;
  70. a = (a >= m) ? a - m : a;
  71. b = (b >= m) ? b - m : b;
  72. d = a - b;
  73. d = (a < b) ? d + m : d;
  74. return d;
  75. }
  76. /*
  77. * Reduce double word modulo m.
  78. * Restrictions: m != 0
  79. * ACL2 proof: umodarith.lisp: section dw-reduce
  80. */
  81. static inline mpd_uint_t
  82. dw_reduce(mpd_uint_t hi, mpd_uint_t lo, mpd_uint_t m)
  83. {
  84. mpd_uint_t r1, r2, w;
  85. _mpd_div_word(&w, &r1, hi, m);
  86. _mpd_div_words(&w, &r2, r1, lo, m);
  87. return r2;
  88. }
  89. /*
  90. * Subtract double word from a.
  91. * Restrictions: a < m
  92. * ACL2 proof: umodarith.lisp: section dw-submod
  93. */
  94. static inline mpd_uint_t
  95. dw_submod(mpd_uint_t a, mpd_uint_t hi, mpd_uint_t lo, mpd_uint_t m)
  96. {
  97. mpd_uint_t d, r;
  98. r = dw_reduce(hi, lo, m);
  99. d = a - r;
  100. d = (a < r) ? d + m : d;
  101. return d;
  102. }
  103. #ifdef CONFIG_64
  104. /**************************************************************************/
  105. /* 64-bit modular arithmetic */
  106. /**************************************************************************/
  107. /*
  108. * A proof of the algorithm is in literature/mulmod-64.txt. An ACL2
  109. * proof is in umodarith.lisp: section "Fast modular reduction".
  110. *
  111. * Algorithm: calculate (a * b) % p:
  112. *
  113. * a) hi, lo <- a * b # Calculate a * b.
  114. *
  115. * b) hi, lo <- R(hi, lo) # Reduce modulo p.
  116. *
  117. * c) Repeat step b) until 0 <= hi * 2**64 + lo < 2*p.
  118. *
  119. * d) If the result is less than p, return lo. Otherwise return lo - p.
  120. */
  121. static inline mpd_uint_t
  122. x64_mulmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m)
  123. {
  124. mpd_uint_t hi, lo, x, y;
  125. _mpd_mul_words(&hi, &lo, a, b);
  126. if (m & (1ULL<<32)) { /* P1 */
  127. /* first reduction */
  128. x = y = hi;
  129. hi >>= 32;
  130. x = lo - x;
  131. if (x > lo) hi--;
  132. y <<= 32;
  133. lo = y + x;
  134. if (lo < y) hi++;
  135. /* second reduction */
  136. x = y = hi;
  137. hi >>= 32;
  138. x = lo - x;
  139. if (x > lo) hi--;
  140. y <<= 32;
  141. lo = y + x;
  142. if (lo < y) hi++;
  143. return (hi || lo >= m ? lo - m : lo);
  144. }
  145. else if (m & (1ULL<<34)) { /* P2 */
  146. /* first reduction */
  147. x = y = hi;
  148. hi >>= 30;
  149. x = lo - x;
  150. if (x > lo) hi--;
  151. y <<= 34;
  152. lo = y + x;
  153. if (lo < y) hi++;
  154. /* second reduction */
  155. x = y = hi;
  156. hi >>= 30;
  157. x = lo - x;
  158. if (x > lo) hi--;
  159. y <<= 34;
  160. lo = y + x;
  161. if (lo < y) hi++;
  162. /* third reduction */
  163. x = y = hi;
  164. hi >>= 30;
  165. x = lo - x;
  166. if (x > lo) hi--;
  167. y <<= 34;
  168. lo = y + x;
  169. if (lo < y) hi++;
  170. return (hi || lo >= m ? lo - m : lo);
  171. }
  172. else { /* P3 */
  173. /* first reduction */
  174. x = y = hi;
  175. hi >>= 24;
  176. x = lo - x;
  177. if (x > lo) hi--;
  178. y <<= 40;
  179. lo = y + x;
  180. if (lo < y) hi++;
  181. /* second reduction */
  182. x = y = hi;
  183. hi >>= 24;
  184. x = lo - x;
  185. if (x > lo) hi--;
  186. y <<= 40;
  187. lo = y + x;
  188. if (lo < y) hi++;
  189. /* third reduction */
  190. x = y = hi;
  191. hi >>= 24;
  192. x = lo - x;
  193. if (x > lo) hi--;
  194. y <<= 40;
  195. lo = y + x;
  196. if (lo < y) hi++;
  197. return (hi || lo >= m ? lo - m : lo);
  198. }
  199. }
  200. static inline void
  201. x64_mulmod2c(mpd_uint_t *a, mpd_uint_t *b, mpd_uint_t w, mpd_uint_t m)
  202. {
  203. *a = x64_mulmod(*a, w, m);
  204. *b = x64_mulmod(*b, w, m);
  205. }
  206. static inline void
  207. x64_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1,
  208. mpd_uint_t m)
  209. {
  210. *a0 = x64_mulmod(*a0, b0, m);
  211. *a1 = x64_mulmod(*a1, b1, m);
  212. }
  213. static inline mpd_uint_t
  214. x64_powmod(mpd_uint_t base, mpd_uint_t exp, mpd_uint_t umod)
  215. {
  216. mpd_uint_t r = 1;
  217. while (exp > 0) {
  218. if (exp & 1)
  219. r = x64_mulmod(r, base, umod);
  220. base = x64_mulmod(base, base, umod);
  221. exp >>= 1;
  222. }
  223. return r;
  224. }
  225. /* END CONFIG_64 */
  226. #else /* CONFIG_32 */
  227. /**************************************************************************/
  228. /* 32-bit modular arithmetic */
  229. /**************************************************************************/
  230. #if defined(ANSI)
  231. #if !defined(LEGACY_COMPILER)
  232. /* HAVE_UINT64_T */
  233. static inline mpd_uint_t
  234. std_mulmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m)
  235. {
  236. return ((mpd_uuint_t) a * b) % m;
  237. }
  238. static inline void
  239. std_mulmod2c(mpd_uint_t *a, mpd_uint_t *b, mpd_uint_t w, mpd_uint_t m)
  240. {
  241. *a = ((mpd_uuint_t) *a * w) % m;
  242. *b = ((mpd_uuint_t) *b * w) % m;
  243. }
  244. static inline void
  245. std_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1,
  246. mpd_uint_t m)
  247. {
  248. *a0 = ((mpd_uuint_t) *a0 * b0) % m;
  249. *a1 = ((mpd_uuint_t) *a1 * b1) % m;
  250. }
  251. /* END HAVE_UINT64_T */
  252. #else
  253. /* LEGACY_COMPILER */
  254. static inline mpd_uint_t
  255. std_mulmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m)
  256. {
  257. mpd_uint_t hi, lo, q, r;
  258. _mpd_mul_words(&hi, &lo, a, b);
  259. _mpd_div_words(&q, &r, hi, lo, m);
  260. return r;
  261. }
  262. static inline void
  263. std_mulmod2c(mpd_uint_t *a, mpd_uint_t *b, mpd_uint_t w, mpd_uint_t m)
  264. {
  265. *a = std_mulmod(*a, w, m);
  266. *b = std_mulmod(*b, w, m);
  267. }
  268. static inline void
  269. std_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1,
  270. mpd_uint_t m)
  271. {
  272. *a0 = std_mulmod(*a0, b0, m);
  273. *a1 = std_mulmod(*a1, b1, m);
  274. }
  275. /* END LEGACY_COMPILER */
  276. #endif
  277. static inline mpd_uint_t
  278. std_powmod(mpd_uint_t base, mpd_uint_t exp, mpd_uint_t umod)
  279. {
  280. mpd_uint_t r = 1;
  281. while (exp > 0) {
  282. if (exp & 1)
  283. r = std_mulmod(r, base, umod);
  284. base = std_mulmod(base, base, umod);
  285. exp >>= 1;
  286. }
  287. return r;
  288. }
  289. #endif /* ANSI CONFIG_32 */
  290. /**************************************************************************/
  291. /* Pentium Pro modular arithmetic */
  292. /**************************************************************************/
  293. /*
  294. * A proof of the algorithm is in literature/mulmod-ppro.txt. The FPU
  295. * control word must be set to 64-bit precision and truncation mode
  296. * prior to using these functions.
  297. *
  298. * Algorithm: calculate (a * b) % p:
  299. *
  300. * p := prime < 2**31
  301. * pinv := (long double)1.0 / p (precalculated)
  302. *
  303. * a) n = a * b # Calculate exact product.
  304. * b) qest = n * pinv # Calculate estimate for q = n / p.
  305. * c) q = (qest+2**63)-2**63 # Truncate qest to the exact quotient.
  306. * d) r = n - q * p # Calculate remainder.
  307. *
  308. * Remarks:
  309. *
  310. * - p = dmod and pinv = dinvmod.
  311. * - dinvmod points to an array of three uint32_t, which is interpreted
  312. * as an 80 bit long double by fldt.
  313. * - Intel compilers prior to version 11 do not seem to handle the
  314. * __GNUC__ inline assembly correctly.
  315. * - random tests are provided in tests/extended/ppro_mulmod.c
  316. */
  317. #if defined(PPRO)
  318. #if defined(ASM)
  319. /* Return (a * b) % dmod */
  320. static inline mpd_uint_t
  321. ppro_mulmod(mpd_uint_t a, mpd_uint_t b, double *dmod, uint32_t *dinvmod)
  322. {
  323. mpd_uint_t retval;
  324. __asm__ (
  325. "fildl %2\n\t"
  326. "fildl %1\n\t"
  327. "fmulp %%st, %%st(1)\n\t"
  328. "fldt (%4)\n\t"
  329. "fmul %%st(1), %%st\n\t"
  330. "flds %5\n\t"
  331. "fadd %%st, %%st(1)\n\t"
  332. "fsubrp %%st, %%st(1)\n\t"
  333. "fldl (%3)\n\t"
  334. "fmulp %%st, %%st(1)\n\t"
  335. "fsubrp %%st, %%st(1)\n\t"
  336. "fistpl %0\n\t"
  337. : "=m" (retval)
  338. : "m" (a), "m" (b), "r" (dmod), "r" (dinvmod), "m" (MPD_TWO63)
  339. : "st", "memory"
  340. );
  341. return retval;
  342. }
  343. /*
  344. * Two modular multiplications in parallel:
  345. * *a0 = (*a0 * w) % dmod
  346. * *a1 = (*a1 * w) % dmod
  347. */
  348. static inline void
  349. ppro_mulmod2c(mpd_uint_t *a0, mpd_uint_t *a1, mpd_uint_t w,
  350. double *dmod, uint32_t *dinvmod)
  351. {
  352. __asm__ (
  353. "fildl %2\n\t"
  354. "fildl (%1)\n\t"
  355. "fmul %%st(1), %%st\n\t"
  356. "fxch %%st(1)\n\t"
  357. "fildl (%0)\n\t"
  358. "fmulp %%st, %%st(1) \n\t"
  359. "fldt (%4)\n\t"
  360. "flds %5\n\t"
  361. "fld %%st(2)\n\t"
  362. "fmul %%st(2)\n\t"
  363. "fadd %%st(1)\n\t"
  364. "fsub %%st(1)\n\t"
  365. "fmull (%3)\n\t"
  366. "fsubrp %%st, %%st(3)\n\t"
  367. "fxch %%st(2)\n\t"
  368. "fistpl (%0)\n\t"
  369. "fmul %%st(2)\n\t"
  370. "fadd %%st(1)\n\t"
  371. "fsubp %%st, %%st(1)\n\t"
  372. "fmull (%3)\n\t"
  373. "fsubrp %%st, %%st(1)\n\t"
  374. "fistpl (%1)\n\t"
  375. : : "r" (a0), "r" (a1), "m" (w),
  376. "r" (dmod), "r" (dinvmod),
  377. "m" (MPD_TWO63)
  378. : "st", "memory"
  379. );
  380. }
  381. /*
  382. * Two modular multiplications in parallel:
  383. * *a0 = (*a0 * b0) % dmod
  384. * *a1 = (*a1 * b1) % dmod
  385. */
  386. static inline void
  387. ppro_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1,
  388. double *dmod, uint32_t *dinvmod)
  389. {
  390. __asm__ (
  391. "fildl %3\n\t"
  392. "fildl (%2)\n\t"
  393. "fmulp %%st, %%st(1)\n\t"
  394. "fildl %1\n\t"
  395. "fildl (%0)\n\t"
  396. "fmulp %%st, %%st(1)\n\t"
  397. "fldt (%5)\n\t"
  398. "fld %%st(2)\n\t"
  399. "fmul %%st(1), %%st\n\t"
  400. "fxch %%st(1)\n\t"
  401. "fmul %%st(2), %%st\n\t"
  402. "flds %6\n\t"
  403. "fldl (%4)\n\t"
  404. "fxch %%st(3)\n\t"
  405. "fadd %%st(1), %%st\n\t"
  406. "fxch %%st(2)\n\t"
  407. "fadd %%st(1), %%st\n\t"
  408. "fxch %%st(2)\n\t"
  409. "fsub %%st(1), %%st\n\t"
  410. "fxch %%st(2)\n\t"
  411. "fsubp %%st, %%st(1)\n\t"
  412. "fxch %%st(1)\n\t"
  413. "fmul %%st(2), %%st\n\t"
  414. "fxch %%st(1)\n\t"
  415. "fmulp %%st, %%st(2)\n\t"
  416. "fsubrp %%st, %%st(3)\n\t"
  417. "fsubrp %%st, %%st(1)\n\t"
  418. "fxch %%st(1)\n\t"
  419. "fistpl (%2)\n\t"
  420. "fistpl (%0)\n\t"
  421. : : "r" (a0), "m" (b0), "r" (a1), "m" (b1),
  422. "r" (dmod), "r" (dinvmod),
  423. "m" (MPD_TWO63)
  424. : "st", "memory"
  425. );
  426. }
  427. /* END PPRO GCC ASM */
  428. #elif defined(MASM)
  429. /* Return (a * b) % dmod */
  430. static inline mpd_uint_t __cdecl
  431. ppro_mulmod(mpd_uint_t a, mpd_uint_t b, double *dmod, uint32_t *dinvmod)
  432. {
  433. mpd_uint_t retval;
  434. __asm {
  435. mov eax, dinvmod
  436. mov edx, dmod
  437. fild b
  438. fild a
  439. fmulp st(1), st
  440. fld TBYTE PTR [eax]
  441. fmul st, st(1)
  442. fld MPD_TWO63
  443. fadd st(1), st
  444. fsubp st(1), st
  445. fld QWORD PTR [edx]
  446. fmulp st(1), st
  447. fsubp st(1), st
  448. fistp retval
  449. }
  450. return retval;
  451. }
  452. /*
  453. * Two modular multiplications in parallel:
  454. * *a0 = (*a0 * w) % dmod
  455. * *a1 = (*a1 * w) % dmod
  456. */
  457. static inline mpd_uint_t __cdecl
  458. ppro_mulmod2c(mpd_uint_t *a0, mpd_uint_t *a1, mpd_uint_t w,
  459. double *dmod, uint32_t *dinvmod)
  460. {
  461. __asm {
  462. mov ecx, dmod
  463. mov edx, a1
  464. mov ebx, dinvmod
  465. mov eax, a0
  466. fild w
  467. fild DWORD PTR [edx]
  468. fmul st, st(1)
  469. fxch st(1)
  470. fild DWORD PTR [eax]
  471. fmulp st(1), st
  472. fld TBYTE PTR [ebx]
  473. fld MPD_TWO63
  474. fld st(2)
  475. fmul st, st(2)
  476. fadd st, st(1)
  477. fsub st, st(1)
  478. fmul QWORD PTR [ecx]
  479. fsubp st(3), st
  480. fxch st(2)
  481. fistp DWORD PTR [eax]
  482. fmul st, st(2)
  483. fadd st, st(1)
  484. fsubrp st(1), st
  485. fmul QWORD PTR [ecx]
  486. fsubp st(1), st
  487. fistp DWORD PTR [edx]
  488. }
  489. }
  490. /*
  491. * Two modular multiplications in parallel:
  492. * *a0 = (*a0 * b0) % dmod
  493. * *a1 = (*a1 * b1) % dmod
  494. */
  495. static inline void __cdecl
  496. ppro_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1,
  497. double *dmod, uint32_t *dinvmod)
  498. {
  499. __asm {
  500. mov ecx, dmod
  501. mov edx, a1
  502. mov ebx, dinvmod
  503. mov eax, a0
  504. fild b1
  505. fild DWORD PTR [edx]
  506. fmulp st(1), st
  507. fild b0
  508. fild DWORD PTR [eax]
  509. fmulp st(1), st
  510. fld TBYTE PTR [ebx]
  511. fld st(2)
  512. fmul st, st(1)
  513. fxch st(1)
  514. fmul st, st(2)
  515. fld DWORD PTR MPD_TWO63
  516. fld QWORD PTR [ecx]
  517. fxch st(3)
  518. fadd st, st(1)
  519. fxch st(2)
  520. fadd st, st(1)
  521. fxch st(2)
  522. fsub st, st(1)
  523. fxch st(2)
  524. fsubrp st(1), st
  525. fxch st(1)
  526. fmul st, st(2)
  527. fxch st(1)
  528. fmulp st(2), st
  529. fsubp st(3), st
  530. fsubp st(1), st
  531. fxch st(1)
  532. fistp DWORD PTR [edx]
  533. fistp DWORD PTR [eax]
  534. }
  535. }
  536. #endif /* PPRO MASM (_MSC_VER) */
  537. /* Return (base ** exp) % dmod */
  538. static inline mpd_uint_t
  539. ppro_powmod(mpd_uint_t base, mpd_uint_t exp, double *dmod, uint32_t *dinvmod)
  540. {
  541. mpd_uint_t r = 1;
  542. while (exp > 0) {
  543. if (exp & 1)
  544. r = ppro_mulmod(r, base, dmod, dinvmod);
  545. base = ppro_mulmod(base, base, dmod, dinvmod);
  546. exp >>= 1;
  547. }
  548. return r;
  549. }
  550. #endif /* PPRO */
  551. #endif /* CONFIG_32 */
  552. #endif /* LIBMPDEC_UMODARITH_H_ */