forked from flintlib/flint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmod_vec.h
308 lines (252 loc) · 9.52 KB
/
nmod_vec.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*=============================================================================
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
=============================================================================*/
/******************************************************************************
Copyright (C) 2010 William Hart
******************************************************************************/
#ifndef NMOD_VEC_H
#define NMOD_VEC_H
#undef ulong /* interferes with system includes */
#include <stdlib.h>
#define ulong unsigned long
#include <gmp.h>
#include "longlong.h"
#include "ulong_extras.h"
#include "flint.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
mp_limb_t n;
mp_limb_t ninv;
mp_bitcnt_t norm;
} nmod_t;
#define NMOD_RED2(r, a_hi, a_lo, mod) \
do { \
mp_limb_t q0, q1, r1; \
const mp_limb_t u1 = ((a_hi)<<(mod).norm) + r_shift((a_lo), FLINT_BITS - (mod).norm); \
const mp_limb_t u0 = ((a_lo)<<(mod).norm); \
const mp_limb_t nxx = ((mod).n<<(mod).norm); \
umul_ppmm(q1, q0, (mod).ninv, u1); \
add_ssaaaa(q1, q0, q1, q0, u1, u0); \
r1 = (u0 - (q1 + 1)*nxx); \
if (r1 >= q0) r1 += nxx; \
if (r1 < nxx) r = (r1>>(mod).norm); \
else r = ((r1 - nxx)>>(mod).norm); \
} while (0)
#define NMOD_RED(r, a, mod) \
do { \
NMOD_RED2(r, 0, a, mod); \
} while (0)
#define NMOD2_RED2(r, a_hi, a_lo, mod) \
do { \
mp_limb_t v_hi; \
NMOD_RED(v_hi, a_hi, mod); \
NMOD_RED2(r, v_hi, a_lo, mod); \
} while (0)
#define NMOD_RED3(r, a_hi, a_me, a_lo, mod) \
do { \
mp_limb_t v_hi; \
NMOD_RED2(v_hi, a_hi, a_me, mod); \
NMOD_RED2(r, v_hi, a_lo, mod); \
} while (0)
#define NMOD_ADDMUL(r, a, b, mod) \
do { \
mp_limb_t a_hi, a_lo; \
umul_ppmm(a_hi, a_lo, a, b); \
add_ssaaaa(a_hi, a_lo, a_hi, a_lo, (mp_limb_t) 0, r); \
NMOD_RED2(r, a_hi, a_lo, mod); \
} while (0)
static __inline__
mp_limb_t _nmod_add(mp_limb_t a, mp_limb_t b, nmod_t mod)
{
const mp_limb_t sum = a + b;
return sum - mod.n + ((((mp_limb_signed_t)(sum - mod.n))>>(FLINT_BITS - 1)) & mod.n);
}
static __inline__
mp_limb_t _nmod_sub(mp_limb_t a, mp_limb_t b, nmod_t mod)
{
const mp_limb_t diff = a - b;
return ((((mp_limb_signed_t)diff)>>(FLINT_BITS - 1)) & mod.n) + diff;
}
static __inline__
mp_limb_t nmod_add(mp_limb_t a, mp_limb_t b, nmod_t mod)
{
const mp_limb_t neg = mod.n - a;
if (neg > b)
return a + b;
else
return b - neg;
}
static __inline__
mp_limb_t nmod_sub(mp_limb_t a, mp_limb_t b, nmod_t mod)
{
const mp_limb_t diff = a - b;
if (a < b)
return mod.n + diff;
else
return diff;
}
static __inline__
mp_limb_t nmod_neg(mp_limb_t a, nmod_t mod)
{
if (a)
return mod.n - a;
else
return 0;
}
static __inline__
mp_limb_t nmod_mul(mp_limb_t a, mp_limb_t b, nmod_t mod)
{
return n_mulmod2_preinv(a, b, mod.n, mod.ninv);
}
static __inline__
mp_limb_t nmod_inv(mp_limb_t a, nmod_t mod)
{
return n_invmod(a, mod.n);
}
static __inline__
mp_limb_t nmod_div(mp_limb_t a, mp_limb_t b, nmod_t mod)
{
b = n_invmod(b, mod.n);
return n_mulmod2_preinv(a, b, mod.n, mod.ninv);
}
static __inline__
mp_limb_t nmod_pow_ui(mp_limb_t a, ulong exp, nmod_t mod)
{
return n_powmod2_ui_preinv(a, exp, mod.n, mod.ninv);
}
static __inline__
void nmod_init(nmod_t * mod, mp_limb_t n)
{
mod->n = n;
mod->ninv = n_preinvert_limb(n);
count_leading_zeros(mod->norm, n);
}
static __inline__
mp_ptr _nmod_vec_init(long len)
{
return (mp_ptr) flint_malloc(len * sizeof(mp_limb_t));
}
static __inline__
void _nmod_vec_clear(mp_ptr vec)
{
flint_free(vec);
}
void _nmod_vec_randtest(mp_ptr vec, flint_rand_t state, long len, nmod_t mod);
static __inline__
void _nmod_vec_zero(mp_ptr vec, long len)
{
flint_mpn_zero(vec, len);
}
mp_bitcnt_t _nmod_vec_max_bits(mp_srcptr vec, long len);
static __inline__
void _nmod_vec_set(mp_ptr res, mp_srcptr vec, long len)
{
flint_mpn_copyi(res, vec, len);
}
static __inline__
void _nmod_vec_swap(mp_ptr a, mp_ptr b, long length)
{
long i;
for (i = 0; i < length; i++)
{
mp_limb_t t = a[i];
a[i] = b[i];
b[i] = t;
}
}
static __inline__
int _nmod_vec_equal(mp_ptr vec, mp_srcptr vec2, long len)
{
long i;
for (i = 0; i < len; i++)
if (vec[i] != vec2[i]) return 0;
return 1;
}
static __inline__
int _nmod_vec_is_zero(mp_srcptr vec, long len)
{
long i;
for (i = 0; i < len; i++)
if (vec[i] != 0) return 0;
return 1;
}
void _nmod_vec_reduce(mp_ptr res, mp_srcptr vec,
long len, nmod_t mod);
void _nmod_vec_add(mp_ptr res, mp_srcptr vec1,
mp_srcptr vec2, long len, nmod_t mod);
void _nmod_vec_sub(mp_ptr res, mp_srcptr vec1,
mp_srcptr vec2, long len, nmod_t mod);
void _nmod_vec_neg(mp_ptr res, mp_srcptr vec,
long len, nmod_t mod);
void _nmod_vec_scalar_mul_nmod(mp_ptr res, mp_srcptr vec,
long len, mp_limb_t c, nmod_t mod);
void _nmod_vec_scalar_addmul_nmod(mp_ptr res, mp_srcptr vec,
long len, mp_limb_t c, nmod_t mod);
int _nmod_vec_dot_bound_limbs(long len, nmod_t mod);
#define NMOD_VEC_DOT(res, i, len, expr1, expr2, mod, nlimbs) \
do \
{ \
mp_limb_t s0, s1, s2, t0, t1; \
s0 = s1 = s2 = 0UL; \
switch (nlimbs) \
{ \
case 1: \
for (i = 0; i < len; i++) \
{ \
s0 += (expr1) * (expr2); \
} \
NMOD_RED(s0, s0, mod); \
break; \
case 2: \
if (mod.n <= (1UL << (FLINT_BITS / 2))) \
{ \
for (i = 0; i < len; i++) \
{ \
t0 = (expr1) * (expr2); \
add_ssaaaa(s1, s0, s1, s0, 0, t0); \
} \
} \
else \
{ \
for (i = 0; i < len; i++) \
{ \
umul_ppmm(t1, t0, (expr1), (expr2)); \
add_ssaaaa(s1, s0, s1, s0, t1, t0); \
} \
} \
NMOD2_RED2(s0, s1, s0, mod); \
break; \
default: \
for (i = 0; i < len; i++) \
{ \
umul_ppmm(t1, t0, (expr1), (expr2)); \
add_sssaaaaaa(s2, s1, s0, s2, s1, s0, 0, t1, t0); \
} \
NMOD_RED(s2, s2, mod); \
NMOD_RED3(s0, s2, s1, s0, mod); \
break; \
} \
res = s0; \
} while (0);
mp_limb_t _nmod_vec_dot(mp_srcptr vec1, mp_srcptr vec2,
long len, nmod_t mod, int nlimbs);
mp_limb_t _nmod_vec_dot_ptr(mp_srcptr vec1, mp_ptr * const vec2, long offset,
long len, nmod_t mod, int nlimbs);
#ifdef __cplusplus
}
#endif
#endif