forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zend_bigint_libtommath.c
974 lines (820 loc) · 25.3 KB
/
zend_bigint_libtommath.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/*
+----------------------------------------------------------------------+
| Zend Big Integer Support - LibTomMath backend |
+----------------------------------------------------------------------+
| Copyright (c) 2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andrea Faulds <ajf@ajf.me> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
/* This file is one of the two implementations of zend_bigint.h
* See zend_bigint.c
*/
#include "zend.h"
#ifdef ZEND_HAVE_LIBTOMMATH
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include <stdlib.h>
#include "zend_types.h"
#include "zend_bigint.h"
#include "zend_string.h"
/* Here we define custom allocators to be used by LibTomMath
* emalloc/realloc/free are macros, not functions, so we need wrappers to pass
* them as our custom allocators
*/
static void* zend_bigint_custom_malloc(size_t size)
{
return emalloc(size);
}
#define XMALLOC zend_bigint_custom_malloc
static void* zend_bigint_custom_realloc(void *ptr, size_t size)
{
return erealloc(ptr, size);
}
#define XREALLOC zend_bigint_custom_realloc
static void* zend_bigint_custom_calloc(size_t num, size_t size)
{
return ecalloc(num, size);
}
#define XCALLOC zend_bigint_custom_calloc
static void zend_bigint_custom_free(void *ptr)
{
efree(ptr);
}
#define XFREE zend_bigint_custom_free
#include "tommath.h"
/* We'll build libtommath as part of this file */
#include "libtommath/mpi.c"
struct _zend_bigint {
zend_refcounted gc;
mp_int mp;
};
/*** INTERNAL MACROS ***/
#define int_abs(n) ((n) >= 0 ? (n) : -(n))
#define int_sgn(n) ((n) > 0 ? 1 : ((n) < 0 ? -1 : 0))
#define double_sgn(n) ((n) > 0 ? 1 : ((n) < 0 ? -1 : 0))
#define mp_sgn(mp) (USED(mp) ? ((SIGN(mp) == MP_NEG) ? -1 : 1) : 0)
/* used for checking if a number fits within a 32-bit unsigned int */
#define BITMASK_32 0xFFFFFFFFL
/* used for checking if a number fits within a LibTomMath "digit" */
#define BITMASK_DIGIT (((zend_ulong)1 << DIGIT_BIT) - 1)
#define FITS_DIGIT(long) ((zend_ulong)(int_abs(long) & BITMASK_DIGIT) == (zend_ulong)int_abs(long))
#define CHECK_ERROR(expr) do { \
int error_code; \
if ((error_code = (expr)) != MP_OKAY) { \
zend_error(E_ERROR, mp_error_to_string(error_code)); \
} \
} while (0)
/* Convenience macro to create a temporary mpz_t from a zend_long
* Used when LibTomMath has no function for an operation taking a long */
#define WITH_TEMP_MP_FROM_ZEND_LONG(long, temp, codeblock) { \
mp_int temp; \
CHECK_ERROR(mp_init(&temp)); \
zend_bigint_long_to_mp_int(long, &temp); \
codeblock \
mp_clear(&temp); \
}
/*** INTERNAL FUNCTIONS ***/
static void zend_bigint_ulong_to_mp_int(zend_ulong value, mp_int *mp)
{
/* mp_set_int only uses 32 bits of an unsigned long
so if zend_long is too big, we need to do multiple ops */
#if SIZEOF_ZEND_LONG > 4
if (value != (value & BITMASK_32)) {
# if defined(mp_import)
/* FIXME: This code is currently untested! */
mp_import(mp, 1, 1, sizeof(value), 0, 0, &value);
return;
# elif SIZEOF_ZEND_LONG == 8
mp_int tmp;
mp_init_set_int(&tmp, value & BITMASK_32);
mp_set_int(mp, (value >> 32) & BITMASK_32);
mp_mul_2d(mp, 32, mp);
mp_or(mp, &tmp, mp);
mp_clear(&tmp);
return;
# else
# error SIZEOF_ZEND_LONG other than 4 or 8 unsupported
# endif
}
#endif
mp_set_int(mp, value);
}
static void zend_bigint_long_to_mp_int(zend_long value, mp_int *mp)
{
zend_ulong ulong_value = (value >= 0) ? value : -value;
zend_bigint_ulong_to_mp_int(ulong_value, mp);
if (value < 0) {
mp_neg(mp, mp);
}
}
static zend_ulong zend_bigint_mp_int_to_ulong(mp_int *mp)
{
# if SIZEOF_ZEND_LONG == 4
/* mp_get_int always returns 32 bits */
return mp_get_int(mp);
# elif SIZEOF_ZEND_LONG == 8
mp_int tmp;
zend_ulong value = 0;
CHECK_ERROR(mp_init_copy(&tmp, mp));
value = mp_get_int(&tmp);
mp_div_2d(&tmp, 32, &tmp, NULL);
value |= mp_get_int(&tmp) << 32;
mp_clear(&tmp);
return value;
# else
# error SIZEOF_ZEND_LONG other than 4 or 8 unsupported
# endif
}
static zend_long zend_bigint_mp_int_to_long(mp_int *mp)
{
return zend_bigint_mp_int_to_ulong(mp) * ((SIGN(mp) == MP_NEG) ? -1 : 1);
}
/* All documentation for bigint functions is found in zend_bigint.h */
mp_int ZEND_LONG_MAX_mp, ZEND_LONG_MIN_mp;
mp_int ZEND_ULONG_MAX_mp;
mp_int two_pow_bits_mp;
void zend_startup_bigint(void)
{
CHECK_ERROR(mp_init_multi(
&ZEND_LONG_MAX_mp, &ZEND_LONG_MIN_mp,
&ZEND_ULONG_MAX_mp,
&two_pow_bits_mp,
NULL
));
zend_bigint_long_to_mp_int(ZEND_LONG_MAX, &ZEND_LONG_MAX_mp);
zend_bigint_long_to_mp_int(ZEND_LONG_MIN, &ZEND_LONG_MAX_mp);
zend_bigint_ulong_to_mp_int(ZEND_ULONG_MAX, &ZEND_ULONG_MAX_mp);
/* either 2^32 or 2^64 */
mp_set_int(&two_pow_bits_mp, 1);
mp_mul_2d(&two_pow_bits_mp, SIZEOF_ZEND_LONG * 8, &two_pow_bits_mp);
}
/*** INITIALISERS ***/
ZEND_API zend_bigint* zend_bigint_init(void) /* {{{ */
{
zend_bigint *return_value = emalloc(sizeof(zend_bigint));
GC_REFCOUNT(return_value) = 1;
GC_TYPE_INFO(return_value) = IS_BIGINT;
CHECK_ERROR(mp_init(&return_value->mp));
return return_value;
}
/* }}} */
ZEND_API zend_bigint* zend_bigint_init_from_string(const char *str, int base) /* {{{ */
{
zend_bigint *big = zend_bigint_init();
if (mp_read_radix(&big->mp, str, base) < 0) {
zend_bigint_release(big);
return NULL;
}
return big;
}
/* }}} */
ZEND_API zend_bigint* zend_bigint_init_from_string_length(const char *str, size_t length, int base) /* {{{ */
{
zend_bigint *big = zend_bigint_init();
char *temp_str = estrndup(str, length);
if (mp_read_radix(&big->mp, temp_str, base) < 0) {
zend_bigint_release(big);
efree(temp_str);
return NULL;
}
efree(temp_str);
return big;
}
/* }}} */
ZEND_API zend_bigint* zend_bigint_init_strtol(const char *str, char** endptr, int base) /* {{{ */
{
zend_bigint *big = zend_bigint_init();
size_t len = 0;
/* Skip leading whitespace */
while (isspace(*str)) {
str++;
}
/* A single sign is valid */
if (str[0] == '+' || str[0] == '-') {
len += 1;
}
/* detect hex prefix */
if (base == 0) {
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
base = 16;
str += 2;
} else {
base = 10;
}
}
if (base == 10) {
while (isdigit(str[len])) {
len++;
}
} else if (base == 16) {
while (isxdigit(str[len])) {
len++;
}
}
if (len) {
char *temp_str = estrndup(str, len);
/* we ignore the return value since if it fails it'll just be zero anyway */
mp_read_radix(&big->mp, temp_str, base);
efree(temp_str);
}
if (endptr) {
*endptr = (char*)(str + len);
}
return big;
}
/* }}} */
ZEND_API zend_bigint* zend_bigint_init_from_long(zend_long value) /* {{{ */
{
zend_bigint *big = zend_bigint_init();
zend_bigint_long_to_mp_int(value, &big->mp);
return big;
}
/* }}} */
ZEND_API zend_bigint* zend_bigint_init_from_double(double value) /* {{{ */
{
zend_bigint *big = zend_bigint_init();
/* prevents crash and ensures zed_dval_to_lval conformity */
if (zend_finite(value) && !zend_isnan(value)) {
/* FIXME: Handle larger doubles */
zend_bigint_long_to_mp_int(zend_dval_to_lval(value), &big->mp);
}
return big;
}
/* }}} */
ZEND_API zend_bigint* zend_bigint_dup(const zend_bigint *source) /* {{{ */
{
zend_bigint *big = zend_bigint_init();
CHECK_ERROR(mp_copy((mp_int *)&source->mp, &big->mp));
return big;
}
/* }}} */
ZEND_API void zend_bigint_release(zend_bigint *big) /* {{{ */
{
if (--GC_REFCOUNT(big) <= 0) {
mp_clear(&big->mp);
efree(big);
}
}
ZEND_API void zend_bigint_free(zend_bigint *big) /* {{{ */
{
mp_clear(&big->mp);
efree(big);
}
/* }}} */
/*** INFORMATION ***/
ZEND_API zend_bool zend_bigint_can_fit_ulong(const zend_bigint *big) /* {{{ */
{
# if SIZEOF_ZEND_LONG == 4
if (mp_count_bits((mp_int*)&big->mp) < 32) {
return 1;
}
# elif SIZEOF_ZEND_LONG == 8
if (mp_count_bits((mp_int*)&big->mp) < 64) {
return 1;
}
# else
# error SIZEOF_ZEND_LONG other than 4 or 8 unsupported
# endif
return (mp_cmp((mp_int*)&big->mp, &ZEND_ULONG_MAX_mp) <= 0 && mp_sgn(&big->mp) >= 0);
}
/* }}} */
ZEND_API zend_bool zend_bigint_can_fit_long(const zend_bigint *big) /* {{{ */
{
# if SIZEOF_ZEND_LONG == 4
if (mp_count_bits((mp_int*)&big->mp) < 31) {
return 1;
}
# elif SIZEOF_ZEND_LONG == 8
if (mp_count_bits((mp_int*)&big->mp) < 63) {
return 1;
}
# else
# error SIZEOF_ZEND_LONG other than 4 or 8 unsupported
# endif
return (mp_cmp((mp_int*)&big->mp, &ZEND_LONG_MAX_mp) <= 0 && mp_cmp((mp_int*)&big->mp, &ZEND_LONG_MIN_mp) >= 0);
}
/* }}} */
ZEND_API int zend_bigint_sign(const zend_bigint *big) /* {{{ */
{
return mp_sgn(&big->mp);
}
/* }}} */
ZEND_API zend_bool zend_bigint_divisible(const zend_bigint *num, const zend_bigint *divisor) /* {{{ */
{
/* TODO: Refactor division so we don't have to do it twice for LibTomMath */
mp_int remainder;
zend_bool result;
CHECK_ERROR(mp_init(&remainder));
CHECK_ERROR(mp_div((mp_int*)&num->mp, (mp_int*)&divisor->mp, NULL, &remainder));
result = USED(&remainder) ? 0 : 1;
mp_clear(&remainder);
return result;
}
/* }}} */
ZEND_API zend_bool zend_bigint_divisible_long(const zend_bigint *num, zend_long divisor) /* {{{ */
{
/* TODO: Refactor division so we don't have to do it twice for LibTomMath */
if (FITS_DIGIT(divisor)) {
mp_digit rem;
CHECK_ERROR(mp_div_d((mp_int*)&num->mp, int_abs(divisor), NULL, &rem));
return rem ? 0 : 1;
} else {
zend_bool result;
mp_int remainder;
CHECK_ERROR(mp_init(&remainder));
WITH_TEMP_MP_FROM_ZEND_LONG(divisor, divisor_mp, {
CHECK_ERROR(mp_div((mp_int*)&num->mp, &divisor_mp, NULL, &remainder));
})
result = USED(&remainder) ? 0 : 1;
mp_clear(&remainder);
return result;
}
}
/* }}} */
ZEND_API zend_bool zend_bigint_long_divisible(zend_long num, const zend_bigint *divisor) /* {{{ */
{
/* TODO: Refactor division so we don't have to do it twice for LibTomMath */
mp_int remainder;
zend_bool result;
CHECK_ERROR(mp_init(&remainder));
WITH_TEMP_MP_FROM_ZEND_LONG(num, num_mp, {
CHECK_ERROR(mp_div(&num_mp, (mp_int*)&divisor->mp, NULL, &remainder));
})
result = USED(&remainder) ? 0 : 1;
mp_clear(&remainder);
return result;
}
/* }}} */
/*** CONVERTORS ***/
ZEND_API zend_long zend_bigint_to_long(const zend_bigint *big) /* {{{ */
{
mp_int bmod;
zend_long result;
mp_init(&bmod);
mp_mod_2d((mp_int*)&big->mp, SIZEOF_ZEND_LONG * 8, &bmod);
if (SIGN(&bmod) == MP_NEG) {
/* we're going to make this number positive */
mp_add(&bmod, &two_pow_bits_mp, &bmod);
}
/* ulong -> long conversion is deliberate */
result = (zend_long)zend_bigint_mp_int_to_ulong(&bmod);
mp_clear(&bmod);
return result;
}
ZEND_API zend_long zend_bigint_to_long_saturate(const zend_bigint *big) /* {{{ */
{
if (zend_bigint_can_fit_long(big)) {
return zend_bigint_mp_int_to_long((mp_int*)&big->mp);
} else {
if (SIGN(&big->mp) == MP_NEG) {
return ZEND_LONG_MIN;
} else {
return ZEND_LONG_MAX;
}
}
}
/* }}} */
ZEND_API zend_long zend_bigint_to_long_ex(const zend_bigint *big, zend_bool *overflow) /* {{{ */
{
*overflow = !zend_bigint_can_fit_long(big);
return zend_bigint_mp_int_to_long((mp_int*)&big->mp);
}
/* }}} */
ZEND_API zend_ulong zend_bigint_to_ulong(const zend_bigint *big) /* {{{ */
{
/* FIXME: reimplement saturation algo and handle larger nos */
return mp_get_int((mp_int*)&big->mp);
}
ZEND_API zend_bool zend_bigint_to_bool(const zend_bigint *big) /* {{{ */
{
return USED(&big->mp) ? 1 : 0;
}
/* }}} */
ZEND_API double zend_bigint_to_double(const zend_bigint *big) /* {{{ */
{
/* from http://github.com/libtom/libtommath/issues/3#issuecomment-5668076
having it depend on library internals is bad, but sadly not yet a function
FIXME: TODO: Replace with built-in LibTomMath function once available */
const int PRECISION = 53;
const int NEED_DIGITS = (PRECISION + 2 * DIGIT_BIT - 2) / DIGIT_BIT;
const double DIGIT_MULTI = (mp_digit)1 << DIGIT_BIT;
mp_int *a = (mp_int*)&big->mp;
int i, limit;
double d = 0.0;
/* this is technically a violation of the parameter being a const
* however, it shouldn't change the value, and I'd rather not duplicate
*/
mp_clamp(a);
i = USED(a);
limit = i <= NEED_DIGITS ? 0 : i - NEED_DIGITS;
while (i-- > limit) {
d += DIGIT(a, i);
d *= DIGIT_MULTI;
}
if (SIGN(a) == MP_NEG) {
d *= -1.0;
}
d *= pow(2.0, i * DIGIT_BIT);
return d;
}
/* }}} */
ZEND_API char* zend_bigint_to_string(const zend_bigint *big) /* {{{ */
{
int size;
char *str;
CHECK_ERROR(mp_radix_size((mp_int*)&big->mp, 10, &size));
str = emalloc(size);
CHECK_ERROR(mp_toradix((mp_int*)&big->mp, str, 10));
return str;
}
/* }}} */
ZEND_API zend_string* zend_bigint_to_zend_string(const zend_bigint *big, int persistent) /* {{{ */
{
int size;
zend_string *str;
CHECK_ERROR(mp_radix_size((mp_int*)&big->mp, 10, &size));
str = zend_string_alloc(size - 1, persistent);
CHECK_ERROR(mp_toradix((mp_int*)&big->mp, str->val, 10));
return str;
}
/* }}} */
ZEND_API char* zend_bigint_to_string_base(const zend_bigint *big, int base) /* {{{ */
{
int size;
char *str;
CHECK_ERROR(mp_radix_size((mp_int*)&big->mp, base, &size));
str = emalloc(size);
CHECK_ERROR(mp_toradix((mp_int*)&big->mp, str, base));
return str;
}
/* }}} */
/*** OPERATIONS **/
ZEND_API void zend_bigint_add(zend_bigint *out, const zend_bigint *op1, const zend_bigint *op2) /* {{{ */
{
CHECK_ERROR(mp_add((mp_int*)&op1->mp, (mp_int*)&op2->mp, &out->mp));
}
/* }}} */
ZEND_API void zend_bigint_add_long(zend_bigint *out, const zend_bigint *op1, zend_long op2) /* {{{ */
{
if (FITS_DIGIT(op2)) {
if (int_sgn(op2) >= 1) {
CHECK_ERROR(mp_add_d((mp_int*)&op1->mp, op2, &out->mp));
} else {
CHECK_ERROR(mp_sub_d((mp_int*)&op1->mp, -op2, &out->mp));
}
} else {
WITH_TEMP_MP_FROM_ZEND_LONG(op2, op2_mp, {
CHECK_ERROR(mp_add((mp_int*)&op1->mp, &op2_mp, &out->mp));
})
}
}
/* }}} */
ZEND_API void zend_bigint_long_add_long(zend_bigint *out, zend_long op1, zend_long op2) /* {{{ */
{
WITH_TEMP_MP_FROM_ZEND_LONG(op1, op1_mp, WITH_TEMP_MP_FROM_ZEND_LONG(op2, op2_mp, {
CHECK_ERROR(mp_add(&op1_mp, &op2_mp, &out->mp));
}))
}
/* }}} */
ZEND_API void zend_bigint_subtract(zend_bigint *out, const zend_bigint *op1, const zend_bigint *op2) /* {{{ */
{
CHECK_ERROR(mp_sub((mp_int*)&op1->mp, (mp_int*)&op2->mp, &out->mp));
}
/* }}} */
ZEND_API void zend_bigint_subtract_long(zend_bigint *out, const zend_bigint *op1, zend_long op2) /* {{{ */
{
if (FITS_DIGIT(op2)) {
if (int_sgn(op2) >= 1) {
CHECK_ERROR(mp_sub_d((mp_int*)&op1->mp, op2, &out->mp));
} else {
CHECK_ERROR(mp_add_d((mp_int*)&op1->mp, -op2, &out->mp));
}
} else {
WITH_TEMP_MP_FROM_ZEND_LONG(op2, op2_mp, {
CHECK_ERROR(mp_sub((mp_int*)&op1->mp, &op2_mp, &out->mp));
})
}
}
/* }}} */
ZEND_API void zend_bigint_long_subtract_long(zend_bigint *out, zend_long op1, zend_long op2) /* {{{ */
{
/* TODO: Use mp_sub_d where possible, to allocate only one temp mp_int */
WITH_TEMP_MP_FROM_ZEND_LONG(op1, op1_mp, WITH_TEMP_MP_FROM_ZEND_LONG(op2, op2_mp, {
CHECK_ERROR(mp_sub(&op1_mp, &op2_mp, &out->mp));
}))
}
/* }}} */
ZEND_API void zend_bigint_long_subtract(zend_bigint *out, zend_long op1, const zend_bigint *op2) /* {{{ */
{
/* TODO: Rather than having _long_subtract detect negation, maybe make a
* _negate function?
*/
if (op1 == 0) {
CHECK_ERROR(mp_neg((mp_int*)&op2->mp, &out->mp));
} else {
/* b - a is just -(a - b) */
zend_bigint_subtract_long(out, op2, op1);
CHECK_ERROR(mp_neg(&out->mp, &out->mp));
}
}
/* }}} */
ZEND_API void zend_bigint_multiply(zend_bigint *out, const zend_bigint *op1, const zend_bigint *op2) /* {{{ */
{
CHECK_ERROR(mp_mul((mp_int*)&op1->mp, (mp_int*)&op2->mp, &out->mp));
}
/* }}} */
ZEND_API void zend_bigint_multiply_long(zend_bigint *out, const zend_bigint *op1, zend_long op2) /* {{{ */
{
if (FITS_DIGIT(op2)) {
CHECK_ERROR(mp_mul_d((mp_int*)&op1->mp, int_abs(op2), &out->mp));
if (op2 < 0) {
/* (a * -b) = -ab = -(a * b)
* (-a * -b) = ab = -(-a * b)
*/
CHECK_ERROR(mp_neg(&out->mp, &out->mp));
}
} else {
WITH_TEMP_MP_FROM_ZEND_LONG(op2, op2_mp, {
CHECK_ERROR(mp_mul((mp_int*)&op1->mp, &op2_mp, &out->mp));
})
}
}
/* }}} */
ZEND_API void zend_bigint_long_multiply_long(zend_bigint *out, zend_long op1, zend_long op2) /* {{{ */
{
WITH_TEMP_MP_FROM_ZEND_LONG(op1, op1_mp, WITH_TEMP_MP_FROM_ZEND_LONG(op2, op2_mp, {
CHECK_ERROR(mp_mul(&op1_mp, &op2_mp, &out->mp));
}))
}
/* }}} */
ZEND_API void zend_bigint_pow_ulong(zend_bigint *out, const zend_bigint *base, zend_ulong power) /* {{{ */
{
if (!FITS_DIGIT(power)) {
zend_error_noreturn(E_ERROR, "Exponent too large");
}
CHECK_ERROR(mp_expt_d((mp_int*)&base->mp, power, &out->mp));
}
/* }}} */
ZEND_API void zend_bigint_long_pow_ulong(zend_bigint *out, zend_long base, zend_ulong power) /* {{{ */
{
if (!FITS_DIGIT(power)) {
zend_error_noreturn(E_ERROR, "Exponent too large");
}
WITH_TEMP_MP_FROM_ZEND_LONG(base, base_mp, {
CHECK_ERROR(mp_expt_d(&base_mp, power, &out->mp));
})
}
/* }}} */
ZEND_API void zend_bigint_divide(zend_bigint *out, const zend_bigint *num, const zend_bigint *divisor) /* {{{ */
{
CHECK_ERROR(mp_div((mp_int*)&num->mp, (mp_int*)&divisor->mp, &out->mp, NULL));
}
/* }}} */
ZEND_API double zend_bigint_divide_as_double(const zend_bigint *num, const zend_bigint *divisor) /* {{{ */
{
/* FIXME: Higher precision? */
return zend_bigint_to_double(num) / zend_bigint_to_double(divisor);
}
/* }}} */
ZEND_API void zend_bigint_divide_long(zend_bigint *out, const zend_bigint *num, zend_long divisor) /* {{{ */
{
if (FITS_DIGIT(divisor)) {
CHECK_ERROR(mp_div_d((mp_int*)&num->mp, int_abs(divisor), &out->mp, NULL));
if (divisor < 0) {
CHECK_ERROR(mp_neg(&out->mp, &out->mp));
}
} else {
WITH_TEMP_MP_FROM_ZEND_LONG(divisor, divisor_mp, {
CHECK_ERROR(mp_div((mp_int*)&num->mp, &divisor_mp, &out->mp, NULL));
})
}
}
/* }}} */
ZEND_API double zend_bigint_divide_long_as_double(const zend_bigint *num, zend_long divisor) /* {{{ */
{
/* FIXME: Higher precision? */
return zend_bigint_to_double(num) / (double)divisor;
}
/* }}} */
ZEND_API void zend_bigint_long_divide(zend_bigint *out, zend_long num, const zend_bigint *divisor) /* {{{ */
{
WITH_TEMP_MP_FROM_ZEND_LONG(num, num_mp, {
CHECK_ERROR(mp_div(&num_mp, (mp_int*)&divisor->mp, &out->mp, NULL));
})
}
/* }}} */
ZEND_API double zend_bigint_long_divide_as_double(zend_long num, const zend_bigint *divisor) /* {{{ */
{
/* FIXME: Higher precision? */
return (double)num / zend_bigint_to_double(divisor);
}
/* }}} */
ZEND_API void zend_bigint_modulus(zend_bigint *out, const zend_bigint *num, const zend_bigint *divisor) /* {{{ */
{
CHECK_ERROR(mp_div((mp_int*)&num->mp, (mp_int*)&divisor->mp, NULL, &out->mp));
}
/* }}} */
ZEND_API void zend_bigint_modulus_long(zend_bigint *out, const zend_bigint *num, zend_long divisor) /* {{{ */
{
WITH_TEMP_MP_FROM_ZEND_LONG(divisor, divisor_mp, {
CHECK_ERROR(mp_div((mp_int*)&num->mp, &divisor_mp, NULL, &out->mp));
})
}
/* }}} */
ZEND_API void zend_bigint_long_modulus(zend_bigint *out, zend_long num, const zend_bigint *divisor) /* {{{ */
{
WITH_TEMP_MP_FROM_ZEND_LONG(num, num_mp, {
CHECK_ERROR(mp_div(&num_mp, (mp_int*)&divisor->mp, NULL, &out->mp));
})
}
/* }}} */
ZEND_API void zend_bigint_ones_complement(zend_bigint *out, const zend_bigint *op) /* {{{ */
{
/* A one's complement, aka bitwise NOT, is actually just -a - 1 */
CHECK_ERROR(mp_neg((mp_int*)&op->mp, &out->mp));
CHECK_ERROR(mp_sub_d(&out->mp, 1, &out->mp));
}
/* }}} */
static void zend_always_inline _zend_bigint_twos_complement(mp_int *out, mp_int *in, int size) /* {{{ */
{
mp_int tmp;
/* two's complement of a number is 2^width - n
* so first we need 2^width
*/
CHECK_ERROR(mp_init(&tmp));
CHECK_ERROR(mp_set_int(&tmp, 1));
CHECK_ERROR(mp_mul_2d(&tmp, size + 1, &tmp));
/* then, we subtract (or add if our number is already negative) */
if (SIGN(in) == MP_NEG) {
CHECK_ERROR(mp_add(&tmp, in, out));
} else {
CHECK_ERROR(mp_sub(&tmp, in, out));
}
mp_clear(&tmp);
}
/* }}} */
static void zend_always_inline _zend_bigint_bitwise_op(mp_int *out, mp_int *real_op1, mp_int *real_op2, char op) /* {{{ */
{
zend_bool op1_sign, op2_sign, sign;
mp_int op1_copy, op2_copy;
int largest_bit_width;
op1_sign = (SIGN(real_op1) == MP_NEG);
op2_sign = (SIGN(real_op2) == MP_NEG);
/* to emulate two's-complement OR, get twos' complement of negatives */
largest_bit_width = MAX(mp_count_bits(real_op1), mp_count_bits(real_op2));
if (op1_sign) {
CHECK_ERROR(mp_init(&op1_copy));
_zend_bigint_twos_complement(&op1_copy, real_op1, largest_bit_width);
real_op1 = &op1_copy;
}
if (op2_sign) {
CHECK_ERROR(mp_init(&op2_copy));
_zend_bigint_twos_complement(&op2_copy, real_op2, largest_bit_width);
real_op2 = &op2_copy;
}
switch (op) {
case '|':
CHECK_ERROR(mp_or(real_op1, real_op2, out));
sign = op1_sign | op2_sign;
break;
case '^':
CHECK_ERROR(mp_xor(real_op1, real_op2, out));
sign = op1_sign ^ op2_sign;
break;
case '&':
CHECK_ERROR(mp_and(real_op1, real_op2, out));
sign = op1_sign & op2_sign;
break;
}
/* since the sign is negative, we need to undo twos' complement */
if (sign) {
_zend_bigint_twos_complement(out, out, largest_bit_width);
mp_neg(out, out);
}
if (op1_sign) {
mp_clear(&op1_copy);
}
if (op2_sign) {
mp_clear(&op2_copy);
}
}
/* }}} */
ZEND_API void zend_bigint_or(zend_bigint *out, const zend_bigint *op1, const zend_bigint *op2) /* {{{ */
{
_zend_bigint_bitwise_op(&out->mp, (mp_int*)&op1->mp, (mp_int*)&op2->mp, '|');
}
/* }}} */
ZEND_API void zend_bigint_or_long(zend_bigint *out, const zend_bigint *op1, zend_long op2) /* {{{ */
{
WITH_TEMP_MP_FROM_ZEND_LONG(op2, op2_mp, {
_zend_bigint_bitwise_op(&out->mp, (mp_int*)&op1->mp, &op2_mp, '|');
})
}
/* }}} */
ZEND_API void zend_bigint_and(zend_bigint *out, const zend_bigint *op1, const zend_bigint *op2) /* {{{ */
{
_zend_bigint_bitwise_op(&out->mp, (mp_int*)&op1->mp, (mp_int*)&op2->mp, '&');
}
/* }}} */
ZEND_API void zend_bigint_and_long(zend_bigint *out, const zend_bigint *op1, zend_long op2) /* {{{ */
{
WITH_TEMP_MP_FROM_ZEND_LONG(op2, op2_mp, {
_zend_bigint_bitwise_op(&out->mp, (mp_int*)&op1->mp, &op2_mp, '&');
})
}
/* }}} */
ZEND_API void zend_bigint_xor(zend_bigint *out, const zend_bigint *op1, const zend_bigint *op2) /* {{{ */
{
_zend_bigint_bitwise_op(&out->mp, (mp_int*)&op1->mp, (mp_int*)&op2->mp, '^');
}
/* }}} */
ZEND_API void zend_bigint_xor_long(zend_bigint *out, const zend_bigint *op1, zend_long op2) /* {{{ */
{
WITH_TEMP_MP_FROM_ZEND_LONG(op2, op2_mp, {
_zend_bigint_bitwise_op(&out->mp, (mp_int*)&op1->mp, &op2_mp, '^');
})
}
/* }}} */
ZEND_API void zend_bigint_shift_left_ulong(zend_bigint *out, const zend_bigint *num, zend_ulong shift) /* {{{ */
{
if (shift > INT_MAX) {
zend_error_noreturn(E_ERROR, "Exponent too large");
}
CHECK_ERROR(mp_mul_2d((mp_int*)&num->mp, shift, &out->mp));
}
/* }}} */
ZEND_API void zend_bigint_long_shift_left_ulong(zend_bigint *out, zend_long num, zend_ulong shift) /* {{{ */
{
if (shift > INT_MAX) {
zend_error_noreturn(E_ERROR, "Exponent too large");
}
WITH_TEMP_MP_FROM_ZEND_LONG(num, num_mp, {
CHECK_ERROR(mp_mul_2d(&num_mp, shift, &out->mp));
})
}
/* }}} */
ZEND_API void zend_bigint_shift_right_ulong(zend_bigint *out, const zend_bigint *num, zend_ulong shift) /* {{{ */
{
if (shift > INT_MAX) {
zend_error_noreturn(E_ERROR, "Exponent too large");
}
/* LibTomMath's mp_div_2d does a logical right shift
* in order to correctly mimick a two's-complement arithmetic right-shift,
* we just need to do a one's complement first */
if (SIGN(&num->mp) == MP_NEG) {
zend_bigint_ones_complement(out, num);
CHECK_ERROR(mp_div_2d(&out->mp, shift, &out->mp, NULL));
zend_bigint_ones_complement(out, out);
/* No mimickry needed for positive op1 :) */
} else {
CHECK_ERROR(mp_div_2d((mp_int*)&num->mp, shift, &out->mp, NULL));
}
}
/* }}} */
ZEND_API int zend_bigint_cmp(const zend_bigint *op1, const zend_bigint *op2) /* {{{ */
{
return mp_cmp((mp_int*)&op1->mp, (mp_int*)&op2->mp);
}
/* }}} */
ZEND_API int zend_bigint_cmp_long(const zend_bigint *op1, zend_long op2) /* {{{ */
{
int provisional_sign =mp_sgn(&op1->mp) - int_sgn(op2);
if (provisional_sign != 0) {
return provisional_sign;
} else if (FITS_DIGIT(op2) && op2 > 0) {
return mp_cmp_d((mp_int*)&op1->mp, op2);
} else {
WITH_TEMP_MP_FROM_ZEND_LONG(op2, op2_mp, {
provisional_sign = mp_cmp((mp_int*)&op1->mp, &op2_mp);
})
return provisional_sign;
}
}
/* }}} */
ZEND_API int zend_bigint_cmp_double(const zend_bigint *op1, double op2) /* {{{ */
{
int provisional_sign = mp_sgn(&op1->mp) - double_sgn(op2);
if (provisional_sign != 0) {
return provisional_sign;
} else {
/* FIXME: Handle larger doubles */
return zend_bigint_cmp_long(op1, zend_dval_to_lval(op2));
}
}
/* }}} */
ZEND_API void zend_bigint_abs(zend_bigint *out, const zend_bigint *big) /* {{{ */
{
CHECK_ERROR(mp_abs((mp_int*)&big->mp, &out->mp));
}
#endif /* ZEND_HAVE_LIBTOMMATH */