-
Notifications
You must be signed in to change notification settings - Fork 1
/
kernel.c
394 lines (358 loc) · 10.6 KB
/
kernel.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* This file is part of the MAYLIB libray.
Copyright 2007-2018 Patrick Pelissier
This Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with th Library; see the file COPYING.LESSER.txt.
If not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "may-impl.h"
/* The global variables of may are ALL stored in one global variable.
Faster on some archi (ARM), and cooler for THREAD */
MAY_THREAD_ATTR struct may_globals_s may_g;
struct may_common_s may_c;
/* Return the length in bytes of an expression.
(Not really tested yet) */
MAY_REGPARM size_t
may_length (may_t x)
{
size_t size;
may_size_t i, n;
switch (MAY_TYPE (x))
{
case MAY_INT_T:
size = MAY_INT_SIZE
+ mpz_size (MAY_INT (x)) * sizeof (mp_limb_t);
break;
case MAY_RAT_T:
size = MAY_RAT_SIZE
+ mpz_size (mpq_numref (MAY_RAT (x))) * sizeof (mp_limb_t)
+ mpz_size (mpq_denref (MAY_RAT (x))) * sizeof (mp_limb_t);
break;
case MAY_FLOAT_T:
size = MAY_FLOAT_SIZE
+ (1+(mpfr_get_prec (MAY_FLOAT(x)) - 1) / (sizeof(mp_limb_t)*CHAR_BIT))
* sizeof (mp_limb_t);
break;
case MAY_COMPLEX_T:
size = MAY_NODE_ALLOC_SIZE (2)
+ may_length (MAY_RE(x)) + may_length (MAY_IM (x));
break;
case MAY_STRING_T:
size = MAY_NAME_SIZE (MAY_SYMBOL_SIZE(x));
break;
case MAY_DATA_T:
size = MAY_DATA_SIZE (MAY_DATA (x).size);
break;
default:
n = MAY_NODE_SIZE(x);
for (i = 0, size = MAY_NODE_ALLOC_SIZE (n) ; i < n ; i++)
size += may_length (MAY_AT(x, i));
break;
}
return size;
}
void MAY_NORETURN
may_assert_fail (const char filename[], int linenum,
const char expr[])
{
fprintf (stderr, "[MAYLIB]: assertion failed in %s.%d: %s\n",
filename, linenum, expr);
#ifdef __CYGWIN__
exit (-1); /* Can't use abort: too slow! */
#else
abort ();
#endif
}
/* Init two integers for kernel booting (positive & negative) */
static void
init_num (long n, may_t *out_pos, may_t *out_neg)
{
MAY_RECORD ();
may_t y = MAY_SLONG_C (n);
MAY_CLOSE_C (y, MAY_NUM_F|MAY_EVAL_F, may_mpz_hash (MAY_INT (y)));
MAY_SET_FLAG (y, MAY_EXPAND_F);
*out_pos = y;
/* Reuse mantissa between positive and negative value */
y = MAY_MPZ_NOCOPY_C(MAY_INT(y));
mpz_neg(MAY_INT(y), MAY_INT(y));
MAY_CLOSE_C (y, MAY_NUM_F|MAY_EVAL_F, may_mpz_hash (MAY_INT (y)));
MAY_SET_FLAG (y, MAY_EXPAND_F);
*out_neg = y;
MAY_COMPACT_2(*out_pos, *out_neg);
}
/* Init a real for kernel booting */
static may_t
init_d (int k)
{
mpfr_t f;
MAY_RECORD ();
mpfr_init2 (f, MPFR_PREC_MIN);
switch (k) {
case 0: mpfr_set_nan (f); break;
case 1: mpfr_set_inf (f, 1); break;
case -1: mpfr_set_inf (f, -1); break;
}
may_t y = MAY_MPFR_NOCOPY_C (f);
MAY_CLOSE_C (y, MAY_NUM_F|MAY_EVAL_F, may_mpfr_hash (MAY_FLOAT (y)));
MAY_SET_FLAG (y, MAY_EXPAND_F);
MAY_RET (y);
}
/* Init a rational for kernel booting */
static may_t
init_q (long n, long d)
{
MAY_RECORD ();
mpq_t q;
mpq_init (q);
mpq_set_si (q, n, d);
may_t y = may_mpq_simplify (NULL, q);
MAY_RET (y);
}
void
may_kernel_start (size_t n, int allow_extend)
{
/* Auto-enlarge if too small */
if (n < 512) {
if (n == 0) {
int i, j;
void *s = &i;
/* Find upper limit for stack size */
for (i = 10; i < (int) (CHAR_BIT*sizeof n) && s != NULL; i++) {
n = 1UL << i;
s = malloc (n);
if (s != NULL)
free (s);
}
n = 1UL << (i-2);
for (j = i-3; j > i-8; j--) {
s = malloc (n + (1UL << j));
if (s != NULL) {
free (s);
n += 1UL <<j;
}
}
} else
n = 512;
}
/* Clean up globals */
memset (&may_g, 0, sizeof may_g);
/* Start Heap */
may_heap_init (&may_g.Heap, n, n-n/4, allow_extend);
/* Set GMP to the allocated heap */
may_kernel_restart ();
/* Set MPFR exponents to their maximum: no need to save them */
mpfr_set_emin (mpfr_get_emin_min ());
mpfr_set_emax (mpfr_get_emax_max ());
/* Set default values */
may_kernel_prec (113);
may_kernel_base (10);
may_kernel_rnd (GMP_RNDN);
may_kernel_zero_cb (may_zero_fastp);
may_kernel_sort_cb (may_identical);
may_kernel_num_presimplify (1);
may_kernel_intmod (NULL);
may_kernel_intmaxsize (65536);
may_kernel_domain (MAY_COMPLEX_D);
/* Some default symbolic numbers (296 bytes used) */
for(int i = 0; i < MAY_MAX_MPZ_CONSTANT; i++) {
init_num(i,
&may_c.mpz_constant[i],
&may_c.mpz_constant[MAY_MAX_MPZ_CONSTANT+i]);
}
may_c.p_inf = init_d (1);
may_c.n_inf = init_d (-1);
may_c.nan = init_d (0);
may_c.dummy = MAY_ALLOC (sizeof (struct may_s));
MAY_OPEN_C (may_c.dummy, MAY_NUM_LIMIT);
MAY_CLOSE_C (may_c.dummy, MAY_EVAL_F, (may_hash_t)0xDEADDEAD);
may_c.half = init_q (1, 2);
may_c.n_half= init_q (-1, 2);
may_c.I = MAY_COMPLEX_C (MAY_ZERO, MAY_ONE);
MAY_CLOSE_C(may_c.I, MAY_EVAL_F|MAY_NUM_F, MAY_NEW_HASH2(MAY_ZERO, MAY_ONE));
may_c.Pi = MAY_STRING_C ("PI", MAY_REAL_POS_D);
MAY_SET_FLAG (may_c.Pi, MAY_NUM_F);
MAY_ASSERT (MAY_HASH (may_c.Pi) == may_recompute_hash (may_c.Pi));
/* Register ELIST extension (mandatory) */
may_elist_init ();
/* After initialisation of the variables of the kernel */
may_kernel_worker(0, 0);
MAY_LOG_MSG(("Starting MAYLIB with size=%ul and options=%d\n", (unsigned long) n, allow_extend));
}
void
may_kernel_end (void)
{
MAY_DEF_IF_THREAD (may_thread_quit();)
may_heap_clear (&may_g.Heap);
MAY_LOG_MSG(("Ending MAYLIB (Used:%lu MaxUsed:%lu)\n", (unsigned long) (may_g.Heap.top-may_g.Heap.base), (unsigned long) (may_g.Heap.max_top-may_g.Heap.base)));
}
/* Set the current rounding mode */
mp_rnd_t
may_kernel_rnd (mp_rnd_t rnd)
{
MAY_LOG_MSG (("New rounding mode: %d\n", rnd));
mp_rnd_t old = may_g.frame.rnd_mode;
may_g.frame.rnd_mode = rnd;
return old;
}
/* Set the current base for input / output conversion */
int
may_kernel_base (int base)
{
MAY_LOG_MSG (("New base: %d\n", base));
int old = may_g.frame.base;
if (MAY_LIKELY (base != 0))
may_g.frame.base = base;
return old;
}
/* Set the precision of the mpfr_t */
mp_prec_t
may_kernel_prec (mp_prec_t p)
{
MAY_LOG_MSG (("New base: %lu\n", (unsigned long) p));
mp_prec_t old = may_g.frame.prec;
if (MAY_LIKELY (p != 0)) {
mpfr_set_default_prec (p);
may_g.frame.prec = p;
}
return old;
}
/* Set the current Domain for variables */
may_domain_e
may_kernel_domain (may_domain_e domain)
{
MAY_LOG_MSG (("New domain: %d\n", domain));
may_domain_e old = may_g.frame.domain;
may_g.frame.domain = domain;
return old;
}
/* Set the current comparaison function for sorting expressions */
int (*may_kernel_sort_cb (int (*new)(may_t, may_t)))(may_t, may_t)
{
int (*old)(may_t, may_t) = may_g.frame.sort_cb;
if (MAY_LIKELY(new != (int (*)(may_t, may_t))0))
may_g.frame.sort_cb = new;
return old;
}
/* Set the current zero_p function, which identify zero */
int (*may_kernel_zero_cb (int (*new)(may_t)))(may_t)
{
int (*old)(may_t) = may_g.frame.zero_cb;
if (MAY_LIKELY(new != (int (*)(may_t))0))
may_g.frame.zero_cb = new;
return old;
}
/* Set the current modulo for the Integers */
may_t
may_kernel_intmod (may_t new)
{
MAY_LOG_MSG (("New IntMod: '%Y'\n", new));
may_t old = may_g.frame.intmod;
if (new != NULL) {
MAY_ASSERT (MAY_TYPE (new) == MAY_INT_T);
if (mpz_cmp_ui (MAY_INT (new), 0) == 0)
new = NULL;
}
may_g.frame.intmod = new;
return old;
}
/* Set if MAY should evaluate the float to the current
precision, or store them as strings, so that we let
futher computing setting the precision without interference */
int
may_kernel_num_presimplify (int n)
{
MAY_LOG_MSG (("New presimplify=%d\n", n));
int old = may_g.frame.num_presimplify;
may_g.frame.num_presimplify = n;
return old;
}
/* Set the maximum for the computation of integers */
unsigned long
may_kernel_intmaxsize (unsigned long n)
{
MAY_LOG_MSG (("New intmaxsize= %lu\n", (unsigned long) n));
unsigned long old = may_g.frame.intmaxsize;
may_g.frame.intmaxsize = n;
return old;
}
/* Stop the MAY kernel */
void
may_kernel_stop (void)
{
MAY_LOG_MSG (("Stop MAYLIB Kernel\n"));
/* Free MPFR cache before changing the memory handler */
mpfr_free_cache ();
/* TODO: Stop all threads ! */
/* Restore original memory handler */
mp_set_memory_functions (may_c.org_gmp_alloc, may_c.org_gmp_realloc,
may_c.org_gmp_free );
}
/* Restart the MAY kernel */
void
may_kernel_restart (void)
{
MAY_LOG_MSG (("Restart MAYLIB Kernel\n"));
/* Free MPFR cache before changing the memory handler */
mpfr_free_cache ();
mp_get_memory_functions (&may_c.org_gmp_alloc,
&may_c.org_gmp_realloc, &may_c.org_gmp_free);
/* Set the dedicated MAY functions for GMP */
mp_set_memory_functions (may_alloc, may_realloc, may_free );
}
/* Display various infos */
void
may_kernel_info (FILE *stream, const char str[])
{
char *max_top = MAX(may_g.Heap.top, may_g.Heap.max_top);
fprintf(stream, "%s -- Base:%p Top:%p Used:%lu MaxUsed:%lu Max:%lu\n",
str, may_g.Heap.base, may_g.Heap.top,
(unsigned long) (may_g.Heap.top-may_g.Heap.base),
(unsigned long) (max_top-may_g.Heap.base),
(unsigned long) (may_g.Heap.limit-may_g.Heap.base));
}
int
may_kernel_worker(int new_number, size_t size)
{
#ifdef MAY_WANT_THREAD
/* If invalid new number, get the number of CPU of the system */
if (new_number <= 0)
new_number = may_get_cpu_count();
MAY_ASSERT(new_number >= 1);
new_number = MIN (new_number, MAY_MAX_THREAD);
if (size == 0)
size = (may_g.Heap.limit - may_g.Heap.base) / new_number;
size = MAX (512, size);
/* TODO: Allow while MT system is still working */
int previous = may_thread_quit();
may_thread_init(new_number, size);
return previous;
#else
UNUSED(new_number);
UNUSED(size);
return -1;
#endif
}
/* Define function to compute the size in bits if needed */
#ifndef MAY_HAVE_BUILTIN_CLZ
MAY_REGPARM int
may_size_in_bits(unsigned long a)
{
if (MAY_UNLIKELY(a == 0))
return 0;
unsigned long p = 1;
int c = 0;
while (p <= a) {
p = 2*p;
c++;
}
return c;
}
#endif