Skip to content

Commit 3604bb2

Browse files
committed
Remove mandatory gmp.h inclusion from fmpz.h
Functions that has an output or input with a GMP type now requires gmp.h to be included first.
1 parent 9b8c137 commit 3604bb2

5 files changed

Lines changed: 130 additions & 73 deletions

File tree

doc/source/fmpz.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ Types, macros and constants
135135
Initialises a new ``mpz_t`` and returns a pointer to it. This is only used
136136
internally.
137137

138+
**Note:** Requires that ``gmp.h`` has been included before any FLINT
139+
header is included.
140+
138141
.. function:: void _fmpz_clear_mpz(fmpz f)
139142

140143
Clears the ``mpz_t`` "pointed to" by the ``fmpz`` `f`. This is only used
@@ -153,6 +156,9 @@ Types, macros and constants
153156
If `f` doesn't represent an ``mpz_t``, initialise one and associate it to
154157
`f`.
155158

159+
**Note:** Requires that ``gmp.h`` has been included before any FLINT
160+
header is included.
161+
156162
.. function:: mpz_ptr _fmpz_promote_val(fmpz_t f)
157163

158164
If `f` doesn't represent an ``mpz_t``, initialise one and associate it to
@@ -163,6 +169,9 @@ Types, macros and constants
163169
inconsistent state with respect to the other Flint ``fmpz`` functions such as
164170
``fmpz_is_zero``, etc.
165171

172+
**Note:** Requires that ``gmp.h`` has been included before any FLINT
173+
header is included.
174+
166175
.. function:: void _fmpz_demote(fmpz_t f)
167176

168177
If `f` represents an ``mpz_t`` clear it and make `f` just represent an
@@ -303,10 +312,16 @@ Conversion
303312
Sets `f` to the ``mpf_t`` `x`, rounding down towards zero if
304313
the value of `x` is fractional.
305314

315+
**Note:** Requires that ``gmp.h`` has been included before any FLINT
316+
header is included.
317+
306318
.. function:: void fmpz_get_mpf(mpf_t x, const fmpz_t f)
307319

308320
Sets the value of the ``mpf_t`` `x` to the value of `f`.
309321

322+
**Note:** Requires that ``gmp.h`` has been included before any FLINT
323+
header is included.
324+
310325
.. function:: void fmpz_get_mpfr(mpfr_t x, const fmpz_t f, mpfr_rnd_t rnd)
311326

312327
Sets the value of `x` from `f`, rounded toward the given
@@ -325,6 +340,9 @@ Conversion
325340

326341
Sets the ``mpz_t`` `x` to the same value as `f`.
327342

343+
**Note:** Requires that ``gmp.h`` has been included before any FLINT
344+
header is included.
345+
328346
.. function:: int fmpz_get_mpn(nn_ptr * n, fmpz_t n_in)
329347

330348
Sets the ``nn_ptr`` `n` to the same value as `n_{in}`. Returned
@@ -421,6 +439,9 @@ Conversion
421439

422440
Sets `f` to the given ``mpz_t`` value.
423441

442+
**Note:** Requires that ``gmp.h`` has been included before any FLINT
443+
header is included.
444+
424445
.. function:: int fmpz_set_str(fmpz_t f, const char * str, int b)
425446

426447
Sets `f` to the value given in the null-terminated string ``str``,
@@ -471,10 +492,16 @@ Conversion
471492
_fmpz_demote_val(f);
472493
}
473494

495+
**Note:** Requires that ``gmp.h`` has been included before any FLINT
496+
header is included.
497+
474498
.. function:: void flint_mpz_clear_readonly(mpz_t z)
475499

476500
Clears the readonly ``mpz_t`` `z`.
477501

502+
**Note:** Requires that ``gmp.h`` has been included before any FLINT
503+
header is included.
504+
478505
.. function:: void fmpz_init_set_readonly(fmpz_t f, const mpz_t z)
479506

