authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-07-27 14:47:34-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-07-27 14:47:34-04:00
log47396c903cb118df9e5562f383026b0f460d38f6
tree80203b7f809f94c3a1a49f6e9404331d8947f78b
parent3b8913a321dbd0ff7de7ac5ac9d4dd82a4235a2e

stage1: update zig1.wasm


2 files changed, 3078 insertions(+), 990 deletions(-)

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