basearith.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2008-2016 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 BASEARITH_H
  28. #define BASEARITH_H
  29. #include "mpdecimal.h"
  30. #include <stdio.h>
  31. #include "typearith.h"
  32. /* Internal header file: all symbols have local scope in the DSO */
  33. MPD_PRAGMA(MPD_HIDE_SYMBOLS_START)
  34. mpd_uint_t _mpd_baseadd(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
  35. mpd_size_t m, mpd_size_t n);
  36. void _mpd_baseaddto(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n);
  37. mpd_uint_t _mpd_shortadd(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v);
  38. mpd_uint_t _mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v,
  39. mpd_uint_t b);
  40. mpd_uint_t _mpd_baseincr(mpd_uint_t *u, mpd_size_t n);
  41. void _mpd_basesub(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
  42. mpd_size_t m, mpd_size_t n);
  43. void _mpd_basesubfrom(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n);
  44. void _mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
  45. mpd_size_t m, mpd_size_t n);
  46. void _mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
  47. mpd_uint_t v);
  48. mpd_uint_t _mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
  49. mpd_uint_t v);
  50. mpd_uint_t _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
  51. mpd_uint_t v, mpd_uint_t b);
  52. mpd_uint_t _mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
  53. mpd_uint_t v);
  54. mpd_uint_t _mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
  55. mpd_uint_t v, mpd_uint_t b);
  56. int _mpd_basedivmod(mpd_uint_t *q, mpd_uint_t *r, const mpd_uint_t *uconst,
  57. const mpd_uint_t *vconst, mpd_size_t nplusm, mpd_size_t n);
  58. void _mpd_baseshiftl(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t n,
  59. mpd_size_t m, mpd_size_t shift);
  60. mpd_uint_t _mpd_baseshiftr(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t slen,
  61. mpd_size_t shift);
  62. #ifdef CONFIG_64
  63. extern const mpd_uint_t mprime_rdx;
  64. /*
  65. * Algorithm from: Division by Invariant Integers using Multiplication,
  66. * T. Granlund and P. L. Montgomery, Proceedings of the SIGPLAN '94
  67. * Conference on Programming Language Design and Implementation.
  68. *
  69. * http://gmplib.org/~tege/divcnst-pldi94.pdf
  70. *
  71. * Variables from the paper and their translations (See section 8):
  72. *
  73. * N := 64
  74. * d := MPD_RADIX
  75. * l := 64
  76. * m' := floor((2**(64+64) - 1)/MPD_RADIX) - 2**64
  77. *
  78. * Since N-l == 0:
  79. *
  80. * dnorm := d
  81. * n2 := hi
  82. * n10 := lo
  83. *
  84. * ACL2 proof: mpd-div-words-r-correct
  85. */
  86. static inline void
  87. _mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo)
  88. {
  89. mpd_uint_t n_adj, h, l, t;
  90. mpd_uint_t n1_neg;
  91. /* n1_neg = if lo >= 2**63 then MPD_UINT_MAX else 0 */
  92. n1_neg = (lo & (1ULL<<63)) ? MPD_UINT_MAX : 0;
  93. /* n_adj = if lo >= 2**63 then lo+MPD_RADIX else lo */
  94. n_adj = lo + (n1_neg & MPD_RADIX);
  95. /* (h, l) = if lo >= 2**63 then m'*(hi+1) else m'*hi */
  96. _mpd_mul_words(&h, &l, mprime_rdx, hi-n1_neg);
  97. l = l + n_adj;
  98. if (l < n_adj) h++;
  99. t = h + hi;
  100. /* At this point t == qest, with q == qest or q == qest+1:
  101. * 1) 0 <= 2**64*hi + lo - qest*MPD_RADIX < 2*MPD_RADIX
  102. */
  103. /* t = 2**64-1 - qest = 2**64 - (qest+1) */
  104. t = MPD_UINT_MAX - t;
  105. /* (h, l) = 2**64*MPD_RADIX - (qest+1)*MPD_RADIX */
  106. _mpd_mul_words(&h, &l, t, MPD_RADIX);
  107. l = l + lo;
  108. if (l < lo) h++;
  109. h += hi;
  110. h -= MPD_RADIX;
  111. /* (h, l) = 2**64*hi + lo - (qest+1)*MPD_RADIX (mod 2**128)
  112. * Case q == qest+1:
  113. * a) h == 0, l == r
  114. * b) q := h - t == qest+1
  115. * c) r := l
  116. * Case q == qest:
  117. * a) h == MPD_UINT_MAX, l == 2**64-(MPD_RADIX-r)
  118. * b) q := h - t == qest
  119. * c) r := l + MPD_RADIX = r
  120. */
  121. *q = (h - t);
  122. *r = l + (MPD_RADIX & h);
  123. }
  124. #else
  125. static inline void
  126. _mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo)
  127. {
  128. _mpd_div_words(q, r, hi, lo, MPD_RADIX);
  129. }
  130. #endif
  131. /* Multiply two single base MPD_RADIX words, store result in array w[2]. */
  132. static inline void
  133. _mpd_singlemul(mpd_uint_t w[2], mpd_uint_t u, mpd_uint_t v)
  134. {
  135. mpd_uint_t hi, lo;
  136. _mpd_mul_words(&hi, &lo, u, v);
  137. _mpd_div_words_r(&w[1], &w[0], hi, lo);
  138. }
  139. /* Multiply u (len 2) and v (len m, 1 <= m <= 2). */
  140. static inline void
  141. _mpd_mul_2_le2(mpd_uint_t w[4], mpd_uint_t u[2], mpd_uint_t v[2], mpd_ssize_t m)
  142. {
  143. mpd_uint_t hi, lo;
  144. _mpd_mul_words(&hi, &lo, u[0], v[0]);
  145. _mpd_div_words_r(&w[1], &w[0], hi, lo);
  146. _mpd_mul_words(&hi, &lo, u[1], v[0]);
  147. lo = w[1] + lo;
  148. if (lo < w[1]) hi++;
  149. _mpd_div_words_r(&w[2], &w[1], hi, lo);
  150. if (m == 1) return;
  151. _mpd_mul_words(&hi, &lo, u[0], v[1]);
  152. lo = w[1] + lo;
  153. if (lo < w[1]) hi++;
  154. _mpd_div_words_r(&w[3], &w[1], hi, lo);
  155. _mpd_mul_words(&hi, &lo, u[1], v[1]);
  156. lo = w[2] + lo;
  157. if (lo < w[2]) hi++;
  158. lo = w[3] + lo;
  159. if (lo < w[3]) hi++;
  160. _mpd_div_words_r(&w[3], &w[2], hi, lo);
  161. }
  162. /*
  163. * Test if all words from data[len-1] to data[0] are zero. If len is 0, nothing
  164. * is tested and the coefficient is regarded as "all zero".
  165. */
  166. static inline int
  167. _mpd_isallzero(const mpd_uint_t *data, mpd_ssize_t len)
  168. {
  169. while (--len >= 0) {
  170. if (data[len] != 0) return 0;
  171. }
  172. return 1;
  173. }
  174. /*
  175. * Test if all full words from data[len-1] to data[0] are MPD_RADIX-1
  176. * (all nines). Return true if len == 0.
  177. */
  178. static inline int
  179. _mpd_isallnine(const mpd_uint_t *data, mpd_ssize_t len)
  180. {
  181. while (--len >= 0) {
  182. if (data[len] != MPD_RADIX-1) return 0;
  183. }
  184. return 1;
  185. }
  186. MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */
  187. #endif /* BASEARITH_H */