Line data Source code
1 : /* Part of file gmp-impl.h from GNU MP.
2 :
3 : Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002 Free
4 : Software Foundation, Inc.
5 :
6 : This file contains modified code from the GNU MP Library.
7 :
8 : The GNU MP Library is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU Lesser General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or (at your
11 : option) any later version.
12 :
13 : The GNU MP Library is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 : or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 : License for more details.
17 :
18 : You should have received a copy of the GNU Lesser General Public License
19 : along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20 : the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 : MA 02110-1301, USA. */
22 :
23 : #ifndef _ECM_GMP_H
24 : #define _ECM_GMP_H 1
25 :
26 : #include "config.h"
27 : #include "basicdefs.h"
28 : #include <gmp.h>
29 :
30 : #define ABSIZ(x) ABS (SIZ (x))
31 : #define ALLOC(x) ((x)->_mp_alloc)
32 : #define PTR(x) ((x)->_mp_d)
33 : #define SIZ(x) ((x)->_mp_size)
34 : #define TMP_DECL
35 : #define TMP_ALLOC(x) alloca(x)
36 : #define TMP_MARK
37 : #define TMP_FREE
38 : #define TMP_ALLOC_TYPE(n,type) ((type *) TMP_ALLOC ((n) * sizeof (type)))
39 : #define TMP_ALLOC_LIMBS(n) TMP_ALLOC_TYPE(n,mp_limb_t)
40 :
41 : #ifndef MPZ_REALLOC
42 : #define MPZ_REALLOC(z,n) ((n) > ALLOC(z) ? _mpz_realloc(z,n) : PTR(z))
43 : #endif
44 :
45 : #ifndef MPN_COPY
46 : #include <string.h> /* for memcpy */
47 : #define MPN_COPY(d,s,n) memcpy((d),(s),(n)*sizeof(mp_limb_t))
48 : #endif
49 :
50 : #ifndef MPN_NORMALIZE
51 : #define MPN_NORMALIZE(DST, NLIMBS) \
52 : do { \
53 : while (NLIMBS > 0) \
54 : { \
55 : if ((DST)[(NLIMBS) - 1] != 0) \
56 : break; \
57 : NLIMBS--; \
58 : } \
59 : } while (0)
60 : #endif
61 :
62 : #ifndef MPN_ZERO
63 : #define MPN_ZERO(dst, n) \
64 : do { \
65 : if ((n) != 0) \
66 : { \
67 : mp_ptr __dst = (dst); \
68 : mp_size_t __n = (n); \
69 : do \
70 : *__dst++ = 0; \
71 : while (--__n); \
72 : } \
73 : } while (0)
74 : #endif
75 :
76 : /* Return non-zero if xp,xsize and yp,ysize overlap.
77 : If xp+xsize<=yp there's no overlap, or if yp+ysize<=xp there's no
78 : overlap. If both these are false, there's an overlap. */
79 : #define MPN_OVERLAP_P(xp, xsize, yp, ysize) \
80 : ((xp) + (xsize) > (yp) && (yp) + (ysize) > (xp))
81 :
82 : /* Return non-zero if xp,xsize and yp,ysize are either identical or not
83 : overlapping. Return zero if they're partially overlapping. */
84 : #define MPN_SAME_OR_SEPARATE_P(xp, yp, size) \
85 : MPN_SAME_OR_SEPARATE2_P(xp, size, yp, size)
86 : #define MPN_SAME_OR_SEPARATE2_P(xp, xsize, yp, ysize) \
87 : ((xp) == (yp) || ! MPN_OVERLAP_P (xp, xsize, yp, ysize))
88 :
89 : #ifndef mpn_com_n
90 : #define mpn_com_n(d,s,n) \
91 : do { \
92 : mp_ptr __d = (d); \
93 : mp_srcptr __s = (s); \
94 : mp_size_t __n = (n); \
95 : ASSERT (__n >= 1); \
96 : ASSERT (MPN_SAME_OR_SEPARATE_P (__d, __s, __n)); \
97 : do \
98 : *__d++ = (~ *__s++) & GMP_NUMB_MASK; \
99 : while (--__n); \
100 : } while (0)
101 : #endif
102 :
103 : #ifdef HAVE___GMPN_ADD_NC
104 : #ifndef __gmpn_add_nc
105 : __GMP_DECLSPEC mp_limb_t __gmpn_add_nc (mp_ptr, mp_srcptr, mp_srcptr,
106 : mp_size_t, mp_limb_t);
107 : #endif
108 : #endif
109 :
110 : #define ECM_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
111 :
112 : #if !defined( __MPIR_RELEASE ) && ECM_VERSION_NUM(__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR,__GNU_MP_VERSION_PATCHLEVEL) >= ECM_VERSION_NUM(5,1,0)
113 : #define MPN_REDC12_RETURNS_CARRY 1
114 : #endif
115 :
116 : /* GMP currently does not define prototypes for these, but MPIR does */
117 : #if defined(HAVE___GMPN_REDC_1) && !defined( __MPIR_RELEASE )
118 : #ifdef MPN_REDC12_RETURNS_CARRY
119 : mp_limb_t __gmpn_redc_1 (mp_ptr, mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
120 : #else
121 : void __gmpn_redc_1 (mp_ptr, mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
122 : #endif
123 : #endif
124 :
125 : #if defined(HAVE___GMPN_REDC_2) && !defined( __MPIR_RELEASE )
126 : #ifdef MPN_REDC12_RETURNS_CARRY
127 : mp_limb_t __gmpn_redc_2 (mp_ptr, mp_ptr, mp_srcptr, mp_size_t, mp_srcptr);
128 : #else
129 : void __gmpn_redc_2 (mp_ptr, mp_ptr, mp_srcptr, mp_size_t, mp_srcptr);
130 : #endif
131 : #endif
132 :
133 : #if defined(HAVE___GMPN_REDC_N)
134 : void __gmpn_redc_n (mp_ptr, mp_ptr, mp_srcptr, mp_size_t, mp_srcptr);
135 : #endif
136 :
137 : #if defined(HAVE___GMPN_MULLO_N)
138 : void __gmpn_mullo_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
139 : #endif
140 :
141 : #if defined( __MPIR_RELEASE ) && __MPIR_RELEASE >= 20600
142 :
143 : #if __MPIR_RELEASE == 20600
144 : #error MPIR 2.6 does not support GMP-ECM, please use an alternative version
145 : #endif
146 : /* WARNING - the following two definitions map the internal interface
147 : of the new FFT in MPIR 2.6 (and later) to the GMP FFT interface -
148 : they work in this context but the parameters for mpn_fft_next_size
149 : and fft_adjust_limbs have different semantics, which means that
150 : these definitions may fail if used in other circumstances
151 : */
152 : # define mpn_fft_best_k(n, k) (0)
153 : # define mpn_fft_next_size(n, k) fft_adjust_limbs(n)
154 :
155 : #else /* we are using the real GMP */
156 :
157 : #define mpn_mul_fft __gmpn_mul_fft
158 : mp_limb_t __gmpn_mul_fft (mp_ptr, mp_size_t, mp_srcptr, mp_size_t, mp_srcptr,
159 : mp_size_t, int);
160 :
161 : #define mpn_mulmod_bnm1 __gmpn_mulmod_bnm1
162 : void mpn_mulmod_bnm1 (mp_ptr, mp_size_t, mp_srcptr, mp_size_t, mp_srcptr,
163 : mp_size_t, mp_ptr);
164 :
165 : #define mpn_mul_fft_full __gmpn_mul_fft_full
166 : void __gmpn_mul_fft_full (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
167 :
168 : #define mpn_fft_next_size __gmpn_fft_next_size
169 : mp_size_t __gmpn_fft_next_size (mp_size_t, int);
170 :
171 : #define mpn_mulmod_bnm1_next_size __gmpn_mulmod_bnm1_next_size
172 : mp_size_t mpn_mulmod_bnm1_next_size (mp_size_t);
173 :
174 : #define mpn_fft_best_k __gmpn_fft_best_k
175 : int __gmpn_fft_best_k (mp_size_t, int);
176 :
177 : #endif
178 :
179 : static inline void
180 4398672 : mpz_set_uint64 (mpz_t m, const uint64_t n)
181 : {
182 : #if GMP_LIMB_BITS == 64 /* 64-bit GMP limb */
183 : if (sizeof(mp_limb_t) > sizeof(unsigned long))
184 : {
185 : mpz_set_ui (m, (unsigned long)(uint32_t)(n >> 32));
186 : mpz_mul_2exp (m, m, 32);
187 : mpz_add_ui (m, m, (unsigned long)(uint32_t)n);
188 : }
189 : else
190 4398672 : mpz_set_ui(m, (unsigned long)n);
191 :
192 : #else /* 32-bit GMP limb */
193 : mpz_set_ui (m, (unsigned long)(uint32_t)(n >> 32));
194 : mpz_mul_2exp (m, m, 32);
195 : mpz_add_ui (m, m, (unsigned long)(uint32_t)n);
196 : #endif
197 4398672 : }
198 :
199 : static inline void
200 : mpz_set_int64 (mpz_t m, const int64_t n)
201 : {
202 : if (n < 0)
203 : {
204 : mpz_set_uint64(m, -n);
205 : mpz_neg(m, m);
206 : }
207 : else
208 : mpz_set_uint64(m, n);
209 : }
210 :
211 :
212 : #endif /* _ECM_GMP_H */
|