gmp-impl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* Include file for internal GNU MP types and definitions.
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU MP Library.
  4. The GNU MP Library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or (at your
  7. option) any later version.
  8. The GNU MP Library is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with the GNU MP Library; see the file COPYING.LIB. If not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* When using gcc, make sure to use its builtin alloca. */
  16. #if ! defined (alloca) && defined (__GNUC__)
  17. #define alloca __builtin_alloca
  18. #define HAVE_ALLOCA
  19. #endif
  20. /* When using cc, do whatever necessary to allow use of alloca. For many
  21. machines, this means including alloca.h. IBM's compilers need a #pragma
  22. in "each module that needs to use alloca". */
  23. #if ! defined (alloca)
  24. /* We need lots of variants for MIPS, to cover all versions and perversions
  25. of OSes for MIPS. */
  26. #if defined (__mips) || defined (MIPSEL) || defined (MIPSEB) \
  27. || defined (_MIPSEL) || defined (_MIPSEB) || defined (__sgi) \
  28. || defined (__alpha) || defined (__sparc) || defined (sparc) \
  29. || defined (__ksr__)
  30. #include <alloca.h>
  31. #define HAVE_ALLOCA
  32. #endif
  33. #if defined (_IBMR2)
  34. #pragma alloca
  35. #define HAVE_ALLOCA
  36. #endif
  37. #if defined (__DECC)
  38. #define alloca(x) __ALLOCA(x)
  39. #define HAVE_ALLOCA
  40. #endif
  41. #endif
  42. #if (! defined (alloca) && ! defined (HAVE_ALLOCA)) \
  43. || defined (USE_STACK_ALLOC)
  44. #include "stack-alloc.h"
  45. #else
  46. #define TMP_DECL(m)
  47. #define TMP_ALLOC(x) alloca(x)
  48. #define TMP_MARK(m)
  49. #define TMP_FREE(m)
  50. #endif
  51. #ifndef NULL
  52. #define NULL ((void *) 0)
  53. #endif
  54. #if ! defined (__GNUC__)
  55. #define inline /* Empty */
  56. #endif
  57. /* Get MAX/MIN macros. */
  58. #include <sys/param.h>
  59. /* Field access macros. */
  60. #define SIZ(x) ((x)->_mp_size)
  61. #define PTR(x) ((x)->_mp_d)
  62. #define EXP(x) ((x)->_mp_exp)
  63. #define PREC(x) ((x)->_mp_prec)
  64. #define ALLOC(x) ((x)->_mp_alloc)
  65. #include "gmp-mparam.h"
  66. /* #include "longlong.h" */
  67. #if defined (__STDC__) || defined (__cplusplus)
  68. void *malloc (size_t);
  69. void *realloc (void *, size_t);
  70. void free (void *);
  71. extern void * (*_mp_allocate_func) (size_t);
  72. extern void * (*_mp_reallocate_func) (void *, size_t, size_t);
  73. extern void (*_mp_free_func) (void *, size_t);
  74. void *_mp_default_allocate (size_t);
  75. void *_mp_default_reallocate (void *, size_t, size_t);
  76. void _mp_default_free (void *, size_t);
  77. #else
  78. #define const /* Empty */
  79. #define signed /* Empty */
  80. void *malloc ();
  81. void *realloc ();
  82. void free ();
  83. extern void * (*_mp_allocate_func) ();
  84. extern void * (*_mp_reallocate_func) ();
  85. extern void (*_mp_free_func) ();
  86. void *_mp_default_allocate ();
  87. void *_mp_default_reallocate ();
  88. void _mp_default_free ();
  89. #endif
  90. /* Copy NLIMBS *limbs* from SRC to DST. */
  91. #define MPN_COPY_INCR(DST, SRC, NLIMBS) \
  92. do { \
  93. mp_size_t __i; \
  94. for (__i = 0; __i < (NLIMBS); __i++) \
  95. (DST)[__i] = (SRC)[__i]; \
  96. } while (0)
  97. #define MPN_COPY_DECR(DST, SRC, NLIMBS) \
  98. do { \
  99. mp_size_t __i; \
  100. for (__i = (NLIMBS) - 1; __i >= 0; __i--) \
  101. (DST)[__i] = (SRC)[__i]; \
  102. } while (0)
  103. #define MPN_COPY MPN_COPY_INCR
  104. /* Zero NLIMBS *limbs* AT DST. */
  105. #define MPN_ZERO(DST, NLIMBS) \
  106. do { \
  107. mp_size_t __i; \
  108. for (__i = 0; __i < (NLIMBS); __i++) \
  109. (DST)[__i] = 0; \
  110. } while (0)
  111. #define MPN_NORMALIZE(DST, NLIMBS) \
  112. do { \
  113. while (NLIMBS > 0) \
  114. { \
  115. if ((DST)[(NLIMBS) - 1] != 0) \
  116. break; \
  117. NLIMBS--; \
  118. } \
  119. } while (0)
  120. #define MPN_NORMALIZE_NOT_ZERO(DST, NLIMBS) \
  121. do { \
  122. while (1) \
  123. { \
  124. if ((DST)[(NLIMBS) - 1] != 0) \
  125. break; \
  126. NLIMBS--; \
  127. } \
  128. } while (0)
  129. /* Initialize the MP_INT X with space for NLIMBS limbs.
  130. X should be a temporary variable, and it will be automatically
  131. cleared out when the running function returns.
  132. We use __x here to make it possible to accept both mpz_ptr and mpz_t
  133. arguments. */
  134. #define MPZ_TMP_INIT(X, NLIMBS) \
  135. do { \
  136. mpz_ptr __x = (X); \
  137. __x->_mp_alloc = (NLIMBS); \
  138. __x->_mp_d = (mp_ptr) TMP_ALLOC ((NLIMBS) * BYTES_PER_MP_LIMB); \
  139. } while (0)
  140. #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
  141. do { \
  142. if ((size) < KARATSUBA_THRESHOLD) \
  143. impn_mul_n_basecase (prodp, up, vp, size); \
  144. else \
  145. impn_mul_n (prodp, up, vp, size, tspace); \
  146. } while (0);
  147. #define MPN_SQR_N_RECURSE(prodp, up, size, tspace) \
  148. do { \
  149. if ((size) < KARATSUBA_THRESHOLD) \
  150. impn_sqr_n_basecase (prodp, up, size); \
  151. else \
  152. impn_sqr_n (prodp, up, size, tspace); \
  153. } while (0);
  154. /* Structure for conversion between internal binary format and
  155. strings in base 2..36. */
  156. struct bases
  157. {
  158. /* Number of digits in the conversion base that always fits in an mp_limb_t.
  159. For example, for base 10 on a machine where a mp_limb_t has 32 bits this
  160. is 9, since 10**9 is the largest number that fits into a mp_limb_t. */
  161. int chars_per_limb;
  162. /* log(2)/log(conversion_base) */
  163. float chars_per_bit_exactly;
  164. /* base**chars_per_limb, i.e. the biggest number that fits a word, built by
  165. factors of base. Exception: For 2, 4, 8, etc, big_base is log2(base),
  166. i.e. the number of bits used to represent each digit in the base. */
  167. mp_limb_t big_base;
  168. /* A BITS_PER_MP_LIMB bit approximation to 1/big_base, represented as a
  169. fixed-point number. Instead of dividing by big_base an application can
  170. choose to multiply by big_base_inverted. */
  171. mp_limb_t big_base_inverted;
  172. };
  173. extern const struct bases __mp_bases[];
  174. extern mp_size_t __gmp_default_fp_limb_precision;
  175. /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
  176. limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
  177. If this would yield overflow, DI should be the largest possible number
  178. (i.e., only ones). For correct operation, the most significant bit of D
  179. has to be set. Put the quotient in Q and the remainder in R. */
  180. #define udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
  181. do { \
  182. mp_limb_t _ql __attribute__ ((unused)); \
  183. mp_limb_t _q, _r; \
  184. mp_limb_t _xh, _xl; \
  185. umul_ppmm (_q, _ql, (nh), (di)); \
  186. _q += (nh); /* DI is 2**BITS_PER_MP_LIMB too small */\
  187. umul_ppmm (_xh, _xl, _q, (d)); \
  188. sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl); \
  189. if (_xh != 0) \
  190. { \
  191. sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
  192. _q += 1; \
  193. if (_xh != 0) \
  194. { \
  195. sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
  196. _q += 1; \
  197. } \
  198. } \
  199. if (_r >= (d)) \
  200. { \
  201. _r -= (d); \
  202. _q += 1; \
  203. } \
  204. (r) = _r; \
  205. (q) = _q; \
  206. } while (0)
  207. /* Like udiv_qrnnd_preinv, but for any value D. DNORM is D shifted left
  208. so that its most significant bit is set. LGUP is ceil(log2(D)). */
  209. #define udiv_qrnnd_preinv2gen(q, r, nh, nl, d, di, dnorm, lgup) \
  210. do { \
  211. mp_limb_t n2, n10, n1, nadj, q1; \
  212. mp_limb_t _xh, _xl; \
  213. n2 = ((nh) << (BITS_PER_MP_LIMB - (lgup))) + ((nl) >> 1 >> (l - 1));\
  214. n10 = (nl) << (BITS_PER_MP_LIMB - (lgup)); \
  215. n1 = ((mp_limb_signed_t) n10 >> (BITS_PER_MP_LIMB - 1)); \
  216. nadj = n10 + (n1 & (dnorm)); \
  217. umul_ppmm (_xh, _xl, di, n2 - n1); \
  218. add_ssaaaa (_xh, _xl, _xh, _xl, 0, nadj); \
  219. q1 = ~(n2 + _xh); \
  220. umul_ppmm (_xh, _xl, q1, d); \
  221. add_ssaaaa (_xh, _xl, _xh, _xl, nh, nl); \
  222. _xh -= (d); \
  223. (r) = _xl + ((d) & _xh); \
  224. (q) = _xh - q1; \
  225. } while (0)
  226. /* Exactly like udiv_qrnnd_preinv, but branch-free. It is not clear which
  227. version to use. */
  228. #define udiv_qrnnd_preinv2norm(q, r, nh, nl, d, di) \
  229. do { \
  230. mp_limb_t n2, n10, n1, nadj, q1; \
  231. mp_limb_t _xh, _xl; \
  232. n2 = (nh); \
  233. n10 = (nl); \
  234. n1 = ((mp_limb_signed_t) n10 >> (BITS_PER_MP_LIMB - 1)); \
  235. nadj = n10 + (n1 & (d)); \
  236. umul_ppmm (_xh, _xl, di, n2 - n1); \
  237. add_ssaaaa (_xh, _xl, _xh, _xl, 0, nadj); \
  238. q1 = ~(n2 + _xh); \
  239. umul_ppmm (_xh, _xl, q1, d); \
  240. add_ssaaaa (_xh, _xl, _xh, _xl, nh, nl); \
  241. _xh -= (d); \
  242. (r) = _xl + ((d) & _xh); \
  243. (q) = _xh - q1; \
  244. } while (0)
  245. #if defined (__GNUC__)
  246. /* Define stuff for longlong.h. */
  247. typedef unsigned int UQItype __attribute__ ((mode (QI)));
  248. typedef int SItype __attribute__ ((mode (SI)));
  249. typedef unsigned int USItype __attribute__ ((mode (SI)));
  250. typedef int DItype __attribute__ ((mode (DI)));
  251. typedef unsigned int UDItype __attribute__ ((mode (DI)));
  252. #else
  253. typedef unsigned char UQItype;
  254. typedef long SItype;
  255. typedef unsigned long USItype;
  256. #endif
  257. typedef mp_limb_t UWtype;
  258. typedef unsigned int UHWtype;
  259. #define W_TYPE_SIZE BITS_PER_MP_LIMB
  260. /* Internal mpn calls */
  261. #define impn_mul_n_basecase __MPN(impn_mul_n_basecase)
  262. #define impn_mul_n __MPN(impn_mul_n)
  263. #define impn_sqr_n_basecase __MPN(impn_sqr_n_basecase)
  264. #define impn_sqr_n __MPN(impn_sqr_n)
  265. #ifndef _PROTO
  266. #if defined (__STDC__) || defined (__cplusplus)
  267. #define _PROTO(x) x
  268. #else
  269. #define _PROTO(x) ()
  270. #endif
  271. #endif
  272. /* Prototypes for internal mpn calls. */
  273. extern void impn_mul_n_basecase _PROTO ((mp_ptr prodp, mp_srcptr up,
  274. mp_srcptr vp, mp_size_t size))
  275. attribute_hidden;
  276. extern void impn_mul_n _PROTO ((mp_ptr prodp, mp_srcptr up, mp_srcptr vp,
  277. mp_size_t size, mp_ptr tspace))
  278. attribute_hidden;
  279. extern void impn_sqr_n_basecase _PROTO ((mp_ptr prodp, mp_srcptr up,
  280. mp_size_t size))
  281. attribute_hidden;
  282. extern void impn_sqr_n _PROTO ((mp_ptr prodp, mp_srcptr up, mp_size_t size,
  283. mp_ptr tspace))
  284. attribute_hidden;
  285. #ifndef IEEE_DOUBLE_BIG_ENDIAN
  286. #define IEEE_DOUBLE_BIG_ENDIAN 1
  287. #endif
  288. #ifndef IEEE_DOUBLE_MIXED_ENDIAN
  289. #define IEEE_DOUBLE_MIXED_ENDIAN 0
  290. #endif
  291. #if IEEE_DOUBLE_MIXED_ENDIAN
  292. union ieee_double_extract
  293. {
  294. struct
  295. {
  296. unsigned int manh:20;
  297. unsigned int exp:11;
  298. unsigned int sig:1;
  299. unsigned int manl:32;
  300. } s;
  301. double d;
  302. };
  303. #else
  304. #if IEEE_DOUBLE_BIG_ENDIAN
  305. union ieee_double_extract
  306. {
  307. struct
  308. {
  309. unsigned int sig:1;
  310. unsigned int exp:11;
  311. unsigned int manh:20;
  312. unsigned int manl:32;
  313. } s;
  314. double d;
  315. };
  316. #else
  317. union ieee_double_extract
  318. {
  319. struct
  320. {
  321. unsigned int manl:32;
  322. unsigned int manh:20;
  323. unsigned int exp:11;
  324. unsigned int sig:1;
  325. } s;
  326. double d;
  327. };
  328. #endif
  329. #endif