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