-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathulbn.c
8900 lines (8068 loc) · 286 KB
/
ulbn.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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ulbn - Big Number Library
# Requirements
- C89/C++98
- `CHAR_BIT` or `sizeof(ulbn_limb_t)` should be even
# License
The MIT License (MIT)
Copyright (C) 2024-2025 Jin Cai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef ULBN_SOURCE
#define ULBN_SOURCE
#ifndef ULBN_HEADER
#include "ulbn.h"
#endif
#include <assert.h> /* assert */
#include <limits.h> /* CHAR_BIT, UCHAR_MAX, UINT_MAX, INT_MAX */
#include <stdio.h> /* FILE*, fwrite, (printf, fprintf, fputc, fputs) */
#include <stdlib.h> /* free, realloc */
#include <string.h> /* memset, memcpy, memmove, memcmp */
#if defined(ULBN_CONF_USE_RAND)
#include <time.h> /* clock, time */
#endif
#if defined(ULBN_CONF_HAS_FLOAT) || defined(ULBN_CONF_HAS_DOUBLE) || defined(ULBN_CONF_HAS_LONG_DOUBLE)
#include <float.h> /* FLT_MAX_EXP, FLT_MANT_DIG, DBL_MAX_EXP, DBL_MANT_DIG, LDBL_MAX_EXP, LDBL_MANT_DIG */
#include <math.h> /* (HUGE_VALF & floorf), floor, floorl */
#endif
/***********
* Utility *
***********/
#ifndef ulbn_assert
#define ulbn_assert(expr) assert(expr)
#endif /* ulbn_assert */
#ifndef ulbn_condexpr
#define ulbn_condexpr(cond, expr) (ulbn_assert(cond), (expr))
#endif /* ulbn_condexpr */
#ifndef ul_assume
#ifndef UL_PEDANTIC
#if defined(_MSC_VER)
#define ul_assume(cond) __assume(cond)
#endif
#if !defined(ul_assume) && ul_has_builtin(__builtin_assume)
#define ul_assume(cond) __builtin_assume(cond)
#endif
#if !defined(ul_assume) && defined(__cplusplus) && defined(__has_cpp_attribute)
#if __has_cpp_attribute(cond)
#define ul_assume(cond) [[assume(cond)]]
#endif
#endif
#if !defined(ul_assume) && defined(__has_attribute) && !defined(UL_PEDANTIC)
#if __has_attribute(assume)
#define ul_assume(cond) __attribute__((assume(cond)))
#endif
#endif
#if !defined(ul_assume) && ul_has_builtin(__builtin_unreachable)
#if ul_has_builtin(__builtin_expect)
#define ul_assume(cond) (__builtin_expect(!(cond), 0) ? __builtin_unreachable() : (void)0)
#else
#define ul_assume(cond) (!(cond) ? __builtin_unreachable() : (void)0)
#endif
#endif
#endif
#ifndef ul_assume
#define ul_assume(cond) ((void)0)
#endif
#endif /* ul_assume */
#if !defined(UL_ENDIAN_BIG) && !defined(UL_ENDIAN_LITTLE)
#if defined(BIG_ENDIAN) && defined(LITTLE_ENDIAN)
#if defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN
#define UL_ENDIAN_BIG
#elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
#define UL_ENDIAN_LITTLE
#endif
#elif defined(BIG_ENDIAN)
#define UL_ENDIAN_BIG
#elif defined(LITTLE_ENDIAN)
#define UL_ENDIAN_LITTLE
#endif
#if defined(_BIG_ENDIAN) && defined(_LITTLE_ENDIAN)
#if defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN
#define UL_ENDIAN_BIG
#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN
#define UL_ENDIAN_LITTLE
#endif
#elif defined(_BIG_ENDIAN)
#define UL_ENDIAN_BIG
#elif defined(_LITTLE_ENDIAN)
#define UL_ENDIAN_LITTLE
#endif
#if defined(__BIG_ENDIAN) && defined(__LITTLE_ENDIAN)
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
#define UL_ENDIAN_BIG
#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
#define UL_ENDIAN_LITTLE
#endif
#elif defined(__BIG_ENDIAN)
#define UL_ENDIAN_BIG
#elif defined(__LITTLE_ENDIAN)
#define UL_ENDIAN_LITTLE
#endif
#if defined(__BIG_ENDIAN__) && defined(__LITTLE_ENDIAN__)
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __BIG_ENDIAN__
#define UL_ENDIAN_BIG
#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __LITTLE_ENDIAN__
#define UL_ENDIAN_LITTLE
#endif
#elif defined(__BIG_ENDIAN__)
#define UL_ENDIAN_BIG
#elif defined(__LITTLE_ENDIAN__)
#define UL_ENDIAN_LITTLE
#endif
#if !defined(UL_ENDIAN_BIG) && !defined(UL_ENDIAN_LITTLE)
#if defined(__alpha__) || defined(__alpha) || defined(i386) || defined(__i386__) || defined(_M_I86) \
|| defined(_M_IX86) || defined(__OS2__) || defined(sun386) || defined(__TURBOC__) || defined(vax) \
|| defined(vms) || defined(VMS) || defined(__VMS) || defined(_M_X64)
#define UL_ENDIAN_LITTLE
#elif defined(AMIGA) || defined(applec) || defined(__AS400__) || defined(_CRAY) || defined(__hppa) \
|| defined(__hp9000) || defined(ibm370) || defined(mc68000) || defined(m68k) || defined(__MRC__) \
|| defined(__MVS__) || defined(__MWERKS__) || defined(sparc) || defined(__sparc) || defined(SYMANTEC_C) \
|| defined(__VOS__) || defined(__TIGCC__) || defined(__TANDEM) || defined(THINK_C) || defined(__VMCMS__) \
|| defined(_AIX) || defined(__s390__) || defined(__s390x__) || defined(__zarch__)
#define UL_ENDIAN_BIG
#elif defined(__arm__)
#ifdef __BIG_ENDIAN
#define UL_ENDIAN_BIG
#else
#define UL_ENDIAN_LITTLE
#endif
#endif
#endif
#endif /* !UL_ENDIAN_BIG && !UL_ENDIAN_LITTLE */
/* check if `x` is 64-bit integer */
#if defined(ULONG_MAX) && (ULONG_MAX >> 63) == 1
#define _ULBN_IS_64BIT(x) ((x) == 0xFFFFFFFFFFFFFFFFul)
#elif defined(ULLONG_MAX) && (ULLONG_MAX >> 63) == 1
#define _ULBN_IS_64BIT(x) ((x) == 0xFFFFFFFFFFFFFFFFull)
#else
#define _ULBN_IS_64BIT(x) (0)
#endif /* _ULBN_IS_64BIT */
#ifdef __cplusplus
#define ulbn_may_static static
#else
#define ulbn_may_static
#endif
/************************
* Macros about overlap *
************************/
#define ulbn_safe_forward_overlap(d, dn, s, sn) ((d) <= (s) || (d) >= (s) + (sn))
#define ulbn_safe_backward_overlap(d, dn, s, sn) ((d) + (dn) <= (s) || ((d) >= (s) && (d) + (dn) >= (s) + (sn)))
#define ulbn_safe_overlap(d, dn, s, sn) ((d) + (dn) <= (s) || (s) + (sn) <= (d))
#define ulbn_assert_forward_overlap(d, dn, s, sn) ulbn_assert(ulbn_safe_forward_overlap(d, dn, s, sn))
#define ulbn_assert_backward_overlap(d, dn, s, sn) ulbn_assert(ulbn_safe_backward_overlap(d, dn, s, sn))
#define ulbn_assert_overlap(d, dn, s, sn) ulbn_assert(ulbn_safe_overlap(d, dn, s, sn))
#define ulbn_assert_same_or_not_overlap(d, dn, s, sn) \
ulbn_assert((d) == ul_nullptr || (s) == ul_nullptr || (s) == (d) || ulbn_safe_overlap(d, dn, s, sn))
/***********************************
* Definations about `ulbn_limb_t` *
***********************************/
#if (CHAR_BIT & 1) != 0
ul_static_assert((sizeof(ulbn_limb_t) & 1) == 0, "`CHAR_BIT` or `sizeof(ulbn_limb_t)` should be even");
#endif
#define _ULBN_LIMB_HALF_BITS (ULBN_LIMB_BITS >> 1)
#define ULBN_LIMB_SHL(val, shift) ulbn_cast_limb(ulbn_cast_limb(val) << (shift))
#define ULBN_LIMB_MASK(val, shift) ulbn_cast_limb(ULBN_LIMB_SHL((val), (shift)) - 1u)
#define _ULBN_LIMB_SIGNBIT (ULBN_LIMB_SHL(1, ULBN_LIMB_BITS - 1))
#define _ULBN_LOWMASK ULBN_LIMB_MASK(1, _ULBN_LIMB_HALF_BITS)
#define ULBN_LOWPART(x) ulbn_cast_limb((x) & ULBN_LOWMASK)
#define ULBN_HIGHPART(x) ulbn_cast_limb((x) >> ULBN_LIMB_HALF_BITS)
static ul_constexpr const size_t ULBN_LIMB_HALF_BITS = _ULBN_LIMB_HALF_BITS;
static ul_constexpr const ulbn_limb_t ULBN_LIMB_SIGNBIT = _ULBN_LIMB_SIGNBIT;
static ul_constexpr const ulbn_limb_t ULBN_LOWMASK = _ULBN_LOWMASK;
#if ULBN_LIMB_MAX < UCHAR_MAX
#define _ULBN_LIMB_1X_CHAR
#endif
#if ULBN_LIMB_MAX / UCHAR_MAX / UCHAR_MAX <= 1 || ULBN_LIMB_MAX / UCHAR_MAX <= UCHAR_MAX + 2
#define _ULBN_LIMB_2X_CHAR
#endif
#define _ULBN_DIV_4LIMB(x) ((x) / UCHAR_MAX / UCHAR_MAX / UCHAR_MAX / UCHAR_MAX)
#if _ULBN_DIV_4LIMB(ULBN_LIMB_MAX) <= 1 \
|| ULBN_LIMB_MAX / UCHAR_MAX <= UCHAR_MAX * UCHAR_MAX * UCHAR_MAX + 4 * UCHAR_MAX * UCHAR_MAX + 6 * UCHAR_MAX + 4
#define ULBN_LIMB_4X_CHAR
#endif
#define _ULBN_MUL_4LIMB(x) ((x) * UCHAR_MAX * UCHAR_MAX * UCHAR_MAX * UCHAR_MAX)
#if _ULBN_DIV_4LIMB(_ULBN_DIV_4LIMB(ULBN_LIMB_MAX)) <= 1 \
|| ULBN_LIMB_MAX / UCHAR_MAX <= _ULBN_MUL_4LIMB(UCHAR_MAX * UCHAR_MAX * UCHAR_MAX) \
+ 8 * _ULBN_MUL_4LIMB(UCHAR_MAX * UCHAR_MAX) + 28 * _ULBN_MUL_4LIMB(UCHAR_MAX) \
+ 56 * _ULBN_MUL_4LIMB(1) + 70 * UCHAR_MAX * UCHAR_MAX * UCHAR_MAX \
+ 56 * UCHAR_MAX * UCHAR_MAX + 28 * UCHAR_MAX + 8
#define ULBN_LIMB_8X_CHAR
#endif
#undef _ULBN_DIV_4LIMB
#undef _ULBN_MUL_4LIMB
/******************************************************
* Definations about `ulbn_usize_t` and `ulbn_bits_t` *
******************************************************/
#define _ULBN_SIZET_MAX ul_static_cast(size_t, ~ul_static_cast(size_t, 0))
#define _ULBN_ALLOC_LIMIT ul_static_cast(size_t, _ULBN_SIZET_MAX / sizeof(ulbn_limb_t))
/* The limitation of `ulbn_usize_t`, mainly to avoid overflow caused by the multiplication of `ulbn_usize_t` */
#define _ULBN_USIZE_LIMIT ulbn_cast_usize(_ulbn_min_(_ULBN_SIZET_MAX / ULBN_LIMB_BITS, ULBN_USIZE_MAX))
static ul_constexpr const size_t ULBN_ALLOC_LIMIT = _ULBN_ALLOC_LIMIT;
static ul_constexpr const ulbn_usize_t ULBN_USIZE_LIMIT = _ULBN_USIZE_LIMIT;
static ul_constexpr const ulbn_usize_t ULBN_SHORT_LIMB_SIZE = _ULBN_SHORT_LIMB_SIZE;
#ifndef ULBN_CHECK_USIZE_OVERFLOW
/* If `ulbn_usize_t` is able to hold bits of `ulbi_t`, we don't need to check if `ulbn_usize_t` exceeds the limit */
#if defined(SIZE_MAX)
#if defined(_ULBN_LIMB_1X_CHAR) && ULBN_USIZE_MAX <= SIZE_MAX / CHAR_BIT
#define ULBN_CHECK_USIZE_OVERFLOW 0
#elif defined(_ULBN_LIMB_2X_CHAR) && ULBN_USIZE_MAX <= SIZE_MAX / 2 / CHAR_BIT
#define ULBN_CHECK_USIZE_OVERFLOW 0
#elif defined(ULBN_LIMB_4X_CHAR) && ULBN_USIZE_MAX <= SIZE_MAX / 4 / CHAR_BIT
#define ULBN_CHECK_USIZE_OVERFLOW 0
#elif defined(ULBN_LIMB_8X_CHAR) && ULBN_USIZE_MAX <= SIZE_MAX / 8 / CHAR_BIT
#define ULBN_CHECK_USIZE_OVERFLOW 0
#endif
#endif
#ifndef ULBN_CHECK_USIZE_OVERFLOW
#define ULBN_CHECK_USIZE_OVERFLOW 1
#endif
#endif /* ULBN_CHECK_USIZE_OVERFLOW */
#if ULBN_CHECK_USIZE_OVERFLOW
#define ULBN_DO_IF_USIZE_COND(cond, expr) \
if(ul_unlikely(cond)) \
expr((void)0)
#define ULBN_RETURN_IF_USIZE_COND(cond, ret) \
if(ul_unlikely(cond)) \
return (ret)
#else /* !ULBN_CHECK_USIZE_OVERFLOW */
#define ULBN_DO_IF_USIZE_COND(cond, expr) ((void)0)
#define ULBN_RETURN_IF_USIZE_COND(cond, ret) ((void)0)
#endif
#define ULBN_DO_IF_USIZE_OVERFLOW(n, expr) ULBN_DO_IF_USIZE_COND((n) > ULBN_USIZE_LIMIT, expr)
#define ULBN_RETURN_IF_USIZE_OVERFLOW(n, ret) ULBN_RETURN_IF_USIZE_COND((n) > ULBN_USIZE_LIMIT, ret)
#if ULBN_CONF_CHECK_ALLOCATION_FAILURE
#define ULBN_RETURN_IF_ALLOC_COND(cond, ret) \
if(ul_unlikely((cond))) \
return (ret)
#define ULBN_DO_IF_ALLOC_COND(cond, expr) \
if(ul_unlikely((cond))) \
expr((void)0)
#else /* !ULBN_CONF_CHECK_ALLOCATION_FAILURE */
#define ULBN_RETURN_IF_ALLOC_COND(cond, ret) ((void)0)
#define ULBN_DO_IF_ALLOC_COND(cond, expr) ((void)0)
#endif
#define ULBN_RETURN_IF_ALLOC_FAILED(ptr, ret) ULBN_RETURN_IF_ALLOC_COND((ptr) == ul_nullptr, ret)
#define ULBN_DO_IF_ALLOC_FAILED(ptr, expr) ULBN_DO_IF_ALLOC_COND((ptr) == ul_nullptr, expr)
#define ULBN_DO_IF_PUBLIC_COND(cond, expr) \
if(ul_unlikely(cond)) \
expr((void)0)
#ifdef __cplusplus
extern "C" {
#endif
/******************
* <ulbn> Startup *
******************/
ULBN_PRIVATE void _ulbn_startup_fft(void);
ULBN_PRIVATE void _ulbn_startup_setstr(void);
ULBN_PRIVATE void _ulbn_startup_baseconv(void);
ULBN_PRIVATE void _ulbn_startup_shortdiv(void);
static int _ulbn_startup_state = 0;
ULBN_PUBLIC void ulbn_startup(void) {
if(_ulbn_startup_state != 0)
return;
_ulbn_startup_fft();
_ulbn_startup_setstr();
_ulbn_startup_baseconv();
_ulbn_startup_shortdiv();
_ulbn_startup_state = 1;
}
#if !defined(ulbn_check_startup) && defined(__has_attribute) && !defined(UL_PEDANTIC)
#if __has_attribute(constructor)
ULBN_PRIVATE __attribute__((constructor)) void ulbn_auto_startup(void) {
ulbn_startup();
}
#define ulbn_check_startup() ((void)0)
#endif
#endif
/*
#if !defined(ulbn_check_startup) && defined(__cplusplus)
ULBN_PRIVATE void _ulbn_auto_startup(void) {
struct AutoStartup {
AutoStartup() {
ulbn_startup();
}
};
static AutoStartup auto_startup;
}
#define ulbn_check_startup() _ulbn_auto_startup()
#endif
*/
#ifndef ulbn_check_startup
#define ulbn_check_startup() ulbn_assert(_ulbn_startup_state != 0)
#endif
/*********************
* <ulbn> Allocation *
*********************/
#define ulbn_doalloc(alloc, p, on, nn) (alloc)->alloc_func((alloc)->alloc_opaque, (p), (on), (nn))
#define ulbn_doallocT(T, alloc, p, onum, nnum) \
ul_reinterpret_cast( \
T*, ulbn_doalloc((alloc), (p), ul_static_cast(size_t, onum) * sizeof(T), ul_static_cast(size_t, nnum) * sizeof(T)) \
)
#define ulbn_allocT(T, alloc, num) ulbn_doallocT(T, alloc, ul_nullptr, 0, num)
#define ulbn_reallocT(T, alloc, p, onum, nnum) ulbn_doallocT(T, alloc, p, onum, nnum)
#define ulbn_deallocT(T, alloc, p, num) ((void)ulbn_doallocT(T, alloc, p, num, 0))
ULBN_PRIVATE void* _ulbn_default_alloc(void* opaque, void* ptr, size_t on, size_t nn) {
(void)opaque;
(void)on;
if(nn == 0) {
free(ptr);
return ul_nullptr;
}
return realloc(ptr, nn);
}
ULBN_PUBLIC const ulbn_alloc_t* ulbn_default_alloc(void) {
static const ulbn_alloc_t alloc = { &_ulbn_default_alloc, ul_nullptr };
return &alloc;
}
ULBN_PUBLIC void* ulbn_alloc(const ulbn_alloc_t* alloc, size_t sz) {
ulbn_assert(sz != 0);
return ulbn_doalloc(alloc, ul_nullptr, 0, sz);
}
ULBN_PUBLIC void* ulbn_realloc(const ulbn_alloc_t* alloc, void* ptr, size_t on, size_t nn) {
ulbn_assert(ptr == ul_nullptr ? on == 0 : on != 0);
ulbn_assert(nn != 0);
return ulbn_doalloc(alloc, ptr, on, nn);
}
ULBN_PUBLIC void ulbn_dealloc(const ulbn_alloc_t* alloc, void* ptr, size_t sz) {
ulbn_assert(sz != 0);
ulbn_doalloc(alloc, ptr, sz, 0);
}
ULBN_PUBLIC ulbn_usize_t ulbn_usize_limit(void) {
return ULBN_USIZE_LIMIT;
}
/*********************************************
* <ulbn> Operations for short `ulbn_limb_t` *
*********************************************/
ULBN_PRIVATE int _ulbn_clz_(ulbn_limb_t x) {
#if ul_has_builtin(__builtin_clz) && ULBN_LIMB_MAX <= UINT_MAX
return ulbn_condexpr(x != 0, __builtin_clz(x) - ulbn_cast_int(sizeof(int) * CHAR_BIT - ULBN_LIMB_BITS));
#elif ul_has_builtin(__builtin_clz) && ULBN_LIMB_MAX == UINT_MAX
return ulbn_condexpr(x != 0, __builtin_clz(x));
#elif ul_has_builtin(__builtin_clzl) && ULBN_LIMB_MAX == ULONG_MAX
return ulbn_condexpr(x != 0, __builtin_clzl(x));
#elif ul_has_builtin(__builtin_clzll) && ULBN_LIMB_MAX == ULLONG_MAX
return ulbn_condexpr(x != 0, __builtin_clzll(x));
#else
int r = 0;
#if ULBN_LIMB_MAX > 0xFFu
static ul_constexpr const ulbn_limb_t MASK8 = ULBN_LIMB_SHL(0xFF, ULBN_LIMB_BITS - 8);
ulbn_assert(x != 0);
while(!(x & MASK8)) {
r += 8;
x <<= 8;
}
#else
ulbn_assert(x != 0);
#endif
while(!(x & ULBN_LIMB_SIGNBIT)) {
++r;
x <<= 1;
}
return r;
#endif
}
ULBN_PRIVATE int _ulbn_ctz_(ulbn_limb_t x) {
#if ul_has_builtin(__builtin_ctz) && ULBN_LIMB_MAX <= UINT_MAX
return ulbn_condexpr(x != 0, __builtin_ctz(x));
#elif ul_has_builtin(__builtin_ctzl) && ULBN_LIMB_MAX <= ULONG_MAX
return ulbn_condexpr(x != 0, __builtin_ctzl(x));
#elif ul_has_builtin(__builtin_ctzll) && ULBN_LIMB_MAX <= ULLONG_MAX
return ulbn_condexpr(x != 0, __builtin_ctzll(x));
#else
int r = 0;
ulbn_assert(x != 0);
#if ULBN_LIMB_MAX > 0xFFu
while(!(x & 0xFF)) {
r += 8;
x >>= 8;
}
#endif
while(!(x & 1)) {
++r;
x >>= 1;
}
return r;
#endif
}
ULBN_PRIVATE int _ulbn_popcount_(ulbn_limb_t x) {
#if ul_has_builtin(__builtin_popcount) && ULBN_LIMB_MAX <= UINT_MAX
return __builtin_popcount(x);
#elif ul_has_builtin(__builtin_popcountl) && ULBN_LIMB_MAX <= ULONG_MAX
return __builtin_popcountl(x);
#elif ul_has_builtin(__builtin_popcountll) && ULBN_LIMB_MAX <= ULLONG_MAX
return __builtin_popcountll(x);
#else
int r = 0;
while(x) {
r += ulbn_cast_int(x & 1);
x >>= 1;
}
return r;
#endif
}
#if ULBN_ULONG_MAX > ULBN_LIMB_MAX
ULBN_PRIVATE int _ulbn_clz_ulong(ulbn_ulong_t x) {
#if ul_has_builtin(__builtin_clz) && ULBN_ULONG_MAX <= UINT_MAX
return ulbn_condexpr(x != 0, __builtin_clz(x) - ulbn_cast_int((sizeof(int) - sizeof(ulbn_ulong_t)) * CHAR_BIT));
#elif ul_has_builtin(__builtin_clz) && ULBN_ULONG_MAX == UINT_MAX
return ulbn_condexpr(x != 0, __builtin_clz(x));
#elif ul_has_builtin(__builtin_clzl) && ULBN_ULONG_MAX == ULONG_MAX
return ulbn_condexpr(x != 0, __builtin_clzl(x));
#elif ul_has_builtin(__builtin_clzll) && ULBN_ULONG_MAX == ULLONG_MAX
return ulbn_condexpr(x != 0, __builtin_clzll(x));
#else
static ul_constexpr const ulbn_ulong_t MASK = ulbn_cast_ulong(ulbn_cast_ulong(1) << (ULBN_ULONG_BITS - 1));
int r = 0;
#if ULBN_ULONG_MAX > 0xFFu
static ul_constexpr const ulbn_ulong_t MASK8 = ulbn_cast_ulong(ulbn_cast_ulong(0xFF) << (ULBN_ULONG_BITS - 8));
ulbn_assert(x != 0);
while(!(x & MASK8)) {
r += 8;
x <<= 8;
}
#else
ulbn_assert(x != 0);
#endif
while(!(x & MASK)) {
++r;
x <<= 1;
}
return r;
#endif
}
#endif
#define _ulbn_swap_(T, a, b) \
do { \
T const __swap_tmp = (a); \
(a) = (b); \
(b) = __swap_tmp; \
} while(0)
#define _ulbn_neg_(v) ulbn_cast_limb(ulbn_cast_limb(0u) - (v))
#ifdef ulbn_dlimb_t
#define _ulbn_dlimb_(h, l) ulbn_cast_dlimb(ulbn_cast_dlimb((h)) << ULBN_LIMB_BITS | (l))
#define _ulbn_add_(s1, s0, a1, a0, b1, b0) \
do { \
const ulbn_dlimb_t __s = ulbn_cast_dlimb(_ulbn_dlimb_((a1), (a0)) + _ulbn_dlimb_((b1), (b0))); \
(s1) = ulbn_cast_limb((__s) >> ULBN_LIMB_BITS); \
(s0) = ulbn_cast_limb((__s)); \
} while(0)
#define _ulbn_sub_(d1, d0, a1, a0, b1, b0) \
do { \
const ulbn_dlimb_t __d = ulbn_cast_dlimb(_ulbn_dlimb_((a1), (a0)) - _ulbn_dlimb_((b1), (b0))); \
(d1) = ulbn_cast_limb((__d) >> ULBN_LIMB_BITS); \
(d0) = ulbn_cast_limb((__d)); \
} while(0)
#define _ulbn_umul_(p1, p0, u, v) \
do { \
const ulbn_dlimb_t __p = ulbn_cast_dlimb(ulbn_cast_dlimb((u)) * (v)); \
(p1) = ulbn_cast_limb((__p) >> ULBN_LIMB_BITS); \
(p0) = ulbn_cast_limb((__p)); \
} while(0)
#define _ulbn_udiv_(q, r, n1, n0, d) \
do { \
const ulbn_dlimb_t __n = _ulbn_dlimb_((n1), (n0)); \
(r) = ulbn_cast_limb(__n % (d)); \
(q) = ulbn_cast_limb(__n / (d)); \
} while(0)
#endif
#ifndef _ulbn_add_
#define _ulbn_add_(s1, s0, a1, a0, b1, b0) \
do { \
const ulbn_limb_t __s = ulbn_cast_limb((a0) + (b0)); \
(s1) = ulbn_cast_limb((a1) + (b1) + (__s < (a0))); \
(s0) = __s; \
} while(0)
#endif
#ifndef _ulbn_sub_
#define _ulbn_sub_(d1, d0, a1, a0, b1, b0) \
do { \
const ulbn_limb_t __d = ulbn_cast_limb((a0) - (b0)); \
(d1) = ulbn_cast_limb((a1) - (b1) - ((a0) < (b0))); \
(d0) = __d; \
} while(0)
#endif
#if !defined(_ulbn_umul_) && !defined(UL_PEDANTIC) && defined(_MSC_VER) && _ULBN_IS_64BIT(ULBN_LIMB_MAX)
#include <intrin.h>
#if defined(__x86_64__) || defined(_M_X64) /* x86_64 */
#if _MSC_VER >= 1900
#pragma intrinsic(_mul128)
#define _ulbn_umul_(p1, p0, u, v) (p0) = _umul128((u), (v), &(p1))
#endif
#elif defined(__arm64) || defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64) /* arm64 */
#if _MSC_VER >= 1900
#pragma intrinsic(__umulh)
#define _ulbn_umul_(p1, p0, u, v) ((void)((p0) = (u) * (v)), (p1) = __umulh((u), (v)))
#endif
#endif
#endif
#if !defined(_ulbn_udiv_) && !defined(UL_PEDANTIC) && defined(_MSC_VER) && _ULBN_IS_64BIT(ULBN_LIMB_MAX)
#include <intrin.h>
#if _MSC_VER >= 1929 && !defined(__clang__) && (defined(__x86_64__) || defined(_M_X64)) /* x86_64 */
#pragma intrinsic(_udiv128)
#define _ulbn_udiv_(q, r, n1, n0, d) ((q) = _udiv128((n1), (n0), (d), &(r)))
#endif
#endif
#ifndef _ulbn_umul_
#define _ulbn_umul_(p1, p0, u, v) \
do { \
const ulbn_limb_t __ul = ULBN_LOWPART(u); \
const ulbn_limb_t __uh = ULBN_HIGHPART(u); \
const ulbn_limb_t __vl = ULBN_LOWPART(v); \
const ulbn_limb_t __vh = ULBN_HIGHPART(v); \
const ulbn_limb_t __x0 = ulbn_cast_limb(__ul * __vl); \
ulbn_limb_t __x1 = ulbn_cast_limb(__ul * __vh); \
const ulbn_limb_t __x2 = ulbn_cast_limb(__uh * __vl); \
ulbn_limb_t __x3 = ulbn_cast_limb(__uh * __vh); \
__x1 += ULBN_HIGHPART(__x0); \
__x1 += __x2; \
if(__x1 < __x2) \
__x3 += ULBN_LIMB_SHL(1, ULBN_LIMB_HALF_BITS); \
(p0) = ulbn_cast_limb((__x1 << ULBN_LIMB_HALF_BITS) | ULBN_LOWPART(__x0)); \
(p1) = __x3 + ULBN_HIGHPART(__x1); \
} while(0)
#endif /* _ulbn_umul_ */
#ifndef _ulbn_udiv_norm_
#ifdef _ulbn_udiv_
#define _ulbn_udiv_norm_(q, r, n1, n0, d) _ulbn_udiv_((q), (r), (n1), (n0), (d))
#else
#define _ulbn_udiv_norm_(q, r, n1, n0, d) \
do { \
const ulbn_limb_t __dh = ULBN_HIGHPART(d); \
const ulbn_limb_t __dl = ULBN_LOWPART(d); \
ulbn_limb_t __qh, __ql, __r, __m; \
ulbn_assert((n1) < (d)); \
ulbn_assert((d) != 0); \
\
__qh = (n1) / __dh; \
__r = (n1) - __qh * __dh; \
__m = __qh * __dl; \
__r = ULBN_LIMB_SHL(__r, ULBN_LIMB_HALF_BITS) | ULBN_HIGHPART(n0); \
if(__r < __m) { \
--__qh, __r += (d); \
if(__r >= (d) && __r < __m) \
--__qh, __r += (d); \
} \
__r -= __m; \
\
__ql = __r / __dh; \
__r = __r - __ql * __dh; \
__m = __ql * __dl; \
__r = ULBN_LIMB_SHL(__r, ULBN_LIMB_HALF_BITS) | ULBN_LOWPART(n0); \
if(__r < __m) { \
--__ql, __r += (d); \
if(__r >= (d) && __r < __m) \
--__ql, __r += (d); \
} \
__r -= __m; \
\
(r) = __r; \
(q) = ULBN_LIMB_SHL(__qh, ULBN_LIMB_HALF_BITS) | __ql; \
} while(0)
#endif
#endif /* _ulbn_udiv_norm_ */
#ifndef _ulbn_udiv_
#define _ulbn_udiv_(q, r, n1, n0, d) \
do { \
if((n1) == 0) { \
(r) = (ulbn_cast_limb(n0) % (d)); \
(q) = (ulbn_cast_limb(n0) / (d)); \
} else { \
const int _U_shift = _ulbn_clz_(ulbn_cast_limb(d)); \
const ulbn_limb_t __U_n1 = (ulbn_cast_limb(n1) << _U_shift) \
| (ulbn_cast_limb(n0) >> (ulbn_cast_int(ULBN_LIMB_BITS) - _U_shift - 1) >> 1); \
const ulbn_limb_t __U_n0 = ulbn_cast_limb(n0) << _U_shift; \
const ulbn_limb_t __U_d = ulbn_cast_limb(d) << _U_shift; \
ulbn_limb_t __U_r; \
_ulbn_udiv_norm_((q), __U_r, __U_n1, __U_n0, __U_d); \
(r) = __U_r >> _U_shift; \
} \
} while(0)
#endif /* _ulbn_udiv_ */
/* See [Improved division by invariant integers](https://gmplib.org/~tege/division-paper.pdf) */
/* Let B to be 2^{ULBN_LIMB_BITS}, di = (B^2-1)//d1 - B */
ULBN_INTERNAL ulbn_limb_t _ulbn_divinv1(ulbn_limb_t d1) {
/*
* di = (B^2-1)//d1 - B
* di = <B-1-d1, B-1>//d1
*/
ulbn_limb_t n1, n0, di;
n1 = _ulbn_neg_(d1 + 1u);
n0 = _ulbn_neg_(1);
_ulbn_udiv_norm_(di, di, n1, n0, d1);
return di;
}
#define ulbn_divinv1(d1) ulbn_condexpr(((d1) & ULBN_LIMB_SIGNBIT) != 0 /* B/2 <= d1 < B */, _ulbn_divinv1(d1))
/* Let B to be 2^{ULBN_LIMB_BITS}, di = (B^3-1)//(d1*B+d0) - B */
ULBN_INTERNAL ulbn_limb_t _ulbn_divinv2(ulbn_limb_t d1, ulbn_limb_t d0) {
/*
* di = (B^3-1 - <d1,d0,0>)//<d1, d0>
* di = <B-1-d1, B-1-d0, B-1>//<d1, d0>
*/
ulbn_limb_t n1, n0, di;
ulbn_limb_t p, t1, t0;
/*
* di = (B^2-1)//d1 - B
* di = <B-1-d1, B-1>//d1
*/
n1 = _ulbn_neg_(d1 + 1u);
n0 = _ulbn_neg_(1);
_ulbn_udiv_norm_(di, di, n1, n0, d1);
/**
* calc <B-1-d1, B-1-d0>//d1
*
* As 0 < -d0/d1 <= 2 - 2/B,
* -2 <= <B-1-d1, B-1-d0>//d1 - <B-1-d1, B-1>//d1 <= 0
*/
p = ulbn_cast_limb(ulbn_cast_limb(d1 * di) + d0);
if(p < d0) {
--di;
if(p >= d1) {
--di;
p -= d1;
}
p -= d1;
}
_ulbn_umul_(t1, t0, d0, di); /* <t1, t0> = <0, d0> * <0, di> */
p += t1;
if(p < t1) {
--di;
if(p >= d1)
if(p > d1 || t0 >= d0)
--di;
}
return di;
}
#define ulbn_divinv2(d1, d0) ulbn_condexpr(((d1) & ULBN_LIMB_SIGNBIT) != 0 /* B/2 <= d1 < B */, _ulbn_divinv2(d1, d0))
#define _ulbn_udiv_2by1_(q, r, a1, a0, d, di) \
do { \
ulbn_limb_t __q1, __q0, __r; \
_ulbn_umul_(__q1, __q0, (a1), (di)); \
_ulbn_add_(__q1, __q0, __q1, __q0, (a1) + 1, (a0)); \
__r = ulbn_cast_limb((a0) - ulbn_cast_limb(__q1 * (d))); \
if(__r > __q0) { \
--__q1; \
__r += (d); \
} \
if(ul_unlikely(__r >= (d))) { \
++__q1; \
__r -= (d); \
} \
(r) = __r; \
(q) = __q1; \
} while(0)
#if 0
/* it seems that `ulbn_dlimb_t` cannot accelerate this */
#define _ulbn_udiv_3by2_(q, r1, r0, a2, a1, a0, d1, d0, di) \
do { \
ulbn_dlimb_t __q = ulbn_cast_dlimb(ulbn_cast_dlimb((a2)) * (di)); \
ulbn_dlimb_t __r; \
ulbn_dlimb_t __t; \
const ulbn_dlimb_t __d = _ulbn_dlimb_((d1), (d0)); \
__q += _ulbn_dlimb_((a2), (a1)); \
__t = ulbn_cast_dlimb(ulbn_cast_dlimb((d0)) * (__q >> ULBN_LIMB_BITS)); \
__r = _ulbn_dlimb_((a1) - ulbn_cast_limb(__q >> ULBN_LIMB_BITS) * (d1), ulbn_cast_limb(__r)); \
__r -= __t + __d; \
if(__r >= (ulbn_cast_dlimb(ulbn_cast_limb(__q)) << ULBN_LIMB_BITS)) { \
__q -= ulbn_cast_dlimb(ulbn_cast_dlimb(1) << ULBN_LIMB_BITS); \
__r += __d; \
} \
if(ul_unlikely(__r >= __d)) { \
__q += ulbn_cast_dlimb(ulbn_cast_dlimb(1) << ULBN_LIMB_BITS); \
__r -= __d; \
} \
(r1) = ulbn_cast_limb(__r); \
(r0) = ulbn_cast_limb(__r >> ULBN_LIMB_BITS); \
(q) = ulbn_cast_limb(__q >> ULBN_LIMB_BITS); \
} while(0)
#else
#define _ulbn_udiv_3by2_(q, r1, r0, a2, a1, a0, d1, d0, di) \
do { \
ulbn_limb_t __q1, __q0; \
ulbn_limb_t __r1, __r0; \
ulbn_limb_t __t1, __t0; \
_ulbn_umul_(__q1, __q0, (a2), (di)); \
_ulbn_add_(__q1, __q0, __q1, __q0, (a2), (a1)); \
__r1 = ulbn_cast_limb((a1) - __q1 * (d1)); \
_ulbn_umul_(__t1, __t0, (d0), __q1); \
_ulbn_sub_(__r1, __r0, __r1, (a0), __t1, __t0); \
_ulbn_sub_(__r1, __r0, __r1, __r0, (d1), (d0)); \
++__q1; \
if(__r1 >= __q0) { \
--__q1; \
_ulbn_add_(__r1, __r0, __r1, __r0, (d1), (d0)); \
} \
if(ul_unlikely(__r1 >= (d1))) \
if(ul_unlikely(__r1 > (d1) || __r0 >= (d0))) { \
++__q1; \
_ulbn_sub_(__r1, __r0, __r1, __r0, (d1), (d0)); \
} \
(r1) = __r1; \
(r0) = __r0; \
(q) = __q1; \
} while(0)
#endif
/*********************************
* <ulbn> Copy, Check, Normalize *
*********************************/
/* Normalize p[0:n] from high to low, return the length */
ULBN_INTERNAL ulbn_usize_t ulbn_rnorm(const ulbn_limb_t* p, ulbn_usize_t n) {
while(n > 0 && p[n - 1] == 0)
--n;
return n;
}
/* Normalize p[0:n] from low to high, return the index of the first non-zero element */
ULBN_INTERNAL ulbn_usize_t ulbn_fnorm(const ulbn_limb_t* p, ulbn_usize_t n) {
ulbn_usize_t i = 0;
while(i < n && p[i] == 0)
++i;
return i;
}
/* Fill p[0:n] with 0 */
ULBN_INTERNAL void ulbn_fill0(ulbn_limb_t* p, ulbn_usize_t n) {
ulbn_assert(ul_static_cast(size_t, n) < _ULBN_SIZET_MAX / sizeof(ulbn_limb_t));
memset(p, 0, ul_static_cast(size_t, n) * sizeof(ulbn_limb_t));
}
/* Fill p[0:n] with ~0 */
ULBN_INTERNAL void ulbn_fill1(ulbn_limb_t* p, ulbn_usize_t n) {
ulbn_assert(ul_static_cast(size_t, n) < _ULBN_SIZET_MAX / sizeof(ulbn_limb_t));
memset(p, 0xFF, ul_static_cast(size_t, n) * sizeof(ulbn_limb_t));
}
/* Check if p[0:n] is 0 */
ULBN_INTERNAL int ulbn_is0(const ulbn_limb_t* p, ulbn_usize_t n) {
return ulbn_fnorm(p, n) >= n;
}
/* Copy src[0:n] to dst[0:n], ensuring src[] and dest[] do not overlap */
ULBN_INTERNAL void ulbn_copy(ulbn_limb_t* ul_restrict dst, const ulbn_limb_t* ul_restrict src, ulbn_usize_t n) {
ulbn_assert_overlap(dst, n, src, n);
ulbn_assert(ul_static_cast(size_t, n) < _ULBN_SIZET_MAX / sizeof(ulbn_limb_t));
memcpy(dst, src, ul_static_cast(size_t, n) * sizeof(*dst));
}
/* Copy src[0:n] to dst[0:n], ensuring dest is after src */
ULBN_INTERNAL void ulbn_fcopy(ulbn_limb_t* dst, const ulbn_limb_t* src, ulbn_usize_t n) {
ulbn_assert_forward_overlap(dst, n, src, n);
ulbn_assert(ul_static_cast(size_t, n) < _ULBN_SIZET_MAX / sizeof(ulbn_limb_t));
memcpy(dst, src, ul_static_cast(size_t, n) * sizeof(*dst));
}
/* Copy src[0:n] to dst[0:n], ensuring dest is before src */
ULBN_INTERNAL void ulbn_rcopy(ulbn_limb_t* dst, const ulbn_limb_t* src, ulbn_usize_t n) {
ulbn_assert_backward_overlap(dst, n, src, n);
ulbn_assert(ul_static_cast(size_t, n) < _ULBN_SIZET_MAX / sizeof(ulbn_limb_t));
memmove(dst, src, ul_static_cast(size_t, n) * sizeof(*dst));
}
#if 0
/* Copy src[0:n] to dst[0:n], not ensuring anything */
ULBN_INTERNAL void ulbn_mcopy(ulbn_limb_t* dst, const ulbn_limb_t* src, ulbn_usize_t n) {
ulbn_assert(ul_static_cast(size_t, n) < _ULBN_SIZET_MAX / sizeof(ulbn_limb_t));
memmove(dst, src, ul_static_cast(size_t, n) * sizeof(*dst));
}
#endif
#define ulbn_maycopy(dst, src, n) ((dst) != (src) ? ulbn_copy((dst), (src), (n)) : (void)0)
/*************************
* <ulbn> Bit operations *
*************************/
/* rp[0:n] = ap[0:n] << b, return overflow part (do not write to rp[n]), ensure 0 < b < {ULBN_LIMB_BITS} */
ULBN_INTERNAL ulbn_limb_t ulbn_shl(ulbn_limb_t* rp, const ulbn_limb_t* ap, ulbn_usize_t n, int b) {
ulbn_usize_t i;
ulbn_limb_t rh, rl, ret;
const int br = ulbn_cast_int(ULBN_LIMB_BITS) - b;
ulbn_assert(n > 0);
ulbn_assert(0 < b && ulbn_cast_uint(b) < ULBN_LIMB_BITS);
ulbn_assert_backward_overlap(rp, n, ap, n);
rl = ap[n - 1];
ret = ulbn_cast_limb(rl >> br);
rh = ulbn_cast_limb(rl << b);
for(i = n - 1; i > 0; --i) {
rl = ap[i - 1];
rp[i] = ulbn_cast_limb(rh | (rl >> br));
rh = ulbn_cast_limb(rl << b);
}
rp[0] = rh;
return ret;
}
/* rp[0:n] = ap[0:n] >> b, return overflow part (do not write to rp[-1]), ensure 0 < b < {ULBN_LIMB_BITS} */
ULBN_INTERNAL ulbn_limb_t ulbn_shr(ulbn_limb_t* rp, const ulbn_limb_t* ap, ulbn_usize_t n, int b) {
ulbn_usize_t i;
ulbn_limb_t rh, rl, ret;
const int br = ulbn_cast_int(ULBN_LIMB_BITS) - b;
ulbn_assert(n > 0);
ulbn_assert(0 < b && ulbn_cast_uint(b) < ULBN_LIMB_BITS);
ulbn_assert_forward_overlap(rp, n, ap, n);
rh = ap[0];
ret = ulbn_cast_limb(rh << br);
rl = ulbn_cast_limb(rh >> b);
for(i = 1; i < n; ++i) {
rh = ap[i];
rp[i - 1] = ulbn_cast_limb(rl | (rh << br));
rl = ulbn_cast_limb(rh >> b);
}
rp[n - 1] = rl;
return ret;
}
ul_static_assert(ULBN_LIMB_BITS <= ULBN_BITS_MAX, "ulbn_usize_t must be able to hold at least ULBN_LIMB_BITS");
ULBN_INTERNAL ulbn_bits_t ulbn_ctz(const ulbn_limb_t* p, ulbn_usize_t n) {
ulbn_bits_t r = 0;
ulbn_usize_t i;
ulbn_assert(ulbn_cast_bits(n) <= ULBN_BITS_MAX / CHAR_BIT);
ulbn_assert(n == 0 || p[n - 1] != 0);
if(ul_unlikely(n == 0))
return 0;
for(i = 0; p[i] == 0; ++i)
r += ULBN_LIMB_BITS;
return ulbn_cast_bits(r + ulbn_cast_uint(_ulbn_ctz_(p[i])));
}
ULBN_INTERNAL ulbn_bits_t ulbn_cto(const ulbn_limb_t* p, ulbn_usize_t n) {
ulbn_bits_t r = 0;
ulbn_usize_t i;
ulbn_assert(ulbn_cast_bits(n) <= ULBN_BITS_MAX / CHAR_BIT);
ulbn_assert(n == 0 || p[n - 1] != 0);
if(ul_unlikely(n == 0))
return 0;
for(i = 0; p[i] == ULBN_LIMB_MAX; ++i)
r += ULBN_LIMB_BITS;
return ulbn_cast_bits(r + ulbn_cast_uint(_ulbn_ctz_(ulbn_cast_limb(~p[i]))));
}
ULBN_INTERNAL ulbn_bits_t ulbn_popcount(const ulbn_limb_t* p, ulbn_usize_t n) {
ulbn_bits_t r = 0;
ulbn_usize_t i;
ulbn_assert(ulbn_cast_bits(n) <= ULBN_BITS_MAX / CHAR_BIT);
ulbn_assert(n == 0 || p[n - 1] != 0);
for(i = 0; i < n; ++i)
r += ulbn_cast_bits(ulbn_cast_uint(_ulbn_popcount_(p[i])));
return r;
}
ULBN_INTERNAL ulbn_bits_t ulbn_bit_width(const ulbn_limb_t* p, ulbn_usize_t n) {
ulbn_assert(n > 0 && ulbn_cast_bits(n) <= ULBN_BITS_MAX / CHAR_BIT);
return ulbn_cast_bits(ulbn_cast_bits(n) * ULBN_LIMB_BITS - ulbn_cast_uint(_ulbn_clz_(p[n - 1])));
}
ULBN_INTERNAL ulbn_usize_t ulbn_set_ulong(ulbn_limb_t* p, ulbn_ulong_t v) {
ulbn_usize_t n = 0;
if(v == 0)
return 0;
#if ULBN_ULONG_MAX <= ULBN_LIMB_MAX
p[n++] = ulbn_cast_limb(v);
#else
do {
p[n++] = ulbn_cast_limb(v);
v >>= ULBN_LIMB_BITS;
} while(v);
#endif
return n;
}
/*********************
* <ulbn> Comparison *
*********************/
/* Compare ap[0:n] and bp[0:n], return direction (<0 means less than, =0 means equal, >0 means greater) */
ULBN_INTERNAL int ulbn_cmpn(const ulbn_limb_t* ap, const ulbn_limb_t* bp, ulbn_usize_t n) {
ulbn_usize_t i = n;
while(i-- > 0)
if(ap[i] != bp[i])
return ap[i] < bp[i] ? -1 : 1;
return 0;
}
/* Compare ap[0:an] and bp[0:bn], return direction (<0 means less than, =0 means equal, >0 means greater) */
ULBN_INTERNAL int ulbn_cmp(const ulbn_limb_t* ap, ulbn_usize_t an, const ulbn_limb_t* bp, ulbn_usize_t bn) {
ulbn_assert(an == 0 || ap[an - 1] != 0);
ulbn_assert(bn == 0 || bp[bn - 1] != 0);
if(an != bn)
return an < bn ? -1 : 1;
else
return ulbn_cmpn(ap, bp, an);
}
/* Compare ap[0:n] and (bp[0:bn] << b), return direction (<0 means less than, =0 means equal, >0 means greater) */
ULBN_INTERNAL int ulbn_cmpshl(const ulbn_limb_t* ap, ulbn_usize_t an, const ulbn_limb_t* bp, ulbn_usize_t bn, int b) {
const int br = ulbn_cast_int(ULBN_LIMB_BITS) - b;
ulbn_limb_t bh, bl;
ulbn_limb_t a0;
ulbn_usize_t i;
ulbn_assert(0 < b && ul_static_cast(size_t, b) < ULBN_LIMB_BITS);
if(an < bn)
return -1;
if(an - bn > 1)
return 1;
a0 = ulbn_cast_limb(an == bn ? 0 : ap[bn]);
bl = bp[bn - 1];
bh = ulbn_cast_limb(bl >> br);