Debian Codegraph @

src/c/big.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * big.c - bignum routines based on the GMP multiple precision
6 * integers.
7 *
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 * Copyright (c) 2015 Daniel Kochmański
11 *
12 * See file 'LICENSE' for the copyright details.
13 *
14 */
15
16 #define ECL_INCLUDE_MATH_H
17 #include <limits.h>
18 #include <string.h>
19 #include <ecl/ecl.h>
20 #include <ecl/internal.h>
21
22 /*************************************************************
23 * MEMORY MANAGEMENT WITH GMP
24 *
25 * A careful reader of the below code will note that there is no
26 * invocation of mpz_init anywhere. The reason for this is that we
27 * keep a number of initialized bignums in the thread local
28 * environment in what are called bignum registers. From these bignum
29 * registers, we obtain bignum objects that are passed around in user
30 * code by copying the contents of the mpz_t object into a compact
31 * object. This means that the bignum type and the contents of the
32 * corresponding mpz_t are allocated as a single block.
33 *
34 * A consequence of the above is that we also need not call mpz_clear,
35 * since a garbage collection of a compactly allocated bignum
36 * automatically leads to a deallocation of the contents of the
37 * corresponding mpz_t object. The exception to this rule are the
38 * bignum registers which are deallocated upon thread exit.
39 *
40 * The GMP library may also allocate temporary memory for its
41 * computations. It is configurable at runtime whether we use malloc
42 * and free or the corresponding equivalents from the garbage
43 * collector (ecl_alloc_uncollectable and ecl_free_uncollectable) for
44 * that.
45 */
46
47 /*************************************************************
48 * HOW TO USE BIGNUM REGISTERS
49 *
50 * All users of the bignum registers must use the following routine to
51 * prevent crashes and memory leaks:
52 * 1) Aquire bignum registers using _ecl_big_registerx
53 * 2) Do some computations using _ecl_big_xxx
54 * 3) Do one of the following with the bignum registers:
55 * - Extract results of the computation using
56 * _ecl_big_register_normalize (if result may be a fixnum) or
57 * _ecl_big_register_copy (if result is known to be a bignum).
58 * Do not pass the obtained value to _ecl_big_register_free.
59 * Do not directly return a bignum register!
60 * - Call _ecl_big_register_free for any registers whose value is not
61 * needed after the computation (not strictly needed, can be
62 * skipped if particularly inconvenient).
63 */
64
65 #define ECL_BIG_REGISTER_SIZE 32
66
67 #define _ecl_big_init2(x,size) mpz_init2(ecl_bignum(x),(size)*GMP_LIMB_BITS)
68 #define _ecl_big_realloc2(x,size) mpz_realloc2(ecl_bignum(x),(size)*GMP_LIMB_BITS)
69 #define _ecl_big_clear(x) mpz_clear(ecl_bignum(x))
70
71 #define _ecl_big_add(z, x, y) mpz_add(ecl_bignum(z),ecl_bignum(x),ecl_bignum(y))
72 #define _ecl_big_mul_si(z, x, y) mpz_mul_si(ecl_bignum(z),ecl_bignum(x),(y))
73
74 void
75 _ecl_big_register_free(cl_object x)
76 {
77 /* We only need to free the integer when it gets too large */
78 if (ECL_BIGNUM_DIM(x) > 4 * ECL_BIG_REGISTER_SIZE) {
79 _ecl_big_realloc2(x, ECL_BIG_REGISTER_SIZE);
80 }
81 }
82
83 static cl_object
84 _ecl_alloc_compact_bignum(cl_index limbs)
85 {
86 cl_index bytes = limbs * sizeof(mp_limb_t);
87 cl_object new_big = ecl_alloc_compact_object(t_bignum, bytes);
88 ECL_BIGNUM_LIMBS(new_big) = ECL_COMPACT_OBJECT_EXTRA(new_big);
89 ECL_BIGNUM_SIZE(new_big) = 0;
90 ECL_BIGNUM_DIM(new_big) = limbs;
91
92 return new_big;
93 }
94
95 static cl_object
96 _ecl_big_copy(cl_object old)
97 {
98 cl_fixnum size = ECL_BIGNUM_SIZE(old);
99 cl_index dim = (size < 0)? (-size) : size;
100 cl_index bytes = dim * sizeof(mp_limb_t);
101 cl_object new_big = _ecl_alloc_compact_bignum(dim);
102 ECL_BIGNUM_SIZE(new_big) = size;
103 memcpy(ECL_BIGNUM_LIMBS(new_big), ECL_BIGNUM_LIMBS(old), bytes);
104 return new_big;
105 }
106
107 cl_object
108 _ecl_big_register_copy(cl_object old)
109 {
110 cl_object new_big = _ecl_big_copy(old);
111 _ecl_big_register_free(old);
112 return new_big;
113 }
114
115 static cl_object
116 big_normalize(cl_object x)
117 {
118 int s = ECL_BIGNUM_SIZE(x);
119 if (s == 0)
120 return(ecl_make_fixnum(0));
121 if (s == 1) {
122 mp_limb_t y = ECL_BIGNUM_LIMBS(x)[0];
123 if (y <= MOST_POSITIVE_FIXNUM)
124 return ecl_make_fixnum(y);
125 } else if (s == -1) {
126 mp_limb_t y = ECL_BIGNUM_LIMBS(x)[0];
127 if (y <= -MOST_NEGATIVE_FIXNUM)
128 return ecl_make_fixnum(-y);
129 }
130 return x;
131 }
132
133 cl_object
134 _ecl_big_register_normalize(cl_object x)
135 {
136 int s = ECL_BIGNUM_SIZE(x);
137 if (s == 0)
138 return(ecl_make_fixnum(0));
139 if (s == 1) {
140 mp_limb_t y = ECL_BIGNUM_LIMBS(x)[0];
141 if (y <= MOST_POSITIVE_FIXNUM)
142 return ecl_make_fixnum(y);
143 } else if (s == -1) {
144 mp_limb_t y = ECL_BIGNUM_LIMBS(x)[0];
145 if (y <= -MOST_NEGATIVE_FIXNUM)
146 return ecl_make_fixnum(-y);
147 }
148 return _ecl_big_register_copy(x);
149 }
150
151 #if GMP_LIMB_BITS >= ECL_FIXNUM_BITS
152 static const int limbs_per_fixnum = 1;
153 #else
154 static const int limbs_per_fixnum = (ECL_FIXNUM_BITS + GMP_LIMB_BITS - 1) /
155 GMP_LIMB_BITS;
156 #endif
157
158 #define ECL_BIGNUM_ABS_SIZE(x) \
159 (ECL_BIGNUM_SIZE(x)<0? -ECL_BIGNUM_SIZE(x) : ECL_BIGNUM_SIZE(x))
160
161 cl_object
162 _ecl_fix_times_fix(cl_fixnum x, cl_fixnum y)
163 {
164 #if ECL_LONG_BITS >= ECL_FIXNUM_BITS
165 ECL_WITH_TEMP_BIGNUM(z,4);
166 _ecl_big_set_si(z, x);
167 _ecl_big_mul_si(z, z, y);
168 #else
169 ECL_WITH_TEMP_BIGNUM(z,4);
170 ECL_WITH_TEMP_BIGNUM(w,4);
171 _ecl_big_set_fixnum(z, x);
172 _ecl_big_set_fixnum(w, y);
173 _ecl_big_mul(z, z, w);
174 #endif
175 {
176 cl_object y = big_normalize(z);
177 if (y == z) y = _ecl_big_copy(z);
178 return y;
179 }
180 }
181
182 cl_object
183 _ecl_big_times_big(cl_object a, cl_object b)
184 {
185 cl_index size_a = ECL_BIGNUM_ABS_SIZE(a);
186 cl_index size_b = ECL_BIGNUM_ABS_SIZE(b);
187 cl_index size = size_a + size_b;
188 cl_object z = _ecl_alloc_compact_bignum(size);
189 _ecl_big_mul(z, a, b);
190 return z;
191 }
192
193
194 cl_object
195 _ecl_big_times_fix(cl_object b, cl_fixnum i)
196 {
197 cl_index size;
198 cl_object z;
199
200 if (i == 0)
201 return ecl_make_fixnum(0);
202 if (i == 1)
203 return b;
204 size = ECL_BIGNUM_ABS_SIZE(b);
205 size += limbs_per_fixnum;
206 z = _ecl_alloc_compact_bignum(size);
207 #if ECL_LONG_BITS >= ECL_FIXNUM_BITS
208 _ecl_big_mul_si(z, b, i);
209 #else
210 {
211 ECL_WITH_TEMP_BIGNUM(w,4);
212 _ecl_big_set_fixnum(w, i);
213 _ecl_big_mul(z, b, w);
214 }
215 #endif
216 return z;
217 }
218
219 cl_object
220 _ecl_big_plus_fix(cl_object a, cl_fixnum b)
221 {
222 ECL_WITH_TEMP_BIGNUM(big_b, 2);
223 _ecl_big_set_fixnum(big_b, b);
224 return _ecl_big_plus_big(a, big_b);
225 }
226
227 cl_object
228 _ecl_big_plus_big(cl_object a, cl_object b)
229 {
230 cl_index size_a = ECL_BIGNUM_ABS_SIZE(a);
231 cl_index size_b = ECL_BIGNUM_ABS_SIZE(b);
232 cl_index size_z = (size_a < size_b)? (size_b + 1) : (size_a + 1);
233 cl_object z = _ecl_alloc_compact_bignum(size_z);
234 _ecl_big_add(z, a, b);
235 return big_normalize(z);
236 }
237
238 cl_object
239 _ecl_big_minus_big(cl_object a, cl_object b)
240 {
241 cl_index size_a = ECL_BIGNUM_ABS_SIZE(a);
242 cl_index size_b = ECL_BIGNUM_ABS_SIZE(b);
243 cl_index size_z = (size_a < size_b)? (size_b + 1) : (size_a + 1);
244 cl_object z = _ecl_alloc_compact_bignum(size_z);
245 mpz_sub(ecl_bignum(z), ecl_bignum(a), ecl_bignum(b));
246 return big_normalize(z);
247 }
248
249 cl_object
250 _ecl_fix_minus_big(cl_fixnum a, cl_object b)
251 {
252 cl_index size_b = ECL_BIGNUM_ABS_SIZE(b);
253 cl_index size_z = size_b + limbs_per_fixnum;
254 cl_object z = _ecl_alloc_compact_bignum(size_z);
255 _ecl_big_set_fixnum(z, a);
256 mpz_sub(ecl_bignum(z), ecl_bignum(z), ecl_bignum(b));
257 return big_normalize(z);
258 }
259
260 cl_object
261 _ecl_big_negate(cl_object a)
262 {
263 cl_index size_a = ECL_BIGNUM_ABS_SIZE(a);
264 cl_object z = _ecl_alloc_compact_bignum(size_a);
265 mpz_neg(ecl_bignum(z), ecl_bignum(a));
266 return big_normalize(z);
267 }
268
269 cl_object
270 _ecl_big_divided_by_big(cl_object a, cl_object b)
271 {
272 cl_object z;
273 cl_index size_a = ECL_BIGNUM_ABS_SIZE(a);
274 cl_index size_b = ECL_BIGNUM_ABS_SIZE(b);
275 cl_fixnum size_z = size_a - size_b + 1;
276 if (size_z <= 0) size_z = 1;
277 z = _ecl_alloc_compact_bignum(size_z);
278 mpz_tdiv_q(ecl_bignum(z), ecl_bignum(a), ecl_bignum(b));
279 return big_normalize(z);
280 }
281
282 cl_object
283 _ecl_big_gcd(cl_object a, cl_object b)
284 {
285 cl_object z = _ecl_big_register0();
286 mpz_gcd(ecl_bignum(z), ecl_bignum(a), ecl_bignum(b));
287 return _ecl_big_register_normalize(z);
288 }
289
290 cl_object
291 _ecl_big_divided_by_fix(cl_object x, cl_fixnum y)
292 {
293 ECL_WITH_TEMP_BIGNUM(by, 2);
294 _ecl_big_set_fixnum(by, y);
295 return _ecl_big_divided_by_big(x, by);
296 }
297
298 cl_object
299 _ecl_big_ceiling(cl_object a, cl_object b, cl_object *pr)
300 {
301 cl_object q = _ecl_big_register0();
302 cl_object r = _ecl_big_register1();
303 mpz_cdiv_qr(ecl_bignum(q), ecl_bignum(r), ecl_bignum(a), ecl_bignum(b));
304 *pr = _ecl_big_register_normalize(r);
305 return _ecl_big_register_normalize(q);
306 }
307
308 cl_object
309 _ecl_big_floor(cl_object a, cl_object b, cl_object *pr)
310 {
311 cl_object q = _ecl_big_register0();
312 cl_object r = _ecl_big_register1();
313 mpz_fdiv_qr(ecl_bignum(q), ecl_bignum(r), ecl_bignum(a), ecl_bignum(b));
314 *pr = _ecl_big_register_normalize(r);
315 return _ecl_big_register_normalize(q);
316 }
317
318 cl_object
319 _ecl_fix_divided_by_big(cl_fixnum x, cl_object y)
320 {
321 ECL_WITH_TEMP_BIGNUM(bx, 2);
322 _ecl_big_set_fixnum(bx, x);
323 return _ecl_big_divided_by_big(bx, y);
324 }
325
326 static void *
327 mp_alloc(size_t size)
328 {
329 return ecl_alloc_uncollectable(size);
330 }
331
332 static void
333 mp_free(void *ptr, size_t size)
334 {
335 ecl_free_uncollectable(ptr);
336 }
337
338 static void *
339 mp_realloc(void *ptr, size_t osize, size_t nsize)
340 {
341 mp_limb_t *p = mp_alloc(nsize);
342 memcpy(p, ptr, (osize < nsize)? osize : nsize);
343 mp_free(ptr, osize);
344 return p;
345 }
346
347 #ifdef ECL_GMP_FIXNUM_TO_LIMBS
348 void
349 _ecl_big_set_idx(cl_object x, cl_index f)
350 {
351 if (f == 0) {
352 mpz_set_si(ecl_bignum(x), 0);
353 } else if (f > 0) {
354 ECL_BIGNUM_SIZE(x) = 1;
355 ECL_BIGNUM_LIMBS(x)[0] = f;
356 }
357 }
358
359 void
360 _ecl_big_set_fix(cl_object x, cl_fixnum f)
361 {
362 if (f == 0) {
363 mpz_set_si(ecl_bignum(x), 0);
364 } else if (f > 0) {
365 ECL_BIGNUM_SIZE(x) = 1;
366 ECL_BIGNUM_LIMBS(x)[0] = f;
367 } else if (f < 0) {
368 ECL_BIGNUM_SIZE(x) = -1;
369 ECL_BIGNUM_LIMBS(x)[0] = -f;
370 }
371 }
372
373 cl_index
374 _ecl_big_get_idx(cl_object x)
375 {
376 /* INV: x is a bignum and thus size != 0 */
377 cl_index output = ECL_BIGNUM_LIMBS(x)[0];
378 return (ECL_BIGNUM_SIZE(x) > 0)? output : ~(output - 1);
379 }
380
381 cl_fixnum
382 _ecl_big_get_fix(cl_object x)
383 {
384 /* INV: x is a bignum and thus size != 0 */
385 cl_fixnum output = ECL_BIGNUM_LIMBS(x)[0];
386 return (ECL_BIGNUM_SIZE(x) > 0) ? output : -output;
387 }
388 #endif /* ECL_GMP_FIXNUM_TO_LIMBS */
389
390 long double
391 _ecl_big_get_lf(cl_object o)
392 {
393 long double output = 0;
394 int i, l = mpz_size(ecl_bignum(o)), exp = 0;
395 for (i = 0; i < l; i++) {
396 output += ldexpl(mpz_getlimbn(ecl_bignum(o), i), exp);
397 exp += GMP_LIMB_BITS;
398 }
399 return (mpz_sgn(ecl_bignum(o)) < 0)? -output : output;
400 }
401
402
403
404 #if ECL_FIXNUM_BITS == ECL_INT_BITS
405 static inline bool
406 _ecl_big_fits_in_fixnum(cl_object x)
407 {
408 return mpz_fits_sint_p(ecl_bignum(x));
409 }
410
411 static inline bool
412 _ecl_big_fits_in_index(cl_object x)
413 {
414 return mpz_fits_uint_p(ecl_bignum(x));
415 }
416 #elif ECL_FIXNUM_BITS == ECL_LONG_BITS
417 static inline bool
418 _ecl_big_fits_in_fixnum(cl_object x)
419 {
420 return mpz_fits_slong_p(ecl_bignum(x));
421 }
422
423 static inline bool
424 _ecl_big_fits_in_index(cl_object x)
425 {
426 return mpz_fits_ulong_p(ecl_bignum(x));
427 }
428 #elif ECL_FIXNUM_BITS == ECL_LONG_LONG_BITS && GMP_LIMB_BITS >= ECL_FIXNUM_BITS
429 static inline bool
430 _ecl_big_fits_in_fixnum(cl_object x)
431 {
432 /* INV: x is a bignum and thus size != 0 */
433 return (ECL_BIGNUM_SIZE(x) == 1 && ECL_BIGNUM_LIMBS(x)[0] <= LLONG_MAX)
434 || (ECL_BIGNUM_SIZE(x) == -1 && -(ECL_BIGNUM_LIMBS(x)[0]) >= LLONG_MIN);
435 }
436
437 static inline bool
438 _ecl_big_fits_in_index(cl_object x)
439 {
440 /* INV: x is a bignum and thus size != 0 */
441 return ECL_BIGNUM_SIZE(x) == 1 && ECL_BIGNUM_LIMBS(x)[0] <= ULLONG_MAX;
442 }
443 #else
444 # error "ECL cannot build with GMP when both long and mp_limb_t are smaller than cl_fixnum"
445 #endif
446
447 cl_fixnum
448 fixint(cl_object x)
449 {
450 if (ECL_FIXNUMP(x))
451 return ecl_fixnum(x);
452 if (ECL_BIGNUMP(x)) {
453 if (_ecl_big_fits_in_fixnum(x)) {
454 return _ecl_big_get_fixnum(x);
455 }
456 }
457 FEwrong_type_argument(@[fixnum], x);
458 }
459
460 cl_index
461 fixnnint(cl_object x)
462 {
463 if (ECL_FIXNUMP(x)) {
464 cl_fixnum i = ecl_fixnum(x);
465 if (i >= 0)
466 return i;
467 } else if (ECL_BIGNUMP(x)) {
468 if (_ecl_big_fits_in_index(x)) {
469 return _ecl_big_get_index(x);
470 }
471 }
472 FEwrong_type_argument(cl_list(3, @'integer', ecl_make_fixnum(0),
473 ecl_make_fixnum(MOST_POSITIVE_FIXNUM)),
474 x);
475 }
476
477 static void
478 mpz_ior_op(cl_object out, cl_object i, cl_object j)
479 {
480 mpz_ior(ecl_bignum(out), ecl_bignum(i), ecl_bignum(j));
481 }
482
483 static void
484 mpz_xor_op(cl_object out, cl_object i, cl_object j)
485 {
486 mpz_xor(ecl_bignum(out), ecl_bignum(i), ecl_bignum(j));
487 }
488
489 static void
490 mpz_and_op(cl_object out, cl_object i, cl_object j)
491 {
492 mpz_and(ecl_bignum(out), ecl_bignum(i), ecl_bignum(j));
493 }
494
495 static void
496 mpz_eqv_op(cl_object out, cl_object i, cl_object j)
497 {
498 mpz_xor(ecl_bignum(out), ecl_bignum(i), ecl_bignum(j));
499 mpz_com(ecl_bignum(out), ecl_bignum(out));
500 }
501
502 static void
503 mpz_nand_op(cl_object out, cl_object i, cl_object j)
504 {
505 mpz_and(ecl_bignum(out), ecl_bignum(i), ecl_bignum(j));
506 mpz_com(ecl_bignum(out), ecl_bignum(out));
507 }
508
509 static void
510 mpz_nor_op(cl_object out, cl_object i, cl_object j)
511 {
512 mpz_ior(ecl_bignum(out), ecl_bignum(i), ecl_bignum(j));
513 mpz_com(ecl_bignum(out), ecl_bignum(out));
514 }
515
516 static void
517 mpz_andc1_op(cl_object out, cl_object i, cl_object j)
518 {
519 mpz_com(ecl_bignum(out), ecl_bignum(i));
520 mpz_and(ecl_bignum(out), ecl_bignum(out), ecl_bignum(j));
521 }
522
523 static void
524 mpz_orc1_op(cl_object out, cl_object i, cl_object j)
525 {
526 mpz_com(ecl_bignum(out), ecl_bignum(i));
527 mpz_ior(ecl_bignum(out), ecl_bignum(out), ecl_bignum(j));
528 }
529
530 static void
531 mpz_andc2_op(cl_object out, cl_object i, cl_object j)
532 {
533 /* (i & ~j) = ~((~i) | j) */
534 mpz_orc1_op(out, i, j);
535 mpz_com(ecl_bignum(out), ecl_bignum(out));
536 }
537
538 static void
539 mpz_orc2_op(cl_object out, cl_object i, cl_object j)
540 {
541 /* (i | ~j) = ~((~i) & j) */
542 mpz_andc1_op(out, i, j);
543 mpz_com(ecl_bignum(out), ecl_bignum(out));
544 }
545
546 static void
547 mpz_b_clr_op(cl_object out, cl_object i, cl_object j)
548 {
549 mpz_set_si(ecl_bignum(out), 0);
550 }
551
552 static void
553 mpz_b_set_op(cl_object o, cl_object i, cl_object j)
554 {
555 mpz_set_si(ecl_bignum(o), -1);
556 }
557
558 static void
559 mpz_b_1_op(cl_object out, cl_object i, cl_object j)
560 {
561 if (i != out)
562 mpz_set(ecl_bignum(out), ecl_bignum(i));
563 }
564
565 static void
566 mpz_b_2_op(cl_object out, cl_object i, cl_object j)
567 {
568 mpz_set(ecl_bignum(out), ecl_bignum(j));
569 }
570
571 static void
572 mpz_b_c1_op(cl_object out, cl_object i, cl_object j)
573 {
574 mpz_com(ecl_bignum(out), ecl_bignum(i));
575 }
576
577 static void
578 mpz_b_c2_op(cl_object out, cl_object i, cl_object j)
579 {
580 mpz_com(ecl_bignum(out), ecl_bignum(j));
581 }
582
583 static _ecl_big_binary_op bignum_operations[16] = {
584 mpz_b_clr_op,
585 mpz_and_op,
586 mpz_andc2_op,
587 mpz_b_1_op,
588 mpz_andc1_op,
589 mpz_b_2_op,
590 mpz_xor_op,
591 mpz_ior_op,
592 mpz_nor_op,
593 mpz_eqv_op,
594 mpz_b_c2_op,
595 mpz_orc2_op,
596 mpz_b_c1_op,
597 mpz_orc1_op,
598 mpz_nand_op,
599 mpz_b_set_op};
600
601 _ecl_big_binary_op
602 _ecl_big_boole_operator(int op)
603 {
604 unlikely_if((op < 0) || (op >= 16)) {
605 ecl_internal_error("_ecl_big_boole_operator passed an invalid operator");
606 }
607 return bignum_operations[op];
608 }
609
610 void
611 ecl_init_bignum_registers(cl_env_ptr env)
612 {
613 int i;
614 for (i = 0; i < ECL_BIGNUM_REGISTER_NUMBER; i++) {
615 cl_object x = ecl_alloc_object(t_bignum);
616 _ecl_big_init2(x, ECL_BIG_REGISTER_SIZE);
617 env->big_register[i] = x;
618 }
619 }
620
621 void
622 ecl_clear_bignum_registers(cl_env_ptr env)
623 {
624 int i;
625 for (i = 0; i < ECL_BIGNUM_REGISTER_NUMBER; i++) {
626 _ecl_big_clear(env->big_register[i]);
627 }
628 }
629
630 void
631 init_big()
632 {
633 if (ecl_option_values[ECL_OPT_SET_GMP_MEMORY_FUNCTIONS])
634 mp_set_memory_functions(mp_alloc, mp_realloc, mp_free);
635 }