1 /*
2 Copyright (C) 2009 William Hart
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 2.1 of the License, or
9 (at your option) any later version. See <https://www.gnu.org/licenses/>.
10 */
11
12 #define FMPZ_INLINES_C
13
14 #define ulong ulongxx /* interferes with system includes */
15 #include <stdlib.h>
16 #undef ulong
17 #include <gmp.h>
18 #include "flint.h"
19 #include "ulong_extras.h"
20 #include "fmpz.h"
21
__new_fmpz()22 fmpz * __new_fmpz()
23 {
24 return flint_calloc(sizeof(fmpz), 1);
25 }
26
__free_fmpz(fmpz * f)27 void __free_fmpz(fmpz * f)
28 {
29 _fmpz_demote(f);
30 flint_free(f);
31 }
32
__fmpz_set_si(fmpz_t f,slong val)33 void __fmpz_set_si(fmpz_t f, slong val)
34 {
35 if (val < COEFF_MIN || val > COEFF_MAX) /* val is large */
36 {
37 __mpz_struct *mpz_coeff = _fmpz_promote(f);
38 flint_mpz_set_si(mpz_coeff, val);
39 }
40 else
41 {
42 _fmpz_demote(f);
43 *f = val; /* val is small */
44 }
45 }
46
__fmpz_set_ui(fmpz_t f,ulong val)47 void __fmpz_set_ui(fmpz_t f, ulong val)
48 {
49 if (val > COEFF_MAX) /* val is large */
50 {
51 __mpz_struct *mpz_coeff = _fmpz_promote(f);
52 flint_mpz_set_ui(mpz_coeff, val);
53 }
54 else
55 {
56 _fmpz_demote(f);
57 *f = val; /* val is small */
58 }
59 }
60
__fmpz_init(fmpz_t f)61 void __fmpz_init(fmpz_t f)
62 {
63 (*f) = WORD(0);
64 }
65
__fmpz_init_set_ui(fmpz_t f,ulong g)66 void __fmpz_init_set_ui(fmpz_t f, ulong g)
67 {
68 if (g <= COEFF_MAX)
69 {
70 *f = g;
71 }
72 else
73 {
74 __mpz_struct *ptr;
75
76 ptr = _fmpz_new_mpz();
77 *f = PTR_TO_COEFF(ptr);
78 flint_mpz_set_ui(ptr, g);
79 }
80 }
81
__fmpz_clear(fmpz_t f)82 void __fmpz_clear(fmpz_t f)
83 {
84 _fmpz_demote(f);
85 }
86
__fmpz_lt(fmpz_t f,fmpz_t g)87 int __fmpz_lt(fmpz_t f, fmpz_t g)
88 {
89 return fmpz_cmp(f, g) < 0;
90 }
91
__fmpz_gt(fmpz_t f,fmpz_t g)92 int __fmpz_gt(fmpz_t f, fmpz_t g)
93 {
94 return fmpz_cmp(f, g) > 0;
95 }
96
__fmpz_lte(fmpz_t f,fmpz_t g)97 int __fmpz_lte(fmpz_t f, fmpz_t g)
98 {
99 return fmpz_cmp(f, g) <= 0;
100 }
101
__fmpz_gte(fmpz_t f,fmpz_t g)102 int __fmpz_gte(fmpz_t f, fmpz_t g)
103 {
104 return fmpz_cmp(f, g) >= 0;
105 }
106
__fmpz_eq(fmpz_t f,fmpz_t g)107 int __fmpz_eq(fmpz_t f, fmpz_t g)
108 {
109 return fmpz_cmp(f, g) == 0;
110 }
111
__fmpz_neq(fmpz_t f,fmpz_t g)112 int __fmpz_neq(fmpz_t f, fmpz_t g)
113 {
114 return fmpz_cmp(f, g) != 0;
115 }
116
__fmpz_init_set(fmpz_t f,const fmpz_t g)117 void __fmpz_init_set(fmpz_t f, const fmpz_t g)
118 {
119 if (!COEFF_IS_MPZ(*g))
120 {
121 *f = *g;
122 }
123 else
124 {
125 __mpz_struct *ptr;
126
127 ptr = _fmpz_new_mpz();
128 *f = PTR_TO_COEFF(ptr);
129 mpz_set(ptr, COEFF_TO_PTR(*g));
130 }
131 }
132
__fmpz_neg(fmpz_t f1,const fmpz_t f2)133 void __fmpz_neg(fmpz_t f1, const fmpz_t f2)
134 {
135 if (!COEFF_IS_MPZ(*f2)) /* coeff is small */
136 {
137 fmpz t = -*f2; /* Need to save value in case of aliasing */
138 _fmpz_demote(f1);
139 *f1 = t;
140 }
141 else /* coeff is large */
142 {
143 /* No need to retain value in promotion, as if aliased, both already large */
144 __mpz_struct *mpz_ptr = _fmpz_promote(f1);
145 mpz_neg(mpz_ptr, COEFF_TO_PTR(*f2));
146 }
147 }
148