480507
Sets the uninitialised ``fmpz_t`` `f` to a readonly
@@ -498,6 +525,9 @@ Conversion
498525
fmpz_clear_readonly(f);
499526
}
500527

528+
**Note:** Requires that ``gmp.h`` has been included before any FLINT
529+
header is included.
530+
501531
.. function:: void fmpz_clear_readonly(fmpz_t f)
502532

503533
Clears the readonly ``fmpz_t`` `f`.

src/flint.h.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ typedef fmpq fmpq_t[1];
478478

479479
/* Conversions between mpz_ptr and fmpz_t */
480480
#define PTR_TO_COEFF(x) (((ulong) (x) >> 2) | (WORD(1) << (FLINT_BITS - 2)))
481-
#define COEFF_TO_PTR(x) ((mpz_ptr) (((ulong)x) << 2))
481+
#define COEFF_TO_PTR(x) ((mpz_ptr) ((ulong) (x) << 2))
482482

483483
#define COEFF_IS_MPZ(x) (((x) >> SMALL_FMPZ_BITCOUNT_MAX) == WORD(1))
484484

src/fmpz.h

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,33 @@
1818
#define FMPZ_INLINE static inline
1919
#endif
2020

21-
#include <gmp.h>
2221
#include "fmpz_types.h"
2322
#include "longlong.h"
2423

2524
#ifdef __cplusplus
26-
extern "C" {
25+
extern "C" {
2726
#endif
2827

2928
/* Memory management *********************************************************/
3029

30+
#if defined(__GMP_H__)
3131
mpz_ptr _fmpz_new_mpz(void);
3232

33+
mpz_ptr _fmpz_promote(fmpz_t f);
34+
mpz_ptr _fmpz_promote_val(fmpz_t f);
35+
36+
void _fmpz_init_readonly_mpz(fmpz_t f, const mpz_t z);
37+
void flint_mpz_init_set_readonly(mpz_t z, const fmpz_t f);
38+
void fmpz_init_set_readonly(fmpz_t f, const mpz_t z);
39+
40+
void flint_mpz_clear_readonly(mpz_t z);
41+
void _fmpz_clear_readonly_mpz(mpz_t);
42+
#endif
43+
3344
void _fmpz_clear_mpz(fmpz f);
3445
void _fmpz_cleanup_mpz_content(void);
3546
void _fmpz_cleanup(void);
3647

37-
mpz_ptr _fmpz_promote(fmpz_t f);
38-
mpz_ptr _fmpz_promote_val(fmpz_t f);
39-
4048
FMPZ_INLINE
4149
void _fmpz_demote(fmpz_t f)
4250
{
@@ -60,6 +68,8 @@ void _fmpz_promote_neg_ui(fmpz_t f, ulong v);
6068
void _fmpz_init_promote_set_ui(fmpz_t f, ulong v);
6169
void _fmpz_init_promote_set_si(fmpz_t f, slong v);
6270

71+
void fmpz_set(fmpz_t f, const fmpz_t g);
72+
6373
FMPZ_INLINE
6474
void fmpz_init_set(fmpz_t f, const fmpz_t g)
6575
{
@@ -69,10 +79,8 @@ void fmpz_init_set(fmpz_t f, const fmpz_t g)
6979
}
7080
else
7181
{
72-
mpz_ptr ptr;
73-
ptr = _fmpz_new_mpz();
74-
*f = PTR_TO_COEFF(ptr);
75-
mpz_set(ptr, COEFF_TO_PTR(*g));
82+
*f = 0;
83+
fmpz_set(f, g);
7684
}
7785
}
7886

@@ -94,12 +102,6 @@ void fmpz_init_set_si(fmpz_t f, slong g)
94102
_fmpz_init_promote_set_si(f, g);
95103
}
96104

97-
void _fmpz_init_readonly_mpz(fmpz_t f, const mpz_t z);
98-
void flint_mpz_init_set_readonly(mpz_t z, const fmpz_t f);
99-
void fmpz_init_set_readonly(fmpz_t f, const mpz_t z);
100-
101-
void flint_mpz_clear_readonly(mpz_t z);
102-
void _fmpz_clear_readonly_mpz(mpz_t);
103105
void fmpz_clear_readonly(fmpz_t f);
104106

105107
int _fmpz_is_canonical(const fmpz_t x);
@@ -134,7 +136,6 @@ void fmpz_one(fmpz_t f)
134136
*f = WORD(1);
135137
}
136138

