basearith.h 6.9 KB

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