Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
241818 views
1
#ifndef _FFWRAPPER_INCLUDE_
2
#define _FFWRAPPER_INCLUDE_
3
4
#include "gmp.h"
5
#include "ff.h"
6
7
/*
8
Copyright 2007 Andrew V. Sutherland
9
10
This file is part of smalljac.
11
12
smalljac is free software: you can redistribute it and/or modify
13
it under the terms of the GNU General Public License as published by
14
the Free Software Foundation, either version 2 of the License, or
15
(at your option) any later version.
16
17
smalljac is distributed in the hope that it will be useful,
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
GNU General Public License for more details.
21
22
You should have received a copy of the GNU General Public License
23
along with smalljac. If not, see <http://www.gnu.org/licenses/>.
24
*/
25
26
// This header file contains declarations and wrappers for code that was
27
// written for a more general multi-precision version of ff.c and needs
28
// to deal with multi-precision and initialization issues. It assume GMP is
29
// available
30
31
#define _ff_t_declare ff_t
32
#define _ff_t_declare_static static ff_t
33
#define _ff_t_declare_dynamic ff_t
34
#if FF_WORDS == 1
35
#define _ff_t_declare_reg register ff_t
36
#else
37
#define _ff_t_declare_reg ff_t
38
#endif
39
40
#define _ff_init(x)
41
#define _ff_clear(x)
42
43
#define _ff_set_mpz(x,Z) _ff_set_ui(x, mpz_fdiv_ui(Z,_ff_p))
44
#define _ff_get_mpz(Z,x) (mpz_set_ui(Z,_ff_get_ui(x)),(Z))
45
46
#define FF_MPZ_WRAPS 16
47
48
// shared global temps - of course these are not thread safe and should probably have a home
49
int _ff_wrapper_initialized;
50
mpz_t __ff_mpz_p;
51
#define _ff_mpz_p (mpz_set_ui(__ff_mpz_p,_ff_p), __ff_mpz_p)
52
53
mpz_t _ff_mpz_wraps[FF_MPZ_WRAPS]; // Wrappers used to convert to mpz for formatted output
54
#define _ff_wrap_mpz(x,i) _ff_get_mpz(_ff_mpz_wraps[i],x)
55
56
#define _ff_random(x) ((x) = ui_randomm(_ff_p))
57
#define ff_random(x) _ff_random(*(x))
58
59
// These macros are used for intermediate operations where reduction mod p may not necessary
60
// With single precision, its generally better to keep things reduced, so we don't attempt to optmize these
61
#define _ff_qneg(z,x) _ff_neg(z,x);
62
#define _ff_qnegsum(z,x,y) { _ff_neg(z,x); _ff_subfrom(z,y); }
63
#define _ff_qsub(z,x,y) _ff_sub(z,x,y);
64
#define _ff_qsubfrom(z,x) _ff_subfrom(z,x);
65
#define _ff_qadd(z,x,y) _ff_add(z,x,y);
66
#define _ff_qaddto(z,x) _ff_addto(z,x);
67
68
69
// MUST BE CALLED AT LEAST ONCE
70
static inline void _ff_wrapper_init (void)
71
{
72
int i;
73
if ( _ff_wrapper_initialized ) return;
74
mpz_init2(__ff_mpz_p, ULONG_BITS);
75
for ( i = 0 ; i < FF_MPZ_WRAPS ; i++ ) mpz_init2 (_ff_mpz_wraps[i], ULONG_BITS);
76
_ff_wrapper_initialized = 1;
77
}
78
79
static inline void ff_setup (mpz_t P)
80
{ _ff_wrapper_init(); ff_setup_ui (mpz_get_ui(P)); }
81
82
#endif
83
84