137-
void fmpz_set(fmpz_t f, const fmpz_t g);
138139
FMPZ_INLINE void fmpz_swap(fmpz_t f, fmpz_t g) { FLINT_SWAP(fmpz, *f, *g); }
139140

140141
slong fmpz_get_si(const fmpz_t f);
@@ -179,57 +180,9 @@ fmpz_neg_ui(fmpz_t f, ulong val)
179180
_fmpz_promote_neg_ui(f, val);
180181
}
181182

182-
FMPZ_INLINE void
183-
fmpz_get_uiui(ulong * hi, ulong * low, const fmpz_t f)
184-
{
185-
if (!COEFF_IS_MPZ(*f))
186-
{
187-
*low = *f;
188-
*hi = 0;
189-
}
190-
else
191-
{
192-
mpz_ptr mpz = COEFF_TO_PTR(*f);
193-
*low = mpz->_mp_d[0];
194-
*hi = mpz->_mp_size == 2 ? mpz->_mp_d[1] : 0;
195-
}
196-
}
197-
198-
FMPZ_INLINE void
199-
fmpz_set_uiui(fmpz_t f, ulong hi, ulong lo)
200-
{
201-
if (hi == 0)
202-
{
203-
fmpz_set_ui(f, lo);
204-
}
205-
else
206-
{
207-
mpz_ptr z = _fmpz_promote(f);
208-
if (z->_mp_alloc < 2)
209-
mpz_realloc2(z, 2 * FLINT_BITS);
210-
z->_mp_d[0] = lo;
211-
z->_mp_d[1] = hi;
212-
z->_mp_size = 2;
213-
}
214-
}
215-
216-
FMPZ_INLINE void
217-
fmpz_neg_uiui(fmpz_t f, ulong hi, ulong lo)
218-
{
219-
if (hi == 0)
220-
{
221-
fmpz_neg_ui(f, lo);
222-
}
223-
else
224-
{
225-
mpz_ptr z = _fmpz_promote(f);
226-
if (z->_mp_alloc < 2)
227-
mpz_realloc2(z, 2 * FLINT_BITS);
228-
z->_mp_d[0] = lo;
229-
z->_mp_d[1] = hi;
230-
z->_mp_size = -2;
231-
}
232-
}
183+
void fmpz_get_uiui(ulong * hi, ulong * low, const fmpz_t f);
184+
void fmpz_set_uiui(fmpz_t f, ulong hi, ulong lo);
185+
void fmpz_neg_uiui(fmpz_t f, ulong hi, ulong lo);
233186

234187
void fmpz_get_signed_uiui(ulong * hi, ulong * lo, const fmpz_t x);
235188

@@ -256,16 +209,18 @@ void fmpz_set_ui_array(fmpz_t out, const ulong * in, slong n);
256209
void fmpz_get_signed_ui_array(ulong * out, slong n, const fmpz_t in);
257210
void fmpz_set_signed_ui_array(fmpz_t out, const ulong * in, slong n);
258211

259-
void fmpz_get_mpz(mpz_t x, const fmpz_t f);
260-
void fmpz_set_mpz(fmpz_t f, const mpz_t x);
261-
262212
ulong fmpz_get_nmod(const fmpz_t f, nmod_t mod);
263213

264214
double fmpz_get_d(const fmpz_t f);
265215
void fmpz_set_d(fmpz_t f, double c);
266216

