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