-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintc.h
1977 lines (1653 loc) · 74.8 KB
/
intc.h
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
#if !defined(INTC_H)
#define INTC_H
// NOTE: Overview ==================================================================================
//
// calccrypto's uint128/256 C++ library converted into a single header file
// C/C++ library with some additional C-isms/cosmetic configurations. Some of
// the over-arching motivations of this library.
//
// - Single header, drop in - no build configuration required.
// - Minimal dependencies (only depends on stdbool.h when compiling in C).
// - Zero allocations (in other libs typically when converting to strings).
// - Ability to rename the API via the pre-processor for tighter integration
// into C code bases (See Configuration > INTC_API_PREFIX).
// - C++ features are optional and can be opt out via the pre-processor, i.e.
// operator overloading and constructors.
// (See Configuration > INTC_NO_CPP_FEATURES).
//
// NOTE: License ===================================================================================
//
// MIT License
// Copyright (c) 2021 github.com/doy-lee
// Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com
//
// 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.
//
// NOTE: Configuration
// -----------------------------------------------------------------------------
// #define INTC_IMPLEMENTATION
// Define this in one and only one C++ file to enable the implementation
// code of the header file
//
// #define INTC_ASSERT
// Define this macro to override the assert implementation in this file to
// a custom macro. If not overridden, when NDEBUG is defined- the assert is
// compiled out, otherwise a default assert macro is provided.
//
// #define INTC_NO_TYPEDEFS
// Define this macro to disable 'typedef struct intc_u128 intc_u128;' to
// force having to specify 'struct intc_u128' when declaring the integer
// types. Only relevant when compiling with a C compiler, otherwise ignored.
//
// #define INTC_NO_U256
// Define this macro to disable the uint256 code in the library.
//
// #define INTC_STATIC_API
// Define this macro to prefix all functions with the static qualifier
//
// #define INTC_NO_CPP_FEATURES
// Define this macro to disable the C++ constructors and operator
// overloading in the library. When this library is compiled as a C library,
// this macro is ignored. When compiling the library in C++ with this macro,
// the functions in the library will be exported with 'extern "C"' defined
// (i.e. prevent name-mangling).
//
// #define INTC_API_PREFIX
// Define this macro to rename the 'intc_u' part of the functions in this
// library to your desired name via the pre-processor, i.e.
//
// #define INTC_API_PREFIX(expr) MyNamespace_u##expr
// intc_u128 value = MyNamespace_u128_add(INTC_U128(128, 128), INTC_U128(128, 128));
//
// or
//
// #define INTC_API_PREFIX(expr) uint##expr
// intc_u128 value = uint128_add(INTC_U128(128, 128), INTC_U128(128, 128));
//
// NOTE: Examples ==================================================================================
/*
#define INTC_IMPLEMENTATION
#include "intc.h"
#include <stdio.h>
int main(int, char**)
{
#if defined(__cplusplus)
intc_u256 value = 32;
value += 32;
if (value == 64)
value *= 1'000'000'000'000;
intc_u256_string string = intc_u256_readable_int_str(value);
printf("%.*s\n", string.size, string.data); // 64,000,000,000,000
#else
intc_u256 value = INTC_U64_TO_U256(32);
value = intc_u256_add_u64(value, 32);
if (intc_u256_eq_u64(value, 64))
value = intc_u256_mul(value, INTC_U64_TO_U256(1'000'000'000'000));
intc_u256_string string = intc_u256_readable_int_str(value);
printf("%.*s\n", string.size, string.data); // 64,000,000,000,000
#endif
return 0;
}
*/
// NOTE: INTC Macros ===============================================================================
#if !defined(INTC_ASSERT)
#if defined(NDEBUG)
#define INTC_ASSERT(expr)
#else
#define INTC_ASSERT(expr) \
do { \
if (!(expr)) { \
(*(volatile int *)0) = 0; \
} \
} while (0)
#endif
#endif
#if !defined(INTC_API_PREFIX)
#define INTC_API_PREFIX(expr) intc_u##expr
#endif
#if !defined(INTC_API)
#if defined(INTC_STATIC_API)
#define INTC_API static
#else
#define INTC_API
#endif
#endif
#if defined(__cplusplus)
#if defined(INTC_NO_CPP_FEATURES)
#define INTC_BEGIN_EXTERN_C extern "C" {
#define INTC_END_EXTERN_C }
#else
#define INTC_BEGIN_EXTERN_C
#define INTC_END_EXTERN_C
#endif
#define INTC_ZERO_INIT {}
#else
#if !defined(INTC_NO_CPP_FEATURES)
#define INTC_NO_CPP_FEATURES
#endif
#define INTC_BEGIN_EXTERN_C
#define INTC_END_EXTERN_C
#define INTC_ZERO_INIT {0}
#include <stdbool.h>
#endif
// NOTE: Typedefs ==================================================================================
typedef unsigned char intc_u8;
typedef unsigned short intc_u16;
typedef unsigned int intc_u32;
typedef unsigned int intc_uint;
#ifdef _MSC_VER
typedef unsigned __int64 intc_u64;
#else
typedef unsigned long long intc_u64;
#endif
// NOTE: 128 Bit Unsigned Integer ==================================================================
struct intc_u128
{
#if !defined(INTC_NO_CPP_FEATURES)
intc_u128() = default;
intc_u128(intc_u64 lo) : lo(lo), hi(0) {}
intc_u128(intc_u64 lo, intc_u64 hi) : lo(lo), hi(hi) {}
#endif // INTC_NO_CPP_FEATURES
intc_u64 lo, hi;
};
struct intc_u128_divmod_result
{
struct intc_u128 quot;
struct intc_u128 rem;
};
struct intc_u128_string
{
// NOTE: Max value 340,282,366,920,938,463,463,374,607,431,768,211,455
char data[63 + 1];
int size;
};
struct intc_u128_init_result
{
bool success;
struct intc_u128 value;
};
// NOTE: Constructors ==============================================================================
#if defined (__cplusplus)
#define INTC_U128(lo, hi) intc_u128{(lo), (hi)}
#define INTC_U128_MIN intc_u128{0, 0}
#define INTC_U128_MAX intc_u128{(intc_u64)-1, (intc_u64)-1}
#define INTC_U128_ZERO intc_u128{0, 0}
#define INTC_U64_TO_U128(u64) intc_u128{(u64), 0}
#else
#define INTC_U128(lo, hi) (struct intc_u128){(lo), (hi)}
#define INTC_U128_MIN (struct intc_u128){0, 0}
#define INTC_U128_MAX (struct intc_u128){(intc_u64)-1, (intc_u64)-1}
#define INTC_U128_ZERO (struct intc_u128){0, 0}
#define INTC_U64_TO_U128(u64) (struct intc_u128){(u64), 0}
#if !defined(INTC_NO_TYPEDEFS)
typedef struct intc_u128 intc_u128;
typedef struct intc_u128_divmod_result intc_u128_divmod_result;
typedef struct intc_u128_string intc_u128_string;
#endif
#endif
INTC_BEGIN_EXTERN_C
// NOTE: U128 Converters ===========================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u128_init_hex_cstring(...)
//
// Construct a 128 unsigned integer from a string. This function supports
// hexadecimal strings with and without the 0x prefix i.e. "0xafc8a" or "afc8a"
// or "0xAFC8A" or "xafc8a" .
//
// The separator character given will be permitted and skipped in the string if
// encountered, e.g. ','. If no separator is permitted, pass 0 as the separator.
INTC_API struct intc_u128_init_result INTC_API_PREFIX(128_init_hex_cstring)(const char *string, size_t size, char separator);
// Construct a 128 unsigned integer from a base 10 number string.
//
// The separator character given will be permitted and skipped in the string if
// encountered, e.g. ','. If no separator is permitted, pass 0 as the separator.
INTC_API struct intc_u128_init_result INTC_API_PREFIX(128_init_cstring)(char const *string, size_t size, char separator);
// Interpret the 128 bit integer as a lower bit-type by using the lo bits of the
// integer and truncating where necessary.
INTC_API bool INTC_API_PREFIX(128_as_bool)(struct intc_u128 in);
INTC_API intc_u8 INTC_API_PREFIX(128_as_u8)(struct intc_u128 in);
INTC_API intc_u16 INTC_API_PREFIX(128_as_u16)(struct intc_u128 in);
INTC_API intc_u32 INTC_API_PREFIX(128_as_u32)(struct intc_u128 in);
INTC_API intc_u64 INTC_API_PREFIX(128_as_u64)(struct intc_u128 in);
// NOTE: U128 Bitwise ==============================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u128_and(...)
INTC_API struct intc_u128 INTC_API_PREFIX(128_and)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_or)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_xor)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_negate)(struct intc_u128 lhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_lshift)(struct intc_u128 lhs, unsigned rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_rshift)(struct intc_u128 lhs, unsigned rhs);
// NOTE: U128 Equality =============================================================================
// If INTC_API_PREFIX not defined then, for example: intc_u128_eq(...)
INTC_API bool INTC_API_PREFIX(128_eq)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API bool INTC_API_PREFIX(128_neq)(struct intc_u128 lhs, struct intc_u128 rhs);
// NOTE: U128 Equality U64 Helpers =================================================================
INTC_API bool INTC_API_PREFIX(128_eq_u64)(struct intc_u128 lhs, intc_u64 rhs);
INTC_API bool INTC_API_PREFIX(128_neq_u64)(struct intc_u128 lhs, intc_u64 rhs);
// NOTE: U128 Relational ===========================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u128_gt(...)
INTC_API bool INTC_API_PREFIX(128_gt)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API bool INTC_API_PREFIX(128_lt)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API bool INTC_API_PREFIX(128_gt_eq)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API bool INTC_API_PREFIX(128_lt_eq)(struct intc_u128 lhs, struct intc_u128 rhs);
// NOTE: U128 Relational U64 Helpers ===============================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u128_gt_u64(...)
INTC_API bool INTC_API_PREFIX(128_gt_u64)(struct intc_u128 lhs, intc_u64 rhs);
INTC_API bool INTC_API_PREFIX(128_lt_u64)(struct intc_u128 lhs, intc_u64 rhs);
INTC_API bool INTC_API_PREFIX(128_gt_eq_u64)(struct intc_u128 lhs, intc_u64 rhs);
INTC_API bool INTC_API_PREFIX(128_lt_eq_u64)(struct intc_u128 lhs, intc_u64 rhs);
// NOTE: U128 Arithmetic ===========================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u128_add(...)
INTC_API struct intc_u128 INTC_API_PREFIX(128_add)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_sub)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_mul)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API struct intc_u128_divmod_result INTC_API_PREFIX(128_divmod)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_div)(struct intc_u128 lhs, struct intc_u128 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_mod)(struct intc_u128 lhs, struct intc_u128 rhs);
// NOTE: U128 Arithmetic U64 Helpers ===============================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u128_add_u64(...)
INTC_API struct intc_u128 INTC_API_PREFIX(128_add_u64)(struct intc_u128 lhs, intc_u64 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_sub_u64)(struct intc_u128 lhs, intc_u64 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_mul_u64)(struct intc_u128 lhs, intc_u64 rhs);
INTC_API struct intc_u128_divmod_result INTC_API_PREFIX(128_divmod_u64)(struct intc_u128 lhs, intc_u64 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_div_u64)(struct intc_u128 lhs, intc_u64 rhs);
INTC_API struct intc_u128 INTC_API_PREFIX(128_mod_u64)(struct intc_u128 lhs, intc_u64 rhs);
// NOTE: U128 Misc =================================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u128_clz(...)
INTC_API int INTC_API_PREFIX(128_clz)(struct intc_u128 in); // CLZ (Count leading zeros)
// NOTE: U128 Printing =============================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u128_str(...)
// TODO(dqn): Add API for letting user pass in a buffer and we write into it.
// Convert the 128 bit unsigned integer into a string.
// base: The number system base to print the string as where base must be (2 < base < 36).
// seperate_every_n_chars: When > 0, the string will be separated by the 'seperate_ch' for every N
// characters specified in this parameter.
// seperate_ch: The character to use to separate the tring. If
// 'seperate_every_n_chars <= 0' then this parameter is ignored.
// return: The integer converted to a string, on failure, an empty string is
// returned. The string is always null-terminated. The string's size is not
// inclusive of the null-terminator.
INTC_API struct intc_u128_string INTC_API_PREFIX(128_str)(struct intc_u128 in, unsigned base, size_t separate_every_n_chars, char separate_ch);
// Helper function that calls intc_u128_str with base 10 (i.e. human readable).
INTC_API struct intc_u128_string INTC_API_PREFIX(128_int_str)(struct intc_u128 in, size_t separate_every_n_chars, char separate_ch);
// Helper function that calls intc_u128_str with base 10 (i.e. human readable),
// seperated by the thousands with a comma, i.e. 1,000.
INTC_API struct intc_u128_string INTC_API_PREFIX(128_readable_int_str)(struct intc_u128 in);
// Helper function that calls intc_u128_str with base 16 (i.e. hex)
INTC_API struct intc_u128_string INTC_API_PREFIX(128_hex_str)(struct intc_u128 in, size_t separate_every_n_chars, char separate_ch);
// Helper function that calls intc_u128_str with base 16 (i.e. hex), seperated
// every 4 hex characters with '_', i.e. ffff_ffff
INTC_API struct intc_u128_string INTC_API_PREFIX(128_readable_hex_str)(struct intc_u128 in);
INTC_END_EXTERN_C
#if !defined(INTC_NO_CPP_FEATURES)
// NOTE: U128 CPP Bitwise ==========================================================================
INTC_API intc_u128 operator&(intc_u128 lhs, intc_u128 rhs);
INTC_API intc_u128 operator|(intc_u128 lhs, intc_u128 rhs);
INTC_API intc_u128 operator^(intc_u128 lhs, intc_u128 rhs);
INTC_API intc_u128 operator~(intc_u128 lhs);
INTC_API intc_u128 operator<<(intc_u128 lhs, unsigned rhs);
INTC_API intc_u128 operator>>(intc_u128 lhs, unsigned rhs);
// NOTE: U128 CPP Equality =========================================================================
INTC_API bool operator==(intc_u128 lhs, intc_u128 rhs);
INTC_API bool operator!=(intc_u128 lhs, intc_u128 rhs);
// NOTE: U128 CPP Relational =======================================================================
INTC_API bool operator>(intc_u128 lhs, intc_u128 rhs);
INTC_API bool operator<(intc_u128 lhs, intc_u128 rhs);
INTC_API bool operator>=(intc_u128 lhs, intc_u128 rhs);
INTC_API bool operator<=(intc_u128 lhs, intc_u128 rhs);
// NOTE: U128 CPP Arithmetic =======================================================================
INTC_API intc_u128 operator+(intc_u128 lhs, intc_u128 rhs);
INTC_API intc_u128 operator-(intc_u128 lhs, intc_u128 rhs);
INTC_API intc_u128 operator*(intc_u128 lhs, intc_u128 rhs);
INTC_API intc_u128 operator/(intc_u128 lhs, intc_u128 rhs);
INTC_API intc_u128 operator%(intc_u128 lhs, intc_u128 rhs);
// NOTE: U128 CPP Other ============================================================================
INTC_API intc_u128 &operator&=(intc_u128 &lhs, intc_u128 rhs);
INTC_API intc_u128 &operator|=(intc_u128 &lhs, intc_u128 rhs);
INTC_API intc_u128 &operator^=(intc_u128 &lhs, intc_u128 rhs);
INTC_API intc_u128 &operator<<=(intc_u128 &lhs, unsigned rhs);
INTC_API intc_u128 &operator>>=(intc_u128 &lhs, unsigned rhs);
INTC_API intc_u128 &operator+=(intc_u128 &lhs, intc_u128 rhs);
INTC_API intc_u128 &operator-=(intc_u128 &lhs, intc_u128 rhs);
INTC_API intc_u128 &operator*=(intc_u128 &lhs, intc_u128 rhs);
INTC_API intc_u128 &operator/=(intc_u128 &lhs, intc_u128 rhs);
INTC_API intc_u128 &operator%=(intc_u128 &lhs, intc_u128 rhs);
INTC_API intc_u128 &operator++(intc_u128 &lhs);
INTC_API intc_u128 &operator--(intc_u128 &lhs);
INTC_API intc_u128 operator++(intc_u128 &lhs, int);
INTC_API intc_u128 operator--(intc_u128 &lhs, int);
#endif // !defined(INTC_NO_CPP_FEATURES)
// NOTE: 256 Bit Unsigned Integer ==================================================================
#if !defined(INTC_NO_U256)
struct intc_u256
{
#if !defined(INTC_NO_CPP_FEATURES)
intc_u256() = default;
intc_u256(intc_u64 lo_u64) { *this = {}; this->lo.lo = lo_u64; }
intc_u256(intc_u128 lo) { *this = {}; this->lo = lo; }
intc_u256(intc_u128 lo, intc_u128 hi) : lo(lo), hi(hi) {}
#endif // INTC_NO_CPP_FEATURES
struct intc_u128 lo, hi;
};
struct intc_u256_divmod_result
{
struct intc_u256 quot;
struct intc_u256 rem;
};
struct intc_u256_string
{
// NOTE: Max value 115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,665,640,564,039,457,584,007,913,129,639,935
char data[512 + 1];
int size;
};
struct intc_u256_init_result
{
bool success;
struct intc_u256 value;
};
// NOTE: U256 Constructors =========================================================================
#if defined(__cplusplus)
#define INTC_U256(lo, hi) intc_u256{lo, hi}
#define INTC_U256_MIN intc_u256{INTC_U128_MIN, INTC_U128_MIN}
#define INTC_U256_MAX intc_u256{INTC_U128_MAX, INTC_U128_MAX}
#define INTC_U256_ZERO intc_u256{INTC_U128_ZERO, INTC_U128_ZERO}
#define INTC_U64_TO_U256(u64) intc_u256{INTC_U64_TO_U128(u64), INTC_U128_ZERO}
#define INTC_U128_TO_U256(u128) intc_u256{(u128), 0}
#else
#define INTC_U256(lo, hi) (struct intc_u256){(lo), (hi)}
#define INTC_U256_MIN (struct intc_u256){INTC_U128_MIN, INTC_U128_MIN}
#define INTC_U256_MAX (struct intc_u256){INTC_U128_MAX, INTC_U128_MAX}
#define INTC_U256_ZERO (struct intc_u256){INTC_U128_ZERO, INTC_U128_ZERO}
#define INTC_U64_TO_U256(u64) (struct intc_u256){INTC_U64_TO_U128(u64), INTC_U128_ZERO}
#define INTC_U128_TO_U256(u128) (struct intc_u256){(u128), 0}
#if !defined(INTC_NO_TYPEDEFS)
typedef struct intc_u256 intc_u256;
typedef struct intc_u256_divmod_result intc_u256_divmod_result;
typedef struct intc_u256_string intc_u256_string;
#endif
#endif
INTC_BEGIN_EXTERN_C
// NOTE: U256 Converters ===========================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u256_init_hex_cstring(...)
//
// Construct a 128 unsigned integer from a string. This function supports
// hexadecimal strings with and without the 0x prefix i.e. "0xafc8a" or "afc8a"
// or "0xAFC8A" or "xafc8a".
//
// The separator character given will be permitted and skipped in the string if
// encountered, e.g. ','. If no separator is permitted, pass 0 as the separator.
INTC_API struct intc_u256_init_result INTC_API_PREFIX(256_init_hex_cstring)(char const *string, size_t size, char separator);
// Construct a 256 unsigned integer from a base 10 number string.
//
// The separator character given will be permitted and skipped in the string if
// encountered, e.g. ','. If no separator is permitted, pass 0 as the separator.
INTC_API struct intc_u256_init_result INTC_API_PREFIX(256_init_cstring)(char const *string, size_t size, char separator);
// Interpret the 256 bit integer as a lower bit-type by using the lo bits of the
// integer and truncating where necessary.
INTC_API bool INTC_API_PREFIX(256_as_bool)(struct intc_u256 in);
INTC_API intc_u8 INTC_API_PREFIX(256_as_u8)(struct intc_u256 in);
INTC_API intc_u16 INTC_API_PREFIX(256_as_u16)(struct intc_u256 in);
INTC_API intc_u32 INTC_API_PREFIX(256_as_u32)(struct intc_u256 in);
INTC_API intc_u64 INTC_API_PREFIX(256_as_u64)(struct intc_u256 in);
INTC_API struct intc_u128 INTC_API_PREFIX(256_as_u128)(struct intc_u256 in);
// NOTE: U256 Bitwise ==============================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u256_and(...)
INTC_API struct intc_u256 INTC_API_PREFIX(256_and)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_or)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_xor)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_and_u128)(struct intc_u256 lhs, struct intc_u128 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_or_u128)(struct intc_u256 lhs, struct intc_u128 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_xor_u128)(struct intc_u256 lhs, struct intc_u128 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_negate)(struct intc_u256 lhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_lshift)(struct intc_u256 lhs, unsigned shift);
INTC_API struct intc_u256 INTC_API_PREFIX(256_rshift)(struct intc_u256 lhs, unsigned shift);
// NOTE: U256 Equality =============================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u256_eq(...)
INTC_API bool INTC_API_PREFIX(256_eq)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API bool INTC_API_PREFIX(256_neq)(struct intc_u256 lhs, struct intc_u256 rhs);
// NOTE: U256 Equality U64 Helpers =================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u256_eq_u64(...)
INTC_API bool INTC_API_PREFIX(256_eq_u64)(struct intc_u256 lhs, intc_u64 rhs);
INTC_API bool INTC_API_PREFIX(256_neq_u64)(struct intc_u256 lhs, intc_u64 rhs);
// NOTE: U256 Relational ===========================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u256_gt(...)
INTC_API bool INTC_API_PREFIX(256_gt)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API bool INTC_API_PREFIX(256_lt)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API bool INTC_API_PREFIX(256_gt_eq)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API bool INTC_API_PREFIX(256_lt_eq)(struct intc_u256 lhs, struct intc_u256 rhs);
// NOTE: U256 Relational U64 Helpers ===============================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u256_gt_u64(...)
INTC_API bool INTC_API_PREFIX(256_gt_u64)(struct intc_u256 lhs, intc_u64 rhs);
INTC_API bool INTC_API_PREFIX(256_lt_u64)(struct intc_u256 lhs, intc_u64 rhs);
INTC_API bool INTC_API_PREFIX(256_gt_eq_u64)(struct intc_u256 lhs, intc_u64 rhs);
INTC_API bool INTC_API_PREFIX(256_lt_eq_u64)(struct intc_u256 lhs, intc_u64 rhs);
// NOTE: U256 Arithmetic ===========================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u256_add(...)
INTC_API struct intc_u256 INTC_API_PREFIX(256_add)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_sub)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_mul)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API struct intc_u256_divmod_result INTC_API_PREFIX(256_divmod)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_div)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_mod)(struct intc_u256 lhs, struct intc_u256 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_add_u64)(struct intc_u256 lhs, intc_u64 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_sub_u64)(struct intc_u256 lhs, intc_u64 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_mul_u64)(struct intc_u256 lhs, intc_u64 rhs);
INTC_API struct intc_u256_divmod_result INTC_API_PREFIX(256_divmod_u64)(struct intc_u256 lhs, intc_u64 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_div_u64)(struct intc_u256 lhs, intc_u64 rhs);
INTC_API struct intc_u256 INTC_API_PREFIX(256_mod_u64)(struct intc_u256 lhs, intc_u64 rhs);
// NOTE: U256 Misc =================================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u256_clz(...)
INTC_API int INTC_API_PREFIX(256_clz)(struct intc_u256 in);
// NOTE: U256 Printing =============================================================================
// Reminder: If INTC_API_PREFIX is not defined, example API looks like: intc_u256_str(...)
// Convert the 256 bit unsigned integer into a string.
// base: The number system base to print the string as where base must be (2 < base < 36).
// seperate_every_n_chars: When > 0, the string will be separated by the 'seperate_ch' for every N
// characters specified in this parameter.
// seperate_ch: The character to use to separate the tring. If
// 'seperate_every_n_chars <= 0' then this parameter is ignored.
// return: The integer converted to a string, on failure, an empty string is
// returned. The string is always null-terminated. The string's size is not
// inclusive of the null-terminator.
INTC_API struct intc_u256_string INTC_API_PREFIX(256_str)(struct intc_u256 in, unsigned base, size_t separate_every_n_chars, char separate_ch);
// Helper function that calls intc_u256_str with base 10 (i.e. human readable).
INTC_API struct intc_u256_string INTC_API_PREFIX(256_int_str)(struct intc_u256 in, size_t separate_every_n_chars, char separate_ch);
// Helper function that calls intc_u256_str with base 10 (i.e. human readable),
// seperated by the thousands with a comma, i.e. 1,000.
INTC_API struct intc_u256_string INTC_API_PREFIX(256_readable_int_str)(struct intc_u256 in);
// Helper function that calls intc_u256_str with base 16 (i.e. hex)
INTC_API struct intc_u256_string INTC_API_PREFIX(256_hex_str)(struct intc_u256 in, size_t separate_every_n_chars, char separate_ch);
// Helper function that calls intc_u256_str with base 16 (i.e. hex), seperated
// every 4 hex characters with '_', i.e. ffff_ffff
INTC_API struct intc_u256_string INTC_API_PREFIX(256_readable_hex_str)(struct intc_u256 in);
INTC_END_EXTERN_C
#if !defined(INTC_NO_CPP_FEATURES)
// NOTE: U256 CPP Bitwise ==========================================================================
INTC_API intc_u256 operator&(intc_u256 lhs, intc_u256 rhs);
INTC_API intc_u256 operator|(intc_u256 lhs, intc_u256 rhs);
INTC_API intc_u256 operator^(intc_u256 lhs, intc_u256 rhs);
INTC_API intc_u256 operator~(intc_u256 lhs);
INTC_API intc_u256 operator<<(intc_u256 lhs, unsigned rhs);
INTC_API intc_u256 operator>>(intc_u256 lhs, unsigned rhs);
// NOTE: U256 CPP Equality =========================================================================
INTC_API bool operator==(intc_u256 lhs, intc_u256 rhs);
INTC_API bool operator!=(intc_u256 lhs, intc_u256 rhs);
// NOTE: U256 CPP Relational =======================================================================
INTC_API bool operator>(intc_u256 lhs, intc_u256 rhs);
INTC_API bool operator<(intc_u256 lhs, intc_u256 rhs);
INTC_API bool operator>=(intc_u256 lhs, intc_u256 rhs);
INTC_API bool operator<=(intc_u256 lhs, intc_u256 rhs);
// NOTE: U256 CPP Arithmetic =======================================================================
INTC_API intc_u256 operator+(intc_u256 lhs, intc_u256 rhs);
INTC_API intc_u256 operator-(intc_u256 lhs, intc_u256 rhs);
INTC_API intc_u256 operator*(intc_u256 lhs, intc_u256 rhs);
INTC_API intc_u256 operator/(intc_u256 lhs, intc_u256 rhs);
INTC_API intc_u256 operator%(intc_u256 lhs, intc_u256 rhs);
// NOTE: U256 CPP Other ============================================================================
INTC_API intc_u256 &operator&=(intc_u256 &lhs, intc_u256 rhs);
INTC_API intc_u256 &operator|=(intc_u256 &lhs, intc_u256 rhs);
INTC_API intc_u256 &operator^=(intc_u256 &lhs, intc_u256 rhs);
INTC_API intc_u256 &operator<<=(intc_u256 &lhs, unsigned rhs);
INTC_API intc_u256 &operator>>=(intc_u256 &lhs, unsigned rhs);
INTC_API intc_u256 &operator+=(intc_u256 &lhs, intc_u256 rhs);
INTC_API intc_u256 &operator-=(intc_u256 &lhs, intc_u256 rhs);
INTC_API intc_u256 &operator*=(intc_u256 &lhs, intc_u256 rhs);
INTC_API intc_u256 &operator/=(intc_u256 &lhs, intc_u256 rhs);
INTC_API intc_u256 &operator%=(intc_u256 &lhs, intc_u256 rhs);
INTC_API intc_u256 &operator++(intc_u256 &lhs);
INTC_API intc_u256 &operator--(intc_u256 &lhs);
INTC_API intc_u256 operator++(intc_u256 &lhs, int);
INTC_API intc_u256 operator--(intc_u256 &lhs, int);
#endif // !defined(INTC_NO_CPP_FEATURES)
#endif // !defined(INTC_NO_U256)
#endif // INTC_H
#if defined(INTC_IMPLEMENTATION)
static bool const INTC__U8_IS_8_BITS [sizeof(intc_u8) == 1 ? 1 : -1] = INTC_ZERO_INIT;
static bool const INTC__U16_IS_16_BITS[sizeof(intc_u16) == 2 ? 1 : -1] = INTC_ZERO_INIT;
static bool const INTC__U32_IS_32_BITS[sizeof(intc_u32) == 4 ? 1 : -1] = INTC_ZERO_INIT;
static bool const INTC__U64_IS_64_BITS[sizeof(intc_u64) == 8 ? 1 : -1] = INTC_ZERO_INIT;
INTC_BEGIN_EXTERN_C
// NOTE: 128 Bit Unsigned Integer ==================================================================
// NOTE: U128 Converters ===========================================================================
INTC_API struct intc_u128_init_result INTC_API_PREFIX(128_init_hex_cstring)(char const *string, size_t size, char separator)
{
struct intc_u128_init_result result = INTC_ZERO_INIT;
if (string == 0 || size <= 0)
return result;
if (size >= 2 && string[0] == '0' && string[1] == 'x') { string += 2; size -= 2; }
else if (size >= 1 && string[0] == 'x') { string += 1; size -= 1; }
struct intc_u128 dest = INTC_ZERO_INIT;
for (size_t index = size - 1, bits_written = 0; index < size; index--) {
if (bits_written >= (sizeof(dest) * 8))
return result;
char hex_ch = string[index];
if (separator != 0 && hex_ch == separator)
continue;
unsigned char bits4 = (hex_ch >= '0' && hex_ch <= '9') ? 0 + (hex_ch - '0')
: (hex_ch >= 'a' && hex_ch <= 'f') ? 10 + (hex_ch - 'a')
: (hex_ch >= 'A' && hex_ch <= 'F') ? 10 + (hex_ch - 'A')
: 0xFF;
if (bits4 == 0xFF)
return result;
struct intc_u128 bits4_as_u128 = INTC_U64_TO_U128(bits4);
dest = INTC_API_PREFIX(128_or)(dest, (INTC_API_PREFIX(128_lshift)(bits4_as_u128, (unsigned)bits_written)));
bits_written += 4;
}
result.value = dest;
result.success = true;
return result;
}
size_t const INTC_API_PREFIX(U128_MAX_STRING_SIZE) = sizeof("340282366920938463463374607431768211455") - 1; // 2^128
INTC_API struct intc_u128_init_result INTC_API_PREFIX(128_init_cstring)(char const *string, size_t size, char separator)
{
struct intc_u128_init_result result = INTC_ZERO_INIT;
if (string == 0 || size <= 0)
return result;
struct intc_u128 dest = INTC_ZERO_INIT;
for (size_t index = 0, digits_written = 0; index < size; index++) {
if (digits_written >= INTC_API_PREFIX(U128_MAX_STRING_SIZE))
return result;
char digit = string[index];
if (separator != 0 && digit == separator)
continue;
intc_u64 value = (digit >= '0' && digit <= '9') ? (digit - '0') : 0xFF;
if (value == 0xFF)
return result;
dest = intc_u128_mul_u64(dest, 10);
dest = intc_u128_add_u64(dest, value);
digits_written++;
}
result.value = dest;
result.success = true;
return result;
}
INTC_API bool INTC_API_PREFIX(128_as_bool)(struct intc_u128 in)
{
bool result = in.lo | in.hi;
return result;
}
INTC_API intc_u8 INTC_API_PREFIX(128_as_u8)(struct intc_u128 in)
{
intc_u8 result = (intc_u8)in.lo;
return result;
}
INTC_API intc_u16 INTC_API_PREFIX(128_as_u16)(struct intc_u128 in)
{
intc_u16 result = (intc_u16)in.lo;
return result;
}
INTC_API intc_u32 INTC_API_PREFIX(128_as_u32)(struct intc_u128 in)
{
intc_u32 result = (intc_u32)in.lo;
return result;
}
INTC_API intc_u64 INTC_API_PREFIX(128_as_u64)(struct intc_u128 in)
{
intc_u64 result = (intc_u64)in.lo;
return result;
}
// NOTE: U128 Bitwise ==============================================================================
INTC_API struct intc_u128 INTC_API_PREFIX(128_and)(struct intc_u128 lhs, struct intc_u128 rhs)
{
struct intc_u128 result = INTC_U128(lhs.lo & rhs.lo, lhs.hi & rhs.hi);
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_or)(struct intc_u128 lhs, struct intc_u128 rhs)
{
struct intc_u128 result = INTC_U128(lhs.lo | rhs.lo, lhs.hi | rhs.hi);
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_xor)(struct intc_u128 lhs, struct intc_u128 rhs)
{
struct intc_u128 result = INTC_U128(lhs.lo ^ rhs.lo, lhs.hi ^ rhs.hi);
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_negate)(struct intc_u128 lhs)
{
struct intc_u128 result = INTC_U128(~lhs.lo, ~lhs.hi);
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_lshift)(struct intc_u128 lhs, unsigned shift)
{
if ((shift >= 128))
return INTC_U128_ZERO;
if (shift == 64)
return INTC_U128(0, lhs.lo);
if (shift == 0)
return lhs;
if (shift < 64)
return INTC_U128(lhs.lo << shift, (lhs.hi << shift) + (lhs.lo >> (64 - shift)));
if ((128 > shift) && (shift > 64))
return INTC_U128(0, lhs.lo << (shift - 64));
return INTC_U128_ZERO;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_rshift)(struct intc_u128 lhs, unsigned shift)
{
if ((shift >= 128))
return INTC_U128_ZERO;
if (shift == 64)
return INTC_U128(lhs.hi, 0);
if (shift == 0)
return lhs;
if (shift < 64)
return INTC_U128((lhs.hi << (64 - shift)) + (lhs.lo >> shift), lhs.hi >> shift);
if ((128 > shift) && (shift > 64))
return INTC_U128((lhs.hi >> (shift - 64)), 0);
return INTC_U128_ZERO;
}
// NOTE: U128 Equality =============================================================================
INTC_API bool INTC_API_PREFIX(128_eq)(struct intc_u128 lhs, struct intc_u128 rhs)
{
bool result = ((lhs.lo == rhs.lo) && (lhs.hi == rhs.hi));
return result;
}
INTC_API bool INTC_API_PREFIX(128_neq)(struct intc_u128 lhs, struct intc_u128 rhs)
{
bool result = ((lhs.lo != rhs.lo) | (lhs.hi != rhs.hi));
return result;
}
// NOTE: U128 Equality U64 Helpers =================================================================
INTC_API bool INTC_API_PREFIX(128_eq_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
bool result = INTC_API_PREFIX(128_eq)(lhs, INTC_U64_TO_U128(rhs));
return result;
}
INTC_API bool INTC_API_PREFIX(128_neq_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
bool result = INTC_API_PREFIX(128_neq)(lhs, INTC_U64_TO_U128(rhs));
return result;
}
// NOTE: U128 Relational ===========================================================================
INTC_API bool INTC_API_PREFIX(128_gt)(struct intc_u128 lhs, struct intc_u128 rhs)
{
bool result = (lhs.hi == rhs.hi) ? (lhs.lo > rhs.lo) : (lhs.hi > rhs.hi);
return result;
}
INTC_API bool INTC_API_PREFIX(128_lt)(struct intc_u128 lhs, struct intc_u128 rhs)
{
bool result = (lhs.hi == rhs.hi) ? (lhs.lo < rhs.lo) : (lhs.hi < rhs.hi);
return result;
}
INTC_API bool INTC_API_PREFIX(128_gt_eq)(struct intc_u128 lhs, struct intc_u128 rhs)
{
bool result = (lhs.hi == rhs.hi) ? (lhs.lo >= rhs.lo) : (lhs.hi >= rhs.hi);
return result;
}
INTC_API bool INTC_API_PREFIX(128_lt_eq)(struct intc_u128 lhs, struct intc_u128 rhs)
{
bool result = (lhs.hi == rhs.hi) ? (lhs.lo <= rhs.lo) : (lhs.hi <= rhs.hi);
return result;
}
// NOTE: U128 Relational U64 Helpers ===============================================================
INTC_API bool INTC_API_PREFIX(128_gt_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
bool result = INTC_API_PREFIX(128_gt)(lhs, INTC_U64_TO_U128(rhs));
return result;
}
INTC_API bool INTC_API_PREFIX(128_lt_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
bool result = INTC_API_PREFIX(128_lt)(lhs, INTC_U64_TO_U128(rhs));
return result;
}
INTC_API bool INTC_API_PREFIX(128_gt_eq_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
bool resugt_eq = INTC_API_PREFIX(128_gt_eq)(lhs, INTC_U64_TO_U128(rhs));
return resugt_eq;
}
INTC_API bool INTC_API_PREFIX(128_lt_eq_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
bool result_eq = INTC_API_PREFIX(128_lt_eq)(lhs, INTC_U64_TO_U128(rhs));
return result_eq;
}
// NOTE: U128 Arithmetic ===========================================================================
INTC_API struct intc_u128 INTC_API_PREFIX(128_add)(struct intc_u128 lhs, struct intc_u128 rhs)
{
struct intc_u128 result = INTC_U128(lhs.lo + rhs.lo, lhs.hi + rhs.hi + ((lhs.lo + rhs.lo) < lhs.lo));
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_sub)(struct intc_u128 lhs, struct intc_u128 rhs)
{
struct intc_u128 result = INTC_U128(lhs.lo - rhs.lo, lhs.hi - rhs.hi - ((lhs.lo - rhs.lo) > lhs.lo));
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_mul)(struct intc_u128 lhs, struct intc_u128 rhs)
{
// split values into 4 32-bit parts
intc_u64 top[4] = {lhs.hi >> 32, lhs.hi & 0xffffffff, lhs.lo >> 32, lhs.lo & 0xffffffff};
intc_u64 bottom[4] = {rhs.hi >> 32, rhs.hi & 0xffffffff, rhs.lo >> 32, rhs.lo & 0xffffffff};
intc_u64 products[4][4];
// multiply each component of the values
for (int y = 3; y > -1; y--) {
for (int x = 3; x > -1; x--) {
products[3 - x][y] = top[x] * bottom[y];
}
}
// first row
intc_u64 fourth32 = (products[0][3] & 0xffffffff);
intc_u64 third32 = (products[0][2] & 0xffffffff) + (products[0][3] >> 32);
intc_u64 second32 = (products[0][1] & 0xffffffff) + (products[0][2] >> 32);
intc_u64 first32 = (products[0][0] & 0xffffffff) + (products[0][1] >> 32);
// second row
third32 += (products[1][3] & 0xffffffff);
second32 += (products[1][2] & 0xffffffff) + (products[1][3] >> 32);
first32 += (products[1][1] & 0xffffffff) + (products[1][2] >> 32);
// third row
second32 += (products[2][3] & 0xffffffff);
first32 += (products[2][2] & 0xffffffff) + (products[2][3] >> 32);
// fourth row
first32 += (products[3][3] & 0xffffffff);
// move carry to next digit
third32 += fourth32 >> 32;
second32 += third32 >> 32;
first32 += second32 >> 32;
// remove carry from current digit
fourth32 &= 0xffffffff;
third32 &= 0xffffffff;
second32 &= 0xffffffff;
first32 &= 0xffffffff;
// combine components
struct intc_u128 result = INTC_U128((third32 << 32) | fourth32, (first32 << 32) | second32);
return result;
}
INTC_API struct intc_u128_divmod_result INTC_API_PREFIX(128_divmod)(struct intc_u128 lhs, struct intc_u128 rhs)
{
// Save some calculations /////////////////////
struct intc_u128_divmod_result result = INTC_ZERO_INIT;
if (INTC_API_PREFIX(128_eq)(rhs, INTC_U128_ZERO)) {
INTC_ASSERT(!"Division by zero");
return result;
}
if (INTC_API_PREFIX(128_eq)(rhs, INTC_U64_TO_U128(1))) {
result.quot = lhs;
return result;
}
if (INTC_API_PREFIX(128_eq)(lhs, rhs)) {
result.quot = INTC_U64_TO_U128(1);
return result;
}
if (INTC_API_PREFIX(128_eq)(lhs, INTC_U128_ZERO) || INTC_API_PREFIX(128_lt)(lhs, rhs)) {
result.rem = lhs;
return result;
}
int count = INTC_API_PREFIX(128_clz)(lhs);
for (int x = count; x > 0; x--) {
result.quot = INTC_API_PREFIX(128_lshift)(result.quot, 1);
result.rem = INTC_API_PREFIX(128_lshift)(result.rem, 1);
if (INTC_API_PREFIX(128_rshift)(lhs, x - 1U).lo & 1)
result.rem = INTC_API_PREFIX(128_add)(result.rem, INTC_U64_TO_U128(1));
if (INTC_API_PREFIX(128_gt_eq)(result.rem, rhs)) {
result.rem = INTC_API_PREFIX(128_sub)(result.rem, rhs);
result.quot = INTC_API_PREFIX(128_add)(result.quot, INTC_U64_TO_U128(1));
}
}
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_div)(struct intc_u128 lhs, struct intc_u128 rhs)
{
struct intc_u128 result = INTC_API_PREFIX(128_divmod)(lhs, rhs).quot;
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_mod)(struct intc_u128 lhs, struct intc_u128 rhs)
{
struct intc_u128 result = INTC_API_PREFIX(128_divmod)(lhs, rhs).rem;
return result;
}
// NOTE: U128 Arithmetic U64 Helpers ===============================================================
INTC_API struct intc_u128 INTC_API_PREFIX(128_add_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
struct intc_u128 result = INTC_API_PREFIX(128_add)(lhs, INTC_U64_TO_U128(rhs));
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_sub_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
struct intc_u128 result = INTC_API_PREFIX(128_sub)(lhs, INTC_U64_TO_U128(rhs));
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_mul_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
struct intc_u128 result = INTC_API_PREFIX(128_mul)(lhs, INTC_U64_TO_U128(rhs));
return result;
}
INTC_API struct intc_u128_divmod_result INTC_API_PREFIX(128_divmod_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
struct intc_u128_divmod_result result = INTC_API_PREFIX(128_divmod)(lhs, INTC_U64_TO_U128(rhs));
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_div_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
struct intc_u128 result = INTC_API_PREFIX(128_div)(lhs, INTC_U64_TO_U128(rhs));
return result;
}
INTC_API struct intc_u128 INTC_API_PREFIX(128_mod_u64)(struct intc_u128 lhs, intc_u64 rhs)
{
struct intc_u128 result = INTC_API_PREFIX(128_mod)(lhs, INTC_U64_TO_U128(rhs));
return result;
}
// NOTE: U128 Misc =================================================================================
INTC_API int INTC_API_PREFIX(128_clz)(struct intc_u128 in)
{
int result = in.hi ? 64 /*include the 64 bits of the low part*/ : 0;
for (intc_u64 val = result ? in.hi : in.lo;
val;
val >>= 1, result++)
;
return result;