217+
#if defined(__GMP_H__)
218+
void fmpz_get_mpz(mpz_t x, const fmpz_t f);
219+
void fmpz_set_mpz(fmpz_t f, const mpz_t x);
220+
267221
void fmpz_get_mpf(mpf_t x, const fmpz_t f);
268222
void fmpz_set_mpf(fmpz_t f, const mpf_t x);
223+
#endif
269224

270225
#ifdef __MPFR_H
271226
void fmpz_get_mpfr(mpfr_t x, const fmpz_t f, mpfr_rnd_t rnd);
@@ -298,7 +253,7 @@ int fmpz_is_even(const fmpz_t f)
298253
if (!COEFF_IS_MPZ(*f))
299254
return !((*f) & WORD(1));
300255
else
301-
return mpz_even_p(COEFF_TO_PTR(*f));
256+
return !(FMPZ_TO_ZZ(*f)->ptr[0] & 1);
302257
}
303258

304259
FMPZ_INLINE
@@ -307,7 +262,7 @@ int fmpz_is_odd(const fmpz_t f)
307262
if (!COEFF_IS_MPZ(*f))
308263
return ((*f) & WORD(1));
309264
else
310-
return mpz_odd_p(COEFF_TO_PTR(*f));
265+
return (FMPZ_TO_ZZ(*f)->ptr[0] & 1);
311266
}
312267

313268
int fmpz_sgn(const fmpz_t f);

src/fmpz/get_set_neg_uiui.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright (C) 2024 Albin Ahlbäck
3+
4+
This file is part of FLINT.
5+
6+
FLINT is free software: you can redistribute it and/or modify it under
7+
the terms of the GNU Lesser General Public License (LGPL) as published
8+
by the Free Software Foundation; either version 3 of the License, or
9+
(at your option) any later version. See <https://www.gnu.org/licenses/>.
10+
*/
11+
12+
#include <gmp.h>
13+
#include "fmpz.h"
14+
15+
void fmpz_get_uiui(ulong * hi, ulong * low, const fmpz_t f)
16+
{
17+
if (!COEFF_IS_MPZ(*f))
18+
{
19+
*low = *f;
20+
*hi = 0;
21+
}
22+
else
23+
{
24+
mpz_ptr mpz = COEFF_TO_PTR(*f);
25+
*low = mpz->_mp_d[0];
26+
*hi = mpz->_mp_size == 2 ? mpz->_mp_d[1] : 0;
27+
}
28+
}
29+
30+
void fmpz_set_uiui(fmpz_t f, ulong hi, ulong lo)
31+
{
32+
if (hi == 0)
33+
{
34+
fmpz_set_ui(f, lo);
35+
}
36+
else
37+
{
38+
mpz_ptr z = _fmpz_promote(f);
39+
if (z->_mp_alloc < 2)
40+
mpz_realloc2(z, 2 * FLINT_BITS);
41+
z->_mp_d[0] = lo;
42+
z->_mp_d[1] = hi;
43+
z->_mp_size = 2;
44+
}
45+
}
46+
47+
void fmpz_neg_uiui(fmpz_t f, ulong hi, ulong lo)
48+
{
49+
if (hi == 0)
50+
{
51+
fmpz_neg_ui(f, lo);
52+
}
53+
else
54+
{
55+
mpz_ptr z = _fmpz_promote(f);
56+
if (z->_mp_alloc < 2)
57+
mpz_realloc2(z, 2 * FLINT_BITS);
58+
z->_mp_d[0] = lo;
59+
z->_mp_d[1] = hi;
60+
z->_mp_size = -2;
61+
}
62+
}

src/fmpz_types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
extern "C" {
1919
#endif
2020

21+
typedef struct
22+
{
23+
int alloc;
24+
int size;
25+
nn_ptr ptr;
26+
} zz_struct;
27+
28+
typedef zz_struct * zz_ptr;
29+
#define FMPZ_TO_ZZ(x) ((zz_ptr) ((ulong) (x) << 2))
30+
2131
typedef struct
2232
{
2333
int sign;

0 commit comments

Comments
 (0)