| ... | ... | @@ -166,6 +166,12 @@ |
| 166 | 166 | #endif |
| 167 | 167 | #define zig_expand_has_builtin(b) zig_has_builtin(b) |
| 168 | 168 | |
| 169 | #if defined(__has_feature) |
| 170 | #define zig_has_feature(feature) __has_feature(feature) |
| 171 | #else |
| 172 | #define zig_has_feature(feature) 0 |
| 173 | #endif |
| 174 | |
| 169 | 175 | #if defined(__has_attribute) |
| 170 | 176 | #define zig_has_attribute(attribute) __has_attribute(attribute) |
| 171 | 177 | #else |
| ... | ... | @@ -175,9 +181,9 @@ |
| 175 | 181 | #if __STDC_VERSION__ >= 201112L |
| 176 | 182 | #define zig_static_assert(cond, msg) _Static_assert(cond, msg) |
| 177 | 183 | #elif zig_has_attribute(unused) |
| 178 | | #define zig_static_assert(cond, _) typedef char zig_expand_concat(zig_static_assert_fail_, __LINE__)[!!(cond)] __attribute__((unused)) |
| 184 | #define zig_static_assert(cond, msg) typedef char zig_expand_concat(zig_static_assert_fail_, __LINE__)[(cond) ? 1 : -1] __attribute__((unused)) |
| 179 | 185 | #else |
| 180 | | #define zig_static_assert(cond, _) typedef char zig_expand_concat(zig_static_assert_fail_, __LINE__)[!!(cond)] |
| 186 | #define zig_static_assert(cond, msg) typedef char zig_expand_concat(zig_static_assert_fail_, __LINE__)[(cond) ? 1 : -1] |
| 181 | 187 | #endif |
| 182 | 188 | |
| 183 | 189 | #if __STDC_VERSION__ >= 202311L |
| ... | ... | @@ -193,10 +199,8 @@ |
| 193 | 199 | #endif |
| 194 | 200 | |
| 195 | 201 | #if defined(zig_msvc) |
| 196 | | #define zig_const_arr |
| 197 | 202 | #define zig_callconv(c) __##c |
| 198 | 203 | #else |
| 199 | | #define zig_const_arr static const |
| 200 | 204 | #define zig_callconv(c) __attribute__((c)) |
| 201 | 205 | #endif |
| 202 | 206 | |
| ... | ... | @@ -267,12 +271,20 @@ |
| 267 | 271 | |
| 268 | 272 | #if __STDC_VERSION__ >= 202311L |
| 269 | 273 | #define zig_align(alignment) alignas(alignment) |
| 270 | | #elif __STDC_VERSION__ >= 201112L |
| 274 | #elif __STDC_VERSION__ >= 201112L || zig_has_feature(c_alignas) |
| 271 | 275 | #define zig_align(alignment) _Alignas(alignment) |
| 272 | 276 | #else |
| 273 | 277 | #define zig_align(alignment) zig_under_align(alignment) |
| 274 | 278 | #endif |
| 275 | 279 | |
| 280 | #if __STDC_VERSION__ >= 202311L |
| 281 | #define zig_alignOf(Type) alignof(Type) |
| 282 | #elif __STDC_VERSION__ >= 201112L || zig_has_feature(c_alignof) |
| 283 | #define zig_alignOf(Type) _Alignof(Type) |
| 284 | #else |
| 285 | #define zig_alignOf(Type) (sizeof(struct { char c; Type t; }) - sizeof(Type)) |
| 286 | #endif |
| 287 | |
| 276 | 288 | #if zig_has_attribute(aligned) || defined(zig_tinyc) |
| 277 | 289 | #define zig_align_fn(alignment) __attribute__((aligned(alignment))) |
| 278 | 290 | #elif defined(zig_msvc) |
| ... | ... | @@ -350,11 +362,9 @@ |
| 350 | 362 | #define zig_export(symbol, name) __attribute__((alias(symbol))) |
| 351 | 363 | #else |
| 352 | 364 | #define zig_export(symbol, name) ; \ |
| 353 | | __asm(zig_mangle_c(name) " = " zig_mangle_c(symbol)) |
| 365 | __asm("\t.globl\t" zig_mangle_c(name) "\n" zig_mangle_c(name) " = " zig_mangle_c(symbol)) |
| 354 | 366 | #endif |
| 355 | 367 | |
| 356 | | #define zig_mangled_tentative zig_mangled |
| 357 | | #define zig_mangled_final zig_mangled |
| 358 | 368 | #if defined(zig_msvc) |
| 359 | 369 | #define zig_mangled(mangled, unmangled) ; \ |
| 360 | 370 | zig_export(#mangled, unmangled) |
| ... | ... | @@ -364,7 +374,7 @@ |
| 364 | 374 | #else /* zig_msvc */ |
| 365 | 375 | #define zig_mangled(mangled, unmangled) __asm(zig_mangle_c(unmangled)) |
| 366 | 376 | #define zig_mangled_export(mangled, unmangled, symbol) \ |
| 367 | | zig_mangled_final(mangled, unmangled) \ |
| 377 | zig_mangled(mangled, unmangled) \ |
| 368 | 378 | zig_export(symbol, unmangled) |
| 369 | 379 | #endif /* zig_msvc */ |
| 370 | 380 | |
| ... | ... | @@ -550,6 +560,9 @@ |
| 550 | 560 | #define zig_noreturn |
| 551 | 561 | #endif |
| 552 | 562 | |
| 563 | #define zig_has_always 1 |
| 564 | #define zig_has_never 0 |
| 565 | |
| 553 | 566 | #define zig_compiler_rt_abbrev_uint32_t si |
| 554 | 567 | #define zig_compiler_rt_abbrev_int32_t si |
| 555 | 568 | #define zig_compiler_rt_abbrev_uint64_t di |
| ... | ... | @@ -560,7 +573,11 @@ |
| 560 | 573 | #define zig_compiler_rt_abbrev_zig_f32 sf |
| 561 | 574 | #define zig_compiler_rt_abbrev_zig_f64 df |
| 562 | 575 | #define zig_compiler_rt_abbrev_zig_f80 xf |
| 576 | #ifdef zig_powerpc |
| 577 | #define zig_compiler_rt_abbrev_zig_f128 kf |
| 578 | #else |
| 563 | 579 | #define zig_compiler_rt_abbrev_zig_f128 tf |
| 580 | #endif |
| 564 | 581 | |
| 565 | 582 | zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, size_t); |
| 566 | 583 | zig_extern void *memset (void *, int, size_t); |
| ... | ... | @@ -645,16 +662,6 @@ typedef signed long long int16_t; |
| 645 | 662 | #define INT16_MAX ( INT16_C(0x7FFF)) |
| 646 | 663 | #define UINT16_MAX ( INT16_C(0xFFFF)) |
| 647 | 664 | |
| 648 | | #if defined(zig_ez80) |
| 649 | | typedef unsigned int uint24_t; |
| 650 | | typedef signed int int24_t; |
| 651 | | #define INT24_C(c) c |
| 652 | | #define UINT24_C(c) c##U |
| 653 | | #endif |
| 654 | | #define INT24_MIN (~INT24_C(0x7FFF)) |
| 655 | | #define INT24_MAX ( INT24_C(0x7FFF)) |
| 656 | | #define UINT24_MAX ( INT24_C(0xFFFF)) |
| 657 | | |
| 658 | 665 | #if SCHAR_MIN == ~0x7FFFFFFF && SCHAR_MAX == 0x7FFFFFFF && UCHAR_MAX == 0xFFFFFFFF |
| 659 | 666 | typedef unsigned char uint32_t; |
| 660 | 667 | typedef signed char int32_t; |
| ... | ... | @@ -685,17 +692,6 @@ typedef signed long long int32_t; |
| 685 | 692 | #define INT32_MAX ( INT32_C(0x7FFFFFFF)) |
| 686 | 693 | #define UINT32_MAX ( INT32_C(0xFFFFFFFF)) |
| 687 | 694 | |
| 688 | | #if defined(zig_ez80) |
| 689 | | typedef unsigned __int48 uint48_t; |
| 690 | | typedef signed __int48 int48_t; |
| 691 | | #define INT48_C(c) c |
| 692 | | /* no suffix */ |
| 693 | | #define UINT48_C(c) ((uint48_t)(c)) |
| 694 | | #endif |
| 695 | | #define INT48_MIN (~INT48_C(0x7FFFFFFFFFFF)) |
| 696 | | #define INT48_MAX ( INT48_C(0x7FFFFFFFFFFF)) |
| 697 | | #define UINT48_MAX ( INT48_C(0xFFFFFFFFFFFF)) |
| 698 | | |
| 699 | 695 | #if SCHAR_MIN == ~0x7FFFFFFFFFFFFFFF && SCHAR_MAX == 0x7FFFFFFFFFFFFFFF && UCHAR_MAX == 0xFFFFFFFFFFFFFFFF |
| 700 | 696 | typedef unsigned char uint64_t; |
| 701 | 697 | typedef signed char int64_t; |
| ... | ... | @@ -726,6 +722,27 @@ typedef signed long long int64_t; |
| 726 | 722 | #define INT64_MAX ( INT64_C(0x7FFFFFFFFFFFFFFF)) |
| 727 | 723 | #define UINT64_MAX ( INT64_C(0xFFFFFFFFFFFFFFFF)) |
| 728 | 724 | |
| 725 | #if defined(zig_ez80) |
| 726 | |
| 727 | typedef unsigned int uint24_t; |
| 728 | typedef signed int int24_t; |
| 729 | #define INT24_C(c) c |
| 730 | #define UINT24_C(c) c##U |
| 731 | #define INT24_MIN (~INT24_C(0x7FFF)) |
| 732 | #define INT24_MAX ( INT24_C(0x7FFF)) |
| 733 | #define UINT24_MAX ( INT24_C(0xFFFF)) |
| 734 | |
| 735 | typedef unsigned __int48 uint48_t; |
| 736 | typedef signed __int48 int48_t; |
| 737 | #define INT48_C(c) c |
| 738 | /* no suffix */ |
| 739 | #define UINT48_C(c) ((uint48_t)(c)) |
| 740 | #define INT48_MIN (~INT48_C(0x7FFFFFFFFFFF)) |
| 741 | #define INT48_MAX ( INT48_C(0x7FFFFFFFFFFF)) |
| 742 | #define UINT48_MAX ( INT48_C(0xFFFFFFFFFFFF)) |
| 743 | |
| 744 | #endif |
| 745 | |
| 729 | 746 | typedef size_t uintptr_t; |
| 730 | 747 | typedef ptrdiff_t intptr_t; |
| 731 | 748 | |
| ... | ... | @@ -739,23 +756,145 @@ typedef ptrdiff_t intptr_t; |
| 739 | 756 | #define zig_maxInt_i16 INT16_MAX |
| 740 | 757 | #define zig_minInt_u16 UINT16_C(0) |
| 741 | 758 | #define zig_maxInt_u16 UINT16_MAX |
| 742 | | #define zig_minInt_i24 INT24_MIN |
| 743 | | #define zig_maxInt_i24 INT24_MAX |
| 744 | | #define zig_minInt_u24 UINT24_C(0) |
| 745 | | #define zig_maxInt_u24 UINT24_MAX |
| 746 | 759 | #define zig_minInt_i32 INT32_MIN |
| 747 | 760 | #define zig_maxInt_i32 INT32_MAX |
| 748 | 761 | #define zig_minInt_u32 UINT32_C(0) |
| 749 | 762 | #define zig_maxInt_u32 UINT32_MAX |
| 750 | | #define zig_minInt_i48 INT48_MIN |
| 751 | | #define zig_maxInt_i48 INT48_MAX |
| 752 | | #define zig_minInt_u48 UINT48_C(0) |
| 753 | | #define zig_maxInt_u48 UINT48_MAX |
| 754 | 763 | #define zig_minInt_i64 INT64_MIN |
| 755 | 764 | #define zig_maxInt_i64 INT64_MAX |
| 756 | 765 | #define zig_minInt_u64 UINT64_C(0) |
| 757 | 766 | #define zig_maxInt_u64 UINT64_MAX |
| 758 | 767 | |
| 768 | // zig_promoted_T implements C integral promotions except with signedness preserved, which |
| 769 | // allows wrapping operations to avoid the ub that would be caused by the normal promotion. |
| 770 | |
| 771 | #if INT8_MAX <= INT_MAX |
| 772 | typedef unsigned int zig_promoted_i8; |
| 773 | #elif INT8_MAX <= LONG_MAX |
| 774 | typedef unsigned long zig_promoted_i8; |
| 775 | #elif INT8_MAX <= LLONG_MAX |
| 776 | typedef unsigned long long zig_promoted_i8; |
| 777 | #else |
| 778 | typedef int8_t zig_promoted_i8; |
| 779 | #endif |
| 780 | #if UINT8_MAX <= UINT_MAX |
| 781 | typedef unsigned int zig_promoted_u8; |
| 782 | #elif UINT8_MAX <= ULONG_MAX |
| 783 | typedef unsigned long zig_promoted_u8; |
| 784 | #elif UINT8_MAX <= ULLONG_MAX |
| 785 | typedef unsigned long long zig_promoted_u8; |
| 786 | #else |
| 787 | typedef uint8_t zig_promoted_u8; |
| 788 | #endif |
| 789 | |
| 790 | #if INT16_MAX <= INT_MAX |
| 791 | typedef unsigned int zig_promoted_i16; |
| 792 | #elif INT16_MAX <= LONG_MAX |
| 793 | typedef unsigned long zig_promoted_i16; |
| 794 | #elif INT16_MAX <= LLONG_MAX |
| 795 | typedef unsigned long long zig_promoted_i16; |
| 796 | #else |
| 797 | typedef int16_t zig_promoted_i16; |
| 798 | #endif |
| 799 | #if UINT16_MAX <= UINT_MAX |
| 800 | typedef unsigned int zig_promoted_u16; |
| 801 | #elif UINT16_MAX <= ULONG_MAX |
| 802 | typedef unsigned long zig_promoted_u16; |
| 803 | #elif UINT16_MAX <= ULLONG_MAX |
| 804 | typedef unsigned long long zig_promoted_u16; |
| 805 | #else |
| 806 | typedef uint16_t zig_promoted_u16; |
| 807 | #endif |
| 808 | |
| 809 | #if INT32_MAX <= INT_MAX |
| 810 | typedef unsigned int zig_promoted_i32; |
| 811 | #elif INT32_MAX <= LONG_MAX |
| 812 | typedef unsigned long zig_promoted_i32; |
| 813 | #elif INT32_MAX <= LLONG_MAX |
| 814 | typedef unsigned long long zig_promoted_i32; |
| 815 | #else |
| 816 | typedef int32_t zig_promoted_i32; |
| 817 | #endif |
| 818 | #if UINT32_MAX <= UINT_MAX |
| 819 | typedef unsigned int zig_promoted_u32; |
| 820 | #elif UINT32_MAX <= ULONG_MAX |
| 821 | typedef unsigned long zig_promoted_u32; |
| 822 | #elif UINT32_MAX <= ULLONG_MAX |
| 823 | typedef unsigned long long zig_promoted_u32; |
| 824 | #else |
| 825 | typedef uint32_t zig_promoted_u32; |
| 826 | #endif |
| 827 | |
| 828 | #if INT64_MAX <= INT_MAX |
| 829 | typedef unsigned int zig_promoted_i64; |
| 830 | #elif INT64_MAX <= LONG_MAX |
| 831 | typedef unsigned long zig_promoted_i64; |
| 832 | #elif INT64_MAX <= LLONG_MAX |
| 833 | typedef unsigned long long zig_promoted_i64; |
| 834 | #else |
| 835 | typedef int64_t zig_promoted_i64; |
| 836 | #endif |
| 837 | #if UINT64_MAX <= UINT_MAX |
| 838 | typedef unsigned int zig_promoted_u64; |
| 839 | #elif UINT64_MAX <= ULONG_MAX |
| 840 | typedef unsigned long zig_promoted_u64; |
| 841 | #elif UINT64_MAX <= ULLONG_MAX |
| 842 | typedef unsigned long long zig_promoted_u64; |
| 843 | #else |
| 844 | typedef uint64_t zig_promoted_u64; |
| 845 | #endif |
| 846 | |
| 847 | #ifdef zig_ez80 |
| 848 | |
| 849 | #define zig_minInt_i24 INT24_MIN |
| 850 | #define zig_maxInt_i24 INT24_MAX |
| 851 | #define zig_minInt_u24 UINT24_C(0) |
| 852 | #define zig_maxInt_u24 UINT24_MAX |
| 853 | #define zig_minInt_i48 INT48_MIN |
| 854 | #define zig_maxInt_i48 INT48_MAX |
| 855 | #define zig_minInt_u48 UINT48_C(0) |
| 856 | #define zig_maxInt_u48 UINT48_MAX |
| 857 | |
| 858 | #if INT24_MAX <= INT_MAX |
| 859 | typedef unsigned int zig_promoted_i24; |
| 860 | #elif INT24_MAX <= LONG_MAX |
| 861 | typedef unsigned long zig_promoted_i24; |
| 862 | #elif INT24_MAX <= LLONG_MAX |
| 863 | typedef unsigned long long zig_promoted_i24; |
| 864 | #else |
| 865 | typedef int24_t zig_promoted_i24; |
| 866 | #endif |
| 867 | #if UINT24_MAX <= UINT_MAX |
| 868 | typedef unsigned int zig_promoted_u24; |
| 869 | #elif UINT24_MAX <= ULONG_MAX |
| 870 | typedef unsigned long zig_promoted_u24; |
| 871 | #elif UINT24_MAX <= ULLONG_MAX |
| 872 | typedef unsigned long long zig_promoted_u24; |
| 873 | #else |
| 874 | typedef uint24_t zig_promoted_u24; |
| 875 | #endif |
| 876 | |
| 877 | #if INT48_MAX <= INT_MAX |
| 878 | typedef unsigned int zig_promoted_i48; |
| 879 | #elif INT48_MAX <= LONG_MAX |
| 880 | typedef unsigned long zig_promoted_i48; |
| 881 | #elif INT48_MAX <= LLONG_MAX |
| 882 | typedef unsigned long long zig_promoted_i48; |
| 883 | #else |
| 884 | typedef int48_t zig_promoted_i48; |
| 885 | #endif |
| 886 | #if UINT48_MAX <= UINT_MAX |
| 887 | typedef unsigned int zig_promoted_u48; |
| 888 | #elif UINT48_MAX <= ULONG_MAX |
| 889 | typedef unsigned long zig_promoted_u48; |
| 890 | #elif UINT48_MAX <= ULLONG_MAX |
| 891 | typedef unsigned long long zig_promoted_u48; |
| 892 | #else |
| 893 | typedef uint48_t zig_promoted_u48; |
| 894 | #endif |
| 895 | |
| 896 | #endif |
| 897 | |
| 759 | 898 | #define zig_intLimit(s, w, limit, bits) zig_shr_##s##w(zig_##limit##Int_##s##w, w - (bits)) |
| 760 | 899 | #define zig_minInt_i(w, bits) zig_intLimit(i, w, min, bits) |
| 761 | 900 | #define zig_maxInt_i(w, bits) zig_intLimit(i, w, max, bits) |
| ... | ... | @@ -770,7 +909,33 @@ typedef ptrdiff_t intptr_t; |
| 770 | 909 | zig_operator(Type, Type, operation, operator) |
| 771 | 910 | #define zig_shift_operator(Type, operation, operator) \ |
| 772 | 911 | zig_operator(Type, uint8_t, operation, operator) |
| 773 | | #define zig_int_helpers(w, PromotedUnsigned) \ |
| 912 | |
| 913 | #define zig_int_casts_common(bw, sw) \ |
| 914 | static inline uint##bw##_t zig_u##bw##_intCast_u##sw(uint##sw##_t arg) { \ |
| 915 | return arg; \ |
| 916 | } \ |
| 917 | \ |
| 918 | static inline uint##bw##_t zig_u##bw##_intCast_i##sw(int##sw##_t arg) { \ |
| 919 | return (uint##bw##_t)arg; \ |
| 920 | } \ |
| 921 | \ |
| 922 | static inline int##bw##_t zig_i##bw##_intCast_u##sw(uint##sw##_t arg) { \ |
| 923 | return arg; \ |
| 924 | } \ |
| 925 | \ |
| 926 | static inline int##bw##_t zig_i##bw##_intCast_i##sw(int##sw##_t arg) { \ |
| 927 | return arg; \ |
| 928 | } \ |
| 929 | \ |
| 930 | static inline uint##sw##_t zig_u##sw##_truncate_u##bw(uint##bw##_t arg, uint8_t bits) { \ |
| 931 | return (uint##sw##_t)arg & zig_maxInt_u(sw, bits); \ |
| 932 | } \ |
| 933 | \ |
| 934 | static inline int##sw##_t zig_i##sw##_truncate_i##bw(int##bw##_t arg, uint8_t bits) { \ |
| 935 | return ((uint##sw##_t)arg & UINT##sw##_C(1) << (bits - UINT8_C(1))) != UINT##sw##_C(0) \ |
| 936 | ? (int##sw##_t)arg | zig_minInt_i(sw, bits) : (int##sw##_t)arg & zig_maxInt_i(sw, bits); \ |
| 937 | } |
| 938 | #define zig_int_operators(w) \ |
| 774 | 939 | zig_basic_operator(uint##w##_t, and_u##w, &) \ |
| 775 | 940 | zig_basic_operator( int##w##_t, and_i##w, &) \ |
| 776 | 941 | zig_basic_operator(uint##w##_t, or_u##w, |) \ |
| ... | ... | @@ -786,44 +951,48 @@ typedef ptrdiff_t intptr_t; |
| 786 | 951 | return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \ |
| 787 | 952 | } \ |
| 788 | 953 | \ |
| 789 | | static inline uint##w##_t zig_not_u##w(uint##w##_t val, uint8_t bits) { \ |
| 790 | | return val ^ zig_maxInt_u(w, bits); \ |
| 954 | static inline uint##w##_t zig_not_u##w(uint##w##_t arg, uint8_t bits) { \ |
| 955 | return arg ^ zig_maxInt_u(w, bits); \ |
| 791 | 956 | } \ |
| 792 | 957 | \ |
| 793 | | static inline int##w##_t zig_not_i##w(int##w##_t val, uint8_t bits) { \ |
| 958 | static inline int##w##_t zig_not_i##w(int##w##_t arg, uint8_t bits) { \ |
| 794 | 959 | (void)bits; \ |
| 795 | | return ~val; \ |
| 960 | return ~arg; \ |
| 796 | 961 | } \ |
| 797 | 962 | \ |
| 798 | | static inline uint##w##_t zig_wrap_u##w(uint##w##_t val, uint8_t bits) { \ |
| 799 | | return val & zig_maxInt_u(w, bits); \ |
| 963 | zig_basic_operator(uint##w##_t, divFloor_u##w, /) \ |
| 964 | \ |
| 965 | static inline int##w##_t zig_divFloor_i##w(int##w##_t lhs, int##w##_t rhs) { \ |
| 966 | return lhs / rhs + (lhs % rhs != INT##w##_C(0) ? zig_shr_i##w(lhs ^ rhs, UINT8_C(w) - UINT8_C(1)) : INT##w##_C(0)); \ |
| 800 | 967 | } \ |
| 801 | 968 | \ |
| 802 | | static inline int##w##_t zig_wrap_i##w(int##w##_t val, uint8_t bits) { \ |
| 803 | | return (val & UINT##w##_C(1) << (bits - UINT8_C(1))) != 0 \ |
| 804 | | ? val | zig_minInt_i(w, bits) : val & zig_maxInt_i(w, bits); \ |
| 969 | static inline uint##w##_t zig_divCeil_u##w(uint##w##_t lhs, uint##w##_t rhs) { \ |
| 970 | return lhs / rhs + (lhs % rhs != UINT##w##_C(0) ? UINT##w##_C(1) : UINT##w##_C(0)); \ |
| 805 | 971 | } \ |
| 806 | 972 | \ |
| 807 | | static inline uint##w##_t zig_abs_i##w(int##w##_t val) { \ |
| 808 | | return (val < 0) ? -(uint##w##_t)val : (uint##w##_t)val; \ |
| 973 | static inline int##w##_t zig_divCeil_i##w(int##w##_t lhs, int##w##_t rhs) { \ |
| 974 | return lhs / rhs + (lhs % rhs != INT##w##_C(0) \ |
| 975 | ? zig_shr_i##w(lhs ^ rhs, UINT8_C(w) - UINT8_C(1)) + INT##w##_C(1) : INT##w##_C(0)); \ |
| 809 | 976 | } \ |
| 810 | 977 | \ |
| 811 | | zig_basic_operator(uint##w##_t, div_floor_u##w, /) \ |
| 978 | zig_basic_operator(uint##w##_t, mod_u##w, %) \ |
| 979 | zig_int_casts_common(w, w) \ |
| 812 | 980 | \ |
| 813 | | static inline int##w##_t zig_div_floor_i##w(int##w##_t lhs, int##w##_t rhs) { \ |
| 814 | | return lhs / rhs + (lhs % rhs != INT##w##_C(0) ? zig_shr_i##w(lhs ^ rhs, UINT8_C(w) - UINT8_C(1)) : INT##w##_C(0)); \ |
| 981 | static inline uint##w##_t zig_u##w##_bitCast_u##w(uint##w##_t arg, uint8_t bits) { \ |
| 982 | return zig_u##w##_truncate_u##w(arg, bits); \ |
| 815 | 983 | } \ |
| 816 | 984 | \ |
| 817 | | static inline uint##w##_t zig_div_ceil_u##w(uint##w##_t lhs, uint##w##_t rhs) { \ |
| 818 | | return lhs / rhs + (lhs % rhs != UINT##w##_C(0) ? UINT##w##_C(1) : UINT##w##_C(0)); \ |
| 985 | static inline uint##w##_t zig_u##w##_bitCast_i##w(int##w##_t arg, uint8_t bits) { \ |
| 986 | return zig_u##w##_bitCast_u##w((uint##w##_t)arg, bits); \ |
| 819 | 987 | } \ |
| 820 | 988 | \ |
| 821 | | static inline int##w##_t zig_div_ceil_i##w(int##w##_t lhs, int##w##_t rhs) { \ |
| 822 | | return lhs / rhs + (lhs % rhs != INT##w##_C(0) \ |
| 823 | | ? zig_shr_i##w(lhs ^ rhs, UINT8_C(w) - UINT8_C(1)) + INT##w##_C(1) : INT##w##_C(0)); \ |
| 989 | static inline int##w##_t zig_i##w##_bitCast_i##w(int##w##_t arg, uint8_t bits) { \ |
| 990 | return zig_i##w##_truncate_i##w(arg, bits); \ |
| 824 | 991 | } \ |
| 825 | 992 | \ |
| 826 | | zig_basic_operator(uint##w##_t, mod_u##w, %) \ |
| 993 | static inline int##w##_t zig_i##w##_bitCast_u##w(uint##w##_t arg, uint8_t bits) { \ |
| 994 | return zig_i##w##_bitCast_i##w((int##w##_t)arg, bits); \ |
| 995 | } \ |
| 827 | 996 | \ |
| 828 | 997 | static inline int##w##_t zig_mod_i##w(int##w##_t lhs, int##w##_t rhs) { \ |
| 829 | 998 | int##w##_t rem = lhs % rhs; \ |
| ... | ... | @@ -831,100 +1000,102 @@ typedef ptrdiff_t intptr_t; |
| 831 | 1000 | } \ |
| 832 | 1001 | \ |
| 833 | 1002 | static inline uint##w##_t zig_shlw_u##w(uint##w##_t lhs, uint8_t rhs, uint8_t bits) { \ |
| 834 | | return zig_wrap_u##w(zig_shl_u##w(lhs, rhs), bits); \ |
| 1003 | return zig_u##w##_truncate_u##w(zig_shl_u##w(lhs, rhs), bits); \ |
| 835 | 1004 | } \ |
| 836 | 1005 | \ |
| 837 | 1006 | static inline int##w##_t zig_shlw_i##w(int##w##_t lhs, uint8_t rhs, uint8_t bits) { \ |
| 838 | | return zig_wrap_i##w((int##w##_t)zig_shl_u##w((uint##w##_t)lhs, rhs), bits); \ |
| 1007 | return zig_i##w##_bitCast_u##w(zig_shl_u##w(zig_u##w##_bitCast_i##w(lhs, bits), rhs), bits); \ |
| 839 | 1008 | } \ |
| 840 | 1009 | \ |
| 841 | 1010 | static inline uint##w##_t zig_addw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 842 | | return zig_wrap_u##w(lhs + rhs, bits); \ |
| 1011 | return zig_u##w##_truncate_u##w((zig_promoted_u##w)lhs + rhs, bits); \ |
| 843 | 1012 | } \ |
| 844 | 1013 | \ |
| 845 | 1014 | static inline int##w##_t zig_addw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \ |
| 846 | | return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs + (uint##w##_t)rhs), bits); \ |
| 1015 | return zig_i##w##_bitCast_u##w(zig_addw_u##w(zig_u##w##_bitCast_i##w(lhs, bits), zig_u##w##_bitCast_i##w(rhs, bits), bits), bits); \ |
| 847 | 1016 | } \ |
| 848 | 1017 | \ |
| 849 | 1018 | static inline uint##w##_t zig_subw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 850 | | return zig_wrap_u##w(lhs - rhs, bits); \ |
| 1019 | return zig_u##w##_truncate_u##w((zig_promoted_u##w)lhs - rhs, bits); \ |
| 851 | 1020 | } \ |
| 852 | 1021 | \ |
| 853 | 1022 | static inline int##w##_t zig_subw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \ |
| 854 | | return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs - (uint##w##_t)rhs), bits); \ |
| 1023 | return zig_i##w##_bitCast_u##w(zig_subw_u##w(zig_u##w##_bitCast_i##w(lhs, bits), zig_u##w##_bitCast_i##w(rhs, bits), bits), bits); \ |
| 855 | 1024 | } \ |
| 856 | 1025 | \ |
| 857 | 1026 | static inline uint##w##_t zig_mulw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 858 | | return zig_wrap_u##w((PromotedUnsigned)lhs * rhs, bits); \ |
| 1027 | return zig_u##w##_truncate_u##w((zig_promoted_u##w)lhs * rhs, bits); \ |
| 859 | 1028 | } \ |
| 860 | 1029 | \ |
| 861 | 1030 | static inline int##w##_t zig_mulw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \ |
| 862 | | return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs * (uint##w##_t)rhs), bits); \ |
| 1031 | return zig_i##w##_bitCast_u##w(zig_mulw_u##w(zig_u##w##_bitCast_i##w(lhs, bits), zig_u##w##_bitCast_i##w(rhs, bits), bits), bits); \ |
| 1032 | } \ |
| 1033 | \ |
| 1034 | static inline uint##w##_t zig_abs_i##w(int##w##_t arg) { \ |
| 1035 | int##w##_t tmp = zig_shr_i##w(arg, UINT8_C(w) - UINT8_C(1)); \ |
| 1036 | return zig_u##w##_bitCast_i##w(zig_subw_i##w(zig_xor_i##w(arg, tmp), tmp, UINT8_C(w)), UINT8_C(w)); \ |
| 1037 | } \ |
| 1038 | \ |
| 1039 | static inline uint##w##_t zig_min_u##w(uint##w##_t lhs, uint##w##_t rhs) { \ |
| 1040 | return lhs < rhs ? lhs : rhs; \ |
| 1041 | } \ |
| 1042 | \ |
| 1043 | static inline int##w##_t zig_min_i##w(int##w##_t lhs, int##w##_t rhs) { \ |
| 1044 | return lhs < rhs ? lhs : rhs; \ |
| 1045 | } \ |
| 1046 | \ |
| 1047 | static inline uint##w##_t zig_max_u##w(uint##w##_t lhs, uint##w##_t rhs) { \ |
| 1048 | return lhs >= rhs ? lhs : rhs; \ |
| 1049 | } \ |
| 1050 | \ |
| 1051 | static inline int##w##_t zig_max_i##w(int##w##_t lhs, int##w##_t rhs) { \ |
| 1052 | return lhs >= rhs ? lhs : rhs; \ |
| 863 | 1053 | } |
| 864 | | #if UINT8_MAX <= UINT_MAX |
| 865 | | zig_int_helpers(8, unsigned int) |
| 866 | | #elif UINT8_MAX <= ULONG_MAX |
| 867 | | zig_int_helpers(8, unsigned long) |
| 868 | | #elif UINT8_MAX <= ULLONG_MAX |
| 869 | | zig_int_helpers(8, unsigned long long) |
| 870 | | #else |
| 871 | | zig_int_helpers(8, uint8_t) |
| 872 | | #endif |
| 873 | | #if UINT16_MAX <= UINT_MAX |
| 874 | | zig_int_helpers(16, unsigned int) |
| 875 | | #elif UINT16_MAX <= ULONG_MAX |
| 876 | | zig_int_helpers(16, unsigned long) |
| 877 | | #elif UINT16_MAX <= ULLONG_MAX |
| 878 | | zig_int_helpers(16, unsigned long long) |
| 879 | | #else |
| 880 | | zig_int_helpers(16, uint16_t) |
| 881 | | #endif |
| 882 | | #if defined(zig_ez80) |
| 883 | | #if UINT24_MAX <= UINT_MAX |
| 884 | | zig_int_helpers(24, unsigned int) |
| 885 | | #elif UINT24_MAX <= ULONG_MAX |
| 886 | | zig_int_helpers(24, unsigned long) |
| 887 | | #elif UINT24_MAX <= ULLONG_MAX |
| 888 | | zig_int_helpers(24, unsigned long long) |
| 889 | | #else |
| 890 | | zig_int_helpers(24, uint24_t) |
| 891 | | #endif |
| 892 | | #endif |
| 893 | | #if UINT32_MAX <= UINT_MAX |
| 894 | | zig_int_helpers(32, unsigned int) |
| 895 | | #elif UINT32_MAX <= ULONG_MAX |
| 896 | | zig_int_helpers(32, unsigned long) |
| 897 | | #elif UINT32_MAX <= ULLONG_MAX |
| 898 | | zig_int_helpers(32, unsigned long long) |
| 899 | | #else |
| 900 | | zig_int_helpers(32, uint32_t) |
| 901 | | #endif |
| 902 | | #if defined(zig_ez80) |
| 903 | | #if UINT24_MAX <= UINT_MAX |
| 904 | | zig_int_helpers(48, unsigned int) |
| 905 | | #elif UINT24_MAX <= ULONG_MAX |
| 906 | | zig_int_helpers(48, unsigned long) |
| 907 | | #elif UINT24_MAX <= ULLONG_MAX |
| 908 | | zig_int_helpers(48, unsigned long long) |
| 909 | | #else |
| 910 | | zig_int_helpers(48, uint48_t) |
| 911 | | #endif |
| 912 | | #endif |
| 913 | | #if UINT64_MAX <= UINT_MAX |
| 914 | | zig_int_helpers(64, unsigned int) |
| 915 | | #elif UINT64_MAX <= ULONG_MAX |
| 916 | | zig_int_helpers(64, unsigned long) |
| 917 | | #elif UINT64_MAX <= ULLONG_MAX |
| 918 | | zig_int_helpers(64, unsigned long long) |
| 919 | | #else |
| 920 | | zig_int_helpers(64, uint64_t) |
| 1054 | zig_int_operators(8) |
| 1055 | zig_int_operators(16) |
| 1056 | zig_int_operators(32) |
| 1057 | zig_int_operators(64) |
| 1058 | #ifdef zig_ez80 |
| 1059 | zig_int_operators(24) |
| 1060 | zig_int_operators(48) |
| 1061 | #endif |
| 1062 | |
| 1063 | #define zig_int_casts(bw, sw) \ |
| 1064 | static inline uint##sw##_t zig_u##sw##_intCast_u##bw(uint##bw##_t arg) { \ |
| 1065 | return (uint##sw##_t)arg; \ |
| 1066 | } \ |
| 1067 | \ |
| 1068 | static inline uint##sw##_t zig_u##sw##_intCast_i##bw(int##bw##_t arg) { \ |
| 1069 | return (uint##sw##_t)arg; \ |
| 1070 | } \ |
| 1071 | \ |
| 1072 | static inline int##sw##_t zig_i##sw##_intCast_u##bw(uint##bw##_t arg) { \ |
| 1073 | return (int##sw##_t)arg; \ |
| 1074 | } \ |
| 1075 | \ |
| 1076 | static inline int##sw##_t zig_i##sw##_intCast_i##bw(int##bw##_t arg) { \ |
| 1077 | return (int##sw##_t)arg; \ |
| 1078 | } \ |
| 1079 | \ |
| 1080 | zig_int_casts_common(bw, sw) |
| 1081 | zig_int_casts(16, 8) |
| 1082 | zig_int_casts(32, 8) |
| 1083 | zig_int_casts(64, 8) |
| 1084 | zig_int_casts(32, 16) |
| 1085 | zig_int_casts(64, 16) |
| 1086 | zig_int_casts(64, 32) |
| 1087 | #ifdef zig_ez80 |
| 1088 | zig_int_casts(32, 24) |
| 1089 | zig_int_casts(48, 24) |
| 1090 | zig_int_casts(64, 24) |
| 1091 | zig_int_casts(64, 48) |
| 921 | 1092 | #endif |
| 922 | 1093 | |
| 923 | 1094 | static inline bool zig_addo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) { |
| 924 | 1095 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 925 | 1096 | uint32_t full_res; |
| 926 | 1097 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 927 | | *res = zig_wrap_u32(full_res, bits); |
| 1098 | *res = zig_u32_truncate_u32(full_res, bits); |
| 928 | 1099 | return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits); |
| 929 | 1100 | #else |
| 930 | 1101 | *res = zig_addw_u32(lhs, rhs, bits); |
| ... | ... | @@ -936,19 +1107,19 @@ static inline bool zig_addo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t |
| 936 | 1107 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 937 | 1108 | int32_t full_res; |
| 938 | 1109 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1110 | *res = zig_i32_truncate_i32(full_res, bits); |
| 1111 | return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits); |
| 939 | 1112 | #else |
| 940 | | int32_t full_res = (int32_t)((uint32_t)lhs + (uint32_t)rhs); |
| 941 | | bool overflow = ((full_res ^ lhs) & (full_res ^ rhs)) < 0; |
| 1113 | *res = zig_addw_i32(lhs, rhs, bits); |
| 1114 | return ((*res ^ lhs) & (*res ^ rhs)) < INT32_C(0); |
| 942 | 1115 | #endif |
| 943 | | *res = zig_wrap_i32(full_res, bits); |
| 944 | | return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits); |
| 945 | 1116 | } |
| 946 | 1117 | |
| 947 | 1118 | static inline bool zig_addo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) { |
| 948 | 1119 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 949 | 1120 | uint64_t full_res; |
| 950 | 1121 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 951 | | *res = zig_wrap_u64(full_res, bits); |
| 1122 | *res = zig_u64_truncate_u64(full_res, bits); |
| 952 | 1123 | return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits); |
| 953 | 1124 | #else |
| 954 | 1125 | *res = zig_addw_u64(lhs, rhs, bits); |
| ... | ... | @@ -960,24 +1131,24 @@ static inline bool zig_addo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t |
| 960 | 1131 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 961 | 1132 | int64_t full_res; |
| 962 | 1133 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1134 | *res = zig_i64_truncate_i64(full_res, bits); |
| 1135 | return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits); |
| 963 | 1136 | #else |
| 964 | | int64_t full_res = (int64_t)((uint64_t)lhs + (uint64_t)rhs); |
| 965 | | bool overflow = ((full_res ^ lhs) & (full_res ^ rhs)) < 0; |
| 1137 | *res = zig_addw_i64(lhs, rhs, bits); |
| 1138 | return ((*res ^ lhs) & (*res ^ rhs)) < INT64_C(0); |
| 966 | 1139 | #endif |
| 967 | | *res = zig_wrap_i64(full_res, bits); |
| 968 | | return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits); |
| 969 | 1140 | } |
| 970 | 1141 | |
| 971 | 1142 | static inline bool zig_addo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) { |
| 972 | 1143 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 973 | 1144 | uint8_t full_res; |
| 974 | 1145 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 975 | | *res = zig_wrap_u8(full_res, bits); |
| 1146 | *res = zig_u8_truncate_u8(full_res, bits); |
| 976 | 1147 | return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits); |
| 977 | 1148 | #else |
| 978 | 1149 | uint32_t full_res; |
| 979 | 1150 | bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits); |
| 980 | | *res = (uint8_t)full_res; |
| 1151 | *res = zig_u8_intCast_u32(full_res); |
| 981 | 1152 | return overflow; |
| 982 | 1153 | #endif |
| 983 | 1154 | } |
| ... | ... | @@ -986,12 +1157,12 @@ static inline bool zig_addo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits |
| 986 | 1157 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 987 | 1158 | int8_t full_res; |
| 988 | 1159 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 989 | | *res = zig_wrap_i8(full_res, bits); |
| 1160 | *res = zig_i8_truncate_i8(full_res, bits); |
| 990 | 1161 | return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits); |
| 991 | 1162 | #else |
| 992 | 1163 | int32_t full_res; |
| 993 | 1164 | bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits); |
| 994 | | *res = (int8_t)full_res; |
| 1165 | *res = zig_i8_intCast_i32(full_res); |
| 995 | 1166 | return overflow; |
| 996 | 1167 | #endif |
| 997 | 1168 | } |
| ... | ... | @@ -1000,12 +1171,12 @@ static inline bool zig_addo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8 |
| 1000 | 1171 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 1001 | 1172 | uint16_t full_res; |
| 1002 | 1173 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1003 | | *res = zig_wrap_u16(full_res, bits); |
| 1174 | *res = zig_u16_truncate_u16(full_res, bits); |
| 1004 | 1175 | return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits); |
| 1005 | 1176 | #else |
| 1006 | 1177 | uint32_t full_res; |
| 1007 | 1178 | bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits); |
| 1008 | | *res = (uint16_t)full_res; |
| 1179 | *res = zig_u16_intCast_u32(full_res); |
| 1009 | 1180 | return overflow; |
| 1010 | 1181 | #endif |
| 1011 | 1182 | } |
| ... | ... | @@ -1014,27 +1185,28 @@ static inline bool zig_addo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t |
| 1014 | 1185 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 1015 | 1186 | int16_t full_res; |
| 1016 | 1187 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1017 | | *res = zig_wrap_i16(full_res, bits); |
| 1188 | *res = zig_i16_truncate_i16(full_res, bits); |
| 1018 | 1189 | return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits); |
| 1019 | 1190 | #else |
| 1020 | 1191 | int32_t full_res; |
| 1021 | 1192 | bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits); |
| 1022 | | *res = (int16_t)full_res; |
| 1193 | *res = zig_i16_intCast_i32(full_res); |
| 1023 | 1194 | return overflow; |
| 1024 | 1195 | #endif |
| 1025 | 1196 | } |
| 1026 | 1197 | |
| 1027 | 1198 | #if defined(zig_ez80) |
| 1199 | |
| 1028 | 1200 | static inline bool zig_addo_u24(uint24_t *res, uint24_t lhs, uint24_t rhs, uint8_t bits) { |
| 1029 | 1201 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 1030 | 1202 | uint24_t full_res; |
| 1031 | 1203 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1032 | | *res = zig_wrap_u24(full_res, bits); |
| 1204 | *res = zig_u24_truncate_u24(full_res, bits); |
| 1033 | 1205 | return overflow || full_res < zig_minInt_u(24, bits) || full_res > zig_maxInt_u(24, bits); |
| 1034 | 1206 | #else |
| 1035 | 1207 | uint32_t full_res; |
| 1036 | 1208 | bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits); |
| 1037 | | *res = (uint24_t)full_res; |
| 1209 | *res = zig_u24_intCast_u32(full_res); |
| 1038 | 1210 | return overflow; |
| 1039 | 1211 | #endif |
| 1040 | 1212 | } |
| ... | ... | @@ -1043,28 +1215,26 @@ static inline bool zig_addo_i24(int24_t *res, int24_t lhs, int24_t rhs, uint8_t |
| 1043 | 1215 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 1044 | 1216 | int24_t full_res; |
| 1045 | 1217 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1046 | | *res = zig_wrap_i24(full_res, bits); |
| 1218 | *res = zig_i24_truncate_i24(full_res, bits); |
| 1047 | 1219 | return overflow || full_res < zig_minInt_i(24, bits) || full_res > zig_maxInt_i(24, bits); |
| 1048 | 1220 | #else |
| 1049 | 1221 | int32_t full_res; |
| 1050 | 1222 | bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits); |
| 1051 | | *res = (int24_t)full_res; |
| 1223 | *res = zig_i24_intCast_i32(full_res); |
| 1052 | 1224 | return overflow; |
| 1053 | 1225 | #endif |
| 1054 | 1226 | } |
| 1055 | | #endif |
| 1056 | 1227 | |
| 1057 | | #if defined(zig_ez80) |
| 1058 | 1228 | static inline bool zig_addo_u48(uint48_t *res, uint48_t lhs, uint48_t rhs, uint8_t bits) { |
| 1059 | 1229 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 1060 | 1230 | uint48_t full_res; |
| 1061 | 1231 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1062 | | *res = zig_wrap_u48(full_res, bits); |
| 1232 | *res = zig_u48_truncate_u48(full_res, bits); |
| 1063 | 1233 | return overflow || full_res < zig_minInt_u(48, bits) || full_res > zig_maxInt_u(48, bits); |
| 1064 | 1234 | #else |
| 1065 | 1235 | uint64_t full_res; |
| 1066 | 1236 | bool overflow = zig_addo_u64(&full_res, lhs, rhs, bits); |
| 1067 | | *res = (uint48_t)full_res; |
| 1237 | *res = zig_u48_intCast_u64(full_res); |
| 1068 | 1238 | return overflow; |
| 1069 | 1239 | #endif |
| 1070 | 1240 | } |
| ... | ... | @@ -1073,22 +1243,23 @@ static inline bool zig_addo_i48(int48_t *res, int48_t lhs, int48_t rhs, uint8_t |
| 1073 | 1243 | #if zig_has_builtin(add_overflow) || defined(zig_gcc) |
| 1074 | 1244 | int48_t full_res; |
| 1075 | 1245 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1076 | | *res = zig_wrap_i48(full_res, bits); |
| 1246 | *res = zig_i48_truncate_i48(full_res, bits); |
| 1077 | 1247 | return overflow || full_res < zig_minInt_i(48, bits) || full_res > zig_maxInt_i(48, bits); |
| 1078 | 1248 | #else |
| 1079 | 1249 | int64_t full_res; |
| 1080 | 1250 | bool overflow = zig_addo_i64(&full_res, lhs, rhs, bits); |
| 1081 | | *res = (int48_t)full_res; |
| 1251 | *res = zig_i48_intCast_i64(full_res); |
| 1082 | 1252 | return overflow; |
| 1083 | 1253 | #endif |
| 1084 | 1254 | } |
| 1255 | |
| 1085 | 1256 | #endif |
| 1086 | 1257 | |
| 1087 | 1258 | static inline bool zig_subo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) { |
| 1088 | 1259 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1089 | 1260 | uint32_t full_res; |
| 1090 | 1261 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1091 | | *res = zig_wrap_u32(full_res, bits); |
| 1262 | *res = zig_u32_truncate_u32(full_res, bits); |
| 1092 | 1263 | return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits); |
| 1093 | 1264 | #else |
| 1094 | 1265 | *res = zig_subw_u32(lhs, rhs, bits); |
| ... | ... | @@ -1100,20 +1271,19 @@ static inline bool zig_subo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t |
| 1100 | 1271 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1101 | 1272 | int32_t full_res; |
| 1102 | 1273 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1274 | *res = zig_i32_truncate_i32(full_res, bits); |
| 1275 | return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits); |
| 1103 | 1276 | #else |
| 1104 | | int32_t full_res = (int32_t)((uint32_t)lhs - (uint32_t)rhs); |
| 1105 | | bool overflow = ((lhs ^ rhs) & (full_res ^ lhs)) < 0; |
| 1277 | *res = zig_subw_i32(lhs, rhs, bits); |
| 1278 | return ((lhs ^ rhs) & (*res ^ lhs)) < INT32_C(0); |
| 1106 | 1279 | #endif |
| 1107 | | *res = zig_wrap_i32(full_res, bits); |
| 1108 | | return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits); |
| 1109 | 1280 | } |
| 1110 | 1281 | |
| 1111 | | |
| 1112 | 1282 | static inline bool zig_subo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) { |
| 1113 | 1283 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1114 | 1284 | uint64_t full_res; |
| 1115 | 1285 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1116 | | *res = zig_wrap_u64(full_res, bits); |
| 1286 | *res = zig_u64_truncate_u64(full_res, bits); |
| 1117 | 1287 | return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits); |
| 1118 | 1288 | #else |
| 1119 | 1289 | *res = zig_subw_u64(lhs, rhs, bits); |
| ... | ... | @@ -1125,24 +1295,24 @@ static inline bool zig_subo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t |
| 1125 | 1295 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1126 | 1296 | int64_t full_res; |
| 1127 | 1297 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1298 | *res = zig_i64_truncate_i64(full_res, bits); |
| 1299 | return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits); |
| 1128 | 1300 | #else |
| 1129 | | int64_t full_res = (int64_t)((uint64_t)lhs - (uint64_t)rhs); |
| 1130 | | bool overflow = ((lhs ^ rhs) & (full_res ^ lhs)) < 0; |
| 1301 | *res = zig_subw_i64(lhs, rhs, bits); |
| 1302 | return ((lhs ^ rhs) & (*res ^ lhs)) < INT64_C(0); |
| 1131 | 1303 | #endif |
| 1132 | | *res = zig_wrap_i64(full_res, bits); |
| 1133 | | return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits); |
| 1134 | 1304 | } |
| 1135 | 1305 | |
| 1136 | 1306 | static inline bool zig_subo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) { |
| 1137 | 1307 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1138 | 1308 | uint8_t full_res; |
| 1139 | 1309 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1140 | | *res = zig_wrap_u8(full_res, bits); |
| 1310 | *res = zig_u8_truncate_u8(full_res, bits); |
| 1141 | 1311 | return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits); |
| 1142 | 1312 | #else |
| 1143 | 1313 | uint32_t full_res; |
| 1144 | 1314 | bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits); |
| 1145 | | *res = (uint8_t)full_res; |
| 1315 | *res = zig_u8_intCast_u32(full_res); |
| 1146 | 1316 | return overflow; |
| 1147 | 1317 | #endif |
| 1148 | 1318 | } |
| ... | ... | @@ -1151,12 +1321,12 @@ static inline bool zig_subo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits |
| 1151 | 1321 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1152 | 1322 | int8_t full_res; |
| 1153 | 1323 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1154 | | *res = zig_wrap_i8(full_res, bits); |
| 1324 | *res = zig_i8_truncate_i8(full_res, bits); |
| 1155 | 1325 | return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits); |
| 1156 | 1326 | #else |
| 1157 | 1327 | int32_t full_res; |
| 1158 | 1328 | bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits); |
| 1159 | | *res = (int8_t)full_res; |
| 1329 | *res = zig_i8_intCast_i32(full_res); |
| 1160 | 1330 | return overflow; |
| 1161 | 1331 | #endif |
| 1162 | 1332 | } |
| ... | ... | @@ -1165,12 +1335,12 @@ static inline bool zig_subo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8 |
| 1165 | 1335 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1166 | 1336 | uint16_t full_res; |
| 1167 | 1337 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1168 | | *res = zig_wrap_u16(full_res, bits); |
| 1338 | *res = zig_u16_truncate_u16(full_res, bits); |
| 1169 | 1339 | return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits); |
| 1170 | 1340 | #else |
| 1171 | 1341 | uint32_t full_res; |
| 1172 | 1342 | bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits); |
| 1173 | | *res = (uint16_t)full_res; |
| 1343 | *res = zig_u16_intCast_u32(full_res); |
| 1174 | 1344 | return overflow; |
| 1175 | 1345 | #endif |
| 1176 | 1346 | } |
| ... | ... | @@ -1179,27 +1349,28 @@ static inline bool zig_subo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t |
| 1179 | 1349 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1180 | 1350 | int16_t full_res; |
| 1181 | 1351 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1182 | | *res = zig_wrap_i16(full_res, bits); |
| 1352 | *res = zig_i16_truncate_i16(full_res, bits); |
| 1183 | 1353 | return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits); |
| 1184 | 1354 | #else |
| 1185 | 1355 | int32_t full_res; |
| 1186 | 1356 | bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits); |
| 1187 | | *res = (int16_t)full_res; |
| 1357 | *res = zig_i16_intCast_i32(full_res); |
| 1188 | 1358 | return overflow; |
| 1189 | 1359 | #endif |
| 1190 | 1360 | } |
| 1191 | 1361 | |
| 1192 | 1362 | #if defined(zig_ez80) |
| 1363 | |
| 1193 | 1364 | static inline bool zig_subo_u24(uint24_t *res, uint24_t lhs, uint24_t rhs, uint8_t bits) { |
| 1194 | 1365 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1195 | 1366 | uint24_t full_res; |
| 1196 | 1367 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1197 | | *res = zig_wrap_u24(full_res, bits); |
| 1368 | *res = zig_u24_truncate_u24(full_res, bits); |
| 1198 | 1369 | return overflow || full_res < zig_minInt_u(24, bits) || full_res > zig_maxInt_u(24, bits); |
| 1199 | 1370 | #else |
| 1200 | 1371 | uint32_t full_res; |
| 1201 | 1372 | bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits); |
| 1202 | | *res = (uint24_t)full_res; |
| 1373 | *res = zig_u24_intCast_u32(full_res); |
| 1203 | 1374 | return overflow; |
| 1204 | 1375 | #endif |
| 1205 | 1376 | } |
| ... | ... | @@ -1208,28 +1379,26 @@ static inline bool zig_subo_i24(int24_t *res, int24_t lhs, int24_t rhs, uint8_t |
| 1208 | 1379 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1209 | 1380 | int24_t full_res; |
| 1210 | 1381 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1211 | | *res = zig_wrap_i24(full_res, bits); |
| 1382 | *res = zig_i24_truncate_i24(full_res, bits); |
| 1212 | 1383 | return overflow || full_res < zig_minInt_i(24, bits) || full_res > zig_maxInt_i(24, bits); |
| 1213 | 1384 | #else |
| 1214 | 1385 | int32_t full_res; |
| 1215 | 1386 | bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits); |
| 1216 | | *res = (int24_t)full_res; |
| 1387 | *res = zig_i24_intCast_i32(full_res); |
| 1217 | 1388 | return overflow; |
| 1218 | 1389 | #endif |
| 1219 | 1390 | } |
| 1220 | | #endif |
| 1221 | 1391 | |
| 1222 | | #if defined(zig_ez80) |
| 1223 | 1392 | static inline bool zig_subo_u48(uint48_t *res, uint48_t lhs, uint48_t rhs, uint8_t bits) { |
| 1224 | 1393 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1225 | 1394 | uint48_t full_res; |
| 1226 | 1395 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1227 | | *res = zig_wrap_u48(full_res, bits); |
| 1396 | *res = zig_u48_truncate_u48(full_res, bits); |
| 1228 | 1397 | return overflow || full_res < zig_minInt_u(48, bits) || full_res > zig_maxInt_u(48, bits); |
| 1229 | 1398 | #else |
| 1230 | 1399 | uint64_t full_res; |
| 1231 | 1400 | bool overflow = zig_subo_u64(&full_res, lhs, rhs, bits); |
| 1232 | | *res = (uint48_t)full_res; |
| 1401 | *res = zig_u48_intCast_u64(full_res); |
| 1233 | 1402 | return overflow; |
| 1234 | 1403 | #endif |
| 1235 | 1404 | } |
| ... | ... | @@ -1238,22 +1407,23 @@ static inline bool zig_subo_i48(int48_t *res, int48_t lhs, int48_t rhs, uint8_t |
| 1238 | 1407 | #if zig_has_builtin(sub_overflow) || defined(zig_gcc) |
| 1239 | 1408 | int48_t full_res; |
| 1240 | 1409 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1241 | | *res = zig_wrap_i48(full_res, bits); |
| 1410 | *res = zig_i48_truncate_i48(full_res, bits); |
| 1242 | 1411 | return overflow || full_res < zig_minInt_i(48, bits) || full_res > zig_maxInt_i(48, bits); |
| 1243 | 1412 | #else |
| 1244 | 1413 | int64_t full_res; |
| 1245 | 1414 | bool overflow = zig_subo_i64(&full_res, lhs, rhs, bits); |
| 1246 | | *res = (int48_t)full_res; |
| 1415 | *res = zig_i48_intCast_i64(full_res); |
| 1247 | 1416 | return overflow; |
| 1248 | 1417 | #endif |
| 1249 | 1418 | } |
| 1419 | |
| 1250 | 1420 | #endif |
| 1251 | 1421 | |
| 1252 | 1422 | static inline bool zig_mulo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) { |
| 1253 | 1423 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1254 | 1424 | uint32_t full_res; |
| 1255 | 1425 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1256 | | *res = zig_wrap_u32(full_res, bits); |
| 1426 | *res = zig_u32_truncate_u32(full_res, bits); |
| 1257 | 1427 | return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits); |
| 1258 | 1428 | #else |
| 1259 | 1429 | *res = zig_mulw_u32(lhs, rhs, bits); |
| ... | ... | @@ -1261,8 +1431,8 @@ static inline bool zig_mulo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8 |
| 1261 | 1431 | #endif |
| 1262 | 1432 | } |
| 1263 | 1433 | |
| 1264 | | zig_extern int32_t __mulosi4(int32_t lhs, int32_t rhs, int *overflow); |
| 1265 | 1434 | static inline bool zig_mulo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) { |
| 1435 | zig_extern int32_t __mulosi4(int32_t lhs, int32_t rhs, int *overflow); |
| 1266 | 1436 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1267 | 1437 | int32_t full_res; |
| 1268 | 1438 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| ... | ... | @@ -1271,7 +1441,7 @@ static inline bool zig_mulo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t |
| 1271 | 1441 | int32_t full_res = __mulosi4(lhs, rhs, &overflow_int); |
| 1272 | 1442 | bool overflow = overflow_int != 0; |
| 1273 | 1443 | #endif |
| 1274 | | *res = zig_wrap_i32(full_res, bits); |
| 1444 | *res = zig_i32_truncate_i32(full_res, bits); |
| 1275 | 1445 | return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits); |
| 1276 | 1446 | } |
| 1277 | 1447 | |
| ... | ... | @@ -1279,7 +1449,7 @@ static inline bool zig_mulo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8 |
| 1279 | 1449 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1280 | 1450 | uint64_t full_res; |
| 1281 | 1451 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1282 | | *res = zig_wrap_u64(full_res, bits); |
| 1452 | *res = zig_u64_truncate_u64(full_res, bits); |
| 1283 | 1453 | return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits); |
| 1284 | 1454 | #else |
| 1285 | 1455 | *res = zig_mulw_u64(lhs, rhs, bits); |
| ... | ... | @@ -1287,8 +1457,8 @@ static inline bool zig_mulo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8 |
| 1287 | 1457 | #endif |
| 1288 | 1458 | } |
| 1289 | 1459 | |
| 1290 | | zig_extern int64_t __mulodi4(int64_t lhs, int64_t rhs, int *overflow); |
| 1291 | 1460 | static inline bool zig_mulo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) { |
| 1461 | zig_extern int64_t __mulodi4(int64_t lhs, int64_t rhs, int *overflow); |
| 1292 | 1462 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1293 | 1463 | int64_t full_res; |
| 1294 | 1464 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| ... | ... | @@ -1297,7 +1467,7 @@ static inline bool zig_mulo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t |
| 1297 | 1467 | int64_t full_res = __mulodi4(lhs, rhs, &overflow_int); |
| 1298 | 1468 | bool overflow = overflow_int != 0; |
| 1299 | 1469 | #endif |
| 1300 | | *res = zig_wrap_i64(full_res, bits); |
| 1470 | *res = zig_i64_truncate_i64(full_res, bits); |
| 1301 | 1471 | return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits); |
| 1302 | 1472 | } |
| 1303 | 1473 | |
| ... | ... | @@ -1305,12 +1475,12 @@ static inline bool zig_mulo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t b |
| 1305 | 1475 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1306 | 1476 | uint8_t full_res; |
| 1307 | 1477 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1308 | | *res = zig_wrap_u8(full_res, bits); |
| 1478 | *res = zig_u8_truncate_u8(full_res, bits); |
| 1309 | 1479 | return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits); |
| 1310 | 1480 | #else |
| 1311 | 1481 | uint32_t full_res; |
| 1312 | 1482 | bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits); |
| 1313 | | *res = (uint8_t)full_res; |
| 1483 | *res = zig_u8_intCast_u32(full_res); |
| 1314 | 1484 | return overflow; |
| 1315 | 1485 | #endif |
| 1316 | 1486 | } |
| ... | ... | @@ -1319,12 +1489,12 @@ static inline bool zig_mulo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits |
| 1319 | 1489 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1320 | 1490 | int8_t full_res; |
| 1321 | 1491 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1322 | | *res = zig_wrap_i8(full_res, bits); |
| 1492 | *res = zig_i8_truncate_i8(full_res, bits); |
| 1323 | 1493 | return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits); |
| 1324 | 1494 | #else |
| 1325 | 1495 | int32_t full_res; |
| 1326 | 1496 | bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits); |
| 1327 | | *res = (int8_t)full_res; |
| 1497 | *res = zig_i8_intCast_i32(full_res); |
| 1328 | 1498 | return overflow; |
| 1329 | 1499 | #endif |
| 1330 | 1500 | } |
| ... | ... | @@ -1333,12 +1503,12 @@ static inline bool zig_mulo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8 |
| 1333 | 1503 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1334 | 1504 | uint16_t full_res; |
| 1335 | 1505 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1336 | | *res = zig_wrap_u16(full_res, bits); |
| 1506 | *res = zig_u16_truncate_u16(full_res, bits); |
| 1337 | 1507 | return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits); |
| 1338 | 1508 | #else |
| 1339 | 1509 | uint32_t full_res; |
| 1340 | 1510 | bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits); |
| 1341 | | *res = (uint16_t)full_res; |
| 1511 | *res = zig_u16_intCast_u32(full_res); |
| 1342 | 1512 | return overflow; |
| 1343 | 1513 | #endif |
| 1344 | 1514 | } |
| ... | ... | @@ -1347,27 +1517,28 @@ static inline bool zig_mulo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t |
| 1347 | 1517 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1348 | 1518 | int16_t full_res; |
| 1349 | 1519 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1350 | | *res = zig_wrap_i16(full_res, bits); |
| 1520 | *res = zig_i16_truncate_i16(full_res, bits); |
| 1351 | 1521 | return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits); |
| 1352 | 1522 | #else |
| 1353 | 1523 | int32_t full_res; |
| 1354 | 1524 | bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits); |
| 1355 | | *res = (int16_t)full_res; |
| 1525 | *res = zig_i16_intCast_i32(full_res); |
| 1356 | 1526 | return overflow; |
| 1357 | 1527 | #endif |
| 1358 | 1528 | } |
| 1359 | 1529 | |
| 1360 | 1530 | #if defined(zig_ez80) |
| 1531 | |
| 1361 | 1532 | static inline bool zig_mulo_u24(uint24_t *res, uint24_t lhs, uint24_t rhs, uint8_t bits) { |
| 1362 | 1533 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1363 | 1534 | uint24_t full_res; |
| 1364 | 1535 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1365 | | *res = zig_wrap_u24(full_res, bits); |
| 1536 | *res = zig_u24_truncate_u24(full_res, bits); |
| 1366 | 1537 | return overflow || full_res < zig_minInt_u(24, bits) || full_res > zig_maxInt_u(24, bits); |
| 1367 | 1538 | #else |
| 1368 | 1539 | uint32_t full_res; |
| 1369 | 1540 | bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits); |
| 1370 | | *res = (uint24_t)full_res; |
| 1541 | *res = zig_u24_intCast_u32(full_res); |
| 1371 | 1542 | return overflow; |
| 1372 | 1543 | #endif |
| 1373 | 1544 | } |
| ... | ... | @@ -1376,28 +1547,26 @@ static inline bool zig_mulo_i24(int24_t *res, int24_t lhs, int24_t rhs, uint8_t |
| 1376 | 1547 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1377 | 1548 | int24_t full_res; |
| 1378 | 1549 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1379 | | *res = zig_wrap_i24(full_res, bits); |
| 1550 | *res = zig_i24_truncate_i24(full_res, bits); |
| 1380 | 1551 | return overflow || full_res < zig_minInt_i(24, bits) || full_res > zig_maxInt_i(24, bits); |
| 1381 | 1552 | #else |
| 1382 | 1553 | int32_t full_res; |
| 1383 | 1554 | bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits); |
| 1384 | | *res = (int24_t)full_res; |
| 1555 | *res = zig_i24_intCast_i32(full_res); |
| 1385 | 1556 | return overflow; |
| 1386 | 1557 | #endif |
| 1387 | 1558 | } |
| 1388 | | #endif |
| 1389 | 1559 | |
| 1390 | | #if defined(zig_ez80) |
| 1391 | 1560 | static inline bool zig_mulo_u48(uint48_t *res, uint48_t lhs, uint48_t rhs, uint8_t bits) { |
| 1392 | 1561 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1393 | 1562 | uint48_t full_res; |
| 1394 | 1563 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1395 | | *res = zig_wrap_u48(full_res, bits); |
| 1564 | *res = zig_u48_truncate_u48(full_res, bits); |
| 1396 | 1565 | return overflow || full_res < zig_minInt_u(48, bits) || full_res > zig_maxInt_u(48, bits); |
| 1397 | 1566 | #else |
| 1398 | 1567 | uint64_t full_res; |
| 1399 | 1568 | bool overflow = zig_mulo_u64(&full_res, lhs, rhs, bits); |
| 1400 | | *res = (uint48_t)full_res; |
| 1569 | *res = zig_u48_intCast_u64(full_res); |
| 1401 | 1570 | return overflow; |
| 1402 | 1571 | #endif |
| 1403 | 1572 | } |
| ... | ... | @@ -1406,18 +1575,32 @@ static inline bool zig_mulo_i48(int48_t *res, int48_t lhs, int48_t rhs, uint8_t |
| 1406 | 1575 | #if zig_has_builtin(mul_overflow) || defined(zig_gcc) |
| 1407 | 1576 | int48_t full_res; |
| 1408 | 1577 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1409 | | *res = zig_wrap_i48(full_res, bits); |
| 1578 | *res = zig_i48_truncate_i48(full_res, bits); |
| 1410 | 1579 | return overflow || full_res < zig_minInt_i(48, bits) || full_res > zig_maxInt_i(48, bits); |
| 1411 | 1580 | #else |
| 1412 | 1581 | int64_t full_res; |
| 1413 | 1582 | bool overflow = zig_mulo_i64(&full_res, lhs, rhs, bits); |
| 1414 | | *res = (int48_t)full_res; |
| 1583 | *res = zig_i48_intCast_i64(full_res); |
| 1415 | 1584 | return overflow; |
| 1416 | 1585 | #endif |
| 1417 | 1586 | } |
| 1587 | |
| 1418 | 1588 | #endif |
| 1419 | 1589 | |
| 1420 | | #define zig_int_builtins(w) \ |
| 1590 | #define zig_shls_builtins(lw, rw) \ |
| 1591 | static inline uint##lw##_t zig_shls_u##lw##_u##rw(uint##lw##_t lhs, uint##rw##_t rhs, uint8_t bits) { \ |
| 1592 | uint##lw##_t res; \ |
| 1593 | if (rhs < bits && !zig_shlo_u##lw(&res, lhs, zig_u8_intCast_u##rw(rhs), bits)) return res; \ |
| 1594 | return lhs == INT##lw##_C(0) ? zig_minInt_u(lw, bits) : zig_maxInt_u(lw, bits); \ |
| 1595 | } \ |
| 1596 | \ |
| 1597 | static inline int##lw##_t zig_shls_i##lw##_u##rw(int##lw##_t lhs, uint##rw##_t rhs, uint8_t bits) { \ |
| 1598 | int##lw##_t res; \ |
| 1599 | if (rhs < bits && !zig_shlo_i##lw(&res, lhs, zig_u8_intCast_u##rw(rhs), bits)) return res; \ |
| 1600 | return lhs == INT##lw##_C(0) ? INT##lw##_C(0) : \ |
| 1601 | lhs < INT##lw##_C(0) ? zig_minInt_i(lw, bits) : zig_maxInt_i(lw, bits); \ |
| 1602 | } |
| 1603 | #define zig_int_sat_builtins(w) \ |
| 1421 | 1604 | static inline bool zig_shlo_u##w(uint##w##_t *res, uint##w##_t lhs, uint8_t rhs, uint8_t bits) { \ |
| 1422 | 1605 | *res = zig_shlw_u##w(lhs, rhs, bits); \ |
| 1423 | 1606 | return lhs > zig_maxInt_u(w, bits) >> rhs; \ |
| ... | ... | @@ -1429,18 +1612,10 @@ static inline bool zig_mulo_i48(int48_t *res, int48_t lhs, int48_t rhs, uint8_t |
| 1429 | 1612 | return (lhs & mask) != INT##w##_C(0) && (lhs & mask) != mask; \ |
| 1430 | 1613 | } \ |
| 1431 | 1614 | \ |
| 1432 | | static inline uint##w##_t zig_shls_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 1433 | | uint##w##_t res; \ |
| 1434 | | if (rhs < bits && !zig_shlo_u##w(&res, lhs, rhs, bits)) return res; \ |
| 1435 | | return lhs == INT##w##_C(0) ? INT##w##_C(0) : zig_maxInt_u(w, bits); \ |
| 1436 | | } \ |
| 1437 | | \ |
| 1438 | | static inline int##w##_t zig_shls_i##w(int##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 1439 | | int##w##_t res; \ |
| 1440 | | if (rhs < bits && !zig_shlo_i##w(&res, lhs, rhs, bits)) return res; \ |
| 1441 | | return lhs == INT##w##_C(0) ? INT##w##_C(0) : \ |
| 1442 | | lhs < INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \ |
| 1443 | | } \ |
| 1615 | zig_shls_builtins(w, 8) \ |
| 1616 | zig_shls_builtins(w, 16) \ |
| 1617 | zig_shls_builtins(w, 32) \ |
| 1618 | zig_shls_builtins(w, 64) \ |
| 1444 | 1619 | \ |
| 1445 | 1620 | static inline uint##w##_t zig_adds_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 1446 | 1621 | uint##w##_t res; \ |
| ... | ... | @@ -1474,332 +1649,321 @@ static inline bool zig_mulo_i48(int48_t *res, int48_t lhs, int48_t rhs, uint8_t |
| 1474 | 1649 | if (!zig_mulo_i##w(&res, lhs, rhs, bits)) return res; \ |
| 1475 | 1650 | return (lhs ^ rhs) < INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \ |
| 1476 | 1651 | } |
| 1477 | | zig_int_builtins(8) |
| 1478 | | zig_int_builtins(16) |
| 1479 | | #if defined(zig_ez80) |
| 1480 | | zig_int_builtins(24) |
| 1481 | | #endif |
| 1482 | | zig_int_builtins(32) |
| 1652 | zig_int_sat_builtins(8) |
| 1653 | zig_int_sat_builtins(16) |
| 1654 | zig_int_sat_builtins(32) |
| 1655 | zig_int_sat_builtins(64) |
| 1483 | 1656 | #if defined(zig_ez80) |
| 1484 | | zig_int_builtins(48) |
| 1657 | zig_int_sat_builtins(24) |
| 1658 | zig_int_sat_builtins(48) |
| 1485 | 1659 | #endif |
| 1486 | | zig_int_builtins(64) |
| 1487 | 1660 | |
| 1488 | | #define zig_builtin8(name, val) __builtin_##name(val) |
| 1661 | #define zig_builtin8(name, arg) __builtin_##name(arg) |
| 1489 | 1662 | typedef unsigned int zig_Builtin8; |
| 1490 | 1663 | |
| 1491 | | #define zig_builtin16(name, val) __builtin_##name(val) |
| 1664 | #define zig_builtin16(name, arg) __builtin_##name(arg) |
| 1492 | 1665 | typedef unsigned int zig_Builtin16; |
| 1493 | 1666 | |
| 1494 | | #if defined(zig_ez80) |
| 1495 | | #define zig_builtin24(name, val) __builtin_##name(val) |
| 1496 | | typedef unsigned int zig_Builtin24; |
| 1497 | | #endif |
| 1498 | | |
| 1499 | 1667 | #if INT_MIN <= INT32_MIN |
| 1500 | | #define zig_builtin32(name, val) __builtin_##name(val) |
| 1668 | #define zig_builtin32(name, arg) __builtin_##name(arg) |
| 1501 | 1669 | typedef unsigned int zig_Builtin32; |
| 1502 | 1670 | #elif LONG_MIN <= INT32_MIN |
| 1503 | | #define zig_builtin32(name, val) __builtin_##name##l(val) |
| 1671 | #define zig_builtin32(name, arg) __builtin_##name##l(arg) |
| 1504 | 1672 | typedef unsigned long zig_Builtin32; |
| 1505 | 1673 | #endif |
| 1506 | 1674 | |
| 1507 | | #if defined(zig_ez80) |
| 1508 | | #define zig_builtin48(name, val) __builtin_##name(val) |
| 1509 | | typedef unsigned long long zig_Builtin48; |
| 1510 | | #endif |
| 1511 | | |
| 1512 | 1675 | #if INT_MIN <= INT64_MIN |
| 1513 | | #define zig_builtin64(name, val) __builtin_##name(val) |
| 1676 | #define zig_builtin64(name, arg) __builtin_##name(arg) |
| 1514 | 1677 | typedef unsigned int zig_Builtin64; |
| 1515 | 1678 | #elif LONG_MIN <= INT64_MIN |
| 1516 | | #define zig_builtin64(name, val) __builtin_##name##l(val) |
| 1679 | #define zig_builtin64(name, arg) __builtin_##name##l(arg) |
| 1517 | 1680 | typedef unsigned long zig_Builtin64; |
| 1518 | 1681 | #elif LLONG_MIN <= INT64_MIN |
| 1519 | | #define zig_builtin64(name, val) __builtin_##name##ll(val) |
| 1682 | #define zig_builtin64(name, arg) __builtin_##name##ll(arg) |
| 1520 | 1683 | typedef unsigned long long zig_Builtin64; |
| 1521 | 1684 | #endif |
| 1522 | 1685 | |
| 1523 | | static inline uint8_t zig_byte_swap_u8(uint8_t val, uint8_t bits) { |
| 1524 | | return zig_wrap_u8(val >> (8 - bits), bits); |
| 1686 | #if defined(zig_ez80) |
| 1687 | #define zig_builtin24(name, arg) __builtin_##name(arg) |
| 1688 | typedef unsigned int zig_Builtin24; |
| 1689 | #define zig_builtin48(name, arg) __builtin_##name(arg) |
| 1690 | typedef unsigned long long zig_Builtin48; |
| 1691 | #endif |
| 1692 | |
| 1693 | static inline uint8_t zig_byteSwap_u8(uint8_t arg, uint8_t bits) { |
| 1694 | return zig_u8_truncate_u8(arg >> (8 - bits), bits); |
| 1525 | 1695 | } |
| 1526 | 1696 | |
| 1527 | | static inline int8_t zig_byte_swap_i8(int8_t val, uint8_t bits) { |
| 1528 | | return zig_wrap_i8((int8_t)zig_byte_swap_u8((uint8_t)val, bits), bits); |
| 1697 | static inline int8_t zig_byteSwap_i8(int8_t arg, uint8_t bits) { |
| 1698 | return zig_i8_truncate_i8((int8_t)zig_byteSwap_u8((uint8_t)arg, bits), bits); |
| 1529 | 1699 | } |
| 1530 | 1700 | |
| 1531 | | static inline uint16_t zig_byte_swap_u16(uint16_t val, uint8_t bits) { |
| 1701 | static inline uint16_t zig_byteSwap_u16(uint16_t arg, uint8_t bits) { |
| 1532 | 1702 | uint16_t full_res; |
| 1533 | 1703 | #if zig_has_builtin(bswap16) || defined(zig_gcc) |
| 1534 | | full_res = __builtin_bswap16(val); |
| 1704 | full_res = __builtin_bswap16(arg); |
| 1535 | 1705 | #else |
| 1536 | | full_res = (uint16_t)zig_byte_swap_u8((uint8_t)(val >> 0), 8) << 8 | |
| 1537 | | (uint16_t)zig_byte_swap_u8((uint8_t)(val >> 8), 8) >> 0; |
| 1706 | full_res = (uint16_t)zig_byteSwap_u8((uint8_t)(arg >> 0), 8) << 8 | |
| 1707 | (uint16_t)zig_byteSwap_u8((uint8_t)(arg >> 8), 8) >> 0; |
| 1538 | 1708 | #endif |
| 1539 | | return zig_wrap_u16(full_res >> (16 - bits), bits); |
| 1709 | return zig_u16_truncate_u16(full_res >> (16 - bits), bits); |
| 1540 | 1710 | } |
| 1541 | 1711 | |
| 1542 | | static inline int16_t zig_byte_swap_i16(int16_t val, uint8_t bits) { |
| 1543 | | return zig_wrap_i16((int16_t)zig_byte_swap_u16((uint16_t)val, bits), bits); |
| 1712 | static inline int16_t zig_byteSwap_i16(int16_t arg, uint8_t bits) { |
| 1713 | return zig_i16_truncate_i16((int16_t)zig_byteSwap_u16((uint16_t)arg, bits), bits); |
| 1544 | 1714 | } |
| 1545 | 1715 | |
| 1546 | 1716 | #if defined(zig_ez80) |
| 1547 | | static inline uint16_t zig_byte_swap_u24(uint24_t val, uint8_t bits) { |
| 1717 | static inline uint16_t zig_byteSwap_u24(uint24_t arg, uint8_t bits) { |
| 1548 | 1718 | uint24_t full_res; |
| 1549 | 1719 | #if zig_has_builtin(bswap24) || defined(zig_gcc) |
| 1550 | | full_res = __builtin_bswap24(val); |
| 1720 | full_res = __builtin_bswap24(arg); |
| 1551 | 1721 | #else |
| 1552 | | full_res = (uint24_t)zig_byte_swap_u8((uint8_t)(val >> 0), 8) << 16 | |
| 1553 | | (uint24_t)zig_byte_swap_u16((uint16_t)(val >> 8), 16) >> 0; |
| 1722 | full_res = (uint24_t)zig_byteSwap_u8((uint8_t)(arg >> 0), 8) << 16 | |
| 1723 | (uint24_t)zig_byteSwap_u16((uint16_t)(arg >> 8), 16) >> 0; |
| 1554 | 1724 | #endif |
| 1555 | | return zig_wrap_u24(full_res >> (24 - bits), bits); |
| 1725 | return zig_u24_truncate_u24(full_res >> (24 - bits), bits); |
| 1556 | 1726 | } |
| 1557 | 1727 | |
| 1558 | | static inline int16_t zig_byte_swap_i24(int24_t val, uint8_t bits) { |
| 1559 | | return zig_wrap_i24((int24_t)zig_byte_swap_u24((uint24_t)val, bits), bits); |
| 1728 | static inline int16_t zig_byteSwap_i24(int24_t arg, uint8_t bits) { |
| 1729 | return zig_i24_truncate_i24((int24_t)zig_byteSwap_u24((uint24_t)arg, bits), bits); |
| 1560 | 1730 | } |
| 1561 | 1731 | #endif |
| 1562 | 1732 | |
| 1563 | | static inline uint32_t zig_byte_swap_u32(uint32_t val, uint8_t bits) { |
| 1733 | static inline uint32_t zig_byteSwap_u32(uint32_t arg, uint8_t bits) { |
| 1564 | 1734 | uint32_t full_res; |
| 1565 | 1735 | #if zig_has_builtin(bswap32) || defined(zig_gcc) |
| 1566 | | full_res = __builtin_bswap32(val); |
| 1736 | full_res = __builtin_bswap32(arg); |
| 1567 | 1737 | #else |
| 1568 | | full_res = (uint32_t)zig_byte_swap_u16((uint16_t)(val >> 0), 16) << 16 | |
| 1569 | | (uint32_t)zig_byte_swap_u16((uint16_t)(val >> 16), 16) >> 0; |
| 1738 | full_res = (uint32_t)zig_byteSwap_u16((uint16_t)(arg >> 0), 16) << 16 | |
| 1739 | (uint32_t)zig_byteSwap_u16((uint16_t)(arg >> 16), 16) >> 0; |
| 1570 | 1740 | #endif |
| 1571 | | return zig_wrap_u32(full_res >> (32 - bits), bits); |
| 1741 | return zig_u32_truncate_u32(full_res >> (32 - bits), bits); |
| 1572 | 1742 | } |
| 1573 | 1743 | |
| 1574 | | static inline int32_t zig_byte_swap_i32(int32_t val, uint8_t bits) { |
| 1575 | | return zig_wrap_i32((int32_t)zig_byte_swap_u32((uint32_t)val, bits), bits); |
| 1744 | static inline int32_t zig_byteSwap_i32(int32_t arg, uint8_t bits) { |
| 1745 | return zig_i32_truncate_i32((int32_t)zig_byteSwap_u32((uint32_t)arg, bits), bits); |
| 1576 | 1746 | } |
| 1577 | 1747 | |
| 1578 | 1748 | #if defined(zig_ez80) |
| 1579 | | static inline uint32_t zig_byte_swap_u48(uint48_t val, uint8_t bits) { |
| 1749 | static inline uint32_t zig_byteSwap_u48(uint48_t arg, uint8_t bits) { |
| 1580 | 1750 | uint48_t full_res; |
| 1581 | 1751 | #if zig_has_builtin(bswap48) || defined(zig_gcc) |
| 1582 | | full_res = __builtin_bswap48(val); |
| 1752 | full_res = __builtin_bswap48(arg); |
| 1583 | 1753 | #else |
| 1584 | | full_res = (uint48_t)zig_byte_swap_u24((uint24_t)(val >> 0), 24) << 24 | |
| 1585 | | (uint48_t)zig_byte_swap_u24((uint24_t)(val >> 24), 24) >> 0; |
| 1754 | full_res = (uint48_t)zig_byteSwap_u24((uint24_t)(arg >> 0), 24) << 24 | |
| 1755 | (uint48_t)zig_byteSwap_u24((uint24_t)(arg >> 24), 24) >> 0; |
| 1586 | 1756 | #endif |
| 1587 | | return zig_wrap_u48(full_res >> (48 - bits), bits); |
| 1757 | return zig_u48_truncate_u48(full_res >> (48 - bits), bits); |
| 1588 | 1758 | } |
| 1589 | 1759 | |
| 1590 | | static inline int32_t zig_byte_swap_i48(int48_t val, uint8_t bits) { |
| 1591 | | return zig_wrap_i48((int48_t)zig_byte_swap_u48((uint48_t)val, bits), bits); |
| 1760 | static inline int32_t zig_byteSwap_i48(int48_t arg, uint8_t bits) { |
| 1761 | return zig_i48_truncate_i48((int48_t)zig_byteSwap_u48((uint48_t)arg, bits), bits); |
| 1592 | 1762 | } |
| 1593 | 1763 | #endif |
| 1594 | 1764 | |
| 1595 | | static inline uint64_t zig_byte_swap_u64(uint64_t val, uint8_t bits) { |
| 1765 | static inline uint64_t zig_byteSwap_u64(uint64_t arg, uint8_t bits) { |
| 1596 | 1766 | uint64_t full_res; |
| 1597 | 1767 | #if zig_has_builtin(bswap64) || defined(zig_gcc) |
| 1598 | | full_res = __builtin_bswap64(val); |
| 1768 | full_res = __builtin_bswap64(arg); |
| 1599 | 1769 | #else |
| 1600 | | full_res = (uint64_t)zig_byte_swap_u32((uint32_t)(val >> 0), 32) << 32 | |
| 1601 | | (uint64_t)zig_byte_swap_u32((uint32_t)(val >> 32), 32) >> 0; |
| 1770 | full_res = (uint64_t)zig_byteSwap_u32((uint32_t)(arg >> 0), 32) << 32 | |
| 1771 | (uint64_t)zig_byteSwap_u32((uint32_t)(arg >> 32), 32) >> 0; |
| 1602 | 1772 | #endif |
| 1603 | | return zig_wrap_u64(full_res >> (64 - bits), bits); |
| 1773 | return zig_u64_truncate_u64(full_res >> (64 - bits), bits); |
| 1604 | 1774 | } |
| 1605 | 1775 | |
| 1606 | | static inline int64_t zig_byte_swap_i64(int64_t val, uint8_t bits) { |
| 1607 | | return zig_wrap_i64((int64_t)zig_byte_swap_u64((uint64_t)val, bits), bits); |
| 1776 | static inline int64_t zig_byteSwap_i64(int64_t arg, uint8_t bits) { |
| 1777 | return zig_i64_truncate_i64((int64_t)zig_byteSwap_u64((uint64_t)arg, bits), bits); |
| 1608 | 1778 | } |
| 1609 | 1779 | |
| 1610 | | static inline uint8_t zig_bit_reverse_u8(uint8_t val, uint8_t bits) { |
| 1780 | static inline uint8_t zig_bitReverse_u8(uint8_t arg, uint8_t bits) { |
| 1611 | 1781 | uint8_t full_res; |
| 1612 | 1782 | #if zig_has_builtin(bitreverse8) |
| 1613 | | full_res = __builtin_bitreverse8(val); |
| 1783 | full_res = __builtin_bitreverse8(arg); |
| 1614 | 1784 | #else |
| 1615 | 1785 | static uint8_t const lut[0x10] = { |
| 1616 | 1786 | 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, |
| 1617 | 1787 | 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf |
| 1618 | 1788 | }; |
| 1619 | | full_res = lut[val >> 0 & 0xF] << 4 | lut[val >> 4 & 0xF] << 0; |
| 1789 | full_res = lut[arg >> 0 & 0xF] << 4 | lut[arg >> 4 & 0xF] << 0; |
| 1620 | 1790 | #endif |
| 1621 | | return zig_wrap_u8(full_res >> (8 - bits), bits); |
| 1791 | return zig_u8_truncate_u8(full_res >> (8 - bits), bits); |
| 1622 | 1792 | } |
| 1623 | 1793 | |
| 1624 | | static inline int8_t zig_bit_reverse_i8(int8_t val, uint8_t bits) { |
| 1625 | | return zig_wrap_i8((int8_t)zig_bit_reverse_u8((uint8_t)val, bits), bits); |
| 1794 | static inline int8_t zig_bitReverse_i8(int8_t arg, uint8_t bits) { |
| 1795 | return zig_i8_truncate_i8((int8_t)zig_bitReverse_u8((uint8_t)arg, bits), bits); |
| 1626 | 1796 | } |
| 1627 | 1797 | |
| 1628 | | static inline uint16_t zig_bit_reverse_u16(uint16_t val, uint8_t bits) { |
| 1798 | static inline uint16_t zig_bitReverse_u16(uint16_t arg, uint8_t bits) { |
| 1629 | 1799 | uint16_t full_res; |
| 1630 | 1800 | #if zig_has_builtin(bitreverse16) |
| 1631 | | full_res = __builtin_bitreverse16(val); |
| 1801 | full_res = __builtin_bitreverse16(arg); |
| 1632 | 1802 | #else |
| 1633 | | full_res = (uint16_t)zig_bit_reverse_u8((uint8_t)(val >> 0), 8) << 8 | |
| 1634 | | (uint16_t)zig_bit_reverse_u8((uint8_t)(val >> 8), 8) >> 0; |
| 1803 | full_res = (uint16_t)zig_bitReverse_u8((uint8_t)(arg >> 0), 8) << 8 | |
| 1804 | (uint16_t)zig_bitReverse_u8((uint8_t)(arg >> 8), 8) >> 0; |
| 1635 | 1805 | #endif |
| 1636 | | return zig_wrap_u16(full_res >> (16 - bits), bits); |
| 1806 | return zig_u16_truncate_u16(full_res >> (16 - bits), bits); |
| 1637 | 1807 | } |
| 1638 | 1808 | |
| 1639 | | static inline int16_t zig_bit_reverse_i16(int16_t val, uint8_t bits) { |
| 1640 | | return zig_wrap_i16((int16_t)zig_bit_reverse_u16((uint16_t)val, bits), bits); |
| 1809 | static inline int16_t zig_bitReverse_i16(int16_t arg, uint8_t bits) { |
| 1810 | return zig_i16_truncate_i16((int16_t)zig_bitReverse_u16((uint16_t)arg, bits), bits); |
| 1641 | 1811 | } |
| 1642 | 1812 | |
| 1643 | 1813 | #if defined(zig_ez80) |
| 1644 | | static inline uint24_t zig_bit_reverse_u24(uint24_t val, uint8_t bits) { |
| 1814 | static inline uint24_t zig_bitReverse_u24(uint24_t arg, uint8_t bits) { |
| 1645 | 1815 | uint24_t full_res; |
| 1646 | 1816 | #if zig_has_builtin(bitreverse24) |
| 1647 | | full_res = __builtin_bitreverse24(val); |
| 1817 | full_res = __builtin_bitreverse24(arg); |
| 1648 | 1818 | #else |
| 1649 | | full_res = (uint24_t)zig_bit_reverse_u8((uint8_t)(val >> 0), 8) << 16 | |
| 1650 | | (uint24_t)zig_bit_reverse_u16((uint16_t)(val >> 8), 16) >> 0; |
| 1819 | full_res = (uint24_t)zig_bitReverse_u8((uint8_t)(arg >> 0), 8) << 16 | |
| 1820 | (uint24_t)zig_bitReverse_u16((uint16_t)(arg >> 8), 16) >> 0; |
| 1651 | 1821 | #endif |
| 1652 | | return zig_wrap_u24(full_res >> (24 - bits), bits); |
| 1822 | return zig_u24_truncate_u24(full_res >> (24 - bits), bits); |
| 1653 | 1823 | } |
| 1654 | 1824 | |
| 1655 | | static inline int24_t zig_bit_reverse_i24(int24_t val, uint8_t bits) { |
| 1656 | | return zig_wrap_i24((int24_t)zig_bit_reverse_u24((uint24_t)val, bits), bits); |
| 1825 | static inline int24_t zig_bitReverse_i24(int24_t arg, uint8_t bits) { |
| 1826 | return zig_i24_truncate_i24((int24_t)zig_bitReverse_u24((uint24_t)arg, bits), bits); |
| 1657 | 1827 | } |
| 1658 | 1828 | #endif |
| 1659 | 1829 | |
| 1660 | | static inline uint32_t zig_bit_reverse_u32(uint32_t val, uint8_t bits) { |
| 1830 | static inline uint32_t zig_bitReverse_u32(uint32_t arg, uint8_t bits) { |
| 1661 | 1831 | uint32_t full_res; |
| 1662 | 1832 | #if zig_has_builtin(bitreverse32) |
| 1663 | | full_res = __builtin_bitreverse32(val); |
| 1833 | full_res = __builtin_bitreverse32(arg); |
| 1664 | 1834 | #else |
| 1665 | | full_res = (uint32_t)zig_bit_reverse_u16((uint16_t)(val >> 0), 16) << 16 | |
| 1666 | | (uint32_t)zig_bit_reverse_u16((uint16_t)(val >> 16), 16) >> 0; |
| 1835 | full_res = (uint32_t)zig_bitReverse_u16((uint16_t)(arg >> 0), 16) << 16 | |
| 1836 | (uint32_t)zig_bitReverse_u16((uint16_t)(arg >> 16), 16) >> 0; |
| 1667 | 1837 | #endif |
| 1668 | | return zig_wrap_u32(full_res >> (32 - bits), bits); |
| 1838 | return zig_u32_truncate_u32(full_res >> (32 - bits), bits); |
| 1669 | 1839 | } |
| 1670 | 1840 | |
| 1671 | | static inline int32_t zig_bit_reverse_i32(int32_t val, uint8_t bits) { |
| 1672 | | return zig_wrap_i32((int32_t)zig_bit_reverse_u32((uint32_t)val, bits), bits); |
| 1841 | static inline int32_t zig_bitReverse_i32(int32_t arg, uint8_t bits) { |
| 1842 | return zig_i32_truncate_i32((int32_t)zig_bitReverse_u32((uint32_t)arg, bits), bits); |
| 1673 | 1843 | } |
| 1674 | 1844 | |
| 1675 | 1845 | #if defined(zig_ez80) |
| 1676 | | static inline uint32_t zig_bit_reverse_u48(uint48_t val, uint8_t bits) { |
| 1846 | static inline uint32_t zig_bitReverse_u48(uint48_t arg, uint8_t bits) { |
| 1677 | 1847 | uint48_t full_res; |
| 1678 | 1848 | #if zig_has_builtin(bitreverse48) |
| 1679 | | full_res = __builtin_bitreverse48(val); |
| 1849 | full_res = __builtin_bitreverse48(arg); |
| 1680 | 1850 | #else |
| 1681 | | full_res = (uint48_t)zig_bit_reverse_u24((uint24_t)(val >> 0), 24) << 24 | |
| 1682 | | (uint48_t)zig_bit_reverse_u24((uint24_t)(val >> 24), 24) >> 0; |
| 1851 | full_res = (uint48_t)zig_bitReverse_u24((uint24_t)(arg >> 0), 24) << 24 | |
| 1852 | (uint48_t)zig_bitReverse_u24((uint24_t)(arg >> 24), 24) >> 0; |
| 1683 | 1853 | #endif |
| 1684 | | return zig_wrap_u32(full_res >> (48 - bits), bits); |
| 1854 | return zig_u48_truncate_u48(full_res >> (48 - bits), bits); |
| 1685 | 1855 | } |
| 1686 | 1856 | |
| 1687 | | static inline int32_t zig_bit_reverse_i48(int48_t val, uint8_t bits) { |
| 1688 | | return zig_wrap_i48((int48_t)zig_bit_reverse_u48((uint48_t)val, bits), bits); |
| 1857 | static inline int32_t zig_bitReverse_i48(int48_t arg, uint8_t bits) { |
| 1858 | return zig_i48_truncate_i48((int48_t)zig_bitReverse_u48((uint48_t)arg, bits), bits); |
| 1689 | 1859 | } |
| 1690 | 1860 | #endif |
| 1691 | 1861 | |
| 1692 | | static inline uint64_t zig_bit_reverse_u64(uint64_t val, uint8_t bits) { |
| 1862 | static inline uint64_t zig_bitReverse_u64(uint64_t arg, uint8_t bits) { |
| 1693 | 1863 | uint64_t full_res; |
| 1694 | 1864 | #if zig_has_builtin(bitreverse64) |
| 1695 | | full_res = __builtin_bitreverse64(val); |
| 1865 | full_res = __builtin_bitreverse64(arg); |
| 1696 | 1866 | #else |
| 1697 | | full_res = (uint64_t)zig_bit_reverse_u32((uint32_t)(val >> 0), 32) << 32 | |
| 1698 | | (uint64_t)zig_bit_reverse_u32((uint32_t)(val >> 32), 32) >> 0; |
| 1867 | full_res = (uint64_t)zig_bitReverse_u32((uint32_t)(arg >> 0), 32) << 32 | |
| 1868 | (uint64_t)zig_bitReverse_u32((uint32_t)(arg >> 32), 32) >> 0; |
| 1699 | 1869 | #endif |
| 1700 | | return zig_wrap_u64(full_res >> (64 - bits), bits); |
| 1870 | return zig_u64_truncate_u64(full_res >> (64 - bits), bits); |
| 1701 | 1871 | } |
| 1702 | 1872 | |
| 1703 | | static inline int64_t zig_bit_reverse_i64(int64_t val, uint8_t bits) { |
| 1704 | | return zig_wrap_i64((int64_t)zig_bit_reverse_u64((uint64_t)val, bits), bits); |
| 1873 | static inline int64_t zig_bitReverse_i64(int64_t arg, uint8_t bits) { |
| 1874 | return zig_i64_truncate_i64((int64_t)zig_bitReverse_u64((uint64_t)arg, bits), bits); |
| 1705 | 1875 | } |
| 1706 | 1876 | |
| 1707 | | #define zig_builtin_popcount_common(w) \ |
| 1708 | | static inline uint8_t zig_popcount_i##w(int##w##_t val, uint8_t bits) { \ |
| 1709 | | return zig_popcount_u##w((uint##w##_t)val, bits); \ |
| 1877 | #define zig_builtin_popCount_common(w) \ |
| 1878 | static inline uint8_t zig_popCount_i##w(int##w##_t arg, uint8_t bits) { \ |
| 1879 | return zig_popCount_u##w((uint##w##_t)arg, bits); \ |
| 1710 | 1880 | } |
| 1711 | | #if zig_has_builtin(popcount) || defined(zig_gcc) || defined(zig_tinyc) |
| 1712 | | #define zig_builtin_popcount(w) \ |
| 1713 | | static inline uint8_t zig_popcount_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1881 | #if zig_has_builtin(popCount) || defined(zig_gcc) || defined(zig_tinyc) |
| 1882 | #define zig_builtin_popCount(w) \ |
| 1883 | static inline uint8_t zig_popCount_u##w(uint##w##_t arg, uint8_t bits) { \ |
| 1714 | 1884 | (void)bits; \ |
| 1715 | | return zig_builtin##w(popcount, val); \ |
| 1885 | return zig_builtin##w(popcount, arg); \ |
| 1716 | 1886 | } \ |
| 1717 | 1887 | \ |
| 1718 | | zig_builtin_popcount_common(w) |
| 1888 | zig_builtin_popCount_common(w) |
| 1719 | 1889 | #else |
| 1720 | | #define zig_builtin_popcount(w) \ |
| 1721 | | static inline uint8_t zig_popcount_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1890 | #define zig_builtin_popCount(w) \ |
| 1891 | static inline uint8_t zig_popCount_u##w(uint##w##_t arg, uint8_t bits) { \ |
| 1722 | 1892 | (void)bits; \ |
| 1723 | | uint##w##_t temp = val - ((val >> 1) & (UINT##w##_MAX / 3)); \ |
| 1893 | uint##w##_t temp = arg - ((arg >> 1) & (UINT##w##_MAX / 3)); \ |
| 1724 | 1894 | temp = (temp & (UINT##w##_MAX / 5)) + ((temp >> 2) & (UINT##w##_MAX / 5)); \ |
| 1725 | 1895 | temp = (temp + (temp >> 4)) & (UINT##w##_MAX / 17); \ |
| 1726 | 1896 | return temp * (UINT##w##_MAX / 255) >> (UINT8_C(w) - UINT8_C(8)); \ |
| 1727 | 1897 | } \ |
| 1728 | 1898 | \ |
| 1729 | | zig_builtin_popcount_common(w) |
| 1730 | | #endif |
| 1731 | | zig_builtin_popcount(8) |
| 1732 | | zig_builtin_popcount(16) |
| 1733 | | #if defined(zig_ez80) |
| 1734 | | zig_builtin_popcount(24) |
| 1899 | zig_builtin_popCount_common(w) |
| 1735 | 1900 | #endif |
| 1736 | | zig_builtin_popcount(32) |
| 1901 | zig_builtin_popCount(8) |
| 1902 | zig_builtin_popCount(16) |
| 1903 | zig_builtin_popCount(32) |
| 1904 | zig_builtin_popCount(64) |
| 1737 | 1905 | #if defined(zig_ez80) |
| 1738 | | zig_builtin_popcount(48) |
| 1906 | zig_builtin_popCount(24) |
| 1907 | zig_builtin_popCount(48) |
| 1739 | 1908 | #endif |
| 1740 | | zig_builtin_popcount(64) |
| 1741 | 1909 | |
| 1742 | 1910 | #define zig_builtin_ctz_common(w) \ |
| 1743 | | static inline uint8_t zig_ctz_i##w(int##w##_t val, uint8_t bits) { \ |
| 1744 | | return zig_ctz_u##w((uint##w##_t)val, bits); \ |
| 1911 | static inline uint8_t zig_ctz_i##w(int##w##_t arg, uint8_t bits) { \ |
| 1912 | return zig_ctz_u##w((uint##w##_t)arg, bits); \ |
| 1745 | 1913 | } |
| 1746 | 1914 | #if zig_has_builtin(ctz) || defined(zig_gcc) || defined(zig_tinyc) |
| 1747 | 1915 | #define zig_builtin_ctz(w) \ |
| 1748 | | static inline uint8_t zig_ctz_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1749 | | if (val == 0) return bits; \ |
| 1750 | | return zig_builtin##w(ctz, val); \ |
| 1916 | static inline uint8_t zig_ctz_u##w(uint##w##_t arg, uint8_t bits) { \ |
| 1917 | if (arg == 0) return bits; \ |
| 1918 | return zig_builtin##w(ctz, arg); \ |
| 1751 | 1919 | } \ |
| 1752 | 1920 | \ |
| 1753 | 1921 | zig_builtin_ctz_common(w) |
| 1754 | 1922 | #else |
| 1755 | 1923 | #define zig_builtin_ctz(w) \ |
| 1756 | | static inline uint8_t zig_ctz_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1757 | | return zig_popcount_u##w(zig_not_u##w(val, bits) & zig_subw_u##w(val, 1, bits), bits); \ |
| 1924 | static inline uint8_t zig_ctz_u##w(uint##w##_t arg, uint8_t bits) { \ |
| 1925 | return zig_popCount_u##w(zig_not_u##w(arg, bits) & zig_subw_u##w(arg, 1, bits), bits); \ |
| 1758 | 1926 | } \ |
| 1759 | 1927 | \ |
| 1760 | 1928 | zig_builtin_ctz_common(w) |
| 1761 | 1929 | #endif |
| 1762 | 1930 | zig_builtin_ctz(8) |
| 1763 | 1931 | zig_builtin_ctz(16) |
| 1764 | | #if defined(zig_ez80) |
| 1765 | | zig_builtin_ctz(24) |
| 1766 | | #endif |
| 1767 | 1932 | zig_builtin_ctz(32) |
| 1933 | zig_builtin_ctz(64) |
| 1768 | 1934 | #if defined(zig_ez80) |
| 1935 | zig_builtin_ctz(24) |
| 1769 | 1936 | zig_builtin_ctz(48) |
| 1770 | 1937 | #endif |
| 1771 | | zig_builtin_ctz(64) |
| 1772 | 1938 | |
| 1773 | 1939 | #define zig_builtin_clz_common(w) \ |
| 1774 | | static inline uint8_t zig_clz_i##w(int##w##_t val, uint8_t bits) { \ |
| 1775 | | return zig_clz_u##w((uint##w##_t)val, bits); \ |
| 1940 | static inline uint8_t zig_clz_i##w(int##w##_t arg, uint8_t bits) { \ |
| 1941 | return zig_clz_u##w((uint##w##_t)arg, bits); \ |
| 1776 | 1942 | } |
| 1777 | 1943 | #if zig_has_builtin(clz) || defined(zig_gcc) || defined(zig_tinyc) |
| 1778 | 1944 | #define zig_builtin_clz(w) \ |
| 1779 | | static inline uint8_t zig_clz_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1780 | | if (val == 0) return bits; \ |
| 1781 | | return zig_builtin##w(clz, val) - (zig_bitSizeOf(zig_Builtin##w) - bits); \ |
| 1945 | static inline uint8_t zig_clz_u##w(uint##w##_t arg, uint8_t bits) { \ |
| 1946 | if (arg == 0) return bits; \ |
| 1947 | return zig_builtin##w(clz, arg) - (zig_bitSizeOf(zig_Builtin##w) - bits); \ |
| 1782 | 1948 | } \ |
| 1783 | 1949 | \ |
| 1784 | 1950 | zig_builtin_clz_common(w) |
| 1785 | 1951 | #else |
| 1786 | 1952 | #define zig_builtin_clz(w) \ |
| 1787 | | static inline uint8_t zig_clz_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1788 | | return zig_ctz_u##w(zig_bit_reverse_u##w(val, bits), bits); \ |
| 1953 | static inline uint8_t zig_clz_u##w(uint##w##_t arg, uint8_t bits) { \ |
| 1954 | return zig_ctz_u##w(zig_bitReverse_u##w(arg, bits), bits); \ |
| 1789 | 1955 | } \ |
| 1790 | 1956 | \ |
| 1791 | 1957 | zig_builtin_clz_common(w) |
| 1792 | 1958 | #endif |
| 1793 | 1959 | zig_builtin_clz(8) |
| 1794 | 1960 | zig_builtin_clz(16) |
| 1795 | | #if defined(zig_ez80) |
| 1796 | | zig_builtin_clz(24) |
| 1797 | | #endif |
| 1798 | 1961 | zig_builtin_clz(32) |
| 1962 | zig_builtin_clz(64) |
| 1799 | 1963 | #if defined(zig_ez80) |
| 1964 | zig_builtin_clz(24) |
| 1800 | 1965 | zig_builtin_clz(48) |
| 1801 | 1966 | #endif |
| 1802 | | zig_builtin_clz(64) |
| 1803 | 1967 | |
| 1804 | 1968 | /* ======================== 128-bit Integer Support ========================= */ |
| 1805 | 1969 | |
| ... | ... | @@ -1816,16 +1980,14 @@ zig_builtin_clz(64) |
| 1816 | 1980 | typedef unsigned __int128 zig_u128; |
| 1817 | 1981 | typedef signed __int128 zig_i128; |
| 1818 | 1982 | |
| 1819 | | #define zig_make_u128(hi, lo) ((zig_u128)(hi)<<64|(lo)) |
| 1820 | | #define zig_make_i128(hi, lo) ((zig_i128)zig_make_u128(hi, lo)) |
| 1821 | | #define zig_init_u128(hi, lo) zig_make_u128(hi, lo) |
| 1822 | | #define zig_init_i128(hi, lo) zig_make_i128(hi, lo) |
| 1823 | | #define zig_hi_u128(val) ((uint64_t)((val) >> 64)) |
| 1824 | | #define zig_lo_u128(val) ((uint64_t)((val) >> 0)) |
| 1825 | | #define zig_hi_i128(val) (( int64_t)((val) >> 64)) |
| 1826 | | #define zig_lo_i128(val) ((uint64_t)((val) >> 0)) |
| 1827 | | #define zig_bitCast_u128(val) ((zig_u128)(val)) |
| 1828 | | #define zig_bitCast_i128(val) ((zig_i128)(val)) |
| 1983 | #define zig_init_u128(hi, lo) ((zig_u128)(hi)<<64|(lo)) |
| 1984 | #define zig_init_i128(hi, lo) ((zig_i128)zig_make_u128(hi, lo)) |
| 1985 | #define zig_make_u128(hi, lo) zig_init_u128(hi, lo) |
| 1986 | #define zig_make_i128(hi, lo) zig_init_i128(hi, lo) |
| 1987 | #define zig_hi_u128(arg) ((uint64_t)((arg) >> 64)) |
| 1988 | #define zig_lo_u128(arg) ((uint64_t)((arg) >> 0)) |
| 1989 | #define zig_hi_i128(arg) (( int64_t)((arg) >> 64)) |
| 1990 | #define zig_lo_i128(arg) ((uint64_t)((arg) >> 0)) |
| 1829 | 1991 | #define zig_cmp_int128(Type) \ |
| 1830 | 1992 | static inline int32_t zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 1831 | 1993 | return (lhs > rhs) - (lhs < rhs); \ |
| ... | ... | @@ -1835,32 +1997,49 @@ typedef signed __int128 zig_i128; |
| 1835 | 1997 | return lhs operator rhs; \ |
| 1836 | 1998 | } |
| 1837 | 1999 | |
| 1838 | | #else /* zig_has_int128 */ |
| 2000 | static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) { |
| 2001 | return lhs << rhs; |
| 2002 | } |
| 1839 | 2003 | |
| 1840 | | #if zig_little_endian |
| 1841 | | typedef struct { zig_align(16) uint64_t lo; uint64_t hi; } zig_u128; |
| 1842 | | typedef struct { zig_align(16) uint64_t lo; int64_t hi; } zig_i128; |
| 2004 | static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) { |
| 2005 | return lhs >> rhs; |
| 2006 | } |
| 2007 | |
| 2008 | static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) { |
| 2009 | return lhs << rhs; |
| 2010 | } |
| 2011 | |
| 2012 | static inline zig_i128 zig_shr_i128(zig_i128 lhs, uint8_t rhs) { |
| 2013 | // This works around a GCC miscompilation, but it has the side benefit of |
| 2014 | // emitting better code. It is behind the `#if` because it depends on |
| 2015 | // arithmetic right shift, which is implementation-defined in C, but should |
| 2016 | // be guaranteed on any GCC-compatible compiler. |
| 2017 | #if defined(zig_gnuc) |
| 2018 | return lhs >> rhs; |
| 1843 | 2019 | #else |
| 1844 | | typedef struct { zig_align(16) uint64_t hi; uint64_t lo; } zig_u128; |
| 1845 | | typedef struct { zig_align(16) int64_t hi; uint64_t lo; } zig_i128; |
| 2020 | zig_i128 sign_mask = lhs < zig_make_i128(0, 0) ? -zig_make_i128(0, 1) : zig_make_i128(0, 0); |
| 2021 | return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; |
| 1846 | 2022 | #endif |
| 2023 | } |
| 1847 | 2024 | |
| 1848 | | #define zig_make_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) }) |
| 1849 | | #define zig_make_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) }) |
| 2025 | #else /* zig_has_int128 */ |
| 1850 | 2026 | |
| 1851 | | #if defined(zig_msvc) /* MSVC doesn't allow struct literals in constant expressions */ |
| 1852 | | #define zig_init_u128(hi, lo) { .h##i = (hi), .l##o = (lo) } |
| 1853 | | #define zig_init_i128(hi, lo) { .h##i = (hi), .l##o = (lo) } |
| 1854 | | #else /* But non-MSVC doesn't like the unprotected commas */ |
| 1855 | | #define zig_init_u128(hi, lo) zig_make_u128(hi, lo) |
| 1856 | | #define zig_init_i128(hi, lo) zig_make_i128(hi, lo) |
| 1857 | | #endif |
| 1858 | | #define zig_hi_u128(val) ((val).hi) |
| 1859 | | #define zig_lo_u128(val) ((val).lo) |
| 1860 | | #define zig_hi_i128(val) ((val).hi) |
| 1861 | | #define zig_lo_i128(val) ((val).lo) |
| 1862 | | #define zig_bitCast_u128(val) zig_make_u128((uint64_t)(val).hi, (val).lo) |
| 1863 | | #define zig_bitCast_i128(val) zig_make_i128(( int64_t)(val).hi, (val).lo) |
| 2027 | #if zig_little_endian |
| 2028 | typedef struct { zig_align(ZIG_TARGET_MAX_INT_ALIGNMENT) uint64_t lo; uint64_t hi; } zig_u128; |
| 2029 | typedef struct { zig_align(ZIG_TARGET_MAX_INT_ALIGNMENT) uint64_t lo; int64_t hi; } zig_i128; |
| 2030 | #else |
| 2031 | typedef struct { zig_align(ZIG_TARGET_MAX_INT_ALIGNMENT) uint64_t hi; uint64_t lo; } zig_u128; |
| 2032 | typedef struct { zig_align(ZIG_TARGET_MAX_INT_ALIGNMENT) int64_t hi; uint64_t lo; } zig_i128; |
| 2033 | #endif |
| 2034 | |
| 2035 | #define zig_init_u128(hi, lo) { .h##i = hi, .l##o = lo } |
| 2036 | #define zig_init_i128(hi, lo) { .h##i = hi, .l##o = lo } |
| 2037 | #define zig_make_u128(hi, lo) (zig_u128)zig_init_u128(hi, lo) |
| 2038 | #define zig_make_i128(hi, lo) (zig_i128)zig_init_i128(hi, lo) |
| 2039 | #define zig_hi_u128(arg) (arg).hi |
| 2040 | #define zig_lo_u128(arg) (arg).lo |
| 2041 | #define zig_hi_i128(arg) (arg).hi |
| 2042 | #define zig_lo_i128(arg) (arg).lo |
| 1864 | 2043 | #define zig_cmp_int128(Type) \ |
| 1865 | 2044 | static inline int32_t zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 1866 | 2045 | return (lhs.hi == rhs.hi) \ |
| ... | ... | @@ -1872,6 +2051,30 @@ typedef struct { zig_align(16) int64_t hi; uint64_t lo; } zig_i128; |
| 1872 | 2051 | return (zig_##Type){ .hi = lhs.hi operator rhs.hi, .lo = lhs.lo operator rhs.lo }; \ |
| 1873 | 2052 | } |
| 1874 | 2053 | |
| 2054 | static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) { |
| 2055 | if (rhs == UINT8_C(0)) return lhs; |
| 2056 | if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 }; |
| 2057 | return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs }; |
| 2058 | } |
| 2059 | |
| 2060 | static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) { |
| 2061 | if (rhs == UINT8_C(0)) return lhs; |
| 2062 | if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = zig_minInt_u64, .lo = lhs.hi >> (rhs - UINT8_C(64)) }; |
| 2063 | return (zig_u128){ .hi = lhs.hi >> rhs, .lo = lhs.hi << (UINT8_C(64) - rhs) | lhs.lo >> rhs }; |
| 2064 | } |
| 2065 | |
| 2066 | static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) { |
| 2067 | if (rhs == UINT8_C(0)) return lhs; |
| 2068 | if (rhs >= UINT8_C(64)) return (zig_i128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 }; |
| 2069 | return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs }; |
| 2070 | } |
| 2071 | |
| 2072 | static inline zig_i128 zig_shr_i128(zig_i128 lhs, uint8_t rhs) { |
| 2073 | if (rhs == UINT8_C(0)) return lhs; |
| 2074 | if (rhs >= UINT8_C(64)) return (zig_i128){ .hi = zig_shr_i64(lhs.hi, 63), .lo = zig_shr_i64(lhs.hi, (rhs - UINT8_C(64))) }; |
| 2075 | return (zig_i128){ .hi = zig_shr_i64(lhs.hi, rhs), .lo = lhs.lo >> rhs | (uint64_t)lhs.hi << (UINT8_C(64) - rhs) }; |
| 2076 | } |
| 2077 | |
| 1875 | 2078 | #endif /* zig_has_int128 */ |
| 1876 | 2079 | |
| 1877 | 2080 | #define zig_minInt_u128 zig_make_u128(zig_minInt_u64, zig_minInt_u64) |
| ... | ... | @@ -1891,42 +2094,177 @@ zig_bit_int128(i128, or, |) |
| 1891 | 2094 | zig_bit_int128(u128, xor, ^) |
| 1892 | 2095 | zig_bit_int128(i128, xor, ^) |
| 1893 | 2096 | |
| 1894 | | static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs); |
| 2097 | static inline uint8_t zig_u8_intCast_u128(zig_u128 arg) { |
| 2098 | return (uint8_t)zig_lo_u128(arg); |
| 2099 | } |
| 2100 | static inline uint8_t zig_u8_intCast_i128(zig_i128 arg) { |
| 2101 | return (uint8_t)zig_lo_i128(arg); |
| 2102 | } |
| 2103 | static inline int8_t zig_i8_intCast_i128(zig_i128 arg) { |
| 2104 | return (int8_t)zig_lo_i128(arg); |
| 2105 | } |
| 2106 | static inline int8_t zig_i8_intCast_u128(zig_u128 arg) { |
| 2107 | return (int8_t)zig_lo_u128(arg); |
| 2108 | } |
| 1895 | 2109 | |
| 1896 | | #if zig_has_int128 |
| 2110 | static inline uint16_t zig_u16_intCast_u128(zig_u128 arg) { |
| 2111 | return (uint16_t)zig_lo_u128(arg); |
| 2112 | } |
| 2113 | static inline uint16_t zig_u16_intCast_i128(zig_i128 arg) { |
| 2114 | return (uint16_t)zig_lo_i128(arg); |
| 2115 | } |
| 2116 | static inline int16_t zig_i16_intCast_i128(zig_i128 arg) { |
| 2117 | return (int16_t)zig_lo_i128(arg); |
| 2118 | } |
| 2119 | static inline int16_t zig_i16_intCast_u128(zig_u128 arg) { |
| 2120 | return (int16_t)zig_lo_u128(arg); |
| 2121 | } |
| 1897 | 2122 | |
| 1898 | | static inline zig_u128 zig_not_u128(zig_u128 val, uint8_t bits) { |
| 1899 | | return val ^ zig_maxInt_u(128, bits); |
| 2123 | static inline uint32_t zig_u32_intCast_u128(zig_u128 arg) { |
| 2124 | return (uint32_t)zig_lo_u128(arg); |
| 2125 | } |
| 2126 | static inline uint32_t zig_u32_intCast_i128(zig_i128 arg) { |
| 2127 | return (uint32_t)zig_lo_i128(arg); |
| 2128 | } |
| 2129 | static inline int32_t zig_i32_intCast_i128(zig_i128 arg) { |
| 2130 | return (int32_t)zig_lo_i128(arg); |
| 2131 | } |
| 2132 | static inline int32_t zig_i32_intCast_u128(zig_u128 arg) { |
| 2133 | return (int32_t)zig_lo_u128(arg); |
| 1900 | 2134 | } |
| 1901 | 2135 | |
| 1902 | | static inline zig_i128 zig_not_i128(zig_i128 val, uint8_t bits) { |
| 1903 | | (void)bits; |
| 1904 | | return ~val; |
| 2136 | static inline uint64_t zig_u64_intCast_u128(zig_u128 arg) { |
| 2137 | return zig_lo_u128(arg); |
| 2138 | } |
| 2139 | static inline uint64_t zig_u64_intCast_i128(zig_i128 arg) { |
| 2140 | return zig_lo_i128(arg); |
| 2141 | } |
| 2142 | static inline int64_t zig_i64_intCast_i128(zig_i128 arg) { |
| 2143 | return (int64_t)zig_lo_i128(arg); |
| 2144 | } |
| 2145 | static inline int64_t zig_i64_intCast_u128(zig_u128 arg) { |
| 2146 | return (int64_t)zig_lo_u128(arg); |
| 1905 | 2147 | } |
| 1906 | 2148 | |
| 1907 | | static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) { |
| 1908 | | return lhs >> rhs; |
| 2149 | static inline zig_u128 zig_u128_intCast_u8(uint8_t arg) { |
| 2150 | return zig_make_u128(UINT8_C(0), arg); |
| 2151 | } |
| 2152 | static inline zig_u128 zig_u128_intCast_i8(int8_t arg) { |
| 2153 | return zig_make_u128(UINT8_C(0), (uint8_t)arg); |
| 2154 | } |
| 2155 | static inline zig_i128 zig_i128_intCast_i8(int8_t arg) { |
| 2156 | return zig_make_i128(zig_shr_i64(arg, 63), (uint8_t)arg); |
| 2157 | } |
| 2158 | static inline zig_i128 zig_i128_intCast_u8(uint8_t arg) { |
| 2159 | return zig_make_i128(INT8_C(0), arg); |
| 1909 | 2160 | } |
| 1910 | 2161 | |
| 1911 | | static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) { |
| 1912 | | return lhs << rhs; |
| 2162 | static inline zig_u128 zig_u128_intCast_u16(uint16_t arg) { |
| 2163 | return zig_make_u128(UINT16_C(0), arg); |
| 2164 | } |
| 2165 | static inline zig_u128 zig_u128_intCast_i16(int16_t arg) { |
| 2166 | return zig_make_u128(UINT16_C(0), (uint16_t)arg); |
| 2167 | } |
| 2168 | static inline zig_i128 zig_i128_intCast_i16(int16_t arg) { |
| 2169 | return zig_make_i128(zig_shr_i64(arg, 63), (uint16_t)arg); |
| 2170 | } |
| 2171 | static inline zig_i128 zig_i128_intCast_u16(uint16_t arg) { |
| 2172 | return zig_make_i128(INT16_C(0), arg); |
| 1913 | 2173 | } |
| 1914 | 2174 | |
| 1915 | | static inline zig_i128 zig_shr_i128(zig_i128 lhs, uint8_t rhs) { |
| 1916 | | // This works around a GCC miscompilation, but it has the side benefit of |
| 1917 | | // emitting better code. It is behind the `#if` because it depends on |
| 1918 | | // arithmetic right shift, which is implementation-defined in C, but should |
| 1919 | | // be guaranteed on any GCC-compatible compiler. |
| 1920 | | #if defined(zig_gnuc) |
| 1921 | | return lhs >> rhs; |
| 2175 | static inline zig_u128 zig_u128_intCast_u32(uint32_t arg) { |
| 2176 | return zig_make_u128(UINT32_C(0), arg); |
| 2177 | } |
| 2178 | static inline zig_u128 zig_u128_intCast_i32(int32_t arg) { |
| 2179 | return zig_make_u128(UINT32_C(0), (uint32_t)arg); |
| 2180 | } |
| 2181 | static inline zig_i128 zig_i128_intCast_i32(int32_t arg) { |
| 2182 | return zig_make_i128(zig_shr_i64(arg, 63), (uint32_t)arg); |
| 2183 | } |
| 2184 | static inline zig_i128 zig_i128_intCast_u32(uint32_t arg) { |
| 2185 | return zig_make_i128(INT32_C(0), arg); |
| 2186 | } |
| 2187 | |
| 2188 | static inline zig_u128 zig_u128_intCast_u64(uint64_t arg) { |
| 2189 | return zig_make_u128(UINT64_C(0), arg); |
| 2190 | } |
| 2191 | static inline zig_u128 zig_u128_intCast_i64(int64_t arg) { |
| 2192 | return zig_make_u128(UINT64_C(0), (uint64_t)arg); |
| 2193 | } |
| 2194 | static inline zig_i128 zig_i128_intCast_i64(int64_t arg) { |
| 2195 | return zig_make_i128(zig_shr_i64(arg, 63), (uint64_t)arg); |
| 2196 | } |
| 2197 | static inline zig_i128 zig_i128_intCast_u64(uint64_t arg) { |
| 2198 | return zig_make_i128(INT64_C(0), arg); |
| 2199 | } |
| 2200 | |
| 2201 | static inline zig_u128 zig_u128_intCast_u128(zig_u128 arg) { |
| 2202 | return arg; |
| 2203 | } |
| 2204 | static inline zig_u128 zig_u128_intCast_i128(zig_i128 arg) { |
| 2205 | #if zig_has_int128 |
| 2206 | return (zig_u128)arg; |
| 1922 | 2207 | #else |
| 1923 | | zig_i128 sign_mask = lhs < zig_make_i128(0, 0) ? -zig_make_i128(0, 1) : zig_make_i128(0, 0); |
| 1924 | | return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; |
| 2208 | return zig_make_u128(zig_u64_bitCast_i64(zig_hi_i128(arg), UINT8_C(64)), zig_lo_u128(arg)); |
| 2209 | #endif |
| 2210 | } |
| 2211 | static inline zig_i128 zig_i128_intCast_i128(zig_i128 arg) { |
| 2212 | return arg; |
| 2213 | } |
| 2214 | static inline zig_i128 zig_i128_intCast_u128(zig_u128 arg) { |
| 2215 | #if zig_has_int128 |
| 2216 | return (zig_i128)arg; |
| 2217 | #else |
| 2218 | return zig_make_i128(zig_i64_bitCast_u64(zig_hi_i128(arg), UINT8_C(64)), zig_lo_u128(arg)); |
| 1925 | 2219 | #endif |
| 1926 | 2220 | } |
| 1927 | 2221 | |
| 1928 | | static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) { |
| 1929 | | return lhs << rhs; |
| 2222 | #define zig_int128_cast_builtins(w) \ |
| 2223 | static inline uint##w##_t zig_u##w##_truncate_u128(zig_u128 arg, uint8_t bits) { \ |
| 2224 | return zig_u##w##_truncate_u##w((uint##w##_t)zig_lo_u128(arg), bits); \ |
| 2225 | } \ |
| 2226 | \ |
| 2227 | static inline int##w##_t zig_i##w##_truncate_i128(zig_i128 arg, uint8_t bits) { \ |
| 2228 | return zig_i##w##_truncate_i##w((int##w##_t)zig_lo_i128(arg), bits); \ |
| 2229 | } |
| 2230 | zig_int128_cast_builtins(8) |
| 2231 | zig_int128_cast_builtins(16) |
| 2232 | zig_int128_cast_builtins(32) |
| 2233 | zig_int128_cast_builtins(64) |
| 2234 | |
| 2235 | static inline zig_u128 zig_u128_truncate_u128(zig_u128 arg, uint8_t bits) { |
| 2236 | return zig_and_u128(arg, zig_maxInt_u(128, bits)); |
| 2237 | } |
| 2238 | static inline zig_i128 zig_i128_truncate_i128(zig_i128 arg, uint8_t bits) { |
| 2239 | if (bits > UINT8_C(64)) return zig_make_i128(zig_i64_truncate_i64(zig_hi_i128(arg), bits - UINT8_C(64)), zig_lo_i128(arg)); |
| 2240 | int64_t lo = zig_i64_truncate_i128(arg, bits); |
| 2241 | return zig_make_i128(zig_shr_i64(lo, 63), (uint64_t)lo); |
| 2242 | } |
| 2243 | |
| 2244 | static inline zig_u128 zig_u128_bitCast_u128(zig_u128 arg, uint8_t bits) { |
| 2245 | (void)bits; |
| 2246 | return arg; |
| 2247 | } |
| 2248 | static inline zig_u128 zig_u128_bitCast_i128(zig_i128 arg, uint8_t bits) { |
| 2249 | return zig_u128_truncate_u128(zig_u128_intCast_i128(arg), bits); |
| 2250 | } |
| 2251 | static inline zig_i128 zig_i128_bitCast_i128(zig_i128 arg, uint8_t bits) { |
| 2252 | (void)bits; |
| 2253 | return arg; |
| 2254 | } |
| 2255 | static inline zig_i128 zig_i128_bitCast_u128(zig_u128 arg, uint8_t bits) { |
| 2256 | return zig_i128_truncate_i128(zig_i128_intCast_u128(arg), bits); |
| 2257 | } |
| 2258 | |
| 2259 | #if zig_has_int128 |
| 2260 | |
| 2261 | static inline zig_u128 zig_not_u128(zig_u128 arg, uint8_t bits) { |
| 2262 | return arg ^ zig_maxInt_u(128, bits); |
| 2263 | } |
| 2264 | |
| 2265 | static inline zig_i128 zig_not_i128(zig_i128 arg, uint8_t bits) { |
| 2266 | (void)bits; |
| 2267 | return ~arg; |
| 1930 | 2268 | } |
| 1931 | 2269 | |
| 1932 | 2270 | static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) { |
| ... | ... | @@ -1953,11 +2291,11 @@ static inline zig_i128 zig_mul_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1953 | 2291 | return lhs * rhs; |
| 1954 | 2292 | } |
| 1955 | 2293 | |
| 1956 | | static inline zig_u128 zig_div_trunc_u128(zig_u128 lhs, zig_u128 rhs) { |
| 2294 | static inline zig_u128 zig_divTrunc_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1957 | 2295 | return lhs / rhs; |
| 1958 | 2296 | } |
| 1959 | 2297 | |
| 1960 | | static inline zig_i128 zig_div_trunc_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2298 | static inline zig_i128 zig_divTrunc_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1961 | 2299 | return lhs / rhs; |
| 1962 | 2300 | } |
| 1963 | 2301 | |
| ... | ... | @@ -1971,36 +2309,14 @@ static inline zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1971 | 2309 | |
| 1972 | 2310 | #else /* zig_has_int128 */ |
| 1973 | 2311 | |
| 1974 | | static inline zig_u128 zig_not_u128(zig_u128 val, uint8_t bits) { |
| 1975 | | return (zig_u128){ .hi = zig_not_u64(val.hi, bits - UINT8_C(64)), .lo = zig_not_u64(val.lo, UINT8_C(64)) }; |
| 1976 | | } |
| 1977 | | |
| 1978 | | static inline zig_i128 zig_not_i128(zig_i128 val, uint8_t bits) { |
| 1979 | | return (zig_i128){ .hi = zig_not_i64(val.hi, bits - UINT8_C(64)), .lo = zig_not_u64(val.lo, UINT8_C(64)) }; |
| 1980 | | } |
| 1981 | | |
| 1982 | | static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) { |
| 1983 | | if (rhs == UINT8_C(0)) return lhs; |
| 1984 | | if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = zig_minInt_u64, .lo = lhs.hi >> (rhs - UINT8_C(64)) }; |
| 1985 | | return (zig_u128){ .hi = lhs.hi >> rhs, .lo = lhs.hi << (UINT8_C(64) - rhs) | lhs.lo >> rhs }; |
| 1986 | | } |
| 1987 | | |
| 1988 | | static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) { |
| 1989 | | if (rhs == UINT8_C(0)) return lhs; |
| 1990 | | if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 }; |
| 1991 | | return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs }; |
| 1992 | | } |
| 1993 | | |
| 1994 | | static inline zig_i128 zig_shr_i128(zig_i128 lhs, uint8_t rhs) { |
| 1995 | | if (rhs == UINT8_C(0)) return lhs; |
| 1996 | | if (rhs >= UINT8_C(64)) return (zig_i128){ .hi = zig_shr_i64(lhs.hi, 63), .lo = zig_shr_i64(lhs.hi, (rhs - UINT8_C(64))) }; |
| 1997 | | return (zig_i128){ .hi = zig_shr_i64(lhs.hi, rhs), .lo = lhs.lo >> rhs | (uint64_t)lhs.hi << (UINT8_C(64) - rhs) }; |
| 2312 | static inline zig_u128 zig_not_u128(zig_u128 arg, uint8_t bits) { |
| 2313 | if (bits <= UINT8_C(64)) return (zig_u128){ .hi = UINT64_C(0), .lo = zig_not_u64(arg.lo, bits) }; |
| 2314 | return (zig_u128){ .hi = zig_not_u64(arg.hi, bits - UINT8_C(64)), .lo = zig_not_u64(arg.lo, UINT8_C(64)) }; |
| 1998 | 2315 | } |
| 1999 | 2316 | |
| 2000 | | static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) { |
| 2001 | | if (rhs == UINT8_C(0)) return lhs; |
| 2002 | | if (rhs >= UINT8_C(64)) return (zig_i128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 }; |
| 2003 | | return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs }; |
| 2317 | static inline zig_i128 zig_not_i128(zig_i128 arg, uint8_t bits) { |
| 2318 | (void)bits; |
| 2319 | return (zig_i128){ .hi = ~arg.hi, .lo = ~arg.lo }; |
| 2004 | 2320 | } |
| 2005 | 2321 | |
| 2006 | 2322 | static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) { |
| ... | ... | @@ -2027,59 +2343,59 @@ static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2027 | 2343 | return res; |
| 2028 | 2344 | } |
| 2029 | 2345 | |
| 2030 | | zig_extern zig_i128 __multi3(zig_i128 lhs, zig_i128 rhs); |
| 2031 | 2346 | static zig_i128 zig_mul_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2347 | zig_extern zig_i128 __multi3(zig_i128 lhs, zig_i128 rhs); |
| 2032 | 2348 | return __multi3(lhs, rhs); |
| 2033 | 2349 | } |
| 2034 | 2350 | |
| 2035 | 2351 | static zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) { |
| 2036 | | return zig_bitCast_u128(zig_mul_i128(zig_bitCast_i128(lhs), zig_bitCast_i128(rhs))); |
| 2352 | return zig_u128_bitCast_i128(zig_mul_i128(zig_i128_bitCast_u128(lhs, UINT8_C(128)), zig_i128_bitCast_u128(rhs, UINT8_C(128))), UINT8_C(128)); |
| 2037 | 2353 | } |
| 2038 | 2354 | |
| 2039 | | zig_extern zig_u128 __udivti3(zig_u128 lhs, zig_u128 rhs); |
| 2040 | | static zig_u128 zig_div_trunc_u128(zig_u128 lhs, zig_u128 rhs) { |
| 2355 | static zig_u128 zig_divTrunc_u128(zig_u128 lhs, zig_u128 rhs) { |
| 2356 | zig_extern zig_u128 __udivti3(zig_u128 lhs, zig_u128 rhs); |
| 2041 | 2357 | return __udivti3(lhs, rhs); |
| 2042 | 2358 | } |
| 2043 | 2359 | |
| 2044 | | zig_extern zig_i128 __divti3(zig_i128 lhs, zig_i128 rhs); |
| 2045 | | static zig_i128 zig_div_trunc_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2360 | static zig_i128 zig_divTrunc_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2361 | zig_extern zig_i128 __divti3(zig_i128 lhs, zig_i128 rhs); |
| 2046 | 2362 | return __divti3(lhs, rhs); |
| 2047 | 2363 | } |
| 2048 | 2364 | |
| 2049 | | zig_extern zig_u128 __umodti3(zig_u128 lhs, zig_u128 rhs); |
| 2050 | 2365 | static zig_u128 zig_rem_u128(zig_u128 lhs, zig_u128 rhs) { |
| 2366 | zig_extern zig_u128 __umodti3(zig_u128 lhs, zig_u128 rhs); |
| 2051 | 2367 | return __umodti3(lhs, rhs); |
| 2052 | 2368 | } |
| 2053 | 2369 | |
| 2054 | | zig_extern zig_i128 __modti3(zig_i128 lhs, zig_i128 rhs); |
| 2055 | 2370 | static zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2371 | zig_extern zig_i128 __modti3(zig_i128 lhs, zig_i128 rhs); |
| 2056 | 2372 | return __modti3(lhs, rhs); |
| 2057 | 2373 | } |
| 2058 | 2374 | |
| 2059 | 2375 | #endif /* zig_has_int128 */ |
| 2060 | 2376 | |
| 2061 | | #define zig_div_floor_u128 zig_div_trunc_u128 |
| 2377 | #define zig_divFloor_u128 zig_divTrunc_u128 |
| 2062 | 2378 | |
| 2063 | | static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2379 | static inline zig_i128 zig_divFloor_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2064 | 2380 | zig_i128 rem = zig_rem_i128(lhs, rhs); |
| 2065 | 2381 | int64_t mask = zig_or_u64((uint64_t)zig_hi_i128(rem), zig_lo_i128(rem)) != UINT64_C(0) |
| 2066 | 2382 | ? zig_shr_i64(zig_xor_i64(zig_hi_i128(lhs), zig_hi_i128(rhs)), UINT8_C(63)) : INT64_C(0); |
| 2067 | | return zig_add_i128(zig_div_trunc_i128(lhs, rhs), zig_make_i128(mask, (uint64_t)mask)); |
| 2383 | return zig_add_i128(zig_divTrunc_i128(lhs, rhs), zig_make_i128(mask, (uint64_t)mask)); |
| 2068 | 2384 | } |
| 2069 | 2385 | |
| 2070 | | static inline zig_u128 zig_div_ceil_u128(zig_u128 lhs, zig_u128 rhs) { |
| 2386 | static inline zig_u128 zig_divCeil_u128(zig_u128 lhs, zig_u128 rhs) { |
| 2071 | 2387 | zig_u128 rem = zig_rem_u128(lhs, rhs); |
| 2072 | 2388 | uint64_t mask = zig_or_u64(zig_hi_u128(rem), zig_lo_u128(rem)) != UINT64_C(0) |
| 2073 | 2389 | ? UINT64_C(1) : UINT64_C(0); |
| 2074 | | return zig_add_u128(zig_div_trunc_u128(lhs, rhs), zig_make_u128(UINT64_C(0), mask)); |
| 2390 | return zig_add_u128(zig_divTrunc_u128(lhs, rhs), zig_make_u128(UINT64_C(0), mask)); |
| 2075 | 2391 | } |
| 2076 | 2392 | |
| 2077 | | static inline zig_i128 zig_div_ceil_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2393 | static inline zig_i128 zig_divCeil_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2078 | 2394 | zig_i128 rem = zig_rem_i128(lhs, rhs); |
| 2079 | 2395 | int64_t mask = zig_or_u64((uint64_t)zig_hi_i128(rem), zig_lo_i128(rem)) != UINT64_C(0) |
| 2080 | 2396 | ? zig_shr_i64(zig_xor_i64(zig_hi_i128(lhs), zig_hi_i128(rhs)), UINT8_C(63)) + INT64_C(1) |
| 2081 | 2397 | : INT64_C(0); |
| 2082 | | return zig_add_i128(zig_div_trunc_i128(lhs, rhs), zig_make_i128(INT64_C(0), (uint64_t)mask)); |
| 2398 | return zig_add_i128(zig_divTrunc_i128(lhs, rhs), zig_make_i128(INT64_C(0), (uint64_t)mask)); |
| 2083 | 2399 | } |
| 2084 | 2400 | |
| 2085 | 2401 | #define zig_mod_u128 zig_rem_u128 |
| ... | ... | @@ -2107,51 +2423,41 @@ static inline zig_i128 zig_max_i128(zig_i128 lhs, zig_i128 rhs) { |
| 2107 | 2423 | return zig_cmp_i128(lhs, rhs) > INT32_C(0) ? lhs : rhs; |
| 2108 | 2424 | } |
| 2109 | 2425 | |
| 2110 | | static inline zig_u128 zig_wrap_u128(zig_u128 val, uint8_t bits) { |
| 2111 | | return zig_and_u128(val, zig_maxInt_u(128, bits)); |
| 2112 | | } |
| 2113 | | |
| 2114 | | static inline zig_i128 zig_wrap_i128(zig_i128 val, uint8_t bits) { |
| 2115 | | if (bits > UINT8_C(64)) return zig_make_i128(zig_wrap_i64(zig_hi_i128(val), bits - UINT8_C(64)), zig_lo_i128(val)); |
| 2116 | | int64_t lo = zig_wrap_i64((int64_t)zig_lo_i128(val), bits); |
| 2117 | | return zig_make_i128(zig_shr_i64(lo, 63), (uint64_t)lo); |
| 2118 | | } |
| 2119 | | |
| 2120 | 2426 | static inline zig_u128 zig_shlw_u128(zig_u128 lhs, uint8_t rhs, uint8_t bits) { |
| 2121 | | return zig_wrap_u128(zig_shl_u128(lhs, rhs), bits); |
| 2427 | return zig_u128_truncate_u128(zig_shl_u128(lhs, rhs), bits); |
| 2122 | 2428 | } |
| 2123 | 2429 | |
| 2124 | 2430 | static inline zig_i128 zig_shlw_i128(zig_i128 lhs, uint8_t rhs, uint8_t bits) { |
| 2125 | | return zig_wrap_i128(zig_bitCast_i128(zig_shl_u128(zig_bitCast_u128(lhs), rhs)), bits); |
| 2431 | return zig_i128_truncate_i128(zig_i128_bitCast_u128(zig_shl_u128(zig_u128_bitCast_i128(lhs, bits), rhs), bits), bits); |
| 2126 | 2432 | } |
| 2127 | 2433 | |
| 2128 | 2434 | static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 2129 | | return zig_wrap_u128(zig_add_u128(lhs, rhs), bits); |
| 2435 | return zig_u128_truncate_u128(zig_add_u128(lhs, rhs), bits); |
| 2130 | 2436 | } |
| 2131 | 2437 | |
| 2132 | 2438 | static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 2133 | | return zig_wrap_i128(zig_bitCast_i128(zig_add_u128(zig_bitCast_u128(lhs), zig_bitCast_u128(rhs))), bits); |
| 2439 | return zig_i128_truncate_i128(zig_i128_bitCast_u128(zig_add_u128(zig_u128_bitCast_i128(lhs, bits), zig_u128_bitCast_i128(rhs, bits)), bits), bits); |
| 2134 | 2440 | } |
| 2135 | 2441 | |
| 2136 | 2442 | static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 2137 | | return zig_wrap_u128(zig_sub_u128(lhs, rhs), bits); |
| 2443 | return zig_u128_truncate_u128(zig_sub_u128(lhs, rhs), bits); |
| 2138 | 2444 | } |
| 2139 | 2445 | |
| 2140 | 2446 | static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 2141 | | return zig_wrap_i128(zig_bitCast_i128(zig_sub_u128(zig_bitCast_u128(lhs), zig_bitCast_u128(rhs))), bits); |
| 2447 | return zig_i128_truncate_i128(zig_i128_bitCast_u128(zig_sub_u128(zig_u128_bitCast_i128(lhs, bits), zig_u128_bitCast_i128(rhs, bits)), bits), bits); |
| 2142 | 2448 | } |
| 2143 | 2449 | |
| 2144 | 2450 | static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 2145 | | return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits); |
| 2451 | return zig_u128_truncate_u128(zig_mul_u128(lhs, rhs), bits); |
| 2146 | 2452 | } |
| 2147 | 2453 | |
| 2148 | 2454 | static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 2149 | | return zig_wrap_i128(zig_bitCast_i128(zig_mul_u128(zig_bitCast_u128(lhs), zig_bitCast_u128(rhs))), bits); |
| 2455 | return zig_i128_truncate_i128(zig_i128_bitCast_u128(zig_mul_u128(zig_u128_bitCast_i128(lhs, bits), zig_u128_bitCast_i128(rhs, bits)), bits), bits); |
| 2150 | 2456 | } |
| 2151 | 2457 | |
| 2152 | | static inline zig_u128 zig_abs_i128(zig_i128 val) { |
| 2153 | | zig_i128 tmp = zig_shr_i128(val, 127); |
| 2154 | | return zig_bitCast_u128(zig_sub_i128(zig_xor_i128(val, tmp), tmp)); |
| 2458 | static inline zig_u128 zig_abs_i128(zig_i128 arg) { |
| 2459 | zig_u128 tmp = zig_u128_bitCast_i128(zig_shr_i128(arg, 127), UINT8_C(128)); |
| 2460 | return zig_sub_u128(zig_xor_u128(zig_u128_bitCast_i128(arg, UINT8_C(128)), tmp), tmp); |
| 2155 | 2461 | } |
| 2156 | 2462 | |
| 2157 | 2463 | #if zig_has_int128 |
| ... | ... | @@ -2160,7 +2466,7 @@ static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint |
| 2160 | 2466 | #if zig_has_builtin(add_overflow) |
| 2161 | 2467 | zig_u128 full_res; |
| 2162 | 2468 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 2163 | | *res = zig_wrap_u128(full_res, bits); |
| 2469 | *res = zig_u128_truncate_u128(full_res, bits); |
| 2164 | 2470 | return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits); |
| 2165 | 2471 | #else |
| 2166 | 2472 | *res = zig_addw_u128(lhs, rhs, bits); |
| ... | ... | @@ -2176,7 +2482,7 @@ static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint |
| 2176 | 2482 | zig_i128 full_res = (zig_i128)((zig_u128)lhs + (zig_u128)rhs); |
| 2177 | 2483 | bool overflow = ((full_res ^ lhs) & (full_res ^ rhs)) < 0; |
| 2178 | 2484 | #endif |
| 2179 | | *res = zig_wrap_i128(full_res, bits); |
| 2485 | *res = zig_i128_truncate_i128(full_res, bits); |
| 2180 | 2486 | return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits); |
| 2181 | 2487 | } |
| 2182 | 2488 | |
| ... | ... | @@ -2184,7 +2490,7 @@ static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint |
| 2184 | 2490 | #if zig_has_builtin(sub_overflow) |
| 2185 | 2491 | zig_u128 full_res; |
| 2186 | 2492 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 2187 | | *res = zig_wrap_u128(full_res, bits); |
| 2493 | *res = zig_u128_truncate_u128(full_res, bits); |
| 2188 | 2494 | return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits); |
| 2189 | 2495 | #else |
| 2190 | 2496 | *res = zig_subw_u128(lhs, rhs, bits); |
| ... | ... | @@ -2200,7 +2506,7 @@ static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint |
| 2200 | 2506 | zig_i128 full_res = (zig_i128)((zig_u128)lhs - (zig_u128)rhs); |
| 2201 | 2507 | bool overflow = ((lhs ^ rhs) & (full_res ^ lhs)) < 0; |
| 2202 | 2508 | #endif |
| 2203 | | *res = zig_wrap_i128(full_res, bits); |
| 2509 | *res = zig_i128_truncate_i128(full_res, bits); |
| 2204 | 2510 | return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits); |
| 2205 | 2511 | } |
| 2206 | 2512 | |
| ... | ... | @@ -2208,7 +2514,7 @@ static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint |
| 2208 | 2514 | #if zig_has_builtin(mul_overflow) |
| 2209 | 2515 | zig_u128 full_res; |
| 2210 | 2516 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 2211 | | *res = zig_wrap_u128(full_res, bits); |
| 2517 | *res = zig_u128_truncate_u128(full_res, bits); |
| 2212 | 2518 | return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits); |
| 2213 | 2519 | #else |
| 2214 | 2520 | *res = zig_mulw_u128(lhs, rhs, bits); |
| ... | ... | @@ -2216,8 +2522,8 @@ static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint |
| 2216 | 2522 | #endif |
| 2217 | 2523 | } |
| 2218 | 2524 | |
| 2219 | | zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow); |
| 2220 | 2525 | static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 2526 | zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow); |
| 2221 | 2527 | #if zig_has_builtin(mul_overflow) |
| 2222 | 2528 | zig_i128 full_res; |
| 2223 | 2529 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| ... | ... | @@ -2226,50 +2532,78 @@ static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint |
| 2226 | 2532 | zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int); |
| 2227 | 2533 | bool overflow = overflow_int != 0; |
| 2228 | 2534 | #endif |
| 2229 | | *res = zig_wrap_i128(full_res, bits); |
| 2535 | *res = zig_i128_truncate_i128(full_res, bits); |
| 2230 | 2536 | return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits); |
| 2231 | 2537 | } |
| 2232 | 2538 | |
| 2233 | 2539 | #else /* zig_has_int128 */ |
| 2234 | 2540 | |
| 2235 | 2541 | static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 2236 | | uint64_t hi; |
| 2237 | | bool overflow = zig_addo_u64(&hi, lhs.hi, rhs.hi, bits - 64); |
| 2238 | | return overflow ^ zig_addo_u64(&res->hi, hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64); |
| 2542 | if (bits <= UINT8_C(64)) { |
| 2543 | uint64_t lo; |
| 2544 | bool overflow = zig_addo_u64(&lo, zig_u64_intCast_u128(lhs), zig_u64_intCast_u128(rhs), bits); |
| 2545 | *res = zig_u128_intCast_u64(lo); |
| 2546 | return overflow; |
| 2547 | } else { |
| 2548 | uint64_t hi; |
| 2549 | bool overflow = zig_addo_u64(&hi, lhs.hi, rhs.hi, bits - UINT8_C(64)); |
| 2550 | return overflow ^ zig_addo_u64(&res->hi, hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, UINT8_C(64)), bits - UINT8_C(64)); |
| 2551 | } |
| 2239 | 2552 | } |
| 2240 | 2553 | |
| 2241 | 2554 | static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 2242 | | int64_t hi; |
| 2243 | | bool overflow = zig_addo_i64(&hi, lhs.hi, rhs.hi, bits - 64); |
| 2244 | | return overflow ^ zig_addo_i64(&res->hi, hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64); |
| 2555 | if (bits <= UINT8_C(64)) { |
| 2556 | int64_t lo; |
| 2557 | bool overflow = zig_addo_i64(&lo, zig_i64_intCast_i128(lhs), zig_i64_intCast_i128(rhs), bits); |
| 2558 | *res = zig_i128_intCast_i64(lo); |
| 2559 | return overflow; |
| 2560 | } else { |
| 2561 | int64_t hi; |
| 2562 | bool overflow = zig_addo_i64(&hi, lhs.hi, rhs.hi, bits - UINT8_C(64)); |
| 2563 | return overflow ^ zig_addo_i64(&res->hi, hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, UINT8_C(64)), bits - UINT8_C(64)); |
| 2564 | } |
| 2245 | 2565 | } |
| 2246 | 2566 | |
| 2247 | 2567 | static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 2248 | | uint64_t hi; |
| 2249 | | bool overflow = zig_subo_u64(&hi, lhs.hi, rhs.hi, bits - 64); |
| 2250 | | return overflow ^ zig_subo_u64(&res->hi, hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64); |
| 2568 | if (bits <= UINT8_C(64)) { |
| 2569 | uint64_t lo; |
| 2570 | bool overflow = zig_subo_u64(&lo, zig_u64_intCast_u128(lhs), zig_u64_intCast_u128(rhs), bits); |
| 2571 | *res = zig_u128_intCast_u64(lo); |
| 2572 | return overflow; |
| 2573 | } else { |
| 2574 | uint64_t hi; |
| 2575 | bool overflow = zig_subo_u64(&hi, lhs.hi, rhs.hi, bits - UINT8_C(64)); |
| 2576 | return overflow ^ zig_subo_u64(&res->hi, hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, UINT8_C(64)), bits - UINT8_C(64)); |
| 2577 | } |
| 2251 | 2578 | } |
| 2252 | 2579 | |
| 2253 | 2580 | static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 2254 | | int64_t hi; |
| 2255 | | bool overflow = zig_subo_i64(&hi, lhs.hi, rhs.hi, bits - 64); |
| 2256 | | return overflow ^ zig_subo_i64(&res->hi, hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64); |
| 2581 | if (bits <= UINT8_C(64)) { |
| 2582 | int64_t lo; |
| 2583 | bool overflow = zig_subo_i64(&lo, zig_i64_intCast_i128(lhs), zig_i64_intCast_i128(rhs), bits); |
| 2584 | *res = zig_i128_intCast_i64(lo); |
| 2585 | return overflow; |
| 2586 | } else { |
| 2587 | int64_t hi; |
| 2588 | bool overflow = zig_subo_i64(&hi, lhs.hi, rhs.hi, bits - UINT8_C(64)); |
| 2589 | return overflow ^ zig_subo_i64(&res->hi, hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, UINT8_C(64)), bits - UINT8_C(64)); |
| 2590 | } |
| 2257 | 2591 | } |
| 2258 | 2592 | |
| 2259 | 2593 | static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 2260 | 2594 | *res = zig_mulw_u128(lhs, rhs, bits); |
| 2261 | | return zig_cmp_u128(*res, zig_make_u128(0, 0)) != INT32_C(0) && |
| 2262 | | zig_cmp_u128(lhs, zig_div_trunc_u128(zig_maxInt_u(128, bits), rhs)) > INT32_C(0); |
| 2595 | return zig_cmp_u128(rhs, zig_make_u128(0, 0)) != INT32_C(0) && |
| 2596 | zig_cmp_u128(lhs, zig_divTrunc_u128(zig_maxInt_u(128, bits), rhs)) > INT32_C(0); |
| 2263 | 2597 | } |
| 2264 | 2598 | |
| 2265 | | zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow); |
| 2266 | 2599 | static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 2600 | zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow); |
| 2267 | 2601 | int overflow_int; |
| 2268 | 2602 | zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int); |
| 2269 | 2603 | bool overflow = overflow_int != 0 || |
| 2270 | 2604 | zig_cmp_i128(full_res, zig_minInt_i(128, bits)) < INT32_C(0) || |
| 2271 | 2605 | zig_cmp_i128(full_res, zig_maxInt_i(128, bits)) > INT32_C(0); |
| 2272 | | *res = zig_wrap_i128(full_res, bits); |
| 2606 | *res = zig_i128_truncate_i128(full_res, bits); |
| 2273 | 2607 | return overflow; |
| 2274 | 2608 | } |
| 2275 | 2609 | |
| ... | ... | @@ -2282,28 +2616,54 @@ static inline bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, uint8_t rhs, uint8 |
| 2282 | 2616 | |
| 2283 | 2617 | static inline bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, uint8_t rhs, uint8_t bits) { |
| 2284 | 2618 | *res = zig_shlw_i128(lhs, rhs, bits); |
| 2285 | | zig_i128 mask = zig_bitCast_i128(zig_shl_u128(zig_maxInt_u128, bits - rhs - UINT8_C(1))); |
| 2619 | zig_i128 mask = zig_i128_bitCast_u128(zig_shl_u128(zig_maxInt_u128, bits - rhs - UINT8_C(1)), bits); |
| 2286 | 2620 | return zig_cmp_i128(zig_and_i128(lhs, mask), zig_make_i128(0, 0)) != INT32_C(0) && |
| 2287 | 2621 | zig_cmp_i128(zig_and_i128(lhs, mask), mask) != INT32_C(0); |
| 2288 | 2622 | } |
| 2289 | 2623 | |
| 2290 | | static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 2624 | #define zig_int128_shls_builtins(rw) \ |
| 2625 | static inline zig_u128 zig_shls_u128_u##rw(zig_u128 lhs, uint##rw##_t rhs, uint8_t bits) { \ |
| 2626 | zig_u128 res; \ |
| 2627 | if (rhs < bits && !zig_shlo_u128(&res, lhs, zig_u8_intCast_u##rw(rhs), bits)) return res; \ |
| 2628 | switch (zig_cmp_u128(lhs, zig_make_u128(UINT64_C(0), UINT64_C(0)))) { \ |
| 2629 | case 0: return zig_minInt_u(128, bits); \ |
| 2630 | case 1: return zig_maxInt_u(128, bits); \ |
| 2631 | default: zig_unreachable(); \ |
| 2632 | } \ |
| 2633 | } \ |
| 2634 | \ |
| 2635 | static inline zig_i128 zig_shls_i128_u##rw(zig_i128 lhs, uint##rw##_t rhs, uint8_t bits) { \ |
| 2636 | zig_i128 res; \ |
| 2637 | if (rhs < bits && !zig_shlo_i128(&res, lhs, zig_u8_intCast_u##rw(rhs), bits)) return res; \ |
| 2638 | switch (zig_cmp_i128(lhs, zig_make_i128(INT64_C(0), UINT64_C(0)))) { \ |
| 2639 | case -1: return zig_minInt_i(128, bits); \ |
| 2640 | case 0: return zig_make_i128(INT64_C(0), UINT64_C(0)); \ |
| 2641 | case 1: return zig_maxInt_i(128, bits); \ |
| 2642 | default: zig_unreachable(); \ |
| 2643 | } \ |
| 2644 | } |
| 2645 | zig_int128_shls_builtins(8) |
| 2646 | zig_int128_shls_builtins(16) |
| 2647 | zig_int128_shls_builtins(32) |
| 2648 | zig_int128_shls_builtins(64) |
| 2649 | |
| 2650 | static inline zig_u128 zig_shls_u128_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 2291 | 2651 | zig_u128 res; |
| 2292 | 2652 | if (zig_cmp_u128(rhs, zig_make_u128(0, bits)) < INT32_C(0) && !zig_shlo_u128(&res, lhs, (uint8_t)zig_lo_u128(rhs), bits)) return res; |
| 2293 | 2653 | switch (zig_cmp_u128(lhs, zig_make_u128(0, 0))) { |
| 2294 | | case 0: return zig_make_u128(0, 0); |
| 2295 | | case 1: return zig_maxInt_u(128, bits); |
| 2654 | case INT32_C(0): return zig_make_u128(0, 0); |
| 2655 | case INT32_C(1): return zig_maxInt_u(128, bits); |
| 2296 | 2656 | default: zig_unreachable(); |
| 2297 | 2657 | } |
| 2298 | 2658 | } |
| 2299 | 2659 | |
| 2300 | | static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_u128 rhs, uint8_t bits) { |
| 2660 | static inline zig_i128 zig_shls_i128_u128(zig_i128 lhs, zig_u128 rhs, uint8_t bits) { |
| 2301 | 2661 | zig_i128 res; |
| 2302 | 2662 | if (zig_cmp_u128(rhs, zig_make_u128(0, bits)) < INT32_C(0) && !zig_shlo_i128(&res, lhs, (uint8_t)zig_lo_u128(rhs), bits)) return res; |
| 2303 | 2663 | switch (zig_cmp_i128(lhs, zig_make_i128(0, 0))) { |
| 2304 | | case -1: return zig_minInt_i(128, bits); |
| 2305 | | case 0: return zig_make_i128(0, 0); |
| 2306 | | case 1: return zig_maxInt_i(128, bits); |
| 2664 | case -INT32_C(1): return zig_minInt_i(128, bits); |
| 2665 | case INT32_C(0): return zig_make_i128(0, 0); |
| 2666 | case INT32_C(1): return zig_maxInt_i(128, bits); |
| 2307 | 2667 | default: zig_unreachable(); |
| 2308 | 2668 | } |
| 2309 | 2669 | } |
| ... | ... | @@ -2341,57 +2701,60 @@ static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 2341 | 2701 | return zig_cmp_i128(zig_xor_i128(lhs, rhs), zig_make_i128(0, 0)) < INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits); |
| 2342 | 2702 | } |
| 2343 | 2703 | |
| 2344 | | static inline uint8_t zig_clz_u128(zig_u128 val, uint8_t bits) { |
| 2345 | | if (bits <= UINT8_C(64)) return zig_clz_u64(zig_lo_u128(val), bits); |
| 2346 | | if (zig_hi_u128(val) != 0) return zig_clz_u64(zig_hi_u128(val), bits - UINT8_C(64)); |
| 2347 | | return zig_clz_u64(zig_lo_u128(val), UINT8_C(64)) + (bits - UINT8_C(64)); |
| 2704 | static inline uint8_t zig_clz_u128(zig_u128 arg, uint8_t bits) { |
| 2705 | if (bits <= UINT8_C(64)) return zig_clz_u64(zig_lo_u128(arg), bits); |
| 2706 | if (zig_hi_u128(arg) != 0) return zig_clz_u64(zig_hi_u128(arg), bits - UINT8_C(64)); |
| 2707 | return zig_clz_u64(zig_lo_u128(arg), UINT8_C(64)) + (bits - UINT8_C(64)); |
| 2348 | 2708 | } |
| 2349 | 2709 | |
| 2350 | | static inline uint8_t zig_clz_i128(zig_i128 val, uint8_t bits) { |
| 2351 | | return zig_clz_u128(zig_bitCast_u128(val), bits); |
| 2710 | static inline uint8_t zig_clz_i128(zig_i128 arg, uint8_t bits) { |
| 2711 | return zig_clz_u128(zig_u128_bitCast_i128(arg, bits), bits); |
| 2352 | 2712 | } |
| 2353 | 2713 | |
| 2354 | | static inline uint8_t zig_ctz_u128(zig_u128 val, uint8_t bits) { |
| 2355 | | if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), UINT8_C(64)); |
| 2356 | | return zig_ctz_u64(zig_hi_u128(val), bits - UINT8_C(64)) + UINT8_C(64); |
| 2714 | static inline uint8_t zig_ctz_u128(zig_u128 arg, uint8_t bits) { |
| 2715 | if (zig_lo_u128(arg) != 0) return zig_ctz_u64(zig_lo_u128(arg), UINT8_C(64)); |
| 2716 | return zig_ctz_u64(zig_hi_u128(arg), bits - UINT8_C(64)) + UINT8_C(64); |
| 2357 | 2717 | } |
| 2358 | 2718 | |
| 2359 | | static inline uint8_t zig_ctz_i128(zig_i128 val, uint8_t bits) { |
| 2360 | | return zig_ctz_u128(zig_bitCast_u128(val), bits); |
| 2719 | static inline uint8_t zig_ctz_i128(zig_i128 arg, uint8_t bits) { |
| 2720 | return zig_ctz_u128(zig_u128_bitCast_i128(arg, bits), bits); |
| 2361 | 2721 | } |
| 2362 | 2722 | |
| 2363 | | static inline uint8_t zig_popcount_u128(zig_u128 val, uint8_t bits) { |
| 2364 | | return zig_popcount_u64(zig_hi_u128(val), bits - UINT8_C(64)) + |
| 2365 | | zig_popcount_u64(zig_lo_u128(val), UINT8_C(64)); |
| 2723 | static inline uint8_t zig_popCount_u128(zig_u128 arg, uint8_t bits) { |
| 2724 | return (bits > UINT8_C(64) ? zig_popCount_u64(zig_hi_u128(arg), bits - UINT8_C(64)) : UINT8_C(0)) + |
| 2725 | zig_popCount_u64(zig_lo_u128(arg), UINT8_C(64)); |
| 2366 | 2726 | } |
| 2367 | 2727 | |
| 2368 | | static inline uint8_t zig_popcount_i128(zig_i128 val, uint8_t bits) { |
| 2369 | | return zig_popcount_u128(zig_bitCast_u128(val), bits); |
| 2728 | static inline uint8_t zig_popCount_i128(zig_i128 arg, uint8_t bits) { |
| 2729 | return zig_popCount_u128(zig_u128_bitCast_i128(arg, bits), bits); |
| 2370 | 2730 | } |
| 2371 | 2731 | |
| 2372 | | static inline zig_u128 zig_byte_swap_u128(zig_u128 val, uint8_t bits) { |
| 2732 | static inline zig_u128 zig_byteSwap_u128(zig_u128 arg, uint8_t bits) { |
| 2373 | 2733 | zig_u128 full_res; |
| 2374 | 2734 | #if zig_has_builtin(bswap128) |
| 2375 | | full_res = __builtin_bswap128(val); |
| 2735 | full_res = __builtin_bswap128(arg); |
| 2376 | 2736 | #else |
| 2377 | | full_res = zig_make_u128(zig_byte_swap_u64(zig_lo_u128(val), UINT8_C(64)), |
| 2378 | | zig_byte_swap_u64(zig_hi_u128(val), UINT8_C(64))); |
| 2737 | full_res = zig_make_u128( |
| 2738 | zig_byteSwap_u64(zig_lo_u128(arg), UINT8_C(64)), |
| 2739 | zig_byteSwap_u64(zig_hi_u128(arg), UINT8_C(64)) |
| 2740 | ); |
| 2379 | 2741 | #endif |
| 2380 | 2742 | return zig_shr_u128(full_res, UINT8_C(128) - bits); |
| 2381 | 2743 | } |
| 2382 | 2744 | |
| 2383 | | static inline zig_i128 zig_byte_swap_i128(zig_i128 val, uint8_t bits) { |
| 2384 | | return zig_bitCast_i128(zig_byte_swap_u128(zig_bitCast_u128(val), bits)); |
| 2745 | static inline zig_i128 zig_byteSwap_i128(zig_i128 arg, uint8_t bits) { |
| 2746 | return zig_i128_bitCast_u128(zig_byteSwap_u128(zig_u128_bitCast_i128(arg, bits), bits), bits); |
| 2385 | 2747 | } |
| 2386 | 2748 | |
| 2387 | | static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, uint8_t bits) { |
| 2388 | | return zig_shr_u128(zig_make_u128(zig_bit_reverse_u64(zig_lo_u128(val), UINT8_C(64)), |
| 2389 | | zig_bit_reverse_u64(zig_hi_u128(val), UINT8_C(64))), |
| 2390 | | UINT8_C(128) - bits); |
| 2749 | static inline zig_u128 zig_bitReverse_u128(zig_u128 arg, uint8_t bits) { |
| 2750 | return zig_shr_u128(zig_make_u128( |
| 2751 | zig_bitReverse_u64(zig_lo_u128(arg), UINT8_C(64)), |
| 2752 | zig_bitReverse_u64(zig_hi_u128(arg), UINT8_C(64)) |
| 2753 | ), UINT8_C(128) - bits); |
| 2391 | 2754 | } |
| 2392 | 2755 | |
| 2393 | | static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, uint8_t bits) { |
| 2394 | | return zig_bitCast_i128(zig_bit_reverse_u128(zig_bitCast_u128(val), bits)); |
| 2756 | static inline zig_i128 zig_bitReverse_i128(zig_i128 arg, uint8_t bits) { |
| 2757 | return zig_i128_bitCast_u128(zig_bitReverse_u128(zig_u128_bitCast_i128(arg, bits), bits), bits); |
| 2395 | 2758 | } |
| 2396 | 2759 | |
| 2397 | 2760 | #if zig_has_int128 |
| ... | ... | @@ -2411,15 +2774,218 @@ static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, uint8_t bits) { |
| 2411 | 2774 | /* ========================== Big Integer Support =========================== */ |
| 2412 | 2775 | |
| 2413 | 2776 | static inline uint16_t zig_int_bytes(uint16_t bits) { |
| 2414 | | uint16_t bytes = (bits + CHAR_BIT - 1) / CHAR_BIT; |
| 2777 | uint16_t bytes = (bits - UINT16_C(1)) / CHAR_BIT + UINT16_C(1); |
| 2415 | 2778 | uint16_t alignment = ZIG_TARGET_MAX_INT_ALIGNMENT; |
| 2779 | |
| 2416 | 2780 | while (alignment / 2 >= bytes) alignment /= 2; |
| 2417 | 2781 | return (bytes + alignment - 1) / alignment * alignment; |
| 2418 | 2782 | } |
| 2419 | 2783 | |
| 2420 | | static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 2784 | static inline void zig_minInt_big(void *res, bool is_signed, uint16_t bits) { |
| 2785 | uint8_t *res_bytes = res; |
| 2786 | uint16_t size = zig_int_bytes(bits); |
| 2787 | uint16_t byte_offset = zig_shr_u16(bits - UINT16_C(1), UINT8_C(3)); |
| 2788 | uint16_t remainder_bits = zig_u8_truncate_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT8_C(1); |
| 2789 | uint8_t sign_byte; |
| 2790 | uint8_t fill_byte; |
| 2791 | |
| 2792 | if (is_signed) { |
| 2793 | int8_t signed_sign_byte = zig_minInt_i(8, remainder_bits); |
| 2794 | |
| 2795 | sign_byte = zig_u8_bitCast_i8(signed_sign_byte, UINT8_C(8)); |
| 2796 | fill_byte = zig_u8_bitCast_i8(zig_shr_i8(signed_sign_byte, UINT8_C(7)), UINT8_C(8)); |
| 2797 | } else { |
| 2798 | sign_byte = zig_minInt_u(8, remainder_bits); |
| 2799 | fill_byte = UINT8_C(0); |
| 2800 | } |
| 2801 | |
| 2802 | #if zig_little_endian |
| 2803 | memset(&res_bytes[0], zig_minInt_u8, byte_offset); |
| 2804 | res_bytes[byte_offset] = sign_byte; |
| 2805 | byte_offset += UINT16_C(1); |
| 2806 | memset(&res_bytes[byte_offset], fill_byte, size - byte_offset); |
| 2807 | #else |
| 2808 | byte_offset = size - UINT16_C(1) - byte_offset; |
| 2809 | memset(&res_bytes[0], fill_byte, byte_offset); |
| 2810 | res_bytes[byte_offset] = sign_byte; |
| 2811 | byte_offset += UINT16_C(1); |
| 2812 | memset(&res_bytes[byte_offset], zig_minInt_u8, size - byte_offset); |
| 2813 | #endif |
| 2814 | } |
| 2815 | |
| 2816 | static inline void zig_maxInt_big(void *res, bool is_signed, uint16_t bits) { |
| 2817 | uint8_t *res_bytes = res; |
| 2818 | uint16_t size = zig_int_bytes(bits); |
| 2819 | uint16_t byte_offset = zig_shr_u16(bits - UINT16_C(1), UINT8_C(3)); |
| 2820 | uint16_t remainder_bits = zig_u8_truncate_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT8_C(1); |
| 2821 | uint8_t sign_byte; |
| 2822 | uint8_t fill_byte; |
| 2823 | |
| 2824 | if (is_signed) { |
| 2825 | int8_t signed_sign_byte = zig_maxInt_i(8, remainder_bits); |
| 2826 | |
| 2827 | sign_byte = zig_u8_bitCast_i8(signed_sign_byte, UINT8_C(8)); |
| 2828 | fill_byte = zig_u8_bitCast_i8(zig_shr_i8(signed_sign_byte, UINT8_C(7)), UINT8_C(8)); |
| 2829 | } else { |
| 2830 | sign_byte = zig_maxInt_u(8, remainder_bits); |
| 2831 | fill_byte = UINT8_C(0); |
| 2832 | } |
| 2833 | |
| 2834 | #if zig_little_endian |
| 2835 | memset(&res_bytes[0], zig_maxInt_u8, byte_offset); |
| 2836 | res_bytes[byte_offset] = sign_byte; |
| 2837 | byte_offset += UINT16_C(1); |
| 2838 | memset(&res_bytes[byte_offset], fill_byte, size - byte_offset); |
| 2839 | #else |
| 2840 | byte_offset = size - UINT16_C(1) - byte_offset; |
| 2841 | memset(&res_bytes[0], fill_byte, byte_offset); |
| 2842 | res_bytes[byte_offset] = sign_byte; |
| 2843 | byte_offset += UINT16_C(1); |
| 2844 | memset(&res_bytes[byte_offset], zig_maxInt_u8, size - byte_offset); |
| 2845 | #endif |
| 2846 | } |
| 2847 | |
| 2848 | static inline int8_t zig_signFill_big(const void *arg, bool is_signed, uint16_t bits) { |
| 2849 | const uint8_t *arg_bytes = arg; |
| 2850 | uint16_t byte_offset = 0; |
| 2851 | |
| 2852 | if (!is_signed) return INT8_C(0); |
| 2853 | #if zig_little_endian |
| 2854 | byte_offset = zig_int_bytes(bits) - 1; |
| 2855 | #endif |
| 2856 | return zig_shr_i8(zig_i8_bitCast_u8(arg_bytes[byte_offset], UINT8_C(8)), UINT8_C(7)); |
| 2857 | } |
| 2858 | |
| 2859 | static inline void zig_big_intCast_big(void *res, const void *arg, bool res_is_signed, uint16_t res_bits, bool arg_is_signed, uint16_t arg_bits) { |
| 2860 | uint8_t *res_bytes = res; |
| 2861 | const uint8_t *arg_bytes = arg; |
| 2862 | uint16_t res_size = zig_int_bytes(res_bits); |
| 2863 | uint16_t arg_size = zig_int_bytes(arg_bits); |
| 2864 | uint16_t copy_size = zig_min_u16(res_size, arg_size); |
| 2865 | uint8_t sign_fill = zig_u8_bitCast_i8(zig_signFill_big(arg, arg_is_signed, arg_bits), UINT8_C(8)); |
| 2866 | |
| 2867 | #if zig_little_endian |
| 2868 | memcpy(&res_bytes[0], &arg_bytes[0], copy_size); |
| 2869 | memset(&res_bytes[copy_size], sign_fill, res_size - copy_size); |
| 2870 | #else |
| 2871 | memset(&res_bytes[0], sign_fill, res_size - copy_size); |
| 2872 | memcpy(&res_bytes[res_size - copy_size], &arg_bytes[arg_size - copy_size], copy_size); |
| 2873 | #endif |
| 2874 | } |
| 2875 | |
| 2876 | static inline void zig_big_truncate_big(void *res, const void *arg, bool res_is_signed, uint16_t res_bits, bool arg_is_signed, uint16_t arg_bits) { |
| 2877 | uint8_t *res_bytes = res; |
| 2878 | const uint8_t *arg_bytes = arg; |
| 2879 | uint16_t res_size = zig_int_bytes(res_bits); |
| 2880 | |
| 2881 | if (res_is_signed != arg_is_signed) zig_unreachable(); |
| 2882 | if (res_bits > arg_bits) zig_unreachable(); |
| 2883 | |
| 2884 | if (res_is_signed) { |
| 2885 | uint16_t arg_byte_offset = UINT16_C(0); |
| 2886 | |
| 2887 | #if zig_big_endian |
| 2888 | arg_byte_offset = zig_int_bytes(arg_bits) - res_size; |
| 2889 | #endif |
| 2890 | |
| 2891 | memcpy(&res_bytes[0], &arg_bytes[arg_byte_offset], res_size); |
| 2892 | } else { |
| 2893 | uint16_t res_byte_offset = zig_shr_u16(res_bits - UINT16_C(1), UINT8_C(3)); |
| 2894 | uint16_t arg_byte_offset = res_byte_offset; |
| 2895 | |
| 2896 | #if zig_little_endian |
| 2897 | memcpy(&res_bytes[0], &arg_bytes[0], res_byte_offset); |
| 2898 | #else |
| 2899 | res_byte_offset = res_size - UINT16_C(1) - res_byte_offset; |
| 2900 | arg_byte_offset = zig_int_bytes(arg_bits) - UINT16_C(1) - arg_byte_offset; |
| 2901 | |
| 2902 | memset(&res_bytes[0], zig_minInt_u8, res_byte_offset); |
| 2903 | #endif |
| 2904 | |
| 2905 | res_bytes[res_byte_offset] = zig_u8_truncate_u8( |
| 2906 | arg_bytes[arg_byte_offset], |
| 2907 | zig_u8_truncate_u8(res_bits - UINT16_C(1), UINT8_C(3)) + UINT16_C(1) |
| 2908 | ); |
| 2909 | res_byte_offset += UINT16_C(1); |
| 2910 | arg_byte_offset += UINT16_C(1); |
| 2911 | |
| 2912 | #if zig_little_endian |
| 2913 | memset(&res_bytes[res_byte_offset], zig_minInt_u8, res_size - res_byte_offset); |
| 2914 | #else |
| 2915 | memcpy(&res_bytes[res_byte_offset], &arg_bytes[arg_byte_offset], res_size - res_byte_offset); |
| 2916 | #endif |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | #define zig_big_casts(is, s, w, IntType) \ |
| 2921 | static inline IntType zig_##s##w##_intCast_big(const void *arg, bool arg_is_signed, uint16_t arg_bits) { \ |
| 2922 | IntType res; \ |
| 2923 | zig_big_intCast_big(&res, arg, is, w, arg_is_signed, arg_bits); \ |
| 2924 | return res; \ |
| 2925 | } \ |
| 2926 | \ |
| 2927 | static inline void zig_big_intCast_##s##w(void *res, IntType arg, bool res_is_signed, uint16_t res_bits) { \ |
| 2928 | zig_big_intCast_big(res, &arg, res_is_signed, res_bits, is, w); \ |
| 2929 | } \ |
| 2930 | \ |
| 2931 | static inline IntType zig_##s##w##_truncate_big(const void *arg, uint8_t res_bits, bool arg_is_signed, uint16_t arg_bits) { \ |
| 2932 | IntType res; \ |
| 2933 | zig_big_truncate_big(&res, arg, is, res_bits, arg_is_signed, arg_bits); \ |
| 2934 | return res; \ |
| 2935 | } \ |
| 2936 | \ |
| 2937 | static inline void zig_big_truncate_##s##w(void *res, IntType arg, bool res_is_signed, uint16_t res_bits) { \ |
| 2938 | zig_big_truncate_big(res, &arg, res_is_signed, res_bits, is, w); \ |
| 2939 | } |
| 2940 | zig_big_casts(false, u, 8, uint8_t) |
| 2941 | zig_big_casts(true , i, 8, int8_t) |
| 2942 | zig_big_casts(false, u, 16, uint16_t) |
| 2943 | zig_big_casts(true , i, 16, int16_t) |
| 2944 | zig_big_casts(false, u, 32, uint32_t) |
| 2945 | zig_big_casts(true , i, 32, int32_t) |
| 2946 | zig_big_casts(false, u, 64, uint64_t) |
| 2947 | zig_big_casts(true , i, 64, int64_t) |
| 2948 | zig_big_casts(false, u, 128, zig_u128) |
| 2949 | zig_big_casts(true , i, 128, zig_i128) |
| 2950 | |
| 2951 | static inline void zig_big_bitCast_big(void *res, const void *arg, bool res_is_signed, uint16_t bits) { |
| 2952 | uint8_t *res_bytes = res; |
| 2953 | const uint8_t *arg_bytes = arg; |
| 2954 | uint16_t size = zig_int_bytes(bits); |
| 2955 | uint16_t byte_offset = zig_shr_u16(bits - UINT16_C(1), UINT8_C(3)); |
| 2956 | uint16_t remainder_bits = zig_u8_truncate_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT8_C(1); |
| 2957 | uint8_t sign_byte; |
| 2958 | uint8_t fill_byte; |
| 2959 | |
| 2960 | #if zig_big_endian |
| 2961 | byte_offset = size - UINT16_C(1) - byte_offset; |
| 2962 | #endif |
| 2963 | |
| 2964 | if (res_is_signed) { |
| 2965 | int8_t signed_sign_byte = zig_i8_bitCast_u8(arg_bytes[byte_offset], remainder_bits); |
| 2966 | |
| 2967 | sign_byte = zig_u8_bitCast_i8(signed_sign_byte, UINT8_C(8)); |
| 2968 | fill_byte = zig_u8_bitCast_i8(zig_shr_i8(signed_sign_byte, UINT8_C(7)), UINT8_C(8)); |
| 2969 | } else { |
| 2970 | sign_byte = zig_u8_bitCast_u8(arg_bytes[byte_offset], remainder_bits); |
| 2971 | fill_byte = UINT8_C(0); |
| 2972 | } |
| 2973 | |
| 2974 | #if zig_little_endian |
| 2975 | memcpy(&res_bytes[0], &arg_bytes[0], byte_offset); |
| 2976 | res_bytes[byte_offset] = sign_byte; |
| 2977 | byte_offset += UINT16_C(1); |
| 2978 | memset(&res_bytes[byte_offset], fill_byte, size - byte_offset); |
| 2979 | #else |
| 2980 | memset(&res_bytes[0], fill_byte, byte_offset); |
| 2981 | res_bytes[byte_offset] = sign_byte; |
| 2982 | byte_offset += UINT16_C(1); |
| 2983 | memcpy(&res_bytes[byte_offset], &arg_bytes[byte_offset], size - byte_offset); |
| 2984 | #endif |
| 2985 | } |
| 2986 | |
| 2987 | static inline int32_t zig_cmp_big_u8(const void *lhs, uint8_t rhs, bool is_signed, uint16_t bits) { |
| 2421 | 2988 | const uint8_t *lhs_bytes = lhs; |
| 2422 | | const uint8_t *rhs_bytes = rhs; |
| 2423 | 2989 | uint16_t byte_offset = 0; |
| 2424 | 2990 | bool do_signed = is_signed; |
| 2425 | 2991 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| ... | ... | @@ -2429,6 +2995,7 @@ static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_sign |
| 2429 | 2995 | #endif |
| 2430 | 2996 | |
| 2431 | 2997 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 2998 | uint8_t rhs_byte = remaining_bytes == 128 / CHAR_BIT ? rhs : UINT8_C(0); |
| 2432 | 2999 | int32_t limb_cmp; |
| 2433 | 3000 | |
| 2434 | 3001 | #if zig_little_endian |
| ... | ... | @@ -2437,18 +3004,16 @@ static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_sign |
| 2437 | 3004 | |
| 2438 | 3005 | if (do_signed) { |
| 2439 | 3006 | zig_i128 lhs_limb; |
| 2440 | | zig_i128 rhs_limb; |
| 3007 | zig_i128 rhs_limb = zig_i128_intCast_u8(rhs_byte); |
| 2441 | 3008 | |
| 2442 | 3009 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2443 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2444 | 3010 | limb_cmp = zig_cmp_i128(lhs_limb, rhs_limb); |
| 2445 | 3011 | do_signed = false; |
| 2446 | 3012 | } else { |
| 2447 | 3013 | zig_u128 lhs_limb; |
| 2448 | | zig_u128 rhs_limb; |
| 3014 | zig_u128 rhs_limb = zig_u128_intCast_u8(rhs_byte); |
| 2449 | 3015 | |
| 2450 | 3016 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2451 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2452 | 3017 | limb_cmp = zig_cmp_u128(lhs_limb, rhs_limb); |
| 2453 | 3018 | } |
| 2454 | 3019 | |
| ... | ... | @@ -2461,24 +3026,24 @@ static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_sign |
| 2461 | 3026 | } |
| 2462 | 3027 | |
| 2463 | 3028 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 3029 | uint8_t rhs_byte = remaining_bytes == 64 / CHAR_BIT ? rhs : UINT8_C(0); |
| 3030 | |
| 2464 | 3031 | #if zig_little_endian |
| 2465 | 3032 | byte_offset -= 64 / CHAR_BIT; |
| 2466 | 3033 | #endif |
| 2467 | 3034 | |
| 2468 | 3035 | if (do_signed) { |
| 2469 | 3036 | int64_t lhs_limb; |
| 2470 | | int64_t rhs_limb; |
| 3037 | int64_t rhs_limb = zig_i64_intCast_u8(rhs_byte); |
| 2471 | 3038 | |
| 2472 | 3039 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2473 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2474 | 3040 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 2475 | 3041 | do_signed = false; |
| 2476 | 3042 | } else { |
| 2477 | 3043 | uint64_t lhs_limb; |
| 2478 | | uint64_t rhs_limb; |
| 3044 | uint64_t rhs_limb = zig_u64_intCast_u8(rhs_byte); |
| 2479 | 3045 | |
| 2480 | 3046 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2481 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2482 | 3047 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 2483 | 3048 | } |
| 2484 | 3049 | |
| ... | ... | @@ -2490,24 +3055,24 @@ static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_sign |
| 2490 | 3055 | } |
| 2491 | 3056 | |
| 2492 | 3057 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 3058 | uint8_t rhs_byte = remaining_bytes == 32 / CHAR_BIT ? rhs : UINT8_C(0); |
| 3059 | |
| 2493 | 3060 | #if zig_little_endian |
| 2494 | 3061 | byte_offset -= 32 / CHAR_BIT; |
| 2495 | 3062 | #endif |
| 2496 | 3063 | |
| 2497 | 3064 | if (do_signed) { |
| 2498 | 3065 | int32_t lhs_limb; |
| 2499 | | int32_t rhs_limb; |
| 3066 | int32_t rhs_limb = zig_i32_intCast_u8(rhs_byte); |
| 2500 | 3067 | |
| 2501 | 3068 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2502 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2503 | 3069 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 2504 | 3070 | do_signed = false; |
| 2505 | 3071 | } else { |
| 2506 | 3072 | uint32_t lhs_limb; |
| 2507 | | uint32_t rhs_limb; |
| 3073 | uint32_t rhs_limb = zig_u32_intCast_u8(rhs_byte); |
| 2508 | 3074 | |
| 2509 | 3075 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2510 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2511 | 3076 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 2512 | 3077 | } |
| 2513 | 3078 | |
| ... | ... | @@ -2519,24 +3084,24 @@ static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_sign |
| 2519 | 3084 | } |
| 2520 | 3085 | |
| 2521 | 3086 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 3087 | uint8_t rhs_byte = remaining_bytes == 16 / CHAR_BIT ? rhs : UINT8_C(0); |
| 3088 | |
| 2522 | 3089 | #if zig_little_endian |
| 2523 | 3090 | byte_offset -= 16 / CHAR_BIT; |
| 2524 | 3091 | #endif |
| 2525 | 3092 | |
| 2526 | 3093 | if (do_signed) { |
| 2527 | 3094 | int16_t lhs_limb; |
| 2528 | | int16_t rhs_limb; |
| 3095 | int16_t rhs_limb = zig_i16_intCast_u8(rhs_byte); |
| 2529 | 3096 | |
| 2530 | 3097 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2531 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2532 | 3098 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 2533 | 3099 | do_signed = false; |
| 2534 | 3100 | } else { |
| 2535 | 3101 | uint16_t lhs_limb; |
| 2536 | | uint16_t rhs_limb; |
| 3102 | uint16_t rhs_limb = zig_u16_intCast_u8(rhs_byte); |
| 2537 | 3103 | |
| 2538 | 3104 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2539 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2540 | 3105 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 2541 | 3106 | } |
| 2542 | 3107 | |
| ... | ... | @@ -2548,24 +3113,26 @@ static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_sign |
| 2548 | 3113 | } |
| 2549 | 3114 | |
| 2550 | 3115 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 3116 | uint8_t rhs_byte = remaining_bytes == 16 / CHAR_BIT ? rhs : UINT8_C(0); |
| 3117 | |
| 2551 | 3118 | #if zig_little_endian |
| 2552 | 3119 | byte_offset -= 8 / CHAR_BIT; |
| 2553 | 3120 | #endif |
| 2554 | 3121 | |
| 2555 | 3122 | if (do_signed) { |
| 2556 | 3123 | int8_t lhs_limb; |
| 2557 | | int8_t rhs_limb; |
| 3124 | int16_t lhs_cmp_limb; |
| 3125 | int16_t rhs_cmp_limb = zig_i16_intCast_u8(rhs_byte); |
| 2558 | 3126 | |
| 2559 | 3127 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2560 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2561 | | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 3128 | lhs_cmp_limb = zig_i16_intCast_i8(lhs_limb); |
| 3129 | if (lhs_cmp_limb != rhs_cmp_limb) return (lhs_cmp_limb > rhs_cmp_limb) - (lhs_cmp_limb < rhs_cmp_limb); |
| 2562 | 3130 | do_signed = false; |
| 2563 | 3131 | } else { |
| 2564 | 3132 | uint8_t lhs_limb; |
| 2565 | | uint8_t rhs_limb; |
| 3133 | uint8_t rhs_limb = rhs_byte; |
| 2566 | 3134 | |
| 2567 | 3135 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2568 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2569 | 3136 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 2570 | 3137 | } |
| 2571 | 3138 | |
| ... | ... | @@ -2579,148 +3146,472 @@ static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_sign |
| 2579 | 3146 | return 0; |
| 2580 | 3147 | } |
| 2581 | 3148 | |
| 2582 | | static inline void zig_and_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 2583 | | uint8_t *res_bytes = res; |
| 3149 | static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 2584 | 3150 | const uint8_t *lhs_bytes = lhs; |
| 2585 | 3151 | const uint8_t *rhs_bytes = rhs; |
| 2586 | 3152 | uint16_t byte_offset = 0; |
| 3153 | bool do_signed = is_signed; |
| 2587 | 3154 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 2588 | | (void)is_signed; |
| 3155 | |
| 3156 | #if zig_little_endian |
| 3157 | byte_offset = remaining_bytes; |
| 3158 | #endif |
| 2589 | 3159 | |
| 2590 | 3160 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 2591 | | zig_u128 res_limb; |
| 2592 | | zig_u128 lhs_limb; |
| 2593 | | zig_u128 rhs_limb; |
| 3161 | int32_t limb_cmp; |
| 2594 | 3162 | |
| 2595 | | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2596 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2597 | | res_limb = zig_and_u128(lhs_limb, rhs_limb); |
| 2598 | | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3163 | #if zig_little_endian |
| 3164 | byte_offset -= 128 / CHAR_BIT; |
| 3165 | #endif |
| 3166 | |
| 3167 | if (do_signed) { |
| 3168 | zig_i128 lhs_limb; |
| 3169 | zig_i128 rhs_limb; |
| 3170 | |
| 3171 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3172 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3173 | limb_cmp = zig_cmp_i128(lhs_limb, rhs_limb); |
| 3174 | do_signed = false; |
| 3175 | } else { |
| 3176 | zig_u128 lhs_limb; |
| 3177 | zig_u128 rhs_limb; |
| 3178 | |
| 3179 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3180 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3181 | limb_cmp = zig_cmp_u128(lhs_limb, rhs_limb); |
| 3182 | } |
| 2599 | 3183 | |
| 3184 | if (limb_cmp != 0) return limb_cmp; |
| 2600 | 3185 | remaining_bytes -= 128 / CHAR_BIT; |
| 3186 | |
| 3187 | #if zig_big_endian |
| 2601 | 3188 | byte_offset += 128 / CHAR_BIT; |
| 3189 | #endif |
| 2602 | 3190 | } |
| 2603 | 3191 | |
| 2604 | 3192 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 2605 | | uint64_t res_limb; |
| 2606 | | uint64_t lhs_limb; |
| 2607 | | uint64_t rhs_limb; |
| 3193 | #if zig_little_endian |
| 3194 | byte_offset -= 64 / CHAR_BIT; |
| 3195 | #endif |
| 2608 | 3196 | |
| 2609 | | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2610 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2611 | | res_limb = zig_and_u64(lhs_limb, rhs_limb); |
| 2612 | | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3197 | if (do_signed) { |
| 3198 | int64_t lhs_limb; |
| 3199 | int64_t rhs_limb; |
| 3200 | |
| 3201 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3202 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3203 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 3204 | do_signed = false; |
| 3205 | } else { |
| 3206 | uint64_t lhs_limb; |
| 3207 | uint64_t rhs_limb; |
| 3208 | |
| 3209 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3210 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3211 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 3212 | } |
| 2613 | 3213 | |
| 2614 | 3214 | remaining_bytes -= 64 / CHAR_BIT; |
| 3215 | |
| 3216 | #if zig_big_endian |
| 2615 | 3217 | byte_offset += 64 / CHAR_BIT; |
| 3218 | #endif |
| 2616 | 3219 | } |
| 2617 | 3220 | |
| 2618 | 3221 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 2619 | | uint32_t res_limb; |
| 2620 | | uint32_t lhs_limb; |
| 2621 | | uint32_t rhs_limb; |
| 3222 | #if zig_little_endian |
| 3223 | byte_offset -= 32 / CHAR_BIT; |
| 3224 | #endif |
| 2622 | 3225 | |
| 2623 | | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2624 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2625 | | res_limb = zig_and_u32(lhs_limb, rhs_limb); |
| 2626 | | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3226 | if (do_signed) { |
| 3227 | int32_t lhs_limb; |
| 3228 | int32_t rhs_limb; |
| 3229 | |
| 3230 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3231 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3232 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 3233 | do_signed = false; |
| 3234 | } else { |
| 3235 | uint32_t lhs_limb; |
| 3236 | uint32_t rhs_limb; |
| 3237 | |
| 3238 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3239 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3240 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 3241 | } |
| 2627 | 3242 | |
| 2628 | 3243 | remaining_bytes -= 32 / CHAR_BIT; |
| 3244 | |
| 3245 | #if zig_big_endian |
| 2629 | 3246 | byte_offset += 32 / CHAR_BIT; |
| 3247 | #endif |
| 2630 | 3248 | } |
| 2631 | 3249 | |
| 2632 | 3250 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 2633 | | uint16_t res_limb; |
| 2634 | | uint16_t lhs_limb; |
| 2635 | | uint16_t rhs_limb; |
| 3251 | #if zig_little_endian |
| 3252 | byte_offset -= 16 / CHAR_BIT; |
| 3253 | #endif |
| 2636 | 3254 | |
| 2637 | | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2638 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2639 | | res_limb = zig_and_u16(lhs_limb, rhs_limb); |
| 2640 | | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3255 | if (do_signed) { |
| 3256 | int16_t lhs_limb; |
| 3257 | int16_t rhs_limb; |
| 3258 | |
| 3259 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3260 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3261 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 3262 | do_signed = false; |
| 3263 | } else { |
| 3264 | uint16_t lhs_limb; |
| 3265 | uint16_t rhs_limb; |
| 3266 | |
| 3267 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3268 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3269 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 3270 | } |
| 2641 | 3271 | |
| 2642 | 3272 | remaining_bytes -= 16 / CHAR_BIT; |
| 3273 | |
| 3274 | #if zig_big_endian |
| 2643 | 3275 | byte_offset += 16 / CHAR_BIT; |
| 3276 | #endif |
| 2644 | 3277 | } |
| 2645 | 3278 | |
| 2646 | 3279 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 2647 | | uint8_t res_limb; |
| 2648 | | uint8_t lhs_limb; |
| 2649 | | uint8_t rhs_limb; |
| 3280 | #if zig_little_endian |
| 3281 | byte_offset -= 8 / CHAR_BIT; |
| 3282 | #endif |
| 2650 | 3283 | |
| 2651 | | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2652 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2653 | | res_limb = zig_and_u8(lhs_limb, rhs_limb); |
| 2654 | | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3284 | if (do_signed) { |
| 3285 | int8_t lhs_limb; |
| 3286 | int8_t rhs_limb; |
| 3287 | |
| 3288 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3289 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3290 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 3291 | do_signed = false; |
| 3292 | } else { |
| 3293 | uint8_t lhs_limb; |
| 3294 | uint8_t rhs_limb; |
| 3295 | |
| 3296 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3297 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3298 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 3299 | } |
| 2655 | 3300 | |
| 2656 | 3301 | remaining_bytes -= 8 / CHAR_BIT; |
| 3302 | |
| 3303 | #if zig_big_endian |
| 2657 | 3304 | byte_offset += 8 / CHAR_BIT; |
| 3305 | #endif |
| 2658 | 3306 | } |
| 3307 | |
| 3308 | return 0; |
| 2659 | 3309 | } |
| 2660 | 3310 | |
| 2661 | | static inline void zig_or_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 3311 | static inline void zig_not_big(void *res, const void *arg, bool is_signed, uint16_t bits) { |
| 2662 | 3312 | uint8_t *res_bytes = res; |
| 2663 | | const uint8_t *lhs_bytes = lhs; |
| 2664 | | const uint8_t *rhs_bytes = rhs; |
| 3313 | const uint8_t *arg_bytes = arg; |
| 2665 | 3314 | uint16_t byte_offset = 0; |
| 2666 | 3315 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 2667 | | (void)is_signed; |
| 3316 | uint8_t top_bits = zig_u8_intCast_u16(remaining_bytes * CHAR_BIT - bits); |
| 3317 | |
| 3318 | #if zig_big_endian |
| 3319 | byte_offset = remaining_bytes; |
| 3320 | #endif |
| 2668 | 3321 | |
| 2669 | 3322 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 2670 | | zig_u128 res_limb; |
| 2671 | | zig_u128 lhs_limb; |
| 2672 | | zig_u128 rhs_limb; |
| 3323 | uint8_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0); |
| 2673 | 3324 | |
| 2674 | | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2675 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2676 | | res_limb = zig_or_u128(lhs_limb, rhs_limb); |
| 2677 | | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3325 | #if zig_big_endian |
| 3326 | byte_offset -= 128 / CHAR_BIT; |
| 3327 | #endif |
| 3328 | |
| 3329 | if (remaining_bytes != 128 / CHAR_BIT || is_signed) { |
| 3330 | zig_i128 res_limb; |
| 3331 | zig_i128 arg_limb; |
| 3332 | |
| 3333 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 3334 | res_limb = zig_not_i128(arg_limb, limb_bits); |
| 3335 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3336 | } else { |
| 3337 | zig_u128 res_limb; |
| 3338 | zig_u128 arg_limb; |
| 3339 | |
| 3340 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 3341 | res_limb = zig_not_u128(arg_limb, limb_bits); |
| 3342 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3343 | } |
| 2678 | 3344 | |
| 2679 | 3345 | remaining_bytes -= 128 / CHAR_BIT; |
| 3346 | |
| 3347 | #if zig_little_endian |
| 2680 | 3348 | byte_offset += 128 / CHAR_BIT; |
| 3349 | #endif |
| 2681 | 3350 | } |
| 2682 | 3351 | |
| 2683 | 3352 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 2684 | | uint64_t res_limb; |
| 2685 | | uint64_t lhs_limb; |
| 2686 | | uint64_t rhs_limb; |
| 3353 | uint8_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0); |
| 2687 | 3354 | |
| 2688 | | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2689 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2690 | | res_limb = zig_or_u64(lhs_limb, rhs_limb); |
| 2691 | | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3355 | #if zig_big_endian |
| 3356 | byte_offset -= 64 / CHAR_BIT; |
| 3357 | #endif |
| 3358 | |
| 3359 | if (remaining_bytes != 64 / CHAR_BIT || is_signed) { |
| 3360 | int64_t res_limb; |
| 3361 | int64_t arg_limb; |
| 3362 | |
| 3363 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 3364 | res_limb = zig_not_i64(arg_limb, limb_bits); |
| 3365 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3366 | } else { |
| 3367 | uint64_t res_limb; |
| 3368 | uint64_t arg_limb; |
| 3369 | |
| 3370 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 3371 | res_limb = zig_not_u64(arg_limb, limb_bits); |
| 3372 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3373 | } |
| 2692 | 3374 | |
| 2693 | 3375 | remaining_bytes -= 64 / CHAR_BIT; |
| 3376 | |
| 3377 | #if zig_little_endian |
| 2694 | 3378 | byte_offset += 64 / CHAR_BIT; |
| 3379 | #endif |
| 2695 | 3380 | } |
| 2696 | 3381 | |
| 2697 | 3382 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 2698 | | uint32_t res_limb; |
| 2699 | | uint32_t lhs_limb; |
| 2700 | | uint32_t rhs_limb; |
| 3383 | uint8_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0); |
| 2701 | 3384 | |
| 2702 | | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2703 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2704 | | res_limb = zig_or_u32(lhs_limb, rhs_limb); |
| 2705 | | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3385 | #if zig_big_endian |
| 3386 | byte_offset -= 32 / CHAR_BIT; |
| 3387 | #endif |
| 3388 | |
| 3389 | if (remaining_bytes != 32 / CHAR_BIT || is_signed) { |
| 3390 | int32_t res_limb; |
| 3391 | int32_t arg_limb; |
| 3392 | |
| 3393 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 3394 | res_limb = zig_not_i32(arg_limb, limb_bits); |
| 3395 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3396 | } else { |
| 3397 | uint32_t res_limb; |
| 3398 | uint32_t arg_limb; |
| 3399 | |
| 3400 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 3401 | res_limb = zig_not_u32(arg_limb, limb_bits); |
| 3402 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3403 | } |
| 2706 | 3404 | |
| 2707 | 3405 | remaining_bytes -= 32 / CHAR_BIT; |
| 3406 | |
| 3407 | #if zig_little_endian |
| 2708 | 3408 | byte_offset += 32 / CHAR_BIT; |
| 3409 | #endif |
| 2709 | 3410 | } |
| 2710 | 3411 | |
| 2711 | 3412 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 2712 | | uint16_t res_limb; |
| 2713 | | uint16_t lhs_limb; |
| 2714 | | uint16_t rhs_limb; |
| 3413 | uint8_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0); |
| 2715 | 3414 | |
| 2716 | | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2717 | | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2718 | | res_limb = zig_or_u16(lhs_limb, rhs_limb); |
| 2719 | | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3415 | #if zig_big_endian |
| 3416 | byte_offset -= 16 / CHAR_BIT; |
| 3417 | #endif |
| 2720 | 3418 | |
| 2721 | | remaining_bytes -= 16 / CHAR_BIT; |
| 2722 | | byte_offset += 16 / CHAR_BIT; |
| 2723 | | } |
| 3419 | if (remaining_bytes != 16 / CHAR_BIT || is_signed) { |
| 3420 | int16_t res_limb; |
| 3421 | int16_t arg_limb; |
| 3422 | |
| 3423 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 3424 | res_limb = zig_not_i16(arg_limb, limb_bits); |
| 3425 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3426 | } else { |
| 3427 | uint16_t res_limb; |
| 3428 | uint16_t arg_limb; |
| 3429 | |
| 3430 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 3431 | res_limb = zig_not_u16(arg_limb, limb_bits); |
| 3432 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3433 | } |
| 3434 | |
| 3435 | remaining_bytes -= 16 / CHAR_BIT; |
| 3436 | |
| 3437 | #if zig_little_endian |
| 3438 | byte_offset += 16 / CHAR_BIT; |
| 3439 | #endif |
| 3440 | } |
| 3441 | |
| 3442 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 3443 | uint8_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0); |
| 3444 | |
| 3445 | #if zig_big_endian |
| 3446 | byte_offset -= 8 / CHAR_BIT; |
| 3447 | #endif |
| 3448 | |
| 3449 | if (remaining_bytes != 8 / CHAR_BIT || is_signed) { |
| 3450 | int8_t res_limb; |
| 3451 | int8_t arg_limb; |
| 3452 | |
| 3453 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 3454 | res_limb = zig_not_i8(arg_limb, limb_bits); |
| 3455 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3456 | } else { |
| 3457 | uint8_t res_limb; |
| 3458 | uint8_t arg_limb; |
| 3459 | |
| 3460 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 3461 | res_limb = zig_not_u8(arg_limb, limb_bits); |
| 3462 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3463 | } |
| 3464 | |
| 3465 | remaining_bytes -= 8 / CHAR_BIT; |
| 3466 | |
| 3467 | #if zig_little_endian |
| 3468 | byte_offset += 8 / CHAR_BIT; |
| 3469 | #endif |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | static inline void zig_and_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 3474 | uint8_t *res_bytes = res; |
| 3475 | const uint8_t *lhs_bytes = lhs; |
| 3476 | const uint8_t *rhs_bytes = rhs; |
| 3477 | uint16_t byte_offset = 0; |
| 3478 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 3479 | (void)is_signed; |
| 3480 | |
| 3481 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 3482 | zig_u128 res_limb; |
| 3483 | zig_u128 lhs_limb; |
| 3484 | zig_u128 rhs_limb; |
| 3485 | |
| 3486 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3487 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3488 | res_limb = zig_and_u128(lhs_limb, rhs_limb); |
| 3489 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3490 | |
| 3491 | remaining_bytes -= 128 / CHAR_BIT; |
| 3492 | byte_offset += 128 / CHAR_BIT; |
| 3493 | } |
| 3494 | |
| 3495 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 3496 | uint64_t res_limb; |
| 3497 | uint64_t lhs_limb; |
| 3498 | uint64_t rhs_limb; |
| 3499 | |
| 3500 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3501 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3502 | res_limb = zig_and_u64(lhs_limb, rhs_limb); |
| 3503 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3504 | |
| 3505 | remaining_bytes -= 64 / CHAR_BIT; |
| 3506 | byte_offset += 64 / CHAR_BIT; |
| 3507 | } |
| 3508 | |
| 3509 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 3510 | uint32_t res_limb; |
| 3511 | uint32_t lhs_limb; |
| 3512 | uint32_t rhs_limb; |
| 3513 | |
| 3514 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3515 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3516 | res_limb = zig_and_u32(lhs_limb, rhs_limb); |
| 3517 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3518 | |
| 3519 | remaining_bytes -= 32 / CHAR_BIT; |
| 3520 | byte_offset += 32 / CHAR_BIT; |
| 3521 | } |
| 3522 | |
| 3523 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 3524 | uint16_t res_limb; |
| 3525 | uint16_t lhs_limb; |
| 3526 | uint16_t rhs_limb; |
| 3527 | |
| 3528 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3529 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3530 | res_limb = zig_and_u16(lhs_limb, rhs_limb); |
| 3531 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3532 | |
| 3533 | remaining_bytes -= 16 / CHAR_BIT; |
| 3534 | byte_offset += 16 / CHAR_BIT; |
| 3535 | } |
| 3536 | |
| 3537 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 3538 | uint8_t res_limb; |
| 3539 | uint8_t lhs_limb; |
| 3540 | uint8_t rhs_limb; |
| 3541 | |
| 3542 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3543 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3544 | res_limb = zig_and_u8(lhs_limb, rhs_limb); |
| 3545 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3546 | |
| 3547 | remaining_bytes -= 8 / CHAR_BIT; |
| 3548 | byte_offset += 8 / CHAR_BIT; |
| 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | static inline void zig_or_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 3553 | uint8_t *res_bytes = res; |
| 3554 | const uint8_t *lhs_bytes = lhs; |
| 3555 | const uint8_t *rhs_bytes = rhs; |
| 3556 | uint16_t byte_offset = 0; |
| 3557 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 3558 | (void)is_signed; |
| 3559 | |
| 3560 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 3561 | zig_u128 res_limb; |
| 3562 | zig_u128 lhs_limb; |
| 3563 | zig_u128 rhs_limb; |
| 3564 | |
| 3565 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3566 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3567 | res_limb = zig_or_u128(lhs_limb, rhs_limb); |
| 3568 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3569 | |
| 3570 | remaining_bytes -= 128 / CHAR_BIT; |
| 3571 | byte_offset += 128 / CHAR_BIT; |
| 3572 | } |
| 3573 | |
| 3574 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 3575 | uint64_t res_limb; |
| 3576 | uint64_t lhs_limb; |
| 3577 | uint64_t rhs_limb; |
| 3578 | |
| 3579 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3580 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3581 | res_limb = zig_or_u64(lhs_limb, rhs_limb); |
| 3582 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3583 | |
| 3584 | remaining_bytes -= 64 / CHAR_BIT; |
| 3585 | byte_offset += 64 / CHAR_BIT; |
| 3586 | } |
| 3587 | |
| 3588 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 3589 | uint32_t res_limb; |
| 3590 | uint32_t lhs_limb; |
| 3591 | uint32_t rhs_limb; |
| 3592 | |
| 3593 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3594 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3595 | res_limb = zig_or_u32(lhs_limb, rhs_limb); |
| 3596 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3597 | |
| 3598 | remaining_bytes -= 32 / CHAR_BIT; |
| 3599 | byte_offset += 32 / CHAR_BIT; |
| 3600 | } |
| 3601 | |
| 3602 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 3603 | uint16_t res_limb; |
| 3604 | uint16_t lhs_limb; |
| 3605 | uint16_t rhs_limb; |
| 3606 | |
| 3607 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 3608 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 3609 | res_limb = zig_or_u16(lhs_limb, rhs_limb); |
| 3610 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3611 | |
| 3612 | remaining_bytes -= 16 / CHAR_BIT; |
| 3613 | byte_offset += 16 / CHAR_BIT; |
| 3614 | } |
| 2724 | 3615 | |
| 2725 | 3616 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 2726 | 3617 | uint8_t res_limb; |
| ... | ... | @@ -2816,13 +3707,415 @@ static inline void zig_xor_big(void *res, const void *lhs, const void *rhs, bool |
| 2816 | 3707 | } |
| 2817 | 3708 | } |
| 2818 | 3709 | |
| 3710 | static inline void zig_increment_big(void *res, bool is_signed, uint16_t bits) { |
| 3711 | uint8_t *res_bytes = res; |
| 3712 | uint16_t byte_offset = 0; |
| 3713 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 3714 | uint8_t top_bits = zig_u8_intCast_u16(remaining_bytes * CHAR_BIT - bits); |
| 3715 | |
| 3716 | #if zig_big_endian |
| 3717 | byte_offset = remaining_bytes; |
| 3718 | #endif |
| 3719 | |
| 3720 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 3721 | uint8_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0); |
| 3722 | |
| 3723 | #if zig_big_endian |
| 3724 | byte_offset -= 128 / CHAR_BIT; |
| 3725 | #endif |
| 3726 | |
| 3727 | { |
| 3728 | zig_u128 res_limb; |
| 3729 | bool limb_overflow; |
| 3730 | |
| 3731 | memcpy(&res_limb, &res_bytes[byte_offset], sizeof(res_limb)); |
| 3732 | limb_overflow = zig_addo_u128(&res_limb, res_limb, zig_make_u128(UINT64_C(0), UINT64_C(1)), limb_bits); |
| 3733 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3734 | if (!limb_overflow) return; |
| 3735 | } |
| 3736 | |
| 3737 | remaining_bytes -= 128 / CHAR_BIT; |
| 3738 | |
| 3739 | #if zig_little_endian |
| 3740 | byte_offset += 128 / CHAR_BIT; |
| 3741 | #endif |
| 3742 | } |
| 3743 | |
| 3744 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 3745 | uint8_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0); |
| 3746 | |
| 3747 | #if zig_big_endian |
| 3748 | byte_offset -= 64 / CHAR_BIT; |
| 3749 | #endif |
| 3750 | |
| 3751 | { |
| 3752 | uint64_t res_limb; |
| 3753 | bool limb_overflow; |
| 3754 | |
| 3755 | memcpy(&res_limb, &res_bytes[byte_offset], sizeof(res_limb)); |
| 3756 | limb_overflow = zig_addo_u64(&res_limb, res_limb, UINT64_C(1), limb_bits); |
| 3757 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3758 | if (!limb_overflow) return; |
| 3759 | } |
| 3760 | |
| 3761 | remaining_bytes -= 64 / CHAR_BIT; |
| 3762 | |
| 3763 | #if zig_little_endian |
| 3764 | byte_offset += 64 / CHAR_BIT; |
| 3765 | #endif |
| 3766 | } |
| 3767 | |
| 3768 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 3769 | uint8_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0); |
| 3770 | |
| 3771 | #if zig_big_endian |
| 3772 | byte_offset -= 32 / CHAR_BIT; |
| 3773 | #endif |
| 3774 | |
| 3775 | { |
| 3776 | uint32_t res_limb; |
| 3777 | bool limb_overflow; |
| 3778 | |
| 3779 | memcpy(&res_limb, &res_bytes[byte_offset], sizeof(res_limb)); |
| 3780 | limb_overflow = zig_addo_u32(&res_limb, res_limb, UINT32_C(1), limb_bits); |
| 3781 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3782 | if (!limb_overflow) return; |
| 3783 | } |
| 3784 | |
| 3785 | remaining_bytes -= 32 / CHAR_BIT; |
| 3786 | |
| 3787 | #if zig_little_endian |
| 3788 | byte_offset += 32 / CHAR_BIT; |
| 3789 | #endif |
| 3790 | } |
| 3791 | |
| 3792 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 3793 | uint8_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0); |
| 3794 | |
| 3795 | #if zig_big_endian |
| 3796 | byte_offset -= 16 / CHAR_BIT; |
| 3797 | #endif |
| 3798 | |
| 3799 | { |
| 3800 | uint16_t res_limb; |
| 3801 | bool limb_overflow; |
| 3802 | |
| 3803 | memcpy(&res_limb, &res_bytes[byte_offset], sizeof(res_limb)); |
| 3804 | limb_overflow = zig_addo_u16(&res_limb, res_limb, UINT16_C(1), limb_bits); |
| 3805 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3806 | if (!limb_overflow) return; |
| 3807 | } |
| 3808 | |
| 3809 | remaining_bytes -= 16 / CHAR_BIT; |
| 3810 | |
| 3811 | #if zig_little_endian |
| 3812 | byte_offset += 16 / CHAR_BIT; |
| 3813 | #endif |
| 3814 | } |
| 3815 | |
| 3816 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 3817 | uint8_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0); |
| 3818 | |
| 3819 | #if zig_big_endian |
| 3820 | byte_offset -= 8 / CHAR_BIT; |
| 3821 | #endif |
| 3822 | |
| 3823 | { |
| 3824 | uint8_t res_limb; |
| 3825 | bool limb_overflow; |
| 3826 | |
| 3827 | memcpy(&res_limb, &res_bytes[byte_offset], sizeof(res_limb)); |
| 3828 | limb_overflow = zig_addo_u8(&res_limb, res_limb, UINT8_C(1), limb_bits); |
| 3829 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3830 | if (!limb_overflow) return; |
| 3831 | } |
| 3832 | |
| 3833 | remaining_bytes -= 8 / CHAR_BIT; |
| 3834 | |
| 3835 | #if zig_little_endian |
| 3836 | byte_offset += 8 / CHAR_BIT; |
| 3837 | #endif |
| 3838 | } |
| 3839 | } |
| 3840 | |
| 3841 | static inline void zig_decrement_big(void *res, bool is_signed, uint16_t bits) { |
| 3842 | uint8_t *res_bytes = res; |
| 3843 | uint16_t byte_offset = 0; |
| 3844 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 3845 | uint8_t top_bits = zig_u8_intCast_u16(remaining_bytes * CHAR_BIT - bits); |
| 3846 | |
| 3847 | #if zig_big_endian |
| 3848 | byte_offset = remaining_bytes; |
| 3849 | #endif |
| 3850 | |
| 3851 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 3852 | uint8_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0); |
| 3853 | |
| 3854 | #if zig_big_endian |
| 3855 | byte_offset -= 128 / CHAR_BIT; |
| 3856 | #endif |
| 3857 | |
| 3858 | { |
| 3859 | zig_u128 res_limb; |
| 3860 | bool limb_overflow; |
| 3861 | |
| 3862 | memcpy(&res_limb, &res_bytes[byte_offset], sizeof(res_limb)); |
| 3863 | limb_overflow = zig_subo_u128(&res_limb, res_limb, zig_make_u128(UINT64_C(0), UINT64_C(1)), limb_bits); |
| 3864 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3865 | if (!limb_overflow) return; |
| 3866 | } |
| 3867 | |
| 3868 | remaining_bytes -= 128 / CHAR_BIT; |
| 3869 | |
| 3870 | #if zig_little_endian |
| 3871 | byte_offset += 128 / CHAR_BIT; |
| 3872 | #endif |
| 3873 | } |
| 3874 | |
| 3875 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 3876 | uint8_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0); |
| 3877 | |
| 3878 | #if zig_big_endian |
| 3879 | byte_offset -= 64 / CHAR_BIT; |
| 3880 | #endif |
| 3881 | |
| 3882 | { |
| 3883 | uint64_t res_limb; |
| 3884 | bool limb_overflow; |
| 3885 | |
| 3886 | memcpy(&res_limb, &res_bytes[byte_offset], sizeof(res_limb)); |
| 3887 | limb_overflow = zig_subo_u64(&res_limb, res_limb, UINT64_C(1), limb_bits); |
| 3888 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3889 | if (!limb_overflow) return; |
| 3890 | } |
| 3891 | |
| 3892 | remaining_bytes -= 64 / CHAR_BIT; |
| 3893 | |
| 3894 | #if zig_little_endian |
| 3895 | byte_offset += 64 / CHAR_BIT; |
| 3896 | #endif |
| 3897 | } |
| 3898 | |
| 3899 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 3900 | uint8_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0); |
| 3901 | |
| 3902 | #if zig_big_endian |
| 3903 | byte_offset -= 32 / CHAR_BIT; |
| 3904 | #endif |
| 3905 | |
| 3906 | { |
| 3907 | uint32_t res_limb; |
| 3908 | bool limb_overflow; |
| 3909 | |
| 3910 | memcpy(&res_limb, &res_bytes[byte_offset], sizeof(res_limb)); |
| 3911 | limb_overflow = zig_subo_u32(&res_limb, res_limb, UINT32_C(1), limb_bits); |
| 3912 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3913 | if (!limb_overflow) return; |
| 3914 | } |
| 3915 | |
| 3916 | remaining_bytes -= 32 / CHAR_BIT; |
| 3917 | |
| 3918 | #if zig_little_endian |
| 3919 | byte_offset += 32 / CHAR_BIT; |
| 3920 | #endif |
| 3921 | } |
| 3922 | |
| 3923 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 3924 | uint8_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0); |
| 3925 | |
| 3926 | #if zig_big_endian |
| 3927 | byte_offset -= 16 / CHAR_BIT; |
| 3928 | #endif |
| 3929 | |
| 3930 | { |
| 3931 | uint16_t res_limb; |
| 3932 | bool limb_overflow; |
| 3933 | |
| 3934 | memcpy(&res_limb, &res_bytes[byte_offset], sizeof(res_limb)); |
| 3935 | limb_overflow = zig_subo_u16(&res_limb, res_limb, UINT16_C(1), limb_bits); |
| 3936 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3937 | if (!limb_overflow) return; |
| 3938 | } |
| 3939 | |
| 3940 | remaining_bytes -= 16 / CHAR_BIT; |
| 3941 | |
| 3942 | #if zig_little_endian |
| 3943 | byte_offset += 16 / CHAR_BIT; |
| 3944 | #endif |
| 3945 | } |
| 3946 | |
| 3947 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 3948 | uint8_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0); |
| 3949 | |
| 3950 | #if zig_big_endian |
| 3951 | byte_offset -= 8 / CHAR_BIT; |
| 3952 | #endif |
| 3953 | |
| 3954 | { |
| 3955 | uint8_t res_limb; |
| 3956 | bool limb_overflow; |
| 3957 | |
| 3958 | memcpy(&res_limb, &res_bytes[byte_offset], sizeof(res_limb)); |
| 3959 | limb_overflow = zig_subo_u8(&res_limb, res_limb, UINT8_C(1), limb_bits); |
| 3960 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3961 | if (!limb_overflow) return; |
| 3962 | } |
| 3963 | |
| 3964 | remaining_bytes -= 8 / CHAR_BIT; |
| 3965 | |
| 3966 | #if zig_little_endian |
| 3967 | byte_offset += 8 / CHAR_BIT; |
| 3968 | #endif |
| 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | static inline void zig_abs_big(void *res, const void *arg, bool is_signed, uint16_t bits) { |
| 3973 | uint8_t *res_bytes = res; |
| 3974 | const uint8_t *arg_bytes = arg; |
| 3975 | uint16_t byte_offset = 0; |
| 3976 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 3977 | if (zig_signFill_big(arg, is_signed, bits) >= INT8_C(0)) { |
| 3978 | memcpy(res, arg, remaining_bytes); |
| 3979 | return; |
| 3980 | } |
| 3981 | uint8_t top_bits = zig_u8_intCast_u16(remaining_bytes * CHAR_BIT - bits); |
| 3982 | bool overflow = true; |
| 3983 | |
| 3984 | #if zig_big_endian |
| 3985 | byte_offset = remaining_bytes; |
| 3986 | #endif |
| 3987 | |
| 3988 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 3989 | uint8_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0); |
| 3990 | |
| 3991 | #if zig_big_endian |
| 3992 | byte_offset -= 128 / CHAR_BIT; |
| 3993 | #endif |
| 3994 | |
| 3995 | { |
| 3996 | zig_u128 res_limb; |
| 3997 | zig_u128 arg_limb; |
| 3998 | |
| 3999 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 4000 | overflow = zig_addo_u128(&res_limb, zig_not_u128(arg_limb, UINT8_C(128)), zig_make_u128(UINT64_C(0), overflow ? UINT64_C(1) : UINT64_C(0)), limb_bits); |
| 4001 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 4002 | } |
| 4003 | |
| 4004 | remaining_bytes -= 128 / CHAR_BIT; |
| 4005 | |
| 4006 | #if zig_little_endian |
| 4007 | byte_offset += 128 / CHAR_BIT; |
| 4008 | #endif |
| 4009 | } |
| 4010 | |
| 4011 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 4012 | uint8_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0); |
| 4013 | |
| 4014 | #if zig_big_endian |
| 4015 | byte_offset -= 64 / CHAR_BIT; |
| 4016 | #endif |
| 4017 | |
| 4018 | { |
| 4019 | uint64_t res_limb; |
| 4020 | uint64_t arg_limb; |
| 4021 | |
| 4022 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 4023 | overflow = zig_addo_u64(&res_limb, zig_not_u64(arg_limb, UINT8_C(64)), overflow ? UINT64_C(1) : UINT64_C(0), limb_bits); |
| 4024 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 4025 | } |
| 4026 | |
| 4027 | remaining_bytes -= 64 / CHAR_BIT; |
| 4028 | |
| 4029 | #if zig_little_endian |
| 4030 | byte_offset += 64 / CHAR_BIT; |
| 4031 | #endif |
| 4032 | } |
| 4033 | |
| 4034 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 4035 | uint8_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0); |
| 4036 | |
| 4037 | #if zig_big_endian |
| 4038 | byte_offset -= 32 / CHAR_BIT; |
| 4039 | #endif |
| 4040 | |
| 4041 | { |
| 4042 | uint32_t res_limb; |
| 4043 | uint32_t arg_limb; |
| 4044 | |
| 4045 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 4046 | overflow = zig_addo_u32(&res_limb, zig_not_u32(arg_limb, UINT8_C(32)), overflow ? UINT32_C(1) : UINT32_C(0), limb_bits); |
| 4047 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 4048 | } |
| 4049 | |
| 4050 | remaining_bytes -= 32 / CHAR_BIT; |
| 4051 | |
| 4052 | #if zig_little_endian |
| 4053 | byte_offset += 32 / CHAR_BIT; |
| 4054 | #endif |
| 4055 | } |
| 4056 | |
| 4057 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 4058 | uint8_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0); |
| 4059 | |
| 4060 | #if zig_big_endian |
| 4061 | byte_offset -= 16 / CHAR_BIT; |
| 4062 | #endif |
| 4063 | |
| 4064 | { |
| 4065 | uint16_t res_limb; |
| 4066 | uint16_t arg_limb; |
| 4067 | |
| 4068 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 4069 | overflow = zig_addo_u16(&res_limb, zig_not_u16(arg_limb, UINT8_C(16)), overflow ? UINT16_C(1) : UINT16_C(0), limb_bits); |
| 4070 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 4071 | } |
| 4072 | |
| 4073 | remaining_bytes -= 16 / CHAR_BIT; |
| 4074 | |
| 4075 | #if zig_little_endian |
| 4076 | byte_offset += 16 / CHAR_BIT; |
| 4077 | #endif |
| 4078 | } |
| 4079 | |
| 4080 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 4081 | uint8_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0); |
| 4082 | |
| 4083 | #if zig_big_endian |
| 4084 | byte_offset -= 8 / CHAR_BIT; |
| 4085 | #endif |
| 4086 | |
| 4087 | { |
| 4088 | uint8_t res_limb; |
| 4089 | uint8_t arg_limb; |
| 4090 | |
| 4091 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 4092 | overflow = zig_addo_u8(&res_limb, zig_not_u8(arg_limb, UINT8_C(8)), overflow ? UINT8_C(1) : UINT8_C(0), limb_bits); |
| 4093 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 4094 | } |
| 4095 | |
| 4096 | remaining_bytes -= 8 / CHAR_BIT; |
| 4097 | |
| 4098 | #if zig_little_endian |
| 4099 | byte_offset += 8 / CHAR_BIT; |
| 4100 | #endif |
| 4101 | } |
| 4102 | } |
| 4103 | |
| 4104 | static inline void zig_min_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4105 | memcpy(res, zig_cmp_big(lhs, rhs, is_signed, bits) < INT32_C(0) ? lhs : rhs, zig_int_bytes(bits)); |
| 4106 | } |
| 4107 | |
| 4108 | static inline void zig_max_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4109 | memcpy(res, zig_cmp_big(lhs, rhs, is_signed, bits) >= INT32_C(0) ? lhs : rhs, zig_int_bytes(bits)); |
| 4110 | } |
| 4111 | |
| 2819 | 4112 | static inline bool zig_addo_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 2820 | 4113 | uint8_t *res_bytes = res; |
| 2821 | 4114 | const uint8_t *lhs_bytes = lhs; |
| 2822 | 4115 | const uint8_t *rhs_bytes = rhs; |
| 2823 | 4116 | uint16_t byte_offset = 0; |
| 2824 | 4117 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 2825 | | uint8_t top_bits = (uint8_t)(remaining_bytes * 8 - bits); |
| 4118 | uint8_t top_bits = zig_u8_intCast_u16(remaining_bytes * CHAR_BIT - bits); |
| 2826 | 4119 | bool overflow = false; |
| 2827 | 4120 | |
| 2828 | 4121 | #if zig_big_endian |
| ... | ... | @@ -3038,7 +4331,7 @@ static inline bool zig_subo_big(void *res, const void *lhs, const void *rhs, boo |
| 3038 | 4331 | const uint8_t *rhs_bytes = rhs; |
| 3039 | 4332 | uint16_t byte_offset = 0; |
| 3040 | 4333 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 3041 | | uint8_t top_bits = (uint8_t)(remaining_bytes * 8 - bits); |
| 4334 | uint8_t top_bits = zig_u8_intCast_u16(remaining_bytes * CHAR_BIT - bits); |
| 3042 | 4335 | bool overflow = false; |
| 3043 | 4336 | |
| 3044 | 4337 | #if zig_big_endian |
| ... | ... | @@ -3238,218 +4531,890 @@ static inline bool zig_subo_big(void *res, const void *lhs, const void *rhs, boo |
| 3238 | 4531 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 3239 | 4532 | } |
| 3240 | 4533 | |
| 3241 | | remaining_bytes -= 8 / CHAR_BIT; |
| 4534 | remaining_bytes -= 8 / CHAR_BIT; |
| 4535 | |
| 4536 | #if zig_little_endian |
| 4537 | byte_offset += 8 / CHAR_BIT; |
| 4538 | #endif |
| 4539 | } |
| 4540 | |
| 4541 | return overflow; |
| 4542 | } |
| 4543 | |
| 4544 | static inline void zig_add_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4545 | if (zig_addo_big(res, lhs, rhs, is_signed, bits)) zig_trap(); // panic: integer overflow |
| 4546 | } |
| 4547 | |
| 4548 | static inline void zig_addw_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4549 | (void)zig_addo_big(res, lhs, rhs, is_signed, bits); |
| 4550 | } |
| 4551 | |
| 4552 | static inline void zig_adds_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4553 | int8_t sat_sign = zig_signFill_big(lhs, is_signed, bits); |
| 4554 | |
| 4555 | if (!zig_addo_big(res, lhs, rhs, is_signed, bits)) return; |
| 4556 | switch (sat_sign) { |
| 4557 | case -INT8_C(1): return zig_minInt_big(res, is_signed, bits); |
| 4558 | case INT8_C(0): return zig_maxInt_big(res, is_signed, bits); |
| 4559 | } |
| 4560 | } |
| 4561 | |
| 4562 | static inline void zig_sub_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4563 | if (zig_subo_big(res, lhs, rhs, is_signed, bits)) zig_trap(); // panic: integer overflow |
| 4564 | } |
| 4565 | |
| 4566 | static inline void zig_subw_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4567 | (void)zig_subo_big(res, lhs, rhs, is_signed, bits); |
| 4568 | } |
| 4569 | |
| 4570 | static inline void zig_subs_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4571 | int8_t sat_sign = is_signed ? zig_signFill_big(lhs, is_signed, bits) : -INT8_C(1); |
| 4572 | |
| 4573 | if (!zig_subo_big(res, lhs, rhs, is_signed, bits)) return; |
| 4574 | switch (sat_sign) { |
| 4575 | case -INT8_C(1): return zig_minInt_big(res, is_signed, bits); |
| 4576 | case INT8_C(0): return zig_maxInt_big(res, is_signed, bits); |
| 4577 | } |
| 4578 | } |
| 4579 | |
| 4580 | static inline bool zig_mulo_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4581 | uint8_t *res_bytes = res; |
| 4582 | const uint8_t *lhs_bytes = lhs; |
| 4583 | const uint8_t *rhs_bytes = rhs; |
| 4584 | uint16_t size = zig_int_bytes(bits); |
| 4585 | uint16_t sign_byte_offset = zig_shr_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT16_C(1); |
| 4586 | uint8_t lhs_sign_fill = zig_u8_bitCast_i8(zig_signFill_big(lhs, is_signed, bits), UINT8_C(8)); |
| 4587 | uint8_t rhs_sign_fill = zig_u8_bitCast_i8(zig_signFill_big(rhs, is_signed, bits), UINT8_C(8)); |
| 4588 | uint16_t lhs_byte_offset = sign_byte_offset; |
| 4589 | uint16_t lhs_end_byte_offset = UINT16_C(0); |
| 4590 | bool overflow = false; |
| 4591 | |
| 4592 | #if zig_big_endian |
| 4593 | lhs_byte_offset = size - lhs_byte_offset; |
| 4594 | lhs_end_byte_offset = size - lhs_end_byte_offset; |
| 4595 | #endif |
| 4596 | |
| 4597 | while (lhs_byte_offset != lhs_end_byte_offset) { |
| 4598 | uint16_t rhs_byte_offset = UINT16_C(0); |
| 4599 | uint16_t end_byte_offset = sign_byte_offset; |
| 4600 | uint16_t res_byte_offset; |
| 4601 | uint16_t lhs_byte; |
| 4602 | uint8_t res_byte = UINT8_C(0); |
| 4603 | uint16_t mul_res = UINT16_C(0); |
| 4604 | uint8_t carry = UINT8_C(0); |
| 4605 | |
| 4606 | #if zig_little_endian |
| 4607 | lhs_byte_offset -= UINT16_C(1); |
| 4608 | #else |
| 4609 | rhs_byte_offset = size - rhs_byte_offset; |
| 4610 | end_byte_offset = size - end_byte_offset; |
| 4611 | #endif |
| 4612 | |
| 4613 | lhs_byte = zig_u16_intCast_u8(lhs_bytes[lhs_byte_offset]) ^ lhs_sign_fill; |
| 4614 | |
| 4615 | #if zig_big_endian |
| 4616 | lhs_byte_offset += UINT16_C(1); |
| 4617 | #endif |
| 4618 | |
| 4619 | res_byte_offset = lhs_byte_offset; |
| 4620 | |
| 4621 | while (res_byte_offset != end_byte_offset) { |
| 4622 | bool res_byte_initialized = res_byte_offset != lhs_byte_offset; |
| 4623 | |
| 4624 | #if zig_big_endian |
| 4625 | rhs_byte_offset -= UINT16_C(1); |
| 4626 | res_byte_offset -= UINT16_C(1); |
| 4627 | #endif |
| 4628 | |
| 4629 | if (res_byte_initialized) res_byte = res_bytes[res_byte_offset]; |
| 4630 | carry = zig_addo_u8(&res_byte, res_byte, carry, UINT8_C(8)); |
| 4631 | carry += zig_addo_u8(&res_byte, res_byte, zig_u8_intCast_u16( |
| 4632 | zig_shr_u16(mul_res, UINT8_C(8)) |
| 4633 | ), UINT8_C(8)); |
| 4634 | mul_res = lhs_byte * zig_u16_intCast_u8(rhs_bytes[rhs_byte_offset] ^ rhs_sign_fill); |
| 4635 | carry += zig_addo_u8(&res_bytes[res_byte_offset], res_byte, zig_u8_truncate_u16( |
| 4636 | mul_res, |
| 4637 | UINT8_C(8) |
| 4638 | ), UINT8_C(8)); |
| 4639 | |
| 4640 | #if zig_little_endian |
| 4641 | rhs_byte_offset += UINT16_C(1); |
| 4642 | res_byte_offset += UINT16_C(1); |
| 4643 | #endif |
| 4644 | } |
| 4645 | |
| 4646 | while (rhs_byte_offset != end_byte_offset) { |
| 4647 | #if zig_big_endian |
| 4648 | rhs_byte_offset -= UINT16_C(1); |
| 4649 | #endif |
| 4650 | |
| 4651 | carry = zig_addo_u8( |
| 4652 | &res_byte, |
| 4653 | zig_u8_intCast_u16(zig_shr_u16(mul_res, UINT8_C(8))), |
| 4654 | carry, |
| 4655 | UINT8_C(8) |
| 4656 | ); |
| 4657 | mul_res = lhs_byte * zig_u16_intCast_u8(rhs_bytes[rhs_byte_offset] ^ rhs_sign_fill); |
| 4658 | carry += zig_addo_u8(&res_byte, res_byte, zig_u8_truncate_u16( |
| 4659 | mul_res, |
| 4660 | UINT8_C(8) |
| 4661 | ), UINT8_C(8)); |
| 4662 | overflow |= res_byte != UINT8_C(0); |
| 4663 | |
| 4664 | #if zig_little_endian |
| 4665 | rhs_byte_offset += UINT16_C(1); |
| 4666 | #endif |
| 4667 | } |
| 4668 | |
| 4669 | overflow |= zig_shr_u16(mul_res, UINT8_C(8)) != UINT16_C(0); |
| 4670 | overflow |= carry != UINT8_C(0); |
| 4671 | } |
| 4672 | |
| 4673 | #if zig_little_endian |
| 4674 | sign_byte_offset -= UINT64_C(1); |
| 4675 | #else |
| 4676 | sign_byte_offset = size - sign_byte_offset; |
| 4677 | #endif |
| 4678 | |
| 4679 | if (lhs_sign_fill != rhs_sign_fill) { |
| 4680 | uint16_t byte_offset = UINT16_C(0); |
| 4681 | uint16_t end_byte_offset = sign_byte_offset; |
| 4682 | uint8_t res_byte; |
| 4683 | int8_t signed_res_byte; |
| 4684 | uint8_t carry = UINT8_C(0); |
| 4685 | |
| 4686 | #if zig_big_endian |
| 4687 | byte_offset = size - byte_offset; |
| 4688 | end_byte_offset += UINT16_C(1); |
| 4689 | #endif |
| 4690 | |
| 4691 | while (byte_offset != end_byte_offset) { |
| 4692 | #if zig_big_endian |
| 4693 | byte_offset -= UINT16_C(1); |
| 4694 | #endif |
| 4695 | |
| 4696 | carry = zig_subo_u8(&res_byte, UINT8_C(0), carry, UINT8_C(8)); |
| 4697 | carry += zig_subo_u8(&res_byte, res_byte, res_bytes[byte_offset], UINT8_C(8)); |
| 4698 | carry += zig_subo_u8( |
| 4699 | &res_bytes[byte_offset], |
| 4700 | res_byte, |
| 4701 | (lhs_sign_fill == UINT8_C(0) ? lhs_bytes : rhs_bytes)[byte_offset], |
| 4702 | UINT8_C(8) |
| 4703 | ); |
| 4704 | |
| 4705 | #if zig_little_endian |
| 4706 | byte_offset += UINT16_C(1); |
| 4707 | #endif |
| 4708 | } |
| 4709 | |
| 4710 | #if zig_big_endian |
| 4711 | byte_offset -= UINT16_C(1); |
| 4712 | #endif |
| 4713 | |
| 4714 | signed_res_byte = zig_i8_bitCast_u8(res_bytes[byte_offset], UINT8_C(8)); |
| 4715 | overflow |= signed_res_byte < INT8_C(0); |
| 4716 | overflow |= zig_subo_i8(&signed_res_byte, INT8_C(0), signed_res_byte, UINT8_C(8)); |
| 4717 | overflow |= zig_subo_i8(&signed_res_byte, signed_res_byte, zig_i8_intCast_u8(carry), UINT8_C(8)); |
| 4718 | overflow |= zig_subo_i8(&signed_res_byte, signed_res_byte, zig_i8_bitCast_u8( |
| 4719 | (lhs_sign_fill == UINT8_C(0) ? lhs_bytes : rhs_bytes)[byte_offset], |
| 4720 | UINT8_C(8) |
| 4721 | ), UINT8_C(8)); |
| 4722 | res_bytes[byte_offset] = zig_i8_bitCast_u8(signed_res_byte, UINT8_C(8)); |
| 4723 | } else if (lhs_sign_fill != UINT8_C(0)) { |
| 4724 | uint16_t byte_offset = UINT16_C(0); |
| 4725 | uint16_t end_byte_offset = sign_byte_offset; |
| 4726 | uint8_t res_byte; |
| 4727 | int8_t signed_res_byte; |
| 4728 | uint8_t carry = UINT8_C(1); |
| 4729 | |
| 4730 | #if zig_big_endian |
| 4731 | byte_offset = size - byte_offset; |
| 4732 | end_byte_offset += UINT16_C(1); |
| 4733 | #endif |
| 4734 | |
| 4735 | while (byte_offset != end_byte_offset) { |
| 4736 | #if zig_big_endian |
| 4737 | byte_offset -= UINT16_C(1); |
| 4738 | #endif |
| 4739 | |
| 4740 | carry = zig_subo_u8(&res_byte, res_bytes[byte_offset], carry, UINT8_C(8)); |
| 4741 | carry += zig_subo_u8(&res_byte, res_byte, lhs_bytes[byte_offset], UINT8_C(8)); |
| 4742 | carry += zig_subo_u8(&res_bytes[byte_offset], res_byte, rhs_bytes[byte_offset], UINT8_C(8)); |
| 4743 | |
| 4744 | #if zig_little_endian |
| 4745 | byte_offset += UINT16_C(1); |
| 4746 | #endif |
| 4747 | } |
| 4748 | |
| 4749 | #if zig_big_endian |
| 4750 | byte_offset -= UINT16_C(1); |
| 4751 | #endif |
| 4752 | |
| 4753 | signed_res_byte = zig_i8_bitCast_u8(res_bytes[byte_offset], UINT8_C(8)); |
| 4754 | overflow |= signed_res_byte < INT8_C(0); |
| 4755 | overflow |= zig_subo_i8(&signed_res_byte, signed_res_byte, zig_i8_intCast_u8(carry), UINT8_C(8)); |
| 4756 | overflow |= zig_subo_i8(&signed_res_byte, signed_res_byte, zig_i8_bitCast_u8( |
| 4757 | lhs_bytes[byte_offset], |
| 4758 | UINT8_C(8) |
| 4759 | ), UINT8_C(8)); |
| 4760 | overflow |= zig_subo_i8(&signed_res_byte, signed_res_byte, zig_i8_bitCast_u8( |
| 4761 | rhs_bytes[byte_offset], |
| 4762 | UINT8_C(8) |
| 4763 | ), UINT8_C(8)); |
| 4764 | res_bytes[byte_offset] = zig_i8_bitCast_u8(signed_res_byte, UINT8_C(8)); |
| 4765 | } else if (is_signed) { |
| 4766 | int8_t signed_res_byte = zig_i8_bitCast_u8(res_bytes[sign_byte_offset], UINT8_C(8)); |
| 4767 | |
| 4768 | overflow |= signed_res_byte < INT8_C(0); |
| 4769 | } |
| 4770 | |
| 4771 | { |
| 4772 | uint8_t truncate_bits = zig_u8_truncate_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT16_C(1); |
| 4773 | uint8_t fill_byte = UINT8_C(0); |
| 4774 | |
| 4775 | if (is_signed) { |
| 4776 | int8_t sign_byte = zig_i8_bitCast_u8(res_bytes[sign_byte_offset], UINT8_C(8)); |
| 4777 | int8_t truncated = zig_i8_truncate_i8(sign_byte, truncate_bits); |
| 4778 | |
| 4779 | overflow |= sign_byte != truncated; |
| 4780 | res_bytes[sign_byte_offset] = zig_u8_bitCast_i8(truncated, UINT8_C(8)); |
| 4781 | fill_byte = zig_u8_bitCast_i8(zig_shr_i8(truncated, UINT8_C(7)), UINT8_C(8)); |
| 4782 | } else { |
| 4783 | uint8_t sign_byte = res_bytes[sign_byte_offset]; |
| 4784 | uint8_t truncated = zig_u8_truncate_u8(sign_byte, truncate_bits); |
| 4785 | |
| 4786 | overflow |= sign_byte != truncated; |
| 4787 | res_bytes[sign_byte_offset] = truncated; |
| 4788 | } |
| 4789 | |
| 4790 | #if zig_little_endian |
| 4791 | sign_byte_offset += UINT16_C(1); |
| 4792 | memset(&res_bytes[sign_byte_offset], fill_byte, size - sign_byte_offset); |
| 4793 | #else |
| 4794 | memset(&res_bytes[0], fill_byte, sign_byte_offset); |
| 4795 | #endif |
| 4796 | } |
| 4797 | |
| 4798 | return overflow; |
| 4799 | } |
| 4800 | |
| 4801 | static inline void zig_mul_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4802 | if (zig_mulo_big(res, lhs, rhs, is_signed, bits)) zig_trap(); // panic: integer overflow |
| 4803 | } |
| 4804 | |
| 4805 | static inline void zig_mulw_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4806 | (void)zig_mulo_big(res, lhs, rhs, is_signed, bits); |
| 4807 | } |
| 4808 | |
| 4809 | static inline void zig_muls_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 4810 | int8_t sat_sign = zig_signFill_big(lhs, is_signed, bits) ^ zig_signFill_big(rhs, is_signed, bits); |
| 4811 | |
| 4812 | if (!zig_mulo_big(res, lhs, rhs, is_signed, bits)) return; |
| 4813 | switch (sat_sign) { |
| 4814 | case -INT8_C(1): return zig_minInt_big(res, is_signed, bits); |
| 4815 | case INT8_C(0): return zig_maxInt_big(res, is_signed, bits); |
| 4816 | } |
| 4817 | } |
| 4818 | |
| 4819 | static inline void zig_divTrunc_big(void *res, const void *lhs, const void *rhs, void *temp, bool is_signed, uint16_t bits) { |
| 4820 | if (is_signed) { |
| 4821 | zig_extern void __divei5(uint32_t *res, const uint32_t *lhs, const uint32_t *rhs, uint32_t *temp, uintptr_t bits); |
| 4822 | __divei5(res, lhs, rhs, temp, bits); |
| 4823 | } else { |
| 4824 | zig_extern void __udivei5(uint32_t *res, const uint32_t *lhs, const uint32_t *rhs, uint32_t *temp, uintptr_t bits); |
| 4825 | __udivei5(res, lhs, rhs, temp, bits); |
| 4826 | } |
| 4827 | } |
| 4828 | |
| 4829 | static inline void zig_rem_big(void *res, const void *lhs, const void *rhs, void *temp, bool is_signed, uint16_t bits) { |
| 4830 | if (is_signed) { |
| 4831 | zig_extern void __modei5(uint32_t *res, const uint32_t *lhs, const uint32_t *rhs, uint32_t *temp, uintptr_t bits); |
| 4832 | __modei5(res, lhs, rhs, temp, bits); |
| 4833 | } else { |
| 4834 | zig_extern void __umodei5(uint32_t *res, const uint32_t *lhs, const uint32_t *rhs, uint32_t *temp, uintptr_t bits); |
| 4835 | __umodei5(res, lhs, rhs, temp, bits); |
| 4836 | } |
| 4837 | } |
| 4838 | |
| 4839 | static inline void zig_divFloor_big(void *res, const void *lhs, const void *rhs, void *temp, bool is_signed, uint16_t bits) { |
| 4840 | bool decrement = false; |
| 4841 | |
| 4842 | if (is_signed) { |
| 4843 | zig_rem_big(res, lhs, rhs, temp, is_signed, bits); |
| 4844 | decrement = zig_u32_bitCast_i32(zig_xor_i32( |
| 4845 | zig_cmp_big_u8(res, UINT8_C(0), is_signed, bits), |
| 4846 | zig_and_i32(zig_i32_intCast_i8(zig_signFill_big(rhs, is_signed, bits)), zig_minInt_i32) |
| 4847 | ), UINT8_C(32)) > zig_u32_bitCast_i32(zig_minInt_i32, UINT8_C(32)); |
| 4848 | } |
| 4849 | zig_divTrunc_big(res, lhs, rhs, temp, is_signed, bits); |
| 4850 | if (decrement) zig_decrement_big(res, is_signed, bits); |
| 4851 | } |
| 4852 | |
| 4853 | static inline void zig_divCeil_big(void *res, const void *lhs, const void *rhs, void *temp, bool is_signed, uint16_t bits) { |
| 4854 | bool increment = false; |
| 4855 | |
| 4856 | zig_rem_big(res, lhs, rhs, temp, is_signed, bits); |
| 4857 | increment = zig_xor_i32( |
| 4858 | zig_cmp_big_u8(res, UINT8_C(0), is_signed, bits), |
| 4859 | zig_and_i32(zig_i32_intCast_i8(zig_signFill_big(rhs, is_signed, bits)), zig_minInt_i32) |
| 4860 | ) > INT32_C(0); |
| 4861 | zig_divTrunc_big(res, lhs, rhs, temp, is_signed, bits); |
| 4862 | if (increment) zig_increment_big(res, is_signed, bits); |
| 4863 | } |
| 4864 | |
| 4865 | static inline void zig_mod_big(void *res, const void *lhs, const void *rhs, void *temp, bool is_signed, uint16_t bits) { |
| 4866 | bool fixup = false; |
| 4867 | |
| 4868 | zig_rem_big(res, lhs, rhs, temp, is_signed, bits); |
| 4869 | if (is_signed && zig_u32_bitCast_i32(zig_xor_i32( |
| 4870 | zig_cmp_big_u8(res, UINT8_C(0), is_signed, bits), |
| 4871 | zig_and_i32(zig_i32_intCast_i8(zig_signFill_big(rhs, is_signed, bits)), zig_minInt_i32) |
| 4872 | ), UINT8_C(32)) > zig_u32_bitCast_i32(zig_minInt_i32, UINT8_C(32))) zig_add_big(res, res, rhs, is_signed, bits); |
| 4873 | } |
| 4874 | |
| 4875 | static inline void zig_shr_big(void *res, const void *lhs, uint16_t rhs, bool is_signed, uint16_t bits) { |
| 4876 | uint8_t *res_bytes = res; |
| 4877 | const uint8_t *lhs_bytes = lhs; |
| 4878 | uint16_t size = zig_int_bytes(bits); |
| 4879 | uint16_t res_byte_offset = UINT16_C(0); |
| 4880 | uint16_t lhs_byte_offset = zig_shr_u16(rhs, UINT8_C(3)); |
| 4881 | uint16_t end_byte_offset = zig_shr_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT16_C(1); |
| 4882 | uint8_t lhs_prev_byte; |
| 4883 | uint8_t byte_shift = zig_u8_truncate_u16(rhs, UINT8_C(3)); |
| 4884 | |
| 4885 | #if zig_big_endian |
| 4886 | res_byte_offset = size - res_byte_offset; |
| 4887 | lhs_byte_offset = size - lhs_byte_offset; |
| 4888 | end_byte_offset = size - end_byte_offset; |
| 4889 | #endif |
| 4890 | |
| 4891 | { |
| 4892 | #if zig_big_endian |
| 4893 | lhs_byte_offset -= UINT16_C(1); |
| 4894 | #endif |
| 4895 | |
| 4896 | lhs_prev_byte = lhs_bytes[lhs_byte_offset]; |
| 4897 | |
| 4898 | #if zig_little_endian |
| 4899 | lhs_byte_offset += UINT16_C(1); |
| 4900 | #endif |
| 4901 | } |
| 4902 | |
| 4903 | while (lhs_byte_offset != end_byte_offset) { |
| 4904 | #if zig_big_endian |
| 4905 | res_byte_offset -= UINT16_C(1); |
| 4906 | lhs_byte_offset -= UINT16_C(1); |
| 4907 | #endif |
| 4908 | |
| 4909 | { |
| 4910 | uint8_t lhs_byte = lhs_bytes[lhs_byte_offset]; |
| 4911 | |
| 4912 | res_bytes[res_byte_offset] = zig_u8_intCast_u16(zig_shr_u16(zig_or_u16( |
| 4913 | zig_shl_u16(zig_u16_intCast_u8(lhs_byte), UINT8_C(8)), |
| 4914 | zig_u16_intCast_u8(lhs_prev_byte) |
| 4915 | ), byte_shift)); |
| 4916 | lhs_prev_byte = lhs_byte; |
| 4917 | } |
| 4918 | |
| 4919 | #if zig_little_endian |
| 4920 | res_byte_offset += UINT16_C(1); |
| 4921 | lhs_byte_offset += UINT16_C(1); |
| 4922 | #endif |
| 4923 | } |
| 4924 | |
| 4925 | { |
| 4926 | uint8_t lhs_sign_fill = UINT8_C(0); |
| 4927 | |
| 4928 | #if zig_big_endian |
| 4929 | res_byte_offset -= UINT16_C(1); |
| 4930 | #endif |
| 4931 | |
| 4932 | if (is_signed) { |
| 4933 | int8_t signed_byte = zig_i8_bitCast_u8(lhs_prev_byte, UINT8_C(8)); |
| 4934 | |
| 4935 | res_bytes[res_byte_offset] = zig_shr_i8(signed_byte, byte_shift); |
| 4936 | lhs_sign_fill = zig_u8_bitCast_i8(zig_shr_i8(signed_byte, UINT8_C(7)), UINT8_C(8)); |
| 4937 | } else { |
| 4938 | res_bytes[res_byte_offset] = zig_shr_u8(lhs_prev_byte, byte_shift); |
| 4939 | } |
| 4940 | |
| 4941 | #if zig_little_endian |
| 4942 | res_byte_offset += UINT16_C(1); |
| 4943 | memset(&res_bytes[res_byte_offset], lhs_sign_fill, size - res_byte_offset); |
| 4944 | #else |
| 4945 | memset(&res_bytes[0], lhs_sign_fill, res_byte_offset); |
| 4946 | #endif |
| 4947 | } |
| 4948 | } |
| 4949 | |
| 4950 | static inline bool zig_shlo_big(void *res, const void *lhs, uint16_t rhs, bool is_signed, uint16_t bits) { |
| 4951 | uint8_t *res_bytes = res; |
| 4952 | const uint8_t *lhs_bytes = lhs; |
| 4953 | uint8_t lhs_sign_fill = zig_u8_bitCast_i8(zig_signFill_big(lhs, is_signed, bits), UINT8_C(8)); |
| 4954 | uint16_t size = zig_int_bytes(bits); |
| 4955 | uint16_t res_byte_offset = zig_shr_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT16_C(1); |
| 4956 | uint16_t lhs_byte_offset = UINT16_C(0); |
| 4957 | uint16_t end_byte_offset = res_byte_offset - UINT16_C(1) - zig_shr_u16(rhs, UINT8_C(3)); |
| 4958 | uint8_t lhs_prev_byte = lhs_sign_fill; |
| 4959 | uint8_t byte_shift = UINT8_C(8) - zig_u8_truncate_u16(rhs, UINT8_C(3)); |
| 4960 | bool overflow = false; |
| 4961 | |
| 4962 | #if zig_little_endian |
| 4963 | lhs_byte_offset = size - lhs_byte_offset; |
| 4964 | #else |
| 4965 | res_byte_offset = size - res_byte_offset; |
| 4966 | end_byte_offset = size - end_byte_offset; |
| 4967 | #endif |
| 4968 | |
| 4969 | while (lhs_byte_offset != end_byte_offset) { |
| 4970 | #if zig_little_endian |
| 4971 | lhs_byte_offset -= UINT16_C(1); |
| 4972 | #endif |
| 4973 | |
| 4974 | overflow |= lhs_prev_byte != lhs_sign_fill; |
| 4975 | lhs_prev_byte = lhs_bytes[lhs_byte_offset]; |
| 4976 | |
| 4977 | #if zig_big_endian |
| 4978 | lhs_byte_offset += UINT16_C(1); |
| 4979 | #endif |
| 4980 | } |
| 4981 | |
| 4982 | #if zig_little_endian |
| 4983 | end_byte_offset = UINT16_C(0); |
| 4984 | #else |
| 4985 | end_byte_offset = size; |
| 4986 | #endif |
| 4987 | |
| 4988 | { |
| 4989 | bool lhs_more_bytes = lhs_byte_offset != end_byte_offset; |
| 4990 | |
| 4991 | #if zig_little_endian |
| 4992 | if (lhs_more_bytes) lhs_byte_offset -= UINT16_C(1); |
| 4993 | #endif |
| 4994 | |
| 4995 | { |
| 4996 | uint8_t lhs_byte = UINT8_C(0); |
| 4997 | |
| 4998 | if (lhs_more_bytes) lhs_byte = lhs_bytes[lhs_byte_offset]; |
| 4999 | |
| 5000 | if (is_signed) { |
| 5001 | int16_t shifted = zig_shr_i16(zig_or_i16( |
| 5002 | zig_shl_i16(zig_i16_intCast_u8(lhs_prev_byte), UINT8_C(8)), |
| 5003 | zig_i16_intCast_u8(lhs_byte) |
| 5004 | ), byte_shift); |
| 5005 | int8_t truncated = zig_i8_truncate_i16( |
| 5006 | shifted, |
| 5007 | zig_u8_truncate_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT8_C(1) |
| 5008 | ); |
| 5009 | uint8_t fill = zig_u8_bitCast_i8(zig_shr_i8(truncated, UINT8_C(7)), UINT8_C(8)); |
| 5010 | |
| 5011 | overflow |= zig_i16_intCast_i8(truncated) != shifted; |
| 5012 | #if zig_little_endian |
| 5013 | memset(&res_bytes[res_byte_offset], fill, size - res_byte_offset); |
| 5014 | res_byte_offset -= UINT16_C(1); |
| 5015 | #else |
| 5016 | memset(&res_bytes[0], fill, res_byte_offset); |
| 5017 | #endif |
| 5018 | res_bytes[res_byte_offset] = zig_u8_bitCast_i8(truncated, UINT8_C(8)); |
| 5019 | } else { |
| 5020 | uint16_t shifted = zig_shr_u16(zig_or_u16( |
| 5021 | zig_shl_u16(zig_u16_intCast_u8(lhs_prev_byte), UINT8_C(8)), |
| 5022 | zig_u16_intCast_u8(lhs_byte) |
| 5023 | ), byte_shift); |
| 5024 | uint8_t truncated = zig_u8_truncate_u16( |
| 5025 | shifted, |
| 5026 | zig_u8_truncate_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT8_C(1) |
| 5027 | ); |
| 5028 | |
| 5029 | overflow |= zig_u16_intCast_u8(truncated) != shifted; |
| 5030 | #if zig_little_endian |
| 5031 | memset(&res_bytes[res_byte_offset], zig_minInt_u8, size - res_byte_offset); |
| 5032 | res_byte_offset -= UINT16_C(1); |
| 5033 | #else |
| 5034 | memset(&res_bytes[0], zig_minInt_u8, res_byte_offset); |
| 5035 | #endif |
| 5036 | res_bytes[res_byte_offset] = truncated; |
| 5037 | } |
| 5038 | |
| 5039 | lhs_prev_byte = lhs_byte; |
| 5040 | } |
| 5041 | |
| 5042 | #if zig_big_endian |
| 5043 | res_byte_offset += UINT16_C(1); |
| 5044 | if (lhs_more_bytes) lhs_byte_offset += UINT16_C(1); |
| 5045 | #endif |
| 5046 | } |
| 5047 | |
| 5048 | while (lhs_byte_offset != end_byte_offset) { |
| 5049 | #if zig_little_endian |
| 5050 | res_byte_offset -= UINT16_C(1); |
| 5051 | lhs_byte_offset -= UINT16_C(1); |
| 5052 | #endif |
| 5053 | |
| 5054 | { |
| 5055 | uint8_t lhs_byte = lhs_bytes[lhs_byte_offset]; |
| 5056 | |
| 5057 | res_bytes[res_byte_offset] = zig_u8_intCast_u16(zig_shr_u16(zig_or_u16( |
| 5058 | zig_shl_u16(zig_u16_intCast_u8(lhs_prev_byte), UINT8_C(8)), |
| 5059 | zig_u16_intCast_u8(lhs_byte) |
| 5060 | ), byte_shift)); |
| 5061 | lhs_prev_byte = lhs_byte; |
| 5062 | } |
| 5063 | |
| 5064 | #if zig_big_endian |
| 5065 | res_byte_offset += UINT16_C(1); |
| 5066 | lhs_byte_offset += UINT16_C(1); |
| 5067 | #endif |
| 5068 | } |
| 5069 | |
| 5070 | { |
| 5071 | #if zig_little_endian |
| 5072 | res_byte_offset -= UINT16_C(1); |
| 5073 | #endif |
| 5074 | |
| 5075 | res_bytes[res_byte_offset] = zig_u8_intCast_u16(zig_shr_u16( |
| 5076 | zig_shl_u16(zig_u16_intCast_u8(lhs_prev_byte), UINT8_C(8)), |
| 5077 | byte_shift |
| 5078 | )); |
| 5079 | |
| 5080 | #if zig_big_endian |
| 5081 | res_byte_offset += UINT16_C(1); |
| 5082 | #endif |
| 5083 | } |
| 5084 | |
| 5085 | #if zig_little_endian |
| 5086 | memset(&res_bytes[0], zig_minInt_u8, res_byte_offset); |
| 5087 | #else |
| 5088 | memset(&res_bytes[res_byte_offset], zig_minInt_u8, size - res_byte_offset); |
| 5089 | #endif |
| 5090 | |
| 5091 | return overflow; |
| 5092 | } |
| 5093 | |
| 5094 | static inline void zig_shl_big(void *res, const void *lhs, uint16_t rhs, bool is_signed, uint16_t bits) { |
| 5095 | if (zig_shlo_big(res, lhs, rhs, is_signed, bits)) zig_trap(); // panic: left shift overflowed bits |
| 5096 | } |
| 5097 | |
| 5098 | static inline void zig_shlw_big(void *res, const void *lhs, uint16_t rhs, bool is_signed, uint16_t bits) { |
| 5099 | (void)zig_shlo_big(res, lhs, rhs, is_signed, bits); |
| 5100 | } |
| 5101 | |
| 5102 | #define zig_big_shls_builtin(w) \ |
| 5103 | static inline uint##w##_t zig_shls_u##w##_big(uint##w##_t lhs, const void *rhs, \ |
| 5104 | uint8_t lhs_bits, bool rhs_is_signed, uint16_t rhs_bits) { \ |
| 5105 | uint##w##_t res; \ |
| 5106 | const uint8_t *rhs_bytes = rhs; \ |
| 5107 | if (zig_cmp_big_u8(rhs, lhs_bits, rhs_is_signed, rhs_bits) < INT32_C(0) && \ |
| 5108 | !zig_shlo_u##w(&res, lhs, rhs_bytes[0], lhs_bits)) return res; \ |
| 5109 | return lhs == INT##w##_C(0) ? zig_minInt_u(w, lhs_bits) : zig_maxInt_u(w, lhs_bits); \ |
| 5110 | } \ |
| 5111 | \ |
| 5112 | static inline int##w##_t zig_shls_i##w##_big(int##w##_t lhs, const void *rhs, \ |
| 5113 | uint8_t lhs_bits, bool rhs_is_signed, uint16_t rhs_bits) { \ |
| 5114 | int##w##_t res; \ |
| 5115 | const uint8_t *rhs_bytes = rhs; \ |
| 5116 | if (zig_cmp_big_u8(rhs, lhs_bits, rhs_is_signed, rhs_bits) < INT32_C(0) && \ |
| 5117 | !zig_shlo_i##w(&res, lhs, rhs_bytes[0], lhs_bits)) return res; \ |
| 5118 | return lhs == INT##w##_C(0) ? INT##w##_C(0) : \ |
| 5119 | lhs < INT##w##_C(0) ? zig_minInt_i(w, lhs_bits) : zig_maxInt_i(w, lhs_bits); \ |
| 5120 | } \ |
| 5121 | \ |
| 5122 | static inline void zig_shls_big_u##w(void *res, const void *lhs, uint##w##_t rhs, bool is_signed, uint16_t bits) { \ |
| 5123 | const uint8_t *lhs_bytes = lhs; \ |
| 5124 | if (rhs < bits && !zig_shlo_big(res, lhs, zig_u16_intCast_u##w(rhs), is_signed, bits)) return; \ |
| 5125 | switch (zig_cmp_big_u8(lhs, UINT8_C(0), is_signed, bits)) { \ |
| 5126 | case -INT32_C(1): return zig_minInt_big(res, is_signed, bits); \ |
| 5127 | case INT32_C(0): return zig_minInt_big(res, false, bits); \ |
| 5128 | case INT32_C(1): return zig_maxInt_big(res, is_signed, bits); \ |
| 5129 | default: zig_unreachable(); \ |
| 5130 | } \ |
| 5131 | } |
| 5132 | zig_big_shls_builtin(8) |
| 5133 | zig_big_shls_builtin(16) |
| 5134 | zig_big_shls_builtin(32) |
| 5135 | zig_big_shls_builtin(64) |
| 5136 | |
| 5137 | static inline void zig_byteSwap_big(void *res, const void *arg, bool is_signed, uint16_t bits) { |
| 5138 | uint8_t *res_bytes = res; |
| 5139 | const uint8_t *arg_bytes = arg; |
| 5140 | uint16_t res_byte_offset = UINT16_C(0); |
| 5141 | uint16_t arg_byte_offset = bits / CHAR_BIT; |
| 5142 | uint16_t end_byte_offset = UINT16_C(1); |
| 5143 | uint16_t size = zig_int_bytes(bits); |
| 5144 | |
| 5145 | #if zig_big_endian |
| 5146 | res_byte_offset = size - res_byte_offset; |
| 5147 | arg_byte_offset = size - arg_byte_offset; |
| 5148 | end_byte_offset = size - end_byte_offset; |
| 5149 | #endif |
| 5150 | |
| 5151 | while (arg_byte_offset != end_byte_offset) { |
| 5152 | #if zig_little_endian |
| 5153 | arg_byte_offset -= UINT16_C(1); |
| 5154 | #else |
| 5155 | res_byte_offset -= UINT16_C(1); |
| 5156 | #endif |
| 5157 | |
| 5158 | res_bytes[res_byte_offset] = arg_bytes[arg_byte_offset]; |
| 3242 | 5159 | |
| 3243 | 5160 | #if zig_little_endian |
| 3244 | | byte_offset += 8 / CHAR_BIT; |
| 5161 | res_byte_offset += UINT16_C(1); |
| 5162 | #else |
| 5163 | arg_byte_offset += UINT16_C(1); |
| 3245 | 5164 | #endif |
| 3246 | 5165 | } |
| 3247 | 5166 | |
| 3248 | | return overflow; |
| 3249 | | } |
| 5167 | { |
| 5168 | #if zig_little_endian |
| 5169 | arg_byte_offset -= UINT16_C(1); |
| 5170 | #else |
| 5171 | res_byte_offset -= UINT16_C(1); |
| 5172 | #endif |
| 3250 | 5173 | |
| 3251 | | static inline void zig_addw_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 3252 | | (void)zig_addo_big(res, lhs, rhs, is_signed, bits); |
| 3253 | | } |
| 5174 | { |
| 5175 | uint8_t byte = arg_bytes[arg_byte_offset]; |
| 5176 | uint8_t fill = is_signed |
| 5177 | ? zig_u8_bitCast_i8(zig_shr_i8(zig_i8_bitCast_u8(byte, UINT8_C(8)), UINT8_C(7)), UINT8_C(8)) |
| 5178 | : UINT8_C(0); |
| 3254 | 5179 | |
| 3255 | | static inline void zig_subw_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 3256 | | (void)zig_subo_big(res, lhs, rhs, is_signed, bits); |
| 3257 | | } |
| 5180 | res_bytes[res_byte_offset] = byte; |
| 3258 | 5181 | |
| 3259 | | zig_extern void __udivei4(uint32_t *res, const uint32_t *lhs, const uint32_t *rhs, uintptr_t bits); |
| 3260 | | static inline void zig_div_trunc_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 3261 | | if (!is_signed) { |
| 3262 | | __udivei4(res, lhs, rhs, bits); |
| 3263 | | return; |
| 5182 | #if zig_little_endian |
| 5183 | res_byte_offset += UINT16_C(1); |
| 5184 | memset(&res_bytes[res_byte_offset], fill, size - res_byte_offset); |
| 5185 | #else |
| 5186 | memset(&res_bytes[0], fill, res_byte_offset); |
| 5187 | #endif |
| 5188 | } |
| 3264 | 5189 | } |
| 3265 | | |
| 3266 | | zig_trap(); |
| 3267 | 5190 | } |
| 3268 | 5191 | |
| 3269 | | static inline void zig_div_floor_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 3270 | | if (!is_signed) { |
| 3271 | | zig_div_trunc_big(res, lhs, rhs, is_signed, bits); |
| 3272 | | return; |
| 3273 | | } |
| 5192 | static inline void zig_bitReverse_big(void *res, const void *arg, bool is_signed, uint16_t bits) { |
| 5193 | uint8_t *res_bytes = res; |
| 5194 | const uint8_t *arg_bytes = arg; |
| 5195 | uint16_t size = zig_int_bytes(bits); |
| 5196 | uint16_t res_byte_offset = UINT16_C(0); |
| 5197 | uint16_t arg_byte_offset = zig_shr_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT16_C(1); |
| 5198 | uint16_t end_byte_offset = UINT16_C(0); |
| 5199 | uint8_t arg_prev_byte; |
| 5200 | uint8_t byte_shift = zig_u8_intCast_u16(zig_subw_u16(UINT16_C(0), bits, UINT8_C(3))); |
| 3274 | 5201 | |
| 3275 | | zig_trap(); |
| 3276 | | } |
| 5202 | #if zig_big_endian |
| 5203 | res_byte_offset = size - res_byte_offset; |
| 5204 | arg_byte_offset = size - arg_byte_offset; |
| 5205 | end_byte_offset = size - end_byte_offset; |
| 5206 | #endif |
| 3277 | 5207 | |
| 3278 | | static inline void zig_div_ceil_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 3279 | | zig_trap(); |
| 3280 | | } |
| 5208 | { |
| 5209 | #if zig_little_endian |
| 5210 | arg_byte_offset -= UINT16_C(1); |
| 5211 | #endif |
| 3281 | 5212 | |
| 3282 | | zig_extern void __umodei4(uint32_t *res, const uint32_t *lhs, const uint32_t *rhs, uintptr_t bits); |
| 3283 | | static inline void zig_rem_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 3284 | | if (!is_signed) { |
| 3285 | | __umodei4(res, lhs, rhs, bits); |
| 3286 | | return; |
| 5213 | arg_prev_byte = zig_bitReverse_u8(arg_bytes[arg_byte_offset], UINT8_C(8)); |
| 5214 | |
| 5215 | #if zig_big_endian |
| 5216 | arg_byte_offset += UINT16_C(1); |
| 5217 | #endif |
| 3287 | 5218 | } |
| 3288 | 5219 | |
| 3289 | | zig_trap(); |
| 3290 | | } |
| 5220 | while (arg_byte_offset != end_byte_offset) { |
| 5221 | #if zig_big_endian |
| 5222 | res_byte_offset -= UINT16_C(1); |
| 5223 | #else |
| 5224 | arg_byte_offset -= UINT16_C(1); |
| 5225 | #endif |
| 3291 | 5226 | |
| 3292 | | static inline void zig_mod_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 3293 | | if (!is_signed) { |
| 3294 | | zig_rem_big(res, lhs, rhs, is_signed, bits); |
| 3295 | | return; |
| 5227 | { |
| 5228 | uint8_t arg_byte = zig_bitReverse_u8(arg_bytes[arg_byte_offset], UINT8_C(8)); |
| 5229 | |
| 5230 | res_bytes[res_byte_offset] = zig_u8_intCast_u16(zig_shr_u16(zig_or_u16( |
| 5231 | zig_shl_u16(zig_u16_intCast_u8(arg_byte), UINT8_C(8)), |
| 5232 | zig_u16_intCast_u8(arg_prev_byte) |
| 5233 | ), byte_shift)); |
| 5234 | arg_prev_byte = arg_byte; |
| 5235 | } |
| 5236 | |
| 5237 | #if zig_little_endian |
| 5238 | res_byte_offset += UINT16_C(1); |
| 5239 | #else |
| 5240 | arg_byte_offset += UINT16_C(1); |
| 5241 | #endif |
| 3296 | 5242 | } |
| 3297 | 5243 | |
| 3298 | | zig_trap(); |
| 5244 | { |
| 5245 | uint8_t arg_sign_fill = UINT8_C(0); |
| 5246 | |
| 5247 | #if zig_big_endian |
| 5248 | res_byte_offset -= UINT16_C(1); |
| 5249 | #endif |
| 5250 | |
| 5251 | if (is_signed) { |
| 5252 | int8_t signed_byte = zig_i8_bitCast_u8(arg_prev_byte, UINT8_C(8)); |
| 5253 | |
| 5254 | res_bytes[res_byte_offset] = zig_shr_i8(signed_byte, byte_shift); |
| 5255 | arg_sign_fill = zig_u8_bitCast_i8(zig_shr_i8(signed_byte, UINT8_C(7)), UINT8_C(8)); |
| 5256 | } else { |
| 5257 | res_bytes[res_byte_offset] = zig_shr_u8(arg_prev_byte, byte_shift); |
| 5258 | } |
| 5259 | |
| 5260 | #if zig_little_endian |
| 5261 | res_byte_offset += UINT16_C(1); |
| 5262 | memset(&res_bytes[res_byte_offset], arg_sign_fill, size - res_byte_offset); |
| 5263 | #else |
| 5264 | memset(&res_bytes[0], arg_sign_fill, res_byte_offset); |
| 5265 | #endif |
| 5266 | } |
| 3299 | 5267 | } |
| 3300 | 5268 | |
| 3301 | | static inline uint16_t zig_clz_big(const void *val, bool is_signed, uint16_t bits) { |
| 3302 | | const uint8_t *val_bytes = val; |
| 5269 | static inline uint16_t zig_popCount_big(const void *arg, bool is_signed, uint16_t bits) { |
| 5270 | const uint8_t *arg_bytes = arg; |
| 3303 | 5271 | uint16_t byte_offset = 0; |
| 3304 | | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 3305 | | uint16_t skip_bits = remaining_bytes * 8 - bits; |
| 3306 | | uint16_t total_lz = 0; |
| 3307 | | uint16_t limb_lz; |
| 5272 | uint16_t remaining_bytes = zig_shr_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT16_C(1); |
| 5273 | uint8_t top_bits = zig_u8_intCast_u16(remaining_bytes * CHAR_BIT - bits); |
| 5274 | uint16_t total_pc = 0; |
| 3308 | 5275 | (void)is_signed; |
| 3309 | 5276 | |
| 3310 | | #if zig_little_endian |
| 3311 | | byte_offset = remaining_bytes; |
| 5277 | #if zig_big_endian |
| 5278 | byte_offset = zig_int_bytes(bits); |
| 3312 | 5279 | #endif |
| 3313 | 5280 | |
| 3314 | 5281 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 3315 | | #if zig_little_endian |
| 5282 | uint8_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0); |
| 5283 | |
| 5284 | #if zig_big_endian |
| 3316 | 5285 | byte_offset -= 128 / CHAR_BIT; |
| 3317 | 5286 | #endif |
| 3318 | 5287 | |
| 3319 | 5288 | { |
| 3320 | | zig_u128 val_limb; |
| 5289 | zig_u128 arg_limb; |
| 3321 | 5290 | |
| 3322 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3323 | | limb_lz = zig_clz_u128(val_limb, 128 - skip_bits); |
| 5291 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5292 | total_pc += zig_popCount_u128(zig_u128_truncate_u128(arg_limb, limb_bits), limb_bits); |
| 3324 | 5293 | } |
| 3325 | 5294 | |
| 3326 | | total_lz += limb_lz; |
| 3327 | | if (limb_lz < 128 - skip_bits) return total_lz; |
| 3328 | | skip_bits = 0; |
| 3329 | 5295 | remaining_bytes -= 128 / CHAR_BIT; |
| 3330 | 5296 | |
| 3331 | | #if zig_big_endian |
| 5297 | #if zig_little_endian |
| 3332 | 5298 | byte_offset += 128 / CHAR_BIT; |
| 3333 | 5299 | #endif |
| 3334 | 5300 | } |
| 3335 | 5301 | |
| 3336 | 5302 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 3337 | | #if zig_little_endian |
| 5303 | uint8_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0); |
| 5304 | |
| 5305 | #if zig_big_endian |
| 3338 | 5306 | byte_offset -= 64 / CHAR_BIT; |
| 3339 | 5307 | #endif |
| 3340 | 5308 | |
| 3341 | 5309 | { |
| 3342 | | uint64_t val_limb; |
| 5310 | uint64_t arg_limb; |
| 3343 | 5311 | |
| 3344 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3345 | | limb_lz = zig_clz_u64(val_limb, 64 - skip_bits); |
| 5312 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5313 | total_pc += zig_popCount_u64(zig_u64_truncate_u64(arg_limb, limb_bits), limb_bits); |
| 3346 | 5314 | } |
| 3347 | 5315 | |
| 3348 | | total_lz += limb_lz; |
| 3349 | | if (limb_lz < 64 - skip_bits) return total_lz; |
| 3350 | | skip_bits = 0; |
| 3351 | 5316 | remaining_bytes -= 64 / CHAR_BIT; |
| 3352 | 5317 | |
| 3353 | | #if zig_big_endian |
| 5318 | #if zig_little_endian |
| 3354 | 5319 | byte_offset += 64 / CHAR_BIT; |
| 3355 | 5320 | #endif |
| 3356 | 5321 | } |
| 3357 | 5322 | |
| 3358 | 5323 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 3359 | | #if zig_little_endian |
| 5324 | uint8_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0); |
| 5325 | |
| 5326 | #if zig_big_endian |
| 3360 | 5327 | byte_offset -= 32 / CHAR_BIT; |
| 3361 | 5328 | #endif |
| 3362 | 5329 | |
| 3363 | 5330 | { |
| 3364 | | uint32_t val_limb; |
| 5331 | uint32_t arg_limb; |
| 3365 | 5332 | |
| 3366 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3367 | | limb_lz = zig_clz_u32(val_limb, 32 - skip_bits); |
| 5333 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5334 | total_pc += zig_popCount_u32(zig_u32_truncate_u32(arg_limb, limb_bits), limb_bits); |
| 3368 | 5335 | } |
| 3369 | 5336 | |
| 3370 | | total_lz += limb_lz; |
| 3371 | | if (limb_lz < 32 - skip_bits) return total_lz; |
| 3372 | | skip_bits = 0; |
| 3373 | 5337 | remaining_bytes -= 32 / CHAR_BIT; |
| 3374 | 5338 | |
| 3375 | | #if zig_big_endian |
| 5339 | #if zig_little_endian |
| 3376 | 5340 | byte_offset += 32 / CHAR_BIT; |
| 3377 | 5341 | #endif |
| 3378 | 5342 | } |
| 3379 | 5343 | |
| 3380 | 5344 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 3381 | | #if zig_little_endian |
| 5345 | uint8_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0); |
| 5346 | |
| 5347 | #if zig_big_endian |
| 3382 | 5348 | byte_offset -= 16 / CHAR_BIT; |
| 3383 | 5349 | #endif |
| 3384 | 5350 | |
| 3385 | 5351 | { |
| 3386 | | uint16_t val_limb; |
| 5352 | uint16_t arg_limb; |
| 3387 | 5353 | |
| 3388 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3389 | | limb_lz = zig_clz_u16(val_limb, 16 - skip_bits); |
| 5354 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5355 | total_pc += zig_popCount_u16(zig_u16_truncate_u16(arg_limb, limb_bits), limb_bits); |
| 3390 | 5356 | } |
| 3391 | 5357 | |
| 3392 | | total_lz += limb_lz; |
| 3393 | | if (limb_lz < 16 - skip_bits) return total_lz; |
| 3394 | | skip_bits = 0; |
| 3395 | 5358 | remaining_bytes -= 16 / CHAR_BIT; |
| 3396 | 5359 | |
| 3397 | | #if zig_big_endian |
| 5360 | #if zig_little_endian |
| 3398 | 5361 | byte_offset += 16 / CHAR_BIT; |
| 3399 | 5362 | #endif |
| 3400 | 5363 | } |
| 3401 | 5364 | |
| 3402 | 5365 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 3403 | | #if zig_little_endian |
| 5366 | uint8_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0); |
| 5367 | |
| 5368 | #if zig_big_endian |
| 3404 | 5369 | byte_offset -= 8 / CHAR_BIT; |
| 3405 | 5370 | #endif |
| 3406 | 5371 | |
| 3407 | 5372 | { |
| 3408 | | uint8_t val_limb; |
| 5373 | uint8_t arg_limb; |
| 3409 | 5374 | |
| 3410 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3411 | | limb_lz = zig_clz_u8(val_limb, 8 - skip_bits); |
| 5375 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5376 | total_pc += zig_popCount_u8(zig_u8_truncate_u8(arg_limb, limb_bits), limb_bits); |
| 3412 | 5377 | } |
| 3413 | 5378 | |
| 3414 | | total_lz += limb_lz; |
| 3415 | | if (limb_lz < 8 - skip_bits) return total_lz; |
| 3416 | | skip_bits = 0; |
| 3417 | 5379 | remaining_bytes -= 8 / CHAR_BIT; |
| 3418 | 5380 | |
| 3419 | | #if zig_big_endian |
| 5381 | #if zig_little_endian |
| 3420 | 5382 | byte_offset += 8 / CHAR_BIT; |
| 3421 | 5383 | #endif |
| 3422 | 5384 | } |
| 3423 | 5385 | |
| 3424 | | return total_lz; |
| 5386 | return total_pc; |
| 3425 | 5387 | } |
| 3426 | 5388 | |
| 3427 | | static inline uint16_t zig_ctz_big(const void *val, bool is_signed, uint16_t bits) { |
| 3428 | | const uint8_t *val_bytes = val; |
| 3429 | | uint16_t byte_offset = 0; |
| 3430 | | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 3431 | | uint16_t total_tz = 0; |
| 5389 | static inline uint16_t zig_ctz_big(const void *arg, bool is_signed, uint16_t bits) { |
| 5390 | const uint8_t *arg_bytes = arg; |
| 5391 | uint16_t byte_offset = UINT16_C(0); |
| 5392 | uint16_t remaining_bytes = zig_shr_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT16_C(1); |
| 5393 | uint8_t top_bits = zig_u8_intCast_u16(remaining_bytes * CHAR_BIT - bits); |
| 5394 | uint16_t total_tz = UINT16_C(0); |
| 3432 | 5395 | uint16_t limb_tz; |
| 3433 | 5396 | (void)is_signed; |
| 3434 | 5397 | |
| 3435 | 5398 | #if zig_big_endian |
| 3436 | | byte_offset = remaining_bytes; |
| 5399 | byte_offset = zig_int_bytes(bits); |
| 3437 | 5400 | #endif |
| 3438 | 5401 | |
| 3439 | 5402 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 5403 | uint8_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0); |
| 5404 | |
| 3440 | 5405 | #if zig_big_endian |
| 3441 | 5406 | byte_offset -= 128 / CHAR_BIT; |
| 3442 | 5407 | #endif |
| 3443 | 5408 | |
| 3444 | 5409 | { |
| 3445 | | zig_u128 val_limb; |
| 5410 | zig_u128 arg_limb; |
| 3446 | 5411 | |
| 3447 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3448 | | limb_tz = zig_ctz_u128(val_limb, 128); |
| 5412 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5413 | limb_tz = zig_ctz_u128(zig_u128_truncate_u128(arg_limb, limb_bits), limb_bits); |
| 3449 | 5414 | } |
| 3450 | 5415 | |
| 3451 | 5416 | total_tz += limb_tz; |
| 3452 | | if (limb_tz < 128) return total_tz; |
| 5417 | if (limb_tz < limb_bits) return total_tz; |
| 3453 | 5418 | remaining_bytes -= 128 / CHAR_BIT; |
| 3454 | 5419 | |
| 3455 | 5420 | #if zig_little_endian |
| ... | ... | @@ -3458,19 +5423,21 @@ static inline uint16_t zig_ctz_big(const void *val, bool is_signed, uint16_t bit |
| 3458 | 5423 | } |
| 3459 | 5424 | |
| 3460 | 5425 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 5426 | uint8_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0); |
| 5427 | |
| 3461 | 5428 | #if zig_big_endian |
| 3462 | 5429 | byte_offset -= 64 / CHAR_BIT; |
| 3463 | 5430 | #endif |
| 3464 | 5431 | |
| 3465 | 5432 | { |
| 3466 | | uint64_t val_limb; |
| 5433 | uint64_t arg_limb; |
| 3467 | 5434 | |
| 3468 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3469 | | limb_tz = zig_ctz_u64(val_limb, 64); |
| 5435 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5436 | limb_tz = zig_ctz_u64(zig_u64_truncate_u64(arg_limb, limb_bits), limb_bits); |
| 3470 | 5437 | } |
| 3471 | 5438 | |
| 3472 | 5439 | total_tz += limb_tz; |
| 3473 | | if (limb_tz < 64) return total_tz; |
| 5440 | if (limb_tz < limb_bits) return total_tz; |
| 3474 | 5441 | remaining_bytes -= 64 / CHAR_BIT; |
| 3475 | 5442 | |
| 3476 | 5443 | #if zig_little_endian |
| ... | ... | @@ -3479,19 +5446,21 @@ static inline uint16_t zig_ctz_big(const void *val, bool is_signed, uint16_t bit |
| 3479 | 5446 | } |
| 3480 | 5447 | |
| 3481 | 5448 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 5449 | uint8_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0); |
| 5450 | |
| 3482 | 5451 | #if zig_big_endian |
| 3483 | 5452 | byte_offset -= 32 / CHAR_BIT; |
| 3484 | 5453 | #endif |
| 3485 | 5454 | |
| 3486 | 5455 | { |
| 3487 | | uint32_t val_limb; |
| 5456 | uint32_t arg_limb; |
| 3488 | 5457 | |
| 3489 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3490 | | limb_tz = zig_ctz_u32(val_limb, 32); |
| 5458 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5459 | limb_tz = zig_ctz_u32(zig_u32_truncate_u32(arg_limb, limb_bits), limb_bits); |
| 3491 | 5460 | } |
| 3492 | 5461 | |
| 3493 | 5462 | total_tz += limb_tz; |
| 3494 | | if (limb_tz < 32) return total_tz; |
| 5463 | if (limb_tz < limb_bits) return total_tz; |
| 3495 | 5464 | remaining_bytes -= 32 / CHAR_BIT; |
| 3496 | 5465 | |
| 3497 | 5466 | #if zig_little_endian |
| ... | ... | @@ -3500,19 +5469,21 @@ static inline uint16_t zig_ctz_big(const void *val, bool is_signed, uint16_t bit |
| 3500 | 5469 | } |
| 3501 | 5470 | |
| 3502 | 5471 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 5472 | uint8_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0); |
| 5473 | |
| 3503 | 5474 | #if zig_big_endian |
| 3504 | 5475 | byte_offset -= 16 / CHAR_BIT; |
| 3505 | 5476 | #endif |
| 3506 | 5477 | |
| 3507 | 5478 | { |
| 3508 | | uint16_t val_limb; |
| 5479 | uint16_t arg_limb; |
| 3509 | 5480 | |
| 3510 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3511 | | limb_tz = zig_ctz_u16(val_limb, 16); |
| 5481 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5482 | limb_tz = zig_ctz_u16(zig_u16_truncate_u16(arg_limb, limb_bits), limb_bits); |
| 3512 | 5483 | } |
| 3513 | 5484 | |
| 3514 | 5485 | total_tz += limb_tz; |
| 3515 | | if (limb_tz < 16) return total_tz; |
| 5486 | if (limb_tz < limb_bits) return total_tz; |
| 3516 | 5487 | remaining_bytes -= 16 / CHAR_BIT; |
| 3517 | 5488 | |
| 3518 | 5489 | #if zig_little_endian |
| ... | ... | @@ -3521,19 +5492,21 @@ static inline uint16_t zig_ctz_big(const void *val, bool is_signed, uint16_t bit |
| 3521 | 5492 | } |
| 3522 | 5493 | |
| 3523 | 5494 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 5495 | uint8_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0); |
| 5496 | |
| 3524 | 5497 | #if zig_big_endian |
| 3525 | 5498 | byte_offset -= 8 / CHAR_BIT; |
| 3526 | 5499 | #endif |
| 3527 | 5500 | |
| 3528 | 5501 | { |
| 3529 | | uint8_t val_limb; |
| 5502 | uint8_t arg_limb; |
| 3530 | 5503 | |
| 3531 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3532 | | limb_tz = zig_ctz_u8(val_limb, 8); |
| 5504 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5505 | limb_tz = zig_ctz_u8(zig_u8_truncate_u8(arg_limb, limb_bits), limb_bits); |
| 3533 | 5506 | } |
| 3534 | 5507 | |
| 3535 | 5508 | total_tz += limb_tz; |
| 3536 | | if (limb_tz < 8) return total_tz; |
| 5509 | if (limb_tz < limb_bits) return total_tz; |
| 3537 | 5510 | remaining_bytes -= 8 / CHAR_BIT; |
| 3538 | 5511 | |
| 3539 | 5512 | #if zig_little_endian |
| ... | ... | @@ -3544,113 +5517,141 @@ static inline uint16_t zig_ctz_big(const void *val, bool is_signed, uint16_t bit |
| 3544 | 5517 | return total_tz; |
| 3545 | 5518 | } |
| 3546 | 5519 | |
| 3547 | | static inline uint16_t zig_popcount_big(const void *val, bool is_signed, uint16_t bits) { |
| 3548 | | const uint8_t *val_bytes = val; |
| 3549 | | uint16_t byte_offset = 0; |
| 3550 | | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 3551 | | uint16_t total_pc = 0; |
| 5520 | static inline uint16_t zig_clz_big(const void *arg, bool is_signed, uint16_t bits) { |
| 5521 | const uint8_t *arg_bytes = arg; |
| 5522 | uint16_t byte_offset = zig_shr_u16(bits - UINT16_C(1), UINT8_C(3)) + UINT16_C(1); |
| 5523 | uint16_t remaining_bytes = byte_offset; |
| 5524 | uint8_t top_bits = zig_u8_intCast_u16(remaining_bytes * CHAR_BIT - bits); |
| 5525 | bool sign_limb = true; |
| 5526 | uint16_t total_lz = UINT16_C(0); |
| 5527 | uint16_t limb_lz; |
| 3552 | 5528 | (void)is_signed; |
| 3553 | 5529 | |
| 3554 | 5530 | #if zig_big_endian |
| 3555 | | byte_offset = remaining_bytes; |
| 5531 | byte_offset = zig_int_bytes(bits) - remaining_bytes; |
| 3556 | 5532 | #endif |
| 3557 | 5533 | |
| 3558 | 5534 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 3559 | | #if zig_big_endian |
| 5535 | uint8_t limb_bits = UINT8_C(128) - (sign_limb ? top_bits : UINT8_C(0)); |
| 5536 | |
| 5537 | #if zig_little_endian |
| 3560 | 5538 | byte_offset -= 128 / CHAR_BIT; |
| 3561 | 5539 | #endif |
| 3562 | 5540 | |
| 3563 | 5541 | { |
| 3564 | | zig_u128 val_limb; |
| 5542 | zig_u128 arg_limb; |
| 3565 | 5543 | |
| 3566 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3567 | | total_pc += zig_popcount_u128(val_limb, 128); |
| 5544 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5545 | limb_lz = zig_clz_u128(zig_u128_truncate_u128(arg_limb, limb_bits), limb_bits); |
| 3568 | 5546 | } |
| 3569 | 5547 | |
| 5548 | total_lz += limb_lz; |
| 5549 | if (limb_lz < limb_bits) return total_lz; |
| 5550 | sign_limb = false; |
| 3570 | 5551 | remaining_bytes -= 128 / CHAR_BIT; |
| 3571 | 5552 | |
| 3572 | | #if zig_little_endian |
| 5553 | #if zig_big_endian |
| 3573 | 5554 | byte_offset += 128 / CHAR_BIT; |
| 3574 | 5555 | #endif |
| 3575 | 5556 | } |
| 3576 | 5557 | |
| 3577 | 5558 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 3578 | | #if zig_big_endian |
| 5559 | uint8_t limb_bits = UINT8_C(64) - (sign_limb ? top_bits : UINT8_C(0)); |
| 5560 | |
| 5561 | #if zig_little_endian |
| 3579 | 5562 | byte_offset -= 64 / CHAR_BIT; |
| 3580 | 5563 | #endif |
| 3581 | 5564 | |
| 3582 | 5565 | { |
| 3583 | | uint64_t val_limb; |
| 5566 | uint64_t arg_limb; |
| 3584 | 5567 | |
| 3585 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3586 | | total_pc += zig_popcount_u64(val_limb, 64); |
| 5568 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5569 | limb_lz = zig_clz_u64(zig_u64_truncate_u64(arg_limb, limb_bits), limb_bits); |
| 3587 | 5570 | } |
| 3588 | 5571 | |
| 5572 | total_lz += limb_lz; |
| 5573 | if (limb_lz < limb_bits) return total_lz; |
| 5574 | sign_limb = false; |
| 3589 | 5575 | remaining_bytes -= 64 / CHAR_BIT; |
| 3590 | 5576 | |
| 3591 | | #if zig_little_endian |
| 5577 | #if zig_big_endian |
| 3592 | 5578 | byte_offset += 64 / CHAR_BIT; |
| 3593 | 5579 | #endif |
| 3594 | 5580 | } |
| 3595 | 5581 | |
| 3596 | 5582 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 3597 | | #if zig_big_endian |
| 5583 | uint8_t limb_bits = UINT8_C(32) - (sign_limb ? top_bits : UINT8_C(0)); |
| 5584 | |
| 5585 | #if zig_little_endian |
| 3598 | 5586 | byte_offset -= 32 / CHAR_BIT; |
| 3599 | 5587 | #endif |
| 3600 | 5588 | |
| 3601 | 5589 | { |
| 3602 | | uint32_t val_limb; |
| 5590 | uint32_t arg_limb; |
| 3603 | 5591 | |
| 3604 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3605 | | total_pc += zig_popcount_u32(val_limb, 32); |
| 5592 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5593 | limb_lz = zig_clz_u32(zig_u32_truncate_u32(arg_limb, limb_bits), limb_bits); |
| 3606 | 5594 | } |
| 3607 | 5595 | |
| 5596 | total_lz += limb_lz; |
| 5597 | if (limb_lz < limb_bits) return total_lz; |
| 5598 | sign_limb = false; |
| 3608 | 5599 | remaining_bytes -= 32 / CHAR_BIT; |
| 3609 | 5600 | |
| 3610 | | #if zig_little_endian |
| 5601 | #if zig_big_endian |
| 3611 | 5602 | byte_offset += 32 / CHAR_BIT; |
| 3612 | 5603 | #endif |
| 3613 | 5604 | } |
| 3614 | 5605 | |
| 3615 | 5606 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 3616 | | #if zig_big_endian |
| 5607 | uint8_t limb_bits = UINT8_C(16) - (sign_limb ? top_bits : UINT8_C(0)); |
| 5608 | |
| 5609 | #if zig_little_endian |
| 3617 | 5610 | byte_offset -= 16 / CHAR_BIT; |
| 3618 | 5611 | #endif |
| 3619 | 5612 | |
| 3620 | 5613 | { |
| 3621 | | uint16_t val_limb; |
| 5614 | uint16_t arg_limb; |
| 3622 | 5615 | |
| 3623 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3624 | | total_pc = zig_popcount_u16(val_limb, 16); |
| 5616 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5617 | limb_lz = zig_clz_u16(zig_u16_truncate_u16(arg_limb, limb_bits), limb_bits); |
| 3625 | 5618 | } |
| 3626 | 5619 | |
| 5620 | total_lz += limb_lz; |
| 5621 | if (limb_lz < limb_bits) return total_lz; |
| 5622 | sign_limb = false; |
| 3627 | 5623 | remaining_bytes -= 16 / CHAR_BIT; |
| 3628 | 5624 | |
| 3629 | | #if zig_little_endian |
| 5625 | #if zig_big_endian |
| 3630 | 5626 | byte_offset += 16 / CHAR_BIT; |
| 3631 | 5627 | #endif |
| 3632 | 5628 | } |
| 3633 | 5629 | |
| 3634 | 5630 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 3635 | | #if zig_big_endian |
| 5631 | uint8_t limb_bits = UINT8_C(8) - (sign_limb ? top_bits : UINT8_C(0)); |
| 5632 | |
| 5633 | #if zig_little_endian |
| 3636 | 5634 | byte_offset -= 8 / CHAR_BIT; |
| 3637 | 5635 | #endif |
| 3638 | 5636 | |
| 3639 | 5637 | { |
| 3640 | | uint8_t val_limb; |
| 5638 | uint8_t arg_limb; |
| 3641 | 5639 | |
| 3642 | | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 3643 | | total_pc = zig_popcount_u8(val_limb, 8); |
| 5640 | memcpy(&arg_limb, &arg_bytes[byte_offset], sizeof(arg_limb)); |
| 5641 | limb_lz = zig_clz_u8(zig_u8_truncate_u8(arg_limb, limb_bits), limb_bits); |
| 3644 | 5642 | } |
| 3645 | 5643 | |
| 5644 | total_lz += limb_lz; |
| 5645 | if (limb_lz < limb_bits) return total_lz; |
| 5646 | sign_limb = false; |
| 3646 | 5647 | remaining_bytes -= 8 / CHAR_BIT; |
| 3647 | 5648 | |
| 3648 | | #if zig_little_endian |
| 5649 | #if zig_big_endian |
| 3649 | 5650 | byte_offset += 8 / CHAR_BIT; |
| 3650 | 5651 | #endif |
| 3651 | 5652 | } |
| 3652 | 5653 | |
| 3653 | | return total_pc; |
| 5654 | return total_lz; |
| 3654 | 5655 | } |
| 3655 | 5656 | |
| 3656 | 5657 | /* ========================= Floating Point Support ========================= */ |
| ... | ... | @@ -3687,29 +5688,29 @@ long double __cdecl nanl(char const* input); |
| 3687 | 5688 | #define zig_make_special_f80(sign, name, arg, repr) sign zig_make_f80 (__builtin_##name, )(arg) |
| 3688 | 5689 | #define zig_make_special_f128(sign, name, arg, repr) sign zig_make_f128(__builtin_##name, )(arg) |
| 3689 | 5690 | #else |
| 3690 | | #define zig_make_special_f16(sign, name, arg, repr) zig_bitCast_f16 (repr) |
| 3691 | | #define zig_make_special_f32(sign, name, arg, repr) zig_bitCast_f32 (repr) |
| 3692 | | #define zig_make_special_f64(sign, name, arg, repr) zig_bitCast_f64 (repr) |
| 3693 | | #define zig_make_special_f80(sign, name, arg, repr) zig_bitCast_f80 (repr) |
| 3694 | | #define zig_make_special_f128(sign, name, arg, repr) zig_bitCast_f128(repr) |
| 5691 | #define zig_make_special_f16(sign, name, arg, repr) zig_f16_bitCast_u16 (repr) |
| 5692 | #define zig_make_special_f32(sign, name, arg, repr) zig_f32_bitCast_u32 (repr) |
| 5693 | #define zig_make_special_f64(sign, name, arg, repr) zig_f64_bitCast_u64 (repr) |
| 5694 | #define zig_make_special_f80(sign, name, arg, repr) zig_f80_bitCast_u128(repr) |
| 5695 | #define zig_make_special_f128(sign, name, arg, repr) zig_f128_bitCast_u128(repr) |
| 3695 | 5696 | #endif |
| 3696 | 5697 | |
| 3697 | 5698 | #define zig_has_f16 1 |
| 3698 | 5699 | #define zig_libc_name_f16(name) __##name##h |
| 3699 | 5700 | #define zig_init_special_f16(sign, name, arg, repr) zig_make_special_f16(sign, name, arg, repr) |
| 3700 | | #if FLT_MANT_DIG == 11 |
| 5701 | #if !defined(ZIG_TARGET_SOFT_COMPILER_RT_F16_ABI) && FLT_MANT_DIG == 11 |
| 3701 | 5702 | typedef float zig_f16; |
| 3702 | 5703 | #define zig_make_f16(fp, repr) fp##f |
| 3703 | | #elif DBL_MANT_DIG == 11 |
| 5704 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F16_ABI) && DBL_MANT_DIG == 11 |
| 3704 | 5705 | typedef double zig_f16; |
| 3705 | 5706 | #define zig_make_f16(fp, repr) fp |
| 3706 | | #elif LDBL_MANT_DIG == 11 |
| 5707 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F16_ABI) && LDBL_MANT_DIG == 11 |
| 3707 | 5708 | typedef long double zig_f16; |
| 3708 | 5709 | #define zig_make_f16(fp, repr) fp##l |
| 3709 | | #elif FLT16_MANT_DIG == 11 && (zig_has_builtin(inff16) || defined(zig_gcc)) |
| 5710 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F16_ABI) && FLT16_MANT_DIG == 11 && (zig_has_builtin(inff16) || defined(zig_gcc)) |
| 3710 | 5711 | typedef _Float16 zig_f16; |
| 3711 | 5712 | #define zig_make_f16(fp, repr) fp##f16 |
| 3712 | | #elif defined(__SIZEOF_FP16__) |
| 5713 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F16_ABI) && defined(__SIZEOF_FP16__) |
| 3713 | 5714 | typedef __fp16 zig_f16; |
| 3714 | 5715 | #define zig_make_f16(fp, repr) fp##f16 |
| 3715 | 5716 | #else |
| ... | ... | @@ -3723,11 +5724,6 @@ typedef uint16_t zig_f16; |
| 3723 | 5724 | #undef zig_init_special_f16 |
| 3724 | 5725 | #define zig_init_special_f16(sign, name, arg, repr) repr |
| 3725 | 5726 | #endif |
| 3726 | | #if defined(zig_darwin) && defined(zig_x86) |
| 3727 | | typedef uint16_t zig_compiler_rt_f16; |
| 3728 | | #else |
| 3729 | | typedef zig_f16 zig_compiler_rt_f16; |
| 3730 | | #endif |
| 3731 | 5727 | |
| 3732 | 5728 | #define zig_has_f32 1 |
| 3733 | 5729 | #define zig_libc_name_f32(name) name##f |
| ... | ... | @@ -3736,16 +5732,16 @@ typedef zig_f16 zig_compiler_rt_f16; |
| 3736 | 5732 | #else |
| 3737 | 5733 | #define zig_init_special_f32(sign, name, arg, repr) zig_make_special_f32(sign, name, arg, repr) |
| 3738 | 5734 | #endif |
| 3739 | | #if FLT_MANT_DIG == 24 |
| 5735 | #if !defined(ZIG_TARGET_SOFT_COMPILER_RT_F32_ABI) && FLT_MANT_DIG == 24 |
| 3740 | 5736 | typedef float zig_f32; |
| 3741 | 5737 | #define zig_make_f32(fp, repr) fp##f |
| 3742 | | #elif DBL_MANT_DIG == 24 |
| 5738 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F32_ABI) && DBL_MANT_DIG == 24 |
| 3743 | 5739 | typedef double zig_f32; |
| 3744 | 5740 | #define zig_make_f32(fp, repr) fp |
| 3745 | | #elif LDBL_MANT_DIG == 24 |
| 5741 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F32_ABI) && LDBL_MANT_DIG == 24 |
| 3746 | 5742 | typedef long double zig_f32; |
| 3747 | 5743 | #define zig_make_f32(fp, repr) fp##l |
| 3748 | | #elif FLT32_MANT_DIG == 24 |
| 5744 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F32_ABI) && FLT32_MANT_DIG == 24 |
| 3749 | 5745 | typedef _Float32 zig_f32; |
| 3750 | 5746 | #define zig_make_f32(fp, repr) fp##f32 |
| 3751 | 5747 | #else |
| ... | ... | @@ -3768,19 +5764,19 @@ typedef uint32_t zig_f32; |
| 3768 | 5764 | #else |
| 3769 | 5765 | #define zig_init_special_f64(sign, name, arg, repr) zig_make_special_f64(sign, name, arg, repr) |
| 3770 | 5766 | #endif |
| 3771 | | #if FLT_MANT_DIG == 53 |
| 5767 | #if !defined(ZIG_TARGET_SOFT_COMPILER_RT_F64_ABI) && FLT_MANT_DIG == 53 |
| 3772 | 5768 | typedef float zig_f64; |
| 3773 | 5769 | #define zig_make_f64(fp, repr) fp##f |
| 3774 | | #elif DBL_MANT_DIG == 53 |
| 5770 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F64_ABI) && DBL_MANT_DIG == 53 |
| 3775 | 5771 | typedef double zig_f64; |
| 3776 | 5772 | #define zig_make_f64(fp, repr) fp |
| 3777 | | #elif LDBL_MANT_DIG == 53 |
| 5773 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F64_ABI) && LDBL_MANT_DIG == 53 |
| 3778 | 5774 | typedef long double zig_f64; |
| 3779 | 5775 | #define zig_make_f64(fp, repr) fp##l |
| 3780 | | #elif FLT64_MANT_DIG == 53 |
| 5776 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F64_ABI) && FLT64_MANT_DIG == 53 |
| 3781 | 5777 | typedef _Float64 zig_f64; |
| 3782 | 5778 | #define zig_make_f64(fp, repr) fp##f64 |
| 3783 | | #elif FLT32X_MANT_DIG == 53 |
| 5779 | #elif !defined(ZIG_TARGET_SOFT_COMPILER_RT_F64_ABI) && FLT32X_MANT_DIG == 53 |
| 3784 | 5780 | typedef _Float32x zig_f64; |
| 3785 | 5781 | #define zig_make_f64(fp, repr) fp##f32x |
| 3786 | 5782 | #else |
| ... | ... | @@ -3798,7 +5794,14 @@ typedef uint64_t zig_f64; |
| 3798 | 5794 | #define zig_has_f80 1 |
| 3799 | 5795 | #define zig_libc_name_f80(name) __##name##x |
| 3800 | 5796 | #define zig_init_special_f80(sign, name, arg, repr) zig_make_special_f80(sign, name, arg, repr) |
| 3801 | | #if FLT_MANT_DIG == 64 |
| 5797 | #ifdef ZIG_TARGET_SOFT_COMPILER_RT_F80_ABI |
| 5798 | #undef zig_has_f80 |
| 5799 | typedef struct { uint64_t mantissa; uint16_t exponent; } zig_f80; |
| 5800 | #define zig_init_repr_f80(mantissa, exponent) { .mant##issa = mantissa, .expo##nent = exponent } |
| 5801 | #define zig_make_repr_f80(mantissa, exponent) (zig_f80)zig_init_repr_f80(mantissa, exponent) |
| 5802 | #define zig_mantissa_repr_f80(arg) (arg).mantissa |
| 5803 | #define zig_exponent_repr_f80(arg) (arg).exponent |
| 5804 | #elif FLT_MANT_DIG == 64 |
| 3802 | 5805 | typedef float zig_f80; |
| 3803 | 5806 | #define zig_make_f80(fp, repr) fp##f |
| 3804 | 5807 | #elif DBL_MANT_DIG == 64 |
| ... | ... | @@ -3818,68 +5821,91 @@ typedef __float80 zig_f80; |
| 3818 | 5821 | #define zig_make_f80(fp, repr) fp##l |
| 3819 | 5822 | #else |
| 3820 | 5823 | #undef zig_has_f80 |
| 3821 | | #define zig_has_f80 0 |
| 3822 | | #define zig_repr_f80 u128 |
| 3823 | 5824 | typedef zig_u128 zig_f80; |
| 5825 | #define zig_init_repr_f80(mantissa, exponent) zig_init_u128(exponent, mantissa) |
| 5826 | #define zig_make_repr_f80(mantissa, exponent) zig_make_u128(exponent, mantissa) |
| 5827 | #define zig_mantissa_repr_f80(arg) zig_lo_u128(arg) |
| 5828 | #define zig_exponent_repr_f80(arg) (uint16_t)zig_hi_u128(arg) |
| 5829 | #endif |
| 5830 | #ifndef zig_has_f80 |
| 5831 | #define zig_has_f80 0 |
| 3824 | 5832 | #define zig_make_f80(fp, repr) repr |
| 5833 | #ifndef zig_make_repr_f80 |
| 5834 | #define zig_make_repr_f80(mantissa, exponent) (zig_f80)zig_init_repr_f80(mantissa, exponent) |
| 5835 | #endif |
| 3825 | 5836 | #undef zig_make_special_f80 |
| 3826 | 5837 | #define zig_make_special_f80(sign, name, arg, repr) repr |
| 3827 | 5838 | #undef zig_init_special_f80 |
| 3828 | 5839 | #define zig_init_special_f80(sign, name, arg, repr) repr |
| 3829 | 5840 | #endif |
| 3830 | 5841 | |
| 3831 | | #if defined(zig_gcc) && defined(zig_x86) |
| 3832 | | #define zig_f128_has_miscompilations 1 |
| 3833 | | #else |
| 3834 | | #define zig_f128_has_miscompilations 0 |
| 3835 | | #endif |
| 3836 | | |
| 3837 | 5842 | #define zig_has_f128 1 |
| 3838 | 5843 | #define zig_libc_name_f128(name) name##f128 |
| 3839 | 5844 | #define zig_init_special_f128(sign, name, arg, repr) zig_make_special_f128(sign, name, arg, repr) |
| 3840 | | #if !zig_f128_has_miscompilations && FLT_MANT_DIG == 113 |
| 5845 | #ifdef ZIG_TARGET_SOFT_COMPILER_RT_F128_ABI |
| 5846 | #undef zig_has_f128 |
| 5847 | #if zig_little_endian |
| 5848 | typedef struct { uint64_t lo, hi; } zig_f128; |
| 5849 | #else |
| 5850 | typedef struct { uint64_t hi, lo; } zig_f128; |
| 5851 | #endif |
| 5852 | #define zig_init_repr_f128(hi, lo) { .h##i = hi, .l##o = lo } |
| 5853 | #define zig_lo_repr_f128(arg) (arg).lo |
| 5854 | #define zig_hi_repr_f128(arg) (arg).hi |
| 5855 | #elif FLT_MANT_DIG == 113 |
| 3841 | 5856 | typedef float zig_f128; |
| 3842 | 5857 | #define zig_make_f128(fp, repr) fp##f |
| 3843 | | #elif !zig_f128_has_miscompilations && DBL_MANT_DIG == 113 |
| 5858 | #elif DBL_MANT_DIG == 113 |
| 3844 | 5859 | typedef double zig_f128; |
| 3845 | 5860 | #define zig_make_f128(fp, repr) fp |
| 3846 | | #elif !zig_f128_has_miscompilations && LDBL_MANT_DIG == 113 |
| 5861 | #elif LDBL_MANT_DIG == 113 |
| 3847 | 5862 | typedef long double zig_f128; |
| 3848 | 5863 | #define zig_make_f128(fp, repr) fp##l |
| 3849 | | #elif !zig_f128_has_miscompilations && FLT128_MANT_DIG == 113 |
| 5864 | #elif FLT128_MANT_DIG == 113 |
| 3850 | 5865 | typedef _Float128 zig_f128; |
| 3851 | 5866 | #define zig_make_f128(fp, repr) fp##f128 |
| 3852 | | #elif !zig_f128_has_miscompilations && FLT64X_MANT_DIG == 113 |
| 5867 | #elif FLT64X_MANT_DIG == 113 |
| 3853 | 5868 | typedef _Float64x zig_f128; |
| 3854 | 5869 | #define zig_make_f128(fp, repr) fp##f64x |
| 3855 | | #elif !zig_f128_has_miscompilations && defined(__SIZEOF_FLOAT128__) |
| 5870 | #elif defined(__SIZEOF_FLOAT128__) |
| 3856 | 5871 | typedef __float128 zig_f128; |
| 3857 | 5872 | #define zig_make_f128(fp, repr) fp##q |
| 3858 | 5873 | #undef zig_make_special_f128 |
| 3859 | 5874 | #define zig_make_special_f128(sign, name, arg, repr) sign __builtin_##name##f128(arg) |
| 3860 | 5875 | #else |
| 3861 | 5876 | #undef zig_has_f128 |
| 3862 | | #define zig_has_f128 0 |
| 3863 | | #undef zig_make_special_f128 |
| 3864 | | #undef zig_init_special_f128 |
| 3865 | | #if defined(zig_darwin) || defined(zig_aarch64) |
| 3866 | | typedef __attribute__((__vector_size__(2 * sizeof(uint64_t)))) uint64_t zig_v2u64; |
| 3867 | | zig_basic_operator(zig_v2u64, xor_v2u64, ^) |
| 3868 | | #define zig_repr_f128 v2u64 |
| 3869 | | typedef zig_v2u64 zig_f128; |
| 3870 | | #define zig_make_f128_zig_make_u128(hi, lo) (zig_f128){ lo, hi } |
| 3871 | | #define zig_make_f128_zig_init_u128 zig_make_f128_zig_make_u128 |
| 3872 | | #define zig_make_f128(fp, repr) zig_make_f128_##repr |
| 3873 | | #define zig_make_special_f128(sign, name, arg, repr) zig_make_f128_##repr |
| 3874 | | #define zig_init_special_f128(sign, name, arg, repr) zig_make_f128_##repr |
| 3875 | | #else |
| 3876 | | #define zig_repr_f128 u128 |
| 5877 | #if defined(zig_x86_64) && defined(ZIG_TARGET_ABI_MSVC) |
| 5878 | #if defined(zig_msvc) && !defined(__clang__) |
| 5879 | #include <emmintrin.h> |
| 5880 | typedef __m128i zig_f128; |
| 5881 | #define zig_init_repr_f128(hi, lo) { .m128i_u64 = { lo, hi } } |
| 5882 | #define zig_lo_repr_f128(arg) (arg).m128i_u64[0] |
| 5883 | #define zig_hi_repr_f128(arg) (arg).m128i_u64[1] |
| 5884 | #else |
| 5885 | typedef __attribute__((__vector_size__(2 * sizeof(uint64_t)))) uint64_t zig_f128; |
| 5886 | #define zig_init_repr_f128(hi, lo) { lo, hi } |
| 5887 | #define zig_lo_repr_f128(arg) (arg)[0] |
| 5888 | #define zig_hi_repr_f128(arg) (arg)[1] |
| 5889 | #endif |
| 5890 | #else |
| 3877 | 5891 | typedef zig_u128 zig_f128; |
| 5892 | #define zig_init_repr_f128(hi, lo) zig_init_u128(hi, lo) |
| 5893 | #define zig_make_repr_f128(hi, lo) zig_make_u128(hi, lo) |
| 5894 | #define zig_lo_repr_f128(arg) zig_lo_u128(arg) |
| 5895 | #define zig_hi_repr_f128(arg) zig_hi_u128(arg) |
| 5896 | #endif |
| 5897 | #endif |
| 5898 | #ifndef zig_has_f128 |
| 5899 | #define zig_has_f128 0 |
| 3878 | 5900 | #define zig_make_f128(fp, repr) repr |
| 5901 | #ifndef zig_make_repr_f128 |
| 5902 | #define zig_make_repr_f128(hi, lo) (zig_f128)zig_init_repr_f128(hi, lo) |
| 5903 | #endif |
| 5904 | #undef zig_make_special_f128 |
| 3879 | 5905 | #define zig_make_special_f128(sign, name, arg, repr) repr |
| 5906 | #undef zig_init_special_f128 |
| 3880 | 5907 | #define zig_init_special_f128(sign, name, arg, repr) repr |
| 3881 | 5908 | #endif |
| 3882 | | #endif |
| 3883 | 5909 | |
| 3884 | 5910 | #if !defined(zig_msvc) && defined(ZIG_TARGET_ABI_MSVC) |
| 3885 | 5911 | /* Emulate msvc abi on a gnu compiler */ |
| ... | ... | @@ -3892,84 +5918,141 @@ typedef zig_f128 zig_c_longdouble; |
| 3892 | 5918 | typedef long double zig_c_longdouble; |
| 3893 | 5919 | #endif |
| 3894 | 5920 | |
| 3895 | | #define zig_bitCast_float(Type, ReprType) \ |
| 3896 | | static inline zig_##Type zig_bitCast_##Type(ReprType repr) { \ |
| 3897 | | zig_##Type result; \ |
| 3898 | | memcpy(&result, &repr, sizeof(result)); \ |
| 3899 | | return result; \ |
| 5921 | #if __AVR__ |
| 5922 | typedef signed char zig_FloatCompareResult; |
| 5923 | #elif defined(zig_aarch64) |
| 5924 | typedef signed int zig_FloatCompareResult; |
| 5925 | #elif __SIZEOF_LONG__ >= __SIZEOF_POINTER__ |
| 5926 | typedef signed long zig_FloatCompareResult; |
| 5927 | #else |
| 5928 | typedef signed long long zig_FloatCompareResult; |
| 5929 | #endif |
| 5930 | |
| 5931 | #define zig_bitCast_float(w, iw, UnsignedReprType, SignedReprType) \ |
| 5932 | static inline zig_f##w zig_f##w##_bitCast_u##iw(UnsignedReprType arg) { \ |
| 5933 | zig_f##w res; \ |
| 5934 | memcpy(&res, &arg, sizeof(zig_f##w)); \ |
| 5935 | return res; \ |
| 5936 | } \ |
| 5937 | static inline zig_f##w zig_f##w##_bitCast_i##iw(SignedReprType arg) { \ |
| 5938 | zig_f##w res; \ |
| 5939 | memcpy(&res, &arg, sizeof(zig_f##w)); \ |
| 5940 | return res; \ |
| 5941 | } \ |
| 5942 | static inline UnsignedReprType zig_u##iw##_bitCast_f##w(zig_f##w arg) { \ |
| 5943 | UnsignedReprType res; \ |
| 5944 | memcpy(&res, &arg, sizeof(zig_f##w)); \ |
| 5945 | return zig_u##iw##_truncate_u##iw(res, w); \ |
| 5946 | } \ |
| 5947 | static inline SignedReprType zig_i##iw##_bitCast_f##w(zig_f##w arg) { \ |
| 5948 | SignedReprType res; \ |
| 5949 | memcpy(&res, &arg, sizeof(zig_f##w)); \ |
| 5950 | return zig_i##iw##_truncate_i##iw(res, w); \ |
| 3900 | 5951 | } |
| 3901 | | zig_bitCast_float(f16, uint16_t) |
| 3902 | | zig_bitCast_float(f32, uint32_t) |
| 3903 | | zig_bitCast_float(f64, uint64_t) |
| 3904 | | zig_bitCast_float(f80, zig_u128) |
| 3905 | | zig_bitCast_float(f128, zig_u128) |
| 5952 | zig_bitCast_float(16, 16, uint16_t, int16_t) |
| 5953 | zig_bitCast_float(32, 32, uint32_t, int32_t) |
| 5954 | zig_bitCast_float(64, 64, uint64_t, int64_t) |
| 5955 | #if zig_has_f80 |
| 5956 | zig_bitCast_float(80, 128, zig_u128, zig_i128) |
| 5957 | #else |
| 5958 | static inline zig_f80 zig_f80_bitCast_u128(zig_u128 arg) { |
| 5959 | return zig_make_repr_f80(zig_lo_u128(arg), (uint16_t)zig_hi_u128(arg)); |
| 5960 | } |
| 5961 | static inline zig_f80 zig_f80_bitCast_i128(zig_i128 arg) { |
| 5962 | return zig_make_repr_f80(zig_lo_i128(arg), (uint16_t)zig_hi_i128(arg)); |
| 5963 | } |
| 5964 | static inline zig_u128 zig_u128_bitCast_f80(zig_f80 arg) { |
| 5965 | return zig_make_u128(zig_exponent_repr_f80(arg), zig_mantissa_repr_f80(arg)); |
| 5966 | } |
| 5967 | static inline zig_i128 zig_i128_bitCast_f80(zig_f80 arg) { |
| 5968 | return zig_make_i128((int16_t)zig_exponent_repr_f80(arg), zig_mantissa_repr_f80(arg)); |
| 5969 | } |
| 5970 | #endif |
| 5971 | static inline zig_f80 zig_f80_bitCast_big(const void *arg) { |
| 5972 | return zig_f80_bitCast_u128(zig_u128_truncate_big(arg, UINT8_C(80), false, UINT16_C(80))); |
| 5973 | } |
| 5974 | static inline void zig_big_bitCast_f80(void *res, zig_f80 arg, bool res_is_signed, uint16_t res_bits) { |
| 5975 | if (res_is_signed) { |
| 5976 | zig_big_truncate_i128(res, zig_i128_bitCast_f80(arg), res_is_signed, res_bits); |
| 5977 | } else { |
| 5978 | zig_big_truncate_u128(res, zig_u128_bitCast_f80(arg), res_is_signed, res_bits); |
| 5979 | } |
| 5980 | } |
| 5981 | #if zig_has_f128 |
| 5982 | zig_bitCast_float(128, 128, zig_u128, zig_i128) |
| 5983 | #else |
| 5984 | static inline zig_f128 zig_f128_bitCast_u128(zig_u128 arg) { |
| 5985 | return zig_make_repr_f128(zig_hi_u128(arg), zig_lo_u128(arg)); |
| 5986 | } |
| 5987 | static inline zig_f128 zig_f128_bitCast_i128(zig_i128 arg) { |
| 5988 | return zig_make_repr_f128((uint64_t)zig_hi_i128(arg), zig_lo_i128(arg)); |
| 5989 | } |
| 5990 | static inline zig_u128 zig_u128_bitCast_f128(zig_f128 arg) { |
| 5991 | return zig_make_u128(zig_hi_repr_f128(arg), zig_lo_repr_f128(arg)); |
| 5992 | } |
| 5993 | static inline zig_i128 zig_i128_bitCast_f128(zig_f128 arg) { |
| 5994 | return zig_make_i128((int64_t)zig_hi_repr_f128(arg), zig_lo_repr_f128(arg)); |
| 5995 | } |
| 5996 | #endif |
| 3906 | 5997 | |
| 3907 | | #define zig_convert_builtin(ExternResType, ResType, operation, ExternArgType, ArgType, version) \ |
| 3908 | | zig_extern ExternResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \ |
| 3909 | | zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(ExternArgType); \ |
| 5998 | #define zig_convert_float_00(ResType, operation, ArgType, version) \ |
| 5999 | zig_extern ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \ |
| 6000 | zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(ArgType arg); \ |
| 6001 | return zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \ |
| 6002 | zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(arg) |
| 6003 | #define zig_convert_float_01(ResType, operation, ArgType, version) \ |
| 6004 | zig_convert_float_00(ResType, operation, ArgType, version) |
| 6005 | #define zig_convert_float_10(ResType, operation, ArgType, version) \ |
| 6006 | zig_convert_float_00(ResType, operation, ArgType, version) |
| 6007 | #define zig_convert_float_11(ResType, operation, ArgType, version) \ |
| 6008 | return (ResType)arg |
| 6009 | #define zig_convert_float(res_when, ResType, operation, arg_when, ArgType, version) \ |
| 3910 | 6010 | static inline ResType zig_expand_concat(zig_expand_concat(zig_##operation, \ |
| 3911 | 6011 | zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType)(ArgType arg) { \ |
| 3912 | | ResType res; \ |
| 3913 | | ExternResType extern_res; \ |
| 3914 | | ExternArgType extern_arg; \ |
| 3915 | | memcpy(&extern_arg, &arg, sizeof(extern_arg)); \ |
| 3916 | | extern_res = zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \ |
| 3917 | | zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(extern_arg); \ |
| 3918 | | memcpy(&res, &extern_res, sizeof(res)); \ |
| 3919 | | return extern_res; \ |
| 3920 | | } |
| 3921 | | zig_convert_builtin(zig_compiler_rt_f16, zig_f16, trunc, zig_f32, zig_f32, 2) |
| 3922 | | zig_convert_builtin(zig_compiler_rt_f16, zig_f16, trunc, zig_f64, zig_f64, 2) |
| 3923 | | zig_convert_builtin(zig_f16, zig_f16, trunc, zig_f80, zig_f80, 2) |
| 3924 | | zig_convert_builtin(zig_f16, zig_f16, trunc, zig_f128, zig_f128, 2) |
| 3925 | | zig_convert_builtin(zig_f32, zig_f32, extend, zig_compiler_rt_f16, zig_f16, 2) |
| 3926 | | zig_convert_builtin(zig_f32, zig_f32, trunc, zig_f80, zig_f80, 2) |
| 3927 | | zig_convert_builtin(zig_f32, zig_f32, trunc, zig_f128, zig_f128, 2) |
| 3928 | | zig_convert_builtin(zig_f64, zig_f64, extend, zig_compiler_rt_f16, zig_f16, 2) |
| 3929 | | zig_convert_builtin(zig_f64, zig_f64, trunc, zig_f80, zig_f80, 2) |
| 3930 | | zig_convert_builtin(zig_f64, zig_f64, trunc, zig_f128, zig_f128, 2) |
| 3931 | | zig_convert_builtin(zig_f80, zig_f80, extend, zig_f16, zig_f16, 2) |
| 3932 | | zig_convert_builtin(zig_f80, zig_f80, extend, zig_f32, zig_f32, 2) |
| 3933 | | zig_convert_builtin(zig_f80, zig_f80, extend, zig_f64, zig_f64, 2) |
| 3934 | | zig_convert_builtin(zig_f80, zig_f80, trunc, zig_f128, zig_f128, 2) |
| 3935 | | zig_convert_builtin(zig_f128, zig_f128, extend, zig_f16, zig_f16, 2) |
| 3936 | | zig_convert_builtin(zig_f128, zig_f128, extend, zig_f32, zig_f32, 2) |
| 3937 | | zig_convert_builtin(zig_f128, zig_f128, extend, zig_f64, zig_f64, 2) |
| 3938 | | zig_convert_builtin(zig_f128, zig_f128, extend, zig_f80, zig_f80, 2) |
| 3939 | | |
| 3940 | | #ifdef __ARM_EABI__ |
| 3941 | | |
| 3942 | | zig_extern zig_callconv(pcs("aapcs")) zig_f32 __aeabi_d2f(zig_f64); |
| 3943 | | static inline zig_f32 zig_truncdfsf(zig_f64 arg) { return __aeabi_d2f(arg); } |
| 3944 | | |
| 3945 | | zig_extern zig_callconv(pcs("aapcs")) zig_f64 __aeabi_f2d(zig_f32); |
| 3946 | | static inline zig_f64 zig_extendsfdf(zig_f32 arg) { return __aeabi_f2d(arg); } |
| 3947 | | |
| 3948 | | #else /* __ARM_EABI__ */ |
| 3949 | | |
| 3950 | | zig_convert_builtin(zig_f32, zig_f32, trunc, zig_f64, zig_f64, 2) |
| 3951 | | zig_convert_builtin(zig_f64, zig_f64, extend, zig_f32, zig_f32, 2) |
| 3952 | | |
| 3953 | | #endif /* __ARM_EABI__ */ |
| 3954 | | |
| 3955 | | #define zig_float_negate_builtin_0(w, c, sb) \ |
| 3956 | | zig_expand_concat(zig_xor_, zig_repr_f##w)(arg, zig_make_f##w(-0x0.0p0, c sb)) |
| 3957 | | #define zig_float_negate_builtin_1(w, c, sb) -arg |
| 3958 | | #define zig_float_negate_builtin(w, c, sb) \ |
| 6012 | zig_expand_concat(zig_expand_concat(zig_convert_float_, zig_has_##res_when), \ |
| 6013 | zig_has_##arg_when)(ResType, operation, ArgType, version); \ |
| 6014 | } |
| 6015 | |
| 6016 | #define zig_convert_floats(SmallType, BigType) \ |
| 6017 | zig_convert_float(SmallType, zig_##SmallType, trunc, BigType, zig_##BigType, 2) \ |
| 6018 | zig_convert_float(BigType, zig_##BigType, extend, SmallType, zig_##SmallType, 2) |
| 6019 | zig_convert_floats(f16, f32) |
| 6020 | zig_convert_floats(f16, f64) |
| 6021 | zig_convert_floats(f16, f80) |
| 6022 | zig_convert_floats(f16, f128) |
| 6023 | zig_convert_floats(f32, f64) |
| 6024 | zig_convert_floats(f32, f80) |
| 6025 | zig_convert_floats(f32, f128) |
| 6026 | zig_convert_floats(f64, f80) |
| 6027 | zig_convert_floats(f64, f128) |
| 6028 | zig_convert_floats(f80, f128) |
| 6029 | |
| 6030 | #define zig_float_negate_builtin_0(w, sb) \ |
| 6031 | zig_expand_concat(zig_xor_, zig_repr_f##w)(arg, zig_make_f##w(-0x0.0p0, sb)) |
| 6032 | #define zig_float_negate_builtin_1(w, sb) -arg |
| 6033 | #define zig_float_negate_builtin(w, sb) \ |
| 3959 | 6034 | static inline zig_f##w zig_neg_f##w(zig_f##w arg) { \ |
| 3960 | | return zig_expand_concat(zig_float_negate_builtin_, zig_has_f##w)(w, c, sb); \ |
| 6035 | return zig_expand_concat(zig_float_negate_builtin_, zig_has_f##w)(w, sb); \ |
| 3961 | 6036 | } |
| 3962 | | zig_float_negate_builtin(16, , UINT16_C(1) << 15 ) |
| 3963 | | zig_float_negate_builtin(32, , UINT32_C(1) << 31 ) |
| 3964 | | zig_float_negate_builtin(64, , UINT64_C(1) << 63 ) |
| 3965 | | zig_float_negate_builtin(80, zig_make_u128, (UINT64_C(1) << 15, UINT64_C(0))) |
| 3966 | | zig_float_negate_builtin(128, zig_make_u128, (UINT64_C(1) << 63, UINT64_C(0))) |
| 6037 | zig_float_negate_builtin(16, UINT16_C(1) << 15) |
| 6038 | zig_float_negate_builtin(32, UINT32_C(1) << 31) |
| 6039 | zig_float_negate_builtin(64, UINT64_C(1) << 63) |
| 6040 | |
| 6041 | #undef zig_float_negate_builtin_0 |
| 6042 | #define zig_float_negate_builtin_0(w, sb) \ |
| 6043 | zig_make_repr_f##w(zig_mantissa_repr_f##w(arg), zig_xor_u16(zig_exponent_repr_f##w(arg), sb)) |
| 6044 | zig_float_negate_builtin(80, UINT16_C(1) << 15) |
| 6045 | |
| 6046 | #undef zig_float_negate_builtin_0 |
| 6047 | #define zig_float_negate_builtin_0(w, sb) \ |
| 6048 | zig_make_repr_f##w(zig_xor_u64(zig_hi_repr_f##w(arg), sb), zig_lo_repr_f##w(arg)) |
| 6049 | zig_float_negate_builtin(128, UINT64_C(1) << 63) |
| 3967 | 6050 | |
| 3968 | 6051 | #define zig_float_less_builtin_0(Type, operation) \ |
| 3969 | | zig_extern int32_t zig_expand_concat(zig_expand_concat(__##operation, \ |
| 6052 | zig_extern zig_FloatCompareResult zig_expand_concat(zig_expand_concat(__##operation, \ |
| 3970 | 6053 | zig_compiler_rt_abbrev_zig_##Type), 2)(zig_##Type, zig_##Type); \ |
| 3971 | 6054 | static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 3972 | | return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_zig_##Type), 2)(lhs, rhs); \ |
| 6055 | return (int32_t)zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_zig_##Type), 2)(lhs, rhs); \ |
| 3973 | 6056 | } |
| 3974 | 6057 | #define zig_float_less_builtin_1(Type, operation) \ |
| 3975 | 6058 | static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| ... | ... | @@ -3994,13 +6077,52 @@ zig_float_negate_builtin(128, zig_make_u128, (UINT64_C(1) << 63, UINT64_C(0))) |
| 3994 | 6077 | return lhs operator rhs; \ |
| 3995 | 6078 | } |
| 3996 | 6079 | |
| 6080 | #define zig_float_builtins(w) \ |
| 6081 | zig_common_float_builtins(w) \ |
| 6082 | zig_convert_float(f##w, zig_f##w, float, int128, zig_i128, ) \ |
| 6083 | zig_convert_float(f##w, zig_f##w, floatun, int128, zig_u128, ) |
| 3997 | 6084 | #define zig_common_float_builtins(w) \ |
| 3998 | | zig_convert_builtin( int64_t, int64_t, fix, zig_f##w, zig_f##w, ) \ |
| 3999 | | zig_convert_builtin(zig_i128, zig_i128, fix, zig_f##w, zig_f##w, ) \ |
| 4000 | | zig_convert_builtin(zig_u128, zig_u128, fixuns, zig_f##w, zig_f##w, ) \ |
| 4001 | | zig_convert_builtin(zig_f##w, zig_f##w, float, int64_t, int64_t, ) \ |
| 4002 | | zig_convert_builtin(zig_f##w, zig_f##w, float, zig_i128, zig_i128, ) \ |
| 4003 | | zig_convert_builtin(zig_f##w, zig_f##w, floatun, zig_u128, zig_u128, ) \ |
| 6085 | zig_convert_float(always, int32_t, fix, f##w, zig_f##w, ) \ |
| 6086 | zig_convert_float(always, int64_t, fix, f##w, zig_f##w, ) \ |
| 6087 | zig_convert_float(int128, zig_i128, fix, f##w, zig_f##w, ) \ |
| 6088 | zig_convert_float(always, uint32_t, fixuns, f##w, zig_f##w, ) \ |
| 6089 | zig_convert_float(always, uint64_t, fixuns, f##w, zig_f##w, ) \ |
| 6090 | zig_convert_float(int128, zig_u128, fixuns, f##w, zig_f##w, ) \ |
| 6091 | zig_convert_float(f##w, zig_f##w, float, always, int32_t, ) \ |
| 6092 | zig_convert_float(f##w, zig_f##w, float, always, int64_t, ) \ |
| 6093 | zig_convert_float(f##w, zig_f##w, floatun, always, uint32_t, ) \ |
| 6094 | zig_convert_float(f##w, zig_f##w, floatun, always, uint64_t, ) \ |
| 6095 | \ |
| 6096 | static inline void zig_expand_concat(zig_expand_concat(zig_fix, \ |
| 6097 | zig_compiler_rt_abbrev_zig_f##w), ei)(void *res, zig_f##w arg, uint16_t bits) { \ |
| 6098 | zig_extern void zig_expand_concat(zig_expand_concat(__fix, \ |
| 6099 | zig_compiler_rt_abbrev_zig_f##w), ei)(uint8_t *res, uintptr_t bits, zig_f##w arg); \ |
| 6100 | zig_expand_concat(zig_expand_concat(__fix, \ |
| 6101 | zig_compiler_rt_abbrev_zig_f##w), ei)(res, bits, arg); \ |
| 6102 | } \ |
| 6103 | \ |
| 6104 | static inline void zig_expand_concat(zig_expand_concat(zig_fixuns, \ |
| 6105 | zig_compiler_rt_abbrev_zig_f##w), ei)(void *res, zig_f##w arg, uint16_t bits) { \ |
| 6106 | zig_extern void zig_expand_concat(zig_expand_concat(__fixuns, \ |
| 6107 | zig_compiler_rt_abbrev_zig_f##w), ei)(uint8_t *res, uintptr_t bits, zig_f##w arg); \ |
| 6108 | zig_expand_concat(zig_expand_concat(__fixuns, \ |
| 6109 | zig_compiler_rt_abbrev_zig_f##w), ei)(res, bits, arg); \ |
| 6110 | } \ |
| 6111 | \ |
| 6112 | static inline zig_f##w zig_expand_concat(zig_floatei, \ |
| 6113 | zig_compiler_rt_abbrev_zig_f##w)(void *res, uint16_t bits) { \ |
| 6114 | zig_extern zig_f##w zig_expand_concat(__floatei, \ |
| 6115 | zig_compiler_rt_abbrev_zig_f##w)(const uint8_t *arg, uintptr_t bits); \ |
| 6116 | return zig_expand_concat(__floatei, zig_compiler_rt_abbrev_zig_f##w)(res, bits); \ |
| 6117 | } \ |
| 6118 | \ |
| 6119 | static inline zig_f##w zig_expand_concat(zig_floatunei, \ |
| 6120 | zig_compiler_rt_abbrev_zig_f##w)(void *res, uint16_t bits) { \ |
| 6121 | zig_extern zig_f##w zig_expand_concat(__floatunei, \ |
| 6122 | zig_compiler_rt_abbrev_zig_f##w)(const uint8_t *arg, uintptr_t bits); \ |
| 6123 | return zig_expand_concat(__floatunei, zig_compiler_rt_abbrev_zig_f##w)(res, bits); \ |
| 6124 | } \ |
| 6125 | \ |
| 4004 | 6126 | zig_expand_concat(zig_float_less_builtin_, zig_has_f##w)(f##w, cmp) \ |
| 4005 | 6127 | zig_expand_concat(zig_float_less_builtin_, zig_has_f##w)(f##w, ne) \ |
| 4006 | 6128 | zig_expand_concat(zig_float_less_builtin_, zig_has_f##w)(f##w, eq) \ |
| ... | ... | @@ -4031,82 +6153,48 @@ zig_float_negate_builtin(128, zig_make_u128, (UINT64_C(1) << 63, UINT64_C(0))) |
| 4031 | 6153 | zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(fmax)))(zig_f##w, zig_max_f##w, zig_libc_name_f##w(fmax), (zig_f##w x, zig_f##w y), (x, y)) \ |
| 4032 | 6154 | zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(fma)))(zig_f##w, zig_fma_f##w, zig_libc_name_f##w(fma), (zig_f##w x, zig_f##w y, zig_f##w z), (x, y, z)) \ |
| 4033 | 6155 | \ |
| 4034 | | static inline zig_f##w zig_div_trunc_f##w(zig_f##w lhs, zig_f##w rhs) { \ |
| 6156 | static inline zig_f##w zig_divTrunc_f##w(zig_f##w lhs, zig_f##w rhs) { \ |
| 4035 | 6157 | return zig_trunc_f##w(zig_div_f##w(lhs, rhs)); \ |
| 4036 | 6158 | } \ |
| 4037 | 6159 | \ |
| 4038 | | static inline zig_f##w zig_div_floor_f##w(zig_f##w lhs, zig_f##w rhs) { \ |
| 6160 | static inline zig_f##w zig_divFloor_f##w(zig_f##w lhs, zig_f##w rhs) { \ |
| 4039 | 6161 | return zig_floor_f##w(zig_div_f##w(lhs, rhs)); \ |
| 4040 | 6162 | } \ |
| 4041 | 6163 | \ |
| 4042 | | static inline zig_f##w zig_div_ceil_f##w(zig_f##w lhs, zig_f##w rhs) { \ |
| 6164 | static inline zig_f##w zig_divCeil_f##w(zig_f##w lhs, zig_f##w rhs) { \ |
| 4043 | 6165 | return zig_ceil_f##w(zig_div_f##w(lhs, rhs)); \ |
| 4044 | 6166 | } \ |
| 4045 | 6167 | \ |
| 4046 | 6168 | static inline zig_f##w zig_mod_f##w(zig_f##w lhs, zig_f##w rhs) { \ |
| 4047 | | return zig_sub_f##w(lhs, zig_mul_f##w(zig_div_floor_f##w(lhs, rhs), rhs)); \ |
| 6169 | return zig_sub_f##w(lhs, zig_mul_f##w(zig_divFloor_f##w(lhs, rhs), rhs)); \ |
| 4048 | 6170 | } |
| 4049 | | zig_common_float_builtins(16) |
| 4050 | | zig_common_float_builtins(32) |
| 4051 | | zig_common_float_builtins(64) |
| 4052 | | zig_common_float_builtins(80) |
| 4053 | | zig_common_float_builtins(128) |
| 4054 | | |
| 4055 | | #define zig_float_builtins(w) \ |
| 4056 | | zig_convert_builtin( int32_t, int32_t, fix, zig_f##w, zig_f##w, ) \ |
| 4057 | | zig_convert_builtin(uint32_t, uint32_t, fixuns, zig_f##w, zig_f##w, ) \ |
| 4058 | | zig_convert_builtin(uint64_t, uint64_t, fixuns, zig_f##w, zig_f##w, ) \ |
| 4059 | | zig_convert_builtin(zig_f##w, zig_f##w, float, int32_t, int32_t, ) \ |
| 4060 | | zig_convert_builtin(zig_f##w, zig_f##w, floatun, uint32_t, uint32_t, ) \ |
| 4061 | | zig_convert_builtin(zig_f##w, zig_f##w, floatun, uint64_t, uint64_t, ) |
| 4062 | 6171 | zig_float_builtins(16) |
| 4063 | | zig_float_builtins(80) |
| 4064 | | zig_float_builtins(128) |
| 4065 | | |
| 4066 | | #ifdef __ARM_EABI__ |
| 4067 | | |
| 4068 | | zig_extern zig_callconv(pcs("aapcs")) int32_t __aeabi_f2iz(zig_f32); |
| 4069 | | static inline int32_t zig_fixsfsi(zig_f32 arg) { return __aeabi_f2iz(arg); } |
| 4070 | | |
| 4071 | | zig_extern zig_callconv(pcs("aapcs")) uint32_t __aeabi_f2uiz(zig_f32); |
| 4072 | | static inline uint32_t zig_fixunssfsi(zig_f32 arg) { return __aeabi_f2uiz(arg); } |
| 4073 | | |
| 4074 | | zig_extern zig_callconv(pcs("aapcs")) uint64_t __aeabi_f2ulz(zig_f32); |
| 4075 | | static inline uint64_t zig_fixunssfdi(zig_f32 arg) { return __aeabi_f2ulz(arg); } |
| 4076 | | |
| 4077 | | zig_extern zig_callconv(pcs("aapcs")) zig_f32 __aeabi_i2f(int32_t); |
| 4078 | | static inline zig_f32 zig_floatsisf(int32_t arg) { return __aeabi_i2f(arg); } |
| 4079 | | |
| 4080 | | zig_extern zig_callconv(pcs("aapcs")) zig_f32 __aeabi_ui2f(uint32_t); |
| 4081 | | static inline zig_f32 zig_floatunsisf(uint32_t arg) { return __aeabi_ui2f(arg); } |
| 4082 | | |
| 4083 | | zig_extern zig_callconv(pcs("aapcs")) zig_f32 __aeabi_ul2f(uint64_t); |
| 4084 | | static inline zig_f32 zig_floatundisf(uint64_t arg) { return __aeabi_ul2f(arg); } |
| 4085 | | |
| 4086 | | zig_extern zig_callconv(pcs("aapcs")) int32_t __aeabi_d2iz(zig_f64); |
| 4087 | | static inline int32_t zig_fixdfsi(zig_f64 arg) { return __aeabi_d2iz(arg); } |
| 4088 | | |
| 4089 | | zig_extern zig_callconv(pcs("aapcs")) uint32_t __aeabi_d2uiz(zig_f64); |
| 4090 | | static inline uint32_t zig_fixunsdfsi(zig_f64 arg) { return __aeabi_d2uiz(arg); } |
| 4091 | | |
| 4092 | | zig_extern zig_callconv(pcs("aapcs")) uint64_t __aeabi_d2ulz(zig_f64); |
| 4093 | | static inline uint64_t zig_fixunsdfdi(zig_f64 arg) { return __aeabi_d2ulz(arg); } |
| 4094 | | |
| 4095 | | zig_extern zig_callconv(pcs("aapcs")) zig_f64 __aeabi_i2d(int32_t); |
| 4096 | | static inline zig_f64 zig_floatsidf(int32_t arg) { return __aeabi_i2d(arg); } |
| 4097 | | |
| 4098 | | zig_extern zig_callconv(pcs("aapcs")) zig_f64 __aeabi_ui2d(uint32_t); |
| 4099 | | static inline zig_f64 zig_floatunsidf(uint32_t arg) { return __aeabi_ui2d(arg); } |
| 4100 | | |
| 4101 | | zig_extern zig_callconv(pcs("aapcs")) zig_f64 __aeabi_ul2d(uint64_t); |
| 4102 | | static inline zig_f64 zig_floatundidf(uint64_t arg) { return __aeabi_ul2d(arg); } |
| 4103 | | |
| 4104 | | #else /* __ARM_EABI__ */ |
| 4105 | | |
| 4106 | 6172 | zig_float_builtins(32) |
| 4107 | 6173 | zig_float_builtins(64) |
| 4108 | | |
| 4109 | | #endif /* __ARM_EABI__ */ |
| 6174 | zig_float_builtins(80) |
| 6175 | #if defined(zig_x86_32) |
| 6176 | zig_common_float_builtins(128) |
| 6177 | static inline zig_f128 zig_floattitf(zig_i128 arg) { |
| 6178 | extern zig_f128 __floattitf(zig_f128 arg); |
| 6179 | return __floattitf(zig_f128_bitCast_i128(arg)); |
| 6180 | } |
| 6181 | static inline zig_f128 zig_floatuntitf(zig_u128 arg) { |
| 6182 | extern zig_f128 __floatuntitf(zig_f128 arg); |
| 6183 | return __floatuntitf(zig_f128_bitCast_u128(arg)); |
| 6184 | } |
| 6185 | #elif defined(zig_x86_64) && defined(zig_windows) |
| 6186 | zig_common_float_builtins(128) |
| 6187 | static inline zig_f128 zig_floattitf(zig_i128 arg) { |
| 6188 | extern zig_f128 __floattitf(zig_i128 arg); |
| 6189 | return __floattitf(arg); |
| 6190 | } |
| 6191 | static inline zig_f128 zig_floatuntitf(zig_u128 arg) { |
| 6192 | extern zig_f128 __floatuntitf(uint64_t arg_lo, uint64_t arg_hi); |
| 6193 | return __floatuntitf(zig_lo_u128(arg), zig_hi_u128(arg)); |
| 6194 | } |
| 6195 | #else |
| 6196 | zig_float_builtins(128) |
| 6197 | #endif |
| 4110 | 6198 | |
| 4111 | 6199 | /* ============================ Atomics Support ============================= */ |
| 4112 | 6200 | |
| ... | ... | @@ -4410,19 +6498,19 @@ typedef int zig_memory_order; |
| 4410 | 6498 | } \ |
| 4411 | 6499 | static inline void zig_msvc_atomic_store_##ZigType(Type volatile* obj, Type value) { \ |
| 4412 | 6500 | (void)_InterlockedExchange##suffix((SigType volatile*)obj, (SigType)value); \ |
| 4413 | | } \ |
| 6501 | } \ |
| 4414 | 6502 | static inline Type zig_msvc_atomic_load_zig_memory_order_relaxed_##ZigType(Type volatile* obj) { \ |
| 4415 | 6503 | return __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ |
| 4416 | 6504 | } \ |
| 4417 | 6505 | static inline Type zig_msvc_atomic_load_zig_memory_order_acquire_##ZigType(Type volatile* obj) { \ |
| 4418 | | Type val = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ |
| 6506 | Type value = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ |
| 4419 | 6507 | _ReadWriteBarrier(); \ |
| 4420 | | return val; \ |
| 6508 | return value; \ |
| 4421 | 6509 | } \ |
| 4422 | 6510 | static inline Type zig_msvc_atomic_load_zig_memory_order_seq_cst_##ZigType(Type volatile* obj) { \ |
| 4423 | | Type val = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ |
| 6511 | Type value = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ |
| 4424 | 6512 | _ReadWriteBarrier(); \ |
| 4425 | | return val; \ |
| 6513 | return value; \ |
| 4426 | 6514 | } |
| 4427 | 6515 | |
| 4428 | 6516 | zig_msvc_atomics( u8, uint8_t, char, 8, 8) |
| ... | ... | @@ -4465,14 +6553,14 @@ zig_msvc_atomics(i64, int64_t, __int64, 64, 64) |
| 4465 | 6553 | zig_##Type result; \ |
| 4466 | 6554 | SigType initial = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ |
| 4467 | 6555 | _ReadWriteBarrier(); \ |
| 4468 | | memcpy(&result, &initial, sizeof(result)); \ |
| 6556 | memcpy(&result, &initial, sizeof(result)); \ |
| 4469 | 6557 | return result; \ |
| 4470 | 6558 | } \ |
| 4471 | 6559 | static inline zig_##Type zig_msvc_atomic_load_zig_memory_order_seq_cst_##Type(zig_##Type volatile* obj) { \ |
| 4472 | 6560 | zig_##Type result; \ |
| 4473 | 6561 | SigType initial = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ |
| 4474 | 6562 | _ReadWriteBarrier(); \ |
| 4475 | | memcpy(&result, &initial, sizeof(result)); \ |
| 6563 | memcpy(&result, &initial, sizeof(result)); \ |
| 4476 | 6564 | return result; \ |
| 4477 | 6565 | } |
| 4478 | 6566 | |
| ... | ... | @@ -4502,9 +6590,9 @@ static inline void* zig_msvc_atomic_load_zig_memory_order_relaxed_p32(void volat |
| 4502 | 6590 | } |
| 4503 | 6591 | |
| 4504 | 6592 | static inline void* zig_msvc_atomic_load_zig_memory_order_acquire_p32(void volatile* obj) { |
| 4505 | | void* val = (void*)__iso_volatile_load32(obj); |
| 6593 | void* value = (void*)__iso_volatile_load32(obj); |
| 4506 | 6594 | _ReadWriteBarrier(); |
| 4507 | | return val; |
| 6595 | return value; |
| 4508 | 6596 | } |
| 4509 | 6597 | |
| 4510 | 6598 | static inline void* zig_msvc_atomic_load_zig_memory_order_seq_cst_p32(void volatile* obj) { |
| ... | ... | @@ -4532,9 +6620,9 @@ static inline void* zig_msvc_atomic_load_zig_memory_order_relaxed_p64(void volat |
| 4532 | 6620 | } |
| 4533 | 6621 | |
| 4534 | 6622 | static inline void* zig_msvc_atomic_load_zig_memory_order_acquire_p64(void volatile* obj) { |
| 4535 | | void* val = (void*)__iso_volatile_load64(obj); |
| 6623 | void* value = (void*)__iso_volatile_load64(obj); |
| 4536 | 6624 | _ReadWriteBarrier(); |
| 4537 | | return val; |
| 6625 | return value; |
| 4538 | 6626 | } |
| 4539 | 6627 | |
| 4540 | 6628 | static inline void* zig_msvc_atomic_load_zig_memory_order_seq_cst_p64(void volatile* obj) { |