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
783783 COMPILE_FLAGS ${ZIG2_COMPILE_FLAGS}
784784 LINK_FLAGS ${ZIG2_LINK_FLAGS}
785785)
786target_include_directories(zig2 PUBLIC "${CMAKE_SOURCE_DIR}/lib")
786target_include_directories(zig2 PUBLIC "${CMAKE_SOURCE_DIR}/stage1")
787787target_link_libraries(zig2 LINK_PUBLIC zigcpp)
788788
789789if(MSVC)
build.zig+31
......@@ -506,8 +506,39 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
506506 run_opt.addArg("-o");
507507 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
509539 const update_zig1_step = b.step("update-zig1", "Update stage1/zig1.wasm");
510540 update_zig1_step.dependOn(&run_opt.step);
541 update_zig1_step.dependOn(&copy_zig_h.step);
511542}
512543
513544fn addCompilerStep(
lib/zig.h+773-667
......@@ -1,6 +1,8 @@
11#undef linux
22
3#ifndef __STDC_WANT_IEC_60559_TYPES_EXT__
34#define __STDC_WANT_IEC_60559_TYPES_EXT__
5#endif
46#include <float.h>
57#include <limits.h>
68#include <stddef.h>
......@@ -297,690 +299,791 @@ typedef char bool;
297299
298300#define zig_bitSizeOf(T) (CHAR_BIT * sizeof(T))
299301
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
302#define zig_compiler_rt_abbrev_uint32_t si
303#define zig_compiler_rt_abbrev_int32_t si
304#define zig_compiler_rt_abbrev_uint64_t di
305#define zig_compiler_rt_abbrev_int64_t di
306#define zig_compiler_rt_abbrev_zig_u128 ti
307#define zig_compiler_rt_abbrev_zig_i128 ti
308#define zig_compiler_rt_abbrev_zig_f16 hf
309#define zig_compiler_rt_abbrev_zig_f32 sf
310#define zig_compiler_rt_abbrev_zig_f64 df
311#define zig_compiler_rt_abbrev_zig_f80 xf
312#define zig_compiler_rt_abbrev_zig_f128 tf
313
314zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, size_t);
315zig_extern void *memset (void *, int, size_t);
316
317/* ===================== 8/16/32/64-bit Integer Support ===================== */
318
319#if __STDC_VERSION__ >= 199901L
320#include <stdint.h>
321#else
322
323#if SCHAR_MIN == ~0x7F && SCHAR_MAX == 0x7F && UCHAR_MAX == 0xFF
324typedef unsigned char uint8_t;
325typedef signed char int8_t;
326#define INT8_C(c) c
327#define UINT8_C(c) c##U
328#elif SHRT_MIN == ~0x7F && SHRT_MAX == 0x7F && USHRT_MAX == 0xFF
329typedef unsigned short uint8_t;
330typedef signed short int8_t;
331#define INT8_C(c) c
332#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
331448#define zig_minInt_i8 INT8_MIN
332449#define zig_maxInt_i8 INT8_MAX
333#define zig_minInt_u16 zig_as_u16(0)
334#define zig_maxInt_u16 UINT16_MAX
450#define zig_minInt_u8 UINT8_C(0)
451#define zig_maxInt_u8 UINT8_MAX
335452#define zig_minInt_i16 INT16_MIN
336453#define zig_maxInt_i16 INT16_MAX
337#define zig_minInt_u32 zig_as_u32(0)
338#define zig_maxInt_u32 UINT32_MAX
454#define zig_minInt_u16 UINT16_C(0)
455#define zig_maxInt_u16 UINT16_MAX
339456#define zig_minInt_i32 INT32_MIN
340457#define zig_maxInt_i32 INT32_MAX
341#define zig_minInt_u64 zig_as_u64(0)
342#define zig_maxInt_u64 UINT64_MAX
458#define zig_minInt_u32 UINT32_C(0)
459#define zig_maxInt_u32 UINT32_MAX
343460#define zig_minInt_i64 INT64_MIN
344461#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 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)
465#define zig_intLimit(s, w, limit, bits) zig_shr_##s##w(zig_##limit##Int_##s##w, w - (bits))
466#define zig_minInt_i(w, bits) zig_intLimit(i, w, min, bits)
467#define zig_maxInt_i(w, bits) zig_intLimit(i, w, max, bits)
468#define zig_minInt_u(w, bits) zig_intLimit(u, w, min, bits)
469#define zig_maxInt_u(w, bits) zig_intLimit(u, w, max, bits)
367470
368471#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) { \
370473 return lhs operator rhs; \
371474 }
372475#define zig_int_basic_operator(Type, operation, operator) \
373 zig_int_operator(Type, Type, operation, operator)
476 zig_int_operator(Type, Type, operation, operator)
374477#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)
376479#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, >>) \
480 zig_int_basic_operator(uint##w##_t, and_u##w, &) \
481 zig_int_basic_operator( int##w##_t, and_i##w, &) \
482 zig_int_basic_operator(uint##w##_t, or_u##w, |) \
483 zig_int_basic_operator( int##w##_t, or_i##w, |) \
484 zig_int_basic_operator(uint##w##_t, xor_u##w, ^) \
485 zig_int_basic_operator( int##w##_t, xor_i##w, ^) \
486 zig_int_shift_operator(uint##w##_t, shl_u##w, <<) \
487 zig_int_shift_operator( int##w##_t, shl_i##w, <<) \
488 zig_int_shift_operator(uint##w##_t, shr_u##w, >>) \
386489\
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); \
490 static inline int##w##_t zig_shr_i##w(int##w##_t lhs, uint8_t rhs) { \
491 int##w##_t sign_mask = lhs < INT##w##_C(0) ? -INT##w##_C(1) : INT##w##_C(0); \
389492 return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \
390493 } \
391494\
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); \
495 static inline uint##w##_t zig_not_u##w(uint##w##_t val, uint8_t bits) { \
496 return val ^ zig_maxInt_u(w, bits); \
394497 } \
395498\
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) { \
397500 (void)bits; \
398501 return ~val; \
399502 } \
400503\
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); \
504 static inline uint##w##_t zig_wrap_u##w(uint##w##_t val, uint8_t bits) { \
505 return val & zig_maxInt_u(w, bits); \
403506 } \
404507\
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); \
508 static inline int##w##_t zig_wrap_i##w(int##w##_t val, uint8_t bits) { \
509 return (val & UINT##w##_C(1) << (bits - UINT8_C(1))) != 0 \
510 ? val | zig_minInt_i(w, bits) : val & zig_maxInt_i(w, bits); \
408511 } \
409512\
410 zig_int_basic_operator(u##w, div_floor, /) \
513 zig_int_basic_operator(uint##w##_t, div_floor_u##w, /) \
411514\
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)); \
515 static inline int##w##_t zig_div_floor_i##w(int##w##_t lhs, int##w##_t rhs) { \
516 return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < INT##w##_C(0)); \
414517 } \
415518\
416 zig_int_basic_operator(u##w, mod, %) \
519 zig_int_basic_operator(uint##w##_t, mod_u##w, %) \
417520\
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)); \
521 static inline int##w##_t zig_mod_i##w(int##w##_t lhs, int##w##_t rhs) { \
522 int##w##_t rem = lhs % rhs; \
523 return rem + (((lhs ^ rhs) & rem) < INT##w##_C(0) ? rhs : INT##w##_C(0)); \
421524 } \
422525\
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) { \
424527 return zig_wrap_u##w(zig_shl_u##w(lhs, rhs), bits); \
425528 } \
426529\
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); \
530 static inline int##w##_t zig_shlw_i##w(int##w##_t lhs, uint8_t rhs, uint8_t bits) { \
531 return zig_wrap_i##w((int##w##_t)zig_shl_u##w((uint##w##_t)lhs, (uint##w##_t)rhs), bits); \
429532 } \
430533\
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) { \
432535 return zig_wrap_u##w(lhs + rhs, bits); \
433536 } \
434537\
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); \
538 static inline int##w##_t zig_addw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
539 return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs + (uint##w##_t)rhs), bits); \
437540 } \
438541\
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) { \
440543 return zig_wrap_u##w(lhs - rhs, bits); \
441544 } \
442545\
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); \
546 static inline int##w##_t zig_subw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
547 return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs - (uint##w##_t)rhs), bits); \
445548 } \
446549\
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) { \
448551 return zig_wrap_u##w(lhs * rhs, bits); \
449552 } \
450553\
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); \
554 static inline int##w##_t zig_mulw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
555 return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs * (uint##w##_t)rhs), bits); \
453556 }
454557zig_int_helpers(8)
455558zig_int_helpers(16)
456559zig_int_helpers(32)
457560zig_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) {
460563#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
461 zig_u32 full_res;
564 uint32_t full_res;
462565 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
463566 *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);
465568#else
466569 *res = zig_addw_u32(lhs, rhs, bits);
467570 return *res < lhs;
468571#endif
469572}
470573
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)
574static inline void zig_vaddo_u32(uint8_t *ov, uint32_t *res, int n,
575 const uint32_t *lhs, const uint32_t *rhs, uint8_t bits)
473576{
474577 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u32(&res[i], lhs[i], rhs[i], bits);
475578}
476579
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) {
580zig_extern int32_t __addosi4(int32_t lhs, int32_t rhs, int *overflow);
581static inline bool zig_addo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
479582#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
480 zig_i32 full_res;
583 int32_t full_res;
481584 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
482585#else
483 zig_c_int overflow_int;
484 zig_i32 full_res = __addosi4(lhs, rhs, &overflow_int);
586 int overflow_int;
587 int32_t full_res = __addosi4(lhs, rhs, &overflow_int);
485588 bool overflow = overflow_int != 0;
486589#endif
487590 *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);
489592}
490593
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)
594static inline void zig_vaddo_i32(uint8_t *ov, int32_t *res, int n,
595 const int32_t *lhs, const int32_t *rhs, uint8_t bits)
493596{
494597 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i32(&res[i], lhs[i], rhs[i], bits);
495598}
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) {
498601#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
499 zig_u64 full_res;
602 uint64_t full_res;
500603 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
501604 *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);
503606#else
504607 *res = zig_addw_u64(lhs, rhs, bits);
505608 return *res < lhs;
506609#endif
507610}
508611
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)
612static inline void zig_vaddo_u64(uint8_t *ov, uint64_t *res, int n,
613 const uint64_t *lhs, const uint64_t *rhs, uint8_t bits)
511614{
512615 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u64(&res[i], lhs[i], rhs[i], bits);
513616}
514617
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) {
618zig_extern int64_t __addodi4(int64_t lhs, int64_t rhs, int *overflow);
619static inline bool zig_addo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
517620#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
518 zig_i64 full_res;
621 int64_t full_res;
519622 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
520623#else
521 zig_c_int overflow_int;
522 zig_i64 full_res = __addodi4(lhs, rhs, &overflow_int);
624 int overflow_int;
625 int64_t full_res = __addodi4(lhs, rhs, &overflow_int);
523626 bool overflow = overflow_int != 0;
524627#endif
525628 *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);
527630}
528631
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)
632static inline void zig_vaddo_i64(uint8_t *ov, int64_t *res, int n,
633 const int64_t *lhs, const int64_t *rhs, uint8_t bits)
531634{
532635 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i64(&res[i], lhs[i], rhs[i], bits);
533636}
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) {
536639#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
537 zig_u8 full_res;
640 uint8_t full_res;
538641 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
539642 *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);
541644#else
542 zig_u32 full_res;
645 uint32_t full_res;
543646 bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits);
544 *res = (zig_u8)full_res;
647 *res = (uint8_t)full_res;
545648 return overflow;
546649#endif
547650}
548651
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)
652static inline void zig_vaddo_u8(uint8_t *ov, uint8_t *res, int n,
653 const uint8_t *lhs, const uint8_t *rhs, uint8_t bits)
551654{
552655 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u8(&res[i], lhs[i], rhs[i], bits);
553656}
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) {
556659#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
557 zig_i8 full_res;
660 int8_t full_res;
558661 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
559662 *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);
561664#else
562 zig_i32 full_res;
665 int32_t full_res;
563666 bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits);
564 *res = (zig_i8)full_res;
667 *res = (int8_t)full_res;
565668 return overflow;
566669#endif
567670}
568671
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)
672static inline void zig_vaddo_i8(uint8_t *ov, int8_t *res, int n,
673 const int8_t *lhs, const int8_t *rhs, uint8_t bits)
571674{
572675 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i8(&res[i], lhs[i], rhs[i], bits);
573676}
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) {
576679#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
577 zig_u16 full_res;
680 uint16_t full_res;
578681 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
579682 *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);
581684#else
582 zig_u32 full_res;
685 uint32_t full_res;
583686 bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits);
584 *res = (zig_u16)full_res;
687 *res = (uint16_t)full_res;
585688 return overflow;
586689#endif
587690}
588691
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)
692static inline void zig_vaddo_u16(uint8_t *ov, uint16_t *res, int n,
693 const uint16_t *lhs, const uint16_t *rhs, uint8_t bits)
591694{
592695 for (int i = 0; i < n; ++i) ov[i] = zig_addo_u16(&res[i], lhs[i], rhs[i], bits);
593696}
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) {
596699#if zig_has_builtin(add_overflow) || defined(zig_gnuc)
597 zig_i16 full_res;
700 int16_t full_res;
598701 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
599702 *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);
601704#else
602 zig_i32 full_res;
705 int32_t full_res;
603706 bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits);
604 *res = (zig_i16)full_res;
707 *res = (int16_t)full_res;
605708 return overflow;
606709#endif
607710}
608711
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)
712static inline void zig_vaddo_i16(uint8_t *ov, int16_t *res, int n,
713 const int16_t *lhs, const int16_t *rhs, uint8_t bits)
611714{
612715 for (int i = 0; i < n; ++i) ov[i] = zig_addo_i16(&res[i], lhs[i], rhs[i], bits);
613716}
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) {
616719#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
617 zig_u32 full_res;
720 uint32_t full_res;
618721 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
619722 *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);
621724#else
622725 *res = zig_subw_u32(lhs, rhs, bits);
623726 return *res > lhs;
624727#endif
625728}
626729
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)
730static inline void zig_vsubo_u32(uint8_t *ov, uint32_t *res, int n,
731 const uint32_t *lhs, const uint32_t *rhs, uint8_t bits)
629732{
630733 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u32(&res[i], lhs[i], rhs[i], bits);
631734}
632735
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) {
736zig_extern int32_t __subosi4(int32_t lhs, int32_t rhs, int *overflow);
737static inline bool zig_subo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
635738#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
636 zig_i32 full_res;
739 int32_t full_res;
637740 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
638741#else
639 zig_c_int overflow_int;
640 zig_i32 full_res = __subosi4(lhs, rhs, &overflow_int);
742 int overflow_int;
743 int32_t full_res = __subosi4(lhs, rhs, &overflow_int);
641744 bool overflow = overflow_int != 0;
642745#endif
643746 *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);
645748}
646749
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)
750static inline void zig_vsubo_i32(uint8_t *ov, int32_t *res, int n,
751 const int32_t *lhs, const int32_t *rhs, uint8_t bits)
649752{
650753 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i32(&res[i], lhs[i], rhs[i], bits);
651754}
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) {
654757#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
655 zig_u64 full_res;
758 uint64_t full_res;
656759 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
657760 *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);
659762#else
660763 *res = zig_subw_u64(lhs, rhs, bits);
661764 return *res > lhs;
662765#endif
663766}
664767
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)
768static inline void zig_vsubo_u64(uint8_t *ov, uint64_t *res, int n,
769 const uint64_t *lhs, const uint64_t *rhs, uint8_t bits)
667770{
668771 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u64(&res[i], lhs[i], rhs[i], bits);
669772}
670773
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) {
774zig_extern int64_t __subodi4(int64_t lhs, int64_t rhs, int *overflow);
775static inline bool zig_subo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
673776#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
674 zig_i64 full_res;
777 int64_t full_res;
675778 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
676779#else
677 zig_c_int overflow_int;
678 zig_i64 full_res = __subodi4(lhs, rhs, &overflow_int);
780 int overflow_int;
781 int64_t full_res = __subodi4(lhs, rhs, &overflow_int);
679782 bool overflow = overflow_int != 0;
680783#endif
681784 *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);
683786}
684787
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)
788static inline void zig_vsubo_i64(uint8_t *ov, int64_t *res, int n,
789 const int64_t *lhs, const int64_t *rhs, uint8_t bits)
687790{
688791 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i64(&res[i], lhs[i], rhs[i], bits);
689792}
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) {
692795#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
693 zig_u8 full_res;
796 uint8_t full_res;
694797 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
695798 *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);
697800#else
698 zig_u32 full_res;
801 uint32_t full_res;
699802 bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits);
700 *res = (zig_u8)full_res;
803 *res = (uint8_t)full_res;
701804 return overflow;
702805#endif
703806}
704807
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)
808static inline void zig_vsubo_u8(uint8_t *ov, uint8_t *res, int n,
809 const uint8_t *lhs, const uint8_t *rhs, uint8_t bits)
707810{
708811 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u8(&res[i], lhs[i], rhs[i], bits);
709812}
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) {
712815#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
713 zig_i8 full_res;
816 int8_t full_res;
714817 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
715818 *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);
717820#else
718 zig_i32 full_res;
821 int32_t full_res;
719822 bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits);
720 *res = (zig_i8)full_res;
823 *res = (int8_t)full_res;
721824 return overflow;
722825#endif
723826}
724827
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)
828static inline void zig_vsubo_i8(uint8_t *ov, int8_t *res, int n,
829 const int8_t *lhs, const int8_t *rhs, uint8_t bits)
727830{
728831 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i8(&res[i], lhs[i], rhs[i], bits);
729832}
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) {
733836#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
734 zig_u16 full_res;
837 uint16_t full_res;
735838 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
736839 *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);
738841#else
739 zig_u32 full_res;
842 uint32_t full_res;
740843 bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits);
741 *res = (zig_u16)full_res;
844 *res = (uint16_t)full_res;
742845 return overflow;
743846#endif
744847}
745848
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)
849static inline void zig_vsubo_u16(uint8_t *ov, uint16_t *res, int n,
850 const uint16_t *lhs, const uint16_t *rhs, uint8_t bits)
748851{
749852 for (int i = 0; i < n; ++i) ov[i] = zig_subo_u16(&res[i], lhs[i], rhs[i], bits);
750853}
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) {
754857#if zig_has_builtin(sub_overflow) || defined(zig_gnuc)
755 zig_i16 full_res;
858 int16_t full_res;
756859 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
757860 *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);
759862#else
760 zig_i32 full_res;
863 int32_t full_res;
761864 bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits);
762 *res = (zig_i16)full_res;
865 *res = (int16_t)full_res;
763866 return overflow;
764867#endif
765868}
766869
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)
870static inline void zig_vsubo_i16(uint8_t *ov, int16_t *res, int n,
871 const int16_t *lhs, const int16_t *rhs, uint8_t bits)
769872{
770873 for (int i = 0; i < n; ++i) ov[i] = zig_subo_i16(&res[i], lhs[i], rhs[i], bits);
771874}
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) {
774877#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
775 zig_u32 full_res;
878 uint32_t full_res;
776879 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
777880 *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);
779882#else
780883 *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;
782885#endif
783886}
784887
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)
888static inline void zig_vmulo_u32(uint8_t *ov, uint32_t *res, int n,
889 const uint32_t *lhs, const uint32_t *rhs, uint8_t bits)
787890{
788891 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u32(&res[i], lhs[i], rhs[i], bits);
789892}
790893
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) {
894zig_extern int32_t __mulosi4(int32_t lhs, int32_t rhs, int *overflow);
895static inline bool zig_mulo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) {
793896#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
794 zig_i32 full_res;
897 int32_t full_res;
795898 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
796899#else
797 zig_c_int overflow_int;
798 zig_i32 full_res = __mulosi4(lhs, rhs, &overflow_int);
900 int overflow_int;
901 int32_t full_res = __mulosi4(lhs, rhs, &overflow_int);
799902 bool overflow = overflow_int != 0;
800903#endif
801904 *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);
803906}
804907
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)
908static inline void zig_vmulo_i32(uint8_t *ov, int32_t *res, int n,
909 const int32_t *lhs, const int32_t *rhs, uint8_t bits)
807910{
808911 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i32(&res[i], lhs[i], rhs[i], bits);
809912}
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) {
812915#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
813 zig_u64 full_res;
916 uint64_t full_res;
814917 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
815918 *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);
817920#else
818921 *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;
820923#endif
821924}
822925
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)
926static inline void zig_vmulo_u64(uint8_t *ov, uint64_t *res, int n,
927 const uint64_t *lhs, const uint64_t *rhs, uint8_t bits)
825928{
826929 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u64(&res[i], lhs[i], rhs[i], bits);
827930}
828931
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) {
932zig_extern int64_t __mulodi4(int64_t lhs, int64_t rhs, int *overflow);
933static inline bool zig_mulo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) {
831934#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
832 zig_i64 full_res;
935 int64_t full_res;
833936 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
834937#else
835 zig_c_int overflow_int;
836 zig_i64 full_res = __mulodi4(lhs, rhs, &overflow_int);
938 int overflow_int;
939 int64_t full_res = __mulodi4(lhs, rhs, &overflow_int);
837940 bool overflow = overflow_int != 0;
838941#endif
839942 *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);
841944}
842945
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)
946static inline void zig_vmulo_i64(uint8_t *ov, int64_t *res, int n,
947 const int64_t *lhs, const int64_t *rhs, uint8_t bits)
845948{
846949 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i64(&res[i], lhs[i], rhs[i], bits);
847950}
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) {
850953#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
851 zig_u8 full_res;
954 uint8_t full_res;
852955 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
853956 *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);
855958#else
856 zig_u32 full_res;
959 uint32_t full_res;
857960 bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits);
858 *res = (zig_u8)full_res;
961 *res = (uint8_t)full_res;
859962 return overflow;
860963#endif
861964}
862965
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)
966static inline void zig_vmulo_u8(uint8_t *ov, uint8_t *res, int n,
967 const uint8_t *lhs, const uint8_t *rhs, uint8_t bits)
865968{
866969 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u8(&res[i], lhs[i], rhs[i], bits);
867970}
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) {
870973#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
871 zig_i8 full_res;
974 int8_t full_res;
872975 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
873976 *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);
875978#else
876 zig_i32 full_res;
979 int32_t full_res;
877980 bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits);
878 *res = (zig_i8)full_res;
981 *res = (int8_t)full_res;
879982 return overflow;
880983#endif
881984}
882985
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)
986static inline void zig_vmulo_i8(uint8_t *ov, int8_t *res, int n,
987 const int8_t *lhs, const int8_t *rhs, uint8_t bits)
885988{
886989 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i8(&res[i], lhs[i], rhs[i], bits);
887990}
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) {
890993#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
891 zig_u16 full_res;
994 uint16_t full_res;
892995 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
893996 *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);
895998#else
896 zig_u32 full_res;
999 uint32_t full_res;
8971000 bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits);
898 *res = (zig_u16)full_res;
1001 *res = (uint16_t)full_res;
8991002 return overflow;
9001003#endif
9011004}
9021005
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)
1006static inline void zig_vmulo_u16(uint8_t *ov, uint16_t *res, int n,
1007 const uint16_t *lhs, const uint16_t *rhs, uint8_t bits)
9051008{
9061009 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u16(&res[i], lhs[i], rhs[i], bits);
9071010}
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) {
9101013#if zig_has_builtin(mul_overflow) || defined(zig_gnuc)
911 zig_i16 full_res;
1014 int16_t full_res;
9121015 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
9131016 *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);
9151018#else
916 zig_i32 full_res;
1019 int32_t full_res;
9171020 bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits);
918 *res = (zig_i16)full_res;
1021 *res = (int16_t)full_res;
9191022 return overflow;
9201023#endif
9211024}
9221025
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)
1026static inline void zig_vmulo_i16(uint8_t *ov, int16_t *res, int n,
1027 const int16_t *lhs, const int16_t *rhs, uint8_t bits)
9251028{
9261029 for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i16(&res[i], lhs[i], rhs[i], bits);
9271030}
9281031
9291032#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) { \
9311034 *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; \
9331036 } \
9341037\
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) { \
9361039 *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; \
1040 int##w##_t mask = (int##w##_t)(UINT##w##_MAX << (bits - rhs - 1)); \
1041 return (lhs & mask) != INT##w##_C(0) && (lhs & mask) != mask; \
9391042 } \
9401043\
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; \
1044 static inline uint##w##_t zig_shls_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
1045 uint##w##_t res; \
1046 if (rhs >= bits) return lhs != UINT##w##_C(0) ? zig_maxInt_u(w, bits) : lhs; \
1047 return zig_shlo_u##w(&res, lhs, (uint8_t)rhs, bits) ? zig_maxInt_u(w, bits) : res; \
9451048 } \
9461049\
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); \
1050 static inline int##w##_t zig_shls_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
1051 int##w##_t res; \
1052 if ((uint##w##_t)rhs < (uint##w##_t)bits && !zig_shlo_i##w(&res, lhs, rhs, bits)) return res; \
1053 return lhs < INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \
9511054 } \
9521055\
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; \
1056 static inline uint##w##_t zig_adds_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
1057 uint##w##_t res; \
1058 return zig_addo_u##w(&res, lhs, rhs, bits) ? zig_maxInt_u(w, bits) : res; \
9561059 } \
9571060\
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; \
1061 static inline int##w##_t zig_adds_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
1062 int##w##_t res; \
9601063 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); \
9621065 } \
9631066\
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; \
1067 static inline uint##w##_t zig_subs_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
1068 uint##w##_t res; \
1069 return zig_subo_u##w(&res, lhs, rhs, bits) ? zig_minInt_u(w, bits) : res; \
9671070 } \
9681071\
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; \
1072 static inline int##w##_t zig_subs_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
1073 int##w##_t res; \
9711074 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); \
9731076 } \
9741077\
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; \
1078 static inline uint##w##_t zig_muls_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \
1079 uint##w##_t res; \
1080 return zig_mulo_u##w(&res, lhs, rhs, bits) ? zig_maxInt_u(w, bits) : res; \
9781081 } \
9791082\
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; \
1083 static inline int##w##_t zig_muls_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \
1084 int##w##_t res; \
9821085 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); \
9841087 }
9851088zig_int_builtins(8)
9861089zig_int_builtins(16)
......@@ -988,89 +1091,89 @@ zig_int_builtins(32)
9881091zig_int_builtins(64)
9891092
9901093#define zig_builtin8(name, val) __builtin_##name(val)
991typedef zig_c_uint zig_Builtin8;
1094typedef unsigned int zig_Builtin8;
9921095
9931096#define zig_builtin16(name, val) __builtin_##name(val)
994typedef zig_c_uint zig_Builtin16;
1097typedef unsigned int zig_Builtin16;
9951098
9961099#if INT_MIN <= INT32_MIN
9971100#define zig_builtin32(name, val) __builtin_##name(val)
998typedef zig_c_uint zig_Builtin32;
1101typedef unsigned int zig_Builtin32;
9991102#elif LONG_MIN <= INT32_MIN
10001103#define zig_builtin32(name, val) __builtin_##name##l(val)
1001typedef zig_c_ulong zig_Builtin32;
1104typedef unsigned long zig_Builtin32;
10021105#endif
10031106
10041107#if INT_MIN <= INT64_MIN
10051108#define zig_builtin64(name, val) __builtin_##name(val)
1006typedef zig_c_uint zig_Builtin64;
1109typedef unsigned int zig_Builtin64;
10071110#elif LONG_MIN <= INT64_MIN
10081111#define zig_builtin64(name, val) __builtin_##name##l(val)
1009typedef zig_c_ulong zig_Builtin64;
1112typedef unsigned long zig_Builtin64;
10101113#elif LLONG_MIN <= INT64_MIN
10111114#define zig_builtin64(name, val) __builtin_##name##ll(val)
1012typedef zig_c_ulonglong zig_Builtin64;
1115typedef unsigned long long zig_Builtin64;
10131116#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) {
10161119 return zig_wrap_u8(val >> (8 - bits), bits);
10171120}
10181121
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);
1122static inline int8_t zig_byte_swap_i8(int8_t val, uint8_t bits) {
1123 return zig_wrap_i8((int8_t)zig_byte_swap_u8((uint8_t)val, bits), bits);
10211124}
10221125
1023static inline zig_u16 zig_byte_swap_u16(zig_u16 val, zig_u8 bits) {
1024 zig_u16 full_res;
1126static inline uint16_t zig_byte_swap_u16(uint16_t val, uint8_t bits) {
1127 uint16_t full_res;
10251128#if zig_has_builtin(bswap16) || defined(zig_gnuc)
10261129 full_res = __builtin_bswap16(val);
10271130#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;
1131 full_res = (uint16_t)zig_byte_swap_u8((uint8_t)(val >> 0), 8) << 8 |
1132 (uint16_t)zig_byte_swap_u8((uint8_t)(val >> 8), 8) >> 0;
10301133#endif
10311134 return zig_wrap_u16(full_res >> (16 - bits), bits);
10321135}
10331136
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);
1137static inline int16_t zig_byte_swap_i16(int16_t val, uint8_t bits) {
1138 return zig_wrap_i16((int16_t)zig_byte_swap_u16((uint16_t)val, bits), bits);
10361139}
10371140
1038static inline zig_u32 zig_byte_swap_u32(zig_u32 val, zig_u8 bits) {
1039 zig_u32 full_res;
1141static inline uint32_t zig_byte_swap_u32(uint32_t val, uint8_t bits) {
1142 uint32_t full_res;
10401143#if zig_has_builtin(bswap32) || defined(zig_gnuc)
10411144 full_res = __builtin_bswap32(val);
10421145#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;
1146 full_res = (uint32_t)zig_byte_swap_u16((uint16_t)(val >> 0), 16) << 16 |
1147 (uint32_t)zig_byte_swap_u16((uint16_t)(val >> 16), 16) >> 0;
10451148#endif
10461149 return zig_wrap_u32(full_res >> (32 - bits), bits);
10471150}
10481151
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);
1152static inline int32_t zig_byte_swap_i32(int32_t val, uint8_t bits) {
1153 return zig_wrap_i32((int32_t)zig_byte_swap_u32((uint32_t)val, bits), bits);
10511154}
10521155
1053static inline zig_u64 zig_byte_swap_u64(zig_u64 val, zig_u8 bits) {
1054 zig_u64 full_res;
1156static inline uint64_t zig_byte_swap_u64(uint64_t val, uint8_t bits) {
1157 uint64_t full_res;
10551158#if zig_has_builtin(bswap64) || defined(zig_gnuc)
10561159 full_res = __builtin_bswap64(val);
10571160#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;
1161 full_res = (uint64_t)zig_byte_swap_u32((uint32_t)(val >> 0), 32) << 32 |
1162 (uint64_t)zig_byte_swap_u32((uint32_t)(val >> 32), 32) >> 0;
10601163#endif
10611164 return zig_wrap_u64(full_res >> (64 - bits), bits);
10621165}
10631166
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);
1167static inline int64_t zig_byte_swap_i64(int64_t val, uint8_t bits) {
1168 return zig_wrap_i64((int64_t)zig_byte_swap_u64((uint64_t)val, bits), bits);
10661169}
10671170
1068static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) {
1069 zig_u8 full_res;
1171static inline uint8_t zig_bit_reverse_u8(uint8_t val, uint8_t bits) {
1172 uint8_t full_res;
10701173#if zig_has_builtin(bitreverse8)
10711174 full_res = __builtin_bitreverse8(val);
10721175#else
1073 static zig_u8 const lut[0x10] = {
1176 static uint8_t const lut[0x10] = {
10741177 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
10751178 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
10761179 };
......@@ -1079,62 +1182,62 @@ static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) {
10791182 return zig_wrap_u8(full_res >> (8 - bits), bits);
10801183}
10811184
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);
1185static inline int8_t zig_bit_reverse_i8(int8_t val, uint8_t bits) {
1186 return zig_wrap_i8((int8_t)zig_bit_reverse_u8((uint8_t)val, bits), bits);
10841187}
10851188
1086static inline zig_u16 zig_bit_reverse_u16(zig_u16 val, zig_u8 bits) {
1087 zig_u16 full_res;
1189static inline uint16_t zig_bit_reverse_u16(uint16_t val, uint8_t bits) {
1190 uint16_t full_res;
10881191#if zig_has_builtin(bitreverse16)
10891192 full_res = __builtin_bitreverse16(val);
10901193#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;
1194 full_res = (uint16_t)zig_bit_reverse_u8((uint8_t)(val >> 0), 8) << 8 |
1195 (uint16_t)zig_bit_reverse_u8((uint8_t)(val >> 8), 8) >> 0;
10931196#endif
10941197 return zig_wrap_u16(full_res >> (16 - bits), bits);
10951198}
10961199
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);
1200static inline int16_t zig_bit_reverse_i16(int16_t val, uint8_t bits) {
1201 return zig_wrap_i16((int16_t)zig_bit_reverse_u16((uint16_t)val, bits), bits);
10991202}
11001203
1101static inline zig_u32 zig_bit_reverse_u32(zig_u32 val, zig_u8 bits) {
1102 zig_u32 full_res;
1204static inline uint32_t zig_bit_reverse_u32(uint32_t val, uint8_t bits) {
1205 uint32_t full_res;
11031206#if zig_has_builtin(bitreverse32)
11041207 full_res = __builtin_bitreverse32(val);
11051208#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;
1209 full_res = (uint32_t)zig_bit_reverse_u16((uint16_t)(val >> 0), 16) << 16 |
1210 (uint32_t)zig_bit_reverse_u16((uint16_t)(val >> 16), 16) >> 0;
11081211#endif
11091212 return zig_wrap_u32(full_res >> (32 - bits), bits);
11101213}
11111214
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);
1215static inline int32_t zig_bit_reverse_i32(int32_t val, uint8_t bits) {
1216 return zig_wrap_i32((int32_t)zig_bit_reverse_u32((uint32_t)val, bits), bits);
11141217}
11151218
1116static inline zig_u64 zig_bit_reverse_u64(zig_u64 val, zig_u8 bits) {
1117 zig_u64 full_res;
1219static inline uint64_t zig_bit_reverse_u64(uint64_t val, uint8_t bits) {
1220 uint64_t full_res;
11181221#if zig_has_builtin(bitreverse64)
11191222 full_res = __builtin_bitreverse64(val);
11201223#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;
1224 full_res = (uint64_t)zig_bit_reverse_u32((uint32_t)(val >> 0), 32) << 32 |
1225 (uint64_t)zig_bit_reverse_u32((uint32_t)(val >> 32), 32) >> 0;
11231226#endif
11241227 return zig_wrap_u64(full_res >> (64 - bits), bits);
11251228}
11261229
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);
1230static inline int64_t zig_bit_reverse_i64(int64_t val, uint8_t bits) {
1231 return zig_wrap_i64((int64_t)zig_bit_reverse_u64((uint64_t)val, bits), bits);
11291232}
11301233
11311234#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); \
1235 static inline uint8_t zig_popcount_i##w(int##w##_t val, uint8_t bits) { \
1236 return zig_popcount_u##w((uint##w##_t)val, bits); \
11341237 }
11351238#if zig_has_builtin(popcount) || defined(zig_gnuc)
11361239#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) { \
11381241 (void)bits; \
11391242 return zig_builtin##w(popcount, val); \
11401243 } \
......@@ -1142,12 +1245,12 @@ static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) {
11421245 zig_builtin_popcount_common(w)
11431246#else
11441247#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) { \
11461249 (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); \
1250 uint##w##_t temp = val - ((val >> 1) & (UINT##w##_MAX / 3)); \
1251 temp = (temp & (UINT##w##_MAX / 5)) + ((temp >> 2) & (UINT##w##_MAX / 5)); \
1252 temp = (temp + (temp >> 4)) & (UINT##w##_MAX / 17); \
1253 return temp * (UINT##w##_MAX / 255) >> (w - 8); \
11511254 } \
11521255\
11531256 zig_builtin_popcount_common(w)
......@@ -1158,12 +1261,12 @@ zig_builtin_popcount(32)
11581261zig_builtin_popcount(64)
11591262
11601263#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); \
1264 static inline uint8_t zig_ctz_i##w(int##w##_t val, uint8_t bits) { \
1265 return zig_ctz_u##w((uint##w##_t)val, bits); \
11631266 }
11641267#if zig_has_builtin(ctz) || defined(zig_gnuc)
11651268#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) { \
11671270 if (val == 0) return bits; \
11681271 return zig_builtin##w(ctz, val); \
11691272 } \
......@@ -1171,7 +1274,7 @@ zig_builtin_popcount(64)
11711274 zig_builtin_ctz_common(w)
11721275#else
11731276#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) { \
11751278 return zig_popcount_u##w(zig_not_u##w(val, bits) & zig_subw_u##w(val, 1, bits), bits); \
11761279 } \
11771280\
......@@ -1183,12 +1286,12 @@ zig_builtin_ctz(32)
11831286zig_builtin_ctz(64)
11841287
11851288#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); \
1289 static inline uint8_t zig_clz_i##w(int##w##_t val, uint8_t bits) { \
1290 return zig_clz_u##w((uint##w##_t)val, bits); \
11881291 }
11891292#if zig_has_builtin(clz) || defined(zig_gnuc)
11901293#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) { \
11921295 if (val == 0) return bits; \
11931296 return zig_builtin##w(clz, val) - (zig_bitSizeOf(zig_Builtin##w) - bits); \
11941297 } \
......@@ -1196,7 +1299,7 @@ zig_builtin_ctz(64)
11961299 zig_builtin_clz_common(w)
11971300#else
11981301#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) { \
12001303 return zig_ctz_u##w(zig_bit_reverse_u##w(val, bits), bits); \
12011304 } \
12021305\
......@@ -1207,7 +1310,7 @@ zig_builtin_clz(16)
12071310zig_builtin_clz(32)
12081311zig_builtin_clz(64)
12091312
1210/* ======================== 128-bit Integer Routines ======================== */
1313/* ======================== 128-bit Integer Support ========================= */
12111314
12121315#if !defined(zig_has_int128)
12131316# if defined(__SIZEOF_INT128__)
......@@ -1222,18 +1325,18 @@ zig_builtin_clz(64)
12221325typedef unsigned __int128 zig_u128;
12231326typedef signed __int128 zig_i128;
12241327
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))
1328#define zig_make_u128(hi, lo) ((zig_u128)(hi)<<64|(lo))
1329#define zig_make_i128(hi, lo) ((zig_i128)zig_make_u128(hi, lo))
1330#define zig_make_constant_u128(hi, lo) zig_make_u128(hi, lo)
1331#define zig_make_constant_i128(hi, lo) zig_make_i128(hi, lo)
1332#define zig_hi_u128(val) ((uint64_t)((val) >> 64))
1333#define zig_lo_u128(val) ((uint64_t)((val) >> 0))
1334#define zig_hi_i128(val) (( int64_t)((val) >> 64))
1335#define zig_lo_i128(val) ((uint64_t)((val) >> 0))
12331336#define zig_bitcast_u128(val) ((zig_u128)(val))
12341337#define zig_bitcast_i128(val) ((zig_i128)(val))
12351338#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) { \
12371340 return (lhs > rhs) - (lhs < rhs); \
12381341 }
12391342#define zig_bit_int128(Type, operation, operator) \
......@@ -1244,31 +1347,31 @@ typedef signed __int128 zig_i128;
12441347#else /* zig_has_int128 */
12451348
12461349#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;
1350typedef struct { zig_align(16) uint64_t lo; uint64_t hi; } zig_u128;
1351typedef struct { zig_align(16) uint64_t lo; int64_t hi; } zig_i128;
12491352#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;
1353typedef struct { zig_align(16) uint64_t hi; uint64_t lo; } zig_u128;
1354typedef struct { zig_align(16) int64_t hi; uint64_t lo; } zig_i128;
12521355#endif
12531356
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) })
1357#define zig_make_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) })
1358#define zig_make_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) })
12561359
12571360#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) }
1361#define zig_make_constant_u128(hi, lo) { .h##i = (hi), .l##o = (lo) }
1362#define zig_make_constant_i128(hi, lo) { .h##i = (hi), .l##o = (lo) }
12601363#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)
1364#define zig_make_constant_u128(hi, lo) zig_make_u128(hi, lo)
1365#define zig_make_constant_i128(hi, lo) zig_make_i128(hi, lo)
12631366#endif
12641367#define zig_hi_u128(val) ((val).hi)
12651368#define zig_lo_u128(val) ((val).lo)
12661369#define zig_hi_i128(val) ((val).hi)
12671370#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)
1371#define zig_bitcast_u128(val) zig_make_u128((uint64_t)(val).hi, (val).lo)
1372#define zig_bitcast_i128(val) zig_make_i128(( int64_t)(val).hi, (val).lo)
12701373#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) { \
12721375 return (lhs.hi == rhs.hi) \
12731376 ? (lhs.lo > rhs.lo) - (lhs.lo < rhs.lo) \
12741377 : (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;
12801383
12811384#endif /* zig_has_int128 */
12821385
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)
1386#define zig_minInt_u128 zig_make_u128(zig_minInt_u64, zig_minInt_u64)
1387#define zig_maxInt_u128 zig_make_u128(zig_maxInt_u64, zig_maxInt_u64)
1388#define zig_minInt_i128 zig_make_i128(zig_minInt_i64, zig_minInt_u64)
1389#define zig_maxInt_i128 zig_make_i128(zig_maxInt_i64, zig_maxInt_u64)
12871390
12881391zig_cmp_int128(u128)
12891392zig_cmp_int128(i128)
......@@ -1297,28 +1400,28 @@ zig_bit_int128(i128, or, |)
12971400zig_bit_int128(u128, xor, ^)
12981401zig_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
13021405#if zig_has_int128
13031406
1304static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) {
1305 return val ^ zig_maxInt(u128, bits);
1407static inline zig_u128 zig_not_u128(zig_u128 val, uint8_t bits) {
1408 return val ^ zig_maxInt_u(128, bits);
13061409}
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) {
13091412 (void)bits;
13101413 return ~val;
13111414}
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) {
13141417 return lhs >> rhs;
13151418}
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) {
13181421 return lhs << rhs;
13191422}
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) {
13221425 return lhs << rhs;
13231426}
13241427
......@@ -1363,40 +1466,40 @@ static inline zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {
13631466}
13641467
13651468static 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));
13671470}
13681471
13691472static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
13701473 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));
13721475}
13731476
13741477#else /* zig_has_int128 */
13751478
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)) };
1479static inline zig_u128 zig_not_u128(zig_u128 val, uint8_t bits) {
1480 return (zig_u128){ .hi = zig_not_u64(val.hi, bits - UINT8_C(64)), .lo = zig_not_u64(val.lo, UINT8_C(64)) };
13781481}
13791482
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)) };
1483static inline zig_i128 zig_not_i128(zig_i128 val, uint8_t bits) {
1484 return (zig_i128){ .hi = zig_not_i64(val.hi, bits - UINT8_C(64)), .lo = zig_not_u64(val.lo, UINT8_C(64)) };
13821485}
13831486
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 };
1487static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) {
1488 if (rhs == UINT8_C(0)) return lhs;
1489 if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = zig_minInt_u64, .lo = lhs.hi >> (rhs - UINT8_C(64)) };
1490 return (zig_u128){ .hi = lhs.hi >> rhs, .lo = lhs.hi << (UINT8_C(64) - rhs) | lhs.lo >> rhs };
13881491}
13891492
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 };
1493static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) {
1494 if (rhs == UINT8_C(0)) return lhs;
1495 if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 };
1496 return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs };
13941497}
13951498
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 };
1499static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) {
1500 if (rhs == UINT8_C(0)) return lhs;
1501 if (rhs >= UINT8_C(64)) return (zig_i128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 };
1502 return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs };
14001503}
14011504
14021505static 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) {
14541557
14551558static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
14561559 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)));
14581561}
14591562
14601563static 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)));
14621565}
14631566
14641567#endif /* zig_has_int128 */
......@@ -1471,161 +1574,161 @@ static inline zig_u128 zig_nand_u128(zig_u128 lhs, zig_u128 rhs) {
14711574}
14721575
14731576static 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;
14751578}
14761579
14771580static 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;
14791582}
14801583
14811584static 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;
14831586}
14841587
14851588static 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;
14871590}
14881591
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);
1592static inline zig_i128 zig_shr_i128(zig_i128 lhs, uint8_t rhs) {
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);
14911594 return zig_xor_i128(zig_bitcast_i128(zig_shr_u128(zig_bitcast_u128(zig_xor_i128(lhs, sign_mask)), rhs)), sign_mask);
14921595}
14931596
1494static inline zig_u128 zig_wrap_u128(zig_u128 val, zig_u8 bits) {
1495 return zig_and_u128(val, zig_maxInt(u128, bits));
1597static inline zig_u128 zig_wrap_u128(zig_u128 val, uint8_t bits) {
1598 return zig_and_u128(val, zig_maxInt_u(128, bits));
14961599}
14971600
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));
1601static inline zig_i128 zig_wrap_i128(zig_i128 val, uint8_t bits) {
1602 return zig_make_i128(zig_wrap_i64(zig_hi_i128(val), bits - UINT8_C(64)), zig_lo_i128(val));
15001603}
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) {
15031606 return zig_wrap_u128(zig_shl_u128(lhs, rhs), bits);
15041607}
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) {
15071610 return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), rhs)), bits);
15081611}
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) {
15111614 return zig_wrap_u128(zig_add_u128(lhs, rhs), bits);
15121615}
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) {
15151618 return zig_wrap_i128(zig_bitcast_i128(zig_add_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
15161619}
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) {
15191622 return zig_wrap_u128(zig_sub_u128(lhs, rhs), bits);
15201623}
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) {
15231626 return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
15241627}
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) {
15271630 return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits);
15281631}
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) {
15311634 return zig_wrap_i128(zig_bitcast_i128(zig_mul_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
15321635}
15331636
15341637#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) {
15371640#if zig_has_builtin(add_overflow)
15381641 zig_u128 full_res;
15391642 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
15401643 *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);
15421645#else
15431646 *res = zig_addw_u128(lhs, rhs, bits);
15441647 return *res < lhs;
15451648#endif
15461649}
15471650
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) {
1651zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1652static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
15501653#if zig_has_builtin(add_overflow)
15511654 zig_i128 full_res;
15521655 bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
15531656#else
1554 zig_c_int overflow_int;
1657 int overflow_int;
15551658 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
15561659 bool overflow = overflow_int != 0;
15571660#endif
15581661 *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);
15601663}
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) {
15631666#if zig_has_builtin(sub_overflow)
15641667 zig_u128 full_res;
15651668 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
15661669 *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);
15681671#else
15691672 *res = zig_subw_u128(lhs, rhs, bits);
15701673 return *res > lhs;
15711674#endif
15721675}
15731676
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) {
1677zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1678static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
15761679#if zig_has_builtin(sub_overflow)
15771680 zig_i128 full_res;
15781681 bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
15791682#else
1580 zig_c_int overflow_int;
1683 int overflow_int;
15811684 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
15821685 bool overflow = overflow_int != 0;
15831686#endif
15841687 *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);
15861689}
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) {
15891692#if zig_has_builtin(mul_overflow)
15901693 zig_u128 full_res;
15911694 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
15921695 *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);
15941697#else
15951698 *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;
15971700#endif
15981701}
15991702
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) {
1703zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1704static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
16021705#if zig_has_builtin(mul_overflow)
16031706 zig_i128 full_res;
16041707 bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
16051708#else
1606 zig_c_int overflow_int;
1709 int overflow_int;
16071710 zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int);
16081711 bool overflow = overflow_int != 0;
16091712#endif
16101713 *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);
16121715}
16131716
16141717#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) {
16171720 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);
1721 zig_cmp_u128(full_res, zig_minInt_u(128, bits)) < INT32_C(0) ||
1722 zig_cmp_u128(full_res, zig_maxInt_u(128, bits)) > INT32_C(0);
16201723}
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) {
16231726 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);
1727 zig_cmp_i128(full_res, zig_minInt_i(128, bits)) < INT32_C(0) ||
1728 zig_cmp_i128(full_res, zig_maxInt_i(128, bits)) > INT32_C(0);
16261729}
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) {
16291732 zig_u128 full_res;
16301733 bool overflow =
16311734 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_
16341737 return zig_overflow_u128(overflow, full_res, bits);
16351738}
16361739
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;
1740zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1741static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1742 int overflow_int;
16401743 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
16411744 *res = zig_wrap_i128(full_res, bits);
16421745 return zig_overflow_i128(overflow_int, full_res, bits);
16431746}
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) {
16461749 zig_u128 full_res;
16471750 bool overflow =
16481751 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_
16511754 return zig_overflow_u128(overflow, full_res, bits);
16521755}
16531756
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;
1757zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1758static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1759 int overflow_int;
16571760 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
16581761 *res = zig_wrap_i128(full_res, bits);
16591762 return zig_overflow_i128(overflow_int, full_res, bits);
16601763}
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) {
16631766 *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);
1767 return zig_cmp_u128(*res, zig_make_u128(0, 0)) != INT32_C(0) &&
1768 zig_cmp_u128(lhs, zig_div_trunc_u128(zig_maxInt_u(128, bits), rhs)) > INT32_C(0);
16661769}
16671770
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;
1771zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow);
1772static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) {
1773 int overflow_int;
16711774 zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int);
16721775 *res = zig_wrap_i128(full_res, bits);
16731776 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_
16751778
16761779#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) {
16791782 *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);
16811784}
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) {
16841787 *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);
1788 zig_i128 mask = zig_bitcast_i128(zig_shl_u128(zig_maxInt_u128, bits - rhs - UINT8_C(1)));
1789 return zig_cmp_i128(zig_and_i128(lhs, mask), zig_make_i128(0, 0)) != INT32_C(0) &&
1790 zig_cmp_i128(zig_and_i128(lhs, mask), mask) != INT32_C(0);
16881791}
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) {
16911794 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;
1795 if (zig_cmp_u128(rhs, zig_make_u128(0, bits)) >= INT32_C(0))
1796 return zig_cmp_u128(lhs, zig_make_u128(0, 0)) != INT32_C(0) ? zig_maxInt_u(128, bits) : lhs;
16941797
16951798#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;
16971800#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;
16991802#endif
17001803}
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) {
17031806 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);
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;
1808 return zig_cmp_i128(lhs, zig_make_i128(0, 0)) < INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits);
17061809}
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) {
17091812 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;
17111814}
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) {
17141817 zig_i128 res;
17151818 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);
17171820}
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) {
17201823 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;
17221825}
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) {
17251828 zig_i128 res;
17261829 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);
17281831}
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) {
17311834 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;
17331836}
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) {
17361839 zig_i128 res;
17371840 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);
17391842}
17401843
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));
1844static inline uint8_t zig_clz_u128(zig_u128 val, uint8_t bits) {
1845 if (bits <= UINT8_C(64)) return zig_clz_u64(zig_lo_u128(val), bits);
1846 if (zig_hi_u128(val) != 0) return zig_clz_u64(zig_hi_u128(val), bits - UINT8_C(64));
1847 return zig_clz_u64(zig_lo_u128(val), UINT8_C(64)) + (bits - UINT8_C(64));
17451848}
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) {
17481851 return zig_clz_u128(zig_bitcast_u128(val), bits);
17491852}
17501853
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);
1854static inline uint8_t zig_ctz_u128(zig_u128 val, uint8_t bits) {
1855 if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), UINT8_C(64));
1856 return zig_ctz_u64(zig_hi_u128(val), bits - UINT8_C(64)) + UINT8_C(64);
17541857}
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) {
17571860 return zig_ctz_u128(zig_bitcast_u128(val), bits);
17581861}
17591862
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));
1863static inline uint8_t zig_popcount_u128(zig_u128 val, uint8_t bits) {
1864 return zig_popcount_u64(zig_hi_u128(val), bits - UINT8_C(64)) +
1865 zig_popcount_u64(zig_lo_u128(val), UINT8_C(64));
17631866}
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) {
17661869 return zig_popcount_u128(zig_bitcast_u128(val), bits);
17671870}
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) {
17701873 zig_u128 full_res;
17711874#if zig_has_builtin(bswap128)
17721875 full_res = __builtin_bswap128(val);
17731876#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)));
1877 full_res = zig_make_u128(zig_byte_swap_u64(zig_lo_u128(val), UINT8_C(64)),
1878 zig_byte_swap_u64(zig_hi_u128(val), UINT8_C(64)));
17761879#endif
1777 return zig_shr_u128(full_res, zig_as_u8(128) - bits);
1880 return zig_shr_u128(full_res, UINT8_C(128) - bits);
17781881}
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) {
17811884 return zig_bitcast_i128(zig_byte_swap_u128(zig_bitcast_u128(val), bits));
17821885}
17831886
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);
1887static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, uint8_t bits) {
1888 return zig_shr_u128(zig_make_u128(zig_bit_reverse_u64(zig_lo_u128(val), UINT8_C(64)),
1889 zig_bit_reverse_u64(zig_hi_u128(val), UINT8_C(64))),
1890 UINT8_C(128) - bits);
17881891}
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) {
17911894 return zig_bitcast_i128(zig_bit_reverse_u128(zig_bitcast_u128(val), bits));
17921895}
17931896
......@@ -1810,85 +1913,85 @@ static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) {
18101913
18111914#if (zig_has_builtin(nan) && zig_has_builtin(nans) && zig_has_builtin(inf)) || defined(zig_gnuc)
18121915#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)
1916#define zig_make_special_f16(sign, name, arg, repr) sign zig_make_f16(__builtin_##name, )(arg)
1917#define zig_make_special_f32(sign, name, arg, repr) sign zig_make_f32(__builtin_##name, )(arg)
1918#define zig_make_special_f64(sign, name, arg, repr) sign zig_make_f64(__builtin_##name, )(arg)
1919#define zig_make_special_f80(sign, name, arg, repr) sign zig_make_f80(__builtin_##name, )(arg)
1920#define zig_make_special_f128(sign, name, arg, repr) sign zig_make_f128(__builtin_##name, )(arg)
1921#define zig_make_special_c_longdouble(sign, name, arg, repr) sign zig_make_c_longdouble(__builtin_##name, )(arg)
18191922#else
18201923#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)
1924#define zig_make_special_f16(sign, name, arg, repr) zig_float_from_repr_f16(repr)
1925#define zig_make_special_f32(sign, name, arg, repr) zig_float_from_repr_f32(repr)
1926#define zig_make_special_f64(sign, name, arg, repr) zig_float_from_repr_f64(repr)
1927#define zig_make_special_f80(sign, name, arg, repr) zig_float_from_repr_f80(repr)
1928#define zig_make_special_f128(sign, name, arg, repr) zig_float_from_repr_f128(repr)
1929#define zig_make_special_c_longdouble(sign, name, arg, repr) zig_float_from_repr_c_longdouble(repr)
18271930#endif
18281931
18291932#define zig_has_f16 1
18301933#define zig_bitSizeOf_f16 16
18311934#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)
18331936#if FLT_MANT_DIG == 11
18341937typedef float zig_f16;
1835#define zig_as_f16(fp, repr) fp##f
1938#define zig_make_f16(fp, repr) fp##f
18361939#elif DBL_MANT_DIG == 11
18371940typedef double zig_f16;
1838#define zig_as_f16(fp, repr) fp
1941#define zig_make_f16(fp, repr) fp
18391942#elif LDBL_MANT_DIG == 11
18401943#define zig_bitSizeOf_c_longdouble 16
18411944typedef long double zig_f16;
1842#define zig_as_f16(fp, repr) fp##l
1945#define zig_make_f16(fp, repr) fp##l
18431946#elif FLT16_MANT_DIG == 11 && (zig_has_builtin(inff16) || defined(zig_gnuc))
18441947typedef _Float16 zig_f16;
1845#define zig_as_f16(fp, repr) fp##f16
1948#define zig_make_f16(fp, repr) fp##f16
18461949#elif defined(__SIZEOF_FP16__)
18471950typedef __fp16 zig_f16;
1848#define zig_as_f16(fp, repr) fp##f16
1951#define zig_make_f16(fp, repr) fp##f16
18491952#else
18501953#undef zig_has_f16
18511954#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
1955#define zig_bitSizeOf_repr_f16 16
1956typedef int16_t zig_f16;
1957#define zig_make_f16(fp, repr) repr
1958#undef zig_make_special_f16
1959#define zig_make_special_f16(sign, name, arg, repr) repr
1960#undef zig_make_special_constant_f16
1961#define zig_make_special_constant_f16(sign, name, arg, repr) repr
18591962#endif
18601963
18611964#define zig_has_f32 1
18621965#define zig_bitSizeOf_f32 32
18631966#define zig_libc_name_f32(name) name##f
18641967#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, )
18661969#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)
18681971#endif
18691972#if FLT_MANT_DIG == 24
18701973typedef float zig_f32;
1871#define zig_as_f32(fp, repr) fp##f
1974#define zig_make_f32(fp, repr) fp##f
18721975#elif DBL_MANT_DIG == 24
18731976typedef double zig_f32;
1874#define zig_as_f32(fp, repr) fp
1977#define zig_make_f32(fp, repr) fp
18751978#elif LDBL_MANT_DIG == 24
18761979#define zig_bitSizeOf_c_longdouble 32
18771980typedef long double zig_f32;
1878#define zig_as_f32(fp, repr) fp##l
1981#define zig_make_f32(fp, repr) fp##l
18791982#elif FLT32_MANT_DIG == 24
18801983typedef _Float32 zig_f32;
1881#define zig_as_f32(fp, repr) fp##f32
1984#define zig_make_f32(fp, repr) fp##f32
18821985#else
18831986#undef zig_has_f32
18841987#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
1988#define zig_bitSizeOf_repr_f32 32
1989typedef int32_t zig_f32;
1990#define zig_make_f32(fp, repr) repr
1991#undef zig_make_special_f32
1992#define zig_make_special_f32(sign, name, arg, repr) repr
1993#undef zig_make_special_constant_f32
1994#define zig_make_special_constant_f32(sign, name, arg, repr) repr
18921995#endif
18931996
18941997#define zig_has_f64 1
......@@ -1898,108 +2001,108 @@ typedef zig_i32 zig_f32;
18982001#ifdef ZIG_TARGET_ABI_MSVC
18992002#define zig_bitSizeOf_c_longdouble 64
19002003#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, )
19022005#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)
19042007#endif /* _MSC_VER */
19052008#if FLT_MANT_DIG == 53
19062009typedef float zig_f64;
1907#define zig_as_f64(fp, repr) fp##f
2010#define zig_make_f64(fp, repr) fp##f
19082011#elif DBL_MANT_DIG == 53
19092012typedef double zig_f64;
1910#define zig_as_f64(fp, repr) fp
2013#define zig_make_f64(fp, repr) fp
19112014#elif LDBL_MANT_DIG == 53
19122015#define zig_bitSizeOf_c_longdouble 64
19132016typedef long double zig_f64;
1914#define zig_as_f64(fp, repr) fp##l
2017#define zig_make_f64(fp, repr) fp##l
19152018#elif FLT64_MANT_DIG == 53
19162019typedef _Float64 zig_f64;
1917#define zig_as_f64(fp, repr) fp##f64
2020#define zig_make_f64(fp, repr) fp##f64
19182021#elif FLT32X_MANT_DIG == 53
19192022typedef _Float32x zig_f64;
1920#define zig_as_f64(fp, repr) fp##f32x
2023#define zig_make_f64(fp, repr) fp##f32x
19212024#else
19222025#undef zig_has_f64
19232026#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
2027#define zig_bitSizeOf_repr_f64 64
2028typedef int64_t zig_f64;
2029#define zig_make_f64(fp, repr) repr
2030#undef zig_make_special_f64
2031#define zig_make_special_f64(sign, name, arg, repr) repr
2032#undef zig_make_special_constant_f64
2033#define zig_make_special_constant_f64(sign, name, arg, repr) repr
19312034#endif
19322035
19332036#define zig_has_f80 1
19342037#define zig_bitSizeOf_f80 80
19352038#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)
19372040#if FLT_MANT_DIG == 64
19382041typedef float zig_f80;
1939#define zig_as_f80(fp, repr) fp##f
2042#define zig_make_f80(fp, repr) fp##f
19402043#elif DBL_MANT_DIG == 64
19412044typedef double zig_f80;
1942#define zig_as_f80(fp, repr) fp
2045#define zig_make_f80(fp, repr) fp
19432046#elif LDBL_MANT_DIG == 64
19442047#define zig_bitSizeOf_c_longdouble 80
19452048typedef long double zig_f80;
1946#define zig_as_f80(fp, repr) fp##l
2049#define zig_make_f80(fp, repr) fp##l
19472050#elif FLT80_MANT_DIG == 64
19482051typedef _Float80 zig_f80;
1949#define zig_as_f80(fp, repr) fp##f80
2052#define zig_make_f80(fp, repr) fp##f80
19502053#elif FLT64X_MANT_DIG == 64
19512054typedef _Float64x zig_f80;
1952#define zig_as_f80(fp, repr) fp##f64x
2055#define zig_make_f80(fp, repr) fp##f64x
19532056#elif defined(__SIZEOF_FLOAT80__)
19542057typedef __float80 zig_f80;
1955#define zig_as_f80(fp, repr) fp##l
2058#define zig_make_f80(fp, repr) fp##l
19562059#else
19572060#undef zig_has_f80
19582061#define zig_has_f80 0
1959#define zig_repr_f80 i128
2062#define zig_bitSizeOf_repr_f80 128
19602063typedef 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
2064#define zig_make_f80(fp, repr) repr
2065#undef zig_make_special_f80
2066#define zig_make_special_f80(sign, name, arg, repr) repr
2067#undef zig_make_special_constant_f80
2068#define zig_make_special_constant_f80(sign, name, arg, repr) repr
19662069#endif
19672070
19682071#define zig_has_f128 1
19692072#define zig_bitSizeOf_f128 128
19702073#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)
19722075#if FLT_MANT_DIG == 113
19732076typedef float zig_f128;
1974#define zig_as_f128(fp, repr) fp##f
2077#define zig_make_f128(fp, repr) fp##f
19752078#elif DBL_MANT_DIG == 113
19762079typedef double zig_f128;
1977#define zig_as_f128(fp, repr) fp
2080#define zig_make_f128(fp, repr) fp
19782081#elif LDBL_MANT_DIG == 113
19792082#define zig_bitSizeOf_c_longdouble 128
19802083typedef long double zig_f128;
1981#define zig_as_f128(fp, repr) fp##l
2084#define zig_make_f128(fp, repr) fp##l
19822085#elif FLT128_MANT_DIG == 113
19832086typedef _Float128 zig_f128;
1984#define zig_as_f128(fp, repr) fp##f128
2087#define zig_make_f128(fp, repr) fp##f128
19852088#elif FLT64X_MANT_DIG == 113
19862089typedef _Float64x zig_f128;
1987#define zig_as_f128(fp, repr) fp##f64x
2090#define zig_make_f128(fp, repr) fp##f64x
19882091#elif defined(__SIZEOF_FLOAT128__)
19892092typedef __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)
2093#define zig_make_f128(fp, repr) fp##q
2094#undef zig_make_special_f128
2095#define zig_make_special_f128(sign, name, arg, repr) sign __builtin_##name##f128(arg)
19932096#else
19942097#undef zig_has_f128
19952098#define zig_has_f128 0
1996#define zig_repr_f128 i128
2099#define zig_bitSizeOf_repr_f128 128
19972100typedef 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
2101#define zig_make_f128(fp, repr) repr
2102#undef zig_make_special_f128
2103#define zig_make_special_f128(sign, name, arg, repr) repr
2104#undef zig_make_special_constant_f128
2105#define zig_make_special_constant_f128(sign, name, arg, repr) repr
20032106#endif
20042107
20052108#define zig_has_c_longdouble 1
......@@ -2010,17 +2113,17 @@ typedef zig_i128 zig_f128;
20102113#define zig_libc_name_c_longdouble(name) name##l
20112114#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)
20142117#ifdef zig_bitSizeOf_c_longdouble
20152118
20162119#ifdef ZIG_TARGET_ABI_MSVC
20172120typedef double zig_c_longdouble;
20182121#undef zig_bitSizeOf_c_longdouble
20192122#define zig_bitSizeOf_c_longdouble 64
2020#define zig_as_c_longdouble(fp, repr) fp
2123#define zig_make_c_longdouble(fp, repr) fp
20212124#else
20222125typedef long double zig_c_longdouble;
2023#define zig_as_c_longdouble(fp, repr) fp##l
2126#define zig_make_c_longdouble(fp, repr) fp##l
20242127#endif
20252128
20262129#else /* zig_bitSizeOf_c_longdouble */
......@@ -2029,13 +2132,13 @@ typedef long double zig_c_longdouble;
20292132#define zig_has_c_longdouble 0
20302133#define zig_bitSizeOf_c_longdouble 80
20312134#define zig_compiler_rt_abbrev_c_longdouble zig_compiler_rt_abbrev_f80
2032#define zig_repr_c_longdouble i128
2135#define zig_bitSizeOf_repr_c_longdouble 128
20332136typedef 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
2137#define zig_make_c_longdouble(fp, repr) repr
2138#undef zig_make_special_c_longdouble
2139#define zig_make_special_c_longdouble(sign, name, arg, repr) repr
2140#undef zig_make_special_constant_c_longdouble
2141#define zig_make_special_constant_c_longdouble(sign, name, arg, repr) repr
20392142
20402143#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
20732176#endif
20742177
20752178#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)
2179 zig_extern ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \
2180 zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(ArgType);
2181zig_convert_builtin(zig_f16, trunc, zig_f32, 2)
2182zig_convert_builtin(zig_f16, trunc, zig_f64, 2)
2183zig_convert_builtin(zig_f16, trunc, zig_f80, 2)
2184zig_convert_builtin(zig_f16, trunc, zig_f128, 2)
2185zig_convert_builtin(zig_f32, extend, zig_f16, 2)
2186zig_convert_builtin(zig_f32, trunc, zig_f64, 2)
2187zig_convert_builtin(zig_f32, trunc, zig_f80, 2)
2188zig_convert_builtin(zig_f32, trunc, zig_f128, 2)
2189zig_convert_builtin(zig_f64, extend, zig_f16, 2)
2190zig_convert_builtin(zig_f64, extend, zig_f32, 2)
2191zig_convert_builtin(zig_f64, trunc, zig_f80, 2)
2192zig_convert_builtin(zig_f64, trunc, zig_f128, 2)
2193zig_convert_builtin(zig_f80, extend, zig_f16, 2)
2194zig_convert_builtin(zig_f80, extend, zig_f32, 2)
2195zig_convert_builtin(zig_f80, extend, zig_f64, 2)
2196zig_convert_builtin(zig_f80, trunc, zig_f128, 2)
2197zig_convert_builtin(zig_f128, extend, zig_f16, 2)
2198zig_convert_builtin(zig_f128, extend, zig_f32, 2)
2199zig_convert_builtin(zig_f128, extend, zig_f64, 2)
2200zig_convert_builtin(zig_f128, extend, zig_f80, 2)
20982201
20992202#define zig_float_negate_builtin_0(Type) \
21002203 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 ); \
21022208 }
21032209#define zig_float_negate_builtin_1(Type) \
21042210 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
......@@ -2106,28 +2212,28 @@ zig_convert_builtin(f128, extend, f80, 2)
21062212 }
21072213
21082214#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); \
2215 zig_extern int32_t zig_expand_concat(zig_expand_concat(__##operation, \
2216 zig_compiler_rt_abbrev_zig_##Type), 2)(zig_##Type, zig_##Type); \
2217 static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
2218 return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_zig_##Type), 2)(lhs, rhs); \
21132219 }
21142220#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) { \
21162222 return (!(lhs <= rhs) - (lhs < rhs)); \
21172223 }
21182224
21192225#define zig_float_greater_builtin_0(Type, operation) \
21202226 zig_float_less_builtin_0(Type, operation)
21212227#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) { \
21232229 return ((lhs > rhs) - !(lhs >= rhs)); \
21242230 }
21252231
21262232#define zig_float_binary_builtin_0(Type, operation, operator) \
21272233 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); \
21292235 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); \
21312237 }
21322238#define zig_float_binary_builtin_1(Type, operation, operator) \
21332239 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
......@@ -2135,18 +2241,18 @@ zig_convert_builtin(f128, extend, f80, 2)
21352241 }
21362242
21372243#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, ) \
2244 zig_convert_builtin( int32_t, fix, zig_##Type, ) \
2245 zig_convert_builtin(uint32_t, fixuns, zig_##Type, ) \
2246 zig_convert_builtin( int64_t, fix, zig_##Type, ) \
2247 zig_convert_builtin(uint64_t, fixuns, zig_##Type, ) \
2248 zig_convert_builtin(zig_i128, fix, zig_##Type, ) \
2249 zig_convert_builtin(zig_u128, fixuns, zig_##Type, ) \
2250 zig_convert_builtin(zig_##Type, float, int32_t, ) \
2251 zig_convert_builtin(zig_##Type, floatun, uint32_t, ) \
2252 zig_convert_builtin(zig_##Type, float, int64_t, ) \
2253 zig_convert_builtin(zig_##Type, floatun, uint64_t, ) \
2254 zig_convert_builtin(zig_##Type, float, zig_i128, ) \
2255 zig_convert_builtin(zig_##Type, floatun, zig_u128, ) \
21502256 zig_expand_concat(zig_float_negate_builtin_, zig_has_##Type)(Type) \
21512257 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, cmp) \
21522258 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, ne) \
......@@ -2332,17 +2438,17 @@ zig_msvc_flt_atomics(f64, u64, 64)
23322438
23332439#if _M_IX86
23342440static inline void zig_msvc_atomic_barrier() {
2335 zig_i32 barrier;
2441 int32_t barrier;
23362442 __asm {
23372443 xchg barrier, eax
23382444 }
23392445}
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) {
23422448 return _InterlockedExchangePointer(obj, arg);
23432449}
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) {
23462452 _InterlockedExchangePointer(obj, arg);
23472453}
23482454
......@@ -2360,11 +2466,11 @@ static inline bool zig_msvc_cmpxchg_p32(void** obj, void** expected, void* desir
23602466 return exchanged;
23612467}
23622468#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) {
23642470 return _InterlockedExchangePointer(obj, arg);
23652471}
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) {
23682474 _InterlockedExchangePointer(obj, arg);
23692475}
23702476
......@@ -2383,11 +2489,11 @@ static inline bool zig_msvc_cmpxchg_p64(void** obj, void** expected, void* desir
23832489}
23842490
23852491static 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);
23872493}
23882494
23892495static 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);
23912497}
23922498
23932499#define zig_msvc_atomics_128xchg(Type) \
......@@ -2429,7 +2535,7 @@ zig_msvc_atomics_128op(u128, max)
24292535
24302536#endif /* _MSC_VER && (_M_IX86 || _M_X64) */
24312537
2432/* ========================= Special Case Intrinsics ========================= */
2538/* ======================== Special Case Intrinsics ========================= */
24332539
24342540#if (_MSC_VER && _M_X64) || defined(__x86_64__)
24352541
......@@ -2459,8 +2565,8 @@ static inline void* zig_x86_windows_teb(void) {
24592565
24602566#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) {
2463 zig_u32 cpu_info[4];
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) {
2569 uint32_t cpu_info[4];
24642570#if _MSC_VER
24652571 __cpuidex(cpu_info, leaf_id, subid);
24662572#else
......@@ -2472,12 +2578,12 @@ static inline void zig_x86_cpuid(zig_u32 leaf_id, zig_u32 subid, zig_u32* eax, z
24722578 *edx = cpu_info[3];
24732579}
24742580
2475static inline zig_u32 zig_x86_get_xcr0(void) {
2581static inline uint32_t zig_x86_get_xcr0(void) {
24762582#if _MSC_VER
2477 return (zig_u32)_xgetbv(0);
2583 return (uint32_t)_xgetbv(0);
24782584#else
2479 zig_u32 eax;
2480 zig_u32 edx;
2585 uint32_t eax;
2586 uint32_t edx;
24812587 __asm__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0));
24822588 return eax;
24832589#endif
src/codegen/c.zig+39-30
......@@ -752,7 +752,7 @@ pub const DeclGen = struct {
752752
753753 try writer.writeAll("zig_cast_");
754754 try dg.renderTypeForBuiltinFnName(writer, ty);
755 try writer.writeAll(" zig_as_");
755 try writer.writeAll(" zig_make_");
756756 try dg.renderTypeForBuiltinFnName(writer, ty);
757757 try writer.writeByte('(');
758758 switch (bits) {
......@@ -962,7 +962,7 @@ pub const DeclGen = struct {
962962 try writer.writeByte(' ');
963963 var empty = true;
964964 if (std.math.isFinite(f128_val)) {
965 try writer.writeAll("zig_as_");
965 try writer.writeAll("zig_make_");
966966 try dg.renderTypeForBuiltinFnName(writer, ty);
967967 try writer.writeByte('(');
968968 switch (bits) {
......@@ -997,7 +997,7 @@ pub const DeclGen = struct {
997997 // return dg.fail("Only quiet nans are supported in global variable initializers", .{});
998998 }
999999
1000 try writer.writeAll("zig_as_special_");
1000 try writer.writeAll("zig_make_special_");
10011001 if (location == .StaticInitializer) try writer.writeAll("constant_");
10021002 try dg.renderTypeForBuiltinFnName(writer, ty);
10031003 try writer.writeByte('(');
......@@ -2016,14 +2016,16 @@ pub const DeclGen = struct {
20162016 .bool,
20172017 .size_t,
20182018 .ptrdiff_t,
2019 .zig_u8,
2020 .zig_i8,
2021 .zig_u16,
2022 .zig_i16,
2023 .zig_u32,
2024 .zig_i32,
2025 .zig_u64,
2026 .zig_i64,
2019 .uint8_t,
2020 .int8_t,
2021 .uint16_t,
2022 .int16_t,
2023 .uint32_t,
2024 .int32_t,
2025 .uint64_t,
2026 .int64_t,
2027 .uintptr_t,
2028 .intptr_t,
20272029 .zig_u128,
20282030 .zig_i128,
20292031 .zig_f16,
......@@ -2158,14 +2160,16 @@ pub const DeclGen = struct {
21582160 .bool,
21592161 .size_t,
21602162 .ptrdiff_t,
2161 .zig_u8,
2162 .zig_i8,
2163 .zig_u16,
2164 .zig_i16,
2165 .zig_u32,
2166 .zig_i32,
2167 .zig_u64,
2168 .zig_i64,
2163 .uint8_t,
2164 .int8_t,
2165 .uint16_t,
2166 .int16_t,
2167 .uint32_t,
2168 .int32_t,
2169 .uint64_t,
2170 .int64_t,
2171 .uintptr_t,
2172 .intptr_t,
21692173 .zig_u128,
21702174 .zig_i128,
21712175 .zig_f16,
......@@ -2285,16 +2289,16 @@ pub const DeclGen = struct {
22852289 /// Renders a cast to an int type, from either an int or a pointer.
22862290 ///
22872291 /// 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.
22892293 ///
22902294 /// | Dest type bits | Src type | Result
22912295 /// |------------------|------------------|---------------------------|
22922296 /// | < 64 bit integer | pointer | (zig_<dest_ty>)(zig_<u|i>size)src
22932297 /// | < 64 bit integer | < 64 bit integer | (zig_<dest_ty>)src
22942298 /// | < 64 bit integer | > 64 bit integer | zig_lo(src)
2295 /// | > 64 bit integer | pointer | zig_as_<dest_ty>(0, (zig_<u|i>size)src)
2296 /// | > 64 bit integer | < 64 bit integer | zig_as_<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))
2299 /// | > 64 bit integer | pointer | zig_make_<dest_ty>(0, (zig_<u|i>size)src)
2300 /// | > 64 bit integer | < 64 bit integer | zig_make_<dest_ty>(0, src)
2301 /// | > 64 bit integer | > 64 bit integer | zig_make_<dest_ty>(zig_hi_<src_ty>(src), zig_lo_<src_ty>(src))
22982302 fn renderIntCast(dg: *DeclGen, w: anytype, dest_ty: Type, context: IntCastContext, src_ty: Type, location: ValueRenderLocation) !void {
22992303 const target = dg.module.getTarget();
23002304 const dest_bits = dest_ty.bitSize(target);
......@@ -2332,7 +2336,7 @@ pub const DeclGen = struct {
23322336 try context.writeValue(dg, w, src_ty, .FunctionArgument);
23332337 try w.writeByte(')');
23342338 } else if (dest_bits > 64 and src_bits <= 64) {
2335 try w.writeAll("zig_as_");
2339 try w.writeAll("zig_make_");
23362340 try dg.renderTypeForBuiltinFnName(w, dest_ty);
23372341 try w.writeAll("(0, "); // TODO: Should the 0 go through fmtIntLiteral?
23382342 if (src_is_ptr) {
......@@ -2344,7 +2348,7 @@ pub const DeclGen = struct {
23442348 try w.writeByte(')');
23452349 } else {
23462350 assert(!src_is_ptr);
2347 try w.writeAll("zig_as_");
2351 try w.writeAll("zig_make_");
23482352 try dg.renderTypeForBuiltinFnName(w, dest_ty);
23492353 try w.writeAll("(zig_hi_");
23502354 try dg.renderTypeForBuiltinFnName(w, src_eff_ty);
......@@ -3858,7 +3862,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue {
38583862 const cant_cast = host_ty.isInt() and host_ty.bitSize(target) > 64;
38593863 if (cant_cast) {
38603864 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_");
38623866 try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty);
38633867 try writer.writeAll("(0, ");
38643868 } else {
......@@ -7355,7 +7359,7 @@ fn formatIntLiteral(
73557359 use_twos_comp = true;
73567360 } else {
73577361 // 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 });
73597363 }
73607364 } else {
73617365 try writer.writeByte('-');
......@@ -7365,11 +7369,16 @@ fn formatIntLiteral(
73657369 switch (data.ty.tag()) {
73667370 .c_short, .c_ushort, .c_int, .c_uint, .c_long, .c_ulong, .c_longlong, .c_ulonglong => {},
73677371 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) {
73697378 // 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 });
73717380 } 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 });
73737382 }
73747383 },
73757384 }
src/codegen/c/type.zig+66-53
......@@ -77,19 +77,24 @@ pub const CType = extern union {
7777 @"long double",
7878
7979 // C header types
80 bool, // stdbool.h
81 size_t, // stddef.h
82 ptrdiff_t, // stddef.h
80 // - stdbool.h
81 bool,
82 // - 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
8497 // 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,
9398 zig_u128,
9499 zig_i128,
95100 zig_f16,
......@@ -149,14 +154,16 @@ pub const CType = extern union {
149154 .bool,
150155 .size_t,
151156 .ptrdiff_t,
152 .zig_u8,
153 .zig_i8,
154 .zig_u16,
155 .zig_i16,
156 .zig_u32,
157 .zig_i32,
158 .zig_u64,
159 .zig_i64,
157 .uint8_t,
158 .int8_t,
159 .uint16_t,
160 .int16_t,
161 .uint32_t,
162 .int32_t,
163 .uint64_t,
164 .int64_t,
165 .uintptr_t,
166 .intptr_t,
160167 .zig_u128,
161168 .zig_i128,
162169 .zig_f16,
......@@ -428,14 +435,16 @@ pub const CType = extern union {
428435 .bool,
429436 .size_t,
430437 .ptrdiff_t,
431 .zig_u8,
432 .zig_i8,
433 .zig_u16,
434 .zig_i16,
435 .zig_u32,
436 .zig_i32,
437 .zig_u64,
438 .zig_i64,
438 .uint8_t,
439 .int8_t,
440 .uint16_t,
441 .int16_t,
442 .uint32_t,
443 .int32_t,
444 .uint64_t,
445 .int64_t,
446 .uintptr_t,
447 .intptr_t,
439448 .zig_u128,
440449 .zig_i128,
441450 .zig_f16,
......@@ -526,14 +535,16 @@ pub const CType = extern union {
526535 .bool,
527536 .size_t,
528537 .ptrdiff_t,
529 .zig_u8,
530 .zig_i8,
531 .zig_u16,
532 .zig_i16,
533 .zig_u32,
534 .zig_i32,
535 .zig_u64,
536 .zig_i64,
538 .uint8_t,
539 .int8_t,
540 .uint16_t,
541 .int16_t,
542 .uint32_t,
543 .int32_t,
544 .uint64_t,
545 .int64_t,
546 .uintptr_t,
547 .intptr_t,
537548 .zig_u128,
538549 .zig_i128,
539550 .zig_f16,
......@@ -628,20 +639,20 @@ pub const CType = extern union {
628639 return switch (bits) {
629640 0 => .void,
630641 1...8 => switch (signedness) {
631 .unsigned => .zig_u8,
632 .signed => .zig_i8,
642 .unsigned => .uint8_t,
643 .signed => .int8_t,
633644 },
634645 9...16 => switch (signedness) {
635 .unsigned => .zig_u16,
636 .signed => .zig_i16,
646 .unsigned => .uint16_t,
647 .signed => .int16_t,
637648 },
638649 17...32 => switch (signedness) {
639 .unsigned => .zig_u32,
640 .signed => .zig_i32,
650 .unsigned => .uint32_t,
651 .signed => .int32_t,
641652 },
642653 33...64 => switch (signedness) {
643 .unsigned => .zig_u64,
644 .signed => .zig_i64,
654 .unsigned => .uint64_t,
655 .signed => .int64_t,
645656 },
646657 65...128 => switch (signedness) {
647658 .unsigned => .zig_u128,
......@@ -712,8 +723,8 @@ pub const CType = extern union {
712723 if (!ty.isFnOrHasRuntimeBitsIgnoreComptime())
713724 self.init(.void)
714725 else if (ty.isAbiInt()) switch (ty.tag()) {
715 .usize => self.init(.size_t),
716 .isize => self.init(.ptrdiff_t),
726 .usize => self.init(.uintptr_t),
727 .isize => self.init(.intptr_t),
717728 .c_short => self.init(.short),
718729 .c_ushort => self.init(.@"unsigned short"),
719730 .c_int => self.init(.int),
......@@ -996,14 +1007,16 @@ pub const CType = extern union {
9961007 .bool,
9971008 .size_t,
9981009 .ptrdiff_t,
999 .zig_u8,
1000 .zig_i8,
1001 .zig_u16,
1002 .zig_i16,
1003 .zig_u32,
1004 .zig_i32,
1005 .zig_u64,
1006 .zig_i64,
1010 .uint8_t,
1011 .int8_t,
1012 .uint16_t,
1013 .int16_t,
1014 .uint32_t,
1015 .int32_t,
1016 .uint64_t,
1017 .int64_t,
1018 .uintptr_t,
1019 .intptr_t,
10071020 .zig_u128,
10081021 .zig_i128,
10091022 .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