authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-11-12 18:11:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-11-13 16:50:16-07:00
log77e7d97725a07bc3eaa7e9c7f9ffd7698f357f1a
tree5f5e0b8d7e318c83f9e0b3cafc4b24ecfb9f251d
parent0184c8d86f614a216641039e06e97f08d439fff7

C backend: improve ergonomics of zig.h a little bit

Partially implements #13528. Enough to unblock the wasi-bootstrap branch.

5 files changed, 1685 insertions(+), 1678 deletions(-)

lib/include/zig.h deleted-1677
...@@ -1,1677 +0,0 @@
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 defined(__has_builtin)
10#define zig_has_builtin(builtin) __has_builtin(__builtin_##builtin)
11#else
12#define zig_has_builtin(builtin) 0
13#endif
14
15#if defined(__has_attribute)
16#define zig_has_attribute(attribute) __has_attribute(attribute)
17#else
18#define zig_has_attribute(attribute) 0
19#endif
20
21#if __STDC_VERSION__ >= 201112L
22#define zig_threadlocal _Thread_local
23#elif defined(__GNUC__)
24#define zig_threadlocal __thread
25#elif _MSC_VER
26#define zig_threadlocal __declspec(thread)
27#else
28#define zig_threadlocal zig_threadlocal_unavailable
29#endif
30
31#if zig_has_attribute(naked) || defined(__GNUC__)
32#define zig_naked __attribute__((naked))
33#elif defined(_MSC_VER)
34#define zig_naked __declspec(naked)
35#else
36#define zig_naked zig_naked_unavailable
37#endif
38
39#if zig_has_attribute(cold)
40#define zig_cold __attribute__((cold))
41#else
42#define zig_cold
43#endif
44
45#if __STDC_VERSION__ >= 199901L
46#define zig_restrict restrict
47#elif defined(__GNUC__)
48#define zig_restrict __restrict
49#else
50#define zig_restrict
51#endif
52
53#if __STDC_VERSION__ >= 201112L
54#define zig_align(alignment) _Alignas(alignment)
55#elif zig_has_attribute(aligned)
56#define zig_align(alignment) __attribute__((aligned(alignment)))
57#elif _MSC_VER
58#else
59#error the C compiler being used does not support aligning variables
60#endif
61
62#if zig_has_builtin(unreachable)
63#define zig_unreachable() __builtin_unreachable()
64#else
65#define zig_unreachable()
66#endif
67
68#if defined(__cplusplus)
69#define zig_extern extern "C"
70#else
71#define zig_extern extern
72#endif
73
74#if zig_has_builtin(debugtrap)
75#define zig_breakpoint() __builtin_debugtrap()
76#elif zig_has_builtin(trap)
77#define zig_breakpoint() __builtin_trap()
78#elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
79#define zig_breakpoint() __debugbreak()
80#elif defined(__i386__) || defined(__x86_64__)
81#define zig_breakpoint() __asm__ volatile("int $0x03");
82#else
83#define zig_breakpoint() raise(SIGTRAP)
84#endif
85
86#if zig_has_builtin(return_address)
87#define zig_return_address() __builtin_extract_return_addr(__builtin_return_address(0))
88#elif defined(_MSC_VER)
89#define zig_return_address() _ReturnAddress()
90#else
91#define zig_return_address() 0
92#endif
93
94#if zig_has_builtin(frame_address)
95#define zig_frame_address() __builtin_frame_address(0)
96#else
97#define zig_frame_address() 0
98#endif
99
100#if zig_has_builtin(prefetch)
101#define zig_prefetch(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
102#else
103#define zig_prefetch(addr, rw, locality)
104#endif
105
106#if zig_has_builtin(memory_size) && zig_has_builtin(memory_grow)
107#define zig_wasm_memory_size(index) __builtin_wasm_memory_size(index)
108#define zig_wasm_memory_grow(index, delta) __builtin_wasm_memory_grow(index, delta)
109#else
110#define zig_wasm_memory_size(index) zig_unimplemented()
111#define zig_wasm_memory_grow(index, delta) zig_unimplemented()
112#endif
113
114#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
115#include <stdatomic.h>
116#define zig_atomic(type) _Atomic(type)
117#define zig_cmpxchg_strong(obj, expected, desired, succ, fail) atomic_compare_exchange_strong_explicit(obj, &(expected), desired, succ, fail)
118#define zig_cmpxchg_weak(obj, expected, desired, succ, fail) atomic_compare_exchange_weak_explicit (obj, &(expected), desired, succ, fail)
119#define zig_atomicrmw_xchg(obj, arg, order) atomic_exchange_explicit (obj, arg, order)
120#define zig_atomicrmw_add(obj, arg, order) atomic_fetch_add_explicit (obj, arg, order)
121#define zig_atomicrmw_sub(obj, arg, order) atomic_fetch_sub_explicit (obj, arg, order)
122#define zig_atomicrmw_or(obj, arg, order) atomic_fetch_or_explicit (obj, arg, order)
123#define zig_atomicrmw_xor(obj, arg, order) atomic_fetch_xor_explicit (obj, arg, order)
124#define zig_atomicrmw_and(obj, arg, order) atomic_fetch_and_explicit (obj, arg, order)
125#define zig_atomicrmw_nand(obj, arg, order) __atomic_fetch_nand (obj, arg, order)
126#define zig_atomicrmw_min(obj, arg, order) __atomic_fetch_min (obj, arg, order)
127#define zig_atomicrmw_max(obj, arg, order) __atomic_fetch_max (obj, arg, order)
128#define zig_atomic_store(obj, arg, order) atomic_store_explicit (obj, arg, order)
129#define zig_atomic_load(obj, order) atomic_load_explicit (obj, order)
130#define zig_fence(order) atomic_thread_fence(order)
131#elif defined(__GNUC__)
132#define memory_order_relaxed __ATOMIC_RELAXED
133#define memory_order_consume __ATOMIC_CONSUME
134#define memory_order_acquire __ATOMIC_ACQUIRE
135#define memory_order_release __ATOMIC_RELEASE
136#define memory_order_acq_rel __ATOMIC_ACQ_REL
137#define memory_order_seq_cst __ATOMIC_SEQ_CST
138#define zig_atomic(type) type
139#define zig_cmpxchg_strong(obj, expected, desired, succ, fail) __atomic_compare_exchange_n(obj, &(expected), desired, zig_false, succ, fail)
140#define zig_cmpxchg_weak(obj, expected, desired, succ, fail) __atomic_compare_exchange_n(obj, &(expected), desired, zig_true , succ, fail)
141#define zig_atomicrmw_xchg(obj, arg, order) __atomic_exchange_n(obj, arg, order)
142#define zig_atomicrmw_add(obj, arg, order) __atomic_fetch_add (obj, arg, order)
143#define zig_atomicrmw_sub(obj, arg, order) __atomic_fetch_sub (obj, arg, order)
144#define zig_atomicrmw_or(obj, arg, order) __atomic_fetch_or (obj, arg, order)
145#define zig_atomicrmw_xor(obj, arg, order) __atomic_fetch_xor (obj, arg, order)
146#define zig_atomicrmw_and(obj, arg, order) __atomic_fetch_and (obj, arg, order)
147#define zig_atomicrmw_nand(obj, arg, order) __atomic_fetch_nand(obj, arg, order)
148#define zig_atomicrmw_min(obj, arg, order) __atomic_fetch_min (obj, arg, order)
149#define zig_atomicrmw_max(obj, arg, order) __atomic_fetch_max (obj, arg, order)
150#define zig_atomic_store(obj, arg, order) __atomic_store_n (obj, arg, order)
151#define zig_atomic_load(obj, order) __atomic_load_n (obj, order)
152#define zig_fence(order) __atomic_thread_fence(order)
153#else
154#define memory_order_relaxed 0
155#define memory_order_consume 1
156#define memory_order_acquire 2
157#define memory_order_release 3
158#define memory_order_acq_rel 4
159#define memory_order_seq_cst 5
160#define zig_atomic(type) type
161#define zig_cmpxchg_strong(obj, expected, desired, succ, fail) zig_unimplemented()
162#define zig_cmpxchg_weak(obj, expected, desired, succ, fail) zig_unimplemented()
163#define zig_atomicrmw_xchg(obj, arg, order) zig_unimplemented()
164#define zig_atomicrmw_add(obj, arg, order) zig_unimplemented()
165#define zig_atomicrmw_sub(obj, arg, order) zig_unimplemented()
166#define zig_atomicrmw_or(obj, arg, order) zig_unimplemented()
167#define zig_atomicrmw_xor(obj, arg, order) zig_unimplemented()
168#define zig_atomicrmw_and(obj, arg, order) zig_unimplemented()
169#define zig_atomicrmw_nand(obj, arg, order) zig_unimplemented()
170#define zig_atomicrmw_min(obj, arg, order) zig_unimplemented()
171#define zig_atomicrmw_max(obj, arg, order) zig_unimplemented()
172#define zig_atomic_store(obj, arg, order) zig_unimplemented()
173#define zig_atomic_load(obj, order) zig_unimplemented()
174#define zig_fence(order) zig_unimplemented()
175#endif
176
177#if __STDC_VERSION__ >= 201112L
178#define zig_noreturn _Noreturn void
179#elif zig_has_attribute(noreturn) || defined(__GNUC__)
180#define zig_noreturn __attribute__((noreturn)) void
181#elif _MSC_VER
182#define zig_noreturn __declspec(noreturn) void
183#else
184#define zig_noreturn void
185#endif
186
187#define zig_concat(lhs, rhs) lhs##rhs
188#define zig_expand_concat(lhs, rhs) zig_concat(lhs, rhs)
189
190#define zig_bitSizeOf(T) (CHAR_BIT * sizeof(T))
191
192typedef void zig_void;
193
194#if defined(__cplusplus)
195typedef bool zig_bool;
196#define zig_false false
197#define zig_true true
198#else
199#if __STDC_VERSION__ >= 199901L
200typedef _Bool zig_bool;
201#else
202typedef char zig_bool;
203#endif
204#define zig_false ((zig_bool)0)
205#define zig_true ((zig_bool)1)
206#endif
207
208typedef uintptr_t zig_usize;
209typedef intptr_t zig_isize;
210typedef signed short int zig_c_short;
211typedef unsigned short int zig_c_ushort;
212typedef signed int zig_c_int;
213typedef unsigned int zig_c_uint;
214typedef signed long int zig_c_long;
215typedef unsigned long int zig_c_ulong;
216typedef signed long long int zig_c_longlong;
217typedef unsigned long long int zig_c_ulonglong;
218
219typedef uint8_t zig_u8;
220typedef int8_t zig_i8;
221typedef uint16_t zig_u16;
222typedef int16_t zig_i16;
223typedef uint16_t zig_u16;
224typedef int16_t zig_i16;
225typedef uint32_t zig_u32;
226typedef int32_t zig_i32;
227typedef uint64_t zig_u64;
228typedef int64_t zig_i64;
229
230#define zig_as_u8(val) UINT8_C(val)
231#define zig_as_i8(val) INT8_C(val)
232#define zig_as_u16(val) UINT16_C(val)
233#define zig_as_i16(val) INT16_C(val)
234#define zig_as_u32(val) UINT32_C(val)
235#define zig_as_i32(val) INT32_C(val)
236#define zig_as_u64(val) UINT64_C(val)
237#define zig_as_i64(val) INT64_C(val)
238
239#define zig_minInt_u8 zig_as_u8(0)
240#define zig_maxInt_u8 UINT8_MAX
241#define zig_minInt_i8 INT8_MIN
242#define zig_maxInt_i8 INT8_MAX
243#define zig_minInt_u16 zig_as_u16(0)
244#define zig_maxInt_u16 UINT16_MAX
245#define zig_minInt_i16 INT16_MIN
246#define zig_maxInt_i16 INT16_MAX
247#define zig_minInt_u32 zig_as_u32(0)
248#define zig_maxInt_u32 UINT32_MAX
249#define zig_minInt_i32 INT32_MIN
250#define zig_maxInt_i32 INT32_MAX
251#define zig_minInt_u64 zig_as_u64(0)
252#define zig_maxInt_u64 UINT64_MAX
253#define zig_minInt_i64 INT64_MIN
254#define zig_maxInt_i64 INT64_MAX
255
256#define zig_compiler_rt_abbrev_u32 si
257#define zig_compiler_rt_abbrev_i32 si
258#define zig_compiler_rt_abbrev_u64 di
259#define zig_compiler_rt_abbrev_i64 di
260#define zig_compiler_rt_abbrev_u128 ti
261#define zig_compiler_rt_abbrev_i128 ti
262#define zig_compiler_rt_abbrev_f16 hf
263#define zig_compiler_rt_abbrev_f32 sf
264#define zig_compiler_rt_abbrev_f64 df
265#define zig_compiler_rt_abbrev_f80 xf
266#define zig_compiler_rt_abbrev_f128 tf
267
268zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, zig_usize);
269zig_extern void *memset (void *, int, zig_usize);
270
271/* ==================== 8/16/32/64-bit Integer Routines ===================== */
272
273#define zig_maxInt(Type, bits) zig_shr_##Type(zig_maxInt_##Type, (zig_bitSizeOf(zig_##Type) - bits))
274#define zig_expand_maxInt(Type, bits) zig_maxInt(Type, bits)
275#define zig_minInt(Type, bits) zig_not_##Type(zig_maxInt(Type, bits), bits)
276#define zig_expand_minInt(Type, bits) zig_minInt(Type, bits)
277
278#define zig_int_operator(Type, RhsType, operation, operator) \
279 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##RhsType rhs) { \
280 return lhs operator rhs; \
281 }
282#define zig_int_basic_operator(Type, operation, operator) \
283 zig_int_operator(Type, Type, operation, operator)
284#define zig_int_shift_operator(Type, operation, operator) \
285 zig_int_operator(Type, u8, operation, operator)
286#define zig_int_helpers(w) \
287 zig_int_basic_operator(u##w, and, &) \
288 zig_int_basic_operator(i##w, and, &) \
289 zig_int_basic_operator(u##w, or, |) \
290 zig_int_basic_operator(i##w, or, |) \
291 zig_int_basic_operator(u##w, xor, ^) \
292 zig_int_basic_operator(i##w, xor, ^) \
293 zig_int_shift_operator(u##w, shl, <<) \
294 zig_int_shift_operator(i##w, shl, <<) \
295 zig_int_shift_operator(u##w, shr, >>) \
296\
297 static inline zig_i##w zig_shr_i##w(zig_i##w lhs, zig_u8 rhs) { \
298 zig_i##w sign_mask = lhs < zig_as_i##w(0) ? -zig_as_i##w(1) : zig_as_i##w(0); \
299 return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \
300 } \
301\
302 static inline zig_u##w zig_not_u##w(zig_u##w val, zig_u8 bits) { \
303 return val ^ zig_maxInt(u##w, bits); \
304 } \
305\
306 static inline zig_i##w zig_not_i##w(zig_i##w val, zig_u8 bits) { \
307 (void)bits; \
308 return ~val; \
309 } \
310\
311 static inline zig_u##w zig_wrap_u##w(zig_u##w val, zig_u8 bits) { \
312 return val & zig_maxInt(u##w, bits); \
313 } \
314\
315 static inline zig_i##w zig_wrap_i##w(zig_i##w val, zig_u8 bits) { \
316 return (val & zig_as_u##w(1) << (bits - zig_as_u8(1))) != 0 \
317 ? val | zig_minInt(i##w, bits) : val & zig_maxInt(i##w, bits); \
318 } \
319\
320 zig_int_basic_operator(u##w, div_floor, /) \
321\
322 static inline zig_i##w zig_div_floor_i##w(zig_i##w lhs, zig_i##w rhs) { \
323 return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < zig_as_i##w(0)); \
324 } \
325\
326 zig_int_basic_operator(u##w, mod, %) \
327\
328 static inline zig_i##w zig_mod_i##w(zig_i##w lhs, zig_i##w rhs) { \
329 zig_i##w rem = lhs % rhs; \
330 return rem + (((lhs ^ rhs) & rem) < zig_as_i##w(0) ? rhs : zig_as_i##w(0)); \
331 }
332zig_int_helpers(8)
333zig_int_helpers(16)
334zig_int_helpers(32)
335zig_int_helpers(64)
336
337static inline zig_bool zig_addo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {
338#if zig_has_builtin(add_overflow)
339 zig_u32 full_res;
340 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
341 *res = zig_wrap_u32(full_res, bits);
342 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
343#else
344 *res = zig_addw_u32(lhs, rhs, bits);
345 return *res < lhs;
346#endif
347}
348
349zig_extern zig_i32 __addosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
350static inline zig_bool zig_addo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
351#if zig_has_builtin(add_overflow)
352 zig_i32 full_res;
353 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
354#else
355 zig_c_int overflow_int;
356 zig_u32 full_res = __addosi4(lhs, rhs, &overflow_int);
357 zig_bool overflow = overflow_int != 0;
358#endif
359 *res = zig_wrap_i32(full_res, bits);
360 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);
361}
362
363static inline zig_bool zig_addo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
364#if zig_has_builtin(add_overflow)
365 zig_u64 full_res;
366 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
367 *res = zig_wrap_u64(full_res, bits);
368 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
369#else
370 *res = zig_addw_u64(lhs, rhs, bits);
371 return *res < lhs;
372#endif
373}
374
375zig_extern zig_i64 __addodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
376static inline zig_bool zig_addo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
377#if zig_has_builtin(add_overflow)
378 zig_i64 full_res;
379 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
380#else
381 zig_c_int overflow_int;
382 zig_u64 full_res = __addodi4(lhs, rhs, &overflow_int);
383 zig_bool overflow = overflow_int != 0;
384#endif
385 *res = zig_wrap_i64(full_res, bits);
386 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
387}
388
389static inline zig_bool zig_addo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {
390#if zig_has_builtin(add_overflow)
391 zig_u8 full_res;
392 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
393 *res = zig_wrap_u8(full_res, bits);
394 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
395#else
396 return zig_addo_u32(res, lhs, rhs, bits);
397#endif
398}
399
400static inline zig_bool zig_addo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {
401#if zig_has_builtin(add_overflow)
402 zig_i8 full_res;
403 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
404 *res = zig_wrap_i8(full_res, bits);
405 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
406#else
407 return zig_addo_i32(res, lhs, rhs, bits);
408#endif
409}
410
411static inline zig_bool zig_addo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {
412#if zig_has_builtin(add_overflow)
413 zig_u16 full_res;
414 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
415 *res = zig_wrap_u16(full_res, bits);
416 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
417#else
418 return zig_addo_u32(res, lhs, rhs, bits);
419#endif
420}
421
422static inline zig_bool zig_addo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {
423#if zig_has_builtin(add_overflow)
424 zig_i16 full_res;
425 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
426 *res = zig_wrap_i16(full_res, bits);
427 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
428#else
429 return zig_addo_i32(res, lhs, rhs, bits);
430#endif
431}
432
433static inline zig_bool zig_subo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {
434#if zig_has_builtin(sub_overflow)
435 zig_u32 full_res;
436 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
437 *res = zig_wrap_u32(full_res, bits);
438 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
439#else
440 *res = zig_subw_u32(lhs, rhs, bits);
441 return *res > lhs;
442#endif
443}
444
445zig_extern zig_i32 __subosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
446static inline zig_bool zig_subo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
447#if zig_has_builtin(sub_overflow)
448 zig_i32 full_res;
449 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
450#else
451 zig_c_int overflow_int;
452 zig_u32 full_res = __subosi4(lhs, rhs, &overflow_int);
453 zig_bool overflow = overflow_int != 0;
454#endif
455 *res = zig_wrap_i32(full_res, bits);
456 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);
457}
458
459static inline zig_bool zig_subo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
460#if zig_has_builtin(sub_overflow)
461 zig_u64 full_res;
462 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
463 *res = zig_wrap_u64(full_res, bits);
464 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
465#else
466 *res = zig_subw_u64(lhs, rhs, bits);
467 return *res > lhs;
468#endif
469}
470
471zig_extern zig_i64 __subodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
472static inline zig_bool zig_subo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
473#if zig_has_builtin(sub_overflow)
474 zig_i64 full_res;
475 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
476#else
477 zig_c_int overflow_int;
478 zig_u64 full_res = __subodi4(lhs, rhs, &overflow_int);
479 zig_bool overflow = overflow_int != 0;
480#endif
481 *res = zig_wrap_i64(full_res, bits);
482 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
483}
484
485static inline zig_bool zig_subo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {
486#if zig_has_builtin(sub_overflow)
487 zig_u8 full_res;
488 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
489 *res = zig_wrap_u8(full_res, bits);
490 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
491#else
492 return zig_subo_u32(res, lhs, rhs, bits);
493#endif
494}
495
496static inline zig_bool zig_subo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {
497#if zig_has_builtin(sub_overflow)
498 zig_i8 full_res;
499 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
500 *res = zig_wrap_i8(full_res, bits);
501 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
502#else
503 return zig_subo_i32(res, lhs, rhs, bits);
504#endif
505}
506
507static inline zig_bool zig_subo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {
508#if zig_has_builtin(sub_overflow)
509 zig_u16 full_res;
510 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
511 *res = zig_wrap_u16(full_res, bits);
512 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
513#else
514 return zig_subo_u32(res, lhs, rhs, bits);
515#endif
516}
517
518static inline zig_bool zig_subo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {
519#if zig_has_builtin(sub_overflow)
520 zig_i16 full_res;
521 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
522 *res = zig_wrap_i16(full_res, bits);
523 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
524#else
525 return zig_subo_i32(res, lhs, rhs, bits);
526#endif
527}
528
529static inline zig_bool zig_mulo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {
530#if zig_has_builtin(mul_overflow)
531 zig_u32 full_res;
532 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
533 *res = zig_wrap_u32(full_res, bits);
534 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
535#else
536 *res = zig_mulw_u32(lhs, rhs, bits);
537 return rhs != zig_as_u32(0) && lhs > zig_maxInt(u32, bits) / rhs;
538#endif
539}
540
541zig_extern zig_i32 __mulosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
542static inline zig_bool zig_mulo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
543#if zig_has_builtin(mul_overflow)
544 zig_i32 full_res;
545 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
546#else
547 zig_c_int overflow_int;
548 zig_u32 full_res = __mulosi4(lhs, rhs, &overflow_int);
549 zig_bool overflow = overflow_int != 0;
550#endif
551 *res = zig_wrap_i32(full_res, bits);
552 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);
553}
554
555static inline zig_bool zig_mulo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
556#if zig_has_builtin(mul_overflow)
557 zig_u64 full_res;
558 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
559 *res = zig_wrap_u64(full_res, bits);
560 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
561#else
562 *res = zig_mulw_u64(lhs, rhs, bits);
563 return rhs != zig_as_u64(0) && lhs > zig_maxInt(u64, bits) / rhs;
564#endif
565}
566
567zig_extern zig_i64 __mulodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
568static inline zig_bool zig_mulo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
569#if zig_has_builtin(mul_overflow)
570 zig_i64 full_res;
571 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
572#else
573 zig_c_int overflow_int;
574 zig_u64 full_res = __mulodi4(lhs, rhs, &overflow_int);
575 zig_bool overflow = overflow_int != 0;
576#endif
577 *res = zig_wrap_i64(full_res, bits);
578 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
579}
580
581static inline zig_bool zig_mulo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {
582#if zig_has_builtin(mul_overflow)
583 zig_u8 full_res;
584 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
585 *res = zig_wrap_u8(full_res, bits);
586 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
587#else
588 return zig_mulo_u32(res, lhs, rhs, bits);
589#endif
590}
591
592static inline zig_bool zig_mulo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {
593#if zig_has_builtin(mul_overflow)
594 zig_i8 full_res;
595 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
596 *res = zig_wrap_i8(full_res, bits);
597 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
598#else
599 return zig_mulo_i32(res, lhs, rhs, bits);
600#endif
601}
602
603static inline zig_bool zig_mulo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {
604#if zig_has_builtin(mul_overflow)
605 zig_u16 full_res;
606 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
607 *res = zig_wrap_u16(full_res, bits);
608 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
609#else
610 return zig_mulo_u32(res, lhs, rhs, bits);
611#endif
612}
613
614static inline zig_bool zig_mulo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {
615#if zig_has_builtin(mul_overflow)
616 zig_i16 full_res;
617 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
618 *res = zig_wrap_i16(full_res, bits);
619 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
620#else
621 return zig_mulo_i32(res, lhs, rhs, bits);
622#endif
623}
624
625#define zig_int_builtins(w) \
626 static inline zig_u##w zig_shlw_u##w(zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \
627 return zig_wrap_u##w(zig_shl_u##w(lhs, rhs), bits); \
628 } \
629\
630 static inline zig_i##w zig_shlw_i##w(zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \
631 return zig_wrap_i##w((zig_i##w)zig_shl_u##w((zig_u##w)lhs, (zig_u##w)rhs), bits); \
632 } \
633\
634 static inline zig_u##w zig_addw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
635 return zig_wrap_u##w(lhs + rhs, bits); \
636 } \
637\
638 static inline zig_i##w zig_addw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
639 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs + (zig_u##w)rhs), bits); \
640 } \
641\
642 static inline zig_u##w zig_subw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
643 return zig_wrap_u##w(lhs - rhs, bits); \
644 } \
645\
646 static inline zig_i##w zig_subw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
647 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs - (zig_u##w)rhs), bits); \
648 } \
649\
650 static inline zig_u##w zig_mulw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
651 return zig_wrap_u##w(lhs * rhs, bits); \
652 } \
653\
654 static inline zig_i##w zig_mulw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
655 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs * (zig_u##w)rhs), bits); \
656 } \
657\
658 static inline zig_bool zig_shlo_u##w(zig_u##w *res, zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \
659 *res = zig_shlw_u##w(lhs, rhs, bits); \
660 return (lhs & zig_maxInt_u##w << (bits - rhs)) != zig_as_u##w(0); \
661 } \
662\
663 static inline zig_bool zig_shlo_i##w(zig_i##w *res, zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \
664 *res = zig_shlw_i##w(lhs, rhs, bits); \
665 zig_i##w mask = (zig_i##w)(zig_maxInt_u##w << (bits - rhs - 1)); \
666 return (lhs & mask) != zig_as_i##w(0) && (lhs & mask) != mask; \
667 } \
668\
669 static inline zig_u##w zig_shls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
670 zig_u##w res; \
671 if (rhs >= bits) return lhs != zig_as_u##w(0) ? zig_maxInt(u##w, bits) : lhs; \
672 return zig_shlo_u##w(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u##w, bits) : res; \
673 } \
674\
675 static inline zig_i##w zig_shls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
676 zig_i##w res; \
677 if ((zig_u##w)rhs < (zig_u##w)bits && !zig_shlo_i##w(&res, lhs, rhs, bits)) return res; \
678 return lhs < zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
679 } \
680\
681 static inline zig_u##w zig_adds_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
682 zig_u##w res; \
683 return zig_addo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \
684 } \
685\
686 static inline zig_i##w zig_adds_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
687 zig_i##w res; \
688 if (!zig_addo_i##w(&res, lhs, rhs, bits)) return res; \
689 return res >= zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
690 } \
691\
692 static inline zig_u##w zig_subs_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
693 zig_u##w res; \
694 return zig_subo_u##w(&res, lhs, rhs, bits) ? zig_minInt(u##w, bits) : res; \
695 } \
696\
697 static inline zig_i##w zig_subs_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
698 zig_i##w res; \
699 if (!zig_subo_i##w(&res, lhs, rhs, bits)) return res; \
700 return res >= zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
701 } \
702\
703 static inline zig_u##w zig_muls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
704 zig_u##w res; \
705 return zig_mulo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \
706 } \
707\
708 static inline zig_i##w zig_muls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
709 zig_i##w res; \
710 if (!zig_mulo_i##w(&res, lhs, rhs, bits)) return res; \
711 return (lhs ^ rhs) < zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
712 }
713zig_int_builtins(8)
714zig_int_builtins(16)
715zig_int_builtins(32)
716zig_int_builtins(64)
717
718#define zig_builtin8(name, val) __builtin_##name(val)
719typedef zig_c_uint zig_Builtin8;
720
721#define zig_builtin16(name, val) __builtin_##name(val)
722typedef zig_c_uint zig_Builtin16;
723
724#if INT_MIN <= INT32_MIN
725#define zig_builtin32(name, val) __builtin_##name(val)
726typedef zig_c_uint zig_Builtin32;
727#elif LONG_MIN <= INT32_MIN
728#define zig_builtin32(name, val) __builtin_##name##l(val)
729typedef zig_c_ulong zig_Builtin32;
730#endif
731
732#if INT_MIN <= INT64_MIN
733#define zig_builtin64(name, val) __builtin_##name(val)
734typedef zig_c_uint zig_Builtin64;
735#elif LONG_MIN <= INT64_MIN
736#define zig_builtin64(name, val) __builtin_##name##l(val)
737typedef zig_c_ulong zig_Builtin64;
738#elif LLONG_MIN <= INT64_MIN
739#define zig_builtin64(name, val) __builtin_##name##ll(val)
740typedef zig_c_ulonglong zig_Builtin64;
741#endif
742
743#if zig_has_builtin(clz)
744#define zig_builtin_clz(w) \
745 static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \
746 if (val == 0) return bits; \
747 return zig_builtin##w(clz, val) - (zig_bitSizeOf(zig_Builtin##w) - bits); \
748 } \
749\
750 static inline zig_u8 zig_clz_i##w(zig_i##w val, zig_u8 bits) { \
751 return zig_clz_u##w((zig_u##w)val, bits); \
752 }
753zig_builtin_clz(8)
754zig_builtin_clz(16)
755zig_builtin_clz(32)
756zig_builtin_clz(64)
757#endif
758
759#if zig_has_builtin(ctz)
760#define zig_builtin_ctz(w) \
761 static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \
762 if (val == 0) return bits; \
763 return zig_builtin##w(ctz, val); \
764 } \
765\
766 static inline zig_u8 zig_ctz_i##w(zig_i##w val, zig_u8 bits) { \
767 return zig_ctz_u##w((zig_u##w)val, bits); \
768 }
769zig_builtin_ctz(8)
770zig_builtin_ctz(16)
771zig_builtin_ctz(32)
772zig_builtin_ctz(64)
773#endif
774
775#if zig_has_builtin(popcount)
776#define zig_builtin_popcount(w) \
777 static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \
778 (void)bits; \
779 return zig_builtin##w(popcount, val); \
780 } \
781\
782 static inline zig_u8 zig_popcount_i##w(zig_i##w val, zig_u8 bits) { \
783 \
784 return zig_popcount_u##w((zig_u##w)val, bits); \
785 }
786zig_builtin_popcount(8)
787zig_builtin_popcount(16)
788zig_builtin_popcount(32)
789zig_builtin_popcount(64)
790#endif
791
792static inline zig_u8 zig_byte_swap_u8(zig_u8 val, zig_u8 bits) {
793 return zig_wrap_u8(val >> (8 - bits), bits);
794}
795
796static inline zig_i8 zig_byte_swap_i8(zig_i8 val, zig_u8 bits) {
797 return zig_wrap_i8((zig_i8)zig_byte_swap_u8((zig_u8)val, bits), bits);
798}
799
800static inline zig_u16 zig_byte_swap_u16(zig_u16 val, zig_u8 bits) {
801 zig_u16 full_res;
802#if zig_has_builtin(bswap16)
803 full_res = __builtin_bswap16(val);
804#else
805 full_res = (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 0)) << 8 |
806 (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 8)) >> 0;
807#endif
808 return zig_wrap_u16(full_res >> (16 - bits), bits);
809}
810
811static inline zig_i16 zig_byte_swap_i16(zig_i16 val, zig_u8 bits) {
812 return zig_wrap_i16((zig_i16)zig_byte_swap_u16((zig_u16)val, bits), bits);
813}
814
815static inline zig_u32 zig_byte_swap_u32(zig_u32 val, zig_u8 bits) {
816 zig_u32 full_res;
817#if zig_has_builtin(bswap32)
818 full_res = __builtin_bswap32(val);
819#else
820 full_res = (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 0)) << 16 |
821 (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 16)) >> 0;
822#endif
823 return zig_wrap_u32(full_res >> (32 - bits), bits);
824}
825
826static inline zig_i32 zig_byte_swap_i32(zig_i32 val, zig_u8 bits) {
827 return zig_wrap_i32((zig_i32)zig_byte_swap_u32((zig_u32)val, bits), bits);
828}
829
830static inline zig_u64 zig_byte_swap_u64(zig_u64 val, zig_u8 bits) {
831 zig_u64 full_res;
832#if zig_has_builtin(bswap64)
833 full_res = __builtin_bswap64(val);
834#else
835 full_res = (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 0)) << 32 |
836 (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 32)) >> 0;
837#endif
838 return zig_wrap_u64(full_res >> (64 - bits), bits);
839}
840
841static inline zig_i64 zig_byte_swap_i64(zig_i64 val, zig_u8 bits) {
842 return zig_wrap_i64((zig_i64)zig_byte_swap_u64((zig_u64)val, bits), bits);
843}
844
845static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) {
846 zig_u8 full_res;
847#if zig_has_builtin(bitreverse8)
848 full_res = __builtin_bitreverse8(val);
849#else
850 static zig_u8 const lut[0x10] = {
851 0b0000, 0b1000, 0b0100, 0b1100,
852 0b0010, 0b1010, 0b0110, 0b1110,
853 0b0001, 0b1001, 0b0101, 0b1101,
854 0b0011, 0b1011, 0b0111, 0b1111,
855 };
856 full_res = lut[val >> 0 & 0xF] << 4 | lut[val >> 4 & 0xF] << 0;
857#endif
858 return zig_wrap_u8(full_res >> (8 - bits), bits);
859}
860
861static inline zig_i8 zig_bit_reverse_i8(zig_i8 val, zig_u8 bits) {
862 return zig_wrap_i8((zig_i8)zig_bit_reverse_u8((zig_u8)val, bits), bits);
863}
864
865static inline zig_u16 zig_bit_reverse_u16(zig_u16 val, zig_u8 bits) {
866 zig_u16 full_res;
867#if zig_has_builtin(bitreverse16)
868 full_res = __builtin_bitreverse16(val);
869#else
870 full_res = (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 0), 8) << 8 |
871 (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 8), 8) >> 0;
872#endif
873 return zig_wrap_u16(full_res >> (16 - bits), bits);
874}
875
876static inline zig_i16 zig_bit_reverse_i16(zig_i16 val, zig_u8 bits) {
877 return zig_wrap_i16((zig_i16)zig_bit_reverse_u16((zig_u16)val, bits), bits);
878}
879
880static inline zig_u32 zig_bit_reverse_u32(zig_u32 val, zig_u8 bits) {
881 zig_u32 full_res;
882#if zig_has_builtin(bitreverse32)
883 full_res = __builtin_bitreverse32(val);
884#else
885 full_res = (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 0), 16) << 16 |
886 (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 16), 16) >> 0;
887#endif
888 return zig_wrap_u32(full_res >> (32 - bits), bits);
889}
890
891static inline zig_i32 zig_bit_reverse_i32(zig_i32 val, zig_u8 bits) {
892 return zig_wrap_i32((zig_i32)zig_bit_reverse_u32((zig_u32)val, bits), bits);
893}
894
895static inline zig_u64 zig_bit_reverse_u64(zig_u64 val, zig_u8 bits) {
896 zig_u64 full_res;
897#if zig_has_builtin(bitreverse64)
898 full_res = __builtin_bitreverse64(val);
899#else
900 full_res = (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 0), 32) << 32 |
901 (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 32), 32) >> 0;
902#endif
903 return zig_wrap_u64(full_res >> (64 - bits), bits);
904}
905
906static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) {
907 return zig_wrap_i64((zig_i64)zig_bit_reverse_u64((zig_u64)val, bits), bits);
908}
909
910/* ======================== 128-bit Integer Routines ======================== */
911
912#if !defined(zig_has_int128)
913# if defined(__SIZEOF_INT128__)
914# define zig_has_int128 1
915# else
916# define zig_has_int128 0
917# endif
918#endif
919
920#if zig_has_int128
921
922typedef unsigned __int128 zig_u128;
923typedef signed __int128 zig_i128;
924
925#define zig_as_u128(hi, lo) ((zig_u128)(hi)<<64|(lo))
926#define zig_as_i128(hi, lo) ((zig_i128)zig_as_u128(hi, lo))
927#define zig_hi_u128(val) ((zig_u64)((val) >> 64))
928#define zig_lo_u128(val) ((zig_u64)((val) >> 0))
929#define zig_hi_i128(val) ((zig_i64)((val) >> 64))
930#define zig_lo_i128(val) ((zig_u64)((val) >> 0))
931#define zig_bitcast_u128(val) ((zig_u128)(val))
932#define zig_bitcast_i128(val) ((zig_i128)(val))
933#define zig_cmp_int128(Type) \
934 static inline zig_i8 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
935 return (zig_i8)((lhs > rhs) - (lhs < rhs)); \
936 }
937#define zig_bit_int128(Type, operation, operator) \
938 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
939 return lhs operator rhs; \
940 }
941
942#else /* zig_has_int128 */
943
944#if __LITTLE_ENDIAN__ || _MSC_VER
945typedef struct { zig_align(16) zig_u64 lo; zig_u64 hi; } zig_u128;
946typedef struct { zig_align(16) zig_u64 lo; zig_i64 hi; } zig_i128;
947#else
948typedef struct { zig_align(16) zig_u64 hi; zig_u64 lo; } zig_u128;
949typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128;
950#endif
951
952#define zig_as_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) })
953#define zig_as_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) })
954#define zig_hi_u128(val) ((val).hi)
955#define zig_lo_u128(val) ((val).lo)
956#define zig_hi_i128(val) ((val).hi)
957#define zig_lo_i128(val) ((val).lo)
958#define zig_bitcast_u128(val) zig_as_u128((zig_u64)(val).hi, (val).lo)
959#define zig_bitcast_i128(val) zig_as_i128((zig_i64)(val).hi, (val).lo)
960#define zig_cmp_int128(Type) \
961 static inline zig_i8 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
962 return (lhs.hi == rhs.hi) \
963 ? (lhs.lo > rhs.lo) - (lhs.lo < rhs.lo) \
964 : (lhs.hi > rhs.hi) - (lhs.hi < rhs.hi); \
965 }
966#define zig_bit_int128(Type, operation, operator) \
967 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
968 return (zig_##Type){ .hi = lhs.hi operator rhs.hi, .lo = lhs.lo operator rhs.lo }; \
969 }
970
971#endif /* zig_has_int128 */
972
973#define zig_minInt_u128 zig_as_u128(zig_minInt_u64, zig_minInt_u64)
974#define zig_maxInt_u128 zig_as_u128(zig_maxInt_u64, zig_maxInt_u64)
975#define zig_minInt_i128 zig_as_i128(zig_minInt_i64, zig_minInt_u64)
976#define zig_maxInt_i128 zig_as_i128(zig_maxInt_i64, zig_maxInt_u64)
977
978zig_cmp_int128(u128)
979zig_cmp_int128(i128)
980
981zig_bit_int128(u128, and, &)
982zig_bit_int128(i128, and, &)
983
984zig_bit_int128(u128, or, |)
985zig_bit_int128(i128, or, |)
986
987zig_bit_int128(u128, xor, ^)
988zig_bit_int128(i128, xor, ^)
989
990static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs);
991
992#if zig_has_int128
993
994static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) {
995 return val ^ zig_maxInt(u128, bits);
996}
997
998static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) {
999 (void)bits;
1000 return ~val;
1001}
1002
1003static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) {
1004 return lhs >> rhs;
1005}
1006
1007static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) {
1008 return lhs << rhs;
1009}
1010
1011static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) {
1012 return lhs << rhs;
1013}
1014
1015static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) {
1016 return lhs + rhs;
1017}
1018
1019static inline zig_i128 zig_add_i128(zig_i128 lhs, zig_i128 rhs) {
1020 return lhs + rhs;
1021}
1022
1023static inline zig_u128 zig_sub_u128(zig_u128 lhs, zig_u128 rhs) {
1024 return lhs - rhs;
1025}
1026
1027static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) {
1028 return lhs - rhs;
1029}
1030
1031static inline zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) {
1032 return lhs * rhs;
1033}
1034
1035static inline zig_i128 zig_mul_i128(zig_i128 lhs, zig_i128 rhs) {
1036 return lhs * rhs;
1037}
1038
1039static inline zig_u128 zig_div_trunc_u128(zig_u128 lhs, zig_u128 rhs) {
1040 return lhs / rhs;
1041}
1042
1043static inline zig_i128 zig_div_trunc_i128(zig_i128 lhs, zig_i128 rhs) {
1044 return lhs / rhs;
1045}
1046
1047static inline zig_u128 zig_rem_u128(zig_u128 lhs, zig_u128 rhs) {
1048 return lhs % rhs;
1049}
1050
1051static inline zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {
1052 return lhs % rhs;
1053}
1054
1055static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) {
1056 return zig_div_trunc_i128(lhs, rhs) - (((lhs ^ rhs) & zig_rem_i128(lhs, rhs)) < zig_as_i128(0, 0));
1057}
1058
1059static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
1060 zig_i128 rem = zig_rem_i128(lhs, rhs);
1061 return rem + (((lhs ^ rhs) & rem) < zig_as_i128(0, 0) ? rhs : zig_as_i128(0, 0));
1062}
1063
1064#else /* zig_has_int128 */
1065
1066static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) {
1067 return (zig_u128){ .hi = zig_not_u64(val.hi, bits - zig_as_u8(64)), .lo = zig_not_u64(val.lo, zig_as_u8(64)) };
1068}
1069
1070static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) {
1071 return (zig_i128){ .hi = zig_not_i64(val.hi, bits - zig_as_u8(64)), .lo = zig_not_u64(val.lo, zig_as_u8(64)) };
1072}
1073
1074static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) {
1075 if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = lhs.hi << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 };
1076 return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };
1077}
1078
1079static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) {
1080 if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = lhs.hi << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 };
1081 return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };
1082}
1083
1084static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) {
1085 if (rhs >= zig_as_u8(64)) return (zig_i128){ .hi = lhs.hi << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 };
1086 return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };
1087}
1088
1089static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) {
1090 zig_u128 res;
1091 res.hi = lhs.hi + rhs.hi + zig_addo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64);
1092 return res;
1093}
1094
1095static inline zig_i128 zig_add_i128(zig_i128 lhs, zig_i128 rhs) {
1096 zig_i128 res;
1097 res.hi = lhs.hi + rhs.hi + zig_addo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64);
1098 return res;
1099}
1100
1101static inline zig_u128 zig_sub_u128(zig_u128 lhs, zig_u128 rhs) {
1102 zig_u128 res;
1103 res.hi = lhs.hi - rhs.hi - zig_subo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64);
1104 return res;
1105}
1106
1107static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) {
1108 zig_i128 res;
1109 res.hi = lhs.hi - rhs.hi - zig_subo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64);
1110 return res;
1111}
1112
1113static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) {
1114 return zig_sub_i128(zig_div_trunc_i128(lhs, rhs), (((lhs.hi ^ rhs.hi) & zig_rem_i128(lhs, rhs).hi) < zig_as_i64(0)) ? zig_as_i128(0, 1) : zig_as_i128(0, 0));
1115}
1116
1117static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
1118 zig_i128 rem = zig_rem_i128(lhs, rhs);
1119 return rem + (((lhs.hi ^ rhs.hi) & rem.hi) < zig_as_i64(0) ? rhs : zig_as_i128(0, 0));
1120}
1121
1122#endif /* zig_has_int128 */
1123
1124#define zig_div_floor_u128 zig_div_trunc_u128
1125#define zig_mod_u128 zig_rem_u128
1126
1127static inline zig_u128 zig_min_u128(zig_u128 lhs, zig_u128 rhs) {
1128 return zig_cmp_u128(lhs, rhs) < 0 ? lhs : rhs;
1129}
1130
1131static inline zig_i128 zig_min_i128(zig_i128 lhs, zig_i128 rhs) {
1132 return zig_cmp_i128(lhs, rhs) < 0 ? lhs : rhs;
1133}
1134
1135static inline zig_u128 zig_max_u128(zig_u128 lhs, zig_u128 rhs) {
1136 return zig_cmp_u128(lhs, rhs) > 0 ? lhs : rhs;
1137}
1138
1139static inline zig_i128 zig_max_i128(zig_i128 lhs, zig_i128 rhs) {
1140 return zig_cmp_i128(lhs, rhs) > 0 ? lhs : rhs;
1141}
1142
1143static inline zig_i128 zig_shr_i128(zig_i128 lhs, zig_u8 rhs) {
1144 zig_i128 sign_mask = zig_cmp_i128(lhs, zig_as_i128(0, 0)) < zig_as_i8(0) ? -zig_as_i128(0, 1) : zig_as_i128(0, 0);
1145 return zig_xor_i128(zig_bitcast_i128(zig_shr_u128(zig_bitcast_u128(zig_xor_i128(lhs, sign_mask)), rhs)), sign_mask);
1146}
1147
1148static inline zig_u128 zig_wrap_u128(zig_u128 val, zig_u8 bits) {
1149 return zig_and_u128(val, zig_maxInt(u128, bits));
1150}
1151
1152static inline zig_i128 zig_wrap_i128(zig_i128 val, zig_u8 bits) {
1153 return zig_as_i128(zig_wrap_i64(zig_hi_i128(val), bits - zig_as_u8(64)), zig_lo_i128(val));
1154}
1155
1156static inline zig_u128 zig_shlw_u128(zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {
1157 return zig_wrap_u128(zig_shl_u128(lhs, rhs), bits);
1158}
1159
1160static inline zig_i128 zig_shlw_i128(zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {
1161 return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1162}
1163
1164static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {
1165 return zig_wrap_u128(zig_add_u128(lhs, rhs), bits);
1166}
1167
1168static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1169 return zig_wrap_i128(zig_bitcast_i128(zig_add_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1170}
1171
1172static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1173 return zig_wrap_u128(zig_sub_u128(lhs, rhs), bits);
1174}
1175
1176static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1177 return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1178}
1179
1180static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1181 return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits);
1182}
1183
1184static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1185 return zig_wrap_i128(zig_bitcast_i128(zig_mul_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1186}
1187
1188#if zig_has_int128
1189
1190static inline zig_bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {
1191 *res = zig_shlw_u128(lhs, rhs, bits);
1192 return zig_and_u128(lhs, zig_shl_u128(zig_maxInt_u128, bits - rhs)) != zig_as_u128(0, 0);
1193}
1194
1195static inline zig_bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {
1196 *res = zig_shlw_i128(lhs, rhs, bits);
1197 zig_i128 mask = zig_bitcast_i128(zig_shl_u128(zig_maxInt_u128, bits - rhs - zig_as_u8(1)));
1198 return zig_cmp_i128(zig_and_i128(lhs, mask), zig_as_i128(0, 0)) != zig_as_i8(0) &&
1199 zig_cmp_i128(zig_and_i128(lhs, mask), mask) != zig_as_i8(0);
1200}
1201
1202static inline zig_bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1203#if zig_has_builtin(add_overflow)
1204 zig_u128 full_res;
1205 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
1206 *res = zig_wrap_u128(full_res, bits);
1207 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1208#else
1209 *res = zig_addw_u128(lhs, rhs, bits);
1210 return *res < lhs;
1211#endif
1212}
1213
1214zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1215static inline zig_bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1216#if zig_has_builtin(add_overflow)
1217 zig_i128 full_res;
1218 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
1219#else
1220 zig_c_int overflow_int;
1221 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
1222 zig_bool overflow = overflow_int != 0;
1223#endif
1224 *res = zig_wrap_i128(full_res, bits);
1225 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1226}
1227
1228static inline zig_bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1229#if zig_has_builtin(sub_overflow)
1230 zig_u128 full_res;
1231 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
1232 *res = zig_wrap_u128(full_res, bits);
1233 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1234#else
1235 *res = zig_subw_u128(lhs, rhs, bits);
1236 return *res > lhs;
1237#endif
1238}
1239
1240zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1241static inline zig_bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1242#if zig_has_builtin(sub_overflow)
1243 zig_i128 full_res;
1244 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
1245#else
1246 zig_c_int overflow_int;
1247 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
1248 zig_bool overflow = overflow_int != 0;
1249#endif
1250 *res = zig_wrap_i128(full_res, bits);
1251 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1252}
1253
1254static inline zig_bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1255#if zig_has_builtin(mul_overflow)
1256 zig_u128 full_res;
1257 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
1258 *res = zig_wrap_u128(full_res, bits);
1259 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1260#else
1261 *res = zig_mulw_u128(lhs, rhs, bits);
1262 return rhs != zig_as_u128(0, 0) && lhs > zig_maxInt(u128, bits) / rhs;
1263#endif
1264}
1265
1266zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1267static inline zig_bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1268#if zig_has_builtin(mul_overflow)
1269 zig_i128 full_res;
1270 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
1271#else
1272 zig_c_int overflow_int;
1273 zig_i128 full_res = __muloti4(lhs, rhs, &overflow);
1274 zig_bool overflow = overflow_int != 0;
1275#endif
1276 *res = zig_wrap_i128(full_res, bits);
1277 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1278}
1279
1280#else /* zig_has_int128 */
1281
1282static inline zig_bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs) {
1283 return zig_addo_u64(&res->hi, lhs.hi, rhs.hi, UINT64_MAX) |
1284 zig_addo_u64(&res->hi, res->hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, UINT64_MAX));
1285}
1286
1287static inline zig_bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs) {
1288 return zig_subo_u64(&res->hi, lhs.hi, rhs.hi, UINT64_MAX) |
1289 zig_subo_u64(&res->hi, res->hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, UINT64_MAX));
1290}
1291
1292#endif /* zig_has_int128 */
1293
1294static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1295 zig_u128 res;
1296 if (zig_cmp_u128(rhs, zig_as_u128(0, bits)) >= zig_as_i8(0))
1297 return zig_cmp_u128(lhs, zig_as_u128(0, 0)) != zig_as_i8(0) ? zig_maxInt(u128, bits) : lhs;
1298 return zig_shlo_u128(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u128, bits) : res;
1299}
1300
1301static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1302 zig_i128 res;
1303 if (zig_cmp_u128(zig_bitcast_u128(rhs), zig_as_u128(0, bits)) < zig_as_i8(0) && !zig_shlo_i128(&res, lhs, rhs, bits)) return res;
1304 return zig_cmp_i128(lhs, zig_as_i128(0, 0)) < zig_as_i8(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1305}
1306
1307static inline zig_u128 zig_adds_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1308 zig_u128 res;
1309 return zig_addo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res;
1310}
1311
1312static inline zig_i128 zig_adds_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1313 zig_i128 res;
1314 if (!zig_addo_i128(&res, lhs, rhs, bits)) return res;
1315 return zig_cmp_i128(res, zig_as_i128(0, 0)) >= zig_as_i8(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1316}
1317
1318static inline zig_u128 zig_subs_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1319 zig_u128 res;
1320 return zig_subo_u128(&res, lhs, rhs, bits) ? zig_minInt(u128, bits) : res;
1321}
1322
1323static inline zig_i128 zig_subs_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1324 zig_i128 res;
1325 if (!zig_subo_i128(&res, lhs, rhs, bits)) return res;
1326 return zig_cmp_i128(res, zig_as_i128(0, 0)) >= zig_as_i8(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1327}
1328
1329static inline zig_u128 zig_muls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1330 zig_u128 res;
1331 return zig_mulo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res;
1332}
1333
1334static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1335 zig_i128 res;
1336 if (!zig_mulo_i128(&res, lhs, rhs, bits)) return res;
1337 return zig_cmp_i128(zig_xor_i128(lhs, rhs), zig_as_i128(0, 0)) < zig_as_i8(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1338}
1339
1340static inline zig_u8 zig_clz_u128(zig_u128 val, zig_u8 bits) {
1341 if (zig_hi_u128(val) != 0) return zig_clz_u64(zig_hi_u128(val), bits - zig_as_u8(64));
1342 return zig_clz_u64(zig_lo_u128(val), zig_as_u8(64)) + zig_as_u8(64);
1343}
1344
1345static inline zig_u8 zig_clz_i128(zig_i128 val, zig_u8 bits) {
1346 return zig_clz_u128(zig_bitcast_u128(val), bits);
1347}
1348
1349static inline zig_u8 zig_ctz_u128(zig_u128 val, zig_u8 bits) {
1350 if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), zig_as_u8(64));
1351 return zig_ctz_u64(zig_hi_u128(val), bits - zig_as_u8(64)) + zig_as_u8(64);
1352}
1353
1354static inline zig_u8 zig_ctz_i128(zig_i128 val, zig_u8 bits) {
1355 return zig_ctz_u128(zig_bitcast_u128(val), bits);
1356}
1357
1358static inline zig_u8 zig_popcount_u128(zig_u128 val, zig_u8 bits) {
1359 return zig_popcount_u64(zig_hi_u128(val), bits - zig_as_u8(64)) +
1360 zig_popcount_u64(zig_lo_u128(val), zig_as_u8(64));
1361}
1362
1363static inline zig_u8 zig_popcount_i128(zig_i128 val, zig_u8 bits) {
1364 return zig_popcount_u128(zig_bitcast_u128(val), bits);
1365}
1366
1367static inline zig_u128 zig_byte_swap_u128(zig_u128 val, zig_u8 bits) {
1368 zig_u128 full_res;
1369#if zig_has_builtin(bswap128)
1370 full_res = __builtin_bswap128(val);
1371#else
1372 full_res = zig_as_u128(zig_byte_swap_u64(zig_lo_u128(val), zig_as_u8(64)),
1373 zig_byte_swap_u64(zig_hi_u128(val), zig_as_u8(64)));
1374#endif
1375 return zig_shr_u128(full_res, zig_as_u8(128) - bits);
1376}
1377
1378static inline zig_i128 zig_byte_swap_i128(zig_i128 val, zig_u8 bits) {
1379 return zig_byte_swap_u128(zig_bitcast_u128(val), bits);
1380}
1381
1382static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, zig_u8 bits) {
1383 return zig_shr_u128(zig_as_u128(zig_bit_reverse_u64(zig_lo_u128(val), zig_as_u8(64)),
1384 zig_bit_reverse_u64(zig_hi_u128(val), zig_as_u8(64))),
1385 zig_as_u8(128) - bits);
1386}
1387
1388static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) {
1389 return zig_bit_reverse_u128(zig_bitcast_u128(val), bits);
1390}
1391
1392/* ========================= Floating Point Support ========================= */
1393
1394#define zig_has_f16 1
1395#define zig_bitSizeOf_f16 16
1396#define zig_libc_name_f16(name) __##name##h
1397#define zig_as_special_f16(sign, name, arg, repr) sign zig_as_f16(__builtin_##name, )(arg)
1398#if FLT_MANT_DIG == 11
1399typedef float zig_f16;
1400#define zig_as_f16(fp, repr) fp##f
1401#elif DBL_MANT_DIG == 11
1402typedef double zig_f16;
1403#define zig_as_f16(fp, repr) fp
1404#elif LDBL_MANT_DIG == 11
1405#define zig_bitSizeOf_c_longdouble 16
1406typedef long double zig_f16;
1407#define zig_as_f16(fp, repr) fp##l
1408#elif FLT16_MANT_DIG == 11
1409typedef _Float16 zig_f16;
1410#define zig_as_f16(fp, repr) fp##f16
1411#elif defined(__SIZEOF_FP16__)
1412typedef __fp16 zig_f16;
1413#define zig_as_f16(fp, repr) fp##f16
1414#else
1415#undef zig_has_f16
1416#define zig_has_f16 0
1417#define zig_repr_f16 i16
1418typedef zig_i16 zig_f16;
1419#define zig_as_f16(fp, repr) repr
1420#undef zig_as_special_f16
1421#define zig_as_special_f16(sign, name, arg, repr) repr
1422#endif
1423
1424#define zig_has_f32 1
1425#define zig_bitSizeOf_f32 32
1426#define zig_libc_name_f32(name) name##f
1427#define zig_as_special_f32(sign, name, arg, repr) sign zig_as_f32(__builtin_##name, )(arg)
1428#if FLT_MANT_DIG == 24
1429typedef float zig_f32;
1430#define zig_as_f32(fp, repr) fp##f
1431#elif DBL_MANT_DIG == 24
1432typedef double zig_f32;
1433#define zig_as_f32(fp, repr) fp
1434#elif LDBL_MANT_DIG == 24
1435#define zig_bitSizeOf_c_longdouble 32
1436typedef long double zig_f32;
1437#define zig_as_f32(fp, repr) fp##l
1438#elif FLT32_MANT_DIG == 24
1439typedef _Float32 zig_f32;
1440#define zig_as_f32(fp, repr) fp##f32
1441#else
1442#undef zig_has_f32
1443#define zig_has_f32 0
1444#define zig_repr_f32 i32
1445typedef zig_i32 zig_f32;
1446#define zig_as_f32(fp, repr) repr
1447#undef zig_as_special_f32
1448#define zig_as_special_f32(sign, name, arg, repr) repr
1449#endif
1450
1451#define zig_has_f64 1
1452#define zig_bitSizeOf_f64 64
1453#define zig_libc_name_f64(name) name
1454#define zig_as_special_f64(sign, name, arg, repr) sign zig_as_f64(__builtin_##name, )(arg)
1455#if FLT_MANT_DIG == 53
1456typedef float zig_f64;
1457#define zig_as_f64(fp, repr) fp##f
1458#elif DBL_MANT_DIG == 53
1459typedef double zig_f64;
1460#define zig_as_f64(fp, repr) fp
1461#elif LDBL_MANT_DIG == 53
1462#define zig_bitSizeOf_c_longdouble 64
1463typedef long double zig_f64;
1464#define zig_as_f64(fp, repr) fp##l
1465#elif FLT64_MANT_DIG == 53
1466typedef _Float64 zig_f64;
1467#define zig_as_f64(fp, repr) fp##f64
1468#elif FLT32X_MANT_DIG == 53
1469typedef _Float32x zig_f64;
1470#define zig_as_f64(fp, repr) fp##f32x
1471#else
1472#undef zig_has_f64
1473#define zig_has_f64 0
1474#define zig_repr_f64 i64
1475typedef zig_i64 zig_f64;
1476#define zig_as_f64(fp, repr) repr
1477#undef zig_as_special_f64
1478#define zig_as_special_f64(sign, name, arg, repr) repr
1479#endif
1480
1481#define zig_has_f80 1
1482#define zig_bitSizeOf_f80 80
1483#define zig_libc_name_f80(name) __##name##x
1484#define zig_as_special_f80(sign, name, arg, repr) sign zig_as_f80(__builtin_##name, )(arg)
1485#if FLT_MANT_DIG == 64
1486typedef float zig_f80;
1487#define zig_as_f80(fp, repr) fp##f
1488#elif DBL_MANT_DIG == 64
1489typedef double zig_f80;
1490#define zig_as_f80(fp, repr) fp
1491#elif LDBL_MANT_DIG == 64
1492#define zig_bitSizeOf_c_longdouble 80
1493typedef long double zig_f80;
1494#define zig_as_f80(fp, repr) fp##l
1495#elif FLT80_MANT_DIG == 64
1496typedef _Float80 zig_f80;
1497#define zig_as_f80(fp, repr) fp##f80
1498#elif FLT64X_MANT_DIG == 64
1499typedef _Float64x zig_f80;
1500#define zig_as_f80(fp, repr) fp##f64x
1501#elif defined(__SIZEOF_FLOAT80__)
1502typedef __float80 zig_f80;
1503#define zig_as_f80(fp, repr) fp##l
1504#else
1505#undef zig_has_f80
1506#define zig_has_f80 0
1507#define zig_repr_f80 i128
1508typedef zig_i128 zig_f80;
1509#define zig_as_f80(fp, repr) repr
1510#undef zig_as_special_f80
1511#define zig_as_special_f80(sign, name, arg, repr) repr
1512#endif
1513
1514#define zig_has_f128 1
1515#define zig_bitSizeOf_f128 128
1516#define zig_libc_name_f128(name) name##q
1517#define zig_as_special_f128(sign, name, arg, repr) sign zig_as_f128(__builtin_##name, )(arg)
1518#if FLT_MANT_DIG == 113
1519typedef float zig_f128;
1520#define zig_as_f128(fp, repr) fp##f
1521#elif DBL_MANT_DIG == 113
1522typedef double zig_f128;
1523#define zig_as_f128(fp, repr) fp
1524#elif LDBL_MANT_DIG == 113
1525#define zig_bitSizeOf_c_longdouble 128
1526typedef long double zig_f128;
1527#define zig_as_f128(fp, repr) fp##l
1528#elif FLT128_MANT_DIG == 113
1529typedef _Float128 zig_f128;
1530#define zig_as_f128(fp, repr) fp##f128
1531#elif FLT64X_MANT_DIG == 113
1532typedef _Float64x zig_f128;
1533#define zig_as_f128(fp, repr) fp##f64x
1534#elif defined(__SIZEOF_FLOAT128__)
1535typedef __float128 zig_f128;
1536#define zig_as_f128(fp, repr) fp##q
1537#undef zig_as_special_f128
1538#define zig_as_special_f128(sign, name, arg, repr) sign __builtin_##name##f128(arg)
1539#else
1540#undef zig_has_f128
1541#define zig_has_f128 0
1542#define zig_repr_f128 i128
1543typedef zig_i128 zig_f128;
1544#define zig_as_f128(fp, repr) repr
1545#undef zig_as_special_f128
1546#define zig_as_special_f128(sign, name, arg, repr) repr
1547#endif
1548
1549#define zig_has_c_longdouble 1
1550typedef long double zig_c_longdouble;
1551#define zig_as_c_longdouble(fp, repr) fp##l
1552#define zig_libc_name_c_longdouble(name) name##l
1553#define zig_as_special_c_longdouble(sign, name, arg, repr) sign __builtin_##name##l(arg)
1554
1555#define zig_convert_builtin(ResType, operation, ArgType, version) \
1556 zig_extern zig_##ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \
1557 zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(zig_##ArgType);
1558zig_convert_builtin(f16, trunc, f32, 2)
1559zig_convert_builtin(f16, trunc, f64, 2)
1560zig_convert_builtin(f16, trunc, f80, 2)
1561zig_convert_builtin(f16, trunc, f128, 2)
1562zig_convert_builtin(f32, extend, f16, 2)
1563zig_convert_builtin(f32, trunc, f64, 2)
1564zig_convert_builtin(f32, trunc, f80, 2)
1565zig_convert_builtin(f32, trunc, f128, 2)
1566zig_convert_builtin(f64, extend, f16, 2)
1567zig_convert_builtin(f64, extend, f32, 2)
1568zig_convert_builtin(f64, trunc, f80, 2)
1569zig_convert_builtin(f64, trunc, f128, 2)
1570zig_convert_builtin(f80, extend, f16, 2)
1571zig_convert_builtin(f80, extend, f32, 2)
1572zig_convert_builtin(f80, extend, f64, 2)
1573zig_convert_builtin(f80, trunc, f128, 2)
1574zig_convert_builtin(f128, extend, f16, 2)
1575zig_convert_builtin(f128, extend, f32, 2)
1576zig_convert_builtin(f128, extend, f64, 2)
1577zig_convert_builtin(f128, extend, f80, 2)
1578
1579#define zig_float_negate_builtin_0(Type) \
1580 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
1581 return zig_expand_concat(zig_xor_, zig_repr_##Type)(arg, zig_expand_minInt(zig_repr_##Type, zig_bitSizeOf_##Type)); \
1582 }
1583#define zig_float_negate_builtin_1(Type) \
1584 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
1585 return -arg; \
1586 }
1587
1588#define zig_float_less_builtin_0(Type, operation) \
1589 zig_extern zig_i8 zig_expand_concat(zig_expand_concat(__##operation, \
1590 zig_compiler_rt_abbrev_##Type), 2)(zig_##Type, zig_##Type); \
1591 static inline zig_i8 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1592 return (zig_i8)zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_##Type), 2)(lhs, rhs); \
1593 }
1594#define zig_float_less_builtin_1(Type, operation) \
1595 static inline zig_i8 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1596 return (zig_i8)(!(lhs <= rhs) - (lhs < rhs)); \
1597 }
1598
1599#define zig_float_greater_builtin_0(Type, operation) \
1600 zig_float_less_builtin_0(Type, operation)
1601#define zig_float_greater_builtin_1(Type, operation) \
1602 static inline zig_i8 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1603 return (zig_i8)((lhs > rhs) - !(lhs >= rhs)); \
1604 }
1605
1606#define zig_float_binary_builtin_0(Type, operation, operator) \
1607 zig_extern zig_##Type zig_expand_concat(zig_expand_concat(__##operation, \
1608 zig_compiler_rt_abbrev_##Type), 3)(zig_##Type, zig_##Type); \
1609 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1610 return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_##Type), 3)(lhs, rhs); \
1611 }
1612#define zig_float_binary_builtin_1(Type, operation, operator) \
1613 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1614 return lhs operator rhs; \
1615 }
1616
1617#define zig_float_builtins(Type) \
1618 zig_convert_builtin(i32, fix, Type, ) \
1619 zig_convert_builtin(u32, fixuns, Type, ) \
1620 zig_convert_builtin(i64, fix, Type, ) \
1621 zig_convert_builtin(u64, fixuns, Type, ) \
1622 zig_convert_builtin(i128, fix, Type, ) \
1623 zig_convert_builtin(u128, fixuns, Type, ) \
1624 zig_convert_builtin(Type, float, i32, ) \
1625 zig_convert_builtin(Type, floatun, u32, ) \
1626 zig_convert_builtin(Type, float, i64, ) \
1627 zig_convert_builtin(Type, floatun, u64, ) \
1628 zig_convert_builtin(Type, float, i128, ) \
1629 zig_convert_builtin(Type, floatun, u128, ) \
1630 zig_expand_concat(zig_float_negate_builtin_, zig_has_##Type)(Type) \
1631 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, cmp) \
1632 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, ne) \
1633 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, eq) \
1634 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, lt) \
1635 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, le) \
1636 zig_expand_concat(zig_float_greater_builtin_, zig_has_##Type)(Type, gt) \
1637 zig_expand_concat(zig_float_greater_builtin_, zig_has_##Type)(Type, ge) \
1638 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, add, +) \
1639 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, sub, -) \
1640 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, mul, *) \
1641 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, div, /) \
1642 zig_extern zig_##Type zig_libc_name_##Type(sqrt)(zig_##Type); \
1643 zig_extern zig_##Type zig_libc_name_##Type(sin)(zig_##Type); \
1644 zig_extern zig_##Type zig_libc_name_##Type(cos)(zig_##Type); \
1645 zig_extern zig_##Type zig_libc_name_##Type(tan)(zig_##Type); \
1646 zig_extern zig_##Type zig_libc_name_##Type(exp)(zig_##Type); \
1647 zig_extern zig_##Type zig_libc_name_##Type(exp2)(zig_##Type); \
1648 zig_extern zig_##Type zig_libc_name_##Type(log)(zig_##Type); \
1649 zig_extern zig_##Type zig_libc_name_##Type(log2)(zig_##Type); \
1650 zig_extern zig_##Type zig_libc_name_##Type(log10)(zig_##Type); \
1651 zig_extern zig_##Type zig_libc_name_##Type(fabs)(zig_##Type); \
1652 zig_extern zig_##Type zig_libc_name_##Type(floor)(zig_##Type); \
1653 zig_extern zig_##Type zig_libc_name_##Type(ceil)(zig_##Type); \
1654 zig_extern zig_##Type zig_libc_name_##Type(round)(zig_##Type); \
1655 zig_extern zig_##Type zig_libc_name_##Type(trunc)(zig_##Type); \
1656 zig_extern zig_##Type zig_libc_name_##Type(fmod)(zig_##Type, zig_##Type); \
1657 zig_extern zig_##Type zig_libc_name_##Type(fmin)(zig_##Type, zig_##Type); \
1658 zig_extern zig_##Type zig_libc_name_##Type(fmax)(zig_##Type, zig_##Type); \
1659 zig_extern zig_##Type zig_libc_name_##Type(fma)(zig_##Type, zig_##Type, zig_##Type); \
1660\
1661 static inline zig_##Type zig_div_trunc_##Type(zig_##Type lhs, zig_##Type rhs) { \
1662 return zig_libc_name_##Type(trunc)(zig_div_##Type(lhs, rhs)); \
1663 } \
1664\
1665 static inline zig_##Type zig_div_floor_##Type(zig_##Type lhs, zig_##Type rhs) { \
1666 return zig_libc_name_##Type(floor)(zig_div_##Type(lhs, rhs)); \
1667 } \
1668\
1669 static inline zig_##Type zig_mod_##Type(zig_##Type lhs, zig_##Type rhs) { \
1670 return zig_sub_##Type(lhs, zig_mul_##Type(zig_div_floor_##Type(lhs, rhs), rhs)); \
1671 }
1672zig_float_builtins(f16)
1673zig_float_builtins(f32)
1674zig_float_builtins(f64)
1675zig_float_builtins(f80)
1676zig_float_builtins(f128)
1677zig_float_builtins(c_longdouble)
lib/zig.h created+1677
...@@ -0,0 +1,1677 @@
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 defined(__has_builtin)
10#define zig_has_builtin(builtin) __has_builtin(__builtin_##builtin)
11#else
12#define zig_has_builtin(builtin) 0
13#endif
14
15#if defined(__has_attribute)
16#define zig_has_attribute(attribute) __has_attribute(attribute)
17#else
18#define zig_has_attribute(attribute) 0
19#endif
20
21#if __STDC_VERSION__ >= 201112L
22#define zig_threadlocal _Thread_local
23#elif defined(__GNUC__)
24#define zig_threadlocal __thread
25#elif _MSC_VER
26#define zig_threadlocal __declspec(thread)
27#else
28#define zig_threadlocal zig_threadlocal_unavailable
29#endif
30
31#if zig_has_attribute(naked) || defined(__GNUC__)
32#define zig_naked __attribute__((naked))
33#elif defined(_MSC_VER)
34#define zig_naked __declspec(naked)
35#else
36#define zig_naked zig_naked_unavailable
37#endif
38
39#if zig_has_attribute(cold)
40#define zig_cold __attribute__((cold))
41#else
42#define zig_cold
43#endif
44
45#if __STDC_VERSION__ >= 199901L
46#define zig_restrict restrict
47#elif defined(__GNUC__)
48#define zig_restrict __restrict
49#else
50#define zig_restrict
51#endif
52
53#if __STDC_VERSION__ >= 201112L
54#define zig_align(alignment) _Alignas(alignment)
55#elif zig_has_attribute(aligned)
56#define zig_align(alignment) __attribute__((aligned(alignment)))
57#elif _MSC_VER
58#else
59#error the C compiler being used does not support aligning variables
60#endif
61
62#if zig_has_builtin(unreachable)
63#define zig_unreachable() __builtin_unreachable()
64#else
65#define zig_unreachable()
66#endif
67
68#if defined(__cplusplus)
69#define zig_extern extern "C"
70#else
71#define zig_extern extern
72#endif
73
74#if zig_has_builtin(debugtrap)
75#define zig_breakpoint() __builtin_debugtrap()
76#elif zig_has_builtin(trap)
77#define zig_breakpoint() __builtin_trap()
78#elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
79#define zig_breakpoint() __debugbreak()
80#elif defined(__i386__) || defined(__x86_64__)
81#define zig_breakpoint() __asm__ volatile("int $0x03");
82#else
83#define zig_breakpoint() raise(SIGTRAP)
84#endif
85
86#if zig_has_builtin(return_address)
87#define zig_return_address() __builtin_extract_return_addr(__builtin_return_address(0))
88#elif defined(_MSC_VER)
89#define zig_return_address() _ReturnAddress()
90#else
91#define zig_return_address() 0
92#endif
93
94#if zig_has_builtin(frame_address)
95#define zig_frame_address() __builtin_frame_address(0)
96#else
97#define zig_frame_address() 0
98#endif
99
100#if zig_has_builtin(prefetch)
101#define zig_prefetch(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
102#else
103#define zig_prefetch(addr, rw, locality)
104#endif
105
106#if zig_has_builtin(memory_size) && zig_has_builtin(memory_grow)
107#define zig_wasm_memory_size(index) __builtin_wasm_memory_size(index)
108#define zig_wasm_memory_grow(index, delta) __builtin_wasm_memory_grow(index, delta)
109#else
110#define zig_wasm_memory_size(index) zig_unimplemented()
111#define zig_wasm_memory_grow(index, delta) zig_unimplemented()
112#endif
113
114#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
115#include <stdatomic.h>
116#define zig_atomic(type) _Atomic(type)
117#define zig_cmpxchg_strong(obj, expected, desired, succ, fail) atomic_compare_exchange_strong_explicit(obj, &(expected), desired, succ, fail)
118#define zig_cmpxchg_weak(obj, expected, desired, succ, fail) atomic_compare_exchange_weak_explicit (obj, &(expected), desired, succ, fail)
119#define zig_atomicrmw_xchg(obj, arg, order) atomic_exchange_explicit (obj, arg, order)
120#define zig_atomicrmw_add(obj, arg, order) atomic_fetch_add_explicit (obj, arg, order)
121#define zig_atomicrmw_sub(obj, arg, order) atomic_fetch_sub_explicit (obj, arg, order)
122#define zig_atomicrmw_or(obj, arg, order) atomic_fetch_or_explicit (obj, arg, order)
123#define zig_atomicrmw_xor(obj, arg, order) atomic_fetch_xor_explicit (obj, arg, order)
124#define zig_atomicrmw_and(obj, arg, order) atomic_fetch_and_explicit (obj, arg, order)
125#define zig_atomicrmw_nand(obj, arg, order) __atomic_fetch_nand (obj, arg, order)
126#define zig_atomicrmw_min(obj, arg, order) __atomic_fetch_min (obj, arg, order)
127#define zig_atomicrmw_max(obj, arg, order) __atomic_fetch_max (obj, arg, order)
128#define zig_atomic_store(obj, arg, order) atomic_store_explicit (obj, arg, order)
129#define zig_atomic_load(obj, order) atomic_load_explicit (obj, order)
130#define zig_fence(order) atomic_thread_fence(order)
131#elif defined(__GNUC__)
132#define memory_order_relaxed __ATOMIC_RELAXED
133#define memory_order_consume __ATOMIC_CONSUME
134#define memory_order_acquire __ATOMIC_ACQUIRE
135#define memory_order_release __ATOMIC_RELEASE
136#define memory_order_acq_rel __ATOMIC_ACQ_REL
137#define memory_order_seq_cst __ATOMIC_SEQ_CST
138#define zig_atomic(type) type
139#define zig_cmpxchg_strong(obj, expected, desired, succ, fail) __atomic_compare_exchange_n(obj, &(expected), desired, zig_false, succ, fail)
140#define zig_cmpxchg_weak(obj, expected, desired, succ, fail) __atomic_compare_exchange_n(obj, &(expected), desired, zig_true , succ, fail)
141#define zig_atomicrmw_xchg(obj, arg, order) __atomic_exchange_n(obj, arg, order)
142#define zig_atomicrmw_add(obj, arg, order) __atomic_fetch_add (obj, arg, order)
143#define zig_atomicrmw_sub(obj, arg, order) __atomic_fetch_sub (obj, arg, order)
144#define zig_atomicrmw_or(obj, arg, order) __atomic_fetch_or (obj, arg, order)
145#define zig_atomicrmw_xor(obj, arg, order) __atomic_fetch_xor (obj, arg, order)
146#define zig_atomicrmw_and(obj, arg, order) __atomic_fetch_and (obj, arg, order)
147#define zig_atomicrmw_nand(obj, arg, order) __atomic_fetch_nand(obj, arg, order)
148#define zig_atomicrmw_min(obj, arg, order) __atomic_fetch_min (obj, arg, order)
149#define zig_atomicrmw_max(obj, arg, order) __atomic_fetch_max (obj, arg, order)
150#define zig_atomic_store(obj, arg, order) __atomic_store_n (obj, arg, order)
151#define zig_atomic_load(obj, order) __atomic_load_n (obj, order)
152#define zig_fence(order) __atomic_thread_fence(order)
153#else
154#define memory_order_relaxed 0
155#define memory_order_consume 1
156#define memory_order_acquire 2
157#define memory_order_release 3
158#define memory_order_acq_rel 4
159#define memory_order_seq_cst 5
160#define zig_atomic(type) type
161#define zig_cmpxchg_strong(obj, expected, desired, succ, fail) zig_unimplemented()
162#define zig_cmpxchg_weak(obj, expected, desired, succ, fail) zig_unimplemented()
163#define zig_atomicrmw_xchg(obj, arg, order) zig_unimplemented()
164#define zig_atomicrmw_add(obj, arg, order) zig_unimplemented()
165#define zig_atomicrmw_sub(obj, arg, order) zig_unimplemented()
166#define zig_atomicrmw_or(obj, arg, order) zig_unimplemented()
167#define zig_atomicrmw_xor(obj, arg, order) zig_unimplemented()
168#define zig_atomicrmw_and(obj, arg, order) zig_unimplemented()
169#define zig_atomicrmw_nand(obj, arg, order) zig_unimplemented()
170#define zig_atomicrmw_min(obj, arg, order) zig_unimplemented()
171#define zig_atomicrmw_max(obj, arg, order) zig_unimplemented()
172#define zig_atomic_store(obj, arg, order) zig_unimplemented()
173#define zig_atomic_load(obj, order) zig_unimplemented()
174#define zig_fence(order) zig_unimplemented()
175#endif
176
177#if __STDC_VERSION__ >= 201112L
178#define zig_noreturn _Noreturn void
179#elif zig_has_attribute(noreturn) || defined(__GNUC__)
180#define zig_noreturn __attribute__((noreturn)) void
181#elif _MSC_VER
182#define zig_noreturn __declspec(noreturn) void
183#else
184#define zig_noreturn void
185#endif
186
187#define zig_concat(lhs, rhs) lhs##rhs
188#define zig_expand_concat(lhs, rhs) zig_concat(lhs, rhs)
189
190#define zig_bitSizeOf(T) (CHAR_BIT * sizeof(T))
191
192typedef void zig_void;
193
194#if defined(__cplusplus)
195typedef bool zig_bool;
196#define zig_false false
197#define zig_true true
198#else
199#if __STDC_VERSION__ >= 199901L
200typedef _Bool zig_bool;
201#else
202typedef char zig_bool;
203#endif
204#define zig_false ((zig_bool)0)
205#define zig_true ((zig_bool)1)
206#endif
207
208typedef uintptr_t zig_usize;
209typedef intptr_t zig_isize;
210typedef signed short int zig_c_short;
211typedef unsigned short int zig_c_ushort;
212typedef signed int zig_c_int;
213typedef unsigned int zig_c_uint;
214typedef signed long int zig_c_long;
215typedef unsigned long int zig_c_ulong;
216typedef signed long long int zig_c_longlong;
217typedef unsigned long long int zig_c_ulonglong;
218
219typedef uint8_t zig_u8;
220typedef int8_t zig_i8;
221typedef uint16_t zig_u16;
222typedef int16_t zig_i16;
223typedef uint16_t zig_u16;
224typedef int16_t zig_i16;
225typedef uint32_t zig_u32;
226typedef int32_t zig_i32;
227typedef uint64_t zig_u64;
228typedef int64_t zig_i64;
229
230#define zig_as_u8(val) UINT8_C(val)
231#define zig_as_i8(val) INT8_C(val)
232#define zig_as_u16(val) UINT16_C(val)
233#define zig_as_i16(val) INT16_C(val)
234#define zig_as_u32(val) UINT32_C(val)
235#define zig_as_i32(val) INT32_C(val)
236#define zig_as_u64(val) UINT64_C(val)
237#define zig_as_i64(val) INT64_C(val)
238
239#define zig_minInt_u8 zig_as_u8(0)
240#define zig_maxInt_u8 UINT8_MAX
241#define zig_minInt_i8 INT8_MIN
242#define zig_maxInt_i8 INT8_MAX
243#define zig_minInt_u16 zig_as_u16(0)
244#define zig_maxInt_u16 UINT16_MAX
245#define zig_minInt_i16 INT16_MIN
246#define zig_maxInt_i16 INT16_MAX
247#define zig_minInt_u32 zig_as_u32(0)
248#define zig_maxInt_u32 UINT32_MAX
249#define zig_minInt_i32 INT32_MIN
250#define zig_maxInt_i32 INT32_MAX
251#define zig_minInt_u64 zig_as_u64(0)
252#define zig_maxInt_u64 UINT64_MAX
253#define zig_minInt_i64 INT64_MIN
254#define zig_maxInt_i64 INT64_MAX
255
256#define zig_compiler_rt_abbrev_u32 si
257#define zig_compiler_rt_abbrev_i32 si
258#define zig_compiler_rt_abbrev_u64 di
259#define zig_compiler_rt_abbrev_i64 di
260#define zig_compiler_rt_abbrev_u128 ti
261#define zig_compiler_rt_abbrev_i128 ti
262#define zig_compiler_rt_abbrev_f16 hf
263#define zig_compiler_rt_abbrev_f32 sf
264#define zig_compiler_rt_abbrev_f64 df
265#define zig_compiler_rt_abbrev_f80 xf
266#define zig_compiler_rt_abbrev_f128 tf
267
268zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, zig_usize);
269zig_extern void *memset (void *, int, zig_usize);
270
271/* ==================== 8/16/32/64-bit Integer Routines ===================== */
272
273#define zig_maxInt(Type, bits) zig_shr_##Type(zig_maxInt_##Type, (zig_bitSizeOf(zig_##Type) - bits))
274#define zig_expand_maxInt(Type, bits) zig_maxInt(Type, bits)
275#define zig_minInt(Type, bits) zig_not_##Type(zig_maxInt(Type, bits), bits)
276#define zig_expand_minInt(Type, bits) zig_minInt(Type, bits)
277
278#define zig_int_operator(Type, RhsType, operation, operator) \
279 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##RhsType rhs) { \
280 return lhs operator rhs; \
281 }
282#define zig_int_basic_operator(Type, operation, operator) \
283 zig_int_operator(Type, Type, operation, operator)
284#define zig_int_shift_operator(Type, operation, operator) \
285 zig_int_operator(Type, u8, operation, operator)
286#define zig_int_helpers(w) \
287 zig_int_basic_operator(u##w, and, &) \
288 zig_int_basic_operator(i##w, and, &) \
289 zig_int_basic_operator(u##w, or, |) \
290 zig_int_basic_operator(i##w, or, |) \
291 zig_int_basic_operator(u##w, xor, ^) \
292 zig_int_basic_operator(i##w, xor, ^) \
293 zig_int_shift_operator(u##w, shl, <<) \
294 zig_int_shift_operator(i##w, shl, <<) \
295 zig_int_shift_operator(u##w, shr, >>) \
296\
297 static inline zig_i##w zig_shr_i##w(zig_i##w lhs, zig_u8 rhs) { \
298 zig_i##w sign_mask = lhs < zig_as_i##w(0) ? -zig_as_i##w(1) : zig_as_i##w(0); \
299 return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \
300 } \
301\
302 static inline zig_u##w zig_not_u##w(zig_u##w val, zig_u8 bits) { \
303 return val ^ zig_maxInt(u##w, bits); \
304 } \
305\
306 static inline zig_i##w zig_not_i##w(zig_i##w val, zig_u8 bits) { \
307 (void)bits; \
308 return ~val; \
309 } \
310\
311 static inline zig_u##w zig_wrap_u##w(zig_u##w val, zig_u8 bits) { \
312 return val & zig_maxInt(u##w, bits); \
313 } \
314\
315 static inline zig_i##w zig_wrap_i##w(zig_i##w val, zig_u8 bits) { \
316 return (val & zig_as_u##w(1) << (bits - zig_as_u8(1))) != 0 \
317 ? val | zig_minInt(i##w, bits) : val & zig_maxInt(i##w, bits); \
318 } \
319\
320 zig_int_basic_operator(u##w, div_floor, /) \
321\
322 static inline zig_i##w zig_div_floor_i##w(zig_i##w lhs, zig_i##w rhs) { \
323 return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < zig_as_i##w(0)); \
324 } \
325\
326 zig_int_basic_operator(u##w, mod, %) \
327\
328 static inline zig_i##w zig_mod_i##w(zig_i##w lhs, zig_i##w rhs) { \
329 zig_i##w rem = lhs % rhs; \
330 return rem + (((lhs ^ rhs) & rem) < zig_as_i##w(0) ? rhs : zig_as_i##w(0)); \
331 }
332zig_int_helpers(8)
333zig_int_helpers(16)
334zig_int_helpers(32)
335zig_int_helpers(64)
336
337static inline zig_bool zig_addo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {
338#if zig_has_builtin(add_overflow)
339 zig_u32 full_res;
340 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
341 *res = zig_wrap_u32(full_res, bits);
342 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
343#else
344 *res = zig_addw_u32(lhs, rhs, bits);
345 return *res < lhs;
346#endif
347}
348
349zig_extern zig_i32 __addosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
350static inline zig_bool zig_addo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
351#if zig_has_builtin(add_overflow)
352 zig_i32 full_res;
353 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
354#else
355 zig_c_int overflow_int;
356 zig_u32 full_res = __addosi4(lhs, rhs, &overflow_int);
357 zig_bool overflow = overflow_int != 0;
358#endif
359 *res = zig_wrap_i32(full_res, bits);
360 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);
361}
362
363static inline zig_bool zig_addo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
364#if zig_has_builtin(add_overflow)
365 zig_u64 full_res;
366 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
367 *res = zig_wrap_u64(full_res, bits);
368 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
369#else
370 *res = zig_addw_u64(lhs, rhs, bits);
371 return *res < lhs;
372#endif
373}
374
375zig_extern zig_i64 __addodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
376static inline zig_bool zig_addo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
377#if zig_has_builtin(add_overflow)
378 zig_i64 full_res;
379 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
380#else
381 zig_c_int overflow_int;
382 zig_u64 full_res = __addodi4(lhs, rhs, &overflow_int);
383 zig_bool overflow = overflow_int != 0;
384#endif
385 *res = zig_wrap_i64(full_res, bits);
386 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
387}
388
389static inline zig_bool zig_addo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {
390#if zig_has_builtin(add_overflow)
391 zig_u8 full_res;
392 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
393 *res = zig_wrap_u8(full_res, bits);
394 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
395#else
396 return zig_addo_u32(res, lhs, rhs, bits);
397#endif
398}
399
400static inline zig_bool zig_addo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {
401#if zig_has_builtin(add_overflow)
402 zig_i8 full_res;
403 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
404 *res = zig_wrap_i8(full_res, bits);
405 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
406#else
407 return zig_addo_i32(res, lhs, rhs, bits);
408#endif
409}
410
411static inline zig_bool zig_addo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {
412#if zig_has_builtin(add_overflow)
413 zig_u16 full_res;
414 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
415 *res = zig_wrap_u16(full_res, bits);
416 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
417#else
418 return zig_addo_u32(res, lhs, rhs, bits);
419#endif
420}
421
422static inline zig_bool zig_addo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {
423#if zig_has_builtin(add_overflow)
424 zig_i16 full_res;
425 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
426 *res = zig_wrap_i16(full_res, bits);
427 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
428#else
429 return zig_addo_i32(res, lhs, rhs, bits);
430#endif
431}
432
433static inline zig_bool zig_subo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {
434#if zig_has_builtin(sub_overflow)
435 zig_u32 full_res;
436 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
437 *res = zig_wrap_u32(full_res, bits);
438 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
439#else
440 *res = zig_subw_u32(lhs, rhs, bits);
441 return *res > lhs;
442#endif
443}
444
445zig_extern zig_i32 __subosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
446static inline zig_bool zig_subo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
447#if zig_has_builtin(sub_overflow)
448 zig_i32 full_res;
449 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
450#else
451 zig_c_int overflow_int;
452 zig_u32 full_res = __subosi4(lhs, rhs, &overflow_int);
453 zig_bool overflow = overflow_int != 0;
454#endif
455 *res = zig_wrap_i32(full_res, bits);
456 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);
457}
458
459static inline zig_bool zig_subo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
460#if zig_has_builtin(sub_overflow)
461 zig_u64 full_res;
462 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
463 *res = zig_wrap_u64(full_res, bits);
464 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
465#else
466 *res = zig_subw_u64(lhs, rhs, bits);
467 return *res > lhs;
468#endif
469}
470
471zig_extern zig_i64 __subodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
472static inline zig_bool zig_subo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
473#if zig_has_builtin(sub_overflow)
474 zig_i64 full_res;
475 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
476#else
477 zig_c_int overflow_int;
478 zig_u64 full_res = __subodi4(lhs, rhs, &overflow_int);
479 zig_bool overflow = overflow_int != 0;
480#endif
481 *res = zig_wrap_i64(full_res, bits);
482 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
483}
484
485static inline zig_bool zig_subo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {
486#if zig_has_builtin(sub_overflow)
487 zig_u8 full_res;
488 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
489 *res = zig_wrap_u8(full_res, bits);
490 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
491#else
492 return zig_subo_u32(res, lhs, rhs, bits);
493#endif
494}
495
496static inline zig_bool zig_subo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {
497#if zig_has_builtin(sub_overflow)
498 zig_i8 full_res;
499 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
500 *res = zig_wrap_i8(full_res, bits);
501 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
502#else
503 return zig_subo_i32(res, lhs, rhs, bits);
504#endif
505}
506
507static inline zig_bool zig_subo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {
508#if zig_has_builtin(sub_overflow)
509 zig_u16 full_res;
510 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
511 *res = zig_wrap_u16(full_res, bits);
512 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
513#else
514 return zig_subo_u32(res, lhs, rhs, bits);
515#endif
516}
517
518static inline zig_bool zig_subo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {
519#if zig_has_builtin(sub_overflow)
520 zig_i16 full_res;
521 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
522 *res = zig_wrap_i16(full_res, bits);
523 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
524#else
525 return zig_subo_i32(res, lhs, rhs, bits);
526#endif
527}
528
529static inline zig_bool zig_mulo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) {
530#if zig_has_builtin(mul_overflow)
531 zig_u32 full_res;
532 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
533 *res = zig_wrap_u32(full_res, bits);
534 return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits);
535#else
536 *res = zig_mulw_u32(lhs, rhs, bits);
537 return rhs != zig_as_u32(0) && lhs > zig_maxInt(u32, bits) / rhs;
538#endif
539}
540
541zig_extern zig_i32 __mulosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
542static inline zig_bool zig_mulo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
543#if zig_has_builtin(mul_overflow)
544 zig_i32 full_res;
545 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
546#else
547 zig_c_int overflow_int;
548 zig_u32 full_res = __mulosi4(lhs, rhs, &overflow_int);
549 zig_bool overflow = overflow_int != 0;
550#endif
551 *res = zig_wrap_i32(full_res, bits);
552 return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits);
553}
554
555static inline zig_bool zig_mulo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) {
556#if zig_has_builtin(mul_overflow)
557 zig_u64 full_res;
558 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
559 *res = zig_wrap_u64(full_res, bits);
560 return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits);
561#else
562 *res = zig_mulw_u64(lhs, rhs, bits);
563 return rhs != zig_as_u64(0) && lhs > zig_maxInt(u64, bits) / rhs;
564#endif
565}
566
567zig_extern zig_i64 __mulodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
568static inline zig_bool zig_mulo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
569#if zig_has_builtin(mul_overflow)
570 zig_i64 full_res;
571 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
572#else
573 zig_c_int overflow_int;
574 zig_u64 full_res = __mulodi4(lhs, rhs, &overflow_int);
575 zig_bool overflow = overflow_int != 0;
576#endif
577 *res = zig_wrap_i64(full_res, bits);
578 return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits);
579}
580
581static inline zig_bool zig_mulo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) {
582#if zig_has_builtin(mul_overflow)
583 zig_u8 full_res;
584 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
585 *res = zig_wrap_u8(full_res, bits);
586 return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits);
587#else
588 return zig_mulo_u32(res, lhs, rhs, bits);
589#endif
590}
591
592static inline zig_bool zig_mulo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) {
593#if zig_has_builtin(mul_overflow)
594 zig_i8 full_res;
595 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
596 *res = zig_wrap_i8(full_res, bits);
597 return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits);
598#else
599 return zig_mulo_i32(res, lhs, rhs, bits);
600#endif
601}
602
603static inline zig_bool zig_mulo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) {
604#if zig_has_builtin(mul_overflow)
605 zig_u16 full_res;
606 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
607 *res = zig_wrap_u16(full_res, bits);
608 return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits);
609#else
610 return zig_mulo_u32(res, lhs, rhs, bits);
611#endif
612}
613
614static inline zig_bool zig_mulo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) {
615#if zig_has_builtin(mul_overflow)
616 zig_i16 full_res;
617 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
618 *res = zig_wrap_i16(full_res, bits);
619 return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits);
620#else
621 return zig_mulo_i32(res, lhs, rhs, bits);
622#endif
623}
624
625#define zig_int_builtins(w) \
626 static inline zig_u##w zig_shlw_u##w(zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \
627 return zig_wrap_u##w(zig_shl_u##w(lhs, rhs), bits); \
628 } \
629\
630 static inline zig_i##w zig_shlw_i##w(zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \
631 return zig_wrap_i##w((zig_i##w)zig_shl_u##w((zig_u##w)lhs, (zig_u##w)rhs), bits); \
632 } \
633\
634 static inline zig_u##w zig_addw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
635 return zig_wrap_u##w(lhs + rhs, bits); \
636 } \
637\
638 static inline zig_i##w zig_addw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
639 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs + (zig_u##w)rhs), bits); \
640 } \
641\
642 static inline zig_u##w zig_subw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
643 return zig_wrap_u##w(lhs - rhs, bits); \
644 } \
645\
646 static inline zig_i##w zig_subw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
647 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs - (zig_u##w)rhs), bits); \
648 } \
649\
650 static inline zig_u##w zig_mulw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
651 return zig_wrap_u##w(lhs * rhs, bits); \
652 } \
653\
654 static inline zig_i##w zig_mulw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
655 return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs * (zig_u##w)rhs), bits); \
656 } \
657\
658 static inline zig_bool zig_shlo_u##w(zig_u##w *res, zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \
659 *res = zig_shlw_u##w(lhs, rhs, bits); \
660 return (lhs & zig_maxInt_u##w << (bits - rhs)) != zig_as_u##w(0); \
661 } \
662\
663 static inline zig_bool zig_shlo_i##w(zig_i##w *res, zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \
664 *res = zig_shlw_i##w(lhs, rhs, bits); \
665 zig_i##w mask = (zig_i##w)(zig_maxInt_u##w << (bits - rhs - 1)); \
666 return (lhs & mask) != zig_as_i##w(0) && (lhs & mask) != mask; \
667 } \
668\
669 static inline zig_u##w zig_shls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
670 zig_u##w res; \
671 if (rhs >= bits) return lhs != zig_as_u##w(0) ? zig_maxInt(u##w, bits) : lhs; \
672 return zig_shlo_u##w(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u##w, bits) : res; \
673 } \
674\
675 static inline zig_i##w zig_shls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
676 zig_i##w res; \
677 if ((zig_u##w)rhs < (zig_u##w)bits && !zig_shlo_i##w(&res, lhs, rhs, bits)) return res; \
678 return lhs < zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
679 } \
680\
681 static inline zig_u##w zig_adds_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
682 zig_u##w res; \
683 return zig_addo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \
684 } \
685\
686 static inline zig_i##w zig_adds_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
687 zig_i##w res; \
688 if (!zig_addo_i##w(&res, lhs, rhs, bits)) return res; \
689 return res >= zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
690 } \
691\
692 static inline zig_u##w zig_subs_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
693 zig_u##w res; \
694 return zig_subo_u##w(&res, lhs, rhs, bits) ? zig_minInt(u##w, bits) : res; \
695 } \
696\
697 static inline zig_i##w zig_subs_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
698 zig_i##w res; \
699 if (!zig_subo_i##w(&res, lhs, rhs, bits)) return res; \
700 return res >= zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
701 } \
702\
703 static inline zig_u##w zig_muls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \
704 zig_u##w res; \
705 return zig_mulo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \
706 } \
707\
708 static inline zig_i##w zig_muls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \
709 zig_i##w res; \
710 if (!zig_mulo_i##w(&res, lhs, rhs, bits)) return res; \
711 return (lhs ^ rhs) < zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \
712 }
713zig_int_builtins(8)
714zig_int_builtins(16)
715zig_int_builtins(32)
716zig_int_builtins(64)
717
718#define zig_builtin8(name, val) __builtin_##name(val)
719typedef zig_c_uint zig_Builtin8;
720
721#define zig_builtin16(name, val) __builtin_##name(val)
722typedef zig_c_uint zig_Builtin16;
723
724#if INT_MIN <= INT32_MIN
725#define zig_builtin32(name, val) __builtin_##name(val)
726typedef zig_c_uint zig_Builtin32;
727#elif LONG_MIN <= INT32_MIN
728#define zig_builtin32(name, val) __builtin_##name##l(val)
729typedef zig_c_ulong zig_Builtin32;
730#endif
731
732#if INT_MIN <= INT64_MIN
733#define zig_builtin64(name, val) __builtin_##name(val)
734typedef zig_c_uint zig_Builtin64;
735#elif LONG_MIN <= INT64_MIN
736#define zig_builtin64(name, val) __builtin_##name##l(val)
737typedef zig_c_ulong zig_Builtin64;
738#elif LLONG_MIN <= INT64_MIN
739#define zig_builtin64(name, val) __builtin_##name##ll(val)
740typedef zig_c_ulonglong zig_Builtin64;
741#endif
742
743#if zig_has_builtin(clz)
744#define zig_builtin_clz(w) \
745 static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \
746 if (val == 0) return bits; \
747 return zig_builtin##w(clz, val) - (zig_bitSizeOf(zig_Builtin##w) - bits); \
748 } \
749\
750 static inline zig_u8 zig_clz_i##w(zig_i##w val, zig_u8 bits) { \
751 return zig_clz_u##w((zig_u##w)val, bits); \
752 }
753zig_builtin_clz(8)
754zig_builtin_clz(16)
755zig_builtin_clz(32)
756zig_builtin_clz(64)
757#endif
758
759#if zig_has_builtin(ctz)
760#define zig_builtin_ctz(w) \
761 static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \
762 if (val == 0) return bits; \
763 return zig_builtin##w(ctz, val); \
764 } \
765\
766 static inline zig_u8 zig_ctz_i##w(zig_i##w val, zig_u8 bits) { \
767 return zig_ctz_u##w((zig_u##w)val, bits); \
768 }
769zig_builtin_ctz(8)
770zig_builtin_ctz(16)
771zig_builtin_ctz(32)
772zig_builtin_ctz(64)
773#endif
774
775#if zig_has_builtin(popcount)
776#define zig_builtin_popcount(w) \
777 static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \
778 (void)bits; \
779 return zig_builtin##w(popcount, val); \
780 } \
781\
782 static inline zig_u8 zig_popcount_i##w(zig_i##w val, zig_u8 bits) { \
783 \
784 return zig_popcount_u##w((zig_u##w)val, bits); \
785 }
786zig_builtin_popcount(8)
787zig_builtin_popcount(16)
788zig_builtin_popcount(32)
789zig_builtin_popcount(64)
790#endif
791
792static inline zig_u8 zig_byte_swap_u8(zig_u8 val, zig_u8 bits) {
793 return zig_wrap_u8(val >> (8 - bits), bits);
794}
795
796static inline zig_i8 zig_byte_swap_i8(zig_i8 val, zig_u8 bits) {
797 return zig_wrap_i8((zig_i8)zig_byte_swap_u8((zig_u8)val, bits), bits);
798}
799
800static inline zig_u16 zig_byte_swap_u16(zig_u16 val, zig_u8 bits) {
801 zig_u16 full_res;
802#if zig_has_builtin(bswap16)
803 full_res = __builtin_bswap16(val);
804#else
805 full_res = (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 0)) << 8 |
806 (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 8)) >> 0;
807#endif
808 return zig_wrap_u16(full_res >> (16 - bits), bits);
809}
810
811static inline zig_i16 zig_byte_swap_i16(zig_i16 val, zig_u8 bits) {
812 return zig_wrap_i16((zig_i16)zig_byte_swap_u16((zig_u16)val, bits), bits);
813}
814
815static inline zig_u32 zig_byte_swap_u32(zig_u32 val, zig_u8 bits) {
816 zig_u32 full_res;
817#if zig_has_builtin(bswap32)
818 full_res = __builtin_bswap32(val);
819#else
820 full_res = (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 0)) << 16 |
821 (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 16)) >> 0;
822#endif
823 return zig_wrap_u32(full_res >> (32 - bits), bits);
824}
825
826static inline zig_i32 zig_byte_swap_i32(zig_i32 val, zig_u8 bits) {
827 return zig_wrap_i32((zig_i32)zig_byte_swap_u32((zig_u32)val, bits), bits);
828}
829
830static inline zig_u64 zig_byte_swap_u64(zig_u64 val, zig_u8 bits) {
831 zig_u64 full_res;
832#if zig_has_builtin(bswap64)
833 full_res = __builtin_bswap64(val);
834#else
835 full_res = (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 0)) << 32 |
836 (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 32)) >> 0;
837#endif
838 return zig_wrap_u64(full_res >> (64 - bits), bits);
839}
840
841static inline zig_i64 zig_byte_swap_i64(zig_i64 val, zig_u8 bits) {
842 return zig_wrap_i64((zig_i64)zig_byte_swap_u64((zig_u64)val, bits), bits);
843}
844
845static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) {
846 zig_u8 full_res;
847#if zig_has_builtin(bitreverse8)
848 full_res = __builtin_bitreverse8(val);
849#else
850 static zig_u8 const lut[0x10] = {
851 0b0000, 0b1000, 0b0100, 0b1100,
852 0b0010, 0b1010, 0b0110, 0b1110,
853 0b0001, 0b1001, 0b0101, 0b1101,
854 0b0011, 0b1011, 0b0111, 0b1111,
855 };
856 full_res = lut[val >> 0 & 0xF] << 4 | lut[val >> 4 & 0xF] << 0;
857#endif
858 return zig_wrap_u8(full_res >> (8 - bits), bits);
859}
860
861static inline zig_i8 zig_bit_reverse_i8(zig_i8 val, zig_u8 bits) {
862 return zig_wrap_i8((zig_i8)zig_bit_reverse_u8((zig_u8)val, bits), bits);
863}
864
865static inline zig_u16 zig_bit_reverse_u16(zig_u16 val, zig_u8 bits) {
866 zig_u16 full_res;
867#if zig_has_builtin(bitreverse16)
868 full_res = __builtin_bitreverse16(val);
869#else
870 full_res = (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 0), 8) << 8 |
871 (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 8), 8) >> 0;
872#endif
873 return zig_wrap_u16(full_res >> (16 - bits), bits);
874}
875
876static inline zig_i16 zig_bit_reverse_i16(zig_i16 val, zig_u8 bits) {
877 return zig_wrap_i16((zig_i16)zig_bit_reverse_u16((zig_u16)val, bits), bits);
878}
879
880static inline zig_u32 zig_bit_reverse_u32(zig_u32 val, zig_u8 bits) {
881 zig_u32 full_res;
882#if zig_has_builtin(bitreverse32)
883 full_res = __builtin_bitreverse32(val);
884#else
885 full_res = (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 0), 16) << 16 |
886 (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 16), 16) >> 0;
887#endif
888 return zig_wrap_u32(full_res >> (32 - bits), bits);
889}
890
891static inline zig_i32 zig_bit_reverse_i32(zig_i32 val, zig_u8 bits) {
892 return zig_wrap_i32((zig_i32)zig_bit_reverse_u32((zig_u32)val, bits), bits);
893}
894
895static inline zig_u64 zig_bit_reverse_u64(zig_u64 val, zig_u8 bits) {
896 zig_u64 full_res;
897#if zig_has_builtin(bitreverse64)
898 full_res = __builtin_bitreverse64(val);
899#else
900 full_res = (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 0), 32) << 32 |
901 (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 32), 32) >> 0;
902#endif
903 return zig_wrap_u64(full_res >> (64 - bits), bits);
904}
905
906static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) {
907 return zig_wrap_i64((zig_i64)zig_bit_reverse_u64((zig_u64)val, bits), bits);
908}
909
910/* ======================== 128-bit Integer Routines ======================== */
911
912#if !defined(zig_has_int128)
913# if defined(__SIZEOF_INT128__)
914# define zig_has_int128 1
915# else
916# define zig_has_int128 0
917# endif
918#endif
919
920#if zig_has_int128
921
922typedef unsigned __int128 zig_u128;
923typedef signed __int128 zig_i128;
924
925#define zig_as_u128(hi, lo) ((zig_u128)(hi)<<64|(lo))
926#define zig_as_i128(hi, lo) ((zig_i128)zig_as_u128(hi, lo))
927#define zig_hi_u128(val) ((zig_u64)((val) >> 64))
928#define zig_lo_u128(val) ((zig_u64)((val) >> 0))
929#define zig_hi_i128(val) ((zig_i64)((val) >> 64))
930#define zig_lo_i128(val) ((zig_u64)((val) >> 0))
931#define zig_bitcast_u128(val) ((zig_u128)(val))
932#define zig_bitcast_i128(val) ((zig_i128)(val))
933#define zig_cmp_int128(Type) \
934 static inline zig_i8 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
935 return (zig_i8)((lhs > rhs) - (lhs < rhs)); \
936 }
937#define zig_bit_int128(Type, operation, operator) \
938 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
939 return lhs operator rhs; \
940 }
941
942#else /* zig_has_int128 */
943
944#if __LITTLE_ENDIAN__ || _MSC_VER
945typedef struct { zig_align(16) zig_u64 lo; zig_u64 hi; } zig_u128;
946typedef struct { zig_align(16) zig_u64 lo; zig_i64 hi; } zig_i128;
947#else
948typedef struct { zig_align(16) zig_u64 hi; zig_u64 lo; } zig_u128;
949typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128;
950#endif
951
952#define zig_as_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) })
953#define zig_as_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) })
954#define zig_hi_u128(val) ((val).hi)
955#define zig_lo_u128(val) ((val).lo)
956#define zig_hi_i128(val) ((val).hi)
957#define zig_lo_i128(val) ((val).lo)
958#define zig_bitcast_u128(val) zig_as_u128((zig_u64)(val).hi, (val).lo)
959#define zig_bitcast_i128(val) zig_as_i128((zig_i64)(val).hi, (val).lo)
960#define zig_cmp_int128(Type) \
961 static inline zig_i8 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \
962 return (lhs.hi == rhs.hi) \
963 ? (lhs.lo > rhs.lo) - (lhs.lo < rhs.lo) \
964 : (lhs.hi > rhs.hi) - (lhs.hi < rhs.hi); \
965 }
966#define zig_bit_int128(Type, operation, operator) \
967 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
968 return (zig_##Type){ .hi = lhs.hi operator rhs.hi, .lo = lhs.lo operator rhs.lo }; \
969 }
970
971#endif /* zig_has_int128 */
972
973#define zig_minInt_u128 zig_as_u128(zig_minInt_u64, zig_minInt_u64)
974#define zig_maxInt_u128 zig_as_u128(zig_maxInt_u64, zig_maxInt_u64)
975#define zig_minInt_i128 zig_as_i128(zig_minInt_i64, zig_minInt_u64)
976#define zig_maxInt_i128 zig_as_i128(zig_maxInt_i64, zig_maxInt_u64)
977
978zig_cmp_int128(u128)
979zig_cmp_int128(i128)
980
981zig_bit_int128(u128, and, &)
982zig_bit_int128(i128, and, &)
983
984zig_bit_int128(u128, or, |)
985zig_bit_int128(i128, or, |)
986
987zig_bit_int128(u128, xor, ^)
988zig_bit_int128(i128, xor, ^)
989
990static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs);
991
992#if zig_has_int128
993
994static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) {
995 return val ^ zig_maxInt(u128, bits);
996}
997
998static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) {
999 (void)bits;
1000 return ~val;
1001}
1002
1003static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) {
1004 return lhs >> rhs;
1005}
1006
1007static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) {
1008 return lhs << rhs;
1009}
1010
1011static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) {
1012 return lhs << rhs;
1013}
1014
1015static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) {
1016 return lhs + rhs;
1017}
1018
1019static inline zig_i128 zig_add_i128(zig_i128 lhs, zig_i128 rhs) {
1020 return lhs + rhs;
1021}
1022
1023static inline zig_u128 zig_sub_u128(zig_u128 lhs, zig_u128 rhs) {
1024 return lhs - rhs;
1025}
1026
1027static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) {
1028 return lhs - rhs;
1029}
1030
1031static inline zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) {
1032 return lhs * rhs;
1033}
1034
1035static inline zig_i128 zig_mul_i128(zig_i128 lhs, zig_i128 rhs) {
1036 return lhs * rhs;
1037}
1038
1039static inline zig_u128 zig_div_trunc_u128(zig_u128 lhs, zig_u128 rhs) {
1040 return lhs / rhs;
1041}
1042
1043static inline zig_i128 zig_div_trunc_i128(zig_i128 lhs, zig_i128 rhs) {
1044 return lhs / rhs;
1045}
1046
1047static inline zig_u128 zig_rem_u128(zig_u128 lhs, zig_u128 rhs) {
1048 return lhs % rhs;
1049}
1050
1051static inline zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) {
1052 return lhs % rhs;
1053}
1054
1055static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) {
1056 return zig_div_trunc_i128(lhs, rhs) - (((lhs ^ rhs) & zig_rem_i128(lhs, rhs)) < zig_as_i128(0, 0));
1057}
1058
1059static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
1060 zig_i128 rem = zig_rem_i128(lhs, rhs);
1061 return rem + (((lhs ^ rhs) & rem) < zig_as_i128(0, 0) ? rhs : zig_as_i128(0, 0));
1062}
1063
1064#else /* zig_has_int128 */
1065
1066static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) {
1067 return (zig_u128){ .hi = zig_not_u64(val.hi, bits - zig_as_u8(64)), .lo = zig_not_u64(val.lo, zig_as_u8(64)) };
1068}
1069
1070static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) {
1071 return (zig_i128){ .hi = zig_not_i64(val.hi, bits - zig_as_u8(64)), .lo = zig_not_u64(val.lo, zig_as_u8(64)) };
1072}
1073
1074static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) {
1075 if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = lhs.hi << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 };
1076 return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };
1077}
1078
1079static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) {
1080 if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = lhs.hi << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 };
1081 return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };
1082}
1083
1084static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) {
1085 if (rhs >= zig_as_u8(64)) return (zig_i128){ .hi = lhs.hi << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 };
1086 return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };
1087}
1088
1089static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) {
1090 zig_u128 res;
1091 res.hi = lhs.hi + rhs.hi + zig_addo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64);
1092 return res;
1093}
1094
1095static inline zig_i128 zig_add_i128(zig_i128 lhs, zig_i128 rhs) {
1096 zig_i128 res;
1097 res.hi = lhs.hi + rhs.hi + zig_addo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64);
1098 return res;
1099}
1100
1101static inline zig_u128 zig_sub_u128(zig_u128 lhs, zig_u128 rhs) {
1102 zig_u128 res;
1103 res.hi = lhs.hi - rhs.hi - zig_subo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64);
1104 return res;
1105}
1106
1107static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) {
1108 zig_i128 res;
1109 res.hi = lhs.hi - rhs.hi - zig_subo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64);
1110 return res;
1111}
1112
1113static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) {
1114 return zig_sub_i128(zig_div_trunc_i128(lhs, rhs), (((lhs.hi ^ rhs.hi) & zig_rem_i128(lhs, rhs).hi) < zig_as_i64(0)) ? zig_as_i128(0, 1) : zig_as_i128(0, 0));
1115}
1116
1117static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
1118 zig_i128 rem = zig_rem_i128(lhs, rhs);
1119 return rem + (((lhs.hi ^ rhs.hi) & rem.hi) < zig_as_i64(0) ? rhs : zig_as_i128(0, 0));
1120}
1121
1122#endif /* zig_has_int128 */
1123
1124#define zig_div_floor_u128 zig_div_trunc_u128
1125#define zig_mod_u128 zig_rem_u128
1126
1127static inline zig_u128 zig_min_u128(zig_u128 lhs, zig_u128 rhs) {
1128 return zig_cmp_u128(lhs, rhs) < 0 ? lhs : rhs;
1129}
1130
1131static inline zig_i128 zig_min_i128(zig_i128 lhs, zig_i128 rhs) {
1132 return zig_cmp_i128(lhs, rhs) < 0 ? lhs : rhs;
1133}
1134
1135static inline zig_u128 zig_max_u128(zig_u128 lhs, zig_u128 rhs) {
1136 return zig_cmp_u128(lhs, rhs) > 0 ? lhs : rhs;
1137}
1138
1139static inline zig_i128 zig_max_i128(zig_i128 lhs, zig_i128 rhs) {
1140 return zig_cmp_i128(lhs, rhs) > 0 ? lhs : rhs;
1141}
1142
1143static inline zig_i128 zig_shr_i128(zig_i128 lhs, zig_u8 rhs) {
1144 zig_i128 sign_mask = zig_cmp_i128(lhs, zig_as_i128(0, 0)) < zig_as_i8(0) ? -zig_as_i128(0, 1) : zig_as_i128(0, 0);
1145 return zig_xor_i128(zig_bitcast_i128(zig_shr_u128(zig_bitcast_u128(zig_xor_i128(lhs, sign_mask)), rhs)), sign_mask);
1146}
1147
1148static inline zig_u128 zig_wrap_u128(zig_u128 val, zig_u8 bits) {
1149 return zig_and_u128(val, zig_maxInt(u128, bits));
1150}
1151
1152static inline zig_i128 zig_wrap_i128(zig_i128 val, zig_u8 bits) {
1153 return zig_as_i128(zig_wrap_i64(zig_hi_i128(val), bits - zig_as_u8(64)), zig_lo_i128(val));
1154}
1155
1156static inline zig_u128 zig_shlw_u128(zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {
1157 return zig_wrap_u128(zig_shl_u128(lhs, rhs), bits);
1158}
1159
1160static inline zig_i128 zig_shlw_i128(zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {
1161 return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1162}
1163
1164static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {
1165 return zig_wrap_u128(zig_add_u128(lhs, rhs), bits);
1166}
1167
1168static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1169 return zig_wrap_i128(zig_bitcast_i128(zig_add_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1170}
1171
1172static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1173 return zig_wrap_u128(zig_sub_u128(lhs, rhs), bits);
1174}
1175
1176static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1177 return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1178}
1179
1180static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1181 return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits);
1182}
1183
1184static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1185 return zig_wrap_i128(zig_bitcast_i128(zig_mul_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1186}
1187
1188#if zig_has_int128
1189
1190static inline zig_bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {
1191 *res = zig_shlw_u128(lhs, rhs, bits);
1192 return zig_and_u128(lhs, zig_shl_u128(zig_maxInt_u128, bits - rhs)) != zig_as_u128(0, 0);
1193}
1194
1195static inline zig_bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {
1196 *res = zig_shlw_i128(lhs, rhs, bits);
1197 zig_i128 mask = zig_bitcast_i128(zig_shl_u128(zig_maxInt_u128, bits - rhs - zig_as_u8(1)));
1198 return zig_cmp_i128(zig_and_i128(lhs, mask), zig_as_i128(0, 0)) != zig_as_i8(0) &&
1199 zig_cmp_i128(zig_and_i128(lhs, mask), mask) != zig_as_i8(0);
1200}
1201
1202static inline zig_bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1203#if zig_has_builtin(add_overflow)
1204 zig_u128 full_res;
1205 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
1206 *res = zig_wrap_u128(full_res, bits);
1207 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1208#else
1209 *res = zig_addw_u128(lhs, rhs, bits);
1210 return *res < lhs;
1211#endif
1212}
1213
1214zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1215static inline zig_bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1216#if zig_has_builtin(add_overflow)
1217 zig_i128 full_res;
1218 zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res);
1219#else
1220 zig_c_int overflow_int;
1221 zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int);
1222 zig_bool overflow = overflow_int != 0;
1223#endif
1224 *res = zig_wrap_i128(full_res, bits);
1225 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1226}
1227
1228static inline zig_bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1229#if zig_has_builtin(sub_overflow)
1230 zig_u128 full_res;
1231 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
1232 *res = zig_wrap_u128(full_res, bits);
1233 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1234#else
1235 *res = zig_subw_u128(lhs, rhs, bits);
1236 return *res > lhs;
1237#endif
1238}
1239
1240zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1241static inline zig_bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1242#if zig_has_builtin(sub_overflow)
1243 zig_i128 full_res;
1244 zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res);
1245#else
1246 zig_c_int overflow_int;
1247 zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int);
1248 zig_bool overflow = overflow_int != 0;
1249#endif
1250 *res = zig_wrap_i128(full_res, bits);
1251 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1252}
1253
1254static inline zig_bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1255#if zig_has_builtin(mul_overflow)
1256 zig_u128 full_res;
1257 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
1258 *res = zig_wrap_u128(full_res, bits);
1259 return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits);
1260#else
1261 *res = zig_mulw_u128(lhs, rhs, bits);
1262 return rhs != zig_as_u128(0, 0) && lhs > zig_maxInt(u128, bits) / rhs;
1263#endif
1264}
1265
1266zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1267static inline zig_bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1268#if zig_has_builtin(mul_overflow)
1269 zig_i128 full_res;
1270 zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res);
1271#else
1272 zig_c_int overflow_int;
1273 zig_i128 full_res = __muloti4(lhs, rhs, &overflow);
1274 zig_bool overflow = overflow_int != 0;
1275#endif
1276 *res = zig_wrap_i128(full_res, bits);
1277 return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits);
1278}
1279
1280#else /* zig_has_int128 */
1281
1282static inline zig_bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs) {
1283 return zig_addo_u64(&res->hi, lhs.hi, rhs.hi, UINT64_MAX) |
1284 zig_addo_u64(&res->hi, res->hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, UINT64_MAX));
1285}
1286
1287static inline zig_bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs) {
1288 return zig_subo_u64(&res->hi, lhs.hi, rhs.hi, UINT64_MAX) |
1289 zig_subo_u64(&res->hi, res->hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, UINT64_MAX));
1290}
1291
1292#endif /* zig_has_int128 */
1293
1294static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1295 zig_u128 res;
1296 if (zig_cmp_u128(rhs, zig_as_u128(0, bits)) >= zig_as_i8(0))
1297 return zig_cmp_u128(lhs, zig_as_u128(0, 0)) != zig_as_i8(0) ? zig_maxInt(u128, bits) : lhs;
1298 return zig_shlo_u128(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u128, bits) : res;
1299}
1300
1301static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1302 zig_i128 res;
1303 if (zig_cmp_u128(zig_bitcast_u128(rhs), zig_as_u128(0, bits)) < zig_as_i8(0) && !zig_shlo_i128(&res, lhs, rhs, bits)) return res;
1304 return zig_cmp_i128(lhs, zig_as_i128(0, 0)) < zig_as_i8(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1305}
1306
1307static inline zig_u128 zig_adds_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1308 zig_u128 res;
1309 return zig_addo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res;
1310}
1311
1312static inline zig_i128 zig_adds_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1313 zig_i128 res;
1314 if (!zig_addo_i128(&res, lhs, rhs, bits)) return res;
1315 return zig_cmp_i128(res, zig_as_i128(0, 0)) >= zig_as_i8(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1316}
1317
1318static inline zig_u128 zig_subs_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1319 zig_u128 res;
1320 return zig_subo_u128(&res, lhs, rhs, bits) ? zig_minInt(u128, bits) : res;
1321}
1322
1323static inline zig_i128 zig_subs_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1324 zig_i128 res;
1325 if (!zig_subo_i128(&res, lhs, rhs, bits)) return res;
1326 return zig_cmp_i128(res, zig_as_i128(0, 0)) >= zig_as_i8(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1327}
1328
1329static inline zig_u128 zig_muls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
1330 zig_u128 res;
1331 return zig_mulo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res;
1332}
1333
1334static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
1335 zig_i128 res;
1336 if (!zig_mulo_i128(&res, lhs, rhs, bits)) return res;
1337 return zig_cmp_i128(zig_xor_i128(lhs, rhs), zig_as_i128(0, 0)) < zig_as_i8(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits);
1338}
1339
1340static inline zig_u8 zig_clz_u128(zig_u128 val, zig_u8 bits) {
1341 if (zig_hi_u128(val) != 0) return zig_clz_u64(zig_hi_u128(val), bits - zig_as_u8(64));
1342 return zig_clz_u64(zig_lo_u128(val), zig_as_u8(64)) + zig_as_u8(64);
1343}
1344
1345static inline zig_u8 zig_clz_i128(zig_i128 val, zig_u8 bits) {
1346 return zig_clz_u128(zig_bitcast_u128(val), bits);
1347}
1348
1349static inline zig_u8 zig_ctz_u128(zig_u128 val, zig_u8 bits) {
1350 if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), zig_as_u8(64));
1351 return zig_ctz_u64(zig_hi_u128(val), bits - zig_as_u8(64)) + zig_as_u8(64);
1352}
1353
1354static inline zig_u8 zig_ctz_i128(zig_i128 val, zig_u8 bits) {
1355 return zig_ctz_u128(zig_bitcast_u128(val), bits);
1356}
1357
1358static inline zig_u8 zig_popcount_u128(zig_u128 val, zig_u8 bits) {
1359 return zig_popcount_u64(zig_hi_u128(val), bits - zig_as_u8(64)) +
1360 zig_popcount_u64(zig_lo_u128(val), zig_as_u8(64));
1361}
1362
1363static inline zig_u8 zig_popcount_i128(zig_i128 val, zig_u8 bits) {
1364 return zig_popcount_u128(zig_bitcast_u128(val), bits);
1365}
1366
1367static inline zig_u128 zig_byte_swap_u128(zig_u128 val, zig_u8 bits) {
1368 zig_u128 full_res;
1369#if zig_has_builtin(bswap128)
1370 full_res = __builtin_bswap128(val);
1371#else
1372 full_res = zig_as_u128(zig_byte_swap_u64(zig_lo_u128(val), zig_as_u8(64)),
1373 zig_byte_swap_u64(zig_hi_u128(val), zig_as_u8(64)));
1374#endif
1375 return zig_shr_u128(full_res, zig_as_u8(128) - bits);
1376}
1377
1378static inline zig_i128 zig_byte_swap_i128(zig_i128 val, zig_u8 bits) {
1379 return zig_byte_swap_u128(zig_bitcast_u128(val), bits);
1380}
1381
1382static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, zig_u8 bits) {
1383 return zig_shr_u128(zig_as_u128(zig_bit_reverse_u64(zig_lo_u128(val), zig_as_u8(64)),
1384 zig_bit_reverse_u64(zig_hi_u128(val), zig_as_u8(64))),
1385 zig_as_u8(128) - bits);
1386}
1387
1388static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) {
1389 return zig_bit_reverse_u128(zig_bitcast_u128(val), bits);
1390}
1391
1392/* ========================= Floating Point Support ========================= */
1393
1394#define zig_has_f16 1
1395#define zig_bitSizeOf_f16 16
1396#define zig_libc_name_f16(name) __##name##h
1397#define zig_as_special_f16(sign, name, arg, repr) sign zig_as_f16(__builtin_##name, )(arg)
1398#if FLT_MANT_DIG == 11
1399typedef float zig_f16;
1400#define zig_as_f16(fp, repr) fp##f
1401#elif DBL_MANT_DIG == 11
1402typedef double zig_f16;
1403#define zig_as_f16(fp, repr) fp
1404#elif LDBL_MANT_DIG == 11
1405#define zig_bitSizeOf_c_longdouble 16
1406typedef long double zig_f16;
1407#define zig_as_f16(fp, repr) fp##l
1408#elif FLT16_MANT_DIG == 11
1409typedef _Float16 zig_f16;
1410#define zig_as_f16(fp, repr) fp##f16
1411#elif defined(__SIZEOF_FP16__)
1412typedef __fp16 zig_f16;
1413#define zig_as_f16(fp, repr) fp##f16
1414#else
1415#undef zig_has_f16
1416#define zig_has_f16 0
1417#define zig_repr_f16 i16
1418typedef zig_i16 zig_f16;
1419#define zig_as_f16(fp, repr) repr
1420#undef zig_as_special_f16
1421#define zig_as_special_f16(sign, name, arg, repr) repr
1422#endif
1423
1424#define zig_has_f32 1
1425#define zig_bitSizeOf_f32 32
1426#define zig_libc_name_f32(name) name##f
1427#define zig_as_special_f32(sign, name, arg, repr) sign zig_as_f32(__builtin_##name, )(arg)
1428#if FLT_MANT_DIG == 24
1429typedef float zig_f32;
1430#define zig_as_f32(fp, repr) fp##f
1431#elif DBL_MANT_DIG == 24
1432typedef double zig_f32;
1433#define zig_as_f32(fp, repr) fp
1434#elif LDBL_MANT_DIG == 24
1435#define zig_bitSizeOf_c_longdouble 32
1436typedef long double zig_f32;
1437#define zig_as_f32(fp, repr) fp##l
1438#elif FLT32_MANT_DIG == 24
1439typedef _Float32 zig_f32;
1440#define zig_as_f32(fp, repr) fp##f32
1441#else
1442#undef zig_has_f32
1443#define zig_has_f32 0
1444#define zig_repr_f32 i32
1445typedef zig_i32 zig_f32;
1446#define zig_as_f32(fp, repr) repr
1447#undef zig_as_special_f32
1448#define zig_as_special_f32(sign, name, arg, repr) repr
1449#endif
1450
1451#define zig_has_f64 1
1452#define zig_bitSizeOf_f64 64
1453#define zig_libc_name_f64(name) name
1454#define zig_as_special_f64(sign, name, arg, repr) sign zig_as_f64(__builtin_##name, )(arg)
1455#if FLT_MANT_DIG == 53
1456typedef float zig_f64;
1457#define zig_as_f64(fp, repr) fp##f
1458#elif DBL_MANT_DIG == 53
1459typedef double zig_f64;
1460#define zig_as_f64(fp, repr) fp
1461#elif LDBL_MANT_DIG == 53
1462#define zig_bitSizeOf_c_longdouble 64
1463typedef long double zig_f64;
1464#define zig_as_f64(fp, repr) fp##l
1465#elif FLT64_MANT_DIG == 53
1466typedef _Float64 zig_f64;
1467#define zig_as_f64(fp, repr) fp##f64
1468#elif FLT32X_MANT_DIG == 53
1469typedef _Float32x zig_f64;
1470#define zig_as_f64(fp, repr) fp##f32x
1471#else
1472#undef zig_has_f64
1473#define zig_has_f64 0
1474#define zig_repr_f64 i64
1475typedef zig_i64 zig_f64;
1476#define zig_as_f64(fp, repr) repr
1477#undef zig_as_special_f64
1478#define zig_as_special_f64(sign, name, arg, repr) repr
1479#endif
1480
1481#define zig_has_f80 1
1482#define zig_bitSizeOf_f80 80
1483#define zig_libc_name_f80(name) __##name##x
1484#define zig_as_special_f80(sign, name, arg, repr) sign zig_as_f80(__builtin_##name, )(arg)
1485#if FLT_MANT_DIG == 64
1486typedef float zig_f80;
1487#define zig_as_f80(fp, repr) fp##f
1488#elif DBL_MANT_DIG == 64
1489typedef double zig_f80;
1490#define zig_as_f80(fp, repr) fp
1491#elif LDBL_MANT_DIG == 64
1492#define zig_bitSizeOf_c_longdouble 80
1493typedef long double zig_f80;
1494#define zig_as_f80(fp, repr) fp##l
1495#elif FLT80_MANT_DIG == 64
1496typedef _Float80 zig_f80;
1497#define zig_as_f80(fp, repr) fp##f80
1498#elif FLT64X_MANT_DIG == 64
1499typedef _Float64x zig_f80;
1500#define zig_as_f80(fp, repr) fp##f64x
1501#elif defined(__SIZEOF_FLOAT80__)
1502typedef __float80 zig_f80;
1503#define zig_as_f80(fp, repr) fp##l
1504#else
1505#undef zig_has_f80
1506#define zig_has_f80 0
1507#define zig_repr_f80 i128
1508typedef zig_i128 zig_f80;
1509#define zig_as_f80(fp, repr) repr
1510#undef zig_as_special_f80
1511#define zig_as_special_f80(sign, name, arg, repr) repr
1512#endif
1513
1514#define zig_has_f128 1
1515#define zig_bitSizeOf_f128 128
1516#define zig_libc_name_f128(name) name##q
1517#define zig_as_special_f128(sign, name, arg, repr) sign zig_as_f128(__builtin_##name, )(arg)
1518#if FLT_MANT_DIG == 113
1519typedef float zig_f128;
1520#define zig_as_f128(fp, repr) fp##f
1521#elif DBL_MANT_DIG == 113
1522typedef double zig_f128;
1523#define zig_as_f128(fp, repr) fp
1524#elif LDBL_MANT_DIG == 113
1525#define zig_bitSizeOf_c_longdouble 128
1526typedef long double zig_f128;
1527#define zig_as_f128(fp, repr) fp##l
1528#elif FLT128_MANT_DIG == 113
1529typedef _Float128 zig_f128;
1530#define zig_as_f128(fp, repr) fp##f128
1531#elif FLT64X_MANT_DIG == 113
1532typedef _Float64x zig_f128;
1533#define zig_as_f128(fp, repr) fp##f64x
1534#elif defined(__SIZEOF_FLOAT128__)
1535typedef __float128 zig_f128;
1536#define zig_as_f128(fp, repr) fp##q
1537#undef zig_as_special_f128
1538#define zig_as_special_f128(sign, name, arg, repr) sign __builtin_##name##f128(arg)
1539#else
1540#undef zig_has_f128
1541#define zig_has_f128 0
1542#define zig_repr_f128 i128
1543typedef zig_i128 zig_f128;
1544#define zig_as_f128(fp, repr) repr
1545#undef zig_as_special_f128
1546#define zig_as_special_f128(sign, name, arg, repr) repr
1547#endif
1548
1549#define zig_has_c_longdouble 1
1550typedef long double zig_c_longdouble;
1551#define zig_as_c_longdouble(fp, repr) fp##l
1552#define zig_libc_name_c_longdouble(name) name##l
1553#define zig_as_special_c_longdouble(sign, name, arg, repr) sign __builtin_##name##l(arg)
1554
1555#define zig_convert_builtin(ResType, operation, ArgType, version) \
1556 zig_extern zig_##ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \
1557 zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(zig_##ArgType);
1558zig_convert_builtin(f16, trunc, f32, 2)
1559zig_convert_builtin(f16, trunc, f64, 2)
1560zig_convert_builtin(f16, trunc, f80, 2)
1561zig_convert_builtin(f16, trunc, f128, 2)
1562zig_convert_builtin(f32, extend, f16, 2)
1563zig_convert_builtin(f32, trunc, f64, 2)
1564zig_convert_builtin(f32, trunc, f80, 2)
1565zig_convert_builtin(f32, trunc, f128, 2)
1566zig_convert_builtin(f64, extend, f16, 2)
1567zig_convert_builtin(f64, extend, f32, 2)
1568zig_convert_builtin(f64, trunc, f80, 2)
1569zig_convert_builtin(f64, trunc, f128, 2)
1570zig_convert_builtin(f80, extend, f16, 2)
1571zig_convert_builtin(f80, extend, f32, 2)
1572zig_convert_builtin(f80, extend, f64, 2)
1573zig_convert_builtin(f80, trunc, f128, 2)
1574zig_convert_builtin(f128, extend, f16, 2)
1575zig_convert_builtin(f128, extend, f32, 2)
1576zig_convert_builtin(f128, extend, f64, 2)
1577zig_convert_builtin(f128, extend, f80, 2)
1578
1579#define zig_float_negate_builtin_0(Type) \
1580 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
1581 return zig_expand_concat(zig_xor_, zig_repr_##Type)(arg, zig_expand_minInt(zig_repr_##Type, zig_bitSizeOf_##Type)); \
1582 }
1583#define zig_float_negate_builtin_1(Type) \
1584 static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \
1585 return -arg; \
1586 }
1587
1588#define zig_float_less_builtin_0(Type, operation) \
1589 zig_extern zig_i8 zig_expand_concat(zig_expand_concat(__##operation, \
1590 zig_compiler_rt_abbrev_##Type), 2)(zig_##Type, zig_##Type); \
1591 static inline zig_i8 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1592 return (zig_i8)zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_##Type), 2)(lhs, rhs); \
1593 }
1594#define zig_float_less_builtin_1(Type, operation) \
1595 static inline zig_i8 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1596 return (zig_i8)(!(lhs <= rhs) - (lhs < rhs)); \
1597 }
1598
1599#define zig_float_greater_builtin_0(Type, operation) \
1600 zig_float_less_builtin_0(Type, operation)
1601#define zig_float_greater_builtin_1(Type, operation) \
1602 static inline zig_i8 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1603 return (zig_i8)((lhs > rhs) - !(lhs >= rhs)); \
1604 }
1605
1606#define zig_float_binary_builtin_0(Type, operation, operator) \
1607 zig_extern zig_##Type zig_expand_concat(zig_expand_concat(__##operation, \
1608 zig_compiler_rt_abbrev_##Type), 3)(zig_##Type, zig_##Type); \
1609 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1610 return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_##Type), 3)(lhs, rhs); \
1611 }
1612#define zig_float_binary_builtin_1(Type, operation, operator) \
1613 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
1614 return lhs operator rhs; \
1615 }
1616
1617#define zig_float_builtins(Type) \
1618 zig_convert_builtin(i32, fix, Type, ) \
1619 zig_convert_builtin(u32, fixuns, Type, ) \
1620 zig_convert_builtin(i64, fix, Type, ) \
1621 zig_convert_builtin(u64, fixuns, Type, ) \
1622 zig_convert_builtin(i128, fix, Type, ) \
1623 zig_convert_builtin(u128, fixuns, Type, ) \
1624 zig_convert_builtin(Type, float, i32, ) \
1625 zig_convert_builtin(Type, floatun, u32, ) \
1626 zig_convert_builtin(Type, float, i64, ) \
1627 zig_convert_builtin(Type, floatun, u64, ) \
1628 zig_convert_builtin(Type, float, i128, ) \
1629 zig_convert_builtin(Type, floatun, u128, ) \
1630 zig_expand_concat(zig_float_negate_builtin_, zig_has_##Type)(Type) \
1631 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, cmp) \
1632 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, ne) \
1633 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, eq) \
1634 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, lt) \
1635 zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, le) \
1636 zig_expand_concat(zig_float_greater_builtin_, zig_has_##Type)(Type, gt) \
1637 zig_expand_concat(zig_float_greater_builtin_, zig_has_##Type)(Type, ge) \
1638 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, add, +) \
1639 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, sub, -) \
1640 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, mul, *) \
1641 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, div, /) \
1642 zig_extern zig_##Type zig_libc_name_##Type(sqrt)(zig_##Type); \
1643 zig_extern zig_##Type zig_libc_name_##Type(sin)(zig_##Type); \
1644 zig_extern zig_##Type zig_libc_name_##Type(cos)(zig_##Type); \
1645 zig_extern zig_##Type zig_libc_name_##Type(tan)(zig_##Type); \
1646 zig_extern zig_##Type zig_libc_name_##Type(exp)(zig_##Type); \
1647 zig_extern zig_##Type zig_libc_name_##Type(exp2)(zig_##Type); \
1648 zig_extern zig_##Type zig_libc_name_##Type(log)(zig_##Type); \
1649 zig_extern zig_##Type zig_libc_name_##Type(log2)(zig_##Type); \
1650 zig_extern zig_##Type zig_libc_name_##Type(log10)(zig_##Type); \
1651 zig_extern zig_##Type zig_libc_name_##Type(fabs)(zig_##Type); \
1652 zig_extern zig_##Type zig_libc_name_##Type(floor)(zig_##Type); \
1653 zig_extern zig_##Type zig_libc_name_##Type(ceil)(zig_##Type); \
1654 zig_extern zig_##Type zig_libc_name_##Type(round)(zig_##Type); \
1655 zig_extern zig_##Type zig_libc_name_##Type(trunc)(zig_##Type); \
1656 zig_extern zig_##Type zig_libc_name_##Type(fmod)(zig_##Type, zig_##Type); \
1657 zig_extern zig_##Type zig_libc_name_##Type(fmin)(zig_##Type, zig_##Type); \
1658 zig_extern zig_##Type zig_libc_name_##Type(fmax)(zig_##Type, zig_##Type); \
1659 zig_extern zig_##Type zig_libc_name_##Type(fma)(zig_##Type, zig_##Type, zig_##Type); \
1660\
1661 static inline zig_##Type zig_div_trunc_##Type(zig_##Type lhs, zig_##Type rhs) { \
1662 return zig_libc_name_##Type(trunc)(zig_div_##Type(lhs, rhs)); \
1663 } \
1664\
1665 static inline zig_##Type zig_div_floor_##Type(zig_##Type lhs, zig_##Type rhs) { \
1666 return zig_libc_name_##Type(floor)(zig_div_##Type(lhs, rhs)); \
1667 } \
1668\
1669 static inline zig_##Type zig_mod_##Type(zig_##Type lhs, zig_##Type rhs) { \
1670 return zig_sub_##Type(lhs, zig_mul_##Type(zig_div_floor_##Type(lhs, rhs), rhs)); \
1671 }
1672zig_float_builtins(f16)
1673zig_float_builtins(f32)
1674zig_float_builtins(f64)
1675zig_float_builtins(f80)
1676zig_float_builtins(f128)
1677zig_float_builtins(c_longdouble)
src/link/C.zig+1-1
...@@ -15,7 +15,7 @@ const Air = @import("../Air.zig");...@@ -15,7 +15,7 @@ const Air = @import("../Air.zig");
15const Liveness = @import("../Liveness.zig");15const Liveness = @import("../Liveness.zig");
1616
17pub const base_tag: link.File.Tag = .c;17pub const base_tag: link.File.Tag = .c;
18pub const zig_h = "#include <zig.h>\n";18pub const zig_h = "#include \"zig.h\"\n";
1919
20base: link.File,20base: link.File,
21/// This linker backend does not try to incrementally link output C source code.21/// This linker backend does not try to incrementally link output C source code.
src/main.zig+4
...@@ -3021,6 +3021,10 @@ fn buildOutputType(...@@ -3021,6 +3021,10 @@ fn buildOutputType(
3021 });3021 });
3022 try test_exec_args.append(self_exe_path);3022 try test_exec_args.append(self_exe_path);
3023 try test_exec_args.append("run");3023 try test_exec_args.append("run");
3024 if (zig_lib_directory.path) |p| {
3025 try test_exec_args.appendSlice(&.{ "-I", p });
3026 }
3027
3024 if (link_libc) try test_exec_args.append("-lc");3028 if (link_libc) try test_exec_args.append("-lc");
3025 if (!mem.eql(u8, target_arch_os_abi, "native")) {3029 if (!mem.eql(u8, target_arch_os_abi, "native")) {
3026 try test_exec_args.append("-target");3030 try test_exec_args.append("-target");
src/test.zig+3
...@@ -1812,6 +1812,9 @@ pub const TestContext = struct {...@@ -1812,6 +1812,9 @@ pub const TestContext = struct {
1812 "-lc",1812 "-lc",
1813 exe_path,1813 exe_path,
1814 });1814 });
1815 if (zig_lib_directory.path) |p| {
1816 try argv.appendSlice(&.{ "-I", p });
1817 }
1815 } else switch (host.getExternalExecutor(target_info, .{ .link_libc = case.link_libc })) {1818 } else switch (host.getExternalExecutor(target_info, .{ .link_libc = case.link_libc })) {
1816 .native => try argv.append(exe_path),1819 .native => try argv.append(exe_path),
1817 .bad_dl, .bad_os_or_cpu => continue :update, // Pass test.1820 .bad_dl, .bad_os_or_cpu => continue :update, // Pass test.