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