authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-02-20 20:52:26-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-02-20 23:59:48-05:00
log3eed197c95c21d850d503687f445946e6bd429c5
treedea7f74c1bc864dfdc97829aaafbcc8b72b92207
parentd513792afa4893c21d5a9635c61d8e41689d9541

CBE: use stdint.h types instead of `zig_` prefixes

This requires manual defines before C99 which may not have stdint.h. Also have update-zig1 leave a copy of lib/zig.h in stage1/zig.h, which allows lib/zig.h to be updated without needing to update zig1.wasm. Note that since the object already existed with the exact same contents, this completely avoids repo bloat due to zig.h changes.

6 files changed, 3396 insertions(+), 751 deletions(-)

CMakeLists.txt+1-1
...@@ -783,7 +783,7 @@ set_target_properties(zig2 PROPERTIES...@@ -783,7 +783,7 @@ set_target_properties(zig2 PROPERTIES
783 COMPILE_FLAGS ${ZIG2_COMPILE_FLAGS}783 COMPILE_FLAGS ${ZIG2_COMPILE_FLAGS}
784 LINK_FLAGS ${ZIG2_LINK_FLAGS}784 LINK_FLAGS ${ZIG2_LINK_FLAGS}
785)785)
786target_include_directories(zig2 PUBLIC "${CMAKE_SOURCE_DIR}/lib")786target_include_directories(zig2 PUBLIC "${CMAKE_SOURCE_DIR}/stage1")
787target_link_libraries(zig2 LINK_PUBLIC zigcpp)787target_link_libraries(zig2 LINK_PUBLIC zigcpp)
788788
789if(MSVC)789if(MSVC)
build.zig+31
...@@ -506,8 +506,39 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {...@@ -506,8 +506,39 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
506 run_opt.addArg("-o");506 run_opt.addArg("-o");
507 run_opt.addFileSourceArg(.{ .path = "stage1/zig1.wasm" });507 run_opt.addFileSourceArg(.{ .path = "stage1/zig1.wasm" });
508508
509 const CopyFileStep = struct {
510 const Step = std.Build.Step;
511 const FileSource = std.Build.FileSource;
512 const CopyFileStep = @This();
513
514 step: Step,
515 builder: *std.Build,
516 source: FileSource,
517 dest_rel_path: []const u8,
518
519 pub fn init(builder: *std.Build, source: FileSource, dest_rel_path: []const u8) CopyFileStep {
520 return CopyFileStep{
521 .builder = builder,
522 .step = Step.init(.custom, builder.fmt("install {s} to {s}", .{ source.getDisplayName(), dest_rel_path }), builder.allocator, make),
523 .source = source.dupe(builder),
524 .dest_rel_path = builder.dupePath(dest_rel_path),
525 };
526 }
527
528 fn make(step: *Step) !void {
529 const self = @fieldParentPtr(CopyFileStep, "step", step);
530 const full_src_path = self.source.getPath(self.builder);
531 const full_dest_path = self.builder.pathFromRoot(self.dest_rel_path);
532 try self.builder.updateFile(full_src_path, full_dest_path);
533 }
534 };
535
536 const copy_zig_h = try b.allocator.create(CopyFileStep);
537 copy_zig_h.* = CopyFileStep.init(b, .{ .path = "lib/zig.h" }, "stage1/zig.h");
538
509 const update_zig1_step = b.step("update-zig1", "Update stage1/zig1.wasm");539 const update_zig1_step = b.step("update-zig1", "Update stage1/zig1.wasm");
510 update_zig1_step.dependOn(&run_opt.step);540 update_zig1_step.dependOn(&run_opt.step);
541 update_zig1_step.dependOn(&copy_zig_h.step);
511}542}
512543
513fn addCompilerStep(544fn addCompilerStep(
lib/zig.h+773-667
...@@ -1,6 +1,8 @@...@@ -1,6 +1,8 @@
1#undef linux1#undef linux
22
3#ifndef __STDC_WANT_IEC_60559_TYPES_EXT__
3#define __STDC_WANT_IEC_60559_TYPES_EXT__4#define __STDC_WANT_IEC_60559_TYPES_EXT__
5#endif
4#include <float.h>6#include <float.h>
5#include <limits.h>7#include <limits.h>
6#include <stddef.h>8#include <stddef.h>
...@@ -297,690 +299,791 @@ typedef char bool;...@@ -297,690 +299,791 @@ typedef char bool;
297299
298#define zig_bitSizeOf(T) (CHAR_BIT * sizeof(T))300#define zig_bitSizeOf(T) (CHAR_BIT * sizeof(T))
299301
300typedef uintptr_t zig_usize;302#define zig_compiler_rt_abbrev_uint32_t si
301typedef intptr_t zig_isize;303#define zig_compiler_rt_abbrev_int32_t si
302typedef signed short int zig_c_short;304#define zig_compiler_rt_abbrev_uint64_t di
303typedef unsigned short int zig_c_ushort;305#define zig_compiler_rt_abbrev_int64_t di
304typedef signed int zig_c_int;306#define zig_compiler_rt_abbrev_zig_u128 ti
305typedef unsigned int zig_c_uint;307#define zig_compiler_rt_abbrev_zig_i128 ti
306typedef signed long int zig_c_long;308#define zig_compiler_rt_abbrev_zig_f16 hf
307typedef unsigned long int zig_c_ulong;309#define zig_compiler_rt_abbrev_zig_f32 sf
308typedef signed long long int zig_c_longlong;310#define zig_compiler_rt_abbrev_zig_f64 df
309typedef unsigned long long int zig_c_ulonglong;311#define zig_compiler_rt_abbrev_zig_f80 xf
310312#define zig_compiler_rt_abbrev_zig_f128 tf
311typedef uint8_t zig_u8;313
312typedef int8_t zig_i8;314zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, size_t);
313typedef uint16_t zig_u16;315zig_extern void *memset (void *, int, size_t);
314typedef int16_t zig_i16;316
315typedef uint32_t zig_u32;317/* ===================== 8/16/32/64-bit Integer Support ===================== */
316typedef int32_t zig_i32;318
317typedef uint64_t zig_u64;319#if __STDC_VERSION__ >= 199901L
318typedef int64_t zig_i64;320#include <stdint.h>
319321#else
320#define zig_as_u8(val) UINT8_C(val)322
321#define zig_as_i8(val) INT8_C(val)323#if SCHAR_MIN == ~0x7F && SCHAR_MAX == 0x7F && UCHAR_MAX == 0xFF
322#define zig_as_u16(val) UINT16_C(val)324typedef unsigned char uint8_t;
323#define zig_as_i16(val) INT16_C(val)325typedef signed char int8_t;
324#define zig_as_u32(val) UINT32_C(val)326#define INT8_C(c) c
325#define zig_as_i32(val) INT32_C(val)327#define UINT8_C(c) c##U
326#define zig_as_u64(val) UINT64_C(val)328#elif SHRT_MIN == ~0x7F && SHRT_MAX == 0x7F && USHRT_MAX == 0xFF
327#define zig_as_i64(val) INT64_C(val)329typedef unsigned short uint8_t;
328330typedef signed short int8_t;
329#define zig_minInt_u8 zig_as_u8(0)331#define INT8_C(c) c
330#define zig_maxInt_u8 UINT8_MAX332#define UINT8_C(c) c##U
333#elif INT_MIN == ~0x7F && INT_MAX == 0x7F && UINT_MAX == 0xFF
334typedef unsigned int uint8_t;
335typedef signed int int8_t;
336#define INT8_C(c) c
337#define UINT8_C(c) c##U
338#elif LONG_MIN == ~0x7F && LONG_MAX == 0x7F && ULONG_MAX == 0xFF
339typedef unsigned long uint8_t;
340typedef signed long int8_t;
341#define INT8_C(c) c##L
342#define UINT8_C(c) c##LU
343#elif LLONG_MIN == ~0x7F && LLONG_MAX == 0x7F && ULLONG_MAX == 0xFF
344typedef unsigned long long uint8_t;
345typedef signed long long int8_t;
346#define INT8_C(c) c##LL
347#define UINT8_C(c) c##LLU
348#endif
349#define INT8_MIN (~INT8_C(0x7F))
350#define INT8_MAX ( INT8_C(0x7F))
351#define UINT8_MAX ( INT8_C(0xFF))
352
353#if SCHAR_MIN == ~0x7FFF && SCHAR_MAX == 0x7FFF && UCHAR_MAX == 0xFFFF
354typedef unsigned char uint16_t;
355typedef signed char int16_t;
356#define INT16_C(c) c
357#define UINT16_C(c) c##U
358#elif SHRT_MIN == ~0x7FFF && SHRT_MAX == 0x7FFF && USHRT_MAX == 0xFFFF
359typedef unsigned short uint16_t;
360typedef signed short int16_t;
361#define INT16_C(c) c
362#define UINT16_C(c) c##U
363#elif INT_MIN == ~0x7FFF && INT_MAX == 0x7FFF && UINT_MAX == 0xFFFF
364typedef unsigned int uint16_t;
365typedef signed int int16_t;
366#define INT16_C(c) c
367#define UINT16_C(c) c##U
368#elif LONG_MIN == ~0x7FFF && LONG_MAX == 0x7FFF && ULONG_MAX == 0xFFFF
369typedef unsigned long uint16_t;
370typedef signed long int16_t;
371#define INT16_C(c) c##L
372#define UINT16_C(c) c##LU
373#elif LLONG_MIN == ~0x7FFF && LLONG_MAX == 0x7FFF && ULLONG_MAX == 0xFFFF
374typedef unsigned long long uint16_t;
375typedef signed long long int16_t;
376#define INT16_C(c) c##LL
377#define UINT16_C(c) c##LLU
378#endif
379#define INT16_MIN (~INT16_C(0x7FFF))
380#define INT16_MAX ( INT16_C(0x7FFF))
381#define UINT16_MAX ( INT16_C(0xFFFF))
382
383#if SCHAR_MIN == ~0x7FFFFFFF && SCHAR_MAX == 0x7FFFFFFF && UCHAR_MAX == 0xFFFFFFFF
384typedef unsigned char uint32_t;
385typedef signed char int32_t;
386#define INT32_C(c) c
387#define UINT32_C(c) c##U
388#elif SHRT_MIN == ~0x7FFFFFFF && SHRT_MAX == 0x7FFFFFFF && USHRT_MAX == 0xFFFFFFFF
389typedef unsigned short uint32_t;
390typedef signed short int32_t;
391#define INT32_C(c) c
392#define UINT32_C(c) c##U
393#elif INT_MIN == ~0x7FFFFFFF && INT_MAX == 0x7FFFFFFF && UINT_MAX == 0xFFFFFFFF
394typedef unsigned int uint32_t;
395typedef signed int int32_t;
396#define INT32_C(c) c
397#define UINT32_C(c) c##U
398#elif LONG_MIN == ~0x7FFFFFFF && LONG_MAX == 0x7FFFFFFF && ULONG_MAX == 0xFFFFFFFF
399typedef unsigned long uint32_t;
400typedef signed long int32_t;
401#define INT32_C(c) c##L
402#define UINT32_C(c) c##LU
403#elif LLONG_MIN == ~0x7FFFFFFF && LLONG_MAX == 0x7FFFFFFF && ULLONG_MAX == 0xFFFFFFFF
404typedef unsigned long long uint32_t;
405typedef signed long long int32_t;
406#define INT32_C(c) c##LL
407#define UINT32_C(c) c##LLU
408#endif
409#define INT32_MIN (~INT32_C(0x7FFFFFFF))
410#define INT32_MAX ( INT32_C(0x7FFFFFFF))
411#define UINT32_MAX ( INT32_C(0xFFFFFFFF))
412
413#if SCHAR_MIN == ~0x7FFFFFFFFFFFFFFF && SCHAR_MAX == 0x7FFFFFFFFFFFFFFF && UCHAR_MAX == 0xFFFFFFFFFFFFFFFF
414typedef unsigned char uint64_t;
415typedef signed char int64_t;
416#define INT64_C(c) c
417#define UINT64_C(c) c##U
418#elif SHRT_MIN == ~0x7FFFFFFFFFFFFFFF && SHRT_MAX == 0x7FFFFFFFFFFFFFFF && USHRT_MAX == 0xFFFFFFFFFFFFFFFF
419typedef unsigned short uint64_t;
420typedef signed short int64_t;
421#define INT64_C(c) c
422#define UINT64_C(c) c##U
423#elif INT_MIN == ~0x7FFFFFFFFFFFFFFF && INT_MAX == 0x7FFFFFFFFFFFFFFF && UINT_MAX == 0xFFFFFFFFFFFFFFFF
424typedef unsigned int uint64_t;
425typedef signed int int64_t;
426#define INT64_C(c) c
427#define UINT64_C(c) c##U
428#elif LONG_MIN == ~0x7FFFFFFFFFFFFFFF && LONG_MAX == 0x7FFFFFFFFFFFFFFF && ULONG_MAX == 0xFFFFFFFFFFFFFFFF
429typedef unsigned long uint64_t;
430typedef signed long int64_t;
431#define INT64_C(c) c##L
432#define UINT64_C(c) c##LU
433#elif LLONG_MIN == ~0x7FFFFFFFFFFFFFFF && LLONG_MAX == 0x7FFFFFFFFFFFFFFF && ULLONG_MAX == 0xFFFFFFFFFFFFFFFF
434typedef unsigned long long uint64_t;
435typedef signed long long int64_t;
436#define INT64_C(c) c##LL
437#define UINT64_C(c) c##LLU
438#endif
439#define INT64_MIN (~INT64_C(0x7FFFFFFFFFFFFFFF))
440#define INT64_MAX ( INT64_C(0x7FFFFFFFFFFFFFFF))
441#define UINT64_MAX ( INT64_C(0xFFFFFFFFFFFFFFFF))
442
443typedef size_t uintptr_t;
444typedef ptrdiff_t intptr_t;
445
446#endif
447
331#define zig_minInt_i8 INT8_MIN448#define zig_minInt_i8 INT8_MIN
332#define zig_maxInt_i8 INT8_MAX449#define zig_maxInt_i8 INT8_MAX
333#define zig_minInt_u16 zig_as_u16(0)450#define zig_minInt_u8 UINT8_C(0)
334#define zig_maxInt_u16 UINT16_MAX451#define zig_maxInt_u8 UINT8_MAX
335#define zig_minInt_i16 INT16_MIN452#define zig_minInt_i16 INT16_MIN
336#define zig_maxInt_i16 INT16_MAX453#define zig_maxInt_i16 INT16_MAX
337#define zig_minInt_u32 zig_as_u32(0)454#define zig_minInt_u16 UINT16_C(0)
338#define zig_maxInt_u32 UINT32_MAX455#define zig_maxInt_u16 UINT16_MAX
339#define zig_minInt_i32 INT32_MIN456#define zig_minInt_i32 INT32_MIN
340#define zig_maxInt_i32 INT32_MAX457#define zig_maxInt_i32 INT32_MAX
341#define zig_minInt_u64 zig_as_u64(0)458#define zig_minInt_u32 UINT32_C(0)
342#define zig_maxInt_u64 UINT64_MAX459#define zig_maxInt_u32 UINT32_MAX
343#define zig_minInt_i64 INT64_MIN460#define zig_minInt_i64 INT64_MIN
344#define zig_maxInt_i64 INT64_MAX461#define zig_maxInt_i64 INT64_MAX
462#define zig_minInt_u64 UINT64_C(0)
463#define zig_maxInt_u64 UINT64_MAX
345464
346#define zig_compiler_rt_abbrev_u32 si465#define zig_intLimit(s, w, limit, bits) zig_shr_##s##w(zig_##limit##Int_##s##w, w - (bits))
347#define zig_compiler_rt_abbrev_i32 si466#define zig_minInt_i(w, bits) zig_intLimit(i, w, min, bits)
348#define zig_compiler_rt_abbrev_u64 di467#define zig_maxInt_i(w, bits) zig_intLimit(i, w, max, bits)
349#define zig_compiler_rt_abbrev_i64 di468#define zig_minInt_u(w, bits) zig_intLimit(u, w, min, bits)
350#define zig_compiler_rt_abbrev_u128 ti469#define zig_maxInt_u(w, bits) zig_intLimit(u, w, max, bits)
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)
367470
368#define zig_int_operator(Type, RhsType, operation, operator) \471#define zig_int_operator(Type, RhsType, operation, operator) \
369 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##RhsType rhs) { \472 static inline Type zig_##operation(Type lhs, RhsType rhs) { \
370 return lhs operator rhs; \473 return lhs operator rhs; \
371 }474 }
372#define zig_int_basic_operator(Type, operation, operator) \475#define zig_int_basic_operator(Type, operation, operator) \
373 zig_int_operator(Type, Type, operation, operator)476 zig_int_operator(Type, Type, operation, operator)
374#define zig_int_shift_operator(Type, operation, operator) \477#define zig_int_shift_operator(Type, operation, operator) \
375 zig_int_operator(Type, u8, operation, operator)478 zig_int_operator(Type, uint8_t, operation, operator)
376#define zig_int_helpers(w) \479#define zig_int_helpers(w) \
377 zig_int_basic_operator(u##w, and, &) \480 zig_int_basic_operator(uint##w##_t, and_u##w, &) \
378 zig_int_basic_operator(i##w, and, &) \481 zig_int_basic_operator( int##w##_t, and_i##w, &) \
379 zig_int_basic_operator(u##w, or, |) \482 zig_int_basic_operator(uint##w##_t, or_u##w, |) \
380 zig_int_basic_operator(i##w, or, |) \483 zig_int_basic_operator( int##w##_t, or_i##w, |) \
381 zig_int_basic_operator(u##w, xor, ^) \484 zig_int_basic_operator(uint##w##_t, xor_u##w, ^) \
382 zig_int_basic_operator(i##w, xor, ^) \485 zig_int_basic_operator( int##w##_t, xor_i##w, ^) \
383 zig_int_shift_operator(u##w, shl, <<) \486 zig_int_shift_operator(uint##w##_t, shl_u##w, <<) \
384 zig_int_shift_operator(i##w, shl, <<) \487 zig_int_shift_operator( int##w##_t, shl_i##w, <<) \
385 zig_int_shift_operator(u##w, shr, >>) \488 zig_int_shift_operator(uint##w##_t, shr_u##w, >>) \
386\489\
387 static inline zig_i##w zig_shr_i##w(zig_i##w lhs, zig_u8 rhs) { \490 static inline int##w##_t zig_shr_i##w(int##w##_t lhs, uint8_t rhs) { \
388 zig_i##w sign_mask = lhs < zig_as_i##w(0) ? -zig_as_i##w(1) : zig_as_i##w(0); \491 int##w##_t sign_mask = lhs < INT##w##_C(0) ? -INT##w##_C(1) : INT##w##_C(0); \
389 return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \492 return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \
390 } \493 } \
391\494\
392 static inline zig_u##w zig_not_u##w(zig_u##w val, zig_u8 bits) { \495 static inline uint##w##_t zig_not_u##w(uint##w##_t val, uint8_t bits) { \
393 return val ^ zig_maxInt(u##w, bits); \496 return val ^ zig_maxInt_u(w, bits); \
394 } \497 } \
395\498\
396 static inline zig_i##w zig_not_i##w(zig_i##w val, zig_u8 bits) { \499 static inline int##w##_t zig_not_i##w(int##w##_t val, uint8_t bits) { \
397 (void)bits; \500 (void)bits; \
398 return ~val; \501 return ~val; \
399 } \502 } \
400\503\
401 static inline zig_u##w zig_wrap_u##w(zig_u##w val, zig_u8 bits) { \504 static inline uint##w##_t zig_wrap_u##w(uint##w##_t val, uint8_t bits) { \
402 return val & zig_maxInt(u##w, bits); \505 return val & zig_maxInt_u(w, bits); \
403 } \506 } \
404\507\
405 static inline zig_i##w zig_wrap_i##w(zig_i##w val, zig_u8 bits) { \508 static inline int##w##_t zig_wrap_i##w(int##w##_t val, uint8_t bits) { \
406 return (val & zig_as_u##w(1) << (bits - zig_as_u8(1))) != 0 \509 return (val & UINT##w##_C(1) << (bits - UINT8_C(1))) != 0 \
407 ? val | zig_minInt(i##w, bits) : val & zig_maxInt(i##w, bits); \510 ? val | zig_minInt_i(w, bits) : val & zig_maxInt_i(w, bits); \
408 } \511 } \
409\512\
410 zig_int_basic_operator(u##w, div_floor, /) \513 zig_int_basic_operator(uint##w##_t, div_floor_u##w, /) \
411\514\
412 static inline zig_i##w zig_div_floor_i##w(zig_i##w lhs, zig_i##w rhs) { \515 static inline int##w##_t zig_div_floor_i##w(int##w##_t lhs, int##w##_t rhs) { \
413 return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < zig_as_i##w(0)); \516 return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < INT##w##_C(0)); \
414 } \517 } \
415\518\
416 zig_int_basic_operator(u##w, mod, %) \519 zig_int_basic_operator(uint##w##_t, mod_u##w, %) \
417\520\
418 static inline zig_i##w zig_mod_i##w(zig_i##w lhs, zig_i##w rhs) { \521 static inline int##w##_t zig_mod_i##w(int##w##_t lhs, int##w##_t rhs) { \
419 zig_i##w rem = lhs % rhs; \522 int##w##_t rem = lhs % rhs; \
420 return rem + (((lhs ^ rhs) & rem) < zig_as_i##w(0) ? rhs : zig_as_i##w(0)); \523 return rem + (((lhs ^ rhs) & rem) < INT##w##_C(0) ? rhs : INT##w##_C(0)); \
421 } \524 } \
422\525\
423 static inline zig_u##w zig_shlw_u##w(zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \526 static inline uint##w##_t zig_shlw_u##w(uint##w##_t lhs, uint8_t rhs, uint8_t bits) { \
424 return zig_wrap_u##w(zig_shl_u##w(lhs, rhs), bits); \527 return zig_wrap_u##w(zig_shl_u##w(lhs, rhs), bits); \
425 } \528 } \
426\529\
427 static inline zig_i##w zig_shlw_i##w(zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \530 static inline int##w##_t zig_shlw_i##w(int##w##_t lhs, uint8_t rhs, uint8_t bits) { \
428 return zig_wrap_i##w((zig_i##w)zig_shl_u##w((zig_u##w)lhs, (zig_u##w)rhs), bits); \531 return zig_wrap_i##w((int##w##_t)zig_shl_u##w((uint##w##_t)lhs, (uint##w##_t)rhs), bits); \
429 } \532 } \
430\533\
431 static inline zig_u##w zig_addw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \534 static inline uint##w##_t zig_addw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
432 return zig_wrap_u##w(lhs + rhs, bits); \535 return zig_wrap_u##w(lhs + rhs, bits); \
433 } \536 } \
434\537\
435 static inline zig_i##w zig_addw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \538 static inline int##w##_t zig_addw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
436 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs + (zig_u##w)rhs), bits); \539 return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs + (uint##w##_t)rhs), bits); \
437 } \540 } \
438\541\
439 static inline zig_u##w zig_subw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \542 static inline uint##w##_t zig_subw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
440 return zig_wrap_u##w(lhs - rhs, bits); \543 return zig_wrap_u##w(lhs - rhs, bits); \
441 } \544 } \
442\545\
443 static inline zig_i##w zig_subw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \546 static inline int##w##_t zig_subw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
444 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs - (zig_u##w)rhs), bits); \547 return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs - (uint##w##_t)rhs), bits); \
445 } \548 } \
446\549\
447 static inline zig_u##w zig_mulw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \550 static inline uint##w##_t zig_mulw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
448 return zig_wrap_u##w(lhs * rhs, bits); \551 return zig_wrap_u##w(lhs * rhs, bits); \
449 } \552 } \
450\553\
451 static inline zig_i##w zig_mulw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \554 static inline int##w##_t zig_mulw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
452 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs * (zig_u##w)rhs), bits); \555 return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs * (uint##w##_t)rhs), bits); \
453 }556 }
454zig_int_helpers(8)557zig_int_helpers(8)
455zig_int_helpers(16)558zig_int_helpers(16)
456zig_int_helpers(32)559zig_int_helpers(32)
457zig_int_helpers(64)560zig_int_helpers(64)
458561
459static inline bool zig_addo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {562static inline bool zig_addo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) {
460#if zig_has_builtin(add_overflow) || defined(zig_gnuc)563#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
461 zig_u32 full_res;564 uint32_t full_res;
462 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);565 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
463 *res = zig_wrap_u32(full_res, bits);566 *res = zig_wrap_u32(full_res, bits);
464 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);567 return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits);
465#else568#else
466 *res = zig_addw_u32(lhs, rhs, bits);569 *res = zig_addw_u32(lhs, rhs, bits);
467 return *res < lhs;570 return *res < lhs;
468#endif571#endif
469}572}
470573
471static inline void zig_vaddo_u32(zig_u8 *ov, zig_u32 *res, int n,574static inline void zig_vaddo_u32(uint8_t *ov, uint32_t *res, int n,
472 const zig_u32 *lhs, const zig_u32 *rhs, zig_u8 bits)575 const uint32_t *lhs, const uint32_t *rhs, uint8_t bits)
473{576{
474 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u32(&res[i], lhs[i], rhs[i], bits);577 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u32(&res[i], lhs[i], rhs[i], bits);
475}578}
476579
477zig_extern zig_i32 __addosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);580zig_extern int32_t __addosi4(int32_t lhs, int32_t rhs, int *overflow);
478static inline bool zig_addo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {581static inline bool zig_addo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
479#if zig_has_builtin(add_overflow) || defined(zig_gnuc)582#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
480 zig_i32 full_res;583 int32_t full_res;
481 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);584 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
482#else585#else
483 zig_c_int overflow_int;586 int overflow_int;
484 zig_i32 full_res = __addosi4(lhs, rhs, &overflow_int);587 int32_t full_res = __addosi4(lhs, rhs, &overflow_int);
485 bool overflow = overflow_int != 0;588 bool overflow = overflow_int != 0;
486#endif589#endif
487 *res = zig_wrap_i32(full_res, bits);590 *res = zig_wrap_i32(full_res, bits);
488 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);591 return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits);
489}592}
490593
491static inline void zig_vaddo_i32(zig_u8 *ov, zig_i32 *res, int n,594static inline void zig_vaddo_i32(uint8_t *ov, int32_t *res, int n,
492 const zig_i32 *lhs, const zig_i32 *rhs, zig_u8 bits)595 const int32_t *lhs, const int32_t *rhs, uint8_t bits)
493{596{
494 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i32(&res[i], lhs[i], rhs[i], bits);597 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i32(&res[i], lhs[i], rhs[i], bits);
495}598}
496599
497static inline bool zig_addo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {600static inline bool zig_addo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) {
498#if zig_has_builtin(add_overflow) || defined(zig_gnuc)601#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
499 zig_u64 full_res;602 uint64_t full_res;
500 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);603 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
501 *res = zig_wrap_u64(full_res, bits);604 *res = zig_wrap_u64(full_res, bits);
502 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);605 return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits);
503#else606#else
504 *res = zig_addw_u64(lhs, rhs, bits);607 *res = zig_addw_u64(lhs, rhs, bits);
505 return *res < lhs;608 return *res < lhs;
506#endif609#endif
507}610}
508611
509static inline void zig_vaddo_u64(zig_u8 *ov, zig_u64 *res, int n,612static inline void zig_vaddo_u64(uint8_t *ov, uint64_t *res, int n,
510 const zig_u64 *lhs, const zig_u64 *rhs, zig_u8 bits)613 const uint64_t *lhs, const uint64_t *rhs, uint8_t bits)
511{614{
512 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u64(&res[i], lhs[i], rhs[i], bits);615 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u64(&res[i], lhs[i], rhs[i], bits);
513}616}
514617
515zig_extern zig_i64 __addodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);618zig_extern int64_t __addodi4(int64_t lhs, int64_t rhs, int *overflow);
516static inline bool zig_addo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {619static inline bool zig_addo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
517#if zig_has_builtin(add_overflow) || defined(zig_gnuc)620#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
518 zig_i64 full_res;621 int64_t full_res;
519 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);622 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
520#else623#else
521 zig_c_int overflow_int;624 int overflow_int;
522 zig_i64 full_res = __addodi4(lhs, rhs, &overflow_int);625 int64_t full_res = __addodi4(lhs, rhs, &overflow_int);
523 bool overflow = overflow_int != 0;626 bool overflow = overflow_int != 0;
524#endif627#endif
525 *res = zig_wrap_i64(full_res, bits);628 *res = zig_wrap_i64(full_res, bits);
526 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);629 return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits);
527}630}
528631
529static inline void zig_vaddo_i64(zig_u8 *ov, zig_i64 *res, int n,632static inline void zig_vaddo_i64(uint8_t *ov, int64_t *res, int n,
530 const zig_i64 *lhs, const zig_i64 *rhs, zig_u8 bits)633 const int64_t *lhs, const int64_t *rhs, uint8_t bits)
531{634{
532 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i64(&res[i], lhs[i], rhs[i], bits);635 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i64(&res[i], lhs[i], rhs[i], bits);
533}636}
534637
535static inline bool zig_addo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {638static inline bool zig_addo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) {
536#if zig_has_builtin(add_overflow) || defined(zig_gnuc)639#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
537 zig_u8 full_res;640 uint8_t full_res;
538 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);641 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
539 *res = zig_wrap_u8(full_res, bits);642 *res = zig_wrap_u8(full_res, bits);
540 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);643 return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits);
541#else644#else
542 zig_u32 full_res;645 uint32_t full_res;
543 bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits);646 bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits);
544 *res = (zig_u8)full_res;647 *res = (uint8_t)full_res;
545 return overflow;648 return overflow;
546#endif649#endif
547}650}
548651
549static inline void zig_vaddo_u8(zig_u8 *ov, zig_u8 *res, int n,652static inline void zig_vaddo_u8(uint8_t *ov, uint8_t *res, int n,
550 const zig_u8 *lhs, const zig_u8 *rhs, zig_u8 bits)653 const uint8_t *lhs, const uint8_t *rhs, uint8_t bits)
551{654{
552 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u8(&res[i], lhs[i], rhs[i], bits);655 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u8(&res[i], lhs[i], rhs[i], bits);
553}656}
554657
555static inline bool zig_addo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {658static inline bool zig_addo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits) {
556#if zig_has_builtin(add_overflow) || defined(zig_gnuc)659#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
557 zig_i8 full_res;660 int8_t full_res;
558 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);661 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
559 *res = zig_wrap_i8(full_res, bits);662 *res = zig_wrap_i8(full_res, bits);
560 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);663 return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits);
561#else664#else
562 zig_i32 full_res;665 int32_t full_res;
563 bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits);666 bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits);
564 *res = (zig_i8)full_res;667 *res = (int8_t)full_res;
565 return overflow;668 return overflow;
566#endif669#endif
567}670}
568671
569static inline void zig_vaddo_i8(zig_u8 *ov, zig_i8 *res, int n,672static inline void zig_vaddo_i8(uint8_t *ov, int8_t *res, int n,
570 const zig_i8 *lhs, const zig_i8 *rhs, zig_u8 bits)673 const int8_t *lhs, const int8_t *rhs, uint8_t bits)
571{674{
572 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i8(&res[i], lhs[i], rhs[i], bits);675 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i8(&res[i], lhs[i], rhs[i], bits);
573}676}
574677
575static inline bool zig_addo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {678static inline bool zig_addo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8_t bits) {
576#if zig_has_builtin(add_overflow) || defined(zig_gnuc)679#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
577 zig_u16 full_res;680 uint16_t full_res;
578 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);681 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
579 *res = zig_wrap_u16(full_res, bits);682 *res = zig_wrap_u16(full_res, bits);
580 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);683 return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits);
581#else684#else
582 zig_u32 full_res;685 uint32_t full_res;
583 bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits);686 bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits);
584 *res = (zig_u16)full_res;687 *res = (uint16_t)full_res;
585 return overflow;688 return overflow;
586#endif689#endif
587}690}
588691
589static inline void zig_vaddo_u16(zig_u8 *ov, zig_u16 *res, int n,692static inline void zig_vaddo_u16(uint8_t *ov, uint16_t *res, int n,
590 const zig_u16 *lhs, const zig_u16 *rhs, zig_u8 bits)693 const uint16_t *lhs, const uint16_t *rhs, uint8_t bits)
591{694{
592 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u16(&res[i], lhs[i], rhs[i], bits);695 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u16(&res[i], lhs[i], rhs[i], bits);
593}696}
594697
595static inline bool zig_addo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {698static inline bool zig_addo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t bits) {
596#if zig_has_builtin(add_overflow) || defined(zig_gnuc)699#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
597 zig_i16 full_res;700 int16_t full_res;
598 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);701 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
599 *res = zig_wrap_i16(full_res, bits);702 *res = zig_wrap_i16(full_res, bits);
600 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);703 return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits);
601#else704#else
602 zig_i32 full_res;705 int32_t full_res;
603 bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits);706 bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits);
604 *res = (zig_i16)full_res;707 *res = (int16_t)full_res;
605 return overflow;708 return overflow;
606#endif709#endif
607}710}
608711
609static inline void zig_vaddo_i16(zig_u8 *ov, zig_i16 *res, int n,712static inline void zig_vaddo_i16(uint8_t *ov, int16_t *res, int n,
610 const zig_i16 *lhs, const zig_i16 *rhs, zig_u8 bits)713 const int16_t *lhs, const int16_t *rhs, uint8_t bits)
611{714{
612 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i16(&res[i], lhs[i], rhs[i], bits);715 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i16(&res[i], lhs[i], rhs[i], bits);
613}716}
614717
615static inline bool zig_subo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {718static inline bool zig_subo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) {
616#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)719#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
617 zig_u32 full_res;720 uint32_t full_res;
618 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);721 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
619 *res = zig_wrap_u32(full_res, bits);722 *res = zig_wrap_u32(full_res, bits);
620 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);723 return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits);
621#else724#else
622 *res = zig_subw_u32(lhs, rhs, bits);725 *res = zig_subw_u32(lhs, rhs, bits);
623 return *res > lhs;726 return *res > lhs;
624#endif727#endif
625}728}
626729
627static inline void zig_vsubo_u32(zig_u8 *ov, zig_u32 *res, int n,730static inline void zig_vsubo_u32(uint8_t *ov, uint32_t *res, int n,
628 const zig_u32 *lhs, const zig_u32 *rhs, zig_u8 bits)731 const uint32_t *lhs, const uint32_t *rhs, uint8_t bits)
629{732{
630 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u32(&res[i], lhs[i], rhs[i], bits);733 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u32(&res[i], lhs[i], rhs[i], bits);
631}734}
632735
633zig_extern zig_i32 __subosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);736zig_extern int32_t __subosi4(int32_t lhs, int32_t rhs, int *overflow);
634static inline bool zig_subo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {737static inline bool zig_subo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
635#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)738#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
636 zig_i32 full_res;739 int32_t full_res;
637 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);740 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
638#else741#else
639 zig_c_int overflow_int;742 int overflow_int;
640 zig_i32 full_res = __subosi4(lhs, rhs, &overflow_int);743 int32_t full_res = __subosi4(lhs, rhs, &overflow_int);
641 bool overflow = overflow_int != 0;744 bool overflow = overflow_int != 0;
642#endif745#endif
643 *res = zig_wrap_i32(full_res, bits);746 *res = zig_wrap_i32(full_res, bits);
644 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);747 return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits);
645}748}
646749
647static inline void zig_vsubo_i32(zig_u8 *ov, zig_i32 *res, int n,750static inline void zig_vsubo_i32(uint8_t *ov, int32_t *res, int n,
648 const zig_i32 *lhs, const zig_i32 *rhs, zig_u8 bits)751 const int32_t *lhs, const int32_t *rhs, uint8_t bits)
649{752{
650 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i32(&res[i], lhs[i], rhs[i], bits);753 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i32(&res[i], lhs[i], rhs[i], bits);
651}754}
652755
653static inline bool zig_subo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {756static inline bool zig_subo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) {
654#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)757#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
655 zig_u64 full_res;758 uint64_t full_res;
656 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);759 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
657 *res = zig_wrap_u64(full_res, bits);760 *res = zig_wrap_u64(full_res, bits);
658 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);761 return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits);
659#else762#else
660 *res = zig_subw_u64(lhs, rhs, bits);763 *res = zig_subw_u64(lhs, rhs, bits);
661 return *res > lhs;764 return *res > lhs;
662#endif765#endif
663}766}
664767
665static inline void zig_vsubo_u64(zig_u8 *ov, zig_u64 *res, int n,768static inline void zig_vsubo_u64(uint8_t *ov, uint64_t *res, int n,
666 const zig_u64 *lhs, const zig_u64 *rhs, zig_u8 bits)769 const uint64_t *lhs, const uint64_t *rhs, uint8_t bits)
667{770{
668 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u64(&res[i], lhs[i], rhs[i], bits);771 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u64(&res[i], lhs[i], rhs[i], bits);
669}772}
670773
671zig_extern zig_i64 __subodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);774zig_extern int64_t __subodi4(int64_t lhs, int64_t rhs, int *overflow);
672static inline bool zig_subo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {775static inline bool zig_subo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
673#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)776#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
674 zig_i64 full_res;777 int64_t full_res;
675 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);778 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
676#else779#else
677 zig_c_int overflow_int;780 int overflow_int;
678 zig_i64 full_res = __subodi4(lhs, rhs, &overflow_int);781 int64_t full_res = __subodi4(lhs, rhs, &overflow_int);
679 bool overflow = overflow_int != 0;782 bool overflow = overflow_int != 0;
680#endif783#endif
681 *res = zig_wrap_i64(full_res, bits);784 *res = zig_wrap_i64(full_res, bits);
682 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);785 return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits);
683}786}
684787
685static inline void zig_vsubo_i64(zig_u8 *ov, zig_i64 *res, int n,788static inline void zig_vsubo_i64(uint8_t *ov, int64_t *res, int n,
686 const zig_i64 *lhs, const zig_i64 *rhs, zig_u8 bits)789 const int64_t *lhs, const int64_t *rhs, uint8_t bits)
687{790{
688 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i64(&res[i], lhs[i], rhs[i], bits);791 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i64(&res[i], lhs[i], rhs[i], bits);
689}792}
690793
691static inline bool zig_subo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {794static inline bool zig_subo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) {
692#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)795#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
693 zig_u8 full_res;796 uint8_t full_res;
694 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);797 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
695 *res = zig_wrap_u8(full_res, bits);798 *res = zig_wrap_u8(full_res, bits);
696 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);799 return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits);
697#else800#else
698 zig_u32 full_res;801 uint32_t full_res;
699 bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits);802 bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits);
700 *res = (zig_u8)full_res;803 *res = (uint8_t)full_res;
701 return overflow;804 return overflow;
702#endif805#endif
703}806}
704807
705static inline void zig_vsubo_u8(zig_u8 *ov, zig_u8 *res, int n,808static inline void zig_vsubo_u8(uint8_t *ov, uint8_t *res, int n,
706 const zig_u8 *lhs, const zig_u8 *rhs, zig_u8 bits)809 const uint8_t *lhs, const uint8_t *rhs, uint8_t bits)
707{810{
708 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u8(&res[i], lhs[i], rhs[i], bits);811 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u8(&res[i], lhs[i], rhs[i], bits);
709}812}
710813
711static inline bool zig_subo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {814static inline bool zig_subo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits) {
712#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)815#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
713 zig_i8 full_res;816 int8_t full_res;
714 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);817 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
715 *res = zig_wrap_i8(full_res, bits);818 *res = zig_wrap_i8(full_res, bits);
716 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);819 return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits);
717#else820#else
718 zig_i32 full_res;821 int32_t full_res;
719 bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits);822 bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits);
720 *res = (zig_i8)full_res;823 *res = (int8_t)full_res;
721 return overflow;824 return overflow;
722#endif825#endif
723}826}
724827
725static inline void zig_vsubo_i8(zig_u8 *ov, zig_i8 *res, int n,828static inline void zig_vsubo_i8(uint8_t *ov, int8_t *res, int n,
726 const zig_i8 *lhs, const zig_i8 *rhs, zig_u8 bits)829 const int8_t *lhs, const int8_t *rhs, uint8_t bits)
727{830{
728 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i8(&res[i], lhs[i], rhs[i], bits);831 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i8(&res[i], lhs[i], rhs[i], bits);
729}832}
730833
731834
732static inline bool zig_subo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {835static inline bool zig_subo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8_t bits) {
733#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)836#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
734 zig_u16 full_res;837 uint16_t full_res;
735 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);838 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
736 *res = zig_wrap_u16(full_res, bits);839 *res = zig_wrap_u16(full_res, bits);
737 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);840 return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits);
738#else841#else
739 zig_u32 full_res;842 uint32_t full_res;
740 bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits);843 bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits);
741 *res = (zig_u16)full_res;844 *res = (uint16_t)full_res;
742 return overflow;845 return overflow;
743#endif846#endif
744}847}
745848
746static inline void zig_vsubo_u16(zig_u8 *ov, zig_u16 *res, int n,849static inline void zig_vsubo_u16(uint8_t *ov, uint16_t *res, int n,
747 const zig_u16 *lhs, const zig_u16 *rhs, zig_u8 bits)850 const uint16_t *lhs, const uint16_t *rhs, uint8_t bits)
748{851{
749 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u16(&res[i], lhs[i], rhs[i], bits);852 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u16(&res[i], lhs[i], rhs[i], bits);
750}853}
751854
752855
753static inline bool zig_subo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {856static inline bool zig_subo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t bits) {
754#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)857#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
755 zig_i16 full_res;858 int16_t full_res;
756 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);859 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
757 *res = zig_wrap_i16(full_res, bits);860 *res = zig_wrap_i16(full_res, bits);
758 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);861 return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits);
759#else862#else
760 zig_i32 full_res;863 int32_t full_res;
761 bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits);864 bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits);
762 *res = (zig_i16)full_res;865 *res = (int16_t)full_res;
763 return overflow;866 return overflow;
764#endif867#endif
765}868}
766869
767static inline void zig_vsubo_i16(zig_u8 *ov, zig_i16 *res, int n,870static inline void zig_vsubo_i16(uint8_t *ov, int16_t *res, int n,
768 const zig_i16 *lhs, const zig_i16 *rhs, zig_u8 bits)871 const int16_t *lhs, const int16_t *rhs, uint8_t bits)
769{872{
770 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i16(&res[i], lhs[i], rhs[i], bits);873 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i16(&res[i], lhs[i], rhs[i], bits);
771}874}
772875
773static inline bool zig_mulo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {876static inline bool zig_mulo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) {
774#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)877#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
775 zig_u32 full_res;878 uint32_t full_res;
776 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);879 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
777 *res = zig_wrap_u32(full_res, bits);880 *res = zig_wrap_u32(full_res, bits);
778 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);881 return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits);
779#else882#else
780 *res = zig_mulw_u32(lhs, rhs, bits);883 *res = zig_mulw_u32(lhs, rhs, bits);
781 return rhs != zig_as_u32(0) && lhs > zig_maxInt(u32, bits) / rhs;884 return rhs != UINT32_C(0) && lhs > zig_maxInt_u(32, bits) / rhs;
782#endif885#endif
783}886}
784887
785static inline void zig_vmulo_u32(zig_u8 *ov, zig_u32 *res, int n,888static inline void zig_vmulo_u32(uint8_t *ov, uint32_t *res, int n,
786 const zig_u32 *lhs, const zig_u32 *rhs, zig_u8 bits)889 const uint32_t *lhs, const uint32_t *rhs, uint8_t bits)
787{890{
788 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u32(&res[i], lhs[i], rhs[i], bits);891 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u32(&res[i], lhs[i], rhs[i], bits);
789}892}
790893
791zig_extern zig_i32 __mulosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);894zig_extern int32_t __mulosi4(int32_t lhs, int32_t rhs, int *overflow);
792static inline bool zig_mulo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {895static inline bool zig_mulo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
793#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)896#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
794 zig_i32 full_res;897 int32_t full_res;
795 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);898 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
796#else899#else
797 zig_c_int overflow_int;900 int overflow_int;
798 zig_i32 full_res = __mulosi4(lhs, rhs, &overflow_int);901 int32_t full_res = __mulosi4(lhs, rhs, &overflow_int);
799 bool overflow = overflow_int != 0;902 bool overflow = overflow_int != 0;
800#endif903#endif
801 *res = zig_wrap_i32(full_res, bits);904 *res = zig_wrap_i32(full_res, bits);
802 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);905 return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits);
803}906}
804907
805static inline void zig_vmulo_i32(zig_u8 *ov, zig_i32 *res, int n,908static inline void zig_vmulo_i32(uint8_t *ov, int32_t *res, int n,
806 const zig_i32 *lhs, const zig_i32 *rhs, zig_u8 bits)909 const int32_t *lhs, const int32_t *rhs, uint8_t bits)
807{910{
808 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i32(&res[i], lhs[i], rhs[i], bits);911 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i32(&res[i], lhs[i], rhs[i], bits);
809}912}
810913
811static inline bool zig_mulo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {914static inline bool zig_mulo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) {
812#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)915#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
813 zig_u64 full_res;916 uint64_t full_res;
814 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);917 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
815 *res = zig_wrap_u64(full_res, bits);918 *res = zig_wrap_u64(full_res, bits);
816 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);919 return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits);
817#else920#else
818 *res = zig_mulw_u64(lhs, rhs, bits);921 *res = zig_mulw_u64(lhs, rhs, bits);
819 return rhs != zig_as_u64(0) && lhs > zig_maxInt(u64, bits) / rhs;922 return rhs != UINT64_C(0) && lhs > zig_maxInt_u(64, bits) / rhs;
820#endif923#endif
821}924}
822925
823static inline void zig_vmulo_u64(zig_u8 *ov, zig_u64 *res, int n,926static inline void zig_vmulo_u64(uint8_t *ov, uint64_t *res, int n,
824 const zig_u64 *lhs, const zig_u64 *rhs, zig_u8 bits)927 const uint64_t *lhs, const uint64_t *rhs, uint8_t bits)
825{928{
826 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u64(&res[i], lhs[i], rhs[i], bits);929 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u64(&res[i], lhs[i], rhs[i], bits);
827}930}
828931
829zig_extern zig_i64 __mulodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);932zig_extern int64_t __mulodi4(int64_t lhs, int64_t rhs, int *overflow);
830static inline bool zig_mulo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {933static inline bool zig_mulo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
831#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)934#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
832 zig_i64 full_res;935 int64_t full_res;
833 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);936 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
834#else937#else
835 zig_c_int overflow_int;938 int overflow_int;
836 zig_i64 full_res = __mulodi4(lhs, rhs, &overflow_int);939 int64_t full_res = __mulodi4(lhs, rhs, &overflow_int);
837 bool overflow = overflow_int != 0;940 bool overflow = overflow_int != 0;
838#endif941#endif
839 *res = zig_wrap_i64(full_res, bits);942 *res = zig_wrap_i64(full_res, bits);
840 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);943 return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits);
841}944}
842945
843static inline void zig_vmulo_i64(zig_u8 *ov, zig_i64 *res, int n,946static inline void zig_vmulo_i64(uint8_t *ov, int64_t *res, int n,
844 const zig_i64 *lhs, const zig_i64 *rhs, zig_u8 bits)947 const int64_t *lhs, const int64_t *rhs, uint8_t bits)
845{948{
846 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i64(&res[i], lhs[i], rhs[i], bits);949 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i64(&res[i], lhs[i], rhs[i], bits);
847}950}
848951
849static inline bool zig_mulo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {952static inline bool zig_mulo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) {
850#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)953#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
851 zig_u8 full_res;954 uint8_t full_res;
852 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);955 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
853 *res = zig_wrap_u8(full_res, bits);956 *res = zig_wrap_u8(full_res, bits);
854 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);957 return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits);
855#else958#else
856 zig_u32 full_res;959 uint32_t full_res;
857 bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits);960 bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits);
858 *res = (zig_u8)full_res;961 *res = (uint8_t)full_res;
859 return overflow;962 return overflow;
860#endif963#endif
861}964}
862965
863static inline void zig_vmulo_u8(zig_u8 *ov, zig_u8 *res, int n,966static inline void zig_vmulo_u8(uint8_t *ov, uint8_t *res, int n,
864 const zig_u8 *lhs, const zig_u8 *rhs, zig_u8 bits)967 const uint8_t *lhs, const uint8_t *rhs, uint8_t bits)
865{968{
866 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u8(&res[i], lhs[i], rhs[i], bits);969 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u8(&res[i], lhs[i], rhs[i], bits);
867}970}
868971
869static inline bool zig_mulo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {972static inline bool zig_mulo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits) {
870#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)973#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
871 zig_i8 full_res;974 int8_t full_res;
872 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);975 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
873 *res = zig_wrap_i8(full_res, bits);976 *res = zig_wrap_i8(full_res, bits);
874 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);977 return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits);
875#else978#else
876 zig_i32 full_res;979 int32_t full_res;
877 bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits);980 bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits);
878 *res = (zig_i8)full_res;981 *res = (int8_t)full_res;
879 return overflow;982 return overflow;
880#endif983#endif
881}984}
882985
883static inline void zig_vmulo_i8(zig_u8 *ov, zig_i8 *res, int n,986static inline void zig_vmulo_i8(uint8_t *ov, int8_t *res, int n,
884 const zig_i8 *lhs, const zig_i8 *rhs, zig_u8 bits)987 const int8_t *lhs, const int8_t *rhs, uint8_t bits)
885{988{
886 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i8(&res[i], lhs[i], rhs[i], bits);989 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i8(&res[i], lhs[i], rhs[i], bits);
887}990}
888991
889static inline bool zig_mulo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {992static inline bool zig_mulo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8_t bits) {
890#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)993#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
891 zig_u16 full_res;994 uint16_t full_res;
892 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);995 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
893 *res = zig_wrap_u16(full_res, bits);996 *res = zig_wrap_u16(full_res, bits);
894 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);997 return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits);
895#else998#else
896 zig_u32 full_res;999 uint32_t full_res;
897 bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits);1000 bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits);
898 *res = (zig_u16)full_res;1001 *res = (uint16_t)full_res;
899 return overflow;1002 return overflow;
900#endif1003#endif
901}1004}
9021005
903static inline void zig_vmulo_u16(zig_u8 *ov, zig_u16 *res, int n,1006static inline void zig_vmulo_u16(uint8_t *ov, uint16_t *res, int n,
904 const zig_u16 *lhs, const zig_u16 *rhs, zig_u8 bits)1007 const uint16_t *lhs, const uint16_t *rhs, uint8_t bits)
905{1008{
906 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u16(&res[i], lhs[i], rhs[i], bits);1009 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u16(&res[i], lhs[i], rhs[i], bits);
907}1010}
9081011
909static inline bool zig_mulo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {1012static inline bool zig_mulo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t bits) {
910#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)1013#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
911 zig_i16 full_res;1014 int16_t full_res;
912 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);1015 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
913 *res = zig_wrap_i16(full_res, bits);1016 *res = zig_wrap_i16(full_res, bits);
914 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);1017 return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits);
915#else1018#else
916 zig_i32 full_res;1019 int32_t full_res;
917 bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits);1020 bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits);
918 *res = (zig_i16)full_res;1021 *res = (int16_t)full_res;
919 return overflow;1022 return overflow;
920#endif1023#endif
921}1024}
9221025
923static inline void zig_vmulo_i16(zig_u8 *ov, zig_i16 *res, int n,1026static inline void zig_vmulo_i16(uint8_t *ov, int16_t *res, int n,
924 const zig_i16 *lhs, const zig_i16 *rhs, zig_u8 bits)1027 const int16_t *lhs, const int16_t *rhs, uint8_t bits)
925{1028{
926 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i16(&res[i], lhs[i], rhs[i], bits);1029 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i16(&res[i], lhs[i], rhs[i], bits);
927}1030}
9281031
929#define zig_int_builtins(w) \1032#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) { \1033 static inline bool zig_shlo_u##w(uint##w##_t *res, uint##w##_t lhs, uint8_t rhs, uint8_t bits) { \
931 *res = zig_shlw_u##w(lhs, rhs, bits); \1034 *res = zig_shlw_u##w(lhs, rhs, bits); \
932 return lhs > zig_maxInt(u##w, bits) >> rhs; \1035 return lhs > zig_maxInt_u(w, bits) >> rhs; \
933 } \1036 } \
934\1037\
935 static inline bool zig_shlo_i##w(zig_i##w *res, zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \1038 static inline bool zig_shlo_i##w(int##w##_t *res, int##w##_t lhs, uint8_t rhs, uint8_t bits) { \
936 *res = zig_shlw_i##w(lhs, rhs, bits); \1039 *res = zig_shlw_i##w(lhs, rhs, bits); \
937 zig_i##w mask = (zig_i##w)(zig_maxInt_u##w << (bits - rhs - 1)); \1040 int##w##_t mask = (int##w##_t)(UINT##w##_MAX << (bits - rhs - 1)); \
938 return (lhs & mask) != zig_as_i##w(0) && (lhs & mask) != mask; \1041 return (lhs & mask) != INT##w##_C(0) && (lhs & mask) != mask; \
939 } \1042 } \
940\1043\
941 static inline zig_u##w zig_shls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \1044 static inline uint##w##_t zig_shls_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
942 zig_u##w res; \1045 uint##w##_t res; \
943 if (rhs >= bits) return lhs != zig_as_u##w(0) ? zig_maxInt(u##w, bits) : lhs; \1046 if (rhs >= bits) return lhs != UINT##w##_C(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; \1047 return zig_shlo_u##w(&res, lhs, (uint8_t)rhs, bits) ? zig_maxInt_u(w, bits) : res; \
945 } \1048 } \
946\1049\
947 static inline zig_i##w zig_shls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \1050 static inline int##w##_t zig_shls_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
948 zig_i##w res; \1051 int##w##_t res; \
949 if ((zig_u##w)rhs < (zig_u##w)bits && !zig_shlo_i##w(&res, lhs, rhs, bits)) return res; \1052 if ((uint##w##_t)rhs < (uint##w##_t)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); \1053 return lhs < INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \
951 } \1054 } \
952\1055\
953 static inline zig_u##w zig_adds_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \1056 static inline uint##w##_t zig_adds_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
954 zig_u##w res; \1057 uint##w##_t res; \
955 return zig_addo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \1058 return zig_addo_u##w(&res, lhs, rhs, bits) ? zig_maxInt_u(w, bits) : res; \
956 } \1059 } \
957\1060\
958 static inline zig_i##w zig_adds_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \1061 static inline int##w##_t zig_adds_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
959 zig_i##w res; \1062 int##w##_t res; \
960 if (!zig_addo_i##w(&res, lhs, rhs, bits)) return res; \1063 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); \1064 return res >= INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \
962 } \1065 } \
963\1066\
964 static inline zig_u##w zig_subs_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \1067 static inline uint##w##_t zig_subs_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
965 zig_u##w res; \1068 uint##w##_t res; \
966 return zig_subo_u##w(&res, lhs, rhs, bits) ? zig_minInt(u##w, bits) : res; \1069 return zig_subo_u##w(&res, lhs, rhs, bits) ? zig_minInt_u(w, bits) : res; \
967 } \1070 } \
968\1071\
969 static inline zig_i##w zig_subs_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \1072 static inline int##w##_t zig_subs_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
970 zig_i##w res; \1073 int##w##_t res; \
971 if (!zig_subo_i##w(&res, lhs, rhs, bits)) return res; \1074 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); \1075 return res >= INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \
973 } \1076 } \
974\1077\
975 static inline zig_u##w zig_muls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \1078 static inline uint##w##_t zig_muls_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
976 zig_u##w res; \1079 uint##w##_t res; \
977 return zig_mulo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \1080 return zig_mulo_u##w(&res, lhs, rhs, bits) ? zig_maxInt_u(w, bits) : res; \
978 } \1081 } \
979\1082\
980 static inline zig_i##w zig_muls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \1083 static inline int##w##_t zig_muls_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
981 zig_i##w res; \1084 int##w##_t res; \
982 if (!zig_mulo_i##w(&res, lhs, rhs, bits)) return res; \1085 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); \1086 return (lhs ^ rhs) < INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \
984 }1087 }
985zig_int_builtins(8)1088zig_int_builtins(8)
986zig_int_builtins(16)1089zig_int_builtins(16)
...@@ -988,89 +1091,89 @@ zig_int_builtins(32)...@@ -988,89 +1091,89 @@ zig_int_builtins(32)
988zig_int_builtins(64)1091zig_int_builtins(64)
9891092
990#define zig_builtin8(name, val) __builtin_##name(val)1093#define zig_builtin8(name, val) __builtin_##name(val)
991typedef zig_c_uint zig_Builtin8;1094typedef unsigned int zig_Builtin8;
9921095
993#define zig_builtin16(name, val) __builtin_##name(val)1096#define zig_builtin16(name, val) __builtin_##name(val)
994typedef zig_c_uint zig_Builtin16;1097typedef unsigned int zig_Builtin16;
9951098
996#if INT_MIN <= INT32_MIN1099#if INT_MIN <= INT32_MIN
997#define zig_builtin32(name, val) __builtin_##name(val)1100#define zig_builtin32(name, val) __builtin_##name(val)
998typedef zig_c_uint zig_Builtin32;1101typedef unsigned int zig_Builtin32;
999#elif LONG_MIN <= INT32_MIN1102#elif LONG_MIN <= INT32_MIN
1000#define zig_builtin32(name, val) __builtin_##name##l(val)1103#define zig_builtin32(name, val) __builtin_##name##l(val)
1001typedef zig_c_ulong zig_Builtin32;1104typedef unsigned long zig_Builtin32;
1002#endif1105#endif
10031106
1004#if INT_MIN <= INT64_MIN1107#if INT_MIN <= INT64_MIN
1005#define zig_builtin64(name, val) __builtin_##name(val)1108#define zig_builtin64(name, val) __builtin_##name(val)
1006typedef zig_c_uint zig_Builtin64;1109typedef unsigned int zig_Builtin64;
1007#elif LONG_MIN <= INT64_MIN1110#elif LONG_MIN <= INT64_MIN
1008#define zig_builtin64(name, val) __builtin_##name##l(val)1111#define zig_builtin64(name, val) __builtin_##name##l(val)
1009typedef zig_c_ulong zig_Builtin64;1112typedef unsigned long zig_Builtin64;
1010#elif LLONG_MIN <= INT64_MIN1113#elif LLONG_MIN <= INT64_MIN
1011#define zig_builtin64(name, val) __builtin_##name##ll(val)1114#define zig_builtin64(name, val) __builtin_##name##ll(val)
1012typedef zig_c_ulonglong zig_Builtin64;1115typedef unsigned long long zig_Builtin64;
1013#endif1116#endif
10141117
1015static inline zig_u8 zig_byte_swap_u8(zig_u8 val, zig_u8 bits) {1118static inline uint8_t zig_byte_swap_u8(uint8_t val, uint8_t bits) {
1016 return zig_wrap_u8(val >> (8 - bits), bits);1119 return zig_wrap_u8(val >> (8 - bits), bits);
1017}1120}
10181121
1019static inline zig_i8 zig_byte_swap_i8(zig_i8 val, zig_u8 bits) {1122static inline int8_t zig_byte_swap_i8(int8_t val, uint8_t bits) {
1020 return zig_wrap_i8((zig_i8)zig_byte_swap_u8((zig_u8)val, bits), bits);1123 return zig_wrap_i8((int8_t)zig_byte_swap_u8((uint8_t)val, bits), bits);
1021}1124}
10221125
1023static inline zig_u16 zig_byte_swap_u16(zig_u16 val, zig_u8 bits) {1126static inline uint16_t zig_byte_swap_u16(uint16_t val, uint8_t bits) {
1024 zig_u16 full_res;1127 uint16_t full_res;
1025#if zig_has_builtin(bswap16) || defined(zig_gnuc)1128#if zig_has_builtin(bswap16) || defined(zig_gnuc)
1026 full_res = __builtin_bswap16(val);1129 full_res = __builtin_bswap16(val);
1027#else1130#else
1028 full_res = (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 0), 8) << 8 |1131 full_res = (uint16_t)zig_byte_swap_u8((uint8_t)(val >> 0), 8) << 8 |
1029 (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 8), 8) >> 0;1132 (uint16_t)zig_byte_swap_u8((uint8_t)(val >> 8), 8) >> 0;
1030#endif1133#endif
1031 return zig_wrap_u16(full_res >> (16 - bits), bits);1134 return zig_wrap_u16(full_res >> (16 - bits), bits);
1032}1135}
10331136
1034static inline zig_i16 zig_byte_swap_i16(zig_i16 val, zig_u8 bits) {1137static inline int16_t zig_byte_swap_i16(int16_t val, uint8_t bits) {
1035 return zig_wrap_i16((zig_i16)zig_byte_swap_u16((zig_u16)val, bits), bits);1138 return zig_wrap_i16((int16_t)zig_byte_swap_u16((uint16_t)val, bits), bits);
1036}1139}
10371140
1038static inline zig_u32 zig_byte_swap_u32(zig_u32 val, zig_u8 bits) {1141static inline uint32_t zig_byte_swap_u32(uint32_t val, uint8_t bits) {
1039 zig_u32 full_res;1142 uint32_t full_res;
1040#if zig_has_builtin(bswap32) || defined(zig_gnuc)1143#if zig_has_builtin(bswap32) || defined(zig_gnuc)
1041 full_res = __builtin_bswap32(val);1144 full_res = __builtin_bswap32(val);
1042#else1145#else
1043 full_res = (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 0), 16) << 16 |1146 full_res = (uint32_t)zig_byte_swap_u16((uint16_t)(val >> 0), 16) << 16 |
1044 (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 16), 16) >> 0;1147 (uint32_t)zig_byte_swap_u16((uint16_t)(val >> 16), 16) >> 0;
1045#endif1148#endif
1046 return zig_wrap_u32(full_res >> (32 - bits), bits);1149 return zig_wrap_u32(full_res >> (32 - bits), bits);
1047}1150}
10481151
1049static inline zig_i32 zig_byte_swap_i32(zig_i32 val, zig_u8 bits) {1152static inline int32_t zig_byte_swap_i32(int32_t val, uint8_t bits) {
1050 return zig_wrap_i32((zig_i32)zig_byte_swap_u32((zig_u32)val, bits), bits);1153 return zig_wrap_i32((int32_t)zig_byte_swap_u32((uint32_t)val, bits), bits);
1051}1154}
10521155
1053static inline zig_u64 zig_byte_swap_u64(zig_u64 val, zig_u8 bits) {1156static inline uint64_t zig_byte_swap_u64(uint64_t val, uint8_t bits) {
1054 zig_u64 full_res;1157 uint64_t full_res;
1055#if zig_has_builtin(bswap64) || defined(zig_gnuc)1158#if zig_has_builtin(bswap64) || defined(zig_gnuc)
1056 full_res = __builtin_bswap64(val);1159 full_res = __builtin_bswap64(val);
1057#else1160#else
1058 full_res = (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 0), 32) << 32 |1161 full_res = (uint64_t)zig_byte_swap_u32((uint32_t)(val >> 0), 32) << 32 |
1059 (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 32), 32) >> 0;1162 (uint64_t)zig_byte_swap_u32((uint32_t)(val >> 32), 32) >> 0;
1060#endif1163#endif
1061 return zig_wrap_u64(full_res >> (64 - bits), bits);1164 return zig_wrap_u64(full_res >> (64 - bits), bits);
1062}1165}
10631166
1064static inline zig_i64 zig_byte_swap_i64(zig_i64 val, zig_u8 bits) {1167static inline int64_t zig_byte_swap_i64(int64_t val, uint8_t bits) {
1065 return zig_wrap_i64((zig_i64)zig_byte_swap_u64((zig_u64)val, bits), bits);1168 return zig_wrap_i64((int64_t)zig_byte_swap_u64((uint64_t)val, bits), bits);
1066}1169}
10671170
1068static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) {1171static inline uint8_t zig_bit_reverse_u8(uint8_t val, uint8_t bits) {
1069 zig_u8 full_res;1172 uint8_t full_res;
1070#if zig_has_builtin(bitreverse8)1173#if zig_has_builtin(bitreverse8)
1071 full_res = __builtin_bitreverse8(val);1174 full_res = __builtin_bitreverse8(val);
1072#else1175#else
1073 static zig_u8 const lut[0x10] = {1176 static uint8_t const lut[0x10] = {
1074 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,1177 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
1075 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf1178 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
1076 };1179 };
...@@ -1079,62 +1182,62 @@ static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) {...@@ -1079,62 +1182,62 @@ static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) {
1079 return zig_wrap_u8(full_res >> (8 - bits), bits);1182 return zig_wrap_u8(full_res >> (8 - bits), bits);
1080}1183}
10811184
1082static inline zig_i8 zig_bit_reverse_i8(zig_i8 val, zig_u8 bits) {1185static inline int8_t zig_bit_reverse_i8(int8_t val, uint8_t bits) {
1083 return zig_wrap_i8((zig_i8)zig_bit_reverse_u8((zig_u8)val, bits), bits);1186 return zig_wrap_i8((int8_t)zig_bit_reverse_u8((uint8_t)val, bits), bits);
1084}1187}
10851188
1086static inline zig_u16 zig_bit_reverse_u16(zig_u16 val, zig_u8 bits) {1189static inline uint16_t zig_bit_reverse_u16(uint16_t val, uint8_t bits) {
1087 zig_u16 full_res;1190 uint16_t full_res;
1088#if zig_has_builtin(bitreverse16)1191#if zig_has_builtin(bitreverse16)
1089 full_res = __builtin_bitreverse16(val);1192 full_res = __builtin_bitreverse16(val);
1090#else1193#else
1091 full_res = (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 0), 8) << 8 |1194 full_res = (uint16_t)zig_bit_reverse_u8((uint8_t)(val >> 0), 8) << 8 |
1092 (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 8), 8) >> 0;1195 (uint16_t)zig_bit_reverse_u8((uint8_t)(val >> 8), 8) >> 0;
1093#endif1196#endif
1094 return zig_wrap_u16(full_res >> (16 - bits), bits);1197 return zig_wrap_u16(full_res >> (16 - bits), bits);
1095}1198}
10961199
1097static inline zig_i16 zig_bit_reverse_i16(zig_i16 val, zig_u8 bits) {1200static inline int16_t zig_bit_reverse_i16(int16_t val, uint8_t bits) {
1098 return zig_wrap_i16((zig_i16)zig_bit_reverse_u16((zig_u16)val, bits), bits);1201 return zig_wrap_i16((int16_t)zig_bit_reverse_u16((uint16_t)val, bits), bits);
1099}1202}
11001203
1101static inline zig_u32 zig_bit_reverse_u32(zig_u32 val, zig_u8 bits) {1204static inline uint32_t zig_bit_reverse_u32(uint32_t val, uint8_t bits) {
1102 zig_u32 full_res;1205 uint32_t full_res;
1103#if zig_has_builtin(bitreverse32)1206#if zig_has_builtin(bitreverse32)
1104 full_res = __builtin_bitreverse32(val);1207 full_res = __builtin_bitreverse32(val);
1105#else1208#else
1106 full_res = (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 0), 16) << 16 |1209 full_res = (uint32_t)zig_bit_reverse_u16((uint16_t)(val >> 0), 16) << 16 |
1107 (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 16), 16) >> 0;1210 (uint32_t)zig_bit_reverse_u16((uint16_t)(val >> 16), 16) >> 0;
1108#endif1211#endif
1109 return zig_wrap_u32(full_res >> (32 - bits), bits);1212 return zig_wrap_u32(full_res >> (32 - bits), bits);
1110}1213}
11111214
1112static inline zig_i32 zig_bit_reverse_i32(zig_i32 val, zig_u8 bits) {1215static inline int32_t zig_bit_reverse_i32(int32_t val, uint8_t bits) {
1113 return zig_wrap_i32((zig_i32)zig_bit_reverse_u32((zig_u32)val, bits), bits);1216 return zig_wrap_i32((int32_t)zig_bit_reverse_u32((uint32_t)val, bits), bits);
1114}1217}
11151218
1116static inline zig_u64 zig_bit_reverse_u64(zig_u64 val, zig_u8 bits) {1219static inline uint64_t zig_bit_reverse_u64(uint64_t val, uint8_t bits) {
1117 zig_u64 full_res;1220 uint64_t full_res;
1118#if zig_has_builtin(bitreverse64)1221#if zig_has_builtin(bitreverse64)
1119 full_res = __builtin_bitreverse64(val);1222 full_res = __builtin_bitreverse64(val);
1120#else1223#else
1121 full_res = (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 0), 32) << 32 |1224 full_res = (uint64_t)zig_bit_reverse_u32((uint32_t)(val >> 0), 32) << 32 |
1122 (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 32), 32) >> 0;1225 (uint64_t)zig_bit_reverse_u32((uint32_t)(val >> 32), 32) >> 0;
1123#endif1226#endif
1124 return zig_wrap_u64(full_res >> (64 - bits), bits);1227 return zig_wrap_u64(full_res >> (64 - bits), bits);
1125}1228}
11261229
1127static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) {1230static inline int64_t zig_bit_reverse_i64(int64_t val, uint8_t bits) {
1128 return zig_wrap_i64((zig_i64)zig_bit_reverse_u64((zig_u64)val, bits), bits);1231 return zig_wrap_i64((int64_t)zig_bit_reverse_u64((uint64_t)val, bits), bits);
1129}1232}
11301233
1131#define zig_builtin_popcount_common(w) \1234#define zig_builtin_popcount_common(w) \
1132 static inline zig_u8 zig_popcount_i##w(zig_i##w val, zig_u8 bits) { \1235 static inline uint8_t zig_popcount_i##w(int##w##_t val, uint8_t bits) { \
1133 return zig_popcount_u##w((zig_u##w)val, bits); \1236 return zig_popcount_u##w((uint##w##_t)val, bits); \
1134 }1237 }
1135#if zig_has_builtin(popcount) || defined(zig_gnuc)1238#if zig_has_builtin(popcount) || defined(zig_gnuc)
1136#define zig_builtin_popcount(w) \1239#define zig_builtin_popcount(w) \
1137 static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \1240 static inline uint8_t zig_popcount_u##w(uint##w##_t val, uint8_t bits) { \
1138 (void)bits; \1241 (void)bits; \
1139 return zig_builtin##w(popcount, val); \1242 return zig_builtin##w(popcount, val); \
1140 } \1243 } \
...@@ -1142,12 +1245,12 @@ static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) {...@@ -1142,12 +1245,12 @@ static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) {
1142 zig_builtin_popcount_common(w)1245 zig_builtin_popcount_common(w)
1143#else1246#else
1144#define zig_builtin_popcount(w) \1247#define zig_builtin_popcount(w) \
1145 static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \1248 static inline uint8_t zig_popcount_u##w(uint##w##_t val, uint8_t bits) { \
1146 (void)bits; \1249 (void)bits; \
1147 zig_u##w temp = val - ((val >> 1) & (zig_maxInt_u##w / 3)); \1250 uint##w##_t temp = val - ((val >> 1) & (UINT##w##_MAX / 3)); \
1148 temp = (temp & (zig_maxInt_u##w / 5)) + ((temp >> 2) & (zig_maxInt_u##w / 5)); \1251 temp = (temp & (UINT##w##_MAX / 5)) + ((temp >> 2) & (UINT##w##_MAX / 5)); \
1149 temp = (temp + (temp >> 4)) & (zig_maxInt_u##w / 17); \1252 temp = (temp + (temp >> 4)) & (UINT##w##_MAX / 17); \
1150 return temp * (zig_maxInt_u##w / 255) >> (w - 8); \1253 return temp * (UINT##w##_MAX / 255) >> (w - 8); \
1151 } \1254 } \
1152\1255\
1153 zig_builtin_popcount_common(w)1256 zig_builtin_popcount_common(w)
...@@ -1158,12 +1261,12 @@ zig_builtin_popcount(32)...@@ -1158,12 +1261,12 @@ zig_builtin_popcount(32)
1158zig_builtin_popcount(64)1261zig_builtin_popcount(64)
11591262
1160#define zig_builtin_ctz_common(w) \1263#define zig_builtin_ctz_common(w) \
1161 static inline zig_u8 zig_ctz_i##w(zig_i##w val, zig_u8 bits) { \1264 static inline uint8_t zig_ctz_i##w(int##w##_t val, uint8_t bits) { \
1162 return zig_ctz_u##w((zig_u##w)val, bits); \1265 return zig_ctz_u##w((uint##w##_t)val, bits); \
1163 }1266 }
1164#if zig_has_builtin(ctz) || defined(zig_gnuc)1267#if zig_has_builtin(ctz) || defined(zig_gnuc)
1165#define zig_builtin_ctz(w) \1268#define zig_builtin_ctz(w) \
1166 static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \1269 static inline uint8_t zig_ctz_u##w(uint##w##_t val, uint8_t bits) { \
1167 if (val == 0) return bits; \1270 if (val == 0) return bits; \
1168 return zig_builtin##w(ctz, val); \1271 return zig_builtin##w(ctz, val); \
1169 } \1272 } \
...@@ -1171,7 +1274,7 @@ zig_builtin_popcount(64)...@@ -1171,7 +1274,7 @@ zig_builtin_popcount(64)
1171 zig_builtin_ctz_common(w)1274 zig_builtin_ctz_common(w)
1172#else1275#else
1173#define zig_builtin_ctz(w) \1276#define zig_builtin_ctz(w) \
1174 static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \1277 static inline uint8_t zig_ctz_u##w(uint##w##_t val, uint8_t bits) { \
1175 return zig_popcount_u##w(zig_not_u##w(val, bits) & zig_subw_u##w(val, 1, bits), bits); \1278 return zig_popcount_u##w(zig_not_u##w(val, bits) & zig_subw_u##w(val, 1, bits), bits); \
1176 } \1279 } \
1177\1280\
...@@ -1183,12 +1286,12 @@ zig_builtin_ctz(32)...@@ -1183,12 +1286,12 @@ zig_builtin_ctz(32)
1183zig_builtin_ctz(64)1286zig_builtin_ctz(64)
11841287
1185#define zig_builtin_clz_common(w) \1288#define zig_builtin_clz_common(w) \
1186 static inline zig_u8 zig_clz_i##w(zig_i##w val, zig_u8 bits) { \1289 static inline uint8_t zig_clz_i##w(int##w##_t val, uint8_t bits) { \
1187 return zig_clz_u##w((zig_u##w)val, bits); \1290 return zig_clz_u##w((uint##w##_t)val, bits); \
1188 }1291 }
1189#if zig_has_builtin(clz) || defined(zig_gnuc)1292#if zig_has_builtin(clz) || defined(zig_gnuc)
1190#define zig_builtin_clz(w) \1293#define zig_builtin_clz(w) \
1191 static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \1294 static inline uint8_t zig_clz_u##w(uint##w##_t val, uint8_t bits) { \
1192 if (val == 0) return bits; \1295 if (val == 0) return bits; \
1193 return zig_builtin##w(clz, val) - (zig_bitSizeOf(zig_Builtin##w) - bits); \1296 return zig_builtin##w(clz, val) - (zig_bitSizeOf(zig_Builtin##w) - bits); \
1194 } \1297 } \
...@@ -1196,7 +1299,7 @@ zig_builtin_ctz(64)...@@ -1196,7 +1299,7 @@ zig_builtin_ctz(64)
1196 zig_builtin_clz_common(w)1299 zig_builtin_clz_common(w)
1197#else1300#else
1198#define zig_builtin_clz(w) \1301#define zig_builtin_clz(w) \
1199 static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \1302 static inline uint8_t zig_clz_u##w(uint##w##_t val, uint8_t bits) { \
1200 return zig_ctz_u##w(zig_bit_reverse_u##w(val, bits), bits); \1303 return zig_ctz_u##w(zig_bit_reverse_u##w(val, bits), bits); \
1201 } \1304 } \
1202\1305\
...@@ -1207,7 +1310,7 @@ zig_builtin_clz(16)...@@ -1207,7 +1310,7 @@ zig_builtin_clz(16)
1207zig_builtin_clz(32)1310zig_builtin_clz(32)
1208zig_builtin_clz(64)1311zig_builtin_clz(64)
12091312
1210/* ======================== 128-bit Integer Routines ======================== */1313/* ======================== 128-bit Integer Support ========================= */
12111314
1212#if !defined(zig_has_int128)1315#if !defined(zig_has_int128)
1213# if defined(__SIZEOF_INT128__)1316# if defined(__SIZEOF_INT128__)
...@@ -1222,18 +1325,18 @@ zig_builtin_clz(64)...@@ -1222,18 +1325,18 @@ zig_builtin_clz(64)
1222typedef unsigned __int128 zig_u128;1325typedef unsigned __int128 zig_u128;
1223typedef signed __int128 zig_i128;1326typedef signed __int128 zig_i128;
12241327
1225#define zig_as_u128(hi, lo) ((zig_u128)(hi)<<64|(lo))1328#define zig_make_u128(hi, lo) ((zig_u128)(hi)<<64|(lo))
1226#define zig_as_i128(hi, lo) ((zig_i128)zig_as_u128(hi, lo))1329#define zig_make_i128(hi, lo) ((zig_i128)zig_make_u128(hi, lo))
1227#define zig_as_constant_u128(hi, lo) zig_as_u128(hi, lo)1330#define zig_make_constant_u128(hi, lo) zig_make_u128(hi, lo)
1228#define zig_as_constant_i128(hi, lo) zig_as_i128(hi, lo)1331#define zig_make_constant_i128(hi, lo) zig_make_i128(hi, lo)
1229#define zig_hi_u128(val) ((zig_u64)((val) >> 64))1332#define zig_hi_u128(val) ((uint64_t)((val) >> 64))
1230#define zig_lo_u128(val) ((zig_u64)((val) >> 0))1333#define zig_lo_u128(val) ((uint64_t)((val) >> 0))
1231#define zig_hi_i128(val) ((zig_i64)((val) >> 64))1334#define zig_hi_i128(val) (( int64_t)((val) >> 64))
1232#define zig_lo_i128(val) ((zig_u64)((val) >> 0))1335#define zig_lo_i128(val) ((uint64_t)((val) >> 0))
1233#define zig_bitcast_u128(val) ((zig_u128)(val))1336#define zig_bitcast_u128(val) ((zig_u128)(val))
1234#define zig_bitcast_i128(val) ((zig_i128)(val))1337#define zig_bitcast_i128(val) ((zig_i128)(val))
1235#define zig_cmp_int128(Type) \1338#define zig_cmp_int128(Type) \
1236 static inline zig_i32 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \1339 static inline int32_t zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
1237 return (lhs > rhs) - (lhs < rhs); \1340 return (lhs > rhs) - (lhs < rhs); \
1238 }1341 }
1239#define zig_bit_int128(Type, operation, operator) \1342#define zig_bit_int128(Type, operation, operator) \
...@@ -1244,31 +1347,31 @@ typedef signed __int128 zig_i128;...@@ -1244,31 +1347,31 @@ typedef signed __int128 zig_i128;
1244#else /* zig_has_int128 */1347#else /* zig_has_int128 */
12451348
1246#if __LITTLE_ENDIAN__ || _MSC_VER1349#if __LITTLE_ENDIAN__ || _MSC_VER
1247typedef struct { zig_align(16) zig_u64 lo; zig_u64 hi; } zig_u128;1350typedef struct { zig_align(16) uint64_t lo; uint64_t hi; } zig_u128;
1248typedef struct { zig_align(16) zig_u64 lo; zig_i64 hi; } zig_i128;1351typedef struct { zig_align(16) uint64_t lo; int64_t hi; } zig_i128;
1249#else1352#else
1250typedef struct { zig_align(16) zig_u64 hi; zig_u64 lo; } zig_u128;1353typedef struct { zig_align(16) uint64_t hi; uint64_t lo; } zig_u128;
1251typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128;1354typedef struct { zig_align(16) int64_t hi; uint64_t lo; } zig_i128;
1252#endif1355#endif
12531356
1254#define zig_as_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) })1357#define zig_make_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) })1358#define zig_make_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) })
12561359
1257#if _MSC_VER1360#if _MSC_VER
1258#define zig_as_constant_u128(hi, lo) { .h##i = (hi), .l##o = (lo) }1361#define zig_make_constant_u128(hi, lo) { .h##i = (hi), .l##o = (lo) }
1259#define zig_as_constant_i128(hi, lo) { .h##i = (hi), .l##o = (lo) }1362#define zig_make_constant_i128(hi, lo) { .h##i = (hi), .l##o = (lo) }
1260#else1363#else
1261#define zig_as_constant_u128(hi, lo) zig_as_u128(hi, lo)1364#define zig_make_constant_u128(hi, lo) zig_make_u128(hi, lo)
1262#define zig_as_constant_i128(hi, lo) zig_as_i128(hi, lo)1365#define zig_make_constant_i128(hi, lo) zig_make_i128(hi, lo)
1263#endif1366#endif
1264#define zig_hi_u128(val) ((val).hi)1367#define zig_hi_u128(val) ((val).hi)
1265#define zig_lo_u128(val) ((val).lo)1368#define zig_lo_u128(val) ((val).lo)
1266#define zig_hi_i128(val) ((val).hi)1369#define zig_hi_i128(val) ((val).hi)
1267#define zig_lo_i128(val) ((val).lo)1370#define zig_lo_i128(val) ((val).lo)
1268#define zig_bitcast_u128(val) zig_as_u128((zig_u64)(val).hi, (val).lo)1371#define zig_bitcast_u128(val) zig_make_u128((uint64_t)(val).hi, (val).lo)
1269#define zig_bitcast_i128(val) zig_as_i128((zig_i64)(val).hi, (val).lo)1372#define zig_bitcast_i128(val) zig_make_i128(( int64_t)(val).hi, (val).lo)
1270#define zig_cmp_int128(Type) \1373#define zig_cmp_int128(Type) \
1271 static inline zig_i32 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \1374 static inline int32_t zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
1272 return (lhs.hi == rhs.hi) \1375 return (lhs.hi == rhs.hi) \
1273 ? (lhs.lo > rhs.lo) - (lhs.lo < rhs.lo) \1376 ? (lhs.lo > rhs.lo) - (lhs.lo < rhs.lo) \
1274 : (lhs.hi > rhs.hi) - (lhs.hi < rhs.hi); \1377 : (lhs.hi > rhs.hi) - (lhs.hi < rhs.hi); \
...@@ -1280,10 +1383,10 @@ typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128;...@@ -1280,10 +1383,10 @@ typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128;
12801383
1281#endif /* zig_has_int128 */1384#endif /* zig_has_int128 */
12821385
1283#define zig_minInt_u128 zig_as_u128(zig_minInt_u64, zig_minInt_u64)1386#define zig_minInt_u128 zig_make_u128(zig_minInt_u64, zig_minInt_u64)
1284#define zig_maxInt_u128 zig_as_u128(zig_maxInt_u64, zig_maxInt_u64)1387#define zig_maxInt_u128 zig_make_u128(zig_maxInt_u64, zig_maxInt_u64)
1285#define zig_minInt_i128 zig_as_i128(zig_minInt_i64, zig_minInt_u64)1388#define zig_minInt_i128 zig_make_i128(zig_minInt_i64, zig_minInt_u64)
1286#define zig_maxInt_i128 zig_as_i128(zig_maxInt_i64, zig_maxInt_u64)1389#define zig_maxInt_i128 zig_make_i128(zig_maxInt_i64, zig_maxInt_u64)
12871390
1288zig_cmp_int128(u128)1391zig_cmp_int128(u128)
1289zig_cmp_int128(i128)1392zig_cmp_int128(i128)
...@@ -1297,28 +1400,28 @@ zig_bit_int128(i128, or, |)...@@ -1297,28 +1400,28 @@ zig_bit_int128(i128, or, |)
1297zig_bit_int128(u128, xor, ^)1400zig_bit_int128(u128, xor, ^)
1298zig_bit_int128(i128, xor, ^)1401zig_bit_int128(i128, xor, ^)
12991402
1300static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs);1403static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs);
13011404
1302#if zig_has_int1281405#if zig_has_int128
13031406
1304static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) {1407static inline zig_u128 zig_not_u128(zig_u128 val, uint8_t bits) {
1305 return val ^ zig_maxInt(u128, bits);1408 return val ^ zig_maxInt_u(128, bits);
1306}1409}
13071410
1308static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) {1411static inline zig_i128 zig_not_i128(zig_i128 val, uint8_t bits) {
1309 (void)bits;1412 (void)bits;
1310 return ~val;1413 return ~val;
1311}1414}
13121415
1313static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) {1416static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) {
1314 return lhs >> rhs;1417 return lhs >> rhs;
1315}1418}
13161419
1317static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) {1420static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) {
1318 return lhs << rhs;1421 return lhs << rhs;
1319}1422}
13201423
1321static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) {1424static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) {
1322 return lhs << rhs;1425 return lhs << rhs;
1323}1426}
13241427
...@@ -1363,40 +1466,40 @@ static inline zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {...@@ -1363,40 +1466,40 @@ static inline zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {
1363}1466}
13641467
1365static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) {1468static 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));1469 return zig_div_trunc_i128(lhs, rhs) - (((lhs ^ rhs) & zig_rem_i128(lhs, rhs)) < zig_make_i128(0, 0));
1367}1470}
13681471
1369static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {1472static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
1370 zig_i128 rem = zig_rem_i128(lhs, rhs);1473 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));1474 return rem + (((lhs ^ rhs) & rem) < zig_make_i128(0, 0) ? rhs : zig_make_i128(0, 0));
1372}1475}
13731476
1374#else /* zig_has_int128 */1477#else /* zig_has_int128 */
13751478
1376static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) {1479static inline zig_u128 zig_not_u128(zig_u128 val, uint8_t 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)) };1480 return (zig_u128){ .hi = zig_not_u64(val.hi, bits - UINT8_C(64)), .lo = zig_not_u64(val.lo, UINT8_C(64)) };
1378}1481}
13791482
1380static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) {1483static inline zig_i128 zig_not_i128(zig_i128 val, uint8_t 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)) };1484 return (zig_i128){ .hi = zig_not_i64(val.hi, bits - UINT8_C(64)), .lo = zig_not_u64(val.lo, UINT8_C(64)) };
1382}1485}
13831486
1384static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) {1487static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) {
1385 if (rhs == zig_as_u8(0)) return lhs;1488 if (rhs == UINT8_C(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)) };1489 if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = zig_minInt_u64, .lo = lhs.hi >> (rhs - UINT8_C(64)) };
1387 return (zig_u128){ .hi = lhs.hi >> rhs, .lo = lhs.hi << (zig_as_u8(64) - rhs) | lhs.lo >> rhs };1490 return (zig_u128){ .hi = lhs.hi >> rhs, .lo = lhs.hi << (UINT8_C(64) - rhs) | lhs.lo >> rhs };
1388}1491}
13891492
1390static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) {1493static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) {
1391 if (rhs == zig_as_u8(0)) return lhs;1494 if (rhs == UINT8_C(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 };1495 if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 };
1393 return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };1496 return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs };
1394}1497}
13951498
1396static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) {1499static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) {
1397 if (rhs == zig_as_u8(0)) return lhs;1500 if (rhs == UINT8_C(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 };1501 if (rhs >= UINT8_C(64)) return (zig_i128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 };
1399 return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };1502 return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs };
1400}1503}
14011504
1402static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) {1505static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) {
...@@ -1454,11 +1557,11 @@ static zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {...@@ -1454,11 +1557,11 @@ static zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {
14541557
1455static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {1558static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
1456 zig_i128 rem = zig_rem_i128(lhs, rhs);1559 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)));1560 return zig_add_i128(rem, (((lhs.hi ^ rhs.hi) & rem.hi) < INT64_C(0) ? rhs : zig_make_i128(0, 0)));
1458}1561}
14591562
1460static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) {1563static 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)));1564 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)));
1462}1565}
14631566
1464#endif /* zig_has_int128 */1567#endif /* zig_has_int128 */
...@@ -1471,161 +1574,161 @@ static inline zig_u128 zig_nand_u128(zig_u128 lhs, zig_u128 rhs) {...@@ -1471,161 +1574,161 @@ static inline zig_u128 zig_nand_u128(zig_u128 lhs, zig_u128 rhs) {
1471}1574}
14721575
1473static inline zig_u128 zig_min_u128(zig_u128 lhs, zig_u128 rhs) {1576static 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;1577 return zig_cmp_u128(lhs, rhs) < INT32_C(0) ? lhs : rhs;
1475}1578}
14761579
1477static inline zig_i128 zig_min_i128(zig_i128 lhs, zig_i128 rhs) {1580static 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;1581 return zig_cmp_i128(lhs, rhs) < INT32_C(0) ? lhs : rhs;
1479}1582}
14801583
1481static inline zig_u128 zig_max_u128(zig_u128 lhs, zig_u128 rhs) {1584static 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;1585 return zig_cmp_u128(lhs, rhs) > INT32_C(0) ? lhs : rhs;
1483}1586}
14841587
1485static inline zig_i128 zig_max_i128(zig_i128 lhs, zig_i128 rhs) {1588static 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;1589 return zig_cmp_i128(lhs, rhs) > INT32_C(0) ? lhs : rhs;
1487}1590}
14881591
1489static inline zig_i128 zig_shr_i128(zig_i128 lhs, zig_u8 rhs) {1592static inline zig_i128 zig_shr_i128(zig_i128 lhs, uint8_t 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);1593 zig_i128 sign_mask = zig_cmp_i128(lhs, zig_make_i128(0, 0)) < INT32_C(0) ? zig_sub_i128(zig_make_i128(0, 0), zig_make_i128(0, 1)) : zig_make_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);1594 return zig_xor_i128(zig_bitcast_i128(zig_shr_u128(zig_bitcast_u128(zig_xor_i128(lhs, sign_mask)), rhs)), sign_mask);
1492}1595}
14931596
1494static inline zig_u128 zig_wrap_u128(zig_u128 val, zig_u8 bits) {1597static inline zig_u128 zig_wrap_u128(zig_u128 val, uint8_t bits) {
1495 return zig_and_u128(val, zig_maxInt(u128, bits));1598 return zig_and_u128(val, zig_maxInt_u(128, bits));
1496}1599}
14971600
1498static inline zig_i128 zig_wrap_i128(zig_i128 val, zig_u8 bits) {1601static inline zig_i128 zig_wrap_i128(zig_i128 val, uint8_t bits) {
1499 return zig_as_i128(zig_wrap_i64(zig_hi_i128(val), bits - zig_as_u8(64)), zig_lo_i128(val));1602 return zig_make_i128(zig_wrap_i64(zig_hi_i128(val), bits - UINT8_C(64)), zig_lo_i128(val));
1500}1603}
15011604
1502static inline zig_u128 zig_shlw_u128(zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {1605static inline zig_u128 zig_shlw_u128(zig_u128 lhs, uint8_t rhs, uint8_t bits) {
1503 return zig_wrap_u128(zig_shl_u128(lhs, rhs), bits);1606 return zig_wrap_u128(zig_shl_u128(lhs, rhs), bits);
1504}1607}
15051608
1506static inline zig_i128 zig_shlw_i128(zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {1609static inline zig_i128 zig_shlw_i128(zig_i128 lhs, uint8_t rhs, uint8_t bits) {
1507 return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), rhs)), bits);1610 return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), rhs)), bits);
1508}1611}
15091612
1510static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1613static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1511 return zig_wrap_u128(zig_add_u128(lhs, rhs), bits);1614 return zig_wrap_u128(zig_add_u128(lhs, rhs), bits);
1512}1615}
15131616
1514static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1617static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1515 return zig_wrap_i128(zig_bitcast_i128(zig_add_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);1618 return zig_wrap_i128(zig_bitcast_i128(zig_add_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1516}1619}
15171620
1518static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1621static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1519 return zig_wrap_u128(zig_sub_u128(lhs, rhs), bits);1622 return zig_wrap_u128(zig_sub_u128(lhs, rhs), bits);
1520}1623}
15211624
1522static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1625static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1523 return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);1626 return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1524}1627}
15251628
1526static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1629static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1527 return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits);1630 return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits);
1528}1631}
15291632
1530static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1633static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1531 return zig_wrap_i128(zig_bitcast_i128(zig_mul_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);1634 return zig_wrap_i128(zig_bitcast_i128(zig_mul_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1532}1635}
15331636
1534#if zig_has_int1281637#if zig_has_int128
15351638
1536static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1639static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1537#if zig_has_builtin(add_overflow)1640#if zig_has_builtin(add_overflow)
1538 zig_u128 full_res;1641 zig_u128 full_res;
1539 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);1642 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
1540 *res = zig_wrap_u128(full_res, bits);1643 *res = zig_wrap_u128(full_res, bits);
1541 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);1644 return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits);
1542#else1645#else
1543 *res = zig_addw_u128(lhs, rhs, bits);1646 *res = zig_addw_u128(lhs, rhs, bits);
1544 return *res < lhs;1647 return *res < lhs;
1545#endif1648#endif
1546}1649}
15471650
1548zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);1651zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1549static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1652static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1550#if zig_has_builtin(add_overflow)1653#if zig_has_builtin(add_overflow)
1551 zig_i128 full_res;1654 zig_i128 full_res;
1552 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);1655 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
1553#else1656#else
1554 zig_c_int overflow_int;1657 int overflow_int;
1555 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);1658 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
1556 bool overflow = overflow_int != 0;1659 bool overflow = overflow_int != 0;
1557#endif1660#endif
1558 *res = zig_wrap_i128(full_res, bits);1661 *res = zig_wrap_i128(full_res, bits);
1559 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);1662 return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits);
1560}1663}
15611664
1562static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1665static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1563#if zig_has_builtin(sub_overflow)1666#if zig_has_builtin(sub_overflow)
1564 zig_u128 full_res;1667 zig_u128 full_res;
1565 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);1668 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
1566 *res = zig_wrap_u128(full_res, bits);1669 *res = zig_wrap_u128(full_res, bits);
1567 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);1670 return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits);
1568#else1671#else
1569 *res = zig_subw_u128(lhs, rhs, bits);1672 *res = zig_subw_u128(lhs, rhs, bits);
1570 return *res > lhs;1673 return *res > lhs;
1571#endif1674#endif
1572}1675}
15731676
1574zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);1677zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1575static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1678static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1576#if zig_has_builtin(sub_overflow)1679#if zig_has_builtin(sub_overflow)
1577 zig_i128 full_res;1680 zig_i128 full_res;
1578 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);1681 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
1579#else1682#else
1580 zig_c_int overflow_int;1683 int overflow_int;
1581 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);1684 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
1582 bool overflow = overflow_int != 0;1685 bool overflow = overflow_int != 0;
1583#endif1686#endif
1584 *res = zig_wrap_i128(full_res, bits);1687 *res = zig_wrap_i128(full_res, bits);
1585 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);1688 return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits);
1586}1689}
15871690
1588static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1691static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1589#if zig_has_builtin(mul_overflow)1692#if zig_has_builtin(mul_overflow)
1590 zig_u128 full_res;1693 zig_u128 full_res;
1591 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);1694 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
1592 *res = zig_wrap_u128(full_res, bits);1695 *res = zig_wrap_u128(full_res, bits);
1593 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);1696 return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits);
1594#else1697#else
1595 *res = zig_mulw_u128(lhs, rhs, bits);1698 *res = zig_mulw_u128(lhs, rhs, bits);
1596 return rhs != zig_as_u128(0, 0) && lhs > zig_maxInt(u128, bits) / rhs;1699 return rhs != zig_make_u128(0, 0) && lhs > zig_maxInt_u(128, bits) / rhs;
1597#endif1700#endif
1598}1701}
15991702
1600zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);1703zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1601static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1704static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1602#if zig_has_builtin(mul_overflow)1705#if zig_has_builtin(mul_overflow)
1603 zig_i128 full_res;1706 zig_i128 full_res;
1604 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);1707 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
1605#else1708#else
1606 zig_c_int overflow_int;1709 int overflow_int;
1607 zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int);1710 zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int);
1608 bool overflow = overflow_int != 0;1711 bool overflow = overflow_int != 0;
1609#endif1712#endif
1610 *res = zig_wrap_i128(full_res, bits);1713 *res = zig_wrap_i128(full_res, bits);
1611 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);1714 return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits);
1612}1715}
16131716
1614#else /* zig_has_int128 */1717#else /* zig_has_int128 */
16151718
1616static inline bool zig_overflow_u128(bool overflow, zig_u128 full_res, zig_u8 bits) {1719static inline bool zig_overflow_u128(bool overflow, zig_u128 full_res, uint8_t bits) {
1617 return overflow ||1720 return overflow ||
1618 zig_cmp_u128(full_res, zig_minInt(u128, bits)) < zig_as_i32(0) ||1721 zig_cmp_u128(full_res, zig_minInt_u(128, bits)) < INT32_C(0) ||
1619 zig_cmp_u128(full_res, zig_maxInt(u128, bits)) > zig_as_i32(0);1722 zig_cmp_u128(full_res, zig_maxInt_u(128, bits)) > INT32_C(0);
1620}1723}
16211724
1622static inline bool zig_overflow_i128(bool overflow, zig_i128 full_res, zig_u8 bits) {1725static inline bool zig_overflow_i128(bool overflow, zig_i128 full_res, uint8_t bits) {
1623 return overflow ||1726 return overflow ||
1624 zig_cmp_i128(full_res, zig_minInt(i128, bits)) < zig_as_i32(0) ||1727 zig_cmp_i128(full_res, zig_minInt_i(128, bits)) < INT32_C(0) ||
1625 zig_cmp_i128(full_res, zig_maxInt(i128, bits)) > zig_as_i32(0);1728 zig_cmp_i128(full_res, zig_maxInt_i(128, bits)) > INT32_C(0);
1626}1729}
16271730
1628static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1731static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1629 zig_u128 full_res;1732 zig_u128 full_res;
1630 bool overflow =1733 bool overflow =
1631 zig_addo_u64(&full_res.hi, lhs.hi, rhs.hi, 64) |1734 zig_addo_u64(&full_res.hi, lhs.hi, rhs.hi, 64) |
...@@ -1634,15 +1737,15 @@ static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_...@@ -1634,15 +1737,15 @@ static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_
1634 return zig_overflow_u128(overflow, full_res, bits);1737 return zig_overflow_u128(overflow, full_res, bits);
1635}1738}
16361739
1637zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);1740zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1638static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1741static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1639 zig_c_int overflow_int;1742 int overflow_int;
1640 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);1743 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
1641 *res = zig_wrap_i128(full_res, bits);1744 *res = zig_wrap_i128(full_res, bits);
1642 return zig_overflow_i128(overflow_int, full_res, bits);1745 return zig_overflow_i128(overflow_int, full_res, bits);
1643}1746}
16441747
1645static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1748static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1646 zig_u128 full_res;1749 zig_u128 full_res;
1647 bool overflow =1750 bool overflow =
1648 zig_subo_u64(&full_res.hi, lhs.hi, rhs.hi, 64) |1751 zig_subo_u64(&full_res.hi, lhs.hi, rhs.hi, 64) |
...@@ -1651,23 +1754,23 @@ static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_...@@ -1651,23 +1754,23 @@ static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_
1651 return zig_overflow_u128(overflow, full_res, bits);1754 return zig_overflow_u128(overflow, full_res, bits);
1652}1755}
16531756
1654zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);1757zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1655static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1758static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1656 zig_c_int overflow_int;1759 int overflow_int;
1657 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);1760 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
1658 *res = zig_wrap_i128(full_res, bits);1761 *res = zig_wrap_i128(full_res, bits);
1659 return zig_overflow_i128(overflow_int, full_res, bits);1762 return zig_overflow_i128(overflow_int, full_res, bits);
1660}1763}
16611764
1662static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1765static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1663 *res = zig_mulw_u128(lhs, rhs, bits);1766 *res = zig_mulw_u128(lhs, rhs, bits);
1664 return zig_cmp_u128(*res, zig_as_u128(0, 0)) != zig_as_i32(0) &&1767 return zig_cmp_u128(*res, zig_make_u128(0, 0)) != INT32_C(0) &&
1665 zig_cmp_u128(lhs, zig_div_trunc_u128(zig_maxInt(u128, bits), rhs)) > zig_as_i32(0);1768 zig_cmp_u128(lhs, zig_div_trunc_u128(zig_maxInt_u(128, bits), rhs)) > INT32_C(0);
1666}1769}
16671770
1668zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);1771zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1669static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1772static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1670 zig_c_int overflow_int;1773 int overflow_int;
1671 zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int);1774 zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int);
1672 *res = zig_wrap_i128(full_res, bits);1775 *res = zig_wrap_i128(full_res, bits);
1673 return zig_overflow_i128(overflow_int, full_res, bits);1776 return zig_overflow_i128(overflow_int, full_res, bits);
...@@ -1675,119 +1778,119 @@ static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_...@@ -1675,119 +1778,119 @@ static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_
16751778
1676#endif /* zig_has_int128 */1779#endif /* zig_has_int128 */
16771780
1678static inline bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {1781static inline bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, uint8_t rhs, uint8_t bits) {
1679 *res = zig_shlw_u128(lhs, rhs, bits);1782 *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);1783 return zig_cmp_u128(lhs, zig_shr_u128(zig_maxInt_u(128, bits), rhs)) > INT32_C(0);
1681}1784}
16821785
1683static inline bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {1786static inline bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, uint8_t rhs, uint8_t bits) {
1684 *res = zig_shlw_i128(lhs, rhs, bits);1787 *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)));1788 zig_i128 mask = zig_bitcast_i128(zig_shl_u128(zig_maxInt_u128, bits - rhs - UINT8_C(1)));
1686 return zig_cmp_i128(zig_and_i128(lhs, mask), zig_as_i128(0, 0)) != zig_as_i32(0) &&1789 return zig_cmp_i128(zig_and_i128(lhs, mask), zig_make_i128(0, 0)) != INT32_C(0) &&
1687 zig_cmp_i128(zig_and_i128(lhs, mask), mask) != zig_as_i32(0);1790 zig_cmp_i128(zig_and_i128(lhs, mask), mask) != INT32_C(0);
1688}1791}
16891792
1690static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1793static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1691 zig_u128 res;1794 zig_u128 res;
1692 if (zig_cmp_u128(rhs, zig_as_u128(0, bits)) >= zig_as_i32(0))1795 if (zig_cmp_u128(rhs, zig_make_u128(0, bits)) >= INT32_C(0))
1693 return zig_cmp_u128(lhs, zig_as_u128(0, 0)) != zig_as_i32(0) ? zig_maxInt(u128, bits) : lhs;1796 return zig_cmp_u128(lhs, zig_make_u128(0, 0)) != INT32_C(0) ? zig_maxInt_u(128, bits) : lhs;
16941797
1695#if zig_has_int1281798#if zig_has_int128
1696 return zig_shlo_u128(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u128, bits) : res;1799 return zig_shlo_u128(&res, lhs, (uint8_t)rhs, bits) ? zig_maxInt_u(128, bits) : res;
1697#else1800#else
1698 return zig_shlo_u128(&res, lhs, (zig_u8)rhs.lo, bits) ? zig_maxInt(u128, bits) : res;1801 return zig_shlo_u128(&res, lhs, (uint8_t)rhs.lo, bits) ? zig_maxInt_u(128, bits) : res;
1699#endif1802#endif
1700}1803}
17011804
1702static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1805static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1703 zig_i128 res;1806 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;1807 if (zig_cmp_u128(zig_bitcast_u128(rhs), zig_make_u128(0, bits)) < INT32_C(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);1808 return zig_cmp_i128(lhs, zig_make_i128(0, 0)) < INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits);
1706}1809}
17071810
1708static inline zig_u128 zig_adds_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1811static inline zig_u128 zig_adds_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1709 zig_u128 res;1812 zig_u128 res;
1710 return zig_addo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res;1813 return zig_addo_u128(&res, lhs, rhs, bits) ? zig_maxInt_u(128, bits) : res;
1711}1814}
17121815
1713static inline zig_i128 zig_adds_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1816static inline zig_i128 zig_adds_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1714 zig_i128 res;1817 zig_i128 res;
1715 if (!zig_addo_i128(&res, lhs, rhs, bits)) return res;1818 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);1819 return zig_cmp_i128(res, zig_make_i128(0, 0)) >= INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits);
1717}1820}
17181821
1719static inline zig_u128 zig_subs_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1822static inline zig_u128 zig_subs_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1720 zig_u128 res;1823 zig_u128 res;
1721 return zig_subo_u128(&res, lhs, rhs, bits) ? zig_minInt(u128, bits) : res;1824 return zig_subo_u128(&res, lhs, rhs, bits) ? zig_minInt_u(128, bits) : res;
1722}1825}
17231826
1724static inline zig_i128 zig_subs_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1827static inline zig_i128 zig_subs_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1725 zig_i128 res;1828 zig_i128 res;
1726 if (!zig_subo_i128(&res, lhs, rhs, bits)) return res;1829 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);1830 return zig_cmp_i128(res, zig_make_i128(0, 0)) >= INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits);
1728}1831}
17291832
1730static inline zig_u128 zig_muls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {1833static inline zig_u128 zig_muls_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) {
1731 zig_u128 res;1834 zig_u128 res;
1732 return zig_mulo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res;1835 return zig_mulo_u128(&res, lhs, rhs, bits) ? zig_maxInt_u(128, bits) : res;
1733}1836}
17341837
1735static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {1838static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1736 zig_i128 res;1839 zig_i128 res;
1737 if (!zig_mulo_i128(&res, lhs, rhs, bits)) return res;1840 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);1841 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);
1739}1842}
17401843
1741static inline zig_u8 zig_clz_u128(zig_u128 val, zig_u8 bits) {1844static inline uint8_t zig_clz_u128(zig_u128 val, uint8_t bits) {
1742 if (bits <= zig_as_u8(64)) return zig_clz_u64(zig_lo_u128(val), bits);1845 if (bits <= UINT8_C(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));1846 if (zig_hi_u128(val) != 0) return zig_clz_u64(zig_hi_u128(val), bits - UINT8_C(64));
1744 return zig_clz_u64(zig_lo_u128(val), zig_as_u8(64)) + (bits - zig_as_u8(64));1847 return zig_clz_u64(zig_lo_u128(val), UINT8_C(64)) + (bits - UINT8_C(64));
1745}1848}
17461849
1747static inline zig_u8 zig_clz_i128(zig_i128 val, zig_u8 bits) {1850static inline uint8_t zig_clz_i128(zig_i128 val, uint8_t bits) {
1748 return zig_clz_u128(zig_bitcast_u128(val), bits);1851 return zig_clz_u128(zig_bitcast_u128(val), bits);
1749}1852}
17501853
1751static inline zig_u8 zig_ctz_u128(zig_u128 val, zig_u8 bits) {1854static inline uint8_t zig_ctz_u128(zig_u128 val, uint8_t bits) {
1752 if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), zig_as_u8(64));1855 if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), UINT8_C(64));
1753 return zig_ctz_u64(zig_hi_u128(val), bits - zig_as_u8(64)) + zig_as_u8(64);1856 return zig_ctz_u64(zig_hi_u128(val), bits - UINT8_C(64)) + UINT8_C(64);
1754}1857}
17551858
1756static inline zig_u8 zig_ctz_i128(zig_i128 val, zig_u8 bits) {1859static inline uint8_t zig_ctz_i128(zig_i128 val, uint8_t bits) {
1757 return zig_ctz_u128(zig_bitcast_u128(val), bits);1860 return zig_ctz_u128(zig_bitcast_u128(val), bits);
1758}1861}
17591862
1760static inline zig_u8 zig_popcount_u128(zig_u128 val, zig_u8 bits) {1863static inline uint8_t zig_popcount_u128(zig_u128 val, uint8_t bits) {
1761 return zig_popcount_u64(zig_hi_u128(val), bits - zig_as_u8(64)) +1864 return zig_popcount_u64(zig_hi_u128(val), bits - UINT8_C(64)) +
1762 zig_popcount_u64(zig_lo_u128(val), zig_as_u8(64));1865 zig_popcount_u64(zig_lo_u128(val), UINT8_C(64));
1763}1866}
17641867
1765static inline zig_u8 zig_popcount_i128(zig_i128 val, zig_u8 bits) {1868static inline uint8_t zig_popcount_i128(zig_i128 val, uint8_t bits) {
1766 return zig_popcount_u128(zig_bitcast_u128(val), bits);1869 return zig_popcount_u128(zig_bitcast_u128(val), bits);
1767}1870}
17681871
1769static inline zig_u128 zig_byte_swap_u128(zig_u128 val, zig_u8 bits) {1872static inline zig_u128 zig_byte_swap_u128(zig_u128 val, uint8_t bits) {
1770 zig_u128 full_res;1873 zig_u128 full_res;
1771#if zig_has_builtin(bswap128)1874#if zig_has_builtin(bswap128)
1772 full_res = __builtin_bswap128(val);1875 full_res = __builtin_bswap128(val);
1773#else1876#else
1774 full_res = zig_as_u128(zig_byte_swap_u64(zig_lo_u128(val), zig_as_u8(64)),1877 full_res = zig_make_u128(zig_byte_swap_u64(zig_lo_u128(val), UINT8_C(64)),
1775 zig_byte_swap_u64(zig_hi_u128(val), zig_as_u8(64)));1878 zig_byte_swap_u64(zig_hi_u128(val), UINT8_C(64)));
1776#endif1879#endif
1777 return zig_shr_u128(full_res, zig_as_u8(128) - bits);1880 return zig_shr_u128(full_res, UINT8_C(128) - bits);
1778}1881}
17791882
1780static inline zig_i128 zig_byte_swap_i128(zig_i128 val, zig_u8 bits) {1883static inline zig_i128 zig_byte_swap_i128(zig_i128 val, uint8_t bits) {
1781 return zig_bitcast_i128(zig_byte_swap_u128(zig_bitcast_u128(val), bits));1884 return zig_bitcast_i128(zig_byte_swap_u128(zig_bitcast_u128(val), bits));
1782}1885}
17831886
1784static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, zig_u8 bits) {1887static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, uint8_t bits) {
1785 return zig_shr_u128(zig_as_u128(zig_bit_reverse_u64(zig_lo_u128(val), zig_as_u8(64)),1888 return zig_shr_u128(zig_make_u128(zig_bit_reverse_u64(zig_lo_u128(val), UINT8_C(64)),
1786 zig_bit_reverse_u64(zig_hi_u128(val), zig_as_u8(64))),1889 zig_bit_reverse_u64(zig_hi_u128(val), UINT8_C(64))),
1787 zig_as_u8(128) - bits);1890 UINT8_C(128) - bits);
1788}1891}
17891892
1790static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) {1893static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, uint8_t bits) {
1791 return zig_bitcast_i128(zig_bit_reverse_u128(zig_bitcast_u128(val), bits));1894 return zig_bitcast_i128(zig_bit_reverse_u128(zig_bitcast_u128(val), bits));
1792}1895}
17931896
...@@ -1810,85 +1913,85 @@ static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) {...@@ -1810,85 +1913,85 @@ static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) {
18101913
1811#if (zig_has_builtin(nan) && zig_has_builtin(nans) && zig_has_builtin(inf)) || defined(zig_gnuc)1914#if (zig_has_builtin(nan) && zig_has_builtin(nans) && zig_has_builtin(inf)) || defined(zig_gnuc)
1812#define zig_has_float_builtins 11915#define zig_has_float_builtins 1
1813#define zig_as_special_f16(sign, name, arg, repr) sign zig_as_f16(__builtin_##name, )(arg)1916#define zig_make_special_f16(sign, name, arg, repr) sign zig_make_f16(__builtin_##name, )(arg)
1814#define zig_as_special_f32(sign, name, arg, repr) sign zig_as_f32(__builtin_##name, )(arg)1917#define zig_make_special_f32(sign, name, arg, repr) sign zig_make_f32(__builtin_##name, )(arg)
1815#define zig_as_special_f64(sign, name, arg, repr) sign zig_as_f64(__builtin_##name, )(arg)1918#define zig_make_special_f64(sign, name, arg, repr) sign zig_make_f64(__builtin_##name, )(arg)
1816#define zig_as_special_f80(sign, name, arg, repr) sign zig_as_f80(__builtin_##name, )(arg)1919#define zig_make_special_f80(sign, name, arg, repr) sign zig_make_f80(__builtin_##name, )(arg)
1817#define zig_as_special_f128(sign, name, arg, repr) sign zig_as_f128(__builtin_##name, )(arg)1920#define zig_make_special_f128(sign, name, arg, repr) sign zig_make_f128(__builtin_##name, )(arg)
1818#define zig_as_special_c_longdouble(sign, name, arg, repr) sign zig_as_c_longdouble(__builtin_##name, )(arg)1921#define zig_make_special_c_longdouble(sign, name, arg, repr) sign zig_make_c_longdouble(__builtin_##name, )(arg)
1819#else1922#else
1820#define zig_has_float_builtins 01923#define zig_has_float_builtins 0
1821#define zig_as_special_f16(sign, name, arg, repr) zig_float_from_repr_f16(repr)1924#define zig_make_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)1925#define zig_make_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)1926#define zig_make_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)1927#define zig_make_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)1928#define zig_make_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)1929#define zig_make_special_c_longdouble(sign, name, arg, repr) zig_float_from_repr_c_longdouble(repr)
1827#endif1930#endif
18281931
1829#define zig_has_f16 11932#define zig_has_f16 1
1830#define zig_bitSizeOf_f16 161933#define zig_bitSizeOf_f16 16
1831#define zig_libc_name_f16(name) __##name##h1934#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)1935#define zig_make_special_constant_f16(sign, name, arg, repr) zig_make_special_f16(sign, name, arg, repr)
1833#if FLT_MANT_DIG == 111936#if FLT_MANT_DIG == 11
1834typedef float zig_f16;1937typedef float zig_f16;
1835#define zig_as_f16(fp, repr) fp##f1938#define zig_make_f16(fp, repr) fp##f
1836#elif DBL_MANT_DIG == 111939#elif DBL_MANT_DIG == 11
1837typedef double zig_f16;1940typedef double zig_f16;
1838#define zig_as_f16(fp, repr) fp1941#define zig_make_f16(fp, repr) fp
1839#elif LDBL_MANT_DIG == 111942#elif LDBL_MANT_DIG == 11
1840#define zig_bitSizeOf_c_longdouble 161943#define zig_bitSizeOf_c_longdouble 16
1841typedef long double zig_f16;1944typedef long double zig_f16;
1842#define zig_as_f16(fp, repr) fp##l1945#define zig_make_f16(fp, repr) fp##l
1843#elif FLT16_MANT_DIG == 11 && (zig_has_builtin(inff16) || defined(zig_gnuc))1946#elif FLT16_MANT_DIG == 11 && (zig_has_builtin(inff16) || defined(zig_gnuc))
1844typedef _Float16 zig_f16;1947typedef _Float16 zig_f16;
1845#define zig_as_f16(fp, repr) fp##f161948#define zig_make_f16(fp, repr) fp##f16
1846#elif defined(__SIZEOF_FP16__)1949#elif defined(__SIZEOF_FP16__)
1847typedef __fp16 zig_f16;1950typedef __fp16 zig_f16;
1848#define zig_as_f16(fp, repr) fp##f161951#define zig_make_f16(fp, repr) fp##f16
1849#else1952#else
1850#undef zig_has_f161953#undef zig_has_f16
1851#define zig_has_f16 01954#define zig_has_f16 0
1852#define zig_repr_f16 i161955#define zig_bitSizeOf_repr_f16 16
1853typedef zig_i16 zig_f16;1956typedef int16_t zig_f16;
1854#define zig_as_f16(fp, repr) repr1957#define zig_make_f16(fp, repr) repr
1855#undef zig_as_special_f161958#undef zig_make_special_f16
1856#define zig_as_special_f16(sign, name, arg, repr) repr1959#define zig_make_special_f16(sign, name, arg, repr) repr
1857#undef zig_as_special_constant_f161960#undef zig_make_special_constant_f16
1858#define zig_as_special_constant_f16(sign, name, arg, repr) repr1961#define zig_make_special_constant_f16(sign, name, arg, repr) repr
1859#endif1962#endif
18601963
1861#define zig_has_f32 11964#define zig_has_f32 1
1862#define zig_bitSizeOf_f32 321965#define zig_bitSizeOf_f32 32
1863#define zig_libc_name_f32(name) name##f1966#define zig_libc_name_f32(name) name##f
1864#if _MSC_VER1967#if _MSC_VER
1865#define zig_as_special_constant_f32(sign, name, arg, repr) sign zig_as_f32(zig_msvc_flt_##name, )1968#define zig_make_special_constant_f32(sign, name, arg, repr) sign zig_make_f32(zig_msvc_flt_##name, )
1866#else1969#else
1867#define zig_as_special_constant_f32(sign, name, arg, repr) zig_as_special_f32(sign, name, arg, repr)1970#define zig_make_special_constant_f32(sign, name, arg, repr) zig_make_special_f32(sign, name, arg, repr)
1868#endif1971#endif
1869#if FLT_MANT_DIG == 241972#if FLT_MANT_DIG == 24
1870typedef float zig_f32;1973typedef float zig_f32;
1871#define zig_as_f32(fp, repr) fp##f1974#define zig_make_f32(fp, repr) fp##f
1872#elif DBL_MANT_DIG == 241975#elif DBL_MANT_DIG == 24
1873typedef double zig_f32;1976typedef double zig_f32;
1874#define zig_as_f32(fp, repr) fp1977#define zig_make_f32(fp, repr) fp
1875#elif LDBL_MANT_DIG == 241978#elif LDBL_MANT_DIG == 24
1876#define zig_bitSizeOf_c_longdouble 321979#define zig_bitSizeOf_c_longdouble 32
1877typedef long double zig_f32;1980typedef long double zig_f32;
1878#define zig_as_f32(fp, repr) fp##l1981#define zig_make_f32(fp, repr) fp##l
1879#elif FLT32_MANT_DIG == 241982#elif FLT32_MANT_DIG == 24
1880typedef _Float32 zig_f32;1983typedef _Float32 zig_f32;
1881#define zig_as_f32(fp, repr) fp##f321984#define zig_make_f32(fp, repr) fp##f32
1882#else1985#else
1883#undef zig_has_f321986#undef zig_has_f32
1884#define zig_has_f32 01987#define zig_has_f32 0
1885#define zig_repr_f32 i321988#define zig_bitSizeOf_repr_f32 32
1886typedef zig_i32 zig_f32;1989typedef int32_t zig_f32;
1887#define zig_as_f32(fp, repr) repr1990#define zig_make_f32(fp, repr) repr
1888#undef zig_as_special_f321991#undef zig_make_special_f32
1889#define zig_as_special_f32(sign, name, arg, repr) repr1992#define zig_make_special_f32(sign, name, arg, repr) repr
1890#undef zig_as_special_constant_f321993#undef zig_make_special_constant_f32
1891#define zig_as_special_constant_f32(sign, name, arg, repr) repr1994#define zig_make_special_constant_f32(sign, name, arg, repr) repr
1892#endif1995#endif
18931996
1894#define zig_has_f64 11997#define zig_has_f64 1
...@@ -1898,108 +2001,108 @@ typedef zig_i32 zig_f32;...@@ -1898,108 +2001,108 @@ typedef zig_i32 zig_f32;
1898#ifdef ZIG_TARGET_ABI_MSVC2001#ifdef ZIG_TARGET_ABI_MSVC
1899#define zig_bitSizeOf_c_longdouble 642002#define zig_bitSizeOf_c_longdouble 64
1900#endif2003#endif
1901#define zig_as_special_constant_f64(sign, name, arg, repr) sign zig_as_f64(zig_msvc_flt_##name, )2004#define zig_make_special_constant_f64(sign, name, arg, repr) sign zig_make_f64(zig_msvc_flt_##name, )
1902#else /* _MSC_VER */2005#else /* _MSC_VER */
1903#define zig_as_special_constant_f64(sign, name, arg, repr) zig_as_special_f64(sign, name, arg, repr)2006#define zig_make_special_constant_f64(sign, name, arg, repr) zig_make_special_f64(sign, name, arg, repr)
1904#endif /* _MSC_VER */2007#endif /* _MSC_VER */
1905#if FLT_MANT_DIG == 532008#if FLT_MANT_DIG == 53
1906typedef float zig_f64;2009typedef float zig_f64;
1907#define zig_as_f64(fp, repr) fp##f2010#define zig_make_f64(fp, repr) fp##f
1908#elif DBL_MANT_DIG == 532011#elif DBL_MANT_DIG == 53
1909typedef double zig_f64;2012typedef double zig_f64;
1910#define zig_as_f64(fp, repr) fp2013#define zig_make_f64(fp, repr) fp
1911#elif LDBL_MANT_DIG == 532014#elif LDBL_MANT_DIG == 53
1912#define zig_bitSizeOf_c_longdouble 642015#define zig_bitSizeOf_c_longdouble 64
1913typedef long double zig_f64;2016typedef long double zig_f64;
1914#define zig_as_f64(fp, repr) fp##l2017#define zig_make_f64(fp, repr) fp##l
1915#elif FLT64_MANT_DIG == 532018#elif FLT64_MANT_DIG == 53
1916typedef _Float64 zig_f64;2019typedef _Float64 zig_f64;
1917#define zig_as_f64(fp, repr) fp##f642020#define zig_make_f64(fp, repr) fp##f64
1918#elif FLT32X_MANT_DIG == 532021#elif FLT32X_MANT_DIG == 53
1919typedef _Float32x zig_f64;2022typedef _Float32x zig_f64;
1920#define zig_as_f64(fp, repr) fp##f32x2023#define zig_make_f64(fp, repr) fp##f32x
1921#else2024#else
1922#undef zig_has_f642025#undef zig_has_f64
1923#define zig_has_f64 02026#define zig_has_f64 0
1924#define zig_repr_f64 i642027#define zig_bitSizeOf_repr_f64 64
1925typedef zig_i64 zig_f64;2028typedef int64_t zig_f64;
1926#define zig_as_f64(fp, repr) repr2029#define zig_make_f64(fp, repr) repr
1927#undef zig_as_special_f642030#undef zig_make_special_f64
1928#define zig_as_special_f64(sign, name, arg, repr) repr2031#define zig_make_special_f64(sign, name, arg, repr) repr
1929#undef zig_as_special_constant_f642032#undef zig_make_special_constant_f64
1930#define zig_as_special_constant_f64(sign, name, arg, repr) repr2033#define zig_make_special_constant_f64(sign, name, arg, repr) repr
1931#endif2034#endif
19322035
1933#define zig_has_f80 12036#define zig_has_f80 1
1934#define zig_bitSizeOf_f80 802037#define zig_bitSizeOf_f80 80
1935#define zig_libc_name_f80(name) __##name##x2038#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)2039#define zig_make_special_constant_f80(sign, name, arg, repr) zig_make_special_f80(sign, name, arg, repr)
1937#if FLT_MANT_DIG == 642040#if FLT_MANT_DIG == 64
1938typedef float zig_f80;2041typedef float zig_f80;
1939#define zig_as_f80(fp, repr) fp##f2042#define zig_make_f80(fp, repr) fp##f
1940#elif DBL_MANT_DIG == 642043#elif DBL_MANT_DIG == 64
1941typedef double zig_f80;2044typedef double zig_f80;
1942#define zig_as_f80(fp, repr) fp2045#define zig_make_f80(fp, repr) fp
1943#elif LDBL_MANT_DIG == 642046#elif LDBL_MANT_DIG == 64
1944#define zig_bitSizeOf_c_longdouble 802047#define zig_bitSizeOf_c_longdouble 80
1945typedef long double zig_f80;2048typedef long double zig_f80;
1946#define zig_as_f80(fp, repr) fp##l2049#define zig_make_f80(fp, repr) fp##l
1947#elif FLT80_MANT_DIG == 642050#elif FLT80_MANT_DIG == 64
1948typedef _Float80 zig_f80;2051typedef _Float80 zig_f80;
1949#define zig_as_f80(fp, repr) fp##f802052#define zig_make_f80(fp, repr) fp##f80
1950#elif FLT64X_MANT_DIG == 642053#elif FLT64X_MANT_DIG == 64
1951typedef _Float64x zig_f80;2054typedef _Float64x zig_f80;
1952#define zig_as_f80(fp, repr) fp##f64x2055#define zig_make_f80(fp, repr) fp##f64x
1953#elif defined(__SIZEOF_FLOAT80__)2056#elif defined(__SIZEOF_FLOAT80__)
1954typedef __float80 zig_f80;2057typedef __float80 zig_f80;
1955#define zig_as_f80(fp, repr) fp##l2058#define zig_make_f80(fp, repr) fp##l
1956#else2059#else
1957#undef zig_has_f802060#undef zig_has_f80
1958#define zig_has_f80 02061#define zig_has_f80 0
1959#define zig_repr_f80 i1282062#define zig_bitSizeOf_repr_f80 128
1960typedef zig_i128 zig_f80;2063typedef zig_i128 zig_f80;
1961#define zig_as_f80(fp, repr) repr2064#define zig_make_f80(fp, repr) repr
1962#undef zig_as_special_f802065#undef zig_make_special_f80
1963#define zig_as_special_f80(sign, name, arg, repr) repr2066#define zig_make_special_f80(sign, name, arg, repr) repr
1964#undef zig_as_special_constant_f802067#undef zig_make_special_constant_f80
1965#define zig_as_special_constant_f80(sign, name, arg, repr) repr2068#define zig_make_special_constant_f80(sign, name, arg, repr) repr
1966#endif2069#endif
19672070
1968#define zig_has_f128 12071#define zig_has_f128 1
1969#define zig_bitSizeOf_f128 1282072#define zig_bitSizeOf_f128 128
1970#define zig_libc_name_f128(name) name##q2073#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)2074#define zig_make_special_constant_f128(sign, name, arg, repr) zig_make_special_f128(sign, name, arg, repr)
1972#if FLT_MANT_DIG == 1132075#if FLT_MANT_DIG == 113
1973typedef float zig_f128;2076typedef float zig_f128;
1974#define zig_as_f128(fp, repr) fp##f2077#define zig_make_f128(fp, repr) fp##f
1975#elif DBL_MANT_DIG == 1132078#elif DBL_MANT_DIG == 113
1976typedef double zig_f128;2079typedef double zig_f128;
1977#define zig_as_f128(fp, repr) fp2080#define zig_make_f128(fp, repr) fp
1978#elif LDBL_MANT_DIG == 1132081#elif LDBL_MANT_DIG == 113
1979#define zig_bitSizeOf_c_longdouble 1282082#define zig_bitSizeOf_c_longdouble 128
1980typedef long double zig_f128;2083typedef long double zig_f128;
1981#define zig_as_f128(fp, repr) fp##l2084#define zig_make_f128(fp, repr) fp##l
1982#elif FLT128_MANT_DIG == 1132085#elif FLT128_MANT_DIG == 113
1983typedef _Float128 zig_f128;2086typedef _Float128 zig_f128;
1984#define zig_as_f128(fp, repr) fp##f1282087#define zig_make_f128(fp, repr) fp##f128
1985#elif FLT64X_MANT_DIG == 1132088#elif FLT64X_MANT_DIG == 113
1986typedef _Float64x zig_f128;2089typedef _Float64x zig_f128;
1987#define zig_as_f128(fp, repr) fp##f64x2090#define zig_make_f128(fp, repr) fp##f64x
1988#elif defined(__SIZEOF_FLOAT128__)2091#elif defined(__SIZEOF_FLOAT128__)
1989typedef __float128 zig_f128;2092typedef __float128 zig_f128;
1990#define zig_as_f128(fp, repr) fp##q2093#define zig_make_f128(fp, repr) fp##q
1991#undef zig_as_special_f1282094#undef zig_make_special_f128
1992#define zig_as_special_f128(sign, name, arg, repr) sign __builtin_##name##f128(arg)2095#define zig_make_special_f128(sign, name, arg, repr) sign __builtin_##name##f128(arg)
1993#else2096#else
1994#undef zig_has_f1282097#undef zig_has_f128
1995#define zig_has_f128 02098#define zig_has_f128 0
1996#define zig_repr_f128 i1282099#define zig_bitSizeOf_repr_f128 128
1997typedef zig_i128 zig_f128;2100typedef zig_i128 zig_f128;
1998#define zig_as_f128(fp, repr) repr2101#define zig_make_f128(fp, repr) repr
1999#undef zig_as_special_f1282102#undef zig_make_special_f128
2000#define zig_as_special_f128(sign, name, arg, repr) repr2103#define zig_make_special_f128(sign, name, arg, repr) repr
2001#undef zig_as_special_constant_f1282104#undef zig_make_special_constant_f128
2002#define zig_as_special_constant_f128(sign, name, arg, repr) repr2105#define zig_make_special_constant_f128(sign, name, arg, repr) repr
2003#endif2106#endif
20042107
2005#define zig_has_c_longdouble 12108#define zig_has_c_longdouble 1
...@@ -2010,17 +2113,17 @@ typedef zig_i128 zig_f128;...@@ -2010,17 +2113,17 @@ typedef zig_i128 zig_f128;
2010#define zig_libc_name_c_longdouble(name) name##l2113#define zig_libc_name_c_longdouble(name) name##l
2011#endif2114#endif
20122115
2013#define zig_as_special_constant_c_longdouble(sign, name, arg, repr) zig_as_special_c_longdouble(sign, name, arg, repr)2116#define zig_make_special_constant_c_longdouble(sign, name, arg, repr) zig_make_special_c_longdouble(sign, name, arg, repr)
2014#ifdef zig_bitSizeOf_c_longdouble2117#ifdef zig_bitSizeOf_c_longdouble
20152118
2016#ifdef ZIG_TARGET_ABI_MSVC2119#ifdef ZIG_TARGET_ABI_MSVC
2017typedef double zig_c_longdouble;2120typedef double zig_c_longdouble;
2018#undef zig_bitSizeOf_c_longdouble2121#undef zig_bitSizeOf_c_longdouble
2019#define zig_bitSizeOf_c_longdouble 642122#define zig_bitSizeOf_c_longdouble 64
2020#define zig_as_c_longdouble(fp, repr) fp2123#define zig_make_c_longdouble(fp, repr) fp
2021#else2124#else
2022typedef long double zig_c_longdouble;2125typedef long double zig_c_longdouble;
2023#define zig_as_c_longdouble(fp, repr) fp##l2126#define zig_make_c_longdouble(fp, repr) fp##l
2024#endif2127#endif
20252128
2026#else /* zig_bitSizeOf_c_longdouble */2129#else /* zig_bitSizeOf_c_longdouble */
...@@ -2029,13 +2132,13 @@ typedef long double zig_c_longdouble;...@@ -2029,13 +2132,13 @@ typedef long double zig_c_longdouble;
2029#define zig_has_c_longdouble 02132#define zig_has_c_longdouble 0
2030#define zig_bitSizeOf_c_longdouble 802133#define zig_bitSizeOf_c_longdouble 80
2031#define zig_compiler_rt_abbrev_c_longdouble zig_compiler_rt_abbrev_f802134#define zig_compiler_rt_abbrev_c_longdouble zig_compiler_rt_abbrev_f80
2032#define zig_repr_c_longdouble i1282135#define zig_bitSizeOf_repr_c_longdouble 128
2033typedef zig_i128 zig_c_longdouble;2136typedef zig_i128 zig_c_longdouble;
2034#define zig_as_c_longdouble(fp, repr) repr2137#define zig_make_c_longdouble(fp, repr) repr
2035#undef zig_as_special_c_longdouble2138#undef zig_make_special_c_longdouble
2036#define zig_as_special_c_longdouble(sign, name, arg, repr) repr2139#define zig_make_special_c_longdouble(sign, name, arg, repr) repr
2037#undef zig_as_special_constant_c_longdouble2140#undef zig_make_special_constant_c_longdouble
2038#define zig_as_special_constant_c_longdouble(sign, name, arg, repr) repr2141#define zig_make_special_constant_c_longdouble(sign, name, arg, repr) repr
20392142
2040#endif /* zig_bitSizeOf_c_longdouble */2143#endif /* zig_bitSizeOf_c_longdouble */
20412144
...@@ -2073,32 +2176,35 @@ zig_expand_float_from_repr(c_longdouble, zig_expand_concat(u, zig_bitSizeOf_c_lo...@@ -2073,32 +2176,35 @@ zig_expand_float_from_repr(c_longdouble, zig_expand_concat(u, zig_bitSizeOf_c_lo
2073#endif2176#endif
20742177
2075#define zig_convert_builtin(ResType, operation, ArgType, version) \2178#define zig_convert_builtin(ResType, operation, ArgType, version) \
2076 zig_extern zig_##ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \2179 zig_extern 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);2180 zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(ArgType);
2078zig_convert_builtin(f16, trunc, f32, 2)2181zig_convert_builtin(zig_f16, trunc, zig_f32, 2)
2079zig_convert_builtin(f16, trunc, f64, 2)2182zig_convert_builtin(zig_f16, trunc, zig_f64, 2)
2080zig_convert_builtin(f16, trunc, f80, 2)2183zig_convert_builtin(zig_f16, trunc, zig_f80, 2)
2081zig_convert_builtin(f16, trunc, f128, 2)2184zig_convert_builtin(zig_f16, trunc, zig_f128, 2)
2082zig_convert_builtin(f32, extend, f16, 2)2185zig_convert_builtin(zig_f32, extend, zig_f16, 2)
2083zig_convert_builtin(f32, trunc, f64, 2)2186zig_convert_builtin(zig_f32, trunc, zig_f64, 2)
2084zig_convert_builtin(f32, trunc, f80, 2)2187zig_convert_builtin(zig_f32, trunc, zig_f80, 2)
2085zig_convert_builtin(f32, trunc, f128, 2)2188zig_convert_builtin(zig_f32, trunc, zig_f128, 2)
2086zig_convert_builtin(f64, extend, f16, 2)2189zig_convert_builtin(zig_f64, extend, zig_f16, 2)
2087zig_convert_builtin(f64, extend, f32, 2)2190zig_convert_builtin(zig_f64, extend, zig_f32, 2)
2088zig_convert_builtin(f64, trunc, f80, 2)2191zig_convert_builtin(zig_f64, trunc, zig_f80, 2)
2089zig_convert_builtin(f64, trunc, f128, 2)2192zig_convert_builtin(zig_f64, trunc, zig_f128, 2)
2090zig_convert_builtin(f80, extend, f16, 2)2193zig_convert_builtin(zig_f80, extend, zig_f16, 2)
2091zig_convert_builtin(f80, extend, f32, 2)2194zig_convert_builtin(zig_f80, extend, zig_f32, 2)
2092zig_convert_builtin(f80, extend, f64, 2)2195zig_convert_builtin(zig_f80, extend, zig_f64, 2)
2093zig_convert_builtin(f80, trunc, f128, 2)2196zig_convert_builtin(zig_f80, trunc, zig_f128, 2)
2094zig_convert_builtin(f128, extend, f16, 2)2197zig_convert_builtin(zig_f128, extend, zig_f16, 2)
2095zig_convert_builtin(f128, extend, f32, 2)2198zig_convert_builtin(zig_f128, extend, zig_f32, 2)
2096zig_convert_builtin(f128, extend, f64, 2)2199zig_convert_builtin(zig_f128, extend, zig_f64, 2)
2097zig_convert_builtin(f128, extend, f80, 2)2200zig_convert_builtin(zig_f128, extend, zig_f80, 2)
20982201
2099#define zig_float_negate_builtin_0(Type) \2202#define zig_float_negate_builtin_0(Type) \
2100 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \2203 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)); \2204 return zig_expand_concat(zig_xor_i, zig_bitSizeOf_repr_##Type)( \
2205 arg, \
2206 zig_minInt_i(zig_bitSizeOf_repr_##Type, zig_bitSizeOf_##Type) \
2207 ); \
2102 }2208 }
2103#define zig_float_negate_builtin_1(Type) \2209#define zig_float_negate_builtin_1(Type) \
2104 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \2210 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
...@@ -2106,28 +2212,28 @@ zig_convert_builtin(f128, extend, f80, 2)...@@ -2106,28 +2212,28 @@ zig_convert_builtin(f128, extend, f80, 2)
2106 }2212 }
21072213
2108#define zig_float_less_builtin_0(Type, operation) \2214#define zig_float_less_builtin_0(Type, operation) \
2109 zig_extern zig_i32 zig_expand_concat(zig_expand_concat(__##operation, \2215 zig_extern int32_t zig_expand_concat(zig_expand_concat(__##operation, \
2110 zig_compiler_rt_abbrev_##Type), 2)(zig_##Type, zig_##Type); \2216 zig_compiler_rt_abbrev_zig_##Type), 2)(zig_##Type, zig_##Type); \
2111 static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \2217 static inline int32_t 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); \2218 return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_zig_##Type), 2)(lhs, rhs); \
2113 }2219 }
2114#define zig_float_less_builtin_1(Type, operation) \2220#define zig_float_less_builtin_1(Type, operation) \
2115 static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \2221 static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
2116 return (!(lhs <= rhs) - (lhs < rhs)); \2222 return (!(lhs <= rhs) - (lhs < rhs)); \
2117 }2223 }
21182224
2119#define zig_float_greater_builtin_0(Type, operation) \2225#define zig_float_greater_builtin_0(Type, operation) \
2120 zig_float_less_builtin_0(Type, operation)2226 zig_float_less_builtin_0(Type, operation)
2121#define zig_float_greater_builtin_1(Type, operation) \2227#define zig_float_greater_builtin_1(Type, operation) \
2122 static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \2228 static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
2123 return ((lhs > rhs) - !(lhs >= rhs)); \2229 return ((lhs > rhs) - !(lhs >= rhs)); \
2124 }2230 }
21252231
2126#define zig_float_binary_builtin_0(Type, operation, operator) \2232#define zig_float_binary_builtin_0(Type, operation, operator) \
2127 zig_extern zig_##Type zig_expand_concat(zig_expand_concat(__##operation, \2233 zig_extern zig_##Type zig_expand_concat(zig_expand_concat(__##operation, \
2128 zig_compiler_rt_abbrev_##Type), 3)(zig_##Type, zig_##Type); \2234 zig_compiler_rt_abbrev_zig_##Type), 3)(zig_##Type, zig_##Type); \
2129 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \2235 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); \2236 return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_zig_##Type), 3)(lhs, rhs); \
2131 }2237 }
2132#define zig_float_binary_builtin_1(Type, operation, operator) \2238#define zig_float_binary_builtin_1(Type, operation, operator) \
2133 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \2239 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
...@@ -2135,18 +2241,18 @@ zig_convert_builtin(f128, extend, f80, 2)...@@ -2135,18 +2241,18 @@ zig_convert_builtin(f128, extend, f80, 2)
2135 }2241 }
21362242
2137#define zig_float_builtins(Type) \2243#define zig_float_builtins(Type) \
2138 zig_convert_builtin(i32, fix, Type, ) \2244 zig_convert_builtin( int32_t, fix, zig_##Type, ) \
2139 zig_convert_builtin(u32, fixuns, Type, ) \2245 zig_convert_builtin(uint32_t, fixuns, zig_##Type, ) \
2140 zig_convert_builtin(i64, fix, Type, ) \2246 zig_convert_builtin( int64_t, fix, zig_##Type, ) \
2141 zig_convert_builtin(u64, fixuns, Type, ) \2247 zig_convert_builtin(uint64_t, fixuns, zig_##Type, ) \
2142 zig_convert_builtin(i128, fix, Type, ) \2248 zig_convert_builtin(zig_i128, fix, zig_##Type, ) \
2143 zig_convert_builtin(u128, fixuns, Type, ) \2249 zig_convert_builtin(zig_u128, fixuns, zig_##Type, ) \
2144 zig_convert_builtin(Type, float, i32, ) \2250 zig_convert_builtin(zig_##Type, float, int32_t, ) \
2145 zig_convert_builtin(Type, floatun, u32, ) \2251 zig_convert_builtin(zig_##Type, floatun, uint32_t, ) \
2146 zig_convert_builtin(Type, float, i64, ) \2252 zig_convert_builtin(zig_##Type, float, int64_t, ) \
2147 zig_convert_builtin(Type, floatun, u64, ) \2253 zig_convert_builtin(zig_##Type, floatun, uint64_t, ) \
2148 zig_convert_builtin(Type, float, i128, ) \2254 zig_convert_builtin(zig_##Type, float, zig_i128, ) \
2149 zig_convert_builtin(Type, floatun, u128, ) \2255 zig_convert_builtin(zig_##Type, floatun, zig_u128, ) \
2150 zig_expand_concat(zig_float_negate_builtin_, zig_has_##Type)(Type) \2256 zig_expand_concat(zig_float_negate_builtin_, zig_has_##Type)(Type) \
2151 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, cmp) \2257 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, cmp) \
2152 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, ne) \2258 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, ne) \
...@@ -2332,17 +2438,17 @@ zig_msvc_flt_atomics(f64, u64, 64)...@@ -2332,17 +2438,17 @@ zig_msvc_flt_atomics(f64, u64, 64)
23322438
2333#if _M_IX862439#if _M_IX86
2334static inline void zig_msvc_atomic_barrier() {2440static inline void zig_msvc_atomic_barrier() {
2335 zig_i32 barrier;2441 int32_t barrier;
2336 __asm {2442 __asm {
2337 xchg barrier, eax2443 xchg barrier, eax
2338 }2444 }
2339}2445}
23402446
2341static inline void* zig_msvc_atomicrmw_xchg_p32(void** obj, zig_u32* arg) {2447static inline void* zig_msvc_atomicrmw_xchg_p32(void** obj, uint32_t* arg) {
2342 return _InterlockedExchangePointer(obj, arg);2448 return _InterlockedExchangePointer(obj, arg);
2343}2449}
23442450
2345static inline void zig_msvc_atomic_store_p32(void** obj, zig_u32* arg) {2451static inline void zig_msvc_atomic_store_p32(void** obj, uint32_t* arg) {
2346 _InterlockedExchangePointer(obj, arg);2452 _InterlockedExchangePointer(obj, arg);
2347}2453}
23482454
...@@ -2360,11 +2466,11 @@ static inline bool zig_msvc_cmpxchg_p32(void** obj, void** expected, void* desir...@@ -2360,11 +2466,11 @@ static inline bool zig_msvc_cmpxchg_p32(void** obj, void** expected, void* desir
2360 return exchanged;2466 return exchanged;
2361}2467}
2362#else /* _M_IX86 */2468#else /* _M_IX86 */
2363static inline void* zig_msvc_atomicrmw_xchg_p64(void** obj, zig_u64* arg) {2469static inline void* zig_msvc_atomicrmw_xchg_p64(void** obj, uint64_t* arg) {
2364 return _InterlockedExchangePointer(obj, arg);2470 return _InterlockedExchangePointer(obj, arg);
2365}2471}
23662472
2367static inline void zig_msvc_atomic_store_p64(void** obj, zig_u64* arg) {2473static inline void zig_msvc_atomic_store_p64(void** obj, uint64_t* arg) {
2368 _InterlockedExchangePointer(obj, arg);2474 _InterlockedExchangePointer(obj, arg);
2369}2475}
23702476
...@@ -2383,11 +2489,11 @@ static inline bool zig_msvc_cmpxchg_p64(void** obj, void** expected, void* desir...@@ -2383,11 +2489,11 @@ static inline bool zig_msvc_cmpxchg_p64(void** obj, void** expected, void* desir
2383}2489}
23842490
2385static inline bool zig_msvc_cmpxchg_u128(zig_u128 volatile* obj, zig_u128* expected, zig_u128 desired) {2491static 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);2492 return _InterlockedCompareExchange128((int64_t volatile*)obj, desired.hi, desired.lo, (int64_t*)expected);
2387}2493}
23882494
2389static inline bool zig_msvc_cmpxchg_i128(zig_i128 volatile* obj, zig_i128* expected, zig_i128 desired) {2495static 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);2496 return _InterlockedCompareExchange128((int64_t volatile*)obj, desired.hi, desired.lo, (uint64_t*)expected);
2391}2497}
23922498
2393#define zig_msvc_atomics_128xchg(Type) \2499#define zig_msvc_atomics_128xchg(Type) \
...@@ -2429,7 +2535,7 @@ zig_msvc_atomics_128op(u128, max)...@@ -2429,7 +2535,7 @@ zig_msvc_atomics_128op(u128, max)
24292535
2430#endif /* _MSC_VER && (_M_IX86 || _M_X64) */2536#endif /* _MSC_VER && (_M_IX86 || _M_X64) */
24312537
2432/* ========================= Special Case Intrinsics ========================= */2538/* ======================== Special Case Intrinsics ========================= */
24332539
2434#if (_MSC_VER && _M_X64) || defined(__x86_64__)2540#if (_MSC_VER && _M_X64) || defined(__x86_64__)
24352541
...@@ -2459,8 +2565,8 @@ static inline void* zig_x86_windows_teb(void) {...@@ -2459,8 +2565,8 @@ static inline void* zig_x86_windows_teb(void) {
24592565
2460#if (_MSC_VER && (_M_IX86 || _M_X64)) || defined(__i386__) || defined(__x86_64__)2566#if (_MSC_VER && (_M_IX86 || _M_X64)) || defined(__i386__) || defined(__x86_64__)
24612567
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) {2568static inline void zig_x86_cpuid(uint32_t leaf_id, uint32_t subid, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx) {
2463 zig_u32 cpu_info[4];2569 uint32_t cpu_info[4];
2464#if _MSC_VER2570#if _MSC_VER
2465 __cpuidex(cpu_info, leaf_id, subid);2571 __cpuidex(cpu_info, leaf_id, subid);
2466#else2572#else
...@@ -2472,12 +2578,12 @@ static inline void zig_x86_cpuid(zig_u32 leaf_id, zig_u32 subid, zig_u32* eax, z...@@ -2472,12 +2578,12 @@ static inline void zig_x86_cpuid(zig_u32 leaf_id, zig_u32 subid, zig_u32* eax, z
2472 *edx = cpu_info[3];2578 *edx = cpu_info[3];
2473}2579}
24742580
2475static inline zig_u32 zig_x86_get_xcr0(void) {2581static inline uint32_t zig_x86_get_xcr0(void) {
2476#if _MSC_VER2582#if _MSC_VER
2477 return (zig_u32)_xgetbv(0);2583 return (uint32_t)_xgetbv(0);
2478#else2584#else
2479 zig_u32 eax;2585 uint32_t eax;
2480 zig_u32 edx;2586 uint32_t edx;
2481 __asm__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0));2587 __asm__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0));
2482 return eax;2588 return eax;
2483#endif2589#endif
src/codegen/c.zig+39-30
...@@ -752,7 +752,7 @@ pub const DeclGen = struct {...@@ -752,7 +752,7 @@ pub const DeclGen = struct {
752752
753 try writer.writeAll("zig_cast_");753 try writer.writeAll("zig_cast_");
754 try dg.renderTypeForBuiltinFnName(writer, ty);754 try dg.renderTypeForBuiltinFnName(writer, ty);
755 try writer.writeAll(" zig_as_");755 try writer.writeAll(" zig_make_");
756 try dg.renderTypeForBuiltinFnName(writer, ty);756 try dg.renderTypeForBuiltinFnName(writer, ty);
757 try writer.writeByte('(');757 try writer.writeByte('(');
758 switch (bits) {758 switch (bits) {
...@@ -962,7 +962,7 @@ pub const DeclGen = struct {...@@ -962,7 +962,7 @@ pub const DeclGen = struct {
962 try writer.writeByte(' ');962 try writer.writeByte(' ');
963 var empty = true;963 var empty = true;
964 if (std.math.isFinite(f128_val)) {964 if (std.math.isFinite(f128_val)) {
965 try writer.writeAll("zig_as_");965 try writer.writeAll("zig_make_");
966 try dg.renderTypeForBuiltinFnName(writer, ty);966 try dg.renderTypeForBuiltinFnName(writer, ty);
967 try writer.writeByte('(');967 try writer.writeByte('(');
968 switch (bits) {968 switch (bits) {
...@@ -997,7 +997,7 @@ pub const DeclGen = struct {...@@ -997,7 +997,7 @@ pub const DeclGen = struct {
997 // return dg.fail("Only quiet nans are supported in global variable initializers", .{});997 // return dg.fail("Only quiet nans are supported in global variable initializers", .{});
998 }998 }
999999
1000 try writer.writeAll("zig_as_special_");1000 try writer.writeAll("zig_make_special_");
1001 if (location == .StaticInitializer) try writer.writeAll("constant_");1001 if (location == .StaticInitializer) try writer.writeAll("constant_");
1002 try dg.renderTypeForBuiltinFnName(writer, ty);1002 try dg.renderTypeForBuiltinFnName(writer, ty);
1003 try writer.writeByte('(');1003 try writer.writeByte('(');
...@@ -2016,14 +2016,16 @@ pub const DeclGen = struct {...@@ -2016,14 +2016,16 @@ pub const DeclGen = struct {
2016 .bool,2016 .bool,
2017 .size_t,2017 .size_t,
2018 .ptrdiff_t,2018 .ptrdiff_t,
2019 .zig_u8,2019 .uint8_t,
2020 .zig_i8,2020 .int8_t,
2021 .zig_u16,2021 .uint16_t,
2022 .zig_i16,2022 .int16_t,
2023 .zig_u32,2023 .uint32_t,
2024 .zig_i32,2024 .int32_t,
2025 .zig_u64,2025 .uint64_t,
2026 .zig_i64,2026 .int64_t,
2027 .uintptr_t,
2028 .intptr_t,
2027 .zig_u128,2029 .zig_u128,
2028 .zig_i128,2030 .zig_i128,
2029 .zig_f16,2031 .zig_f16,
...@@ -2158,14 +2160,16 @@ pub const DeclGen = struct {...@@ -2158,14 +2160,16 @@ pub const DeclGen = struct {
2158 .bool,2160 .bool,
2159 .size_t,2161 .size_t,
2160 .ptrdiff_t,2162 .ptrdiff_t,
2161 .zig_u8,2163 .uint8_t,
2162 .zig_i8,2164 .int8_t,
2163 .zig_u16,2165 .uint16_t,
2164 .zig_i16,2166 .int16_t,
2165 .zig_u32,2167 .uint32_t,
2166 .zig_i32,2168 .int32_t,
2167 .zig_u64,2169 .uint64_t,
2168 .zig_i64,2170 .int64_t,
2171 .uintptr_t,
2172 .intptr_t,
2169 .zig_u128,2173 .zig_u128,
2170 .zig_i128,2174 .zig_i128,
2171 .zig_f16,2175 .zig_f16,
...@@ -2285,16 +2289,16 @@ pub const DeclGen = struct {...@@ -2285,16 +2289,16 @@ pub const DeclGen = struct {
2285 /// Renders a cast to an int type, from either an int or a pointer.2289 /// Renders a cast to an int type, from either an int or a pointer.
2286 ///2290 ///
2287 /// Some platforms don't have 128 bit integers, so we need to use2291 /// Some platforms don't have 128 bit integers, so we need to use
2288 /// the zig_as_ and zig_lo_ macros in those cases.2292 /// the zig_make_ and zig_lo_ macros in those cases.
2289 ///2293 ///
2290 /// | Dest type bits | Src type | Result2294 /// | Dest type bits | Src type | Result
2291 /// |------------------|------------------|---------------------------|2295 /// |------------------|------------------|---------------------------|
2292 /// | < 64 bit integer | pointer | (zig_<dest_ty>)(zig_<u|i>size)src2296 /// | < 64 bit integer | pointer | (zig_<dest_ty>)(zig_<u|i>size)src
2293 /// | < 64 bit integer | < 64 bit integer | (zig_<dest_ty>)src2297 /// | < 64 bit integer | < 64 bit integer | (zig_<dest_ty>)src
2294 /// | < 64 bit integer | > 64 bit integer | zig_lo(src)2298 /// | < 64 bit integer | > 64 bit integer | zig_lo(src)
2295 /// | > 64 bit integer | pointer | zig_as_<dest_ty>(0, (zig_<u|i>size)src)2299 /// | > 64 bit integer | pointer | zig_make_<dest_ty>(0, (zig_<u|i>size)src)
2296 /// | > 64 bit integer | < 64 bit integer | zig_as_<dest_ty>(0, src)2300 /// | > 64 bit integer | < 64 bit integer | zig_make_<dest_ty>(0, src)
2297 /// | > 64 bit integer | > 64 bit integer | zig_as_<dest_ty>(zig_hi_<src_ty>(src), zig_lo_<src_ty>(src))2301 /// | > 64 bit integer | > 64 bit integer | zig_make_<dest_ty>(zig_hi_<src_ty>(src), zig_lo_<src_ty>(src))
2298 fn renderIntCast(dg: *DeclGen, w: anytype, dest_ty: Type, context: IntCastContext, src_ty: Type, location: ValueRenderLocation) !void {2302 fn renderIntCast(dg: *DeclGen, w: anytype, dest_ty: Type, context: IntCastContext, src_ty: Type, location: ValueRenderLocation) !void {
2299 const target = dg.module.getTarget();2303 const target = dg.module.getTarget();
2300 const dest_bits = dest_ty.bitSize(target);2304 const dest_bits = dest_ty.bitSize(target);
...@@ -2332,7 +2336,7 @@ pub const DeclGen = struct {...@@ -2332,7 +2336,7 @@ pub const DeclGen = struct {
2332 try context.writeValue(dg, w, src_ty, .FunctionArgument);2336 try context.writeValue(dg, w, src_ty, .FunctionArgument);
2333 try w.writeByte(')');2337 try w.writeByte(')');
2334 } else if (dest_bits > 64 and src_bits <= 64) {2338 } else if (dest_bits > 64 and src_bits <= 64) {
2335 try w.writeAll("zig_as_");2339 try w.writeAll("zig_make_");
2336 try dg.renderTypeForBuiltinFnName(w, dest_ty);2340 try dg.renderTypeForBuiltinFnName(w, dest_ty);
2337 try w.writeAll("(0, "); // TODO: Should the 0 go through fmtIntLiteral?2341 try w.writeAll("(0, "); // TODO: Should the 0 go through fmtIntLiteral?
2338 if (src_is_ptr) {2342 if (src_is_ptr) {
...@@ -2344,7 +2348,7 @@ pub const DeclGen = struct {...@@ -2344,7 +2348,7 @@ pub const DeclGen = struct {
2344 try w.writeByte(')');2348 try w.writeByte(')');
2345 } else {2349 } else {
2346 assert(!src_is_ptr);2350 assert(!src_is_ptr);
2347 try w.writeAll("zig_as_");2351 try w.writeAll("zig_make_");
2348 try dg.renderTypeForBuiltinFnName(w, dest_ty);2352 try dg.renderTypeForBuiltinFnName(w, dest_ty);
2349 try w.writeAll("(zig_hi_");2353 try w.writeAll("(zig_hi_");
2350 try dg.renderTypeForBuiltinFnName(w, src_eff_ty);2354 try dg.renderTypeForBuiltinFnName(w, src_eff_ty);
...@@ -3858,7 +3862,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3858,7 +3862,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue {
3858 const cant_cast = host_ty.isInt() and host_ty.bitSize(target) > 64;3862 const cant_cast = host_ty.isInt() and host_ty.bitSize(target) > 64;
3859 if (cant_cast) {3863 if (cant_cast) {
3860 if (src_ty.bitSize(target) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});3864 if (src_ty.bitSize(target) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});
3861 try writer.writeAll("zig_as_");3865 try writer.writeAll("zig_make_");
3862 try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty);3866 try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty);
3863 try writer.writeAll("(0, ");3867 try writer.writeAll("(0, ");
3864 } else {3868 } else {
...@@ -7355,7 +7359,7 @@ fn formatIntLiteral(...@@ -7355,7 +7359,7 @@ fn formatIntLiteral(
7355 use_twos_comp = true;7359 use_twos_comp = true;
7356 } else {7360 } else {
7357 // TODO: Use fmtIntLiteral for 0?7361 // TODO: Use fmtIntLiteral for 0?
7358 try writer.print("zig_sub_{c}{d}(zig_as_{c}{d}(0, 0), ", .{ signAbbrev(int_info.signedness), c_bits, signAbbrev(int_info.signedness), c_bits });7362 try writer.print("zig_sub_{c}{d}(zig_make_{c}{d}(0, 0), ", .{ signAbbrev(int_info.signedness), c_bits, signAbbrev(int_info.signedness), c_bits });
7359 }7363 }
7360 } else {7364 } else {
7361 try writer.writeByte('-');7365 try writer.writeByte('-');
...@@ -7365,11 +7369,16 @@ fn formatIntLiteral(...@@ -7365,11 +7369,16 @@ fn formatIntLiteral(
7365 switch (data.ty.tag()) {7369 switch (data.ty.tag()) {
7366 .c_short, .c_ushort, .c_int, .c_uint, .c_long, .c_ulong, .c_longlong, .c_ulonglong => {},7370 .c_short, .c_ushort, .c_int, .c_uint, .c_long, .c_ulong, .c_longlong, .c_ulonglong => {},
7367 else => {7371 else => {
7368 if (int_info.bits > 64 and data.location != null and data.location.? == .StaticInitializer) {7372 if (int_info.bits <= 64) {
7373 try writer.print("{s}INT{d}_C(", .{ switch (int_info.signedness) {
7374 .signed => "",
7375 .unsigned => "U",
7376 }, c_bits });
7377 } else if (data.location != null and data.location.? == .StaticInitializer) {
7369 // MSVC treats casting the struct initializer as not constant (C2099), so an alternate form is used in global initializers7378 // MSVC treats casting the struct initializer as not constant (C2099), so an alternate form is used in global initializers
7370 try writer.print("zig_as_constant_{c}{d}(", .{ signAbbrev(int_info.signedness), c_bits });7379 try writer.print("zig_make_constant_{c}{d}(", .{ signAbbrev(int_info.signedness), c_bits });
7371 } else {7380 } else {
7372 try writer.print("zig_as_{c}{d}(", .{ signAbbrev(int_info.signedness), c_bits });7381 try writer.print("zig_make_{c}{d}(", .{ signAbbrev(int_info.signedness), c_bits });
7373 }7382 }
7374 },7383 },
7375 }7384 }
src/codegen/c/type.zig+66-53
...@@ -77,19 +77,24 @@ pub const CType = extern union {...@@ -77,19 +77,24 @@ pub const CType = extern union {
77 @"long double",77 @"long double",
7878
79 // C header types79 // C header types
80 bool, // stdbool.h80 // - stdbool.h
81 size_t, // stddef.h81 bool,
82 ptrdiff_t, // stddef.h82 // - stddef.h
83 size_t,
84 ptrdiff_t,
85 // - stdint.h
86 uint8_t,
87 int8_t,
88 uint16_t,
89 int16_t,
90 uint32_t,
91 int32_t,
92 uint64_t,
93 int64_t,
94 uintptr_t,
95 intptr_t,
8396
84 // zig.h types97 // zig.h types
85 zig_u8,
86 zig_i8,
87 zig_u16,
88 zig_i16,
89 zig_u32,
90 zig_i32,
91 zig_u64,
92 zig_i64,
93 zig_u128,98 zig_u128,
94 zig_i128,99 zig_i128,
95 zig_f16,100 zig_f16,
...@@ -149,14 +154,16 @@ pub const CType = extern union {...@@ -149,14 +154,16 @@ pub const CType = extern union {
149 .bool,154 .bool,
150 .size_t,155 .size_t,
151 .ptrdiff_t,156 .ptrdiff_t,
152 .zig_u8,157 .uint8_t,
153 .zig_i8,158 .int8_t,
154 .zig_u16,159 .uint16_t,
155 .zig_i16,160 .int16_t,
156 .zig_u32,161 .uint32_t,
157 .zig_i32,162 .int32_t,
158 .zig_u64,163 .uint64_t,
159 .zig_i64,164 .int64_t,
165 .uintptr_t,
166 .intptr_t,
160 .zig_u128,167 .zig_u128,
161 .zig_i128,168 .zig_i128,
162 .zig_f16,169 .zig_f16,
...@@ -428,14 +435,16 @@ pub const CType = extern union {...@@ -428,14 +435,16 @@ pub const CType = extern union {
428 .bool,435 .bool,
429 .size_t,436 .size_t,
430 .ptrdiff_t,437 .ptrdiff_t,
431 .zig_u8,438 .uint8_t,
432 .zig_i8,439 .int8_t,
433 .zig_u16,440 .uint16_t,
434 .zig_i16,441 .int16_t,
435 .zig_u32,442 .uint32_t,
436 .zig_i32,443 .int32_t,
437 .zig_u64,444 .uint64_t,
438 .zig_i64,445 .int64_t,
446 .uintptr_t,
447 .intptr_t,
439 .zig_u128,448 .zig_u128,
440 .zig_i128,449 .zig_i128,
441 .zig_f16,450 .zig_f16,
...@@ -526,14 +535,16 @@ pub const CType = extern union {...@@ -526,14 +535,16 @@ pub const CType = extern union {
526 .bool,535 .bool,
527 .size_t,536 .size_t,
528 .ptrdiff_t,537 .ptrdiff_t,
529 .zig_u8,538 .uint8_t,
530 .zig_i8,539 .int8_t,
531 .zig_u16,540 .uint16_t,
532 .zig_i16,541 .int16_t,
533 .zig_u32,542 .uint32_t,
534 .zig_i32,543 .int32_t,
535 .zig_u64,544 .uint64_t,
536 .zig_i64,545 .int64_t,
546 .uintptr_t,
547 .intptr_t,
537 .zig_u128,548 .zig_u128,
538 .zig_i128,549 .zig_i128,
539 .zig_f16,550 .zig_f16,
...@@ -628,20 +639,20 @@ pub const CType = extern union {...@@ -628,20 +639,20 @@ pub const CType = extern union {
628 return switch (bits) {639 return switch (bits) {
629 0 => .void,640 0 => .void,
630 1...8 => switch (signedness) {641 1...8 => switch (signedness) {
631 .unsigned => .zig_u8,642 .unsigned => .uint8_t,
632 .signed => .zig_i8,643 .signed => .int8_t,
633 },644 },
634 9...16 => switch (signedness) {645 9...16 => switch (signedness) {
635 .unsigned => .zig_u16,646 .unsigned => .uint16_t,
636 .signed => .zig_i16,647 .signed => .int16_t,
637 },648 },
638 17...32 => switch (signedness) {649 17...32 => switch (signedness) {
639 .unsigned => .zig_u32,650 .unsigned => .uint32_t,
640 .signed => .zig_i32,651 .signed => .int32_t,
641 },652 },
642 33...64 => switch (signedness) {653 33...64 => switch (signedness) {
643 .unsigned => .zig_u64,654 .unsigned => .uint64_t,
644 .signed => .zig_i64,655 .signed => .int64_t,
645 },656 },
646 65...128 => switch (signedness) {657 65...128 => switch (signedness) {
647 .unsigned => .zig_u128,658 .unsigned => .zig_u128,
...@@ -712,8 +723,8 @@ pub const CType = extern union {...@@ -712,8 +723,8 @@ pub const CType = extern union {
712 if (!ty.isFnOrHasRuntimeBitsIgnoreComptime())723 if (!ty.isFnOrHasRuntimeBitsIgnoreComptime())
713 self.init(.void)724 self.init(.void)
714 else if (ty.isAbiInt()) switch (ty.tag()) {725 else if (ty.isAbiInt()) switch (ty.tag()) {
715 .usize => self.init(.size_t),726 .usize => self.init(.uintptr_t),
716 .isize => self.init(.ptrdiff_t),727 .isize => self.init(.intptr_t),
717 .c_short => self.init(.short),728 .c_short => self.init(.short),
718 .c_ushort => self.init(.@"unsigned short"),729 .c_ushort => self.init(.@"unsigned short"),
719 .c_int => self.init(.int),730 .c_int => self.init(.int),
...@@ -996,14 +1007,16 @@ pub const CType = extern union {...@@ -996,14 +1007,16 @@ pub const CType = extern union {
996 .bool,1007 .bool,
997 .size_t,1008 .size_t,
998 .ptrdiff_t,1009 .ptrdiff_t,
999 .zig_u8,1010 .uint8_t,
1000 .zig_i8,1011 .int8_t,
1001 .zig_u16,1012 .uint16_t,
1002 .zig_i16,1013 .int16_t,
1003 .zig_u32,1014 .uint32_t,
1004 .zig_i32,1015 .int32_t,
1005 .zig_u64,1016 .uint64_t,
1006 .zig_i64,1017 .int64_t,
1018 .uintptr_t,
1019 .intptr_t,
1007 .zig_u128,1020 .zig_u128,
1008 .zig_i128,1021 .zig_i128,
1009 .zig_f16,1022 .zig_f16,
stage1/zig.h created+2486
...@@ -0,0 +1,2486 @@
1#undef linux
2
3#define __STDC_WANT_IEC_60559_TYPES_EXT__
4#include <float.h>
5#include <limits.h>
6#include <stddef.h>
7#include <stdint.h>
8
9#if _MSC_VER
10#include <intrin.h>
11#elif defined(__i386__) || defined(__x86_64__)
12#include <cpuid.h>
13#endif
14
15#if !defined(__cplusplus) && __STDC_VERSION__ <= 201710L
16#if __STDC_VERSION__ >= 199901L
17#include <stdbool.h>
18#else
19typedef char bool;
20#define false 0
21#define true 1
22#endif
23#endif
24
25#if defined(__has_builtin)
26#define zig_has_builtin(builtin) __has_builtin(__builtin_##builtin)
27#else
28#define zig_has_builtin(builtin) 0
29#endif
30
31#if defined(__has_attribute)
32#define zig_has_attribute(attribute) __has_attribute(attribute)
33#else
34#define zig_has_attribute(attribute) 0
35#endif
36
37#if __STDC_VERSION__ >= 201112L
38#define zig_threadlocal _Thread_local
39#elif defined(__GNUC__)
40#define zig_threadlocal __thread
41#elif _MSC_VER
42#define zig_threadlocal __declspec(thread)
43#else
44#define zig_threadlocal zig_threadlocal_unavailable
45#endif
46
47#if defined(__clang__)
48#define zig_clang
49#elif defined(__GNUC__)
50#define zig_gnuc
51#endif
52
53#if _MSC_VER
54#define zig_const_arr
55#define zig_callconv(c) __##c
56#else
57#define zig_const_arr static const
58#define zig_callconv(c) __attribute__((c))
59#endif
60
61#if zig_has_attribute(naked) || defined(zig_gnuc)
62#define zig_naked_decl __attribute__((naked))
63#define zig_naked __attribute__((naked))
64#elif defined(_MSC_VER)
65#define zig_naked_decl
66#define zig_naked __declspec(naked)
67#else
68#define zig_naked_decl zig_naked_unavailable
69#define zig_naked zig_naked_unavailable
70#endif
71
72#if zig_has_attribute(cold)
73#define zig_cold __attribute__((cold))
74#else
75#define zig_cold
76#endif
77
78#if __STDC_VERSION__ >= 199901L
79#define zig_restrict restrict
80#elif defined(__GNUC__)
81#define zig_restrict __restrict
82#else
83#define zig_restrict
84#endif
85
86#if __STDC_VERSION__ >= 201112L
87#define zig_align(alignment) _Alignas(alignment)
88#elif zig_has_attribute(aligned)
89#define zig_align(alignment) __attribute__((aligned(alignment)))
90#elif _MSC_VER
91#define zig_align(alignment) __declspec(align(alignment))
92#else
93#define zig_align zig_align_unavailable
94#endif
95
96#if zig_has_attribute(aligned)
97#define zig_under_align(alignment) __attribute__((aligned(alignment)))
98#elif _MSC_VER
99#define zig_under_align(alignment) zig_align(alignment)
100#else
101#define zig_align zig_align_unavailable
102#endif
103
104#if zig_has_attribute(aligned)
105#define zig_align_fn(alignment) __attribute__((aligned(alignment)))
106#elif _MSC_VER
107#define zig_align_fn(alignment)
108#else
109#define zig_align_fn zig_align_fn_unavailable
110#endif
111
112#if zig_has_attribute(packed)
113#define zig_packed(definition) __attribute__((packed)) definition
114#elif _MSC_VER
115#define zig_packed(definition) __pragma(pack(1)) definition __pragma(pack())
116#else
117#define zig_packed(definition) zig_packed_unavailable
118#endif
119
120#if zig_has_attribute(section)
121#define zig_linksection(name, def, ...) def __attribute__((section(name)))
122#elif _MSC_VER
123#define zig_linksection(name, def, ...) __pragma(section(name, __VA_ARGS__)) __declspec(allocate(name)) def
124#else
125#define zig_linksection(name, def, ...) zig_linksection_unavailable
126#endif
127
128#if zig_has_builtin(unreachable) || defined(zig_gnuc)
129#define zig_unreachable() __builtin_unreachable()
130#else
131#define zig_unreachable()
132#endif
133
134#if defined(__cplusplus)
135#define zig_extern extern "C"
136#else
137#define zig_extern extern
138#endif
139
140#if zig_has_attribute(alias)
141#define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(symbol)))
142#elif _MSC_VER
143#if _M_X64
144#define zig_export(sig, symbol, name) sig;\
145 __pragma(comment(linker, "/alternatename:" name "=" symbol ))
146#else /*_M_X64 */
147#define zig_export(sig, symbol, name) sig;\
148 __pragma(comment(linker, "/alternatename:_" name "=_" symbol ))
149#endif /*_M_X64 */
150#else
151#define zig_export(sig, symbol, name) __asm(name " = " symbol)
152#endif
153
154#if zig_has_builtin(debugtrap)
155#define zig_breakpoint() __builtin_debugtrap()
156#elif zig_has_builtin(trap) || defined(zig_gnuc)
157#define zig_breakpoint() __builtin_trap()
158#elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
159#define zig_breakpoint() __debugbreak()
160#elif defined(__i386__) || defined(__x86_64__)
161#define zig_breakpoint() __asm__ volatile("int $0x03");
162#else
163#define zig_breakpoint() raise(SIGTRAP)
164#endif
165
166#if zig_has_builtin(return_address) || defined(zig_gnuc)
167#define zig_return_address() __builtin_extract_return_addr(__builtin_return_address(0))
168#elif defined(_MSC_VER)
169#define zig_return_address() _ReturnAddress()
170#else
171#define zig_return_address() 0
172#endif
173
174#if zig_has_builtin(frame_address) || defined(zig_gnuc)
175#define zig_frame_address() __builtin_frame_address(0)
176#else
177#define zig_frame_address() 0
178#endif
179
180#if zig_has_builtin(prefetch) || defined(zig_gnuc)
181#define zig_prefetch(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
182#else
183#define zig_prefetch(addr, rw, locality)
184#endif
185
186#if zig_has_builtin(memory_size) && zig_has_builtin(memory_grow)
187#define zig_wasm_memory_size(index) __builtin_wasm_memory_size(index)
188#define zig_wasm_memory_grow(index, delta) __builtin_wasm_memory_grow(index, delta)
189#else
190#define zig_wasm_memory_size(index) zig_unimplemented()
191#define zig_wasm_memory_grow(index, delta) zig_unimplemented()
192#endif
193
194#define zig_concat(lhs, rhs) lhs##rhs
195#define zig_expand_concat(lhs, rhs) zig_concat(lhs, rhs)
196
197#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
198#include <stdatomic.h>
199#define zig_atomic(type) _Atomic(type)
200#define zig_cmpxchg_strong(obj, expected, desired, succ, fail, type) atomic_compare_exchange_strong_explicit(obj, &(expected), desired, succ, fail)
201#define zig_cmpxchg_weak(obj, expected, desired, succ, fail, type) atomic_compare_exchange_weak_explicit (obj, &(expected), desired, succ, fail)
202#define zig_atomicrmw_xchg(obj, arg, order, type) atomic_exchange_explicit (obj, arg, order)
203#define zig_atomicrmw_add(obj, arg, order, type) atomic_fetch_add_explicit (obj, arg, order)
204#define zig_atomicrmw_sub(obj, arg, order, type) atomic_fetch_sub_explicit (obj, arg, order)
205#define zig_atomicrmw_or(obj, arg, order, type) atomic_fetch_or_explicit (obj, arg, order)
206#define zig_atomicrmw_xor(obj, arg, order, type) atomic_fetch_xor_explicit (obj, arg, order)
207#define zig_atomicrmw_and(obj, arg, order, type) atomic_fetch_and_explicit (obj, arg, order)
208#define zig_atomicrmw_nand(obj, arg, order, type) __atomic_fetch_nand (obj, arg, order)
209#define zig_atomicrmw_min(obj, arg, order, type) __atomic_fetch_min (obj, arg, order)
210#define zig_atomicrmw_max(obj, arg, order, type) __atomic_fetch_max (obj, arg, order)
211#define zig_atomic_store(obj, arg, order, type) atomic_store_explicit (obj, arg, order)
212#define zig_atomic_load(obj, order, type) atomic_load_explicit (obj, order)
213#define zig_fence(order) atomic_thread_fence(order)
214#elif defined(__GNUC__)
215#define memory_order_relaxed __ATOMIC_RELAXED
216#define memory_order_consume __ATOMIC_CONSUME
217#define memory_order_acquire __ATOMIC_ACQUIRE
218#define memory_order_release __ATOMIC_RELEASE
219#define memory_order_acq_rel __ATOMIC_ACQ_REL
220#define memory_order_seq_cst __ATOMIC_SEQ_CST
221#define zig_atomic(type) type
222#define zig_cmpxchg_strong(obj, expected, desired, succ, fail, type) __atomic_compare_exchange_n(obj, &(expected), desired, false, succ, fail)
223#define zig_cmpxchg_weak(obj, expected, desired, succ, fail, type) __atomic_compare_exchange_n(obj, &(expected), desired, true , succ, fail)
224#define zig_atomicrmw_xchg(obj, arg, order, type) __atomic_exchange_n(obj, arg, order)
225#define zig_atomicrmw_add(obj, arg, order, type) __atomic_fetch_add (obj, arg, order)
226#define zig_atomicrmw_sub(obj, arg, order, type) __atomic_fetch_sub (obj, arg, order)
227#define zig_atomicrmw_or(obj, arg, order, type) __atomic_fetch_or (obj, arg, order)
228#define zig_atomicrmw_xor(obj, arg, order, type) __atomic_fetch_xor (obj, arg, order)
229#define zig_atomicrmw_and(obj, arg, order, type) __atomic_fetch_and (obj, arg, order)
230#define zig_atomicrmw_nand(obj, arg, order, type) __atomic_fetch_nand(obj, arg, order)
231#define zig_atomicrmw_min(obj, arg, order, type) __atomic_fetch_min (obj, arg, order)
232#define zig_atomicrmw_max(obj, arg, order, type) __atomic_fetch_max (obj, arg, order)
233#define zig_atomic_store(obj, arg, order, type) __atomic_store_n (obj, arg, order)
234#define zig_atomic_load(obj, order, type) __atomic_load_n (obj, order)
235#define zig_fence(order) __atomic_thread_fence(order)
236#elif _MSC_VER && (_M_IX86 || _M_X64)
237#define memory_order_relaxed 0
238#define memory_order_consume 1
239#define memory_order_acquire 2
240#define memory_order_release 3
241#define memory_order_acq_rel 4
242#define memory_order_seq_cst 5
243#define zig_atomic(type) type
244#define zig_cmpxchg_strong(obj, expected, desired, succ, fail, type) zig_expand_concat(zig_msvc_cmpxchg_, type)(obj, &(expected), desired)
245#define zig_cmpxchg_weak(obj, expected, desired, succ, fail, type) zig_cmpxchg_strong(obj, expected, desired, succ, fail, type)
246#define zig_atomicrmw_xchg(obj, arg, order, type) zig_expand_concat(zig_msvc_atomicrmw_xchg_, type)(obj, arg)
247#define zig_atomicrmw_add(obj, arg, order, type) zig_expand_concat(zig_msvc_atomicrmw_add_, type)(obj, arg)
248#define zig_atomicrmw_sub(obj, arg, order, type) zig_expand_concat(zig_msvc_atomicrmw_sub_, type)(obj, arg)
249#define zig_atomicrmw_or(obj, arg, order, type) zig_expand_concat(zig_msvc_atomicrmw_or_, type)(obj, arg)
250#define zig_atomicrmw_xor(obj, arg, order, type) zig_expand_concat(zig_msvc_atomicrmw_xor_, type)(obj, arg)
251#define zig_atomicrmw_and(obj, arg, order, type) zig_expand_concat(zig_msvc_atomicrmw_and_, type)(obj, arg)
252#define zig_atomicrmw_nand(obj, arg, order, type) zig_expand_concat(zig_msvc_atomicrmw_nand_, type)(obj, arg)
253#define zig_atomicrmw_min(obj, arg, order, type) zig_expand_concat(zig_msvc_atomicrmw_min_, type)(obj, arg)
254#define zig_atomicrmw_max(obj, arg, order, type) zig_expand_concat(zig_msvc_atomicrmw_max_, type)(obj, arg)
255#define zig_atomic_store(obj, arg, order, type) zig_expand_concat(zig_msvc_atomic_store_, type)(obj, arg)
256#define zig_atomic_load(obj, order, type) zig_expand_concat(zig_msvc_atomic_load_, type)(obj)
257#if _M_X64
258#define zig_fence(order) __faststorefence()
259#else
260#define zig_fence(order) zig_msvc_atomic_barrier()
261#endif
262
263// TODO: _MSC_VER && (_M_ARM || _M_ARM64)
264#else
265#define memory_order_relaxed 0
266#define memory_order_consume 1
267#define memory_order_acquire 2
268#define memory_order_release 3
269#define memory_order_acq_rel 4
270#define memory_order_seq_cst 5
271#define zig_atomic(type) type
272#define zig_cmpxchg_strong(obj, expected, desired, succ, fail, type) zig_unimplemented()
273#define zig_cmpxchg_weak(obj, expected, desired, succ, fail, type) zig_unimplemented()
274#define zig_atomicrmw_xchg(obj, arg, order, type) zig_unimplemented()
275#define zig_atomicrmw_add(obj, arg, order, type) zig_unimplemented()
276#define zig_atomicrmw_sub(obj, arg, order, type) zig_unimplemented()
277#define zig_atomicrmw_or(obj, arg, order, type) zig_unimplemented()
278#define zig_atomicrmw_xor(obj, arg, order, type) zig_unimplemented()
279#define zig_atomicrmw_and(obj, arg, order, type) zig_unimplemented()
280#define zig_atomicrmw_nand(obj, arg, order, type) zig_unimplemented()
281#define zig_atomicrmw_min(obj, arg, order, type) zig_unimplemented()
282#define zig_atomicrmw_max(obj, arg, order, type) zig_unimplemented()
283#define zig_atomic_store(obj, arg, order, type) zig_unimplemented()
284#define zig_atomic_load(obj, order, type) zig_unimplemented()
285#define zig_fence(order) zig_unimplemented()
286#endif
287
288#if __STDC_VERSION__ >= 201112L
289#define zig_noreturn _Noreturn void
290#elif zig_has_attribute(noreturn) || defined(zig_gnuc)
291#define zig_noreturn __attribute__((noreturn)) void
292#elif _MSC_VER
293#define zig_noreturn __declspec(noreturn) void
294#else
295#define zig_noreturn void
296#endif
297
298#define zig_bitSizeOf(T) (CHAR_BIT * sizeof(T))
299
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
331#define zig_minInt_i8 INT8_MIN
332#define zig_maxInt_i8 INT8_MAX
333#define zig_minInt_u16 zig_as_u16(0)
334#define zig_maxInt_u16 UINT16_MAX
335#define zig_minInt_i16 INT16_MIN
336#define zig_maxInt_i16 INT16_MAX
337#define zig_minInt_u32 zig_as_u32(0)
338#define zig_maxInt_u32 UINT32_MAX
339#define zig_minInt_i32 INT32_MIN
340#define zig_maxInt_i32 INT32_MAX
341#define zig_minInt_u64 zig_as_u64(0)
342#define zig_maxInt_u64 UINT64_MAX
343#define zig_minInt_i64 INT64_MIN
344#define zig_maxInt_i64 INT64_MAX
345
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)
367
368#define zig_int_operator(Type, RhsType, operation, operator) \
369 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##RhsType rhs) { \
370 return lhs operator rhs; \
371 }
372#define zig_int_basic_operator(Type, operation, operator) \
373 zig_int_operator(Type, Type, operation, operator)
374#define zig_int_shift_operator(Type, operation, operator) \
375 zig_int_operator(Type, u8, operation, operator)
376#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, >>) \
386\
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); \
389 return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \
390 } \
391\
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); \
394 } \
395\
396 static inline zig_i##w zig_not_i##w(zig_i##w val, zig_u8 bits) { \
397 (void)bits; \
398 return ~val; \
399 } \
400\
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); \
403 } \
404\
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); \
408 } \
409\
410 zig_int_basic_operator(u##w, div_floor, /) \
411\
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)); \
414 } \
415\
416 zig_int_basic_operator(u##w, mod, %) \
417\
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)); \
421 } \
422\
423 static inline zig_u##w zig_shlw_u##w(zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \
424 return zig_wrap_u##w(zig_shl_u##w(lhs, rhs), bits); \
425 } \
426\
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); \
429 } \
430\
431 static inline zig_u##w zig_addw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
432 return zig_wrap_u##w(lhs + rhs, bits); \
433 } \
434\
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); \
437 } \
438\
439 static inline zig_u##w zig_subw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
440 return zig_wrap_u##w(lhs - rhs, bits); \
441 } \
442\
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); \
445 } \
446\
447 static inline zig_u##w zig_mulw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
448 return zig_wrap_u##w(lhs * rhs, bits); \
449 } \
450\
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); \
453 }
454zig_int_helpers(8)
455zig_int_helpers(16)
456zig_int_helpers(32)
457zig_int_helpers(64)
458
459static inline bool zig_addo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {
460#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
461 zig_u32 full_res;
462 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
463 *res = zig_wrap_u32(full_res, bits);
464 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
465#else
466 *res = zig_addw_u32(lhs, rhs, bits);
467 return *res < lhs;
468#endif
469}
470
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) {
479#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
480 zig_i32 full_res;
481 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
482#else
483 zig_c_int overflow_int;
484 zig_i32 full_res = __addosi4(lhs, rhs, &overflow_int);
485 bool overflow = overflow_int != 0;
486#endif
487 *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);
495}
496
497static inline bool zig_addo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
498#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
499 zig_u64 full_res;
500 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
501 *res = zig_wrap_u64(full_res, bits);
502 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
503#else
504 *res = zig_addw_u64(lhs, rhs, bits);
505 return *res < lhs;
506#endif
507}
508
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) {
517#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
518 zig_i64 full_res;
519 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
520#else
521 zig_c_int overflow_int;
522 zig_i64 full_res = __addodi4(lhs, rhs, &overflow_int);
523 bool overflow = overflow_int != 0;
524#endif
525 *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);
533}
534
535static inline bool zig_addo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {
536#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
537 zig_u8 full_res;
538 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
539 *res = zig_wrap_u8(full_res, bits);
540 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
541#else
542 zig_u32 full_res;
543 bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits);
544 *res = (zig_u8)full_res;
545 return overflow;
546#endif
547}
548
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) {
556#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
557 zig_i8 full_res;
558 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
559 *res = zig_wrap_i8(full_res, bits);
560 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
561#else
562 zig_i32 full_res;
563 bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits);
564 *res = (zig_i8)full_res;
565 return overflow;
566#endif
567}
568
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) {
576#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
577 zig_u16 full_res;
578 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
579 *res = zig_wrap_u16(full_res, bits);
580 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
581#else
582 zig_u32 full_res;
583 bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits);
584 *res = (zig_u16)full_res;
585 return overflow;
586#endif
587}
588
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) {
596#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
597 zig_i16 full_res;
598 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
599 *res = zig_wrap_i16(full_res, bits);
600 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
601#else
602 zig_i32 full_res;
603 bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits);
604 *res = (zig_i16)full_res;
605 return overflow;
606#endif
607}
608
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) {
616#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
617 zig_u32 full_res;
618 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
619 *res = zig_wrap_u32(full_res, bits);
620 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
621#else
622 *res = zig_subw_u32(lhs, rhs, bits);
623 return *res > lhs;
624#endif
625}
626
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) {
635#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
636 zig_i32 full_res;
637 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
638#else
639 zig_c_int overflow_int;
640 zig_i32 full_res = __subosi4(lhs, rhs, &overflow_int);
641 bool overflow = overflow_int != 0;
642#endif
643 *res = zig_wrap_i32(full_res, bits);
644 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);
645}
646
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) {
654#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
655 zig_u64 full_res;
656 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
657 *res = zig_wrap_u64(full_res, bits);
658 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
659#else
660 *res = zig_subw_u64(lhs, rhs, bits);
661 return *res > lhs;
662#endif
663}
664
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) {
673#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
674 zig_i64 full_res;
675 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
676#else
677 zig_c_int overflow_int;
678 zig_i64 full_res = __subodi4(lhs, rhs, &overflow_int);
679 bool overflow = overflow_int != 0;
680#endif
681 *res = zig_wrap_i64(full_res, bits);
682 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
683}
684
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) {
692#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
693 zig_u8 full_res;
694 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
695 *res = zig_wrap_u8(full_res, bits);
696 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
697#else
698 zig_u32 full_res;
699 bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits);
700 *res = (zig_u8)full_res;
701 return overflow;
702#endif
703}
704
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) {
712#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
713 zig_i8 full_res;
714 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
715 *res = zig_wrap_i8(full_res, bits);
716 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
717#else
718 zig_i32 full_res;
719 bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits);
720 *res = (zig_i8)full_res;
721 return overflow;
722#endif
723}
724
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) {
733#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
734 zig_u16 full_res;
735 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
736 *res = zig_wrap_u16(full_res, bits);
737 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
738#else
739 zig_u32 full_res;
740 bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits);
741 *res = (zig_u16)full_res;
742 return overflow;
743#endif
744}
745
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) {
754#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
755 zig_i16 full_res;
756 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
757 *res = zig_wrap_i16(full_res, bits);
758 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
759#else
760 zig_i32 full_res;
761 bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits);
762 *res = (zig_i16)full_res;
763 return overflow;
764#endif
765}
766
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) {
774#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
775 zig_u32 full_res;
776 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
777 *res = zig_wrap_u32(full_res, bits);
778 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
779#else
780 *res = zig_mulw_u32(lhs, rhs, bits);
781 return rhs != zig_as_u32(0) && lhs > zig_maxInt(u32, bits) / rhs;
782#endif
783}
784
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) {
793#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
794 zig_i32 full_res;
795 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
796#else
797 zig_c_int overflow_int;
798 zig_i32 full_res = __mulosi4(lhs, rhs, &overflow_int);
799 bool overflow = overflow_int != 0;
800#endif
801 *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);
809}
810
811static inline bool zig_mulo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
812#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
813 zig_u64 full_res;
814 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
815 *res = zig_wrap_u64(full_res, bits);
816 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
817#else
818 *res = zig_mulw_u64(lhs, rhs, bits);
819 return rhs != zig_as_u64(0) && lhs > zig_maxInt(u64, bits) / rhs;
820#endif
821}
822
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) {
831#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
832 zig_i64 full_res;
833 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
834#else
835 zig_c_int overflow_int;
836 zig_i64 full_res = __mulodi4(lhs, rhs, &overflow_int);
837 bool overflow = overflow_int != 0;
838#endif
839 *res = zig_wrap_i64(full_res, bits);
840 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
841}
842
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) {
850#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
851 zig_u8 full_res;
852 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
853 *res = zig_wrap_u8(full_res, bits);
854 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
855#else
856 zig_u32 full_res;
857 bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits);
858 *res = (zig_u8)full_res;
859 return overflow;
860#endif
861}
862
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) {
870#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
871 zig_i8 full_res;
872 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
873 *res = zig_wrap_i8(full_res, bits);
874 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
875#else
876 zig_i32 full_res;
877 bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits);
878 *res = (zig_i8)full_res;
879 return overflow;
880#endif
881}
882
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) {
890#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
891 zig_u16 full_res;
892 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
893 *res = zig_wrap_u16(full_res, bits);
894 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
895#else
896 zig_u32 full_res;
897 bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits);
898 *res = (zig_u16)full_res;
899 return overflow;
900#endif
901}
902
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) {
910#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
911 zig_i16 full_res;
912 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
913 *res = zig_wrap_i16(full_res, bits);
914 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
915#else
916 zig_i32 full_res;
917 bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits);
918 *res = (zig_i16)full_res;
919 return overflow;
920#endif
921}
922
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
929#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) { \
931 *res = zig_shlw_u##w(lhs, rhs, bits); \
932 return lhs > zig_maxInt(u##w, bits) >> rhs; \
933 } \
934\
935 static inline bool zig_shlo_i##w(zig_i##w *res, zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \
936 *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; \
939 } \
940\
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; \
945 } \
946\
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); \
951 } \
952\
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; \
956 } \
957\
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; \
960 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); \
962 } \
963\
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; \
967 } \
968\
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; \
971 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); \
973 } \
974\
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; \
978 } \
979\
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; \
982 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); \
984 }
985zig_int_builtins(8)
986zig_int_builtins(16)
987zig_int_builtins(32)
988zig_int_builtins(64)
989
990#define zig_builtin8(name, val) __builtin_##name(val)
991typedef zig_c_uint zig_Builtin8;
992
993#define zig_builtin16(name, val) __builtin_##name(val)
994typedef zig_c_uint zig_Builtin16;
995
996#if INT_MIN <= INT32_MIN
997#define zig_builtin32(name, val) __builtin_##name(val)
998typedef zig_c_uint zig_Builtin32;
999#elif LONG_MIN <= INT32_MIN
1000#define zig_builtin32(name, val) __builtin_##name##l(val)
1001typedef zig_c_ulong zig_Builtin32;
1002#endif
1003
1004#if INT_MIN <= INT64_MIN
1005#define zig_builtin64(name, val) __builtin_##name(val)
1006typedef zig_c_uint zig_Builtin64;
1007#elif LONG_MIN <= INT64_MIN
1008#define zig_builtin64(name, val) __builtin_##name##l(val)
1009typedef zig_c_ulong zig_Builtin64;
1010#elif LLONG_MIN <= INT64_MIN
1011#define zig_builtin64(name, val) __builtin_##name##ll(val)
1012typedef zig_c_ulonglong zig_Builtin64;
1013#endif
1014
1015static inline zig_u8 zig_byte_swap_u8(zig_u8 val, zig_u8 bits) {
1016 return zig_wrap_u8(val >> (8 - bits), bits);
1017}
1018
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);
1021}
1022
1023static inline zig_u16 zig_byte_swap_u16(zig_u16 val, zig_u8 bits) {
1024 zig_u16 full_res;
1025#if zig_has_builtin(bswap16) || defined(zig_gnuc)
1026 full_res = __builtin_bswap16(val);
1027#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;
1030#endif
1031 return zig_wrap_u16(full_res >> (16 - bits), bits);
1032}
1033
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);
1036}
1037
1038static inline zig_u32 zig_byte_swap_u32(zig_u32 val, zig_u8 bits) {
1039 zig_u32 full_res;
1040#if zig_has_builtin(bswap32) || defined(zig_gnuc)
1041 full_res = __builtin_bswap32(val);
1042#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;
1045#endif
1046 return zig_wrap_u32(full_res >> (32 - bits), bits);
1047}
1048
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);
1051}
1052
1053static inline zig_u64 zig_byte_swap_u64(zig_u64 val, zig_u8 bits) {
1054 zig_u64 full_res;
1055#if zig_has_builtin(bswap64) || defined(zig_gnuc)
1056 full_res = __builtin_bswap64(val);
1057#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;
1060#endif
1061 return zig_wrap_u64(full_res >> (64 - bits), bits);
1062}
1063
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);
1066}
1067
1068static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) {
1069 zig_u8 full_res;
1070#if zig_has_builtin(bitreverse8)
1071 full_res = __builtin_bitreverse8(val);
1072#else
1073 static zig_u8 const lut[0x10] = {
1074 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
1075 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
1076 };
1077 full_res = lut[val >> 0 & 0xF] << 4 | lut[val >> 4 & 0xF] << 0;
1078#endif
1079 return zig_wrap_u8(full_res >> (8 - bits), bits);
1080}
1081
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);
1084}
1085
1086static inline zig_u16 zig_bit_reverse_u16(zig_u16 val, zig_u8 bits) {
1087 zig_u16 full_res;
1088#if zig_has_builtin(bitreverse16)
1089 full_res = __builtin_bitreverse16(val);
1090#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;
1093#endif
1094 return zig_wrap_u16(full_res >> (16 - bits), bits);
1095}
1096
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);
1099}
1100
1101static inline zig_u32 zig_bit_reverse_u32(zig_u32 val, zig_u8 bits) {
1102 zig_u32 full_res;
1103#if zig_has_builtin(bitreverse32)
1104 full_res = __builtin_bitreverse32(val);
1105#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;
1108#endif
1109 return zig_wrap_u32(full_res >> (32 - bits), bits);
1110}
1111
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);
1114}
1115
1116static inline zig_u64 zig_bit_reverse_u64(zig_u64 val, zig_u8 bits) {
1117 zig_u64 full_res;
1118#if zig_has_builtin(bitreverse64)
1119 full_res = __builtin_bitreverse64(val);
1120#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;
1123#endif
1124 return zig_wrap_u64(full_res >> (64 - bits), bits);
1125}
1126
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);
1129}
1130
1131#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); \
1134 }
1135#if zig_has_builtin(popcount) || defined(zig_gnuc)
1136#define zig_builtin_popcount(w) \
1137 static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \
1138 (void)bits; \
1139 return zig_builtin##w(popcount, val); \
1140 } \
1141\
1142 zig_builtin_popcount_common(w)
1143#else
1144#define zig_builtin_popcount(w) \
1145 static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \
1146 (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); \
1151 } \
1152\
1153 zig_builtin_popcount_common(w)
1154#endif
1155zig_builtin_popcount(8)
1156zig_builtin_popcount(16)
1157zig_builtin_popcount(32)
1158zig_builtin_popcount(64)
1159
1160#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); \
1163 }
1164#if zig_has_builtin(ctz) || defined(zig_gnuc)
1165#define zig_builtin_ctz(w) \
1166 static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \
1167 if (val == 0) return bits; \
1168 return zig_builtin##w(ctz, val); \
1169 } \
1170\
1171 zig_builtin_ctz_common(w)
1172#else
1173#define zig_builtin_ctz(w) \
1174 static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \
1175 return zig_popcount_u##w(zig_not_u##w(val, bits) & zig_subw_u##w(val, 1, bits), bits); \
1176 } \
1177\
1178 zig_builtin_ctz_common(w)
1179#endif
1180zig_builtin_ctz(8)
1181zig_builtin_ctz(16)
1182zig_builtin_ctz(32)
1183zig_builtin_ctz(64)
1184
1185#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); \
1188 }
1189#if zig_has_builtin(clz) || defined(zig_gnuc)
1190#define zig_builtin_clz(w) \
1191 static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \
1192 if (val == 0) return bits; \
1193 return zig_builtin##w(clz, val) - (zig_bitSizeOf(zig_Builtin##w) - bits); \
1194 } \
1195\
1196 zig_builtin_clz_common(w)
1197#else
1198#define zig_builtin_clz(w) \
1199 static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \
1200 return zig_ctz_u##w(zig_bit_reverse_u##w(val, bits), bits); \
1201 } \
1202\
1203 zig_builtin_clz_common(w)
1204#endif
1205zig_builtin_clz(8)
1206zig_builtin_clz(16)
1207zig_builtin_clz(32)
1208zig_builtin_clz(64)
1209
1210/* ======================== 128-bit Integer Routines ======================== */
1211
1212#if !defined(zig_has_int128)
1213# if defined(__SIZEOF_INT128__)
1214# define zig_has_int128 1
1215# else
1216# define zig_has_int128 0
1217# endif
1218#endif
1219
1220#if zig_has_int128
1221
1222typedef unsigned __int128 zig_u128;
1223typedef signed __int128 zig_i128;
1224
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))
1233#define zig_bitcast_u128(val) ((zig_u128)(val))
1234#define zig_bitcast_i128(val) ((zig_i128)(val))
1235#define zig_cmp_int128(Type) \
1236 static inline zig_i32 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
1237 return (lhs > rhs) - (lhs < rhs); \
1238 }
1239#define zig_bit_int128(Type, operation, operator) \
1240 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1241 return lhs operator rhs; \
1242 }
1243
1244#else /* zig_has_int128 */
1245
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;
1249#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;
1252#endif
1253
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) })
1256
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)
1263#endif
1264#define zig_hi_u128(val) ((val).hi)
1265#define zig_lo_u128(val) ((val).lo)
1266#define zig_hi_i128(val) ((val).hi)
1267#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)
1270#define zig_cmp_int128(Type) \
1271 static inline zig_i32 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
1272 return (lhs.hi == rhs.hi) \
1273 ? (lhs.lo > rhs.lo) - (lhs.lo < rhs.lo) \
1274 : (lhs.hi > rhs.hi) - (lhs.hi < rhs.hi); \
1275 }
1276#define zig_bit_int128(Type, operation, operator) \
1277 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1278 return (zig_##Type){ .hi = lhs.hi operator rhs.hi, .lo = lhs.lo operator rhs.lo }; \
1279 }
1280
1281#endif /* zig_has_int128 */
1282
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)
1287
1288zig_cmp_int128(u128)
1289zig_cmp_int128(i128)
1290
1291zig_bit_int128(u128, and, &)
1292zig_bit_int128(i128, and, &)
1293
1294zig_bit_int128(u128, or, |)
1295zig_bit_int128(i128, or, |)
1296
1297zig_bit_int128(u128, xor, ^)
1298zig_bit_int128(i128, xor, ^)
1299
1300static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs);
1301
1302#if zig_has_int128
1303
1304static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) {
1305 return val ^ zig_maxInt(u128, bits);
1306}
1307
1308static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) {
1309 (void)bits;
1310 return ~val;
1311}
1312
1313static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) {
1314 return lhs >> rhs;
1315}
1316
1317static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) {
1318 return lhs << rhs;
1319}
1320
1321static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) {
1322 return lhs << rhs;
1323}
1324
1325static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) {
1326 return lhs + rhs;
1327}
1328
1329static inline zig_i128 zig_add_i128(zig_i128 lhs, zig_i128 rhs) {
1330 return lhs + rhs;
1331}
1332
1333static inline zig_u128 zig_sub_u128(zig_u128 lhs, zig_u128 rhs) {
1334 return lhs - rhs;
1335}
1336
1337static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) {
1338 return lhs - rhs;
1339}
1340
1341static inline zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) {
1342 return lhs * rhs;
1343}
1344
1345static inline zig_i128 zig_mul_i128(zig_i128 lhs, zig_i128 rhs) {
1346 return lhs * rhs;
1347}
1348
1349static inline zig_u128 zig_div_trunc_u128(zig_u128 lhs, zig_u128 rhs) {
1350 return lhs / rhs;
1351}
1352
1353static inline zig_i128 zig_div_trunc_i128(zig_i128 lhs, zig_i128 rhs) {
1354 return lhs / rhs;
1355}
1356
1357static inline zig_u128 zig_rem_u128(zig_u128 lhs, zig_u128 rhs) {
1358 return lhs % rhs;
1359}
1360
1361static inline zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {
1362 return lhs % rhs;
1363}
1364
1365static 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));
1367}
1368
1369static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
1370 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));
1372}
1373
1374#else /* zig_has_int128 */
1375
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)) };
1378}
1379
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)) };
1382}
1383
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 };
1388}
1389
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 };
1394}
1395
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 };
1400}
1401
1402static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) {
1403 zig_u128 res;
1404 res.hi = lhs.hi + rhs.hi + zig_addo_u64(&res.lo, lhs.lo, rhs.lo, 64);
1405 return res;
1406}
1407
1408static inline zig_i128 zig_add_i128(zig_i128 lhs, zig_i128 rhs) {
1409 zig_i128 res;
1410 res.hi = lhs.hi + rhs.hi + zig_addo_u64(&res.lo, lhs.lo, rhs.lo, 64);
1411 return res;
1412}
1413
1414static inline zig_u128 zig_sub_u128(zig_u128 lhs, zig_u128 rhs) {
1415 zig_u128 res;
1416 res.hi = lhs.hi - rhs.hi - zig_subo_u64(&res.lo, lhs.lo, rhs.lo, 64);
1417 return res;
1418}
1419
1420static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) {
1421 zig_i128 res;
1422 res.hi = lhs.hi - rhs.hi - zig_subo_u64(&res.lo, lhs.lo, rhs.lo, 64);
1423 return res;
1424}
1425
1426zig_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
1431static zig_i128 zig_mul_i128(zig_i128 lhs, zig_i128 rhs) {
1432 return __multi3(lhs, rhs);
1433}
1434
1435zig_extern zig_u128 __udivti3(zig_u128 lhs, zig_u128 rhs);
1436static zig_u128 zig_div_trunc_u128(zig_u128 lhs, zig_u128 rhs) {
1437 return __udivti3(lhs, rhs);
1438};
1439
1440zig_extern zig_i128 __divti3(zig_i128 lhs, zig_i128 rhs);
1441static zig_i128 zig_div_trunc_i128(zig_i128 lhs, zig_i128 rhs) {
1442 return __divti3(lhs, rhs);
1443};
1444
1445zig_extern zig_u128 __umodti3(zig_u128 lhs, zig_u128 rhs);
1446static zig_u128 zig_rem_u128(zig_u128 lhs, zig_u128 rhs) {
1447 return __umodti3(lhs, rhs);
1448}
1449
1450zig_extern zig_i128 __modti3(zig_i128 lhs, zig_i128 rhs);
1451static zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {
1452 return __modti3(lhs, rhs);
1453}
1454
1455static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
1456 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)));
1458}
1459
1460static 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)));
1462}
1463
1464#endif /* zig_has_int128 */
1465
1466#define zig_div_floor_u128 zig_div_trunc_u128
1467#define zig_mod_u128 zig_rem_u128
1468
1469static inline zig_u128 zig_nand_u128(zig_u128 lhs, zig_u128 rhs) {
1470 return zig_not_u128(zig_and_u128(lhs, rhs), 128);
1471}
1472
1473static 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;
1475}
1476
1477static 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;
1479}
1480
1481static 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;
1483}
1484
1485static 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;
1487}
1488
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);
1492}
1493
1494static inline zig_u128 zig_wrap_u128(zig_u128 val, zig_u8 bits) {
1495 return zig_and_u128(val, zig_maxInt(u128, bits));
1496}
1497
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) {
1503 return zig_wrap_u128(zig_shl_u128(lhs, rhs), bits);
1504}
1505
1506static inline zig_i128 zig_shlw_i128(zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {
1507 return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), rhs)), bits);
1508}
1509
1510static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1511 return zig_wrap_u128(zig_add_u128(lhs, rhs), bits);
1512}
1513
1514static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1515 return zig_wrap_i128(zig_bitcast_i128(zig_add_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1516}
1517
1518static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1519 return zig_wrap_u128(zig_sub_u128(lhs, rhs), bits);
1520}
1521
1522static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1523 return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1524}
1525
1526static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1527 return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits);
1528}
1529
1530static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1531 return zig_wrap_i128(zig_bitcast_i128(zig_mul_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1532}
1533
1534#if zig_has_int128
1535
1536static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1537#if zig_has_builtin(add_overflow)
1538 zig_u128 full_res;
1539 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
1540 *res = zig_wrap_u128(full_res, bits);
1541 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1542#else
1543 *res = zig_addw_u128(lhs, rhs, bits);
1544 return *res < lhs;
1545#endif
1546}
1547
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) {
1550#if zig_has_builtin(add_overflow)
1551 zig_i128 full_res;
1552 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
1553#else
1554 zig_c_int overflow_int;
1555 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
1556 bool overflow = overflow_int != 0;
1557#endif
1558 *res = zig_wrap_i128(full_res, bits);
1559 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1560}
1561
1562static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1563#if zig_has_builtin(sub_overflow)
1564 zig_u128 full_res;
1565 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
1566 *res = zig_wrap_u128(full_res, bits);
1567 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1568#else
1569 *res = zig_subw_u128(lhs, rhs, bits);
1570 return *res > lhs;
1571#endif
1572}
1573
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) {
1576#if zig_has_builtin(sub_overflow)
1577 zig_i128 full_res;
1578 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
1579#else
1580 zig_c_int overflow_int;
1581 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
1582 bool overflow = overflow_int != 0;
1583#endif
1584 *res = zig_wrap_i128(full_res, bits);
1585 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1586}
1587
1588static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1589#if zig_has_builtin(mul_overflow)
1590 zig_u128 full_res;
1591 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
1592 *res = zig_wrap_u128(full_res, bits);
1593 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1594#else
1595 *res = zig_mulw_u128(lhs, rhs, bits);
1596 return rhs != zig_as_u128(0, 0) && lhs > zig_maxInt(u128, bits) / rhs;
1597#endif
1598}
1599
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) {
1602#if zig_has_builtin(mul_overflow)
1603 zig_i128 full_res;
1604 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
1605#else
1606 zig_c_int overflow_int;
1607 zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int);
1608 bool overflow = overflow_int != 0;
1609#endif
1610 *res = zig_wrap_i128(full_res, bits);
1611 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1612}
1613
1614#else /* zig_has_int128 */
1615
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);
1620}
1621
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);
1626}
1627
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);
1635}
1636
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);
1643}
1644
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) {
1663 *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);
1666}
1667
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;
1671 zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int);
1672 *res = zig_wrap_i128(full_res, bits);
1673 return zig_overflow_i128(overflow_int, full_res, bits);
1674}
1675
1676#endif /* zig_has_int128 */
1677
1678static inline bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {
1679 *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);
1681}
1682
1683static inline bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {
1684 *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);
1688}
1689
1690static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1691 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
1700}
1701
1702static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1703 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);
1706}
1707
1708static inline zig_u128 zig_adds_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1709 zig_u128 res;
1710 return zig_addo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res;
1711}
1712
1713static inline zig_i128 zig_adds_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1714 zig_i128 res;
1715 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);
1717}
1718
1719static inline zig_u128 zig_subs_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1720 zig_u128 res;
1721 return zig_subo_u128(&res, lhs, rhs, bits) ? zig_minInt(u128, bits) : res;
1722}
1723
1724static inline zig_i128 zig_subs_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1725 zig_i128 res;
1726 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);
1728}
1729
1730static inline zig_u128 zig_muls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1731 zig_u128 res;
1732 return zig_mulo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res;
1733}
1734
1735static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1736 zig_i128 res;
1737 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);
1739}
1740
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));
1745}
1746
1747static inline zig_u8 zig_clz_i128(zig_i128 val, zig_u8 bits) {
1748 return zig_clz_u128(zig_bitcast_u128(val), bits);
1749}
1750
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);
1754}
1755
1756static inline zig_u8 zig_ctz_i128(zig_i128 val, zig_u8 bits) {
1757 return zig_ctz_u128(zig_bitcast_u128(val), bits);
1758}
1759
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));
1763}
1764
1765static inline zig_u8 zig_popcount_i128(zig_i128 val, zig_u8 bits) {
1766 return zig_popcount_u128(zig_bitcast_u128(val), bits);
1767}
1768
1769static inline zig_u128 zig_byte_swap_u128(zig_u128 val, zig_u8 bits) {
1770 zig_u128 full_res;
1771#if zig_has_builtin(bswap128)
1772 full_res = __builtin_bswap128(val);
1773#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)));
1776#endif
1777 return zig_shr_u128(full_res, zig_as_u8(128) - bits);
1778}
1779
1780static inline zig_i128 zig_byte_swap_i128(zig_i128 val, zig_u8 bits) {
1781 return zig_bitcast_i128(zig_byte_swap_u128(zig_bitcast_u128(val), bits));
1782}
1783
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);
1788}
1789
1790static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) {
1791 return zig_bitcast_i128(zig_bit_reverse_u128(zig_bitcast_u128(val), bits));
1792}
1793
1794/* ========================= Floating Point Support ========================= */
1795
1796#if _MSC_VER
1797#define zig_msvc_flt_inf ((double)(1e+300 * 1e+300))
1798#define zig_msvc_flt_inff ((float)(1e+300 * 1e+300))
1799#define zig_msvc_flt_infl ((long double)(1e+300 * 1e+300))
1800#define zig_msvc_flt_nan ((double)(zig_msvc_flt_inf * 0.f))
1801#define zig_msvc_flt_nanf ((float)(zig_msvc_flt_inf * 0.f))
1802#define zig_msvc_flt_nanl ((long double)(zig_msvc_flt_inf * 0.f))
1803#define __builtin_nan(str) nan(str)
1804#define __builtin_nanf(str) nanf(str)
1805#define __builtin_nanl(str) nanl(str)
1806#define __builtin_inf() zig_msvc_flt_inf
1807#define __builtin_inff() zig_msvc_flt_inff
1808#define __builtin_infl() zig_msvc_flt_infl
1809#endif
1810
1811#if (zig_has_builtin(nan) && zig_has_builtin(nans) && zig_has_builtin(inf)) || defined(zig_gnuc)
1812#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)
1819#else
1820#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)
1827#endif
1828
1829#define zig_has_f16 1
1830#define zig_bitSizeOf_f16 16
1831#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)
1833#if FLT_MANT_DIG == 11
1834typedef float zig_f16;
1835#define zig_as_f16(fp, repr) fp##f
1836#elif DBL_MANT_DIG == 11
1837typedef double zig_f16;
1838#define zig_as_f16(fp, repr) fp
1839#elif LDBL_MANT_DIG == 11
1840#define zig_bitSizeOf_c_longdouble 16
1841typedef long double zig_f16;
1842#define zig_as_f16(fp, repr) fp##l
1843#elif FLT16_MANT_DIG == 11 && (zig_has_builtin(inff16) || defined(zig_gnuc))
1844typedef _Float16 zig_f16;
1845#define zig_as_f16(fp, repr) fp##f16
1846#elif defined(__SIZEOF_FP16__)
1847typedef __fp16 zig_f16;
1848#define zig_as_f16(fp, repr) fp##f16
1849#else
1850#undef zig_has_f16
1851#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
1859#endif
1860
1861#define zig_has_f32 1
1862#define zig_bitSizeOf_f32 32
1863#define zig_libc_name_f32(name) name##f
1864#if _MSC_VER
1865#define zig_as_special_constant_f32(sign, name, arg, repr) sign zig_as_f32(zig_msvc_flt_##name, )
1866#else
1867#define zig_as_special_constant_f32(sign, name, arg, repr) zig_as_special_f32(sign, name, arg, repr)
1868#endif
1869#if FLT_MANT_DIG == 24
1870typedef float zig_f32;
1871#define zig_as_f32(fp, repr) fp##f
1872#elif DBL_MANT_DIG == 24
1873typedef double zig_f32;
1874#define zig_as_f32(fp, repr) fp
1875#elif LDBL_MANT_DIG == 24
1876#define zig_bitSizeOf_c_longdouble 32
1877typedef long double zig_f32;
1878#define zig_as_f32(fp, repr) fp##l
1879#elif FLT32_MANT_DIG == 24
1880typedef _Float32 zig_f32;
1881#define zig_as_f32(fp, repr) fp##f32
1882#else
1883#undef zig_has_f32
1884#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
1892#endif
1893
1894#define zig_has_f64 1
1895#define zig_bitSizeOf_f64 64
1896#define zig_libc_name_f64(name) name
1897#if _MSC_VER
1898#ifdef ZIG_TARGET_ABI_MSVC
1899#define zig_bitSizeOf_c_longdouble 64
1900#endif
1901#define zig_as_special_constant_f64(sign, name, arg, repr) sign zig_as_f64(zig_msvc_flt_##name, )
1902#else /* _MSC_VER */
1903#define zig_as_special_constant_f64(sign, name, arg, repr) zig_as_special_f64(sign, name, arg, repr)
1904#endif /* _MSC_VER */
1905#if FLT_MANT_DIG == 53
1906typedef float zig_f64;
1907#define zig_as_f64(fp, repr) fp##f
1908#elif DBL_MANT_DIG == 53
1909typedef double zig_f64;
1910#define zig_as_f64(fp, repr) fp
1911#elif LDBL_MANT_DIG == 53
1912#define zig_bitSizeOf_c_longdouble 64
1913typedef long double zig_f64;
1914#define zig_as_f64(fp, repr) fp##l
1915#elif FLT64_MANT_DIG == 53
1916typedef _Float64 zig_f64;
1917#define zig_as_f64(fp, repr) fp##f64
1918#elif FLT32X_MANT_DIG == 53
1919typedef _Float32x zig_f64;
1920#define zig_as_f64(fp, repr) fp##f32x
1921#else
1922#undef zig_has_f64
1923#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
1931#endif
1932
1933#define zig_has_f80 1
1934#define zig_bitSizeOf_f80 80
1935#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)
1937#if FLT_MANT_DIG == 64
1938typedef float zig_f80;
1939#define zig_as_f80(fp, repr) fp##f
1940#elif DBL_MANT_DIG == 64
1941typedef double zig_f80;
1942#define zig_as_f80(fp, repr) fp
1943#elif LDBL_MANT_DIG == 64
1944#define zig_bitSizeOf_c_longdouble 80
1945typedef long double zig_f80;
1946#define zig_as_f80(fp, repr) fp##l
1947#elif FLT80_MANT_DIG == 64
1948typedef _Float80 zig_f80;
1949#define zig_as_f80(fp, repr) fp##f80
1950#elif FLT64X_MANT_DIG == 64
1951typedef _Float64x zig_f80;
1952#define zig_as_f80(fp, repr) fp##f64x
1953#elif defined(__SIZEOF_FLOAT80__)
1954typedef __float80 zig_f80;
1955#define zig_as_f80(fp, repr) fp##l
1956#else
1957#undef zig_has_f80
1958#define zig_has_f80 0
1959#define zig_repr_f80 i128
1960typedef 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
1966#endif
1967
1968#define zig_has_f128 1
1969#define zig_bitSizeOf_f128 128
1970#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)
1972#if FLT_MANT_DIG == 113
1973typedef float zig_f128;
1974#define zig_as_f128(fp, repr) fp##f
1975#elif DBL_MANT_DIG == 113
1976typedef double zig_f128;
1977#define zig_as_f128(fp, repr) fp
1978#elif LDBL_MANT_DIG == 113
1979#define zig_bitSizeOf_c_longdouble 128
1980typedef long double zig_f128;
1981#define zig_as_f128(fp, repr) fp##l
1982#elif FLT128_MANT_DIG == 113
1983typedef _Float128 zig_f128;
1984#define zig_as_f128(fp, repr) fp##f128
1985#elif FLT64X_MANT_DIG == 113
1986typedef _Float64x zig_f128;
1987#define zig_as_f128(fp, repr) fp##f64x
1988#elif defined(__SIZEOF_FLOAT128__)
1989typedef __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)
1993#else
1994#undef zig_has_f128
1995#define zig_has_f128 0
1996#define zig_repr_f128 i128
1997typedef 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
2011#endif
2012
2013#define zig_as_special_constant_c_longdouble(sign, name, arg, repr) zig_as_special_c_longdouble(sign, name, arg, repr)
2014#ifdef zig_bitSizeOf_c_longdouble
2015
2016#ifdef ZIG_TARGET_ABI_MSVC
2017typedef double zig_c_longdouble;
2018#undef zig_bitSizeOf_c_longdouble
2019#define zig_bitSizeOf_c_longdouble 64
2020#define zig_as_c_longdouble(fp, repr) fp
2021#else
2022typedef long double zig_c_longdouble;
2023#define zig_as_c_longdouble(fp, repr) fp##l
2024#endif
2025
2026#else /* zig_bitSizeOf_c_longdouble */
2027
2028#undef zig_has_c_longdouble
2029#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
2039
2040#endif /* zig_bitSizeOf_c_longdouble */
2041
2042#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); \
2046 }
2047
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
2059#endif
2060
2061#define zig_cast_f16 (zig_f16)
2062#define zig_cast_f32 (zig_f32)
2063#define zig_cast_f64 (zig_f64)
2064
2065#if _MSC_VER && !zig_has_f128
2066#define zig_cast_f80
2067#define zig_cast_c_longdouble
2068#define zig_cast_f128
2069#else
2070#define zig_cast_f80 (zig_f80)
2071#define zig_cast_c_longdouble (zig_c_longdouble)
2072#define zig_cast_f128 (zig_f128)
2073#endif
2074
2075#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)
2098
2099#define zig_float_negate_builtin_0(Type) \
2100 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)); \
2102 }
2103#define zig_float_negate_builtin_1(Type) \
2104 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
2105 return -arg; \
2106 }
2107
2108#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); \
2113 }
2114#define zig_float_less_builtin_1(Type, operation) \
2115 static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
2116 return (!(lhs <= rhs) - (lhs < rhs)); \
2117 }
2118
2119#define zig_float_greater_builtin_0(Type, operation) \
2120 zig_float_less_builtin_0(Type, operation)
2121#define zig_float_greater_builtin_1(Type, operation) \
2122 static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
2123 return ((lhs > rhs) - !(lhs >= rhs)); \
2124 }
2125
2126#define zig_float_binary_builtin_0(Type, operation, operator) \
2127 zig_extern zig_##Type zig_expand_concat(zig_expand_concat(__##operation, \
2128 zig_compiler_rt_abbrev_##Type), 3)(zig_##Type, zig_##Type); \
2129 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); \
2131 }
2132#define zig_float_binary_builtin_1(Type, operation, operator) \
2133 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
2134 return lhs operator rhs; \
2135 }
2136
2137#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, ) \
2150 zig_expand_concat(zig_float_negate_builtin_, zig_has_##Type)(Type) \
2151 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, cmp) \
2152 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, ne) \
2153 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, eq) \
2154 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, lt) \
2155 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, le) \
2156 zig_expand_concat(zig_float_greater_builtin_, zig_has_##Type)(Type, gt) \
2157 zig_expand_concat(zig_float_greater_builtin_, zig_has_##Type)(Type, ge) \
2158 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, add, +) \
2159 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, sub, -) \
2160 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, mul, *) \
2161 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, div, /) \
2162 zig_extern zig_##Type zig_libc_name_##Type(sqrt)(zig_##Type); \
2163 zig_extern zig_##Type zig_libc_name_##Type(sin)(zig_##Type); \
2164 zig_extern zig_##Type zig_libc_name_##Type(cos)(zig_##Type); \
2165 zig_extern zig_##Type zig_libc_name_##Type(tan)(zig_##Type); \
2166 zig_extern zig_##Type zig_libc_name_##Type(exp)(zig_##Type); \
2167 zig_extern zig_##Type zig_libc_name_##Type(exp2)(zig_##Type); \
2168 zig_extern zig_##Type zig_libc_name_##Type(log)(zig_##Type); \
2169 zig_extern zig_##Type zig_libc_name_##Type(log2)(zig_##Type); \
2170 zig_extern zig_##Type zig_libc_name_##Type(log10)(zig_##Type); \
2171 zig_extern zig_##Type zig_libc_name_##Type(fabs)(zig_##Type); \
2172 zig_extern zig_##Type zig_libc_name_##Type(floor)(zig_##Type); \
2173 zig_extern zig_##Type zig_libc_name_##Type(ceil)(zig_##Type); \
2174 zig_extern zig_##Type zig_libc_name_##Type(round)(zig_##Type); \
2175 zig_extern zig_##Type zig_libc_name_##Type(trunc)(zig_##Type); \
2176 zig_extern zig_##Type zig_libc_name_##Type(fmod)(zig_##Type, zig_##Type); \
2177 zig_extern zig_##Type zig_libc_name_##Type(fmin)(zig_##Type, zig_##Type); \
2178 zig_extern zig_##Type zig_libc_name_##Type(fmax)(zig_##Type, zig_##Type); \
2179 zig_extern zig_##Type zig_libc_name_##Type(fma)(zig_##Type, zig_##Type, zig_##Type); \
2180\
2181 static inline zig_##Type zig_div_trunc_##Type(zig_##Type lhs, zig_##Type rhs) { \
2182 return zig_libc_name_##Type(trunc)(zig_div_##Type(lhs, rhs)); \
2183 } \
2184\
2185 static inline zig_##Type zig_div_floor_##Type(zig_##Type lhs, zig_##Type rhs) { \
2186 return zig_libc_name_##Type(floor)(zig_div_##Type(lhs, rhs)); \
2187 } \
2188\
2189 static inline zig_##Type zig_mod_##Type(zig_##Type lhs, zig_##Type rhs) { \
2190 return zig_sub_##Type(lhs, zig_mul_##Type(zig_div_floor_##Type(lhs, rhs), rhs)); \
2191 }
2192zig_float_builtins(f16)
2193zig_float_builtins(f32)
2194zig_float_builtins(f64)
2195zig_float_builtins(f80)
2196zig_float_builtins(f128)
2197zig_float_builtins(c_longdouble)
2198
2199#if _MSC_VER && (_M_IX86 || _M_X64)
2200
2201// TODO: zig_msvc_atomic_load should load 32 bit without interlocked on x86, and load 64 bit without interlocked on x64
2202
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); \
2207 bool exchanged = initial == comparand; \
2208 if (!exchanged) { \
2209 *expected = initial; \
2210 } \
2211 return exchanged; \
2212 } \
2213 static inline zig_##Type zig_msvc_atomicrmw_xchg_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2214 return _InterlockedExchange##suffix(obj, value); \
2215 } \
2216 static inline zig_##Type zig_msvc_atomicrmw_add_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2217 return _InterlockedExchangeAdd##suffix(obj, value); \
2218 } \
2219 static inline zig_##Type zig_msvc_atomicrmw_sub_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2220 bool success = false; \
2221 zig_##Type new; \
2222 zig_##Type prev; \
2223 while (!success) { \
2224 prev = *obj; \
2225 new = prev - value; \
2226 success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \
2227 } \
2228 return prev; \
2229 } \
2230 static inline zig_##Type zig_msvc_atomicrmw_or_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2231 return _InterlockedOr##suffix(obj, value); \
2232 } \
2233 static inline zig_##Type zig_msvc_atomicrmw_xor_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2234 return _InterlockedXor##suffix(obj, value); \
2235 } \
2236 static inline zig_##Type zig_msvc_atomicrmw_and_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2237 return _InterlockedAnd##suffix(obj, value); \
2238 } \
2239 static inline zig_##Type zig_msvc_atomicrmw_nand_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2240 bool success = false; \
2241 zig_##Type new; \
2242 zig_##Type prev; \
2243 while (!success) { \
2244 prev = *obj; \
2245 new = ~(prev & value); \
2246 success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \
2247 } \
2248 return prev; \
2249 } \
2250 static inline zig_##Type zig_msvc_atomicrmw_min_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2251 bool success = false; \
2252 zig_##Type new; \
2253 zig_##Type prev; \
2254 while (!success) { \
2255 prev = *obj; \
2256 new = value < prev ? value : prev; \
2257 success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \
2258 } \
2259 return prev; \
2260 } \
2261 static inline zig_##Type zig_msvc_atomicrmw_max_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2262 bool success = false; \
2263 zig_##Type new; \
2264 zig_##Type prev; \
2265 while (!success) { \
2266 prev = *obj; \
2267 new = value > prev ? value : prev; \
2268 success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \
2269 } \
2270 return prev; \
2271 } \
2272 static inline void zig_msvc_atomic_store_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2273 _InterlockedExchange##suffix(obj, value); \
2274 } \
2275 static inline zig_##Type zig_msvc_atomic_load_##Type(zig_##Type volatile* obj) { \
2276 return _InterlockedOr##suffix(obj, 0); \
2277 }
2278
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, )
2285
2286#if _M_X64
2287zig_msvc_atomics(u64, 64)
2288zig_msvc_atomics(i64, 64)
2289#endif
2290
2291#define zig_msvc_flt_atomics(Type, ReprType, suffix) \
2292 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; \
2300 } \
2301 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); \
2304 } \
2305 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; \
2315 } \
2316 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; \
2326 }
2327
2328zig_msvc_flt_atomics(f32, u32, )
2329#if _M_X64
2330zig_msvc_flt_atomics(f64, u64, 64)
2331#endif
2332
2333#if _M_IX86
2334static inline void zig_msvc_atomic_barrier() {
2335 zig_i32 barrier;
2336 __asm {
2337 xchg barrier, eax
2338 }
2339}
2340
2341static inline void* zig_msvc_atomicrmw_xchg_p32(void** obj, zig_u32* arg) {
2342 return _InterlockedExchangePointer(obj, arg);
2343}
2344
2345static inline void zig_msvc_atomic_store_p32(void** obj, zig_u32* arg) {
2346 _InterlockedExchangePointer(obj, arg);
2347}
2348
2349static inline void* zig_msvc_atomic_load_p32(void** obj) {
2350 return (void*)_InterlockedOr((void*)obj, 0);
2351}
2352
2353static inline bool zig_msvc_cmpxchg_p32(void** obj, void** expected, void* desired) {
2354 void* comparand = *expected;
2355 void* initial = _InterlockedCompareExchangePointer(obj, desired, comparand);
2356 bool exchanged = initial == comparand;
2357 if (!exchanged) {
2358 *expected = initial;
2359 }
2360 return exchanged;
2361}
2362#else /* _M_IX86 */
2363static inline void* zig_msvc_atomicrmw_xchg_p64(void** obj, zig_u64* arg) {
2364 return _InterlockedExchangePointer(obj, arg);
2365}
2366
2367static inline void zig_msvc_atomic_store_p64(void** obj, zig_u64* arg) {
2368 _InterlockedExchangePointer(obj, arg);
2369}
2370
2371static inline void* zig_msvc_atomic_load_p64(void** obj) {
2372 return (void*)_InterlockedOr64((void*)obj, 0);
2373}
2374
2375static inline bool zig_msvc_cmpxchg_p64(void** obj, void** expected, void* desired) {
2376 void* comparand = *expected;
2377 void* initial = _InterlockedCompareExchangePointer(obj, desired, comparand);
2378 bool exchanged = initial == comparand;
2379 if (!exchanged) {
2380 *expected = initial;
2381 }
2382 return exchanged;
2383}
2384
2385static 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);
2387}
2388
2389static 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);
2391}
2392
2393#define zig_msvc_atomics_128xchg(Type) \
2394 static inline zig_##Type zig_msvc_atomicrmw_xchg_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2395 bool success = false; \
2396 zig_##Type prev; \
2397 while (!success) { \
2398 prev = *obj; \
2399 success = zig_msvc_cmpxchg_##Type(obj, &prev, value); \
2400 } \
2401 return prev; \
2402 }
2403
2404zig_msvc_atomics_128xchg(u128)
2405zig_msvc_atomics_128xchg(i128)
2406
2407#define zig_msvc_atomics_128op(Type, operation) \
2408 static inline zig_##Type zig_msvc_atomicrmw_##operation##_##Type(zig_##Type volatile* obj, zig_##Type value) { \
2409 bool success = false; \
2410 zig_##Type new; \
2411 zig_##Type prev; \
2412 while (!success) { \
2413 prev = *obj; \
2414 new = zig_##operation##_##Type(prev, value); \
2415 success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \
2416 } \
2417 return prev; \
2418 }
2419
2420zig_msvc_atomics_128op(u128, add)
2421zig_msvc_atomics_128op(u128, sub)
2422zig_msvc_atomics_128op(u128, or)
2423zig_msvc_atomics_128op(u128, xor)
2424zig_msvc_atomics_128op(u128, and)
2425zig_msvc_atomics_128op(u128, nand)
2426zig_msvc_atomics_128op(u128, min)
2427zig_msvc_atomics_128op(u128, max)
2428#endif /* _M_IX86 */
2429
2430#endif /* _MSC_VER && (_M_IX86 || _M_X64) */
2431
2432/* ========================= Special Case Intrinsics ========================= */
2433
2434#if (_MSC_VER && _M_X64) || defined(__x86_64__)
2435
2436static inline void* zig_x86_64_windows_teb(void) {
2437#if _MSC_VER
2438 return (void*)__readgsqword(0x30);
2439#else
2440 void* teb;
2441 __asm volatile(" movq %%gs:0x30, %[ptr]": [ptr]"=r"(teb)::);
2442 return teb;
2443#endif
2444}
2445
2446#elif (_MSC_VER && _M_IX86) || defined(__i386__) || defined(__X86__)
2447
2448static inline void* zig_x86_windows_teb(void) {
2449#if _MSC_VER
2450 return (void*)__readfsdword(0x18);
2451#else
2452 void* teb;
2453 __asm volatile(" movl %%fs:0x18, %[ptr]": [ptr]"=r"(teb)::);
2454 return teb;
2455#endif
2456}
2457
2458#endif
2459
2460#if (_MSC_VER && (_M_IX86 || _M_X64)) || defined(__i386__) || defined(__x86_64__)
2461
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];
2464#if _MSC_VER
2465 __cpuidex(cpu_info, leaf_id, subid);
2466#else
2467 __cpuid_count(leaf_id, subid, cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]);
2468#endif
2469 *eax = cpu_info[0];
2470 *ebx = cpu_info[1];
2471 *ecx = cpu_info[2];
2472 *edx = cpu_info[3];
2473}
2474
2475static inline zig_u32 zig_x86_get_xcr0(void) {
2476#if _MSC_VER
2477 return (zig_u32)_xgetbv(0);
2478#else
2479 zig_u32 eax;
2480 zig_u32 edx;
2481 __asm__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0));
2482 return eax;
2483#endif
2484}
2485
2486#endif