forked from flintlib/flint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmpz_single.c
159 lines (129 loc) · 3.93 KB
/
fmpz_single.c
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
/*=============================================================================
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) 2009 William Hart
******************************************************************************/
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
/* Always free larger mpz's to avoid wasting too much heap space */
#define FLINT_MPZ_MAX_CACHE_LIMBS 64
/* The number of new mpz's allocated at a time */
#define MPZ_BLOCK 64
FLINT_TLS_PREFIX __mpz_struct ** mpz_free_arr = NULL;
FLINT_TLS_PREFIX ulong mpz_free_num = 0;
FLINT_TLS_PREFIX ulong mpz_free_alloc = 0;
__mpz_struct * _fmpz_new_mpz(void)
{
if (mpz_free_num != 0)
{
return mpz_free_arr[--mpz_free_num];
}
else
{
__mpz_struct * z = flint_malloc(sizeof(__mpz_struct));
mpz_init(z);
return z;
}
}
void _fmpz_clear_mpz(fmpz f)
{
__mpz_struct * ptr = COEFF_TO_PTR(f);
if (ptr->_mp_alloc > FLINT_MPZ_MAX_CACHE_LIMBS)
mpz_realloc2(ptr, 1);
if (mpz_free_num == mpz_free_alloc)
{
mpz_free_alloc = FLINT_MAX(64, mpz_free_alloc * 2);
mpz_free_arr = flint_realloc(mpz_free_arr, mpz_free_alloc * sizeof(__mpz_struct *));
}
mpz_free_arr[mpz_free_num++] = ptr;
}
void _fmpz_cleanup_mpz_content(void)
{
ulong i;
for (i = 0; i < mpz_free_num; i++)
{
mpz_clear(mpz_free_arr[i]);
flint_free(mpz_free_arr[i]);
}
mpz_free_num = mpz_free_alloc = 0;
}
void _fmpz_cleanup(void)
{
_fmpz_cleanup_mpz_content();
flint_free(mpz_free_arr);
mpz_free_arr = NULL;
}
__mpz_struct * _fmpz_promote(fmpz_t f)
{
if (!COEFF_IS_MPZ(*f)) /* f is small so promote it first */
{
__mpz_struct * mpz_ptr = _fmpz_new_mpz();
(*f) = PTR_TO_COEFF(mpz_ptr);
return mpz_ptr;
}
else /* f is large already, just return the pointer */
return COEFF_TO_PTR(*f);
}
__mpz_struct * _fmpz_promote_val(fmpz_t f)
{
fmpz c = (*f);
if (!COEFF_IS_MPZ(c)) /* f is small so promote it */
{
__mpz_struct * mpz_ptr = _fmpz_new_mpz();
(*f) = PTR_TO_COEFF(mpz_ptr);
flint_mpz_set_si(mpz_ptr, c);
return mpz_ptr;
}
else /* f is large already, just return the pointer */
return COEFF_TO_PTR(c);
}
void _fmpz_demote_val(fmpz_t f)
{
__mpz_struct * mpz_ptr = COEFF_TO_PTR(*f);
int size = mpz_ptr->_mp_size;
if (size == 1 || size == -1)
{
ulong uval = mpz_ptr->_mp_d[0];
if (uval <= (ulong) COEFF_MAX)
{
_fmpz_clear_mpz(*f);
*f = size * (fmpz) uval;
}
}
else if (size == 0) /* value is 0 */
{
_fmpz_clear_mpz(*f);
*f = 0;
}
/* don't do anything if value has to be multi precision */
}
void _fmpz_init_readonly_mpz(fmpz_t f, const mpz_t z)
{
__mpz_struct *ptr;
*f = WORD(0);
ptr = _fmpz_promote(f);
mpz_clear(ptr);
*ptr = *z;
}
void _fmpz_clear_readonly_mpz(mpz_t z)
{
if (((z->_mp_size == 1 || z->_mp_size == -1) && (z->_mp_d[0] <= COEFF_MAX))
|| (z->_mp_size == 0))
{
mpz_clear(z);
}
}