-
Notifications
You must be signed in to change notification settings - Fork 0
/
flint.h
267 lines (222 loc) · 5.88 KB
/
flint.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===============================================================================*/
/******************************************************************************
flint.h
Main header file for FLINT.
(C) 2006 William Hart and David Harvey
******************************************************************************/
#ifndef FLINT_H
#define FLINT_H
#include <limits.h>
#include <assert.h>
#include <gmp.h>
#include <math.h>
#include <stdint.h>
#include "longlong_wrapper.h"
#ifdef __TINYC__
#include "longlong.h"
#define sqrtf(xxx) sqrt(xxx)
#define floorf(xxx) floor(xxx)
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifndef mpz_div_2exp
#define mpz_div_2exp mpz_tdiv_q_2exp
#endif
#ifndef mpz_div_ui
#define mpz_div_ui mpz_tdiv_q_ui
#endif
#if 0
#define FLINT_ASSERT assert
#else
#define FLINT_ASSERT(zzz_dummy)
#endif
#ifndef __USE_ISOC99
#define round(x) floor(x + 0.5)
#endif
#define FLINT_MAX(zzz1, zzz2) ((zzz1) > (zzz2) ? (zzz1) : (zzz2))
#define FLINT_MIN(zzz1, zzz2) ((zzz1) > (zzz2) ? (zzz2) : (zzz1))
#define FLINT_ABS(zzz) ((long)(zzz) < 0 ? (-zzz) : (zzz))
#define WANT_ASSERT 0
/*
FLINT_BITS is the number of bits per limb.
*/
#if ULONG_MAX == 4294967295U
#define FLINT_BITS 32
#define FLINT_D_BITS 32
#define FLINT_LG_BITS_PER_LIMB 5
#define FLINT_BYTES_PER_LIMB 4
#define FLINT_LG_BYTES_PER_LIMB 2
#elif ULONG_MAX == 18446744073709551615U
#define FLINT_BITS 64
#define FLINT_D_BITS 53
#define FLINT_LG_BITS_PER_LIMB 6
#define FLINT_BYTES_PER_LIMB 8
#define FLINT_LG_BYTES_PER_LIMB 3
#else
// only 32 and 64 bits are supported
#error FLINT requires that unsigned long is 32 bits or 64 bits
#endif
/*
Cache/branch hints to speed up reads from data in memory/branches
*/
#if defined(__GNUC__)
#define FLINT_PREFETCH(addr,n) __builtin_prefetch((unsigned long*)addr+n,1,0)
#define LIKELY(cond) (__builtin_expect ((cond) != 0, 1))
#define UNLIKELY(cond) (__builtin_expect ((cond) != 0, 0))
#elif defined(_MSC_VER) && _MSC_VER >= 1400
#define FLINT_PREFETCH(addr,n) PreFetchCacheLine(PF_TEMPORAL_LEVEL_1, (unsigned long*)addr+n)
#define LIKELY(cond)
#define UNLIKELY(cond)
#else
#define FLINT_PREFETCH(addr,n)
#define LIKELY(cond) (cond)
#define UNLIKELY(cond) (cond)
#endif
/*
Thread stuff
*/
#define THREAD
#ifdef FLINT_TEST_SUPPORT_H
#define FLINT_THREAD_CLEANUP \
do { \
if (omp_get_thread_num() != 0) \
{ \
flint_stack_cleanup(); \
flint_test_support_cleanup(); \
} \
} while (0);
#else
#define FLINT_THREAD_CLEANUP \
do { \
if (omp_get_thread_num() != 0) \
{ \
flint_stack_cleanup(); \
} \
} while (0);
#endif
/*
Cache size in bytes.
*/
#define FLINT_CACHE_SIZE 65536
#define FLINT_POL_DIV_1_LENGTH 10
#define ulong unsigned long
#if FLINT_BITS == 32
#define half_ulong uint16_t
#define half_long int16_t
#define HALF_FLINT_BITS 16
#else
#define half_ulong uint32_t
#define half_long int32_t
#define HALF_FLINT_BITS 32
#endif
#if defined(__GNUC__)
#if FLINT_BITS == 64
#define count_lead_zeros(a,b) \
a = __builtin_clzll(b);
#define count_trail_zeros(a,b) \
a = __builtin_ctzll(b);
#else
#define count_lead_zeros(a,b) \
a = __builtin_clzl(b);
#define count_trail_zeros(a,b) \
a = __builtin_ctzl(b);
#endif
#else
#ifdef __TINYC__
#define count_lead_zeros(c,n) \
do { \
union { \
double d; \
unsigned a[2]; \
} __u; \
ASSERT ((n) != 0); \
__u.d = (UWtype) (n); \
(c) = 0x3FF + 31 - (__u.a[1] >> 20); \
} while (0)
#define COUNT_LEADING_ZEROS_0 (0x3FF + 31)^M
#else
#define count_lead_zeros(a, b) \
count_leading_zeros(a, b)
#endif // __TINYCC__
#define count_trail_zeros(a, b) \
count_trailing_zeros(a, b)
#endif
#if !defined(mpn_zero)
#define mpn_zero(xxx, nnn) \
do \
{ \
ulong ixxx; \
for (ixxx = 0; ixxx < nnn; ixxx++) \
(xxx)[ixxx] = 0UL; \
} while (0)
#endif
#if !defined(mpn_store)
#define mpn_store(xxx, nnn, yyy) \
do \
{ \
ulong ixxx; \
for (ixxx = 0; ixxx < nnn; ixxx++) \
(xxx)[ixxx] = yyy; \
} while (0)
#endif
#if !defined(mpn_copyi)
#define mpn_copyi(xxx, yyy, nnn) \
do { \
ulong ixxx; \
for (ixxx = 0; ixxx < nnn; ixxx++) \
(xxx)[ixxx] = (yyy)[ixxx]; \
} while (0)
#endif
/*
On some platforms arithmetic shifts by FLINT_BITS don't yield all zeros
So we define these macros for use in situations where this would be a problem
*/
static inline unsigned long r_shift(unsigned long in, unsigned long shift)
{
if (shift == FLINT_BITS) return 0L;
return (in>>shift);
}
static inline unsigned long l_shift(unsigned long in, unsigned long shift)
{
if (shift == FLINT_BITS) return 0L;
return (in<<shift);
}
static inline unsigned long FLINT_BIT_COUNT(unsigned long x)
{
unsigned long zeros = FLINT_BITS;
if (x) count_lead_zeros(zeros, x);
return FLINT_BITS - zeros;
}
/*
Returns ceil(log2(x)).
If x == 0, returns 0.
*/
static inline unsigned long ceil_log2(unsigned long x)
{
unsigned long result = 0;
if (x > 1)
{
x--;
result = FLINT_BIT_COUNT(x);
}
return result;
}
#ifdef __cplusplus
}
#endif
#endif
// end of file ****************************************************************