arith.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Declarations for arithmetic types and functions.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef __KP_ARITH__
  14. #define __KP_ARITH__ 1
  15. #include "interp.hpp"
  16. KP_DECLS_BEGIN
  17. /* We'll be using a similar convention to GMP, but with vastly
  18. * simpler (and slower) algorithms. */
  19. /* Limbs make up long integers and long floats. We settle at 32 bits in
  20. * order to simplify things, and so that they're easier to convert from
  21. * regular ints. */
  22. #define KP_LIMB_BITS 32
  23. const int LIMB_BITS = KP_LIMB_BITS;
  24. typedef uint32_t limb_t;
  25. typedef uint64_t twolimb_t;
  26. // Normalize the limbs at [PTR .. PTR + LEN)
  27. template <typename T>
  28. inline void uitrim (limb_t *ptr, T& len)
  29. {
  30. for (; len > 0 && ptr[len - 1] == 0; --len) ;
  31. }
  32. // Compare XP and YP, up to N limbs.
  33. KP_EXPORT int uicmpn (const limb_t *xp, const limb_t *yp, int n);
  34. // Compare [XP .. XP + XL) with [YP .. YP + YL)
  35. KP_EXPORT int uicmp (const limb_t *xp, int xl, const limb_t *yp, int yl);
  36. // Add VAL to [SRC .. SRC + LEN), placing the result at DST.
  37. KP_EXPORT limb_t uiadd1 (limb_t *dst, const limb_t *src, int len, limb_t val);
  38. // Add LEN limbs from XP and YP, placing the result at DST.
  39. KP_EXPORT int uiaddn (limb_t *dst,
  40. const limb_t *xp, const limb_t *yp, int len);
  41. // Add [YP .. YP + YL) to [XP .. XP + XL), placing the result at DST.
  42. KP_EXPORT int uiadd (limb_t *dst,
  43. const limb_t *xp, int xl,
  44. const limb_t *yp, int yl);
  45. // Subtract VAL from [SRC .. SRC + LEN), placing the result at DST.
  46. KP_EXPORT limb_t uisub1 (limb_t *dst, const limb_t *src, int len, limb_t val);
  47. // Subtract LEN limbs from XP and YP, placing the result at DST. */
  48. KP_EXPORT int uisubn (limb_t *dst,
  49. const limb_t *xp, const limb_t *yp, int len);
  50. // Subtract [YP .. YP + YL) from [XP .. XP + XL), placing the result at DST.
  51. KP_EXPORT int uisub (limb_t *dst, const limb_t *xp,
  52. int xl, const limb_t *yp, int yl);
  53. /* Left shift [SRC .. SRC + LEN) by SHIFT bits, placing the result at DST.
  54. * SHIFT cannot exceed 32 bits. */
  55. KP_EXPORT int uilsh (limb_t *dst, const limb_t *src, int len, int shift);
  56. // Multiply [SRC .. SRC + LEN) by 2^EXPO, and place the result at DST.
  57. KP_EXPORT int uimul2exp (limb_t *dst, const limb_t *src, int len, int expo);
  58. /* Right shift [SRC .. SRC + LEN) by SHIFT bits, placing the result at DST.
  59. * SHIFT cannot exceed 32 bits. */
  60. KP_EXPORT int uirsh (limb_t *dst, const limb_t *src, int len, int shift);
  61. // Divide [SRC .. SRC + LEN) by 2^EXPO, placing the result at DST.
  62. KP_EXPORT int uidiv2exp (limb_t *dst, const limb_t *src, int len, int expo);
  63. // Multiply [SRC .. SRC + LEN) by VAL, placing the result in DST.
  64. KP_EXPORT int uimul1 (limb_t *dst, const limb_t *src, int len, limb_t val);
  65. // Multiply [XP .. XP + XL) by [YP ..YP + YL), placing the result at DST.
  66. KP_EXPORT limb_t uimul (interpreter *interp, limb_t *dst,
  67. const limb_t *xp, int xl,
  68. const limb_t *yp, int yl);
  69. // Square [SRC .. SRC + LEN), placing the results at DST.
  70. KP_EXPORT limb_t uisqr (interpreter *interp,
  71. limb_t *dst, const limb_t *src, int len);
  72. /* Divide [SRC .. SRC + LEN) by VAL, placing the result at DST,
  73. * and returning the remainder. */
  74. KP_EXPORT limb_t uidivrem1 (limb_t *dst,
  75. const limb_t *src, int len, limb_t val);
  76. /* Divide [XP .. XP + XL) by [YP .. YP + YL). If REM_P is true,
  77. * compute the remainder as well. The quotient is stored at
  78. * [DST + YL .. DST + XL - YL), and the remainder at [DST .. DST + YL). */
  79. KP_EXPORT int uidiv (interpreter *interp,
  80. const limb_t *xp, int xl,
  81. const limb_t *yp, int yl,
  82. limb_t *dst, bool rem_p);
  83. /* For LEN limbs, the most significant being MSD, compute the needed number
  84. * characters to produce a string in radix RADIX. */
  85. KP_EXPORT int uibsize (int radix, limb_t msd, int len);
  86. /* Given a string of length LEN in radix RADIX, compute the number of limbs
  87. * needed to represent the integer number. */
  88. KP_EXPORT int invbsize (int radix, int len);
  89. /* Convert [SRC .. SRC + LEN) into a string of size CAP in *DST,
  90. * using RADIX as the radix. Returns the resulting string's length. */
  91. KP_EXPORT int uitostr (interpreter *interp, char **dst,
  92. int cap, const limb_t *src, int len, int radix);
  93. /* Convert the string at [SRC .. SRC + LEN) into a limb array, placing
  94. * the result at DST, and using RADIX as the radix. */
  95. KP_EXPORT int strtoui (limb_t *dst,
  96. const char *src, int len, int radix);
  97. // Same as above, for single limbs.
  98. KP_EXPORT char* uitostr1 (char *dst, limb_t val, int radix);
  99. KP_EXPORT intptr_t strtoui1 (const char *src, int len, int radix);
  100. #ifdef __GNUC__
  101. inline unsigned int uiclz (limb_t val)
  102. {
  103. return (__builtin_clz (val));
  104. }
  105. #elif defined (KP_PLATFORM_UNIX)
  106. #include <strings.h>
  107. inline unsigned int uiclz (limb_t val)
  108. {
  109. return (LIMB_BITS - 1 - ffsl (val));
  110. }
  111. #else
  112. extern int uiclz (limb_t val);
  113. #endif
  114. // Convert a floating point value VAL into a limb array.
  115. KP_EXPORT int dbltoui (double val, limb_t *dst, int& len);
  116. // Convert [SRC .. SRC + LEN) with exponent EXPO to a floating point value.
  117. KP_EXPORT double uitodbl (const limb_t *src, int len, int expo);
  118. /* Convert a long float indicated by [SRC .. SRC + LEN), with exponent *EXPP
  119. * into a string, placing the result a DST. Produce no more than DIGS digits,
  120. * and use RADIX as the radix. */
  121. KP_EXPORT int lftostr (interpreter *interp, char *dst,
  122. int *expp, const limb_t *src, int len, int digs, int radix);
  123. struct num_info
  124. {
  125. int type;
  126. int sign;
  127. int radix;
  128. int dec_start;
  129. int dec_end;
  130. int frac_end;
  131. int got_dot;
  132. int expo_sign;
  133. int expo_start;
  134. };
  135. /* Parse the string [SRC .. SRC + LEN), and determine the numeric
  136. * type it evaluates to. Store the information at INFO. */
  137. KP_EXPORT int parse_num (interpreter *interp, const char *src,
  138. int len, num_info& info);
  139. /* Transform [SRC .. SRC + SLEN) into a series of limbs, and multiply
  140. * it by radix^EXPO, taking the radix, as well as any necessary numeric data
  141. * from INFO. Place the result at OUTP. */
  142. KP_EXPORT int strtolf (interpreter *interp, const char *src,
  143. int slen, const num_info& info, int& expo, limb_t *outp);
  144. union uival
  145. {
  146. twolimb_t qv;
  147. struct
  148. {
  149. #ifdef KP_LITTLE_ENDIAN
  150. limb_t lo, hi;
  151. #else
  152. limb_t hi, lo;
  153. #endif
  154. } limbs;
  155. void cuthi ()
  156. {
  157. #ifdef KP_ARCH_WIDE
  158. this->qv >>= LIMB_BITS;
  159. #else
  160. this->limbs.lo = this->limbs.hi;
  161. this->limbs.hi = 0;
  162. #endif
  163. }
  164. // Compute M1 * M2 and store the result in *this.
  165. void qmul (limb_t m1, limb_t m2)
  166. {
  167. this->qv = (twolimb_t)m1 * m2;
  168. }
  169. // Compute M1 * M2 + ADD and store the result in *this.
  170. void qmuladd (limb_t m1, limb_t m2, limb_t add)
  171. {
  172. this->qmul (m1, m2);
  173. this->qv += add;
  174. }
  175. };
  176. KP_DECLS_END
  177. #endif