authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-06 01:29:56-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-03-06 01:29:56-05:00
logac1b0e832b4b9d151098050e1d29e28a568e215c
tree755f438b1bb25a03f10469e5b87c30718aa17eed
parent2641feb9b98e794608917231c2fa775ea4daea06
parent8558983c865b6d7ec8a692bd1027443109058cf6
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14799 from ziglang/update-zig1

`@trap` fixups

7 files changed, 1868 insertions(+), 919 deletions(-)

doc/langref.html.in+1-1
......@@ -9403,7 +9403,7 @@ fn List(comptime T: type) type {
94039403 Unlike for {#syntax#}@breakpoint(){#endsyntax#}, execution does not continue after this point.
94049404 </p>
94059405 <p>
9406 This function is only valid within function scope.
9406 Outside function scope, this builtin causes a compile error.
94079407 </p>
94089408 {#see_also|@breakpoint#}
94099409 {#header_close#}
lib/std/os.zig+3-13
......@@ -575,22 +575,12 @@ pub fn abort() noreturn {
575575 raise(SIG.KILL) catch {};
576576 exit(127); // Pid 1 might not be signalled in some containers.
577577 }
578 if (builtin.os.tag == .uefi) {
579 exit(0); // TODO choose appropriate exit code
580 }
581 if (builtin.os.tag == .wasi) {
582 exit(1);
583 }
584 if (builtin.os.tag == .cuda) {
585 // TODO: introduce `@trap` instead of abusing https://github.com/ziglang/zig/issues/2291
586 @"llvm.trap"();
578 switch (builtin.os.tag) {
579 .uefi, .wasi, .cuda => @trap(),
580 else => system.abort(),
587581 }
588
589 system.abort();
590582}
591583
592extern fn @"llvm.trap"() noreturn;
593
594584pub const RaiseError = UnexpectedError;
595585
596586pub fn raise(sig: u8) RaiseError!void {
lib/zig.h+1-1
......@@ -193,7 +193,7 @@ typedef char bool;
193193#elif defined(__i386__) || defined(__x86_64__)
194194#define zig_trap() __asm__ volatile("ud2");
195195#else
196#define zig_trap() raise(SIGILL)
196#define zig_trap() raise(SIGTRAP)
197197#endif
198198
199199#if zig_has_builtin(debugtrap)
src/Module.zig+1
......@@ -3528,6 +3528,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
35283528 const digest = hash: {
35293529 var path_hash: Cache.HashHelper = .{};
35303530 path_hash.addBytes(build_options.version);
3531 path_hash.add(builtin.zig_backend);
35313532 if (!want_local_cache) {
35323533 path_hash.addOptionalBytes(file.pkg.root_src_directory.path);
35333534 }
src/codegen/llvm.zig+1
......@@ -8261,6 +8261,7 @@ pub const FuncGen = struct {
82618261 _ = inst;
82628262 const llvm_fn = self.getIntrinsic("llvm.trap", &.{});
82638263 _ = self.builder.buildCall(llvm_fn.globalGetValueType(), llvm_fn, undefined, 0, .Cold, .Auto, "");
8264 _ = self.builder.buildUnreachable();
82648265 return null;
82658266 }
82668267
stage1/zig.h+1861-904
......@@ -1,8 +1,11 @@
11#undef linux
22
3#ifndef __STDC_WANT_IEC_60559_TYPES_EXT__
34#define __STDC_WANT_IEC_60559_TYPES_EXT__
5#endif
46#include <float.h>
57#include <limits.h>
8#include <stdarg.h>
69#include <stddef.h>
710#include <stdint.h>
811
......@@ -34,6 +37,14 @@ typedef char bool;
3437#define zig_has_attribute(attribute) 0
3538#endif
3639
40#if __LITTLE_ENDIAN__ || _MSC_VER
41#define zig_little_endian 1
42#define zig_big_endian 0
43#else
44#define zig_little_endian 0
45#define zig_big_endian 1
46#endif
47
3748#if __STDC_VERSION__ >= 201112L
3849#define zig_threadlocal _Thread_local
3950#elif defined(__GNUC__)
......@@ -75,6 +86,32 @@ typedef char bool;
7586#define zig_cold
7687#endif
7788
89#if zig_has_attribute(flatten)
90#define zig_maybe_flatten __attribute__((flatten))
91#else
92#define zig_maybe_flatten
93#endif
94
95#if zig_has_attribute(noinline)
96#define zig_never_inline __attribute__((noinline)) zig_maybe_flatten
97#elif defined(_MSC_VER)
98#define zig_never_inline __declspec(noinline) zig_maybe_flatten
99#else
100#define zig_never_inline zig_never_inline_unavailable
101#endif
102
103#if zig_has_attribute(not_tail_called)
104#define zig_never_tail __attribute__((not_tail_called)) zig_never_inline
105#else
106#define zig_never_tail zig_never_tail_unavailable
107#endif
108
109#if zig_has_attribute(always_inline)
110#define zig_always_tail __attribute__((musttail))
111#else
112#define zig_always_tail zig_always_tail_unavailable
113#endif
114
78115#if __STDC_VERSION__ >= 199901L
79116#define zig_restrict restrict
80117#elif defined(__GNUC__)
......@@ -151,10 +188,16 @@ typedef char bool;
151188#define zig_export(sig, symbol, name) __asm(name " = " symbol)
152189#endif
153190
191#if zig_has_builtin(trap)
192#define zig_trap() __builtin_trap()
193#elif defined(__i386__) || defined(__x86_64__)
194#define zig_trap() __asm__ volatile("ud2");
195#else
196#define zig_trap() raise(SIGTRAP)
197#endif
198
154199#if zig_has_builtin(debugtrap)
155200#define zig_breakpoint() __builtin_debugtrap()
156#elif zig_has_builtin(trap) || defined(zig_gnuc)
157#define zig_breakpoint() __builtin_trap()
158201#elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
159202#define zig_breakpoint() __debugbreak()
160203#elif defined(__i386__) || defined(__x86_64__)
......@@ -286,701 +329,656 @@ typedef char bool;
286329#endif
287330
288331#if __STDC_VERSION__ >= 201112L
289#define zig_noreturn _Noreturn void
332#define zig_noreturn _Noreturn
290333#elif zig_has_attribute(noreturn) || defined(zig_gnuc)
291#define zig_noreturn __attribute__((noreturn)) void
334#define zig_noreturn __attribute__((noreturn))
292335#elif _MSC_VER
293#define zig_noreturn __declspec(noreturn) void
336#define zig_noreturn __declspec(noreturn)
294337#else
295#define zig_noreturn void
338#define zig_noreturn
296339#endif
297340
298341#define zig_bitSizeOf(T) (CHAR_BIT * sizeof(T))
299342
300typedef uintptr_t zig_usize;
301typedef intptr_t zig_isize;
302typedef signed short int zig_c_short;
303typedef unsigned short int zig_c_ushort;
304typedef signed int zig_c_int;
305typedef unsigned int zig_c_uint;
306typedef signed long int zig_c_long;
307typedef unsigned long int zig_c_ulong;
308typedef signed long long int zig_c_longlong;
309typedef unsigned long long int zig_c_ulonglong;
310
311typedef uint8_t zig_u8;
312typedef int8_t zig_i8;
313typedef uint16_t zig_u16;
314typedef int16_t zig_i16;
315typedef uint32_t zig_u32;
316typedef int32_t zig_i32;
317typedef uint64_t zig_u64;
318typedef int64_t zig_i64;
319
320#define zig_as_u8(val) UINT8_C(val)
321#define zig_as_i8(val) INT8_C(val)
322#define zig_as_u16(val) UINT16_C(val)
323#define zig_as_i16(val) INT16_C(val)
324#define zig_as_u32(val) UINT32_C(val)
325#define zig_as_i32(val) INT32_C(val)
326#define zig_as_u64(val) UINT64_C(val)
327#define zig_as_i64(val) INT64_C(val)
328
329#define zig_minInt_u8 zig_as_u8(0)
330#define zig_maxInt_u8 UINT8_MAX
343#define zig_compiler_rt_abbrev_uint32_t si
344#define zig_compiler_rt_abbrev_int32_t si
345#define zig_compiler_rt_abbrev_uint64_t di
346#define zig_compiler_rt_abbrev_int64_t di
347#define zig_compiler_rt_abbrev_zig_u128 ti
348#define zig_compiler_rt_abbrev_zig_i128 ti
349#define zig_compiler_rt_abbrev_zig_f16 hf
350#define zig_compiler_rt_abbrev_zig_f32 sf
351#define zig_compiler_rt_abbrev_zig_f64 df
352#define zig_compiler_rt_abbrev_zig_f80 xf
353#define zig_compiler_rt_abbrev_zig_f128 tf
354
355zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, size_t);
356zig_extern void *memset (void *, int, size_t);
357
358/* ===================== 8/16/32/64-bit Integer Support ===================== */
359
360#if __STDC_VERSION__ >= 199901L || _MSC_VER
361#include <stdint.h>
362#else
363
364#if SCHAR_MIN == ~0x7F && SCHAR_MAX == 0x7F && UCHAR_MAX == 0xFF
365typedef unsigned char uint8_t;
366typedef signed char int8_t;
367#define INT8_C(c) c
368#define UINT8_C(c) c##U
369#elif SHRT_MIN == ~0x7F && SHRT_MAX == 0x7F && USHRT_MAX == 0xFF
370typedef unsigned short uint8_t;
371typedef signed short int8_t;
372#define INT8_C(c) c
373#define UINT8_C(c) c##U
374#elif INT_MIN == ~0x7F && INT_MAX == 0x7F && UINT_MAX == 0xFF
375typedef unsigned int uint8_t;
376typedef signed int int8_t;
377#define INT8_C(c) c
378#define UINT8_C(c) c##U
379#elif LONG_MIN == ~0x7F && LONG_MAX == 0x7F && ULONG_MAX == 0xFF
380typedef unsigned long uint8_t;
381typedef signed long int8_t;
382#define INT8_C(c) c##L
383#define UINT8_C(c) c##LU
384#elif LLONG_MIN == ~0x7F && LLONG_MAX == 0x7F && ULLONG_MAX == 0xFF
385typedef unsigned long long uint8_t;
386typedef signed long long int8_t;
387#define INT8_C(c) c##LL
388#define UINT8_C(c) c##LLU
389#endif
390#define INT8_MIN (~INT8_C(0x7F))
391#define INT8_MAX ( INT8_C(0x7F))
392#define UINT8_MAX ( INT8_C(0xFF))
393
394#if SCHAR_MIN == ~0x7FFF && SCHAR_MAX == 0x7FFF && UCHAR_MAX == 0xFFFF
395typedef unsigned char uint16_t;
396typedef signed char int16_t;
397#define INT16_C(c) c
398#define UINT16_C(c) c##U
399#elif SHRT_MIN == ~0x7FFF && SHRT_MAX == 0x7FFF && USHRT_MAX == 0xFFFF
400typedef unsigned short uint16_t;
401typedef signed short int16_t;
402#define INT16_C(c) c
403#define UINT16_C(c) c##U
404#elif INT_MIN == ~0x7FFF && INT_MAX == 0x7FFF && UINT_MAX == 0xFFFF
405typedef unsigned int uint16_t;
406typedef signed int int16_t;
407#define INT16_C(c) c
408#define UINT16_C(c) c##U
409#elif LONG_MIN == ~0x7FFF && LONG_MAX == 0x7FFF && ULONG_MAX == 0xFFFF
410typedef unsigned long uint16_t;
411typedef signed long int16_t;
412#define INT16_C(c) c##L
413#define UINT16_C(c) c##LU
414#elif LLONG_MIN == ~0x7FFF && LLONG_MAX == 0x7FFF && ULLONG_MAX == 0xFFFF
415typedef unsigned long long uint16_t;
416typedef signed long long int16_t;
417#define INT16_C(c) c##LL
418#define UINT16_C(c) c##LLU
419#endif
420#define INT16_MIN (~INT16_C(0x7FFF))
421#define INT16_MAX ( INT16_C(0x7FFF))
422#define UINT16_MAX ( INT16_C(0xFFFF))
423
424#if SCHAR_MIN == ~0x7FFFFFFF && SCHAR_MAX == 0x7FFFFFFF && UCHAR_MAX == 0xFFFFFFFF
425typedef unsigned char uint32_t;
426typedef signed char int32_t;
427#define INT32_C(c) c
428#define UINT32_C(c) c##U
429#elif SHRT_MIN == ~0x7FFFFFFF && SHRT_MAX == 0x7FFFFFFF && USHRT_MAX == 0xFFFFFFFF
430typedef unsigned short uint32_t;
431typedef signed short int32_t;
432#define INT32_C(c) c
433#define UINT32_C(c) c##U
434#elif INT_MIN == ~0x7FFFFFFF && INT_MAX == 0x7FFFFFFF && UINT_MAX == 0xFFFFFFFF
435typedef unsigned int uint32_t;
436typedef signed int int32_t;
437#define INT32_C(c) c
438#define UINT32_C(c) c##U
439#elif LONG_MIN == ~0x7FFFFFFF && LONG_MAX == 0x7FFFFFFF && ULONG_MAX == 0xFFFFFFFF
440typedef unsigned long uint32_t;
441typedef signed long int32_t;
442#define INT32_C(c) c##L
443#define UINT32_C(c) c##LU
444#elif LLONG_MIN == ~0x7FFFFFFF && LLONG_MAX == 0x7FFFFFFF && ULLONG_MAX == 0xFFFFFFFF
445typedef unsigned long long uint32_t;
446typedef signed long long int32_t;
447#define INT32_C(c) c##LL
448#define UINT32_C(c) c##LLU
449#endif
450#define INT32_MIN (~INT32_C(0x7FFFFFFF))
451#define INT32_MAX ( INT32_C(0x7FFFFFFF))
452#define UINT32_MAX ( INT32_C(0xFFFFFFFF))
453
454#if SCHAR_MIN == ~0x7FFFFFFFFFFFFFFF && SCHAR_MAX == 0x7FFFFFFFFFFFFFFF && UCHAR_MAX == 0xFFFFFFFFFFFFFFFF
455typedef unsigned char uint64_t;
456typedef signed char int64_t;
457#define INT64_C(c) c
458#define UINT64_C(c) c##U
459#elif SHRT_MIN == ~0x7FFFFFFFFFFFFFFF && SHRT_MAX == 0x7FFFFFFFFFFFFFFF && USHRT_MAX == 0xFFFFFFFFFFFFFFFF
460typedef unsigned short uint64_t;
461typedef signed short int64_t;
462#define INT64_C(c) c
463#define UINT64_C(c) c##U
464#elif INT_MIN == ~0x7FFFFFFFFFFFFFFF && INT_MAX == 0x7FFFFFFFFFFFFFFF && UINT_MAX == 0xFFFFFFFFFFFFFFFF
465typedef unsigned int uint64_t;
466typedef signed int int64_t;
467#define INT64_C(c) c
468#define UINT64_C(c) c##U
469#elif LONG_MIN == ~0x7FFFFFFFFFFFFFFF && LONG_MAX == 0x7FFFFFFFFFFFFFFF && ULONG_MAX == 0xFFFFFFFFFFFFFFFF
470typedef unsigned long uint64_t;
471typedef signed long int64_t;
472#define INT64_C(c) c##L
473#define UINT64_C(c) c##LU
474#elif LLONG_MIN == ~0x7FFFFFFFFFFFFFFF && LLONG_MAX == 0x7FFFFFFFFFFFFFFF && ULLONG_MAX == 0xFFFFFFFFFFFFFFFF
475typedef unsigned long long uint64_t;
476typedef signed long long int64_t;
477#define INT64_C(c) c##LL
478#define UINT64_C(c) c##LLU
479#endif
480#define INT64_MIN (~INT64_C(0x7FFFFFFFFFFFFFFF))
481#define INT64_MAX ( INT64_C(0x7FFFFFFFFFFFFFFF))
482#define UINT64_MAX ( INT64_C(0xFFFFFFFFFFFFFFFF))
483
484typedef size_t uintptr_t;
485typedef ptrdiff_t intptr_t;
486
487#endif
488
331489#define zig_minInt_i8 INT8_MIN
332490#define zig_maxInt_i8 INT8_MAX
333#define zig_minInt_u16 zig_as_u16(0)
334#define zig_maxInt_u16 UINT16_MAX
491#define zig_minInt_u8 UINT8_C(0)
492#define zig_maxInt_u8 UINT8_MAX
335493#define zig_minInt_i16 INT16_MIN
336494#define zig_maxInt_i16 INT16_MAX
337#define zig_minInt_u32 zig_as_u32(0)
338#define zig_maxInt_u32 UINT32_MAX
495#define zig_minInt_u16 UINT16_C(0)
496#define zig_maxInt_u16 UINT16_MAX
339497#define zig_minInt_i32 INT32_MIN
340498#define zig_maxInt_i32 INT32_MAX
341#define zig_minInt_u64 zig_as_u64(0)
342#define zig_maxInt_u64 UINT64_MAX
499#define zig_minInt_u32 UINT32_C(0)
500#define zig_maxInt_u32 UINT32_MAX
343501#define zig_minInt_i64 INT64_MIN
344502#define zig_maxInt_i64 INT64_MAX
503#define zig_minInt_u64 UINT64_C(0)
504#define zig_maxInt_u64 UINT64_MAX
345505
346#define zig_compiler_rt_abbrev_u32 si
347#define zig_compiler_rt_abbrev_i32 si
348#define zig_compiler_rt_abbrev_u64 di
349#define zig_compiler_rt_abbrev_i64 di
350#define zig_compiler_rt_abbrev_u128 ti
351#define zig_compiler_rt_abbrev_i128 ti
352#define zig_compiler_rt_abbrev_f16 hf
353#define zig_compiler_rt_abbrev_f32 sf
354#define zig_compiler_rt_abbrev_f64 df
355#define zig_compiler_rt_abbrev_f80 xf
356#define zig_compiler_rt_abbrev_f128 tf
357
358zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, zig_usize);
359zig_extern void *memset (void *, int, zig_usize);
360
361/* ==================== 8/16/32/64-bit Integer Routines ===================== */
362
363#define zig_maxInt(Type, bits) zig_shr_##Type(zig_maxInt_##Type, (zig_bitSizeOf(zig_##Type) - bits))
364#define zig_expand_maxInt(Type, bits) zig_maxInt(Type, bits)
365#define zig_minInt(Type, bits) zig_not_##Type(zig_maxInt(Type, bits), bits)
366#define zig_expand_minInt(Type, bits) zig_minInt(Type, bits)
506#define zig_intLimit(s, w, limit, bits) zig_shr_##s##w(zig_##limit##Int_##s##w, w - (bits))
507#define zig_minInt_i(w, bits) zig_intLimit(i, w, min, bits)
508#define zig_maxInt_i(w, bits) zig_intLimit(i, w, max, bits)
509#define zig_minInt_u(w, bits) zig_intLimit(u, w, min, bits)
510#define zig_maxInt_u(w, bits) zig_intLimit(u, w, max, bits)
367511
368512#define zig_int_operator(Type, RhsType, operation, operator) \
369 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##RhsType rhs) { \
513 static inline Type zig_##operation(Type lhs, RhsType rhs) { \
370514 return lhs operator rhs; \
371515 }
372516#define zig_int_basic_operator(Type, operation, operator) \
373 zig_int_operator(Type, Type, operation, operator)
517 zig_int_operator(Type, Type, operation, operator)
374518#define zig_int_shift_operator(Type, operation, operator) \
375 zig_int_operator(Type, u8, operation, operator)
519 zig_int_operator(Type, uint8_t, operation, operator)
376520#define zig_int_helpers(w) \
377 zig_int_basic_operator(u##w, and, &) \
378 zig_int_basic_operator(i##w, and, &) \
379 zig_int_basic_operator(u##w, or, |) \
380 zig_int_basic_operator(i##w, or, |) \
381 zig_int_basic_operator(u##w, xor, ^) \
382 zig_int_basic_operator(i##w, xor, ^) \
383 zig_int_shift_operator(u##w, shl, <<) \
384 zig_int_shift_operator(i##w, shl, <<) \
385 zig_int_shift_operator(u##w, shr, >>) \
521 zig_int_basic_operator(uint##w##_t, and_u##w, &) \
522 zig_int_basic_operator( int##w##_t, and_i##w, &) \
523 zig_int_basic_operator(uint##w##_t, or_u##w, |) \
524 zig_int_basic_operator( int##w##_t, or_i##w, |) \
525 zig_int_basic_operator(uint##w##_t, xor_u##w, ^) \
526 zig_int_basic_operator( int##w##_t, xor_i##w, ^) \
527 zig_int_shift_operator(uint##w##_t, shl_u##w, <<) \
528 zig_int_shift_operator( int##w##_t, shl_i##w, <<) \
529 zig_int_shift_operator(uint##w##_t, shr_u##w, >>) \
386530\
387 static inline zig_i##w zig_shr_i##w(zig_i##w lhs, zig_u8 rhs) { \
388 zig_i##w sign_mask = lhs < zig_as_i##w(0) ? -zig_as_i##w(1) : zig_as_i##w(0); \
531 static inline int##w##_t zig_shr_i##w(int##w##_t lhs, uint8_t rhs) { \
532 int##w##_t sign_mask = lhs < INT##w##_C(0) ? -INT##w##_C(1) : INT##w##_C(0); \
389533 return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \
390534 } \
391535\
392 static inline zig_u##w zig_not_u##w(zig_u##w val, zig_u8 bits) { \
393 return val ^ zig_maxInt(u##w, bits); \
536 static inline uint##w##_t zig_not_u##w(uint##w##_t val, uint8_t bits) { \
537 return val ^ zig_maxInt_u(w, bits); \
394538 } \
395539\
396 static inline zig_i##w zig_not_i##w(zig_i##w val, zig_u8 bits) { \
540 static inline int##w##_t zig_not_i##w(int##w##_t val, uint8_t bits) { \
397541 (void)bits; \
398542 return ~val; \
399543 } \
400544\
401 static inline zig_u##w zig_wrap_u##w(zig_u##w val, zig_u8 bits) { \
402 return val & zig_maxInt(u##w, bits); \
545 static inline uint##w##_t zig_wrap_u##w(uint##w##_t val, uint8_t bits) { \
546 return val & zig_maxInt_u(w, bits); \
403547 } \
404548\
405 static inline zig_i##w zig_wrap_i##w(zig_i##w val, zig_u8 bits) { \
406 return (val & zig_as_u##w(1) << (bits - zig_as_u8(1))) != 0 \
407 ? val | zig_minInt(i##w, bits) : val & zig_maxInt(i##w, bits); \
549 static inline int##w##_t zig_wrap_i##w(int##w##_t val, uint8_t bits) { \
550 return (val & UINT##w##_C(1) << (bits - UINT8_C(1))) != 0 \
551 ? val | zig_minInt_i(w, bits) : val & zig_maxInt_i(w, bits); \
408552 } \
409553\
410 zig_int_basic_operator(u##w, div_floor, /) \
554 zig_int_basic_operator(uint##w##_t, div_floor_u##w, /) \
411555\
412 static inline zig_i##w zig_div_floor_i##w(zig_i##w lhs, zig_i##w rhs) { \
413 return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < zig_as_i##w(0)); \
556 static inline int##w##_t zig_div_floor_i##w(int##w##_t lhs, int##w##_t rhs) { \
557 return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < INT##w##_C(0)); \
414558 } \
415559\
416 zig_int_basic_operator(u##w, mod, %) \
560 zig_int_basic_operator(uint##w##_t, mod_u##w, %) \
417561\
418 static inline zig_i##w zig_mod_i##w(zig_i##w lhs, zig_i##w rhs) { \
419 zig_i##w rem = lhs % rhs; \
420 return rem + (((lhs ^ rhs) & rem) < zig_as_i##w(0) ? rhs : zig_as_i##w(0)); \
562 static inline int##w##_t zig_mod_i##w(int##w##_t lhs, int##w##_t rhs) { \
563 int##w##_t rem = lhs % rhs; \
564 return rem + (((lhs ^ rhs) & rem) < INT##w##_C(0) ? rhs : INT##w##_C(0)); \
421565 } \
422566\
423 static inline zig_u##w zig_shlw_u##w(zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \
567 static inline uint##w##_t zig_shlw_u##w(uint##w##_t lhs, uint8_t rhs, uint8_t bits) { \
424568 return zig_wrap_u##w(zig_shl_u##w(lhs, rhs), bits); \
425569 } \
426570\
427 static inline zig_i##w zig_shlw_i##w(zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \
428 return zig_wrap_i##w((zig_i##w)zig_shl_u##w((zig_u##w)lhs, (zig_u##w)rhs), bits); \
571 static inline int##w##_t zig_shlw_i##w(int##w##_t lhs, uint8_t rhs, uint8_t bits) { \
572 return zig_wrap_i##w((int##w##_t)zig_shl_u##w((uint##w##_t)lhs, (uint##w##_t)rhs), bits); \
429573 } \
430574\
431 static inline zig_u##w zig_addw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
575 static inline uint##w##_t zig_addw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
432576 return zig_wrap_u##w(lhs + rhs, bits); \
433577 } \
434578\
435 static inline zig_i##w zig_addw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
436 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs + (zig_u##w)rhs), bits); \
579 static inline int##w##_t zig_addw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
580 return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs + (uint##w##_t)rhs), bits); \
437581 } \
438582\
439 static inline zig_u##w zig_subw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
583 static inline uint##w##_t zig_subw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
440584 return zig_wrap_u##w(lhs - rhs, bits); \
441585 } \
442586\
443 static inline zig_i##w zig_subw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
444 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs - (zig_u##w)rhs), bits); \
587 static inline int##w##_t zig_subw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
588 return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs - (uint##w##_t)rhs), bits); \
445589 } \
446590\
447 static inline zig_u##w zig_mulw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
591 static inline uint##w##_t zig_mulw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
448592 return zig_wrap_u##w(lhs * rhs, bits); \
449593 } \
450594\
451 static inline zig_i##w zig_mulw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
452 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs * (zig_u##w)rhs), bits); \
595 static inline int##w##_t zig_mulw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
596 return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs * (uint##w##_t)rhs), bits); \
453597 }
454598zig_int_helpers(8)
455599zig_int_helpers(16)
456600zig_int_helpers(32)
457601zig_int_helpers(64)
458602
459static inline bool zig_addo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {
603static inline bool zig_addo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) {
460604#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
461 zig_u32 full_res;
605 uint32_t full_res;
462606 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
463607 *res = zig_wrap_u32(full_res, bits);
464 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
608 return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits);
465609#else
466610 *res = zig_addw_u32(lhs, rhs, bits);
467611 return *res < lhs;
468612#endif
469613}
470614
471static inline void zig_vaddo_u32(zig_u8 *ov, zig_u32 *res, int n,
472 const zig_u32 *lhs, const zig_u32 *rhs, zig_u8 bits)
473{
474 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u32(&res[i], lhs[i], rhs[i], bits);
475}
476
477zig_extern zig_i32 __addosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
478static inline bool zig_addo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
615zig_extern int32_t __addosi4(int32_t lhs, int32_t rhs, int *overflow);
616static inline bool zig_addo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
479617#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
480 zig_i32 full_res;
618 int32_t full_res;
481619 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
482620#else
483 zig_c_int overflow_int;
484 zig_i32 full_res = __addosi4(lhs, rhs, &overflow_int);
621 int overflow_int;
622 int32_t full_res = __addosi4(lhs, rhs, &overflow_int);
485623 bool overflow = overflow_int != 0;
486624#endif
487625 *res = zig_wrap_i32(full_res, bits);
488 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);
489}
490
491static inline void zig_vaddo_i32(zig_u8 *ov, zig_i32 *res, int n,
492 const zig_i32 *lhs, const zig_i32 *rhs, zig_u8 bits)
493{
494 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i32(&res[i], lhs[i], rhs[i], bits);
626 return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits);
495627}
496628
497static inline bool zig_addo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
629static inline bool zig_addo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) {
498630#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
499 zig_u64 full_res;
631 uint64_t full_res;
500632 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
501633 *res = zig_wrap_u64(full_res, bits);
502 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
634 return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits);
503635#else
504636 *res = zig_addw_u64(lhs, rhs, bits);
505637 return *res < lhs;
506638#endif
507639}
508640
509static inline void zig_vaddo_u64(zig_u8 *ov, zig_u64 *res, int n,
510 const zig_u64 *lhs, const zig_u64 *rhs, zig_u8 bits)
511{
512 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u64(&res[i], lhs[i], rhs[i], bits);
513}
514
515zig_extern zig_i64 __addodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
516static inline bool zig_addo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
641zig_extern int64_t __addodi4(int64_t lhs, int64_t rhs, int *overflow);
642static inline bool zig_addo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
517643#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
518 zig_i64 full_res;
644 int64_t full_res;
519645 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
520646#else
521 zig_c_int overflow_int;
522 zig_i64 full_res = __addodi4(lhs, rhs, &overflow_int);
647 int overflow_int;
648 int64_t full_res = __addodi4(lhs, rhs, &overflow_int);
523649 bool overflow = overflow_int != 0;
524650#endif
525651 *res = zig_wrap_i64(full_res, bits);
526 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
527}
528
529static inline void zig_vaddo_i64(zig_u8 *ov, zig_i64 *res, int n,
530 const zig_i64 *lhs, const zig_i64 *rhs, zig_u8 bits)
531{
532 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i64(&res[i], lhs[i], rhs[i], bits);
652 return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits);
533653}
534654
535static inline bool zig_addo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {
655static inline bool zig_addo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) {
536656#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
537 zig_u8 full_res;
657 uint8_t full_res;
538658 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
539659 *res = zig_wrap_u8(full_res, bits);
540 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
660 return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits);
541661#else
542 zig_u32 full_res;
662 uint32_t full_res;
543663 bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits);
544 *res = (zig_u8)full_res;
664 *res = (uint8_t)full_res;
545665 return overflow;
546666#endif
547667}
548668
549static inline void zig_vaddo_u8(zig_u8 *ov, zig_u8 *res, int n,
550 const zig_u8 *lhs, const zig_u8 *rhs, zig_u8 bits)
551{
552 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u8(&res[i], lhs[i], rhs[i], bits);
553}
554
555static inline bool zig_addo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {
669static inline bool zig_addo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits) {
556670#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
557 zig_i8 full_res;
671 int8_t full_res;
558672 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
559673 *res = zig_wrap_i8(full_res, bits);
560 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
674 return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits);
561675#else
562 zig_i32 full_res;
676 int32_t full_res;
563677 bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits);
564 *res = (zig_i8)full_res;
678 *res = (int8_t)full_res;
565679 return overflow;
566680#endif
567681}
568682
569static inline void zig_vaddo_i8(zig_u8 *ov, zig_i8 *res, int n,
570 const zig_i8 *lhs, const zig_i8 *rhs, zig_u8 bits)
571{
572 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i8(&res[i], lhs[i], rhs[i], bits);
573}
574
575static inline bool zig_addo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {
683static inline bool zig_addo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8_t bits) {
576684#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
577 zig_u16 full_res;
685 uint16_t full_res;
578686 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
579687 *res = zig_wrap_u16(full_res, bits);
580 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
688 return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits);
581689#else
582 zig_u32 full_res;
690 uint32_t full_res;
583691 bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits);
584 *res = (zig_u16)full_res;
692 *res = (uint16_t)full_res;
585693 return overflow;
586694#endif
587695}
588696
589static inline void zig_vaddo_u16(zig_u8 *ov, zig_u16 *res, int n,
590 const zig_u16 *lhs, const zig_u16 *rhs, zig_u8 bits)
591{
592 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u16(&res[i], lhs[i], rhs[i], bits);
593}
594
595static inline bool zig_addo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {
697static inline bool zig_addo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t bits) {
596698#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
597 zig_i16 full_res;
699 int16_t full_res;
598700 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
599701 *res = zig_wrap_i16(full_res, bits);
600 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
702 return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits);
601703#else
602 zig_i32 full_res;
704 int32_t full_res;
603705 bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits);
604 *res = (zig_i16)full_res;
706 *res = (int16_t)full_res;
605707 return overflow;
606708#endif
607709}
608710
609static inline void zig_vaddo_i16(zig_u8 *ov, zig_i16 *res, int n,
610 const zig_i16 *lhs, const zig_i16 *rhs, zig_u8 bits)
611{
612 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i16(&res[i], lhs[i], rhs[i], bits);
613}
614
615static inline bool zig_subo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {
711static inline bool zig_subo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) {
616712#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
617 zig_u32 full_res;
713 uint32_t full_res;
618714 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
619715 *res = zig_wrap_u32(full_res, bits);
620 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
716 return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits);
621717#else
622718 *res = zig_subw_u32(lhs, rhs, bits);
623719 return *res > lhs;
624720#endif
625721}
626722
627static inline void zig_vsubo_u32(zig_u8 *ov, zig_u32 *res, int n,
628 const zig_u32 *lhs, const zig_u32 *rhs, zig_u8 bits)
629{
630 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u32(&res[i], lhs[i], rhs[i], bits);
631}
632
633zig_extern zig_i32 __subosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
634static inline bool zig_subo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
723zig_extern int32_t __subosi4(int32_t lhs, int32_t rhs, int *overflow);
724static inline bool zig_subo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
635725#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
636 zig_i32 full_res;
726 int32_t full_res;
637727 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
638728#else
639 zig_c_int overflow_int;
640 zig_i32 full_res = __subosi4(lhs, rhs, &overflow_int);
729 int overflow_int;
730 int32_t full_res = __subosi4(lhs, rhs, &overflow_int);
641731 bool overflow = overflow_int != 0;
642732#endif
643733 *res = zig_wrap_i32(full_res, bits);
644 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);
734 return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits);
645735}
646736
647static inline void zig_vsubo_i32(zig_u8 *ov, zig_i32 *res, int n,
648 const zig_i32 *lhs, const zig_i32 *rhs, zig_u8 bits)
649{
650 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i32(&res[i], lhs[i], rhs[i], bits);
651}
652
653static inline bool zig_subo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
737static inline bool zig_subo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) {
654738#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
655 zig_u64 full_res;
739 uint64_t full_res;
656740 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
657741 *res = zig_wrap_u64(full_res, bits);
658 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
742 return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits);
659743#else
660744 *res = zig_subw_u64(lhs, rhs, bits);
661745 return *res > lhs;
662746#endif
663747}
664748
665static inline void zig_vsubo_u64(zig_u8 *ov, zig_u64 *res, int n,
666 const zig_u64 *lhs, const zig_u64 *rhs, zig_u8 bits)
667{
668 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u64(&res[i], lhs[i], rhs[i], bits);
669}
670
671zig_extern zig_i64 __subodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
672static inline bool zig_subo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
749zig_extern int64_t __subodi4(int64_t lhs, int64_t rhs, int *overflow);
750static inline bool zig_subo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
673751#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
674 zig_i64 full_res;
752 int64_t full_res;
675753 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
676754#else
677 zig_c_int overflow_int;
678 zig_i64 full_res = __subodi4(lhs, rhs, &overflow_int);
755 int overflow_int;
756 int64_t full_res = __subodi4(lhs, rhs, &overflow_int);
679757 bool overflow = overflow_int != 0;
680758#endif
681759 *res = zig_wrap_i64(full_res, bits);
682 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
760 return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits);
683761}
684762
685static inline void zig_vsubo_i64(zig_u8 *ov, zig_i64 *res, int n,
686 const zig_i64 *lhs, const zig_i64 *rhs, zig_u8 bits)
687{
688 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i64(&res[i], lhs[i], rhs[i], bits);
689}
690
691static inline bool zig_subo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {
763static inline bool zig_subo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) {
692764#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
693 zig_u8 full_res;
765 uint8_t full_res;
694766 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
695767 *res = zig_wrap_u8(full_res, bits);
696 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
768 return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits);
697769#else
698 zig_u32 full_res;
770 uint32_t full_res;
699771 bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits);
700 *res = (zig_u8)full_res;
772 *res = (uint8_t)full_res;
701773 return overflow;
702774#endif
703775}
704776
705static inline void zig_vsubo_u8(zig_u8 *ov, zig_u8 *res, int n,
706 const zig_u8 *lhs, const zig_u8 *rhs, zig_u8 bits)
707{
708 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u8(&res[i], lhs[i], rhs[i], bits);
709}
710
711static inline bool zig_subo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {
777static inline bool zig_subo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits) {
712778#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
713 zig_i8 full_res;
779 int8_t full_res;
714780 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
715781 *res = zig_wrap_i8(full_res, bits);
716 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
782 return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits);
717783#else
718 zig_i32 full_res;
784 int32_t full_res;
719785 bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits);
720 *res = (zig_i8)full_res;
786 *res = (int8_t)full_res;
721787 return overflow;
722788#endif
723789}
724790
725static inline void zig_vsubo_i8(zig_u8 *ov, zig_i8 *res, int n,
726 const zig_i8 *lhs, const zig_i8 *rhs, zig_u8 bits)
727{
728 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i8(&res[i], lhs[i], rhs[i], bits);
729}
730
731
732static inline bool zig_subo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {
791static inline bool zig_subo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8_t bits) {
733792#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
734 zig_u16 full_res;
793 uint16_t full_res;
735794 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
736795 *res = zig_wrap_u16(full_res, bits);
737 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
796 return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits);
738797#else
739 zig_u32 full_res;
798 uint32_t full_res;
740799 bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits);
741 *res = (zig_u16)full_res;
800 *res = (uint16_t)full_res;
742801 return overflow;
743802#endif
744803}
745804
746static inline void zig_vsubo_u16(zig_u8 *ov, zig_u16 *res, int n,
747 const zig_u16 *lhs, const zig_u16 *rhs, zig_u8 bits)
748{
749 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u16(&res[i], lhs[i], rhs[i], bits);
750}
751
752
753static inline bool zig_subo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {
805static inline bool zig_subo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t bits) {
754806#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
755 zig_i16 full_res;
807 int16_t full_res;
756808 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
757809 *res = zig_wrap_i16(full_res, bits);
758 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
810 return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits);
759811#else
760 zig_i32 full_res;
812 int32_t full_res;
761813 bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits);
762 *res = (zig_i16)full_res;
814 *res = (int16_t)full_res;
763815 return overflow;
764816#endif
765817}
766818
767static inline void zig_vsubo_i16(zig_u8 *ov, zig_i16 *res, int n,
768 const zig_i16 *lhs, const zig_i16 *rhs, zig_u8 bits)
769{
770 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i16(&res[i], lhs[i], rhs[i], bits);
771}
772
773static inline bool zig_mulo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {
819static inline bool zig_mulo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) {
774820#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
775 zig_u32 full_res;
821 uint32_t full_res;
776822 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
777823 *res = zig_wrap_u32(full_res, bits);
778 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
824 return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits);
779825#else
780826 *res = zig_mulw_u32(lhs, rhs, bits);
781 return rhs != zig_as_u32(0) && lhs > zig_maxInt(u32, bits) / rhs;
827 return rhs != UINT32_C(0) && lhs > zig_maxInt_u(32, bits) / rhs;
782828#endif
783829}
784830
785static inline void zig_vmulo_u32(zig_u8 *ov, zig_u32 *res, int n,
786 const zig_u32 *lhs, const zig_u32 *rhs, zig_u8 bits)
787{
788 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u32(&res[i], lhs[i], rhs[i], bits);
789}
790
791zig_extern zig_i32 __mulosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
792static inline bool zig_mulo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
831zig_extern int32_t __mulosi4(int32_t lhs, int32_t rhs, int *overflow);
832static inline bool zig_mulo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
793833#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
794 zig_i32 full_res;
834 int32_t full_res;
795835 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
796836#else
797 zig_c_int overflow_int;
798 zig_i32 full_res = __mulosi4(lhs, rhs, &overflow_int);
837 int overflow_int;
838 int32_t full_res = __mulosi4(lhs, rhs, &overflow_int);
799839 bool overflow = overflow_int != 0;
800840#endif
801841 *res = zig_wrap_i32(full_res, bits);
802 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);
803}
804
805static inline void zig_vmulo_i32(zig_u8 *ov, zig_i32 *res, int n,
806 const zig_i32 *lhs, const zig_i32 *rhs, zig_u8 bits)
807{
808 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i32(&res[i], lhs[i], rhs[i], bits);
842 return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits);
809843}
810844
811static inline bool zig_mulo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
845static inline bool zig_mulo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) {
812846#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
813 zig_u64 full_res;
847 uint64_t full_res;
814848 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
815849 *res = zig_wrap_u64(full_res, bits);
816 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
850 return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits);
817851#else
818852 *res = zig_mulw_u64(lhs, rhs, bits);
819 return rhs != zig_as_u64(0) && lhs > zig_maxInt(u64, bits) / rhs;
853 return rhs != UINT64_C(0) && lhs > zig_maxInt_u(64, bits) / rhs;
820854#endif
821855}
822856
823static inline void zig_vmulo_u64(zig_u8 *ov, zig_u64 *res, int n,
824 const zig_u64 *lhs, const zig_u64 *rhs, zig_u8 bits)
825{
826 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u64(&res[i], lhs[i], rhs[i], bits);
827}
828
829zig_extern zig_i64 __mulodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
830static inline bool zig_mulo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
857zig_extern int64_t __mulodi4(int64_t lhs, int64_t rhs, int *overflow);
858static inline bool zig_mulo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
831859#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
832 zig_i64 full_res;
860 int64_t full_res;
833861 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
834862#else
835 zig_c_int overflow_int;
836 zig_i64 full_res = __mulodi4(lhs, rhs, &overflow_int);
863 int overflow_int;
864 int64_t full_res = __mulodi4(lhs, rhs, &overflow_int);
837865 bool overflow = overflow_int != 0;
838866#endif
839867 *res = zig_wrap_i64(full_res, bits);
840 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
868 return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits);
841869}
842870
843static inline void zig_vmulo_i64(zig_u8 *ov, zig_i64 *res, int n,
844 const zig_i64 *lhs, const zig_i64 *rhs, zig_u8 bits)
845{
846 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i64(&res[i], lhs[i], rhs[i], bits);
847}
848
849static inline bool zig_mulo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {
871static inline bool zig_mulo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) {
850872#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
851 zig_u8 full_res;
873 uint8_t full_res;
852874 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
853875 *res = zig_wrap_u8(full_res, bits);
854 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
876 return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits);
855877#else
856 zig_u32 full_res;
878 uint32_t full_res;
857879 bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits);
858 *res = (zig_u8)full_res;
880 *res = (uint8_t)full_res;
859881 return overflow;
860882#endif
861883}
862884
863static inline void zig_vmulo_u8(zig_u8 *ov, zig_u8 *res, int n,
864 const zig_u8 *lhs, const zig_u8 *rhs, zig_u8 bits)
865{
866 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u8(&res[i], lhs[i], rhs[i], bits);
867}
868
869static inline bool zig_mulo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {
885static inline bool zig_mulo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits) {
870886#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
871 zig_i8 full_res;
887 int8_t full_res;
872888 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
873889 *res = zig_wrap_i8(full_res, bits);
874 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
890 return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits);
875891#else
876 zig_i32 full_res;
892 int32_t full_res;
877893 bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits);
878 *res = (zig_i8)full_res;
894 *res = (int8_t)full_res;
879895 return overflow;
880896#endif
881897}
882898
883static inline void zig_vmulo_i8(zig_u8 *ov, zig_i8 *res, int n,
884 const zig_i8 *lhs, const zig_i8 *rhs, zig_u8 bits)
885{
886 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i8(&res[i], lhs[i], rhs[i], bits);
887}
888
889static inline bool zig_mulo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {
899static inline bool zig_mulo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8_t bits) {
890900#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
891 zig_u16 full_res;
901 uint16_t full_res;
892902 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
893903 *res = zig_wrap_u16(full_res, bits);
894 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
904 return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits);
895905#else
896 zig_u32 full_res;
906 uint32_t full_res;
897907 bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits);
898 *res = (zig_u16)full_res;
908 *res = (uint16_t)full_res;
899909 return overflow;
900910#endif
901911}
902912
903static inline void zig_vmulo_u16(zig_u8 *ov, zig_u16 *res, int n,
904 const zig_u16 *lhs, const zig_u16 *rhs, zig_u8 bits)
905{
906 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u16(&res[i], lhs[i], rhs[i], bits);
907}
908
909static inline bool zig_mulo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {
913static inline bool zig_mulo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t bits) {
910914#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
911 zig_i16 full_res;
915 int16_t full_res;
912916 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
913917 *res = zig_wrap_i16(full_res, bits);
914 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
918 return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits);
915919#else
916 zig_i32 full_res;
920 int32_t full_res;
917921 bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits);
918 *res = (zig_i16)full_res;
922 *res = (int16_t)full_res;
919923 return overflow;
920924#endif
921925}
922926
923static inline void zig_vmulo_i16(zig_u8 *ov, zig_i16 *res, int n,
924 const zig_i16 *lhs, const zig_i16 *rhs, zig_u8 bits)
925{
926 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i16(&res[i], lhs[i], rhs[i], bits);
927}
928
929927#define zig_int_builtins(w) \
930 static inline bool zig_shlo_u##w(zig_u##w *res, zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \
928 static inline bool zig_shlo_u##w(uint##w##_t *res, uint##w##_t lhs, uint8_t rhs, uint8_t bits) { \
931929 *res = zig_shlw_u##w(lhs, rhs, bits); \
932 return lhs > zig_maxInt(u##w, bits) >> rhs; \
930 return lhs > zig_maxInt_u(w, bits) >> rhs; \
933931 } \
934932\
935 static inline bool zig_shlo_i##w(zig_i##w *res, zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \
933 static inline bool zig_shlo_i##w(int##w##_t *res, int##w##_t lhs, uint8_t rhs, uint8_t bits) { \
936934 *res = zig_shlw_i##w(lhs, rhs, bits); \
937 zig_i##w mask = (zig_i##w)(zig_maxInt_u##w << (bits - rhs - 1)); \
938 return (lhs & mask) != zig_as_i##w(0) && (lhs & mask) != mask; \
935 int##w##_t mask = (int##w##_t)(UINT##w##_MAX << (bits - rhs - 1)); \
936 return (lhs & mask) != INT##w##_C(0) && (lhs & mask) != mask; \
939937 } \
940938\
941 static inline zig_u##w zig_shls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
942 zig_u##w res; \
943 if (rhs >= bits) return lhs != zig_as_u##w(0) ? zig_maxInt(u##w, bits) : lhs; \
944 return zig_shlo_u##w(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u##w, bits) : res; \
939 static inline uint##w##_t zig_shls_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
940 uint##w##_t res; \
941 if (rhs >= bits) return lhs != UINT##w##_C(0) ? zig_maxInt_u(w, bits) : lhs; \
942 return zig_shlo_u##w(&res, lhs, (uint8_t)rhs, bits) ? zig_maxInt_u(w, bits) : res; \
945943 } \
946944\
947 static inline zig_i##w zig_shls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
948 zig_i##w res; \
949 if ((zig_u##w)rhs < (zig_u##w)bits && !zig_shlo_i##w(&res, lhs, rhs, bits)) return res; \
950 return lhs < zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
945 static inline int##w##_t zig_shls_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
946 int##w##_t res; \
947 if ((uint##w##_t)rhs < (uint##w##_t)bits && !zig_shlo_i##w(&res, lhs, (uint8_t)rhs, bits)) return res; \
948 return lhs < INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \
951949 } \
952950\
953 static inline zig_u##w zig_adds_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
954 zig_u##w res; \
955 return zig_addo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \
951 static inline uint##w##_t zig_adds_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
952 uint##w##_t res; \
953 return zig_addo_u##w(&res, lhs, rhs, bits) ? zig_maxInt_u(w, bits) : res; \
956954 } \
957955\
958 static inline zig_i##w zig_adds_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
959 zig_i##w res; \
956 static inline int##w##_t zig_adds_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
957 int##w##_t res; \
960958 if (!zig_addo_i##w(&res, lhs, rhs, bits)) return res; \
961 return res >= zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
959 return res >= INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \
962960 } \
963961\
964 static inline zig_u##w zig_subs_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
965 zig_u##w res; \
966 return zig_subo_u##w(&res, lhs, rhs, bits) ? zig_minInt(u##w, bits) : res; \
962 static inline uint##w##_t zig_subs_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
963 uint##w##_t res; \
964 return zig_subo_u##w(&res, lhs, rhs, bits) ? zig_minInt_u(w, bits) : res; \
967965 } \
968966\
969 static inline zig_i##w zig_subs_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
970 zig_i##w res; \
967 static inline int##w##_t zig_subs_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
968 int##w##_t res; \
971969 if (!zig_subo_i##w(&res, lhs, rhs, bits)) return res; \
972 return res >= zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
970 return res >= INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \
973971 } \
974972\
975 static inline zig_u##w zig_muls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
976 zig_u##w res; \
977 return zig_mulo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \
973 static inline uint##w##_t zig_muls_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
974 uint##w##_t res; \
975 return zig_mulo_u##w(&res, lhs, rhs, bits) ? zig_maxInt_u(w, bits) : res; \
978976 } \
979977\
980 static inline zig_i##w zig_muls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
981 zig_i##w res; \
978 static inline int##w##_t zig_muls_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
979 int##w##_t res; \
982980 if (!zig_mulo_i##w(&res, lhs, rhs, bits)) return res; \
983 return (lhs ^ rhs) < zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
981 return (lhs ^ rhs) < INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \
984982 }
985983zig_int_builtins(8)
986984zig_int_builtins(16)
......@@ -988,89 +986,89 @@ zig_int_builtins(32)
988986zig_int_builtins(64)
989987
990988#define zig_builtin8(name, val) __builtin_##name(val)
991typedef zig_c_uint zig_Builtin8;
989typedef unsigned int zig_Builtin8;
992990
993991#define zig_builtin16(name, val) __builtin_##name(val)
994typedef zig_c_uint zig_Builtin16;
992typedef unsigned int zig_Builtin16;
995993
996994#if INT_MIN <= INT32_MIN
997995#define zig_builtin32(name, val) __builtin_##name(val)
998typedef zig_c_uint zig_Builtin32;
996typedef unsigned int zig_Builtin32;
999997#elif LONG_MIN <= INT32_MIN
1000998#define zig_builtin32(name, val) __builtin_##name##l(val)
1001typedef zig_c_ulong zig_Builtin32;
999typedef unsigned long zig_Builtin32;
10021000#endif
10031001
10041002#if INT_MIN <= INT64_MIN
10051003#define zig_builtin64(name, val) __builtin_##name(val)
1006typedef zig_c_uint zig_Builtin64;
1004typedef unsigned int zig_Builtin64;
10071005#elif LONG_MIN <= INT64_MIN
10081006#define zig_builtin64(name, val) __builtin_##name##l(val)
1009typedef zig_c_ulong zig_Builtin64;
1007typedef unsigned long zig_Builtin64;
10101008#elif LLONG_MIN <= INT64_MIN
10111009#define zig_builtin64(name, val) __builtin_##name##ll(val)
1012typedef zig_c_ulonglong zig_Builtin64;
1010typedef unsigned long long zig_Builtin64;
10131011#endif
10141012
1015static inline zig_u8 zig_byte_swap_u8(zig_u8 val, zig_u8 bits) {
1013static inline uint8_t zig_byte_swap_u8(uint8_t val, uint8_t bits) {
10161014 return zig_wrap_u8(val >> (8 - bits), bits);
10171015}
10181016
1019static inline zig_i8 zig_byte_swap_i8(zig_i8 val, zig_u8 bits) {
1020 return zig_wrap_i8((zig_i8)zig_byte_swap_u8((zig_u8)val, bits), bits);
1017static inline int8_t zig_byte_swap_i8(int8_t val, uint8_t bits) {
1018 return zig_wrap_i8((int8_t)zig_byte_swap_u8((uint8_t)val, bits), bits);
10211019}
10221020
1023static inline zig_u16 zig_byte_swap_u16(zig_u16 val, zig_u8 bits) {
1024 zig_u16 full_res;
1021static inline uint16_t zig_byte_swap_u16(uint16_t val, uint8_t bits) {
1022 uint16_t full_res;
10251023#if zig_has_builtin(bswap16) || defined(zig_gnuc)
10261024 full_res = __builtin_bswap16(val);
10271025#else
1028 full_res = (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 0), 8) << 8 |
1029 (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 8), 8) >> 0;
1026 full_res = (uint16_t)zig_byte_swap_u8((uint8_t)(val >> 0), 8) << 8 |
1027 (uint16_t)zig_byte_swap_u8((uint8_t)(val >> 8), 8) >> 0;
10301028#endif
10311029 return zig_wrap_u16(full_res >> (16 - bits), bits);
10321030}
10331031
1034static inline zig_i16 zig_byte_swap_i16(zig_i16 val, zig_u8 bits) {
1035 return zig_wrap_i16((zig_i16)zig_byte_swap_u16((zig_u16)val, bits), bits);
1032static inline int16_t zig_byte_swap_i16(int16_t val, uint8_t bits) {
1033 return zig_wrap_i16((int16_t)zig_byte_swap_u16((uint16_t)val, bits), bits);
10361034}
10371035
1038static inline zig_u32 zig_byte_swap_u32(zig_u32 val, zig_u8 bits) {
1039 zig_u32 full_res;
1036static inline uint32_t zig_byte_swap_u32(uint32_t val, uint8_t bits) {
1037 uint32_t full_res;
10401038#if zig_has_builtin(bswap32) || defined(zig_gnuc)
10411039 full_res = __builtin_bswap32(val);
10421040#else
1043 full_res = (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 0), 16) << 16 |
1044 (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 16), 16) >> 0;
1041 full_res = (uint32_t)zig_byte_swap_u16((uint16_t)(val >> 0), 16) << 16 |
1042 (uint32_t)zig_byte_swap_u16((uint16_t)(val >> 16), 16) >> 0;
10451043#endif
10461044 return zig_wrap_u32(full_res >> (32 - bits), bits);
10471045}
10481046
1049static inline zig_i32 zig_byte_swap_i32(zig_i32 val, zig_u8 bits) {
1050 return zig_wrap_i32((zig_i32)zig_byte_swap_u32((zig_u32)val, bits), bits);
1047static inline int32_t zig_byte_swap_i32(int32_t val, uint8_t bits) {
1048 return zig_wrap_i32((int32_t)zig_byte_swap_u32((uint32_t)val, bits), bits);
10511049}
10521050
1053static inline zig_u64 zig_byte_swap_u64(zig_u64 val, zig_u8 bits) {
1054 zig_u64 full_res;
1051static inline uint64_t zig_byte_swap_u64(uint64_t val, uint8_t bits) {
1052 uint64_t full_res;
10551053#if zig_has_builtin(bswap64) || defined(zig_gnuc)
10561054 full_res = __builtin_bswap64(val);
10571055#else
1058 full_res = (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 0), 32) << 32 |
1059 (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 32), 32) >> 0;
1056 full_res = (uint64_t)zig_byte_swap_u32((uint32_t)(val >> 0), 32) << 32 |
1057 (uint64_t)zig_byte_swap_u32((uint32_t)(val >> 32), 32) >> 0;
10601058#endif
10611059 return zig_wrap_u64(full_res >> (64 - bits), bits);
10621060}
10631061
1064static inline zig_i64 zig_byte_swap_i64(zig_i64 val, zig_u8 bits) {
1065 return zig_wrap_i64((zig_i64)zig_byte_swap_u64((zig_u64)val, bits), bits);
1062static inline int64_t zig_byte_swap_i64(int64_t val, uint8_t bits) {
1063 return zig_wrap_i64((int64_t)zig_byte_swap_u64((uint64_t)val, bits), bits);
10661064}
10671065
1068static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) {
1069 zig_u8 full_res;
1066static inline uint8_t zig_bit_reverse_u8(uint8_t val, uint8_t bits) {
1067 uint8_t full_res;
10701068#if zig_has_builtin(bitreverse8)
10711069 full_res = __builtin_bitreverse8(val);
10721070#else
1073 static zig_u8 const lut[0x10] = {
1071 static uint8_t const lut[0x10] = {
10741072 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
10751073 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
10761074 };
......@@ -1079,62 +1077,62 @@ static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) {
10791077 return zig_wrap_u8(full_res >> (8 - bits), bits);
10801078}
10811079
1082static inline zig_i8 zig_bit_reverse_i8(zig_i8 val, zig_u8 bits) {
1083 return zig_wrap_i8((zig_i8)zig_bit_reverse_u8((zig_u8)val, bits), bits);
1080static inline int8_t zig_bit_reverse_i8(int8_t val, uint8_t bits) {
1081 return zig_wrap_i8((int8_t)zig_bit_reverse_u8((uint8_t)val, bits), bits);
10841082}
10851083
1086static inline zig_u16 zig_bit_reverse_u16(zig_u16 val, zig_u8 bits) {
1087 zig_u16 full_res;
1084static inline uint16_t zig_bit_reverse_u16(uint16_t val, uint8_t bits) {
1085 uint16_t full_res;
10881086#if zig_has_builtin(bitreverse16)
10891087 full_res = __builtin_bitreverse16(val);
10901088#else
1091 full_res = (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 0), 8) << 8 |
1092 (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 8), 8) >> 0;
1089 full_res = (uint16_t)zig_bit_reverse_u8((uint8_t)(val >> 0), 8) << 8 |
1090 (uint16_t)zig_bit_reverse_u8((uint8_t)(val >> 8), 8) >> 0;
10931091#endif
10941092 return zig_wrap_u16(full_res >> (16 - bits), bits);
10951093}
10961094
1097static inline zig_i16 zig_bit_reverse_i16(zig_i16 val, zig_u8 bits) {
1098 return zig_wrap_i16((zig_i16)zig_bit_reverse_u16((zig_u16)val, bits), bits);
1095static inline int16_t zig_bit_reverse_i16(int16_t val, uint8_t bits) {
1096 return zig_wrap_i16((int16_t)zig_bit_reverse_u16((uint16_t)val, bits), bits);
10991097}
11001098
1101static inline zig_u32 zig_bit_reverse_u32(zig_u32 val, zig_u8 bits) {
1102 zig_u32 full_res;
1099static inline uint32_t zig_bit_reverse_u32(uint32_t val, uint8_t bits) {
1100 uint32_t full_res;
11031101#if zig_has_builtin(bitreverse32)
11041102 full_res = __builtin_bitreverse32(val);
11051103#else
1106 full_res = (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 0), 16) << 16 |
1107 (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 16), 16) >> 0;
1104 full_res = (uint32_t)zig_bit_reverse_u16((uint16_t)(val >> 0), 16) << 16 |
1105 (uint32_t)zig_bit_reverse_u16((uint16_t)(val >> 16), 16) >> 0;
11081106#endif
11091107 return zig_wrap_u32(full_res >> (32 - bits), bits);
11101108}
11111109
1112static inline zig_i32 zig_bit_reverse_i32(zig_i32 val, zig_u8 bits) {
1113 return zig_wrap_i32((zig_i32)zig_bit_reverse_u32((zig_u32)val, bits), bits);
1110static inline int32_t zig_bit_reverse_i32(int32_t val, uint8_t bits) {
1111 return zig_wrap_i32((int32_t)zig_bit_reverse_u32((uint32_t)val, bits), bits);
11141112}
11151113
1116static inline zig_u64 zig_bit_reverse_u64(zig_u64 val, zig_u8 bits) {
1117 zig_u64 full_res;
1114static inline uint64_t zig_bit_reverse_u64(uint64_t val, uint8_t bits) {
1115 uint64_t full_res;
11181116#if zig_has_builtin(bitreverse64)
11191117 full_res = __builtin_bitreverse64(val);
11201118#else
1121 full_res = (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 0), 32) << 32 |
1122 (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 32), 32) >> 0;
1119 full_res = (uint64_t)zig_bit_reverse_u32((uint32_t)(val >> 0), 32) << 32 |
1120 (uint64_t)zig_bit_reverse_u32((uint32_t)(val >> 32), 32) >> 0;
11231121#endif
11241122 return zig_wrap_u64(full_res >> (64 - bits), bits);
11251123}
11261124
1127static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) {
1128 return zig_wrap_i64((zig_i64)zig_bit_reverse_u64((zig_u64)val, bits), bits);
1125static inline int64_t zig_bit_reverse_i64(int64_t val, uint8_t bits) {
1126 return zig_wrap_i64((int64_t)zig_bit_reverse_u64((uint64_t)val, bits), bits);
11291127}
11301128
11311129#define zig_builtin_popcount_common(w) \
1132 static inline zig_u8 zig_popcount_i##w(zig_i##w val, zig_u8 bits) { \
1133 return zig_popcount_u##w((zig_u##w)val, bits); \
1130 static inline uint8_t zig_popcount_i##w(int##w##_t val, uint8_t bits) { \
1131 return zig_popcount_u##w((uint##w##_t)val, bits); \
11341132 }
11351133#if zig_has_builtin(popcount) || defined(zig_gnuc)
11361134#define zig_builtin_popcount(w) \
1137 static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \
1135 static inline uint8_t zig_popcount_u##w(uint##w##_t val, uint8_t bits) { \
11381136 (void)bits; \
11391137 return zig_builtin##w(popcount, val); \
11401138 } \
......@@ -1142,12 +1140,12 @@ static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) {
11421140 zig_builtin_popcount_common(w)
11431141#else
11441142#define zig_builtin_popcount(w) \
1145 static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \
1143 static inline uint8_t zig_popcount_u##w(uint##w##_t val, uint8_t bits) { \
11461144 (void)bits; \
1147 zig_u##w temp = val - ((val >> 1) & (zig_maxInt_u##w / 3)); \
1148 temp = (temp & (zig_maxInt_u##w / 5)) + ((temp >> 2) & (zig_maxInt_u##w / 5)); \
1149 temp = (temp + (temp >> 4)) & (zig_maxInt_u##w / 17); \
1150 return temp * (zig_maxInt_u##w / 255) >> (w - 8); \
1145 uint##w##_t temp = val - ((val >> 1) & (UINT##w##_MAX / 3)); \
1146 temp = (temp & (UINT##w##_MAX / 5)) + ((temp >> 2) & (UINT##w##_MAX / 5)); \
1147 temp = (temp + (temp >> 4)) & (UINT##w##_MAX / 17); \
1148 return temp * (UINT##w##_MAX / 255) >> (w - 8); \
11511149 } \
11521150\
11531151 zig_builtin_popcount_common(w)
......@@ -1158,12 +1156,12 @@ zig_builtin_popcount(32)
11581156zig_builtin_popcount(64)
11591157
11601158#define zig_builtin_ctz_common(w) \
1161 static inline zig_u8 zig_ctz_i##w(zig_i##w val, zig_u8 bits) { \
1162 return zig_ctz_u##w((zig_u##w)val, bits); \
1159 static inline uint8_t zig_ctz_i##w(int##w##_t val, uint8_t bits) { \
1160 return zig_ctz_u##w((uint##w##_t)val, bits); \
11631161 }
11641162#if zig_has_builtin(ctz) || defined(zig_gnuc)
11651163#define zig_builtin_ctz(w) \
1166 static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \
1164 static inline uint8_t zig_ctz_u##w(uint##w##_t val, uint8_t bits) { \
11671165 if (val == 0) return bits; \
11681166 return zig_builtin##w(ctz, val); \
11691167 } \
......@@ -1171,7 +1169,7 @@ zig_builtin_popcount(64)
11711169 zig_builtin_ctz_common(w)
11721170#else
11731171#define zig_builtin_ctz(w) \
1174 static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \
1172 static inline uint8_t zig_ctz_u##w(uint##w##_t val, uint8_t bits) { \
11751173 return zig_popcount_u##w(zig_not_u##w(val, bits) & zig_subw_u##w(val, 1, bits), bits); \
11761174 } \
11771175\
......@@ -1183,12 +1181,12 @@ zig_builtin_ctz(32)
11831181zig_builtin_ctz(64)
11841182
11851183#define zig_builtin_clz_common(w) \
1186 static inline zig_u8 zig_clz_i##w(zig_i##w val, zig_u8 bits) { \
1187 return zig_clz_u##w((zig_u##w)val, bits); \
1184 static inline uint8_t zig_clz_i##w(int##w##_t val, uint8_t bits) { \
1185 return zig_clz_u##w((uint##w##_t)val, bits); \
11881186 }
11891187#if zig_has_builtin(clz) || defined(zig_gnuc)
11901188#define zig_builtin_clz(w) \
1191 static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \
1189 static inline uint8_t zig_clz_u##w(uint##w##_t val, uint8_t bits) { \
11921190 if (val == 0) return bits; \
11931191 return zig_builtin##w(clz, val) - (zig_bitSizeOf(zig_Builtin##w) - bits); \
11941192 } \
......@@ -1196,7 +1194,7 @@ zig_builtin_ctz(64)
11961194 zig_builtin_clz_common(w)
11971195#else
11981196#define zig_builtin_clz(w) \
1199 static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \
1197 static inline uint8_t zig_clz_u##w(uint##w##_t val, uint8_t bits) { \
12001198 return zig_ctz_u##w(zig_bit_reverse_u##w(val, bits), bits); \
12011199 } \
12021200\
......@@ -1207,7 +1205,7 @@ zig_builtin_clz(16)
12071205zig_builtin_clz(32)
12081206zig_builtin_clz(64)
12091207
1210/* ======================== 128-bit Integer Routines ======================== */
1208/* ======================== 128-bit Integer Support ========================= */
12111209
12121210#if !defined(zig_has_int128)
12131211# if defined(__SIZEOF_INT128__)
......@@ -1222,18 +1220,18 @@ zig_builtin_clz(64)
12221220typedef unsigned __int128 zig_u128;
12231221typedef signed __int128 zig_i128;
12241222
1225#define zig_as_u128(hi, lo) ((zig_u128)(hi)<<64|(lo))
1226#define zig_as_i128(hi, lo) ((zig_i128)zig_as_u128(hi, lo))
1227#define zig_as_constant_u128(hi, lo) zig_as_u128(hi, lo)
1228#define zig_as_constant_i128(hi, lo) zig_as_i128(hi, lo)
1229#define zig_hi_u128(val) ((zig_u64)((val) >> 64))
1230#define zig_lo_u128(val) ((zig_u64)((val) >> 0))
1231#define zig_hi_i128(val) ((zig_i64)((val) >> 64))
1232#define zig_lo_i128(val) ((zig_u64)((val) >> 0))
1223#define zig_make_u128(hi, lo) ((zig_u128)(hi)<<64|(lo))
1224#define zig_make_i128(hi, lo) ((zig_i128)zig_make_u128(hi, lo))
1225#define zig_init_u128(hi, lo) zig_make_u128(hi, lo)
1226#define zig_init_i128(hi, lo) zig_make_i128(hi, lo)
1227#define zig_hi_u128(val) ((uint64_t)((val) >> 64))
1228#define zig_lo_u128(val) ((uint64_t)((val) >> 0))
1229#define zig_hi_i128(val) (( int64_t)((val) >> 64))
1230#define zig_lo_i128(val) ((uint64_t)((val) >> 0))
12331231#define zig_bitcast_u128(val) ((zig_u128)(val))
12341232#define zig_bitcast_i128(val) ((zig_i128)(val))
12351233#define zig_cmp_int128(Type) \
1236 static inline zig_i32 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
1234 static inline int32_t zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
12371235 return (lhs > rhs) - (lhs < rhs); \
12381236 }
12391237#define zig_bit_int128(Type, operation, operator) \
......@@ -1243,32 +1241,32 @@ typedef signed __int128 zig_i128;
12431241
12441242#else /* zig_has_int128 */
12451243
1246#if __LITTLE_ENDIAN__ || _MSC_VER
1247typedef struct { zig_align(16) zig_u64 lo; zig_u64 hi; } zig_u128;
1248typedef struct { zig_align(16) zig_u64 lo; zig_i64 hi; } zig_i128;
1244#if zig_little_endian
1245typedef struct { zig_align(16) uint64_t lo; uint64_t hi; } zig_u128;
1246typedef struct { zig_align(16) uint64_t lo; int64_t hi; } zig_i128;
12491247#else
1250typedef struct { zig_align(16) zig_u64 hi; zig_u64 lo; } zig_u128;
1251typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128;
1248typedef struct { zig_align(16) uint64_t hi; uint64_t lo; } zig_u128;
1249typedef struct { zig_align(16) int64_t hi; uint64_t lo; } zig_i128;
12521250#endif
12531251
1254#define zig_as_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) })
1255#define zig_as_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) })
1252#define zig_make_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) })
1253#define zig_make_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) })
12561254
1257#if _MSC_VER
1258#define zig_as_constant_u128(hi, lo) { .h##i = (hi), .l##o = (lo) }
1259#define zig_as_constant_i128(hi, lo) { .h##i = (hi), .l##o = (lo) }
1260#else
1261#define zig_as_constant_u128(hi, lo) zig_as_u128(hi, lo)
1262#define zig_as_constant_i128(hi, lo) zig_as_i128(hi, lo)
1255#if _MSC_VER /* MSVC doesn't allow struct literals in constant expressions */
1256#define zig_init_u128(hi, lo) { .h##i = (hi), .l##o = (lo) }
1257#define zig_init_i128(hi, lo) { .h##i = (hi), .l##o = (lo) }
1258#else /* But non-MSVC doesn't like the unprotected commas */
1259#define zig_init_u128(hi, lo) zig_make_u128(hi, lo)
1260#define zig_init_i128(hi, lo) zig_make_i128(hi, lo)
12631261#endif
12641262#define zig_hi_u128(val) ((val).hi)
12651263#define zig_lo_u128(val) ((val).lo)
12661264#define zig_hi_i128(val) ((val).hi)
12671265#define zig_lo_i128(val) ((val).lo)
1268#define zig_bitcast_u128(val) zig_as_u128((zig_u64)(val).hi, (val).lo)
1269#define zig_bitcast_i128(val) zig_as_i128((zig_i64)(val).hi, (val).lo)
1266#define zig_bitcast_u128(val) zig_make_u128((uint64_t)(val).hi, (val).lo)
1267#define zig_bitcast_i128(val) zig_make_i128(( int64_t)(val).hi, (val).lo)
12701268#define zig_cmp_int128(Type) \
1271 static inline zig_i32 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
1269 static inline int32_t zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
12721270 return (lhs.hi == rhs.hi) \
12731271 ? (lhs.lo > rhs.lo) - (lhs.lo < rhs.lo) \
12741272 : (lhs.hi > rhs.hi) - (lhs.hi < rhs.hi); \
......@@ -1280,10 +1278,10 @@ typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128;
12801278
12811279#endif /* zig_has_int128 */
12821280
1283#define zig_minInt_u128 zig_as_u128(zig_minInt_u64, zig_minInt_u64)
1284#define zig_maxInt_u128 zig_as_u128(zig_maxInt_u64, zig_maxInt_u64)
1285#define zig_minInt_i128 zig_as_i128(zig_minInt_i64, zig_minInt_u64)
1286#define zig_maxInt_i128 zig_as_i128(zig_maxInt_i64, zig_maxInt_u64)
1281#define zig_minInt_u128 zig_make_u128(zig_minInt_u64, zig_minInt_u64)
1282#define zig_maxInt_u128 zig_make_u128(zig_maxInt_u64, zig_maxInt_u64)
1283#define zig_minInt_i128 zig_make_i128(zig_minInt_i64, zig_minInt_u64)
1284#define zig_maxInt_i128 zig_make_i128(zig_maxInt_i64, zig_maxInt_u64)
12871285
12881286zig_cmp_int128(u128)
12891287zig_cmp_int128(i128)
......@@ -1297,28 +1295,33 @@ zig_bit_int128(i128, or, |)
12971295zig_bit_int128(u128, xor, ^)
12981296zig_bit_int128(i128, xor, ^)
12991297
1300static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs);
1298static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs);
13011299
13021300#if zig_has_int128
13031301
1304static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) {
1305 return val ^ zig_maxInt(u128, bits);
1302static inline zig_u128 zig_not_u128(zig_u128 val, uint8_t bits) {
1303 return val ^ zig_maxInt_u(128, bits);
13061304}
13071305
1308static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) {
1306static inline zig_i128 zig_not_i128(zig_i128 val, uint8_t bits) {
13091307 (void)bits;
13101308 return ~val;
13111309}
13121310
1313static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) {
1311static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) {
13141312 return lhs >> rhs;
13151313}
13161314
1317static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) {
1315static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) {
13181316 return lhs << rhs;
13191317}
13201318
1321static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) {
1319static inline zig_i128 zig_shr_i128(zig_i128 lhs, uint8_t rhs) {
1320 zig_i128 sign_mask = lhs < zig_make_i128(0, 0) ? -zig_make_i128(0, 1) : zig_make_i128(0, 0);
1321 return ((lhs ^ sign_mask) >> rhs) ^ sign_mask;
1322}
1323
1324static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) {
13221325 return lhs << rhs;
13231326}
13241327
......@@ -1363,40 +1366,46 @@ static inline zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {
13631366}
13641367
13651368static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) {
1366 return zig_div_trunc_i128(lhs, rhs) - (((lhs ^ rhs) & zig_rem_i128(lhs, rhs)) < zig_as_i128(0, 0));
1369 return zig_div_trunc_i128(lhs, rhs) - (((lhs ^ rhs) & zig_rem_i128(lhs, rhs)) < zig_make_i128(0, 0));
13671370}
13681371
13691372static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
13701373 zig_i128 rem = zig_rem_i128(lhs, rhs);
1371 return rem + (((lhs ^ rhs) & rem) < zig_as_i128(0, 0) ? rhs : zig_as_i128(0, 0));
1374 return rem + (((lhs ^ rhs) & rem) < zig_make_i128(0, 0) ? rhs : zig_make_i128(0, 0));
13721375}
13731376
13741377#else /* zig_has_int128 */
13751378
1376static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) {
1377 return (zig_u128){ .hi = zig_not_u64(val.hi, bits - zig_as_u8(64)), .lo = zig_not_u64(val.lo, zig_as_u8(64)) };
1379static inline zig_u128 zig_not_u128(zig_u128 val, uint8_t bits) {
1380 return (zig_u128){ .hi = zig_not_u64(val.hi, bits - UINT8_C(64)), .lo = zig_not_u64(val.lo, UINT8_C(64)) };
13781381}
13791382
1380static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) {
1381 return (zig_i128){ .hi = zig_not_i64(val.hi, bits - zig_as_u8(64)), .lo = zig_not_u64(val.lo, zig_as_u8(64)) };
1383static inline zig_i128 zig_not_i128(zig_i128 val, uint8_t bits) {
1384 return (zig_i128){ .hi = zig_not_i64(val.hi, bits - UINT8_C(64)), .lo = zig_not_u64(val.lo, UINT8_C(64)) };
13821385}
13831386
1384static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) {
1385 if (rhs == zig_as_u8(0)) return lhs;
1386 if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = zig_minInt_u64, .lo = lhs.hi >> (rhs - zig_as_u8(64)) };
1387 return (zig_u128){ .hi = lhs.hi >> rhs, .lo = lhs.hi << (zig_as_u8(64) - rhs) | lhs.lo >> rhs };
1387static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) {
1388 if (rhs == UINT8_C(0)) return lhs;
1389 if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = zig_minInt_u64, .lo = lhs.hi >> (rhs - UINT8_C(64)) };
1390 return (zig_u128){ .hi = lhs.hi >> rhs, .lo = lhs.hi << (UINT8_C(64) - rhs) | lhs.lo >> rhs };
13881391}
13891392
1390static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) {
1391 if (rhs == zig_as_u8(0)) return lhs;
1392 if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = lhs.lo << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 };
1393 return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };
1393static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) {
1394 if (rhs == UINT8_C(0)) return lhs;
1395 if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 };
1396 return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs };
13941397}
13951398
1396static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) {
1397 if (rhs == zig_as_u8(0)) return lhs;
1398 if (rhs >= zig_as_u8(64)) return (zig_i128){ .hi = lhs.lo << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 };
1399 return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };
1399static inline zig_i128 zig_shr_i128(zig_i128 lhs, uint8_t rhs) {
1400 if (rhs == UINT8_C(0)) return lhs;
1401 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))) };
1402 return (zig_i128){ .hi = zig_shr_i64(lhs.hi, rhs), .lo = lhs.lo >> rhs | (uint64_t)lhs.hi << (UINT8_C(64) - rhs) };
1403}
1404
1405static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) {
1406 if (rhs == UINT8_C(0)) return lhs;
1407 if (rhs >= UINT8_C(64)) return (zig_i128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 };
1408 return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs };
14001409}
14011410
14021411static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) {
......@@ -1424,14 +1433,14 @@ static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) {
14241433}
14251434
14261435zig_extern zig_i128 __multi3(zig_i128 lhs, zig_i128 rhs);
1427static zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) {
1428 return zig_bitcast_u128(__multi3(zig_bitcast_i128(lhs), zig_bitcast_i128(rhs)));
1429}
1430
14311436static zig_i128 zig_mul_i128(zig_i128 lhs, zig_i128 rhs) {
14321437 return __multi3(lhs, rhs);
14331438}
14341439
1440static zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) {
1441 return zig_bitcast_u128(zig_mul_i128(zig_bitcast_i128(lhs), zig_bitcast_i128(rhs)));
1442}
1443
14351444zig_extern zig_u128 __udivti3(zig_u128 lhs, zig_u128 rhs);
14361445static zig_u128 zig_div_trunc_u128(zig_u128 lhs, zig_u128 rhs) {
14371446 return __udivti3(lhs, rhs);
......@@ -1454,11 +1463,11 @@ static zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {
14541463
14551464static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
14561465 zig_i128 rem = zig_rem_i128(lhs, rhs);
1457 return zig_add_i128(rem, (((lhs.hi ^ rhs.hi) & rem.hi) < zig_as_i64(0) ? rhs : zig_as_i128(0, 0)));
1466 return zig_add_i128(rem, ((lhs.hi ^ rhs.hi) & rem.hi) < INT64_C(0) ? rhs : zig_make_i128(0, 0));
14581467}
14591468
14601469static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) {
1461 return zig_sub_i128(zig_div_trunc_i128(lhs, rhs), zig_as_i128(0, zig_cmp_i128(zig_and_i128(zig_xor_i128(lhs, rhs), zig_rem_i128(lhs, rhs)), zig_as_i128(0, 0)) < zig_as_i32(0)));
1470 return zig_sub_i128(zig_div_trunc_i128(lhs, rhs), zig_make_i128(0, zig_cmp_i128(zig_and_i128(zig_xor_i128(lhs, rhs), zig_rem_i128(lhs, rhs)), zig_make_i128(0, 0)) < INT32_C(0)));
14621471}
14631472
14641473#endif /* zig_has_int128 */
......@@ -1471,326 +1480,1265 @@ static inline zig_u128 zig_nand_u128(zig_u128 lhs, zig_u128 rhs) {
14711480}
14721481
14731482static inline zig_u128 zig_min_u128(zig_u128 lhs, zig_u128 rhs) {
1474 return zig_cmp_u128(lhs, rhs) < zig_as_i32(0) ? lhs : rhs;
1483 return zig_cmp_u128(lhs, rhs) < INT32_C(0) ? lhs : rhs;
14751484}
14761485
14771486static inline zig_i128 zig_min_i128(zig_i128 lhs, zig_i128 rhs) {
1478 return zig_cmp_i128(lhs, rhs) < zig_as_i32(0) ? lhs : rhs;
1487 return zig_cmp_i128(lhs, rhs) < INT32_C(0) ? lhs : rhs;
14791488}
14801489
14811490static inline zig_u128 zig_max_u128(zig_u128 lhs, zig_u128 rhs) {
1482 return zig_cmp_u128(lhs, rhs) > zig_as_i32(0) ? lhs : rhs;
1491 return zig_cmp_u128(lhs, rhs) > INT32_C(0) ? lhs : rhs;
14831492}
14841493
14851494static inline zig_i128 zig_max_i128(zig_i128 lhs, zig_i128 rhs) {
1486 return zig_cmp_i128(lhs, rhs) > zig_as_i32(0) ? lhs : rhs;
1495 return zig_cmp_i128(lhs, rhs) > INT32_C(0) ? lhs : rhs;
14871496}
14881497
1489static inline zig_i128 zig_shr_i128(zig_i128 lhs, zig_u8 rhs) {
1490 zig_i128 sign_mask = zig_cmp_i128(lhs, zig_as_i128(0, 0)) < zig_as_i32(0) ? zig_sub_i128(zig_as_i128(0, 0), zig_as_i128(0, 1)) : zig_as_i128(0, 0);
1491 return zig_xor_i128(zig_bitcast_i128(zig_shr_u128(zig_bitcast_u128(zig_xor_i128(lhs, sign_mask)), rhs)), sign_mask);
1498static inline zig_u128 zig_wrap_u128(zig_u128 val, uint8_t bits) {
1499 return zig_and_u128(val, zig_maxInt_u(128, bits));
14921500}
14931501
1494static inline zig_u128 zig_wrap_u128(zig_u128 val, zig_u8 bits) {
1495 return zig_and_u128(val, zig_maxInt(u128, bits));
1502static inline zig_i128 zig_wrap_i128(zig_i128 val, uint8_t bits) {
1503 if (bits > UINT8_C(64)) return zig_make_i128(zig_wrap_i64(zig_hi_i128(val), bits - UINT8_C(64)), zig_lo_i128(val));
1504 int64_t lo = zig_wrap_i64((int64_t)zig_lo_i128(val), bits);
1505 return zig_make_i128(zig_shr_i64(lo, 63), (uint64_t)lo);
14961506}
14971507
1498static inline zig_i128 zig_wrap_i128(zig_i128 val, zig_u8 bits) {
1499 return zig_as_i128(zig_wrap_i64(zig_hi_i128(val), bits - zig_as_u8(64)), zig_lo_i128(val));
1500}
1501
1502static inline zig_u128 zig_shlw_u128(zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {
1508static inline zig_u128 zig_shlw_u128(zig_u128 lhs, uint8_t rhs, uint8_t bits) {
15031509 return zig_wrap_u128(zig_shl_u128(lhs, rhs), bits);
15041510}
15051511
1506static inline zig_i128 zig_shlw_i128(zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {
1512static inline zig_i128 zig_shlw_i128(zig_i128 lhs, uint8_t rhs, uint8_t bits) {
15071513 return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), rhs)), bits);
15081514}
15091515
1510static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1516static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
15111517 return zig_wrap_u128(zig_add_u128(lhs, rhs), bits);
15121518}
15131519
1514static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1520static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
15151521 return zig_wrap_i128(zig_bitcast_i128(zig_add_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
15161522}
15171523
1518static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1524static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
15191525 return zig_wrap_u128(zig_sub_u128(lhs, rhs), bits);
15201526}
15211527
1522static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1528static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
15231529 return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
15241530}
15251531
1526static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1532static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
15271533 return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits);
15281534}
15291535
1530static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1536static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
15311537 return zig_wrap_i128(zig_bitcast_i128(zig_mul_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
15321538}
15331539
15341540#if zig_has_int128
15351541
1536static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1542static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
15371543#if zig_has_builtin(add_overflow)
15381544 zig_u128 full_res;
15391545 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
15401546 *res = zig_wrap_u128(full_res, bits);
1541 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1547 return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits);
15421548#else
15431549 *res = zig_addw_u128(lhs, rhs, bits);
15441550 return *res < lhs;
15451551#endif
15461552}
15471553
1548zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1549static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1554zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1555static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
15501556#if zig_has_builtin(add_overflow)
15511557 zig_i128 full_res;
15521558 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
15531559#else
1554 zig_c_int overflow_int;
1560 int overflow_int;
15551561 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
15561562 bool overflow = overflow_int != 0;
15571563#endif
15581564 *res = zig_wrap_i128(full_res, bits);
1559 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1565 return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits);
15601566}
15611567
1562static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1568static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
15631569#if zig_has_builtin(sub_overflow)
15641570 zig_u128 full_res;
15651571 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
15661572 *res = zig_wrap_u128(full_res, bits);
1567 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1573 return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits);
15681574#else
15691575 *res = zig_subw_u128(lhs, rhs, bits);
15701576 return *res > lhs;
15711577#endif
15721578}
15731579
1574zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1575static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1580zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1581static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
15761582#if zig_has_builtin(sub_overflow)
15771583 zig_i128 full_res;
15781584 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
15791585#else
1580 zig_c_int overflow_int;
1586 int overflow_int;
15811587 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
15821588 bool overflow = overflow_int != 0;
15831589#endif
15841590 *res = zig_wrap_i128(full_res, bits);
1585 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1591 return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits);
15861592}
15871593
1588static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1594static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
15891595#if zig_has_builtin(mul_overflow)
15901596 zig_u128 full_res;
15911597 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
15921598 *res = zig_wrap_u128(full_res, bits);
1593 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1599 return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits);
15941600#else
15951601 *res = zig_mulw_u128(lhs, rhs, bits);
1596 return rhs != zig_as_u128(0, 0) && lhs > zig_maxInt(u128, bits) / rhs;
1602 return rhs != zig_make_u128(0, 0) && lhs > zig_maxInt_u(128, bits) / rhs;
15971603#endif
15981604}
15991605
1600zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1601static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1606zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1607static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
16021608#if zig_has_builtin(mul_overflow)
16031609 zig_i128 full_res;
16041610 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
16051611#else
1606 zig_c_int overflow_int;
1612 int overflow_int;
16071613 zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int);
16081614 bool overflow = overflow_int != 0;
16091615#endif
16101616 *res = zig_wrap_i128(full_res, bits);
1611 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1617 return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits);
16121618}
16131619
16141620#else /* zig_has_int128 */
16151621
1616static inline bool zig_overflow_u128(bool overflow, zig_u128 full_res, zig_u8 bits) {
1617 return overflow ||
1618 zig_cmp_u128(full_res, zig_minInt(u128, bits)) < zig_as_i32(0) ||
1619 zig_cmp_u128(full_res, zig_maxInt(u128, bits)) > zig_as_i32(0);
1622static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1623 uint64_t hi;
1624 bool overflow = zig_addo_u64(&hi, lhs.hi, rhs.hi, bits - 64);
1625 return overflow ^ zig_addo_u64(&res->hi, hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64);
16201626}
16211627
1622static inline bool zig_overflow_i128(bool overflow, zig_i128 full_res, zig_u8 bits) {
1623 return overflow ||
1624 zig_cmp_i128(full_res, zig_minInt(i128, bits)) < zig_as_i32(0) ||
1625 zig_cmp_i128(full_res, zig_maxInt(i128, bits)) > zig_as_i32(0);
1628static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1629 int64_t hi;
1630 bool overflow = zig_addo_i64(&hi, lhs.hi, rhs.hi, bits - 64);
1631 return overflow ^ zig_addo_i64(&res->hi, hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64);
16261632}
16271633
1628static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1629 zig_u128 full_res;
1630 bool overflow =
1631 zig_addo_u64(&full_res.hi, lhs.hi, rhs.hi, 64) |
1632 zig_addo_u64(&full_res.hi, full_res.hi, zig_addo_u64(&full_res.lo, lhs.lo, rhs.lo, 64), 64);
1633 *res = zig_wrap_u128(full_res, bits);
1634 return zig_overflow_u128(overflow, full_res, bits);
1634static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1635 uint64_t hi;
1636 bool overflow = zig_subo_u64(&hi, lhs.hi, rhs.hi, bits - 64);
1637 return overflow ^ zig_subo_u64(&res->hi, hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64);
16351638}
16361639
1637zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1638static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1639 zig_c_int overflow_int;
1640 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
1641 *res = zig_wrap_i128(full_res, bits);
1642 return zig_overflow_i128(overflow_int, full_res, bits);
1640static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1641 int64_t hi;
1642 bool overflow = zig_subo_i64(&hi, lhs.hi, rhs.hi, bits - 64);
1643 return overflow ^ zig_subo_i64(&res->hi, hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64);
16431644}
16441645
1645static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1646 zig_u128 full_res;
1647 bool overflow =
1648 zig_subo_u64(&full_res.hi, lhs.hi, rhs.hi, 64) |
1649 zig_subo_u64(&full_res.hi, full_res.hi, zig_subo_u64(&full_res.lo, lhs.lo, rhs.lo, 64), 64);
1650 *res = zig_wrap_u128(full_res, bits);
1651 return zig_overflow_u128(overflow, full_res, bits);
1652}
1653
1654zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1655static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1656 zig_c_int overflow_int;
1657 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
1658 *res = zig_wrap_i128(full_res, bits);
1659 return zig_overflow_i128(overflow_int, full_res, bits);
1660}
1661
1662static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1646static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
16631647 *res = zig_mulw_u128(lhs, rhs, bits);
1664 return zig_cmp_u128(*res, zig_as_u128(0, 0)) != zig_as_i32(0) &&
1665 zig_cmp_u128(lhs, zig_div_trunc_u128(zig_maxInt(u128, bits), rhs)) > zig_as_i32(0);
1648 return zig_cmp_u128(*res, zig_make_u128(0, 0)) != INT32_C(0) &&
1649 zig_cmp_u128(lhs, zig_div_trunc_u128(zig_maxInt_u(128, bits), rhs)) > INT32_C(0);
16661650}
16671651
1668zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1669static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1670 zig_c_int overflow_int;
1652zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1653static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1654 int overflow_int;
16711655 zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int);
1656 bool overflow = overflow_int != 0 ||
1657 zig_cmp_i128(full_res, zig_minInt_i(128, bits)) < INT32_C(0) ||
1658 zig_cmp_i128(full_res, zig_maxInt_i(128, bits)) > INT32_C(0);
16721659 *res = zig_wrap_i128(full_res, bits);
1673 return zig_overflow_i128(overflow_int, full_res, bits);
1660 return overflow;
16741661}
16751662
16761663#endif /* zig_has_int128 */
16771664
1678static inline bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {
1665static inline bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, uint8_t rhs, uint8_t bits) {
16791666 *res = zig_shlw_u128(lhs, rhs, bits);
1680 return zig_cmp_u128(lhs, zig_shr_u128(zig_maxInt(u128, bits), rhs)) > zig_as_i32(0);
1667 return zig_cmp_u128(lhs, zig_shr_u128(zig_maxInt_u(128, bits), rhs)) > INT32_C(0);
16811668}
16821669
1683static inline bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {
1670static inline bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, uint8_t rhs, uint8_t bits) {
16841671 *res = zig_shlw_i128(lhs, rhs, bits);
1685 zig_i128 mask = zig_bitcast_i128(zig_shl_u128(zig_maxInt_u128, bits - rhs - zig_as_u8(1)));
1686 return zig_cmp_i128(zig_and_i128(lhs, mask), zig_as_i128(0, 0)) != zig_as_i32(0) &&
1687 zig_cmp_i128(zig_and_i128(lhs, mask), mask) != zig_as_i32(0);
1672 zig_i128 mask = zig_bitcast_i128(zig_shl_u128(zig_maxInt_u128, bits - rhs - UINT8_C(1)));
1673 return zig_cmp_i128(zig_and_i128(lhs, mask), zig_make_i128(0, 0)) != INT32_C(0) &&
1674 zig_cmp_i128(zig_and_i128(lhs, mask), mask) != INT32_C(0);
16881675}
16891676
1690static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1677static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
16911678 zig_u128 res;
1692 if (zig_cmp_u128(rhs, zig_as_u128(0, bits)) >= zig_as_i32(0))
1693 return zig_cmp_u128(lhs, zig_as_u128(0, 0)) != zig_as_i32(0) ? zig_maxInt(u128, bits) : lhs;
1694
1695#if zig_has_int128
1696 return zig_shlo_u128(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u128, bits) : res;
1697#else
1698 return zig_shlo_u128(&res, lhs, (zig_u8)rhs.lo, bits) ? zig_maxInt(u128, bits) : res;
1699#endif
1679 if (zig_cmp_u128(rhs, zig_make_u128(0, bits)) >= INT32_C(0))
1680 return zig_cmp_u128(lhs, zig_make_u128(0, 0)) != INT32_C(0) ? zig_maxInt_u(128, bits) : lhs;
1681 return zig_shlo_u128(&res, lhs, (uint8_t)zig_lo_u128(rhs), bits) ? zig_maxInt_u(128, bits) : res;
17001682}
17011683
1702static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1684static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
17031685 zig_i128 res;
1704 if (zig_cmp_u128(zig_bitcast_u128(rhs), zig_as_u128(0, bits)) < zig_as_i32(0) && !zig_shlo_i128(&res, lhs, zig_lo_i128(rhs), bits)) return res;
1705 return zig_cmp_i128(lhs, zig_as_i128(0, 0)) < zig_as_i32(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1686 if (zig_cmp_u128(zig_bitcast_u128(rhs), zig_make_u128(0, bits)) < INT32_C(0) && !zig_shlo_i128(&res, lhs, (uint8_t)zig_lo_i128(rhs), bits)) return res;
1687 return zig_cmp_i128(lhs, zig_make_i128(0, 0)) < INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits);
17061688}
17071689
1708static inline zig_u128 zig_adds_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1690static inline zig_u128 zig_adds_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
17091691 zig_u128 res;
1710 return zig_addo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res;
1692 return zig_addo_u128(&res, lhs, rhs, bits) ? zig_maxInt_u(128, bits) : res;
17111693}
17121694
1713static inline zig_i128 zig_adds_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1695static inline zig_i128 zig_adds_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
17141696 zig_i128 res;
17151697 if (!zig_addo_i128(&res, lhs, rhs, bits)) return res;
1716 return zig_cmp_i128(res, zig_as_i128(0, 0)) >= zig_as_i32(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1698 return zig_cmp_i128(res, zig_make_i128(0, 0)) >= INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits);
17171699}
17181700
1719static inline zig_u128 zig_subs_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1701static inline zig_u128 zig_subs_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
17201702 zig_u128 res;
1721 return zig_subo_u128(&res, lhs, rhs, bits) ? zig_minInt(u128, bits) : res;
1703 return zig_subo_u128(&res, lhs, rhs, bits) ? zig_minInt_u(128, bits) : res;
17221704}
17231705
1724static inline zig_i128 zig_subs_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1706static inline zig_i128 zig_subs_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
17251707 zig_i128 res;
17261708 if (!zig_subo_i128(&res, lhs, rhs, bits)) return res;
1727 return zig_cmp_i128(res, zig_as_i128(0, 0)) >= zig_as_i32(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1709 return zig_cmp_i128(res, zig_make_i128(0, 0)) >= INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits);
17281710}
17291711
1730static inline zig_u128 zig_muls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1712static inline zig_u128 zig_muls_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
17311713 zig_u128 res;
1732 return zig_mulo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res;
1714 return zig_mulo_u128(&res, lhs, rhs, bits) ? zig_maxInt_u(128, bits) : res;
17331715}
17341716
1735static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1717static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
17361718 zig_i128 res;
17371719 if (!zig_mulo_i128(&res, lhs, rhs, bits)) return res;
1738 return zig_cmp_i128(zig_xor_i128(lhs, rhs), zig_as_i128(0, 0)) < zig_as_i32(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1720 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);
17391721}
17401722
1741static inline zig_u8 zig_clz_u128(zig_u128 val, zig_u8 bits) {
1742 if (bits <= zig_as_u8(64)) return zig_clz_u64(zig_lo_u128(val), bits);
1743 if (zig_hi_u128(val) != 0) return zig_clz_u64(zig_hi_u128(val), bits - zig_as_u8(64));
1744 return zig_clz_u64(zig_lo_u128(val), zig_as_u8(64)) + (bits - zig_as_u8(64));
1723static inline uint8_t zig_clz_u128(zig_u128 val, uint8_t bits) {
1724 if (bits <= UINT8_C(64)) return zig_clz_u64(zig_lo_u128(val), bits);
1725 if (zig_hi_u128(val) != 0) return zig_clz_u64(zig_hi_u128(val), bits - UINT8_C(64));
1726 return zig_clz_u64(zig_lo_u128(val), UINT8_C(64)) + (bits - UINT8_C(64));
17451727}
17461728
1747static inline zig_u8 zig_clz_i128(zig_i128 val, zig_u8 bits) {
1729static inline uint8_t zig_clz_i128(zig_i128 val, uint8_t bits) {
17481730 return zig_clz_u128(zig_bitcast_u128(val), bits);
17491731}
17501732
1751static inline zig_u8 zig_ctz_u128(zig_u128 val, zig_u8 bits) {
1752 if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), zig_as_u8(64));
1753 return zig_ctz_u64(zig_hi_u128(val), bits - zig_as_u8(64)) + zig_as_u8(64);
1733static inline uint8_t zig_ctz_u128(zig_u128 val, uint8_t bits) {
1734 if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), UINT8_C(64));
1735 return zig_ctz_u64(zig_hi_u128(val), bits - UINT8_C(64)) + UINT8_C(64);
17541736}
17551737
1756static inline zig_u8 zig_ctz_i128(zig_i128 val, zig_u8 bits) {
1738static inline uint8_t zig_ctz_i128(zig_i128 val, uint8_t bits) {
17571739 return zig_ctz_u128(zig_bitcast_u128(val), bits);
17581740}
17591741
1760static inline zig_u8 zig_popcount_u128(zig_u128 val, zig_u8 bits) {
1761 return zig_popcount_u64(zig_hi_u128(val), bits - zig_as_u8(64)) +
1762 zig_popcount_u64(zig_lo_u128(val), zig_as_u8(64));
1742static inline uint8_t zig_popcount_u128(zig_u128 val, uint8_t bits) {
1743 return zig_popcount_u64(zig_hi_u128(val), bits - UINT8_C(64)) +
1744 zig_popcount_u64(zig_lo_u128(val), UINT8_C(64));
17631745}
17641746
1765static inline zig_u8 zig_popcount_i128(zig_i128 val, zig_u8 bits) {
1747static inline uint8_t zig_popcount_i128(zig_i128 val, uint8_t bits) {
17661748 return zig_popcount_u128(zig_bitcast_u128(val), bits);
17671749}
17681750
1769static inline zig_u128 zig_byte_swap_u128(zig_u128 val, zig_u8 bits) {
1751static inline zig_u128 zig_byte_swap_u128(zig_u128 val, uint8_t bits) {
17701752 zig_u128 full_res;
17711753#if zig_has_builtin(bswap128)
17721754 full_res = __builtin_bswap128(val);
17731755#else
1774 full_res = zig_as_u128(zig_byte_swap_u64(zig_lo_u128(val), zig_as_u8(64)),
1775 zig_byte_swap_u64(zig_hi_u128(val), zig_as_u8(64)));
1756 full_res = zig_make_u128(zig_byte_swap_u64(zig_lo_u128(val), UINT8_C(64)),
1757 zig_byte_swap_u64(zig_hi_u128(val), UINT8_C(64)));
17761758#endif
1777 return zig_shr_u128(full_res, zig_as_u8(128) - bits);
1759 return zig_shr_u128(full_res, UINT8_C(128) - bits);
17781760}
17791761
1780static inline zig_i128 zig_byte_swap_i128(zig_i128 val, zig_u8 bits) {
1762static inline zig_i128 zig_byte_swap_i128(zig_i128 val, uint8_t bits) {
17811763 return zig_bitcast_i128(zig_byte_swap_u128(zig_bitcast_u128(val), bits));
17821764}
17831765
1784static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, zig_u8 bits) {
1785 return zig_shr_u128(zig_as_u128(zig_bit_reverse_u64(zig_lo_u128(val), zig_as_u8(64)),
1786 zig_bit_reverse_u64(zig_hi_u128(val), zig_as_u8(64))),
1787 zig_as_u8(128) - bits);
1766static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, uint8_t bits) {
1767 return zig_shr_u128(zig_make_u128(zig_bit_reverse_u64(zig_lo_u128(val), UINT8_C(64)),
1768 zig_bit_reverse_u64(zig_hi_u128(val), UINT8_C(64))),
1769 UINT8_C(128) - bits);
17881770}
17891771
1790static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) {
1772static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, uint8_t bits) {
17911773 return zig_bitcast_i128(zig_bit_reverse_u128(zig_bitcast_u128(val), bits));
17921774}
17931775
1776/* ========================== Big Integer Support =========================== */
1777
1778static inline uint16_t zig_int_bytes(uint16_t bits) {
1779 uint16_t bytes = (bits + CHAR_BIT - 1) / CHAR_BIT;
1780 uint16_t alignment = ZIG_TARGET_MAX_INT_ALIGNMENT;
1781 while (alignment / 2 >= bytes) alignment /= 2;
1782 return (bytes + alignment - 1) / alignment * alignment;
1783}
1784
1785static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_signed, uint16_t bits) {
1786 const uint8_t *lhs_bytes = lhs;
1787 const uint8_t *rhs_bytes = rhs;
1788 uint16_t byte_offset = 0;
1789 bool do_signed = is_signed;
1790 uint16_t remaining_bytes = zig_int_bytes(bits);
1791
1792#if zig_little_endian
1793 byte_offset = remaining_bytes;
1794#endif
1795
1796 while (remaining_bytes >= 128 / CHAR_BIT) {
1797 int32_t limb_cmp;
1798
1799#if zig_little_endian
1800 byte_offset -= 128 / CHAR_BIT;
1801#endif
1802
1803 if (do_signed) {
1804 zig_i128 lhs_limb;
1805 zig_i128 rhs_limb;
1806
1807 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1808 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1809 limb_cmp = zig_cmp_i128(lhs_limb, rhs_limb);
1810 do_signed = false;
1811 } else {
1812 zig_u128 lhs_limb;
1813 zig_u128 rhs_limb;
1814
1815 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1816 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1817 limb_cmp = zig_cmp_u128(lhs_limb, rhs_limb);
1818 }
1819
1820 if (limb_cmp != 0) return limb_cmp;
1821 remaining_bytes -= 128 / CHAR_BIT;
1822
1823#if zig_big_endian
1824 byte_offset += 128 / CHAR_BIT;
1825#endif
1826 }
1827
1828 while (remaining_bytes >= 64 / CHAR_BIT) {
1829#if zig_little_endian
1830 byte_offset -= 64 / CHAR_BIT;
1831#endif
1832
1833 if (do_signed) {
1834 int64_t lhs_limb;
1835 int64_t rhs_limb;
1836
1837 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1838 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1839 if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb);
1840 do_signed = false;
1841 } else {
1842 uint64_t lhs_limb;
1843 uint64_t rhs_limb;
1844
1845 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1846 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1847 if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb);
1848 }
1849
1850 remaining_bytes -= 64 / CHAR_BIT;
1851
1852#if zig_big_endian
1853 byte_offset += 64 / CHAR_BIT;
1854#endif
1855 }
1856
1857 while (remaining_bytes >= 32 / CHAR_BIT) {
1858#if zig_little_endian
1859 byte_offset -= 32 / CHAR_BIT;
1860#endif
1861
1862 if (do_signed) {
1863 int32_t lhs_limb;
1864 int32_t rhs_limb;
1865
1866 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1867 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1868 if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb);
1869 do_signed = false;
1870 } else {
1871 uint32_t lhs_limb;
1872 uint32_t rhs_limb;
1873
1874 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1875 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1876 if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb);
1877 }
1878
1879 remaining_bytes -= 32 / CHAR_BIT;
1880
1881#if zig_big_endian
1882 byte_offset += 32 / CHAR_BIT;
1883#endif
1884 }
1885
1886 while (remaining_bytes >= 16 / CHAR_BIT) {
1887#if zig_little_endian
1888 byte_offset -= 16 / CHAR_BIT;
1889#endif
1890
1891 if (do_signed) {
1892 int16_t lhs_limb;
1893 int16_t rhs_limb;
1894
1895 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1896 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1897 if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb);
1898 do_signed = false;
1899 } else {
1900 uint16_t lhs_limb;
1901 uint16_t rhs_limb;
1902
1903 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1904 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1905 if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb);
1906 }
1907
1908 remaining_bytes -= 16 / CHAR_BIT;
1909
1910#if zig_big_endian
1911 byte_offset += 16 / CHAR_BIT;
1912#endif
1913 }
1914
1915 while (remaining_bytes >= 8 / CHAR_BIT) {
1916#if zig_little_endian
1917 byte_offset -= 8 / CHAR_BIT;
1918#endif
1919
1920 if (do_signed) {
1921 int8_t lhs_limb;
1922 int8_t rhs_limb;
1923
1924 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1925 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1926 if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb);
1927 do_signed = false;
1928 } else {
1929 uint8_t lhs_limb;
1930 uint8_t rhs_limb;
1931
1932 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1933 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1934 if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb);
1935 }
1936
1937 remaining_bytes -= 8 / CHAR_BIT;
1938
1939#if zig_big_endian
1940 byte_offset += 8 / CHAR_BIT;
1941#endif
1942 }
1943
1944 return 0;
1945}
1946
1947static inline bool zig_addo_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) {
1948 uint8_t *res_bytes = res;
1949 const uint8_t *lhs_bytes = lhs;
1950 const uint8_t *rhs_bytes = rhs;
1951 uint16_t byte_offset = 0;
1952 uint16_t remaining_bytes = zig_int_bytes(bits);
1953 uint16_t top_bits = remaining_bytes * 8 - bits;
1954 bool overflow = false;
1955
1956#if zig_big_endian
1957 byte_offset = remaining_bytes;
1958#endif
1959
1960 while (remaining_bytes >= 128 / CHAR_BIT) {
1961 uint16_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0);
1962
1963#if zig_big_endian
1964 byte_offset -= 128 / CHAR_BIT;
1965#endif
1966
1967 if (remaining_bytes == 128 / CHAR_BIT && is_signed) {
1968 zig_i128 res_limb;
1969 zig_i128 tmp_limb;
1970 zig_i128 lhs_limb;
1971 zig_i128 rhs_limb;
1972 bool limb_overflow;
1973
1974 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1975 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1976 limb_overflow = zig_addo_i128(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
1977 overflow = limb_overflow ^ zig_addo_i128(&res_limb, tmp_limb, zig_make_i128(INT64_C(0), overflow ? UINT64_C(1) : UINT64_C(0)), limb_bits);
1978 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
1979 } else {
1980 zig_u128 res_limb;
1981 zig_u128 tmp_limb;
1982 zig_u128 lhs_limb;
1983 zig_u128 rhs_limb;
1984 bool limb_overflow;
1985
1986 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
1987 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
1988 limb_overflow = zig_addo_u128(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
1989 overflow = limb_overflow ^ zig_addo_u128(&res_limb, tmp_limb, zig_make_u128(UINT64_C(0), overflow ? UINT64_C(1) : UINT64_C(0)), limb_bits);
1990 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
1991 }
1992
1993 remaining_bytes -= 128 / CHAR_BIT;
1994
1995#if zig_little_endian
1996 byte_offset += 128 / CHAR_BIT;
1997#endif
1998 }
1999
2000 while (remaining_bytes >= 64 / CHAR_BIT) {
2001 uint16_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0);
2002
2003#if zig_big_endian
2004 byte_offset -= 64 / CHAR_BIT;
2005#endif
2006
2007 if (remaining_bytes == 64 / CHAR_BIT && is_signed) {
2008 int64_t res_limb;
2009 int64_t tmp_limb;
2010 int64_t lhs_limb;
2011 int64_t rhs_limb;
2012 bool limb_overflow;
2013
2014 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2015 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2016 limb_overflow = zig_addo_i64(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2017 overflow = limb_overflow ^ zig_addo_i64(&res_limb, tmp_limb, overflow ? INT64_C(1) : INT64_C(0), limb_bits);
2018 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2019 } else {
2020 uint64_t res_limb;
2021 uint64_t tmp_limb;
2022 uint64_t lhs_limb;
2023 uint64_t rhs_limb;
2024 bool limb_overflow;
2025
2026 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2027 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2028 limb_overflow = zig_addo_u64(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2029 overflow = limb_overflow ^ zig_addo_u64(&res_limb, tmp_limb, overflow ? UINT64_C(1) : UINT64_C(0), limb_bits);
2030 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2031 }
2032
2033 remaining_bytes -= 64 / CHAR_BIT;
2034
2035#if zig_little_endian
2036 byte_offset += 64 / CHAR_BIT;
2037#endif
2038 }
2039
2040 while (remaining_bytes >= 32 / CHAR_BIT) {
2041 uint16_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0);
2042
2043#if zig_big_endian
2044 byte_offset -= 32 / CHAR_BIT;
2045#endif
2046
2047 if (remaining_bytes == 32 / CHAR_BIT && is_signed) {
2048 int32_t res_limb;
2049 int32_t tmp_limb;
2050 int32_t lhs_limb;
2051 int32_t rhs_limb;
2052 bool limb_overflow;
2053
2054 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2055 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2056 limb_overflow = zig_addo_i32(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2057 overflow = limb_overflow ^ zig_addo_i32(&res_limb, tmp_limb, overflow ? INT32_C(1) : INT32_C(0), limb_bits);
2058 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2059 } else {
2060 uint32_t res_limb;
2061 uint32_t tmp_limb;
2062 uint32_t lhs_limb;
2063 uint32_t rhs_limb;
2064 bool limb_overflow;
2065
2066 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2067 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2068 limb_overflow = zig_addo_u32(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2069 overflow = limb_overflow ^ zig_addo_u32(&res_limb, tmp_limb, overflow ? UINT32_C(1) : UINT32_C(0), limb_bits);
2070 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2071 }
2072
2073 remaining_bytes -= 32 / CHAR_BIT;
2074
2075#if zig_little_endian
2076 byte_offset += 32 / CHAR_BIT;
2077#endif
2078 }
2079
2080 while (remaining_bytes >= 16 / CHAR_BIT) {
2081 uint16_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0);
2082
2083#if zig_big_endian
2084 byte_offset -= 16 / CHAR_BIT;
2085#endif
2086
2087 if (remaining_bytes == 16 / CHAR_BIT && is_signed) {
2088 int16_t res_limb;
2089 int16_t tmp_limb;
2090 int16_t lhs_limb;
2091 int16_t rhs_limb;
2092 bool limb_overflow;
2093
2094 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2095 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2096 limb_overflow = zig_addo_i16(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2097 overflow = limb_overflow ^ zig_addo_i16(&res_limb, tmp_limb, overflow ? INT16_C(1) : INT16_C(0), limb_bits);
2098 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2099 } else {
2100 uint16_t res_limb;
2101 uint16_t tmp_limb;
2102 uint16_t lhs_limb;
2103 uint16_t rhs_limb;
2104 bool limb_overflow;
2105
2106 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2107 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2108 limb_overflow = zig_addo_u16(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2109 overflow = limb_overflow ^ zig_addo_u16(&res_limb, tmp_limb, overflow ? UINT16_C(1) : UINT16_C(0), limb_bits);
2110 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2111 }
2112
2113 remaining_bytes -= 16 / CHAR_BIT;
2114
2115#if zig_little_endian
2116 byte_offset += 16 / CHAR_BIT;
2117#endif
2118 }
2119
2120 while (remaining_bytes >= 8 / CHAR_BIT) {
2121 uint16_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0);
2122
2123#if zig_big_endian
2124 byte_offset -= 8 / CHAR_BIT;
2125#endif
2126
2127 if (remaining_bytes == 8 / CHAR_BIT && is_signed) {
2128 int8_t res_limb;
2129 int8_t tmp_limb;
2130 int8_t lhs_limb;
2131 int8_t rhs_limb;
2132 bool limb_overflow;
2133
2134 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2135 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2136 limb_overflow = zig_addo_i8(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2137 overflow = limb_overflow ^ zig_addo_i8(&res_limb, tmp_limb, overflow ? INT8_C(1) : INT8_C(0), limb_bits);
2138 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2139 } else {
2140 uint8_t res_limb;
2141 uint8_t tmp_limb;
2142 uint8_t lhs_limb;
2143 uint8_t rhs_limb;
2144 bool limb_overflow;
2145
2146 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2147 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2148 limb_overflow = zig_addo_u8(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2149 overflow = limb_overflow ^ zig_addo_u8(&res_limb, tmp_limb, overflow ? UINT8_C(1) : UINT8_C(0), limb_bits);
2150 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2151 }
2152
2153 remaining_bytes -= 8 / CHAR_BIT;
2154
2155#if zig_little_endian
2156 byte_offset += 8 / CHAR_BIT;
2157#endif
2158 }
2159
2160 return overflow;
2161}
2162
2163static inline bool zig_subo_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) {
2164 uint8_t *res_bytes = res;
2165 const uint8_t *lhs_bytes = lhs;
2166 const uint8_t *rhs_bytes = rhs;
2167 uint16_t byte_offset = 0;
2168 uint16_t remaining_bytes = zig_int_bytes(bits);
2169 uint16_t top_bits = remaining_bytes * 8 - bits;
2170 bool overflow = false;
2171
2172#if zig_big_endian
2173 byte_offset = remaining_bytes;
2174#endif
2175
2176 while (remaining_bytes >= 128 / CHAR_BIT) {
2177 uint16_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0);
2178
2179#if zig_big_endian
2180 byte_offset -= 128 / CHAR_BIT;
2181#endif
2182
2183 if (remaining_bytes == 128 / CHAR_BIT && is_signed) {
2184 zig_i128 res_limb;
2185 zig_i128 tmp_limb;
2186 zig_i128 lhs_limb;
2187 zig_i128 rhs_limb;
2188 bool limb_overflow;
2189
2190 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2191 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2192 limb_overflow = zig_subo_i128(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2193 overflow = limb_overflow ^ zig_subo_i128(&res_limb, tmp_limb, zig_make_i128(INT64_C(0), overflow ? UINT64_C(1) : UINT64_C(0)), limb_bits);
2194 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2195 } else {
2196 zig_u128 res_limb;
2197 zig_u128 tmp_limb;
2198 zig_u128 lhs_limb;
2199 zig_u128 rhs_limb;
2200 bool limb_overflow;
2201
2202 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2203 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2204 limb_overflow = zig_subo_u128(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2205 overflow = limb_overflow ^ zig_subo_u128(&res_limb, tmp_limb, zig_make_u128(UINT64_C(0), overflow ? UINT64_C(1) : UINT64_C(0)), limb_bits);
2206 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2207 }
2208
2209 remaining_bytes -= 128 / CHAR_BIT;
2210
2211#if zig_little_endian
2212 byte_offset += 128 / CHAR_BIT;
2213#endif
2214 }
2215
2216 while (remaining_bytes >= 64 / CHAR_BIT) {
2217 uint16_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0);
2218
2219#if zig_big_endian
2220 byte_offset -= 64 / CHAR_BIT;
2221#endif
2222
2223 if (remaining_bytes == 64 / CHAR_BIT && is_signed) {
2224 int64_t res_limb;
2225 int64_t tmp_limb;
2226 int64_t lhs_limb;
2227 int64_t rhs_limb;
2228 bool limb_overflow;
2229
2230 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2231 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2232 limb_overflow = zig_subo_i64(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2233 overflow = limb_overflow ^ zig_subo_i64(&res_limb, tmp_limb, overflow ? INT64_C(1) : INT64_C(0), limb_bits);
2234 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2235 } else {
2236 uint64_t res_limb;
2237 uint64_t tmp_limb;
2238 uint64_t lhs_limb;
2239 uint64_t rhs_limb;
2240 bool limb_overflow;
2241
2242 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2243 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2244 limb_overflow = zig_subo_u64(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2245 overflow = limb_overflow ^ zig_subo_u64(&res_limb, tmp_limb, overflow ? UINT64_C(1) : UINT64_C(0), limb_bits);
2246 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2247 }
2248
2249 remaining_bytes -= 64 / CHAR_BIT;
2250
2251#if zig_little_endian
2252 byte_offset += 64 / CHAR_BIT;
2253#endif
2254 }
2255
2256 while (remaining_bytes >= 32 / CHAR_BIT) {
2257 uint16_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0);
2258
2259#if zig_big_endian
2260 byte_offset -= 32 / CHAR_BIT;
2261#endif
2262
2263 if (remaining_bytes == 32 / CHAR_BIT && is_signed) {
2264 int32_t res_limb;
2265 int32_t tmp_limb;
2266 int32_t lhs_limb;
2267 int32_t rhs_limb;
2268 bool limb_overflow;
2269
2270 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2271 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2272 limb_overflow = zig_subo_i32(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2273 overflow = limb_overflow ^ zig_subo_i32(&res_limb, tmp_limb, overflow ? INT32_C(1) : INT32_C(0), limb_bits);
2274 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2275 } else {
2276 uint32_t res_limb;
2277 uint32_t tmp_limb;
2278 uint32_t lhs_limb;
2279 uint32_t rhs_limb;
2280 bool limb_overflow;
2281
2282 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2283 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2284 limb_overflow = zig_subo_u32(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2285 overflow = limb_overflow ^ zig_subo_u32(&res_limb, tmp_limb, overflow ? UINT32_C(1) : UINT32_C(0), limb_bits);
2286 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2287 }
2288
2289 remaining_bytes -= 32 / CHAR_BIT;
2290
2291#if zig_little_endian
2292 byte_offset += 32 / CHAR_BIT;
2293#endif
2294 }
2295
2296 while (remaining_bytes >= 16 / CHAR_BIT) {
2297 uint16_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0);
2298
2299#if zig_big_endian
2300 byte_offset -= 16 / CHAR_BIT;
2301#endif
2302
2303 if (remaining_bytes == 16 / CHAR_BIT && is_signed) {
2304 int16_t res_limb;
2305 int16_t tmp_limb;
2306 int16_t lhs_limb;
2307 int16_t rhs_limb;
2308 bool limb_overflow;
2309
2310 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2311 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2312 limb_overflow = zig_subo_i16(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2313 overflow = limb_overflow ^ zig_subo_i16(&res_limb, tmp_limb, overflow ? INT16_C(1) : INT16_C(0), limb_bits);
2314 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2315 } else {
2316 uint16_t res_limb;
2317 uint16_t tmp_limb;
2318 uint16_t lhs_limb;
2319 uint16_t rhs_limb;
2320 bool limb_overflow;
2321
2322 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2323 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2324 limb_overflow = zig_subo_u16(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2325 overflow = limb_overflow ^ zig_subo_u16(&res_limb, tmp_limb, overflow ? UINT16_C(1) : UINT16_C(0), limb_bits);
2326 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2327 }
2328
2329 remaining_bytes -= 16 / CHAR_BIT;
2330
2331#if zig_little_endian
2332 byte_offset += 16 / CHAR_BIT;
2333#endif
2334 }
2335
2336 while (remaining_bytes >= 8 / CHAR_BIT) {
2337 uint16_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0);
2338
2339#if zig_big_endian
2340 byte_offset -= 8 / CHAR_BIT;
2341#endif
2342
2343 if (remaining_bytes == 8 / CHAR_BIT && is_signed) {
2344 int8_t res_limb;
2345 int8_t tmp_limb;
2346 int8_t lhs_limb;
2347 int8_t rhs_limb;
2348 bool limb_overflow;
2349
2350 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2351 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2352 limb_overflow = zig_subo_i8(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2353 overflow = limb_overflow ^ zig_subo_i8(&res_limb, tmp_limb, overflow ? INT8_C(1) : INT8_C(0), limb_bits);
2354 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2355 } else {
2356 uint8_t res_limb;
2357 uint8_t tmp_limb;
2358 uint8_t lhs_limb;
2359 uint8_t rhs_limb;
2360 bool limb_overflow;
2361
2362 memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb));
2363 memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb));
2364 limb_overflow = zig_subo_u8(&tmp_limb, lhs_limb, rhs_limb, limb_bits);
2365 overflow = limb_overflow ^ zig_subo_u8(&res_limb, tmp_limb, overflow ? UINT8_C(1) : UINT8_C(0), limb_bits);
2366 memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb));
2367 }
2368
2369 remaining_bytes -= 8 / CHAR_BIT;
2370
2371#if zig_little_endian
2372 byte_offset += 8 / CHAR_BIT;
2373#endif
2374 }
2375
2376 return overflow;
2377}
2378
2379static inline void zig_addw_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) {
2380 (void)zig_addo_big(res, lhs, rhs, is_signed, bits);
2381}
2382
2383static inline void zig_subw_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) {
2384 (void)zig_subo_big(res, lhs, rhs, is_signed, bits);
2385}
2386
2387static inline uint16_t zig_clz_big(const void *val, bool is_signed, uint16_t bits) {
2388 const uint8_t *val_bytes = val;
2389 uint16_t byte_offset = 0;
2390 uint16_t remaining_bytes = zig_int_bytes(bits);
2391 uint16_t skip_bits = remaining_bytes * 8 - bits;
2392 uint16_t total_lz = 0;
2393 uint16_t limb_lz;
2394 (void)is_signed;
2395
2396#if zig_little_endian
2397 byte_offset = remaining_bytes;
2398#endif
2399
2400 while (remaining_bytes >= 128 / CHAR_BIT) {
2401#if zig_little_endian
2402 byte_offset -= 128 / CHAR_BIT;
2403#endif
2404
2405 {
2406 zig_u128 val_limb;
2407
2408 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2409 limb_lz = zig_clz_u128(val_limb, 128 - skip_bits);
2410 }
2411
2412 total_lz += limb_lz;
2413 if (limb_lz < 128 - skip_bits) return total_lz;
2414 skip_bits = 0;
2415 remaining_bytes -= 128 / CHAR_BIT;
2416
2417#if zig_big_endian
2418 byte_offset += 128 / CHAR_BIT;
2419#endif
2420 }
2421
2422 while (remaining_bytes >= 64 / CHAR_BIT) {
2423#if zig_little_endian
2424 byte_offset -= 64 / CHAR_BIT;
2425#endif
2426
2427 {
2428 uint64_t val_limb;
2429
2430 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2431 limb_lz = zig_clz_u64(val_limb, 64 - skip_bits);
2432 }
2433
2434 total_lz += limb_lz;
2435 if (limb_lz < 64 - skip_bits) return total_lz;
2436 skip_bits = 0;
2437 remaining_bytes -= 64 / CHAR_BIT;
2438
2439#if zig_big_endian
2440 byte_offset += 64 / CHAR_BIT;
2441#endif
2442 }
2443
2444 while (remaining_bytes >= 32 / CHAR_BIT) {
2445#if zig_little_endian
2446 byte_offset -= 32 / CHAR_BIT;
2447#endif
2448
2449 {
2450 uint32_t val_limb;
2451
2452 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2453 limb_lz = zig_clz_u32(val_limb, 32 - skip_bits);
2454 }
2455
2456 total_lz += limb_lz;
2457 if (limb_lz < 32 - skip_bits) return total_lz;
2458 skip_bits = 0;
2459 remaining_bytes -= 32 / CHAR_BIT;
2460
2461#if zig_big_endian
2462 byte_offset += 32 / CHAR_BIT;
2463#endif
2464 }
2465
2466 while (remaining_bytes >= 16 / CHAR_BIT) {
2467#if zig_little_endian
2468 byte_offset -= 16 / CHAR_BIT;
2469#endif
2470
2471 {
2472 uint16_t val_limb;
2473
2474 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2475 limb_lz = zig_clz_u16(val_limb, 16 - skip_bits);
2476 }
2477
2478 total_lz += limb_lz;
2479 if (limb_lz < 16 - skip_bits) return total_lz;
2480 skip_bits = 0;
2481 remaining_bytes -= 16 / CHAR_BIT;
2482
2483#if zig_big_endian
2484 byte_offset += 16 / CHAR_BIT;
2485#endif
2486 }
2487
2488 while (remaining_bytes >= 8 / CHAR_BIT) {
2489#if zig_little_endian
2490 byte_offset -= 8 / CHAR_BIT;
2491#endif
2492
2493 {
2494 uint8_t val_limb;
2495
2496 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2497 limb_lz = zig_clz_u8(val_limb, 8 - skip_bits);
2498 }
2499
2500 total_lz += limb_lz;
2501 if (limb_lz < 8 - skip_bits) return total_lz;
2502 skip_bits = 0;
2503 remaining_bytes -= 8 / CHAR_BIT;
2504
2505#if zig_big_endian
2506 byte_offset += 8 / CHAR_BIT;
2507#endif
2508 }
2509
2510 return total_lz;
2511}
2512
2513static inline uint16_t zig_ctz_big(const void *val, bool is_signed, uint16_t bits) {
2514 const uint8_t *val_bytes = val;
2515 uint16_t byte_offset = 0;
2516 uint16_t remaining_bytes = zig_int_bytes(bits);
2517 uint16_t total_tz = 0;
2518 uint16_t limb_tz;
2519 (void)is_signed;
2520
2521#if zig_big_endian
2522 byte_offset = remaining_bytes;
2523#endif
2524
2525 while (remaining_bytes >= 128 / CHAR_BIT) {
2526#if zig_big_endian
2527 byte_offset -= 128 / CHAR_BIT;
2528#endif
2529
2530 {
2531 zig_u128 val_limb;
2532
2533 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2534 limb_tz = zig_ctz_u128(val_limb, 128);
2535 }
2536
2537 total_tz += limb_tz;
2538 if (limb_tz < 128) return total_tz;
2539 remaining_bytes -= 128 / CHAR_BIT;
2540
2541#if zig_little_endian
2542 byte_offset += 128 / CHAR_BIT;
2543#endif
2544 }
2545
2546 while (remaining_bytes >= 64 / CHAR_BIT) {
2547#if zig_big_endian
2548 byte_offset -= 64 / CHAR_BIT;
2549#endif
2550
2551 {
2552 uint64_t val_limb;
2553
2554 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2555 limb_tz = zig_ctz_u64(val_limb, 64);
2556 }
2557
2558 total_tz += limb_tz;
2559 if (limb_tz < 64) return total_tz;
2560 remaining_bytes -= 64 / CHAR_BIT;
2561
2562#if zig_little_endian
2563 byte_offset += 64 / CHAR_BIT;
2564#endif
2565 }
2566
2567 while (remaining_bytes >= 32 / CHAR_BIT) {
2568#if zig_big_endian
2569 byte_offset -= 32 / CHAR_BIT;
2570#endif
2571
2572 {
2573 uint32_t val_limb;
2574
2575 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2576 limb_tz = zig_ctz_u32(val_limb, 32);
2577 }
2578
2579 total_tz += limb_tz;
2580 if (limb_tz < 32) return total_tz;
2581 remaining_bytes -= 32 / CHAR_BIT;
2582
2583#if zig_little_endian
2584 byte_offset += 32 / CHAR_BIT;
2585#endif
2586 }
2587
2588 while (remaining_bytes >= 16 / CHAR_BIT) {
2589#if zig_big_endian
2590 byte_offset -= 16 / CHAR_BIT;
2591#endif
2592
2593 {
2594 uint16_t val_limb;
2595
2596 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2597 limb_tz = zig_ctz_u16(val_limb, 16);
2598 }
2599
2600 total_tz += limb_tz;
2601 if (limb_tz < 16) return total_tz;
2602 remaining_bytes -= 16 / CHAR_BIT;
2603
2604#if zig_little_endian
2605 byte_offset += 16 / CHAR_BIT;
2606#endif
2607 }
2608
2609 while (remaining_bytes >= 8 / CHAR_BIT) {
2610#if zig_big_endian
2611 byte_offset -= 8 / CHAR_BIT;
2612#endif
2613
2614 {
2615 uint8_t val_limb;
2616
2617 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2618 limb_tz = zig_ctz_u8(val_limb, 8);
2619 }
2620
2621 total_tz += limb_tz;
2622 if (limb_tz < 8) return total_tz;
2623 remaining_bytes -= 8 / CHAR_BIT;
2624
2625#if zig_little_endian
2626 byte_offset += 8 / CHAR_BIT;
2627#endif
2628 }
2629
2630 return total_tz;
2631}
2632
2633static inline uint16_t zig_popcount_big(const void *val, bool is_signed, uint16_t bits) {
2634 const uint8_t *val_bytes = val;
2635 uint16_t byte_offset = 0;
2636 uint16_t remaining_bytes = zig_int_bytes(bits);
2637 uint16_t total_pc = 0;
2638 (void)is_signed;
2639
2640#if zig_big_endian
2641 byte_offset = remaining_bytes;
2642#endif
2643
2644 while (remaining_bytes >= 128 / CHAR_BIT) {
2645#if zig_big_endian
2646 byte_offset -= 128 / CHAR_BIT;
2647#endif
2648
2649 {
2650 zig_u128 val_limb;
2651
2652 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2653 total_pc += zig_popcount_u128(val_limb, 128);
2654 }
2655
2656 remaining_bytes -= 128 / CHAR_BIT;
2657
2658#if zig_little_endian
2659 byte_offset += 128 / CHAR_BIT;
2660#endif
2661 }
2662
2663 while (remaining_bytes >= 64 / CHAR_BIT) {
2664#if zig_big_endian
2665 byte_offset -= 64 / CHAR_BIT;
2666#endif
2667
2668 {
2669 uint64_t val_limb;
2670
2671 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2672 total_pc += zig_popcount_u64(val_limb, 64);
2673 }
2674
2675 remaining_bytes -= 64 / CHAR_BIT;
2676
2677#if zig_little_endian
2678 byte_offset += 64 / CHAR_BIT;
2679#endif
2680 }
2681
2682 while (remaining_bytes >= 32 / CHAR_BIT) {
2683#if zig_big_endian
2684 byte_offset -= 32 / CHAR_BIT;
2685#endif
2686
2687 {
2688 uint32_t val_limb;
2689
2690 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2691 total_pc += zig_popcount_u32(val_limb, 32);
2692 }
2693
2694 remaining_bytes -= 32 / CHAR_BIT;
2695
2696#if zig_little_endian
2697 byte_offset += 32 / CHAR_BIT;
2698#endif
2699 }
2700
2701 while (remaining_bytes >= 16 / CHAR_BIT) {
2702#if zig_big_endian
2703 byte_offset -= 16 / CHAR_BIT;
2704#endif
2705
2706 {
2707 uint16_t val_limb;
2708
2709 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2710 total_pc = zig_popcount_u16(val_limb, 16);
2711 }
2712
2713 remaining_bytes -= 16 / CHAR_BIT;
2714
2715#if zig_little_endian
2716 byte_offset += 16 / CHAR_BIT;
2717#endif
2718 }
2719
2720 while (remaining_bytes >= 8 / CHAR_BIT) {
2721#if zig_big_endian
2722 byte_offset -= 8 / CHAR_BIT;
2723#endif
2724
2725 {
2726 uint8_t val_limb;
2727
2728 memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb));
2729 total_pc = zig_popcount_u8(val_limb, 8);
2730 }
2731
2732 remaining_bytes -= 8 / CHAR_BIT;
2733
2734#if zig_little_endian
2735 byte_offset += 8 / CHAR_BIT;
2736#endif
2737 }
2738
2739 return total_pc;
2740}
2741
17942742/* ========================= Floating Point Support ========================= */
17952743
17962744#if _MSC_VER
......@@ -1810,252 +2758,253 @@ static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) {
18102758
18112759#if (zig_has_builtin(nan) && zig_has_builtin(nans) && zig_has_builtin(inf)) || defined(zig_gnuc)
18122760#define zig_has_float_builtins 1
1813#define zig_as_special_f16(sign, name, arg, repr) sign zig_as_f16(__builtin_##name, )(arg)
1814#define zig_as_special_f32(sign, name, arg, repr) sign zig_as_f32(__builtin_##name, )(arg)
1815#define zig_as_special_f64(sign, name, arg, repr) sign zig_as_f64(__builtin_##name, )(arg)
1816#define zig_as_special_f80(sign, name, arg, repr) sign zig_as_f80(__builtin_##name, )(arg)
1817#define zig_as_special_f128(sign, name, arg, repr) sign zig_as_f128(__builtin_##name, )(arg)
1818#define zig_as_special_c_longdouble(sign, name, arg, repr) sign zig_as_c_longdouble(__builtin_##name, )(arg)
2761#define zig_make_special_f16(sign, name, arg, repr) sign zig_make_f16(__builtin_##name, )(arg)
2762#define zig_make_special_f32(sign, name, arg, repr) sign zig_make_f32(__builtin_##name, )(arg)
2763#define zig_make_special_f64(sign, name, arg, repr) sign zig_make_f64(__builtin_##name, )(arg)
2764#define zig_make_special_f80(sign, name, arg, repr) sign zig_make_f80(__builtin_##name, )(arg)
2765#define zig_make_special_f128(sign, name, arg, repr) sign zig_make_f128(__builtin_##name, )(arg)
18192766#else
18202767#define zig_has_float_builtins 0
1821#define zig_as_special_f16(sign, name, arg, repr) zig_float_from_repr_f16(repr)
1822#define zig_as_special_f32(sign, name, arg, repr) zig_float_from_repr_f32(repr)
1823#define zig_as_special_f64(sign, name, arg, repr) zig_float_from_repr_f64(repr)
1824#define zig_as_special_f80(sign, name, arg, repr) zig_float_from_repr_f80(repr)
1825#define zig_as_special_f128(sign, name, arg, repr) zig_float_from_repr_f128(repr)
1826#define zig_as_special_c_longdouble(sign, name, arg, repr) zig_float_from_repr_c_longdouble(repr)
2768#define zig_make_special_f16(sign, name, arg, repr) zig_float_from_repr_f16(repr)
2769#define zig_make_special_f32(sign, name, arg, repr) zig_float_from_repr_f32(repr)
2770#define zig_make_special_f64(sign, name, arg, repr) zig_float_from_repr_f64(repr)
2771#define zig_make_special_f80(sign, name, arg, repr) zig_float_from_repr_f80(repr)
2772#define zig_make_special_f128(sign, name, arg, repr) zig_float_from_repr_f128(repr)
18272773#endif
18282774
18292775#define zig_has_f16 1
18302776#define zig_bitSizeOf_f16 16
2777typedef int16_t zig_repr_f16;
18312778#define zig_libc_name_f16(name) __##name##h
1832#define zig_as_special_constant_f16(sign, name, arg, repr) zig_as_special_f16(sign, name, arg, repr)
2779#define zig_init_special_f16(sign, name, arg, repr) zig_make_special_f16(sign, name, arg, repr)
18332780#if FLT_MANT_DIG == 11
18342781typedef float zig_f16;
1835#define zig_as_f16(fp, repr) fp##f
2782#define zig_make_f16(fp, repr) fp##f
18362783#elif DBL_MANT_DIG == 11
18372784typedef double zig_f16;
1838#define zig_as_f16(fp, repr) fp
2785#define zig_make_f16(fp, repr) fp
18392786#elif LDBL_MANT_DIG == 11
18402787#define zig_bitSizeOf_c_longdouble 16
2788#ifndef ZIG_TARGET_ABI_MSVC
2789typedef zig_repr_f16 zig_repr_c_longdouble;
2790#endif
18412791typedef long double zig_f16;
1842#define zig_as_f16(fp, repr) fp##l
2792#define zig_make_f16(fp, repr) fp##l
18432793#elif FLT16_MANT_DIG == 11 && (zig_has_builtin(inff16) || defined(zig_gnuc))
18442794typedef _Float16 zig_f16;
1845#define zig_as_f16(fp, repr) fp##f16
2795#define zig_make_f16(fp, repr) fp##f16
18462796#elif defined(__SIZEOF_FP16__)
18472797typedef __fp16 zig_f16;
1848#define zig_as_f16(fp, repr) fp##f16
2798#define zig_make_f16(fp, repr) fp##f16
18492799#else
18502800#undef zig_has_f16
18512801#define zig_has_f16 0
1852#define zig_repr_f16 i16
1853typedef zig_i16 zig_f16;
1854#define zig_as_f16(fp, repr) repr
1855#undef zig_as_special_f16
1856#define zig_as_special_f16(sign, name, arg, repr) repr
1857#undef zig_as_special_constant_f16
1858#define zig_as_special_constant_f16(sign, name, arg, repr) repr
2802#define zig_bitSizeOf_repr_f16 16
2803typedef int16_t zig_f16;
2804#define zig_make_f16(fp, repr) repr
2805#undef zig_make_special_f16
2806#define zig_make_special_f16(sign, name, arg, repr) repr
2807#undef zig_init_special_f16
2808#define zig_init_special_f16(sign, name, arg, repr) repr
18592809#endif
18602810
18612811#define zig_has_f32 1
18622812#define zig_bitSizeOf_f32 32
2813typedef int32_t zig_repr_f32;
18632814#define zig_libc_name_f32(name) name##f
18642815#if _MSC_VER
1865#define zig_as_special_constant_f32(sign, name, arg, repr) sign zig_as_f32(zig_msvc_flt_##name, )
2816#define zig_init_special_f32(sign, name, arg, repr) sign zig_make_f32(zig_msvc_flt_##name, )
18662817#else
1867#define zig_as_special_constant_f32(sign, name, arg, repr) zig_as_special_f32(sign, name, arg, repr)
2818#define zig_init_special_f32(sign, name, arg, repr) zig_make_special_f32(sign, name, arg, repr)
18682819#endif
18692820#if FLT_MANT_DIG == 24
18702821typedef float zig_f32;
1871#define zig_as_f32(fp, repr) fp##f
2822#define zig_make_f32(fp, repr) fp##f
18722823#elif DBL_MANT_DIG == 24
18732824typedef double zig_f32;
1874#define zig_as_f32(fp, repr) fp
2825#define zig_make_f32(fp, repr) fp
18752826#elif LDBL_MANT_DIG == 24
18762827#define zig_bitSizeOf_c_longdouble 32
2828#ifndef ZIG_TARGET_ABI_MSVC
2829typedef zig_repr_f32 zig_repr_c_longdouble;
2830#endif
18772831typedef long double zig_f32;
1878#define zig_as_f32(fp, repr) fp##l
2832#define zig_make_f32(fp, repr) fp##l
18792833#elif FLT32_MANT_DIG == 24
18802834typedef _Float32 zig_f32;
1881#define zig_as_f32(fp, repr) fp##f32
2835#define zig_make_f32(fp, repr) fp##f32
18822836#else
18832837#undef zig_has_f32
18842838#define zig_has_f32 0
1885#define zig_repr_f32 i32
1886typedef zig_i32 zig_f32;
1887#define zig_as_f32(fp, repr) repr
1888#undef zig_as_special_f32
1889#define zig_as_special_f32(sign, name, arg, repr) repr
1890#undef zig_as_special_constant_f32
1891#define zig_as_special_constant_f32(sign, name, arg, repr) repr
2839#define zig_bitSizeOf_repr_f32 32
2840typedef int32_t zig_f32;
2841#define zig_make_f32(fp, repr) repr
2842#undef zig_make_special_f32
2843#define zig_make_special_f32(sign, name, arg, repr) repr
2844#undef zig_init_special_f32
2845#define zig_init_special_f32(sign, name, arg, repr) repr
18922846#endif
18932847
18942848#define zig_has_f64 1
18952849#define zig_bitSizeOf_f64 64
2850typedef int64_t zig_repr_f64;
18962851#define zig_libc_name_f64(name) name
18972852#if _MSC_VER
18982853#ifdef ZIG_TARGET_ABI_MSVC
18992854#define zig_bitSizeOf_c_longdouble 64
2855#ifndef ZIG_TARGET_ABI_MSVC
2856typedef zig_repr_f64 zig_repr_c_longdouble;
19002857#endif
1901#define zig_as_special_constant_f64(sign, name, arg, repr) sign zig_as_f64(zig_msvc_flt_##name, )
2858#endif
2859#define zig_init_special_f64(sign, name, arg, repr) sign zig_make_f64(zig_msvc_flt_##name, )
19022860#else /* _MSC_VER */
1903#define zig_as_special_constant_f64(sign, name, arg, repr) zig_as_special_f64(sign, name, arg, repr)
2861#define zig_init_special_f64(sign, name, arg, repr) zig_make_special_f64(sign, name, arg, repr)
19042862#endif /* _MSC_VER */
19052863#if FLT_MANT_DIG == 53
19062864typedef float zig_f64;
1907#define zig_as_f64(fp, repr) fp##f
2865#define zig_make_f64(fp, repr) fp##f
19082866#elif DBL_MANT_DIG == 53
19092867typedef double zig_f64;
1910#define zig_as_f64(fp, repr) fp
2868#define zig_make_f64(fp, repr) fp
19112869#elif LDBL_MANT_DIG == 53
19122870#define zig_bitSizeOf_c_longdouble 64
2871#ifndef ZIG_TARGET_ABI_MSVC
2872typedef zig_repr_f64 zig_repr_c_longdouble;
2873#endif
19132874typedef long double zig_f64;
1914#define zig_as_f64(fp, repr) fp##l
2875#define zig_make_f64(fp, repr) fp##l
19152876#elif FLT64_MANT_DIG == 53
19162877typedef _Float64 zig_f64;
1917#define zig_as_f64(fp, repr) fp##f64
2878#define zig_make_f64(fp, repr) fp##f64
19182879#elif FLT32X_MANT_DIG == 53
19192880typedef _Float32x zig_f64;
1920#define zig_as_f64(fp, repr) fp##f32x
2881#define zig_make_f64(fp, repr) fp##f32x
19212882#else
19222883#undef zig_has_f64
19232884#define zig_has_f64 0
1924#define zig_repr_f64 i64
1925typedef zig_i64 zig_f64;
1926#define zig_as_f64(fp, repr) repr
1927#undef zig_as_special_f64
1928#define zig_as_special_f64(sign, name, arg, repr) repr
1929#undef zig_as_special_constant_f64
1930#define zig_as_special_constant_f64(sign, name, arg, repr) repr
2885#define zig_bitSizeOf_repr_f64 64
2886typedef int64_t zig_f64;
2887#define zig_make_f64(fp, repr) repr
2888#undef zig_make_special_f64
2889#define zig_make_special_f64(sign, name, arg, repr) repr
2890#undef zig_init_special_f64
2891#define zig_init_special_f64(sign, name, arg, repr) repr
19312892#endif
19322893
19332894#define zig_has_f80 1
19342895#define zig_bitSizeOf_f80 80
2896typedef zig_i128 zig_repr_f80;
19352897#define zig_libc_name_f80(name) __##name##x
1936#define zig_as_special_constant_f80(sign, name, arg, repr) zig_as_special_f80(sign, name, arg, repr)
2898#define zig_init_special_f80(sign, name, arg, repr) zig_make_special_f80(sign, name, arg, repr)
19372899#if FLT_MANT_DIG == 64
19382900typedef float zig_f80;
1939#define zig_as_f80(fp, repr) fp##f
2901#define zig_make_f80(fp, repr) fp##f
19402902#elif DBL_MANT_DIG == 64
19412903typedef double zig_f80;
1942#define zig_as_f80(fp, repr) fp
2904#define zig_make_f80(fp, repr) fp
19432905#elif LDBL_MANT_DIG == 64
19442906#define zig_bitSizeOf_c_longdouble 80
2907#ifndef ZIG_TARGET_ABI_MSVC
2908typedef zig_repr_f80 zig_repr_c_longdouble;
2909#endif
19452910typedef long double zig_f80;
1946#define zig_as_f80(fp, repr) fp##l
2911#define zig_make_f80(fp, repr) fp##l
19472912#elif FLT80_MANT_DIG == 64
19482913typedef _Float80 zig_f80;
1949#define zig_as_f80(fp, repr) fp##f80
2914#define zig_make_f80(fp, repr) fp##f80
19502915#elif FLT64X_MANT_DIG == 64
19512916typedef _Float64x zig_f80;
1952#define zig_as_f80(fp, repr) fp##f64x
2917#define zig_make_f80(fp, repr) fp##f64x
19532918#elif defined(__SIZEOF_FLOAT80__)
19542919typedef __float80 zig_f80;
1955#define zig_as_f80(fp, repr) fp##l
2920#define zig_make_f80(fp, repr) fp##l
19562921#else
19572922#undef zig_has_f80
19582923#define zig_has_f80 0
1959#define zig_repr_f80 i128
2924#define zig_bitSizeOf_repr_f80 128
19602925typedef zig_i128 zig_f80;
1961#define zig_as_f80(fp, repr) repr
1962#undef zig_as_special_f80
1963#define zig_as_special_f80(sign, name, arg, repr) repr
1964#undef zig_as_special_constant_f80
1965#define zig_as_special_constant_f80(sign, name, arg, repr) repr
2926#define zig_make_f80(fp, repr) repr
2927#undef zig_make_special_f80
2928#define zig_make_special_f80(sign, name, arg, repr) repr
2929#undef zig_init_special_f80
2930#define zig_init_special_f80(sign, name, arg, repr) repr
19662931#endif
19672932
19682933#define zig_has_f128 1
19692934#define zig_bitSizeOf_f128 128
2935typedef zig_i128 zig_repr_f128;
19702936#define zig_libc_name_f128(name) name##q
1971#define zig_as_special_constant_f128(sign, name, arg, repr) zig_as_special_f128(sign, name, arg, repr)
2937#define zig_init_special_f128(sign, name, arg, repr) zig_make_special_f128(sign, name, arg, repr)
19722938#if FLT_MANT_DIG == 113
19732939typedef float zig_f128;
1974#define zig_as_f128(fp, repr) fp##f
2940#define zig_make_f128(fp, repr) fp##f
19752941#elif DBL_MANT_DIG == 113
19762942typedef double zig_f128;
1977#define zig_as_f128(fp, repr) fp
2943#define zig_make_f128(fp, repr) fp
19782944#elif LDBL_MANT_DIG == 113
19792945#define zig_bitSizeOf_c_longdouble 128
2946#ifndef ZIG_TARGET_ABI_MSVC
2947typedef zig_repr_f128 zig_repr_c_longdouble;
2948#endif
19802949typedef long double zig_f128;
1981#define zig_as_f128(fp, repr) fp##l
2950#define zig_make_f128(fp, repr) fp##l
19822951#elif FLT128_MANT_DIG == 113
19832952typedef _Float128 zig_f128;
1984#define zig_as_f128(fp, repr) fp##f128
2953#define zig_make_f128(fp, repr) fp##f128
19852954#elif FLT64X_MANT_DIG == 113
19862955typedef _Float64x zig_f128;
1987#define zig_as_f128(fp, repr) fp##f64x
2956#define zig_make_f128(fp, repr) fp##f64x
19882957#elif defined(__SIZEOF_FLOAT128__)
19892958typedef __float128 zig_f128;
1990#define zig_as_f128(fp, repr) fp##q
1991#undef zig_as_special_f128
1992#define zig_as_special_f128(sign, name, arg, repr) sign __builtin_##name##f128(arg)
2959#define zig_make_f128(fp, repr) fp##q
2960#undef zig_make_special_f128
2961#define zig_make_special_f128(sign, name, arg, repr) sign __builtin_##name##f128(arg)
19932962#else
19942963#undef zig_has_f128
19952964#define zig_has_f128 0
1996#define zig_repr_f128 i128
2965#define zig_bitSizeOf_repr_f128 128
19972966typedef zig_i128 zig_f128;
1998#define zig_as_f128(fp, repr) repr
1999#undef zig_as_special_f128
2000#define zig_as_special_f128(sign, name, arg, repr) repr
2001#undef zig_as_special_constant_f128
2002#define zig_as_special_constant_f128(sign, name, arg, repr) repr
2003#endif
2004
2005#define zig_has_c_longdouble 1
2006
2007#ifdef ZIG_TARGET_ABI_MSVC
2008#define zig_libc_name_c_longdouble(name) name
2009#else
2010#define zig_libc_name_c_longdouble(name) name##l
2967#define zig_make_f128(fp, repr) repr
2968#undef zig_make_special_f128
2969#define zig_make_special_f128(sign, name, arg, repr) repr
2970#undef zig_init_special_f128
2971#define zig_init_special_f128(sign, name, arg, repr) repr
20112972#endif
20122973
2013#define zig_as_special_constant_c_longdouble(sign, name, arg, repr) zig_as_special_c_longdouble(sign, name, arg, repr)
20142974#ifdef zig_bitSizeOf_c_longdouble
20152975
2976#define zig_has_c_longdouble 1
20162977#ifdef ZIG_TARGET_ABI_MSVC
2017typedef double zig_c_longdouble;
20182978#undef zig_bitSizeOf_c_longdouble
20192979#define zig_bitSizeOf_c_longdouble 64
2020#define zig_as_c_longdouble(fp, repr) fp
2980typedef zig_f64 zig_c_longdouble;
2981typedef zig_repr_f64 zig_repr_c_longdouble;
20212982#else
20222983typedef long double zig_c_longdouble;
2023#define zig_as_c_longdouble(fp, repr) fp##l
20242984#endif
20252985
20262986#else /* zig_bitSizeOf_c_longdouble */
20272987
2028#undef zig_has_c_longdouble
20292988#define zig_has_c_longdouble 0
2030#define zig_bitSizeOf_c_longdouble 80
2031#define zig_compiler_rt_abbrev_c_longdouble zig_compiler_rt_abbrev_f80
2032#define zig_repr_c_longdouble i128
2033typedef zig_i128 zig_c_longdouble;
2034#define zig_as_c_longdouble(fp, repr) repr
2035#undef zig_as_special_c_longdouble
2036#define zig_as_special_c_longdouble(sign, name, arg, repr) repr
2037#undef zig_as_special_constant_c_longdouble
2038#define zig_as_special_constant_c_longdouble(sign, name, arg, repr) repr
2989#define zig_bitSizeOf_repr_c_longdouble 128
2990typedef zig_f128 zig_c_longdouble;
2991typedef zig_repr_f128 zig_repr_c_longdouble;
20392992
20402993#endif /* zig_bitSizeOf_c_longdouble */
20412994
20422995#if !zig_has_float_builtins
2043#define zig_float_from_repr(Type, ReprType) \
2044 static inline zig_##Type zig_float_from_repr_##Type(zig_##ReprType repr) { \
2045 return *((zig_##Type*)&repr); \
2996#define zig_float_from_repr(Type) \
2997 static inline zig_##Type zig_float_from_repr_##Type(zig_repr_##Type repr) { \
2998 zig_##Type result; \
2999 memcpy(&result, &repr, sizeof(result)); \
3000 return result; \
20463001 }
20473002
2048zig_float_from_repr(f16, u16)
2049zig_float_from_repr(f32, u32)
2050zig_float_from_repr(f64, u64)
2051zig_float_from_repr(f80, u128)
2052zig_float_from_repr(f128, u128)
2053#if zig_bitSizeOf_c_longdouble == 80
2054zig_float_from_repr(c_longdouble, u128)
2055#else
2056#define zig_expand_float_from_repr(Type, ReprType) zig_float_from_repr(Type, ReprType)
2057zig_expand_float_from_repr(c_longdouble, zig_expand_concat(u, zig_bitSizeOf_c_longdouble))
2058#endif
3003zig_float_from_repr(f16)
3004zig_float_from_repr(f32)
3005zig_float_from_repr(f64)
3006zig_float_from_repr(f80)
3007zig_float_from_repr(f128)
20593008#endif
20603009
20613010#define zig_cast_f16 (zig_f16)
......@@ -2064,41 +3013,42 @@ zig_expand_float_from_repr(c_longdouble, zig_expand_concat(u, zig_bitSizeOf_c_lo
20643013
20653014#if _MSC_VER && !zig_has_f128
20663015#define zig_cast_f80
2067#define zig_cast_c_longdouble
20683016#define zig_cast_f128
20693017#else
20703018#define zig_cast_f80 (zig_f80)
2071#define zig_cast_c_longdouble (zig_c_longdouble)
20723019#define zig_cast_f128 (zig_f128)
20733020#endif
20743021
20753022#define zig_convert_builtin(ResType, operation, ArgType, version) \
2076 zig_extern zig_##ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \
2077 zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(zig_##ArgType);
2078zig_convert_builtin(f16, trunc, f32, 2)
2079zig_convert_builtin(f16, trunc, f64, 2)
2080zig_convert_builtin(f16, trunc, f80, 2)
2081zig_convert_builtin(f16, trunc, f128, 2)
2082zig_convert_builtin(f32, extend, f16, 2)
2083zig_convert_builtin(f32, trunc, f64, 2)
2084zig_convert_builtin(f32, trunc, f80, 2)
2085zig_convert_builtin(f32, trunc, f128, 2)
2086zig_convert_builtin(f64, extend, f16, 2)
2087zig_convert_builtin(f64, extend, f32, 2)
2088zig_convert_builtin(f64, trunc, f80, 2)
2089zig_convert_builtin(f64, trunc, f128, 2)
2090zig_convert_builtin(f80, extend, f16, 2)
2091zig_convert_builtin(f80, extend, f32, 2)
2092zig_convert_builtin(f80, extend, f64, 2)
2093zig_convert_builtin(f80, trunc, f128, 2)
2094zig_convert_builtin(f128, extend, f16, 2)
2095zig_convert_builtin(f128, extend, f32, 2)
2096zig_convert_builtin(f128, extend, f64, 2)
2097zig_convert_builtin(f128, extend, f80, 2)
3023 zig_extern ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \
3024 zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(ArgType);
3025zig_convert_builtin(zig_f16, trunc, zig_f32, 2)
3026zig_convert_builtin(zig_f16, trunc, zig_f64, 2)
3027zig_convert_builtin(zig_f16, trunc, zig_f80, 2)
3028zig_convert_builtin(zig_f16, trunc, zig_f128, 2)
3029zig_convert_builtin(zig_f32, extend, zig_f16, 2)
3030zig_convert_builtin(zig_f32, trunc, zig_f64, 2)
3031zig_convert_builtin(zig_f32, trunc, zig_f80, 2)
3032zig_convert_builtin(zig_f32, trunc, zig_f128, 2)
3033zig_convert_builtin(zig_f64, extend, zig_f16, 2)
3034zig_convert_builtin(zig_f64, extend, zig_f32, 2)
3035zig_convert_builtin(zig_f64, trunc, zig_f80, 2)
3036zig_convert_builtin(zig_f64, trunc, zig_f128, 2)
3037zig_convert_builtin(zig_f80, extend, zig_f16, 2)
3038zig_convert_builtin(zig_f80, extend, zig_f32, 2)
3039zig_convert_builtin(zig_f80, extend, zig_f64, 2)
3040zig_convert_builtin(zig_f80, trunc, zig_f128, 2)
3041zig_convert_builtin(zig_f128, extend, zig_f16, 2)
3042zig_convert_builtin(zig_f128, extend, zig_f32, 2)
3043zig_convert_builtin(zig_f128, extend, zig_f64, 2)
3044zig_convert_builtin(zig_f128, extend, zig_f80, 2)
20983045
20993046#define zig_float_negate_builtin_0(Type) \
21003047 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
2101 return zig_expand_concat(zig_xor_, zig_repr_##Type)(arg, zig_expand_minInt(zig_repr_##Type, zig_bitSizeOf_##Type)); \
3048 return zig_expand_concat(zig_xor_i, zig_bitSizeOf_repr_##Type)( \
3049 arg, \
3050 zig_minInt_i(zig_bitSizeOf_repr_##Type, zig_bitSizeOf_##Type) \
3051 ); \
21023052 }
21033053#define zig_float_negate_builtin_1(Type) \
21043054 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
......@@ -2106,28 +3056,28 @@ zig_convert_builtin(f128, extend, f80, 2)
21063056 }
21073057
21083058#define zig_float_less_builtin_0(Type, operation) \
2109 zig_extern zig_i32 zig_expand_concat(zig_expand_concat(__##operation, \
2110 zig_compiler_rt_abbrev_##Type), 2)(zig_##Type, zig_##Type); \
2111 static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
2112 return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_##Type), 2)(lhs, rhs); \
3059 zig_extern int32_t zig_expand_concat(zig_expand_concat(__##operation, \
3060 zig_compiler_rt_abbrev_zig_##Type), 2)(zig_##Type, zig_##Type); \
3061 static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
3062 return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_zig_##Type), 2)(lhs, rhs); \
21133063 }
21143064#define zig_float_less_builtin_1(Type, operation) \
2115 static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
3065 static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
21163066 return (!(lhs <= rhs) - (lhs < rhs)); \
21173067 }
21183068
21193069#define zig_float_greater_builtin_0(Type, operation) \
21203070 zig_float_less_builtin_0(Type, operation)
21213071#define zig_float_greater_builtin_1(Type, operation) \
2122 static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
3072 static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
21233073 return ((lhs > rhs) - !(lhs >= rhs)); \
21243074 }
21253075
21263076#define zig_float_binary_builtin_0(Type, operation, operator) \
21273077 zig_extern zig_##Type zig_expand_concat(zig_expand_concat(__##operation, \
2128 zig_compiler_rt_abbrev_##Type), 3)(zig_##Type, zig_##Type); \
3078 zig_compiler_rt_abbrev_zig_##Type), 3)(zig_##Type, zig_##Type); \
21293079 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
2130 return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_##Type), 3)(lhs, rhs); \
3080 return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_zig_##Type), 3)(lhs, rhs); \
21313081 }
21323082#define zig_float_binary_builtin_1(Type, operation, operator) \
21333083 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
......@@ -2135,18 +3085,18 @@ zig_convert_builtin(f128, extend, f80, 2)
21353085 }
21363086
21373087#define zig_float_builtins(Type) \
2138 zig_convert_builtin(i32, fix, Type, ) \
2139 zig_convert_builtin(u32, fixuns, Type, ) \
2140 zig_convert_builtin(i64, fix, Type, ) \
2141 zig_convert_builtin(u64, fixuns, Type, ) \
2142 zig_convert_builtin(i128, fix, Type, ) \
2143 zig_convert_builtin(u128, fixuns, Type, ) \
2144 zig_convert_builtin(Type, float, i32, ) \
2145 zig_convert_builtin(Type, floatun, u32, ) \
2146 zig_convert_builtin(Type, float, i64, ) \
2147 zig_convert_builtin(Type, floatun, u64, ) \
2148 zig_convert_builtin(Type, float, i128, ) \
2149 zig_convert_builtin(Type, floatun, u128, ) \
3088 zig_convert_builtin( int32_t, fix, zig_##Type, ) \
3089 zig_convert_builtin(uint32_t, fixuns, zig_##Type, ) \
3090 zig_convert_builtin( int64_t, fix, zig_##Type, ) \
3091 zig_convert_builtin(uint64_t, fixuns, zig_##Type, ) \
3092 zig_convert_builtin(zig_i128, fix, zig_##Type, ) \
3093 zig_convert_builtin(zig_u128, fixuns, zig_##Type, ) \
3094 zig_convert_builtin(zig_##Type, float, int32_t, ) \
3095 zig_convert_builtin(zig_##Type, floatun, uint32_t, ) \
3096 zig_convert_builtin(zig_##Type, float, int64_t, ) \
3097 zig_convert_builtin(zig_##Type, floatun, uint64_t, ) \
3098 zig_convert_builtin(zig_##Type, float, zig_i128, ) \
3099 zig_convert_builtin(zig_##Type, floatun, zig_u128, ) \
21503100 zig_expand_concat(zig_float_negate_builtin_, zig_has_##Type)(Type) \
21513101 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, cmp) \
21523102 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, ne) \
......@@ -2194,155 +3144,162 @@ zig_float_builtins(f32)
21943144zig_float_builtins(f64)
21953145zig_float_builtins(f80)
21963146zig_float_builtins(f128)
2197zig_float_builtins(c_longdouble)
21983147
21993148#if _MSC_VER && (_M_IX86 || _M_X64)
22003149
22013150// TODO: zig_msvc_atomic_load should load 32 bit without interlocked on x86, and load 64 bit without interlocked on x64
22023151
2203#define zig_msvc_atomics(Type, suffix) \
2204 static inline bool zig_msvc_cmpxchg_##Type(zig_##Type volatile* obj, zig_##Type* expected, zig_##Type desired) { \
2205 zig_##Type comparand = *expected; \
2206 zig_##Type initial = _InterlockedCompareExchange##suffix(obj, desired, comparand); \
3152#define zig_msvc_atomics(ZigType, Type, suffix) \
3153 static inline bool zig_msvc_cmpxchg_##ZigType(Type volatile* obj, Type* expected, Type desired) { \
3154 Type comparand = *expected; \
3155 Type initial = _InterlockedCompareExchange##suffix(obj, desired, comparand); \
22073156 bool exchanged = initial == comparand; \
22083157 if (!exchanged) { \
22093158 *expected = initial; \
22103159 } \
22113160 return exchanged; \
22123161 } \
2213 static inline zig_##Type zig_msvc_atomicrmw_xchg_##Type(zig_##Type volatile* obj, zig_##Type value) { \
3162 static inline Type zig_msvc_atomicrmw_xchg_##ZigType(Type volatile* obj, Type value) { \
22143163 return _InterlockedExchange##suffix(obj, value); \
22153164 } \
2216 static inline zig_##Type zig_msvc_atomicrmw_add_##Type(zig_##Type volatile* obj, zig_##Type value) { \
3165 static inline Type zig_msvc_atomicrmw_add_##ZigType(Type volatile* obj, Type value) { \
22173166 return _InterlockedExchangeAdd##suffix(obj, value); \
22183167 } \
2219 static inline zig_##Type zig_msvc_atomicrmw_sub_##Type(zig_##Type volatile* obj, zig_##Type value) { \
3168 static inline Type zig_msvc_atomicrmw_sub_##ZigType(Type volatile* obj, Type value) { \
22203169 bool success = false; \
2221 zig_##Type new; \
2222 zig_##Type prev; \
3170 Type new; \
3171 Type prev; \
22233172 while (!success) { \
22243173 prev = *obj; \
22253174 new = prev - value; \
2226 success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \
3175 success = zig_msvc_cmpxchg_##ZigType(obj, &prev, new); \
22273176 } \
22283177 return prev; \
22293178 } \
2230 static inline zig_##Type zig_msvc_atomicrmw_or_##Type(zig_##Type volatile* obj, zig_##Type value) { \
3179 static inline Type zig_msvc_atomicrmw_or_##ZigType(Type volatile* obj, Type value) { \
22313180 return _InterlockedOr##suffix(obj, value); \
22323181 } \
2233 static inline zig_##Type zig_msvc_atomicrmw_xor_##Type(zig_##Type volatile* obj, zig_##Type value) { \
3182 static inline Type zig_msvc_atomicrmw_xor_##ZigType(Type volatile* obj, Type value) { \
22343183 return _InterlockedXor##suffix(obj, value); \
22353184 } \
2236 static inline zig_##Type zig_msvc_atomicrmw_and_##Type(zig_##Type volatile* obj, zig_##Type value) { \
3185 static inline Type zig_msvc_atomicrmw_and_##ZigType(Type volatile* obj, Type value) { \
22373186 return _InterlockedAnd##suffix(obj, value); \
22383187 } \
2239 static inline zig_##Type zig_msvc_atomicrmw_nand_##Type(zig_##Type volatile* obj, zig_##Type value) { \
3188 static inline Type zig_msvc_atomicrmw_nand_##ZigType(Type volatile* obj, Type value) { \
22403189 bool success = false; \
2241 zig_##Type new; \
2242 zig_##Type prev; \
3190 Type new; \
3191 Type prev; \
22433192 while (!success) { \
22443193 prev = *obj; \
22453194 new = ~(prev & value); \
2246 success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \
3195 success = zig_msvc_cmpxchg_##ZigType(obj, &prev, new); \
22473196 } \
22483197 return prev; \
22493198 } \
2250 static inline zig_##Type zig_msvc_atomicrmw_min_##Type(zig_##Type volatile* obj, zig_##Type value) { \
3199 static inline Type zig_msvc_atomicrmw_min_##ZigType(Type volatile* obj, Type value) { \
22513200 bool success = false; \
2252 zig_##Type new; \
2253 zig_##Type prev; \
3201 Type new; \
3202 Type prev; \
22543203 while (!success) { \
22553204 prev = *obj; \
22563205 new = value < prev ? value : prev; \
2257 success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \
3206 success = zig_msvc_cmpxchg_##ZigType(obj, &prev, new); \
22583207 } \
22593208 return prev; \
22603209 } \
2261 static inline zig_##Type zig_msvc_atomicrmw_max_##Type(zig_##Type volatile* obj, zig_##Type value) { \
3210 static inline Type zig_msvc_atomicrmw_max_##ZigType(Type volatile* obj, Type value) { \
22623211 bool success = false; \
2263 zig_##Type new; \
2264 zig_##Type prev; \
3212 Type new; \
3213 Type prev; \
22653214 while (!success) { \
22663215 prev = *obj; \
22673216 new = value > prev ? value : prev; \
2268 success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \
3217 success = zig_msvc_cmpxchg_##ZigType(obj, &prev, new); \
22693218 } \
22703219 return prev; \
22713220 } \
2272 static inline void zig_msvc_atomic_store_##Type(zig_##Type volatile* obj, zig_##Type value) { \
3221 static inline void zig_msvc_atomic_store_##ZigType(Type volatile* obj, Type value) { \
22733222 _InterlockedExchange##suffix(obj, value); \
22743223 } \
2275 static inline zig_##Type zig_msvc_atomic_load_##Type(zig_##Type volatile* obj) { \
3224 static inline Type zig_msvc_atomic_load_##ZigType(Type volatile* obj) { \
22763225 return _InterlockedOr##suffix(obj, 0); \
22773226 }
22783227
2279zig_msvc_atomics(u8, 8)
2280zig_msvc_atomics(i8, 8)
2281zig_msvc_atomics(u16, 16)
2282zig_msvc_atomics(i16, 16)
2283zig_msvc_atomics(u32, )
2284zig_msvc_atomics(i32, )
3228zig_msvc_atomics( u8, uint8_t, 8)
3229zig_msvc_atomics( i8, int8_t, 8)
3230zig_msvc_atomics(u16, uint16_t, 16)
3231zig_msvc_atomics(i16, int16_t, 16)
3232zig_msvc_atomics(u32, uint32_t, )
3233zig_msvc_atomics(i32, int32_t, )
22853234
22863235#if _M_X64
2287zig_msvc_atomics(u64, 64)
2288zig_msvc_atomics(i64, 64)
3236zig_msvc_atomics(u64, uint64_t, 64)
3237zig_msvc_atomics(i64, int64_t, 64)
22893238#endif
22903239
22913240#define zig_msvc_flt_atomics(Type, ReprType, suffix) \
22923241 static inline bool zig_msvc_cmpxchg_##Type(zig_##Type volatile* obj, zig_##Type* expected, zig_##Type desired) { \
2293 zig_##ReprType comparand = *((zig_##ReprType*)expected); \
2294 zig_##ReprType initial = _InterlockedCompareExchange##suffix((zig_##ReprType volatile*)obj, *((zig_##ReprType*)&desired), comparand); \
2295 bool exchanged = initial == comparand; \
2296 if (!exchanged) { \
2297 *expected = *((zig_##Type*)&initial); \
2298 } \
2299 return exchanged; \
3242 ReprType exchange; \
3243 ReprType comparand; \
3244 ReprType initial; \
3245 bool success; \
3246 memcpy(&comparand, expected, sizeof(comparand)); \
3247 memcpy(&exchange, &desired, sizeof(exchange)); \
3248 initial = _InterlockedCompareExchange##suffix((ReprType volatile*)obj, exchange, comparand); \
3249 success = initial == comparand; \
3250 if (!success) memcpy(expected, &initial, sizeof(*expected)); \
3251 return success; \
23003252 } \
23013253 static inline zig_##Type zig_msvc_atomicrmw_xchg_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2302 zig_##ReprType initial = _InterlockedExchange##suffix((zig_##ReprType volatile*)obj, *((zig_##ReprType*)&value)); \
2303 return *((zig_##Type*)&initial); \
3254 ReprType repr; \
3255 ReprType initial; \
3256 zig_##Type result; \
3257 memcpy(&repr, &value, sizeof(repr)); \
3258 initial = _InterlockedExchange##suffix((ReprType volatile*)obj, repr); \
3259 memcpy(&result, &initial, sizeof(result)); \
3260 return result; \
23043261 } \
23053262 static inline zig_##Type zig_msvc_atomicrmw_add_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2306 bool success = false; \
2307 zig_##ReprType new; \
2308 zig_##Type prev; \
2309 while (!success) { \
2310 prev = *obj; \
2311 new = prev + value; \
2312 success = zig_msvc_cmpxchg_##Type(obj, &prev, *((zig_##ReprType*)&new)); \
2313 } \
2314 return prev; \
3263 ReprType repr; \
3264 zig_##Type expected; \
3265 zig_##Type desired; \
3266 repr = *(ReprType volatile*)obj; \
3267 memcpy(&expected, &repr, sizeof(expected)); \
3268 do { \
3269 desired = expected + value; \
3270 } while (!zig_msvc_cmpxchg_##Type(obj, &expected, desired)); \
3271 return expected; \
23153272 } \
23163273 static inline zig_##Type zig_msvc_atomicrmw_sub_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2317 bool success = false; \
2318 zig_##ReprType new; \
2319 zig_##Type prev; \
2320 while (!success) { \
2321 prev = *obj; \
2322 new = prev - value; \
2323 success = zig_msvc_cmpxchg_##Type(obj, &prev, *((zig_##ReprType*)&new)); \
2324 } \
2325 return prev; \
3274 ReprType repr; \
3275 zig_##Type expected; \
3276 zig_##Type desired; \
3277 repr = *(ReprType volatile*)obj; \
3278 memcpy(&expected, &repr, sizeof(expected)); \
3279 do { \
3280 desired = expected - value; \
3281 } while (!zig_msvc_cmpxchg_##Type(obj, &expected, desired)); \
3282 return expected; \
23263283 }
23273284
2328zig_msvc_flt_atomics(f32, u32, )
3285zig_msvc_flt_atomics(f32, uint32_t, )
23293286#if _M_X64
2330zig_msvc_flt_atomics(f64, u64, 64)
3287zig_msvc_flt_atomics(f64, uint64_t, 64)
23313288#endif
23323289
23333290#if _M_IX86
23343291static inline void zig_msvc_atomic_barrier() {
2335 zig_i32 barrier;
3292 int32_t barrier;
23363293 __asm {
23373294 xchg barrier, eax
23383295 }
23393296}
23403297
2341static inline void* zig_msvc_atomicrmw_xchg_p32(void** obj, zig_u32* arg) {
3298static inline void* zig_msvc_atomicrmw_xchg_p32(void** obj, void* arg) {
23423299 return _InterlockedExchangePointer(obj, arg);
23433300}
23443301
2345static inline void zig_msvc_atomic_store_p32(void** obj, zig_u32* arg) {
3302static inline void zig_msvc_atomic_store_p32(void** obj, void* arg) {
23463303 _InterlockedExchangePointer(obj, arg);
23473304}
23483305
......@@ -2360,11 +3317,11 @@ static inline bool zig_msvc_cmpxchg_p32(void** obj, void** expected, void* desir
23603317 return exchanged;
23613318}
23623319#else /* _M_IX86 */
2363static inline void* zig_msvc_atomicrmw_xchg_p64(void** obj, zig_u64* arg) {
3320static inline void* zig_msvc_atomicrmw_xchg_p64(void** obj, void* arg) {
23643321 return _InterlockedExchangePointer(obj, arg);
23653322}
23663323
2367static inline void zig_msvc_atomic_store_p64(void** obj, zig_u64* arg) {
3324static inline void zig_msvc_atomic_store_p64(void** obj, void* arg) {
23683325 _InterlockedExchangePointer(obj, arg);
23693326}
23703327
......@@ -2383,11 +3340,11 @@ static inline bool zig_msvc_cmpxchg_p64(void** obj, void** expected, void* desir
23833340}
23843341
23853342static inline bool zig_msvc_cmpxchg_u128(zig_u128 volatile* obj, zig_u128* expected, zig_u128 desired) {
2386 return _InterlockedCompareExchange128((zig_i64 volatile*)obj, desired.hi, desired.lo, (zig_i64*)expected);
3343 return _InterlockedCompareExchange128((int64_t volatile*)obj, desired.hi, desired.lo, (int64_t*)expected);
23873344}
23883345
23893346static inline bool zig_msvc_cmpxchg_i128(zig_i128 volatile* obj, zig_i128* expected, zig_i128 desired) {
2390 return _InterlockedCompareExchange128((zig_i64 volatile*)obj, desired.hi, desired.lo, (zig_u64*)expected);
3347 return _InterlockedCompareExchange128((int64_t volatile*)obj, desired.hi, desired.lo, (uint64_t*)expected);
23913348}
23923349
23933350#define zig_msvc_atomics_128xchg(Type) \
......@@ -2429,7 +3386,7 @@ zig_msvc_atomics_128op(u128, max)
24293386
24303387#endif /* _MSC_VER && (_M_IX86 || _M_X64) */
24313388
2432/* ========================= Special Case Intrinsics ========================= */
3389/* ======================== Special Case Intrinsics ========================= */
24333390
24343391#if (_MSC_VER && _M_X64) || defined(__x86_64__)
24353392
......@@ -2459,8 +3416,8 @@ static inline void* zig_x86_windows_teb(void) {
24593416
24603417#if (_MSC_VER && (_M_IX86 || _M_X64)) || defined(__i386__) || defined(__x86_64__)
24613418
2462static inline void zig_x86_cpuid(zig_u32 leaf_id, zig_u32 subid, zig_u32* eax, zig_u32* ebx, zig_u32* ecx, zig_u32* edx) {
2463 zig_u32 cpu_info[4];
3419static inline void zig_x86_cpuid(uint32_t leaf_id, uint32_t subid, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx) {
3420 uint32_t cpu_info[4];
24643421#if _MSC_VER
24653422 __cpuidex(cpu_info, leaf_id, subid);
24663423#else
......@@ -2472,12 +3429,12 @@ static inline void zig_x86_cpuid(zig_u32 leaf_id, zig_u32 subid, zig_u32* eax, z
24723429 *edx = cpu_info[3];
24733430}
24743431
2475static inline zig_u32 zig_x86_get_xcr0(void) {
3432static inline uint32_t zig_x86_get_xcr0(void) {
24763433#if _MSC_VER
2477 return (zig_u32)_xgetbv(0);
3434 return (uint32_t)_xgetbv(0);
24783435#else
2479 zig_u32 eax;
2480 zig_u32 edx;
3436 uint32_t eax;
3437 uint32_t edx;
24813438 __asm__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0));
24823439 return eax;
24833440#endif
stage1/zig1.wasm
Binary files a/stage1/zig1.wasm and b/stage1/zig1.wasm differ