| ... | ... | @@ -1,103 +1,108 @@ |
| 1 | 1 | #undef linux |
| 2 | 2 | |
| 3 | #define __STDC_WANT_IEC_60559_TYPES_EXT__ |
| 4 | #include <float.h> |
| 5 | #include <limits.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #if defined(__has_builtin) |
| 9 | #define zig_has_builtin(builtin) __has_builtin(__builtin_##builtin) |
| 10 | #else |
| 11 | #define zig_has_builtin(builtin) 0 |
| 12 | #endif |
| 13 | |
| 14 | #if defined(__has_attribute) |
| 15 | #define zig_has_attribute(attribute) __has_attribute(attribute) |
| 16 | #else |
| 17 | #define zig_has_attribute(attribute) 0 |
| 18 | #endif |
| 19 | |
| 3 | 20 | #if __STDC_VERSION__ >= 201112L |
| 4 | | #define zig_noreturn _Noreturn |
| 5 | 21 | #define zig_threadlocal thread_local |
| 6 | 22 | #elif __GNUC__ |
| 7 | | #define zig_noreturn __attribute__ ((noreturn)) |
| 8 | 23 | #define zig_threadlocal __thread |
| 9 | 24 | #elif _MSC_VER |
| 10 | | #define zig_noreturn __declspec(noreturn) |
| 11 | 25 | #define zig_threadlocal __declspec(thread) |
| 12 | 26 | #else |
| 13 | | #define zig_noreturn |
| 14 | 27 | #define zig_threadlocal zig_threadlocal_unavailable |
| 15 | 28 | #endif |
| 16 | 29 | |
| 17 | | #if defined(_MSC_VER) |
| 18 | | #define ZIG_NAKED __declspec(naked) |
| 30 | #if zig_has_attribute(naked) |
| 31 | #define zig_naked __attribute__((naked)) |
| 32 | #elif defined(_MSC_VER) |
| 33 | #define zig_naked __declspec(naked) |
| 19 | 34 | #else |
| 20 | | #define ZIG_NAKED __attribute__((naked)) |
| 35 | #define zig_naked zig_naked_unavailable |
| 21 | 36 | #endif |
| 22 | 37 | |
| 23 | | #if __GNUC__ |
| 24 | | #define ZIG_COLD __attribute__ ((cold)) |
| 38 | #if zig_has_attribute(cold) |
| 39 | #define zig_cold __attribute__((cold)) |
| 25 | 40 | #else |
| 26 | | #define ZIG_COLD |
| 41 | #define zig_cold |
| 27 | 42 | #endif |
| 28 | 43 | |
| 29 | 44 | #if __STDC_VERSION__ >= 199901L |
| 30 | | #define ZIG_RESTRICT restrict |
| 45 | #define zig_restrict restrict |
| 31 | 46 | #elif defined(__GNUC__) |
| 32 | | #define ZIG_RESTRICT __restrict |
| 47 | #define zig_restrict __restrict |
| 33 | 48 | #else |
| 34 | | #define ZIG_RESTRICT |
| 49 | #define zig_restrict |
| 35 | 50 | #endif |
| 36 | 51 | |
| 37 | 52 | #if __STDC_VERSION__ >= 201112L |
| 38 | | #include <stdalign.h> |
| 39 | | #define ZIG_ALIGN(alignment) alignas(alignment) |
| 40 | | #elif defined(__GNUC__) |
| 41 | | #define ZIG_ALIGN(alignment) __attribute__((aligned(alignment))) |
| 42 | | #else |
| 43 | | #define ZIG_ALIGN(alignment) zig_compile_error("the C compiler being used does not support aligning variables") |
| 44 | | #endif |
| 45 | | |
| 46 | | #if __STDC_VERSION__ >= 199901L |
| 47 | | #include <stdbool.h> |
| 53 | #define zig_align(alignment) _Alignas(alignment) |
| 54 | #elif zig_has_attribute(aligned) |
| 55 | #define zig_align(alignment) __attribute__((aligned(alignment))) |
| 56 | #elif _MSC_VER |
| 48 | 57 | #else |
| 49 | | #define bool unsigned char |
| 50 | | #define true 1 |
| 51 | | #define false 0 |
| 58 | #error the C compiler being used does not support aligning variables |
| 52 | 59 | #endif |
| 53 | 60 | |
| 54 | | #if defined(__GNUC__) |
| 61 | #if zig_has_builtin(unreachable) |
| 55 | 62 | #define zig_unreachable() __builtin_unreachable() |
| 56 | 63 | #else |
| 57 | 64 | #define zig_unreachable() |
| 58 | 65 | #endif |
| 59 | 66 | |
| 60 | | #ifdef __cplusplus |
| 61 | | #define ZIG_EXTERN_C extern "C" |
| 67 | #if defined(__cplusplus) |
| 68 | #define zig_extern_c extern "C" |
| 62 | 69 | #else |
| 63 | | #define ZIG_EXTERN_C |
| 70 | #define zig_extern_c |
| 64 | 71 | #endif |
| 65 | 72 | |
| 66 | | #if defined(_MSC_VER) |
| 67 | | #define zig_breakpoint() __debugbreak() |
| 68 | | #elif defined(__MINGW32__) || defined(__MINGW64__) |
| 69 | | #define zig_breakpoint() __debugbreak() |
| 70 | | #elif defined(__clang__) |
| 73 | #if zig_has_builtin(debugtrap) |
| 71 | 74 | #define zig_breakpoint() __builtin_debugtrap() |
| 72 | | #elif defined(__GNUC__) |
| 75 | #elif zig_has_builtin(trap) |
| 73 | 76 | #define zig_breakpoint() __builtin_trap() |
| 77 | #elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) |
| 78 | #define zig_breakpoint() __debugbreak() |
| 74 | 79 | #elif defined(__i386__) || defined(__x86_64__) |
| 75 | 80 | #define zig_breakpoint() __asm__ volatile("int $0x03"); |
| 76 | 81 | #else |
| 77 | 82 | #define zig_breakpoint() raise(SIGTRAP) |
| 78 | 83 | #endif |
| 79 | 84 | |
| 80 | | #if defined(_MSC_VER) |
| 81 | | #define zig_return_address() _ReturnAddress() |
| 82 | | #elif defined(__GNUC__) |
| 85 | #if zig_has_builtin(return_address) |
| 83 | 86 | #define zig_return_address() __builtin_extract_return_addr(__builtin_return_address(0)) |
| 87 | #elif defined(_MSC_VER) |
| 88 | #define zig_return_address() _ReturnAddress() |
| 84 | 89 | #else |
| 85 | 90 | #define zig_return_address() 0 |
| 86 | 91 | #endif |
| 87 | 92 | |
| 88 | | #if defined(__GNUC__) |
| 93 | #if zig_has_builtin(frame_address) |
| 89 | 94 | #define zig_frame_address() __builtin_frame_address(0) |
| 90 | 95 | #else |
| 91 | 96 | #define zig_frame_address() 0 |
| 92 | 97 | #endif |
| 93 | 98 | |
| 94 | | #if defined(__GNUC__) |
| 99 | #if zig_has_builtin(prefetch) |
| 95 | 100 | #define zig_prefetch(addr, rw, locality) __builtin_prefetch(addr, rw, locality) |
| 96 | 101 | #else |
| 97 | 102 | #define zig_prefetch(addr, rw, locality) |
| 98 | 103 | #endif |
| 99 | 104 | |
| 100 | | #if defined(__clang__) |
| 105 | #if zig_has_builtin(memory_size) && zig_has_builtin(memory_grow) |
| 101 | 106 | #define zig_wasm_memory_size(index) __builtin_wasm_memory_size(index) |
| 102 | 107 | #define zig_wasm_memory_grow(index, delta) __builtin_wasm_memory_grow(index, delta) |
| 103 | 108 | #else |
| ... | ... | @@ -130,8 +135,8 @@ |
| 130 | 135 | #define memory_order_acq_rel __ATOMIC_ACQ_REL |
| 131 | 136 | #define memory_order_seq_cst __ATOMIC_SEQ_CST |
| 132 | 137 | #define zig_atomic(type) type |
| 133 | | #define zig_cmpxchg_strong(obj, expected, desired, succ, fail) __atomic_compare_exchange_n(obj, &(expected), desired, false, succ, fail) |
| 134 | | #define zig_cmpxchg_weak(obj, expected, desired, succ, fail) __atomic_compare_exchange_n(obj, &(expected), desired, true , succ, fail) |
| 138 | #define zig_cmpxchg_strong(obj, expected, desired, succ, fail) __atomic_compare_exchange_n(obj, &(expected), desired, zig_false, succ, fail) |
| 139 | #define zig_cmpxchg_weak(obj, expected, desired, succ, fail) __atomic_compare_exchange_n(obj, &(expected), desired, zig_true , succ, fail) |
| 135 | 140 | #define zig_atomicrmw_xchg(obj, arg, order) __atomic_exchange_n(obj, arg, order) |
| 136 | 141 | #define zig_atomicrmw_add(obj, arg, order) __atomic_fetch_add (obj, arg, order) |
| 137 | 142 | #define zig_atomicrmw_sub(obj, arg, order) __atomic_fetch_sub (obj, arg, order) |
| ... | ... | @@ -168,1366 +173,1258 @@ |
| 168 | 173 | #define zig_fence(order) zig_unimplemented() |
| 169 | 174 | #endif |
| 170 | 175 | |
| 171 | | #include <stdint.h> |
| 172 | | #include <stddef.h> |
| 173 | | #include <limits.h> |
| 176 | #if __STDC_VERSION__ >= 201112L |
| 177 | #define zig_noreturn _Noreturn void |
| 178 | #define zig_threadlocal thread_local |
| 179 | #elif __GNUC__ |
| 180 | #define zig_noreturn __attribute__ ((noreturn)) void |
| 181 | #define zig_threadlocal __thread |
| 182 | #elif _MSC_VER |
| 183 | #define zig_noreturn __declspec(noreturn) void |
| 184 | #define zig_threadlocal __declspec(thread) |
| 185 | #else |
| 186 | #define zig_noreturn void |
| 187 | #define zig_threadlocal zig_threadlocal_unavailable |
| 188 | #endif |
| 174 | 189 | |
| 175 | | #define int128_t __int128 |
| 176 | | #define uint128_t unsigned __int128 |
| 177 | | #define UINT128_MAX (((uint128_t)UINT64_MAX<<64|UINT64_MAX)) |
| 178 | | ZIG_EXTERN_C void *memcpy (void *ZIG_RESTRICT, const void *ZIG_RESTRICT, size_t); |
| 179 | | ZIG_EXTERN_C void *memset (void *, int, size_t); |
| 180 | | ZIG_EXTERN_C int64_t __addodi4(int64_t lhs, int64_t rhs, int *overflow); |
| 181 | | ZIG_EXTERN_C int128_t __addoti4(int128_t lhs, int128_t rhs, int *overflow); |
| 182 | | ZIG_EXTERN_C uint64_t __uaddodi4(uint64_t lhs, uint64_t rhs, int *overflow); |
| 183 | | ZIG_EXTERN_C uint128_t __uaddoti4(uint128_t lhs, uint128_t rhs, int *overflow); |
| 184 | | ZIG_EXTERN_C int32_t __subosi4(int32_t lhs, int32_t rhs, int *overflow); |
| 185 | | ZIG_EXTERN_C int64_t __subodi4(int64_t lhs, int64_t rhs, int *overflow); |
| 186 | | ZIG_EXTERN_C int128_t __suboti4(int128_t lhs, int128_t rhs, int *overflow); |
| 187 | | ZIG_EXTERN_C uint32_t __usubosi4(uint32_t lhs, uint32_t rhs, int *overflow); |
| 188 | | ZIG_EXTERN_C uint64_t __usubodi4(uint64_t lhs, uint64_t rhs, int *overflow); |
| 189 | | ZIG_EXTERN_C uint128_t __usuboti4(uint128_t lhs, uint128_t rhs, int *overflow); |
| 190 | | ZIG_EXTERN_C int64_t __mulodi4(int64_t lhs, int64_t rhs, int *overflow); |
| 191 | | ZIG_EXTERN_C int128_t __muloti4(int128_t lhs, int128_t rhs, int *overflow); |
| 192 | | ZIG_EXTERN_C uint64_t __umulodi4(uint64_t lhs, uint64_t rhs, int *overflow); |
| 193 | | ZIG_EXTERN_C uint128_t __umuloti4(uint128_t lhs, uint128_t rhs, int *overflow); |
| 194 | | |
| 195 | | |
| 196 | | static inline uint8_t zig_addw_u8(uint8_t lhs, uint8_t rhs, uint8_t max) { |
| 197 | | uint8_t thresh = max - rhs; |
| 198 | | if (lhs > thresh) { |
| 199 | | return lhs - thresh - 1; |
| 200 | | } else { |
| 201 | | return lhs + rhs; |
| 202 | | } |
| 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 | typedef long double zig_c_longdouble; |
| 219 | |
| 220 | typedef uint8_t zig_u8; |
| 221 | typedef int8_t zig_i8; |
| 222 | typedef uint16_t zig_u16; |
| 223 | typedef int16_t zig_i16; |
| 224 | typedef uint16_t zig_u16; |
| 225 | typedef int16_t zig_i16; |
| 226 | typedef uint32_t zig_u32; |
| 227 | typedef int32_t zig_i32; |
| 228 | typedef uint64_t zig_u64; |
| 229 | typedef int64_t zig_i64; |
| 230 | |
| 231 | #define zig_as_u8(val) UINT8_C(val) |
| 232 | #define zig_as_i8(val) INT8_C(val) |
| 233 | #define zig_as_u16(val) UINT16_C(val) |
| 234 | #define zig_as_i16(val) INT16_C(val) |
| 235 | #define zig_as_u32(val) UINT32_C(val) |
| 236 | #define zig_as_i32(val) INT32_C(val) |
| 237 | #define zig_as_u64(val) UINT64_C(val) |
| 238 | #define zig_as_i64(val) INT64_C(val) |
| 239 | |
| 240 | #define zig_minInt_u8 zig_as_u8(0) |
| 241 | #define zig_maxInt_u8 UINT8_MAX |
| 242 | #define zig_minInt_i8 INT8_MIN |
| 243 | #define zig_maxInt_i8 INT8_MAX |
| 244 | #define zig_minInt_u16 zig_as_u16(0) |
| 245 | #define zig_maxInt_u16 UINT16_MAX |
| 246 | #define zig_minInt_i16 INT16_MIN |
| 247 | #define zig_maxInt_i16 INT16_MAX |
| 248 | #define zig_minInt_u32 zig_as_u32(0) |
| 249 | #define zig_maxInt_u32 UINT32_MAX |
| 250 | #define zig_minInt_i32 INT32_MIN |
| 251 | #define zig_maxInt_i32 INT32_MAX |
| 252 | #define zig_minInt_u64 zig_as_u64(0) |
| 253 | #define zig_maxInt_u64 UINT64_MAX |
| 254 | #define zig_minInt_i64 INT64_MIN |
| 255 | #define zig_maxInt_i64 INT64_MAX |
| 256 | |
| 257 | #if FLT_MANT_DIG == 11 |
| 258 | typedef float zig_f16; |
| 259 | #elif DBL_MANT_DIG == 11 |
| 260 | typedef double zig_f16; |
| 261 | #elif LDBL_MANT_DIG == 11 |
| 262 | typedef long double zig_f16; |
| 263 | #elif FLT16_MANT_DIG == 11 |
| 264 | typedef _Float16 zig_f16; |
| 265 | #endif |
| 266 | |
| 267 | #if FLT_MANT_DIG == 24 |
| 268 | typedef float zig_f32; |
| 269 | #elif DBL_MANT_DIG == 24 |
| 270 | typedef double zig_f32; |
| 271 | #elif LDBL_MANT_DIG == 24 |
| 272 | typedef long double zig_f32; |
| 273 | #elif FLT32_MANT_DIG == 24 |
| 274 | typedef _Float32 zig_f32; |
| 275 | #endif |
| 276 | |
| 277 | #if FLT_MANT_DIG == 53 |
| 278 | typedef float zig_f64; |
| 279 | #elif DBL_MANT_DIG == 53 |
| 280 | typedef double zig_f64; |
| 281 | #elif LDBL_MANT_DIG == 53 |
| 282 | typedef long double zig_f64; |
| 283 | #elif FLT64_MANT_DIG == 53 |
| 284 | typedef _Float64 zig_f64; |
| 285 | #endif |
| 286 | |
| 287 | #if FLT_MANT_DIG == 64 |
| 288 | typedef float zig_f80; |
| 289 | #elif DBL_MANT_DIG == 64 |
| 290 | typedef double zig_f80; |
| 291 | #elif LDBL_MANT_DIG == 64 |
| 292 | typedef long double zig_f80; |
| 293 | #elif FLT80_MANT_DIG == 64 |
| 294 | typedef _Float80 zig_f80; |
| 295 | #elif defined(__SIZEOF_FLOAT80__) |
| 296 | typedef __float80 zig_f80; |
| 297 | #endif |
| 298 | |
| 299 | #if FLT_MANT_DIG == 113 |
| 300 | typedef float zig_f128; |
| 301 | #elif DBL_MANT_DIG == 113 |
| 302 | typedef double zig_f128; |
| 303 | #elif LDBL_MANT_DIG == 113 |
| 304 | typedef long double zig_f128; |
| 305 | #elif FLT128_MANT_DIG == 113 |
| 306 | typedef _Float128 zig_f128; |
| 307 | #elif defined(__SIZEOF_FLOAT128__) |
| 308 | typedef __float128 zig_f128; |
| 309 | #endif |
| 310 | |
| 311 | zig_extern_c void *memcpy (void *zig_restrict, void const *zig_restrict, zig_usize); |
| 312 | zig_extern_c void *memset (void *, int, zig_usize); |
| 313 | |
| 314 | /* ==================== 8/16/32/64-bit Integer Routines ===================== */ |
| 315 | |
| 316 | #define zig_maxInt(Type, bits) zig_shr_##Type(zig_maxInt_##Type, (zig_bitSizeOf(zig_##Type) - bits)) |
| 317 | #define zig_minInt(Type, bits) zig_not_##Type(zig_maxInt(Type, bits), bits) |
| 318 | |
| 319 | #define zig_int_helpers(w) \ |
| 320 | static inline zig_u##w zig_shl_u##w(zig_u##w lhs, zig_u8 rhs) { \ |
| 321 | return lhs << rhs; \ |
| 322 | } \ |
| 323 | \ |
| 324 | static inline zig_i##w zig_shl_i##w(zig_i##w lhs, zig_u8 rhs) { \ |
| 325 | return lhs << rhs; \ |
| 326 | } \ |
| 327 | \ |
| 328 | static inline zig_u##w zig_shr_u##w(zig_u##w lhs, zig_u8 rhs) { \ |
| 329 | return lhs >> rhs; \ |
| 330 | } \ |
| 331 | \ |
| 332 | static inline zig_i##w zig_shr_i##w(zig_i##w lhs, zig_u8 rhs) { \ |
| 333 | zig_i##w sign_mask = lhs < zig_as_i##w(0) ? zig_as_i##w(-1) : zig_as_i##w(0); \ |
| 334 | return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \ |
| 335 | } \ |
| 336 | \ |
| 337 | static inline zig_u##w zig_not_u##w(zig_u##w val, zig_u8 bits) { \ |
| 338 | return val ^ zig_maxInt(u##w, bits); \ |
| 339 | } \ |
| 340 | \ |
| 341 | static inline zig_i##w zig_not_i##w(zig_i##w val, zig_u8 bits) { \ |
| 342 | (void)bits; \ |
| 343 | return ~val; \ |
| 344 | } \ |
| 345 | \ |
| 346 | static inline zig_u##w zig_wrap_u##w(zig_u##w val, zig_u8 bits) { \ |
| 347 | return val & zig_maxInt(u##w, bits); \ |
| 348 | } \ |
| 349 | \ |
| 350 | static inline zig_i##w zig_wrap_i##w(zig_i##w val, zig_u8 bits) { \ |
| 351 | return (val & zig_as_u##w(1) << (bits - zig_as_u8(1))) != 0 \ |
| 352 | ? val | zig_minInt(i##w, bits) : val & zig_maxInt(i##w, bits); \ |
| 353 | } \ |
| 354 | \ |
| 355 | static inline zig_u##w zig_div_floor_u##w(zig_u##w lhs, zig_u##w rhs) { \ |
| 356 | return lhs / rhs; \ |
| 357 | } \ |
| 358 | \ |
| 359 | static inline zig_i##w zig_div_floor_i##w(zig_i##w lhs, zig_i##w rhs) { \ |
| 360 | return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < zig_as_i##w(0)); \ |
| 361 | } \ |
| 362 | \ |
| 363 | static inline zig_u##w zig_mod_u##w(zig_u##w lhs, zig_u##w rhs) { \ |
| 364 | return lhs % rhs; \ |
| 365 | } \ |
| 366 | \ |
| 367 | static inline zig_i##w zig_mod_i##w(zig_i##w lhs, zig_i##w rhs) { \ |
| 368 | zig_i##w rem = lhs % rhs; \ |
| 369 | return rem + (((lhs ^ rhs) & rem) < zig_as_i##w(0) ? rhs : zig_as_i##w(0)); \ |
| 370 | } |
| 371 | zig_int_helpers(8) |
| 372 | zig_int_helpers(16) |
| 373 | zig_int_helpers(32) |
| 374 | zig_int_helpers(64) |
| 375 | |
| 376 | static inline zig_bool zig_addo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) { |
| 377 | #if zig_has_builtin(add_overflow) |
| 378 | zig_u32 full_res; |
| 379 | zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 380 | *res = zig_wrap_u32(full_res, bits); |
| 381 | return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits); |
| 382 | #else |
| 383 | *res = zig_addw_u32(lhs, rhs, bits); |
| 384 | return *res < lhs; |
| 385 | #endif |
| 203 | 386 | } |
| 204 | 387 | |
| 205 | | static inline int8_t zig_addw_i8(int8_t lhs, int8_t rhs, int8_t min, int8_t max) { |
| 206 | | if ((lhs > 0) && (rhs > 0)) { |
| 207 | | int8_t thresh = max - rhs; |
| 208 | | if (lhs > thresh) { |
| 209 | | return min + lhs - thresh - 1; |
| 210 | | } |
| 211 | | } else if ((lhs < 0) && (rhs < 0)) { |
| 212 | | int8_t thresh = min - rhs; |
| 213 | | if (lhs < thresh) { |
| 214 | | return max + lhs - thresh + 1; |
| 215 | | } |
| 216 | | } |
| 217 | | return lhs + rhs; |
| 388 | zig_extern_c zig_i32 __addosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow); |
| 389 | static inline zig_bool zig_addo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) { |
| 390 | #if zig_has_builtin(add_overflow) |
| 391 | zig_i32 full_res; |
| 392 | zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 393 | #else |
| 394 | zig_c_int overflow_int; |
| 395 | zig_u32 full_res = __addosi4(lhs, rhs, &overflow_int); |
| 396 | zig_bool overflow = overflow_int != 0; |
| 397 | #endif |
| 398 | *res = zig_wrap_i32(full_res, bits); |
| 399 | return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits); |
| 218 | 400 | } |
| 219 | 401 | |
| 220 | | static inline uint16_t zig_addw_u16(uint16_t lhs, uint16_t rhs, uint16_t max) { |
| 221 | | uint16_t thresh = max - rhs; |
| 222 | | if (lhs > thresh) { |
| 223 | | return lhs - thresh - 1; |
| 224 | | } else { |
| 225 | | return lhs + rhs; |
| 226 | | } |
| 402 | static inline zig_bool zig_addo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) { |
| 403 | #if zig_has_builtin(add_overflow) |
| 404 | zig_u64 full_res; |
| 405 | zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 406 | *res = zig_wrap_u64(full_res, bits); |
| 407 | return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits); |
| 408 | #else |
| 409 | *res = zig_addw_u64(lhs, rhs, bits); |
| 410 | return *res < lhs; |
| 411 | #endif |
| 227 | 412 | } |
| 228 | 413 | |
| 229 | | static inline int16_t zig_addw_i16(int16_t lhs, int16_t rhs, int16_t min, int16_t max) { |
| 230 | | if ((lhs > 0) && (rhs > 0)) { |
| 231 | | int16_t thresh = max - rhs; |
| 232 | | if (lhs > thresh) { |
| 233 | | return min + lhs - thresh - 1; |
| 234 | | } |
| 235 | | } else if ((lhs < 0) && (rhs < 0)) { |
| 236 | | int16_t thresh = min - rhs; |
| 237 | | if (lhs < thresh) { |
| 238 | | return max + lhs - thresh + 1; |
| 239 | | } |
| 240 | | } |
| 241 | | return lhs + rhs; |
| 414 | zig_extern_c zig_i64 __addodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow); |
| 415 | static inline zig_bool zig_addo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) { |
| 416 | #if zig_has_builtin(add_overflow) |
| 417 | zig_i64 full_res; |
| 418 | zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 419 | #else |
| 420 | zig_c_int overflow_int; |
| 421 | zig_u64 full_res = __addodi4(lhs, rhs, &overflow_int); |
| 422 | zig_bool overflow = overflow_int != 0; |
| 423 | #endif |
| 424 | *res = zig_wrap_i64(full_res, bits); |
| 425 | return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits); |
| 242 | 426 | } |
| 243 | 427 | |
| 244 | | static inline uint32_t zig_addw_u32(uint32_t lhs, uint32_t rhs, uint32_t max) { |
| 245 | | uint32_t thresh = max - rhs; |
| 246 | | if (lhs > thresh) { |
| 247 | | return lhs - thresh - 1; |
| 248 | | } else { |
| 249 | | return lhs + rhs; |
| 250 | | } |
| 428 | static inline zig_bool zig_addo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) { |
| 429 | #if zig_has_builtin(add_overflow) |
| 430 | zig_u8 full_res; |
| 431 | zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 432 | *res = zig_wrap_u8(full_res, bits); |
| 433 | return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits); |
| 434 | #else |
| 435 | return zig_addo_u32(res, lhs, rhs, bits); |
| 436 | #endif |
| 251 | 437 | } |
| 252 | 438 | |
| 253 | | static inline int32_t zig_addw_i32(int32_t lhs, int32_t rhs, int32_t min, int32_t max) { |
| 254 | | if ((lhs > 0) && (rhs > 0)) { |
| 255 | | int32_t thresh = max - rhs; |
| 256 | | if (lhs > thresh) { |
| 257 | | return min + lhs - thresh - 1; |
| 258 | | } |
| 259 | | } else if ((lhs < 0) && (rhs < 0)) { |
| 260 | | int32_t thresh = min - rhs; |
| 261 | | if (lhs < thresh) { |
| 262 | | return max + lhs - thresh + 1; |
| 263 | | } |
| 264 | | } |
| 265 | | return lhs + rhs; |
| 439 | static inline zig_bool zig_addo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) { |
| 440 | #if zig_has_builtin(add_overflow) |
| 441 | zig_i8 full_res; |
| 442 | zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 443 | *res = zig_wrap_i8(full_res, bits); |
| 444 | return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits); |
| 445 | #else |
| 446 | return zig_addo_i32(res, lhs, rhs, bits); |
| 447 | #endif |
| 266 | 448 | } |
| 267 | 449 | |
| 268 | | static inline uint64_t zig_addw_u64(uint64_t lhs, uint64_t rhs, uint64_t max) { |
| 269 | | uint64_t thresh = max - rhs; |
| 270 | | if (lhs > thresh) { |
| 271 | | return lhs - thresh - 1; |
| 272 | | } else { |
| 273 | | return lhs + rhs; |
| 274 | | } |
| 450 | static inline zig_bool zig_addo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) { |
| 451 | #if zig_has_builtin(add_overflow) |
| 452 | zig_u16 full_res; |
| 453 | zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 454 | *res = zig_wrap_u16(full_res, bits); |
| 455 | return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits); |
| 456 | #else |
| 457 | return zig_addo_u32(res, lhs, rhs, bits); |
| 458 | #endif |
| 275 | 459 | } |
| 276 | 460 | |
| 277 | | static inline int64_t zig_addw_i64(int64_t lhs, int64_t rhs, int64_t min, int64_t max) { |
| 278 | | if ((lhs > 0) && (rhs > 0)) { |
| 279 | | int64_t thresh = max - rhs; |
| 280 | | if (lhs > thresh) { |
| 281 | | return min + lhs - thresh - 1; |
| 282 | | } |
| 283 | | } else if ((lhs < 0) && (rhs < 0)) { |
| 284 | | int64_t thresh = min - rhs; |
| 285 | | if (lhs < thresh) { |
| 286 | | return max + lhs - thresh + 1; |
| 287 | | } |
| 288 | | } |
| 289 | | return lhs + rhs; |
| 461 | static inline zig_bool zig_addo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) { |
| 462 | #if zig_has_builtin(add_overflow) |
| 463 | zig_i16 full_res; |
| 464 | zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 465 | *res = zig_wrap_i16(full_res, bits); |
| 466 | return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits); |
| 467 | #else |
| 468 | return zig_addo_i32(res, lhs, rhs, bits); |
| 469 | #endif |
| 290 | 470 | } |
| 291 | 471 | |
| 292 | | static inline intptr_t zig_addw_isize(intptr_t lhs, intptr_t rhs, intptr_t min, intptr_t max) { |
| 293 | | return (intptr_t)(((uintptr_t)lhs) + ((uintptr_t)rhs)); |
| 472 | static inline zig_bool zig_subo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) { |
| 473 | #if zig_has_builtin(sub_overflow) |
| 474 | zig_u32 full_res; |
| 475 | zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 476 | *res = zig_wrap_u32(full_res, bits); |
| 477 | return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits); |
| 478 | #else |
| 479 | *res = zig_subw_u32(lhs, rhs, bits); |
| 480 | return *res > lhs; |
| 481 | #endif |
| 294 | 482 | } |
| 295 | 483 | |
| 296 | | static inline short zig_addw_short(short lhs, short rhs, short min, short max) { |
| 297 | | return (short)(((unsigned short)lhs) + ((unsigned short)rhs)); |
| 484 | zig_extern_c zig_i32 __subosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow); |
| 485 | static inline zig_bool zig_subo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) { |
| 486 | #if zig_has_builtin(sub_overflow) |
| 487 | zig_i32 full_res; |
| 488 | zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 489 | #else |
| 490 | zig_c_int overflow_int; |
| 491 | zig_u32 full_res = __subosi4(lhs, rhs, &overflow_int); |
| 492 | zig_bool overflow = overflow_int != 0; |
| 493 | #endif |
| 494 | *res = zig_wrap_i32(full_res, bits); |
| 495 | return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits); |
| 298 | 496 | } |
| 299 | 497 | |
| 300 | | static inline int zig_addw_int(int lhs, int rhs, int min, int max) { |
| 301 | | return (int)(((unsigned)lhs) + ((unsigned)rhs)); |
| 498 | static inline zig_bool zig_subo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) { |
| 499 | #if zig_has_builtin(sub_overflow) |
| 500 | zig_u64 full_res; |
| 501 | zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 502 | *res = zig_wrap_u64(full_res, bits); |
| 503 | return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits); |
| 504 | #else |
| 505 | *res = zig_subw_u64(lhs, rhs, bits); |
| 506 | return *res > lhs; |
| 507 | #endif |
| 302 | 508 | } |
| 303 | 509 | |
| 304 | | static inline long zig_addw_long(long lhs, long rhs, long min, long max) { |
| 305 | | return (long)(((unsigned long)lhs) + ((unsigned long)rhs)); |
| 510 | zig_extern_c zig_i64 __subodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow); |
| 511 | static inline zig_bool zig_subo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) { |
| 512 | #if zig_has_builtin(sub_overflow) |
| 513 | zig_i64 full_res; |
| 514 | zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 515 | #else |
| 516 | zig_c_int overflow_int; |
| 517 | zig_u64 full_res = __subodi4(lhs, rhs, &overflow_int); |
| 518 | zig_bool overflow = overflow_int != 0; |
| 519 | #endif |
| 520 | *res = zig_wrap_i64(full_res, bits); |
| 521 | return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits); |
| 306 | 522 | } |
| 307 | 523 | |
| 308 | | static inline long long zig_addw_longlong(long long lhs, long long rhs, long long min, long long max) { |
| 309 | | return (long long)(((unsigned long long)lhs) + ((unsigned long long)rhs)); |
| 524 | static inline zig_bool zig_subo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) { |
| 525 | #if zig_has_builtin(sub_overflow) |
| 526 | zig_u8 full_res; |
| 527 | zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 528 | *res = zig_wrap_u8(full_res, bits); |
| 529 | return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits); |
| 530 | #else |
| 531 | return zig_subo_u32(res, lhs, rhs, bits); |
| 532 | #endif |
| 310 | 533 | } |
| 311 | 534 | |
| 312 | | static inline uint8_t zig_subw_u8(uint8_t lhs, uint8_t rhs, uint8_t max) { |
| 313 | | if (lhs < rhs) { |
| 314 | | return max - rhs - lhs + 1; |
| 315 | | } else { |
| 316 | | return lhs - rhs; |
| 317 | | } |
| 535 | static inline zig_bool zig_subo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) { |
| 536 | #if zig_has_builtin(sub_overflow) |
| 537 | zig_i8 full_res; |
| 538 | zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 539 | *res = zig_wrap_i8(full_res, bits); |
| 540 | return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits); |
| 541 | #else |
| 542 | return zig_subo_i32(res, lhs, rhs, bits); |
| 543 | #endif |
| 318 | 544 | } |
| 319 | 545 | |
| 320 | | static inline int8_t zig_subw_i8(int8_t lhs, int8_t rhs, int8_t min, int8_t max) { |
| 321 | | if ((lhs > 0) && (rhs < 0)) { |
| 322 | | int8_t thresh = lhs - max; |
| 323 | | if (rhs < thresh) { |
| 324 | | return min + (thresh - rhs - 1); |
| 325 | | } |
| 326 | | } else if ((lhs < 0) && (rhs > 0)) { |
| 327 | | int8_t thresh = lhs - min; |
| 328 | | if (rhs > thresh) { |
| 329 | | return max - (rhs - thresh - 1); |
| 330 | | } |
| 331 | | } |
| 332 | | return lhs - rhs; |
| 546 | static inline zig_bool zig_subo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) { |
| 547 | #if zig_has_builtin(sub_overflow) |
| 548 | zig_u16 full_res; |
| 549 | zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 550 | *res = zig_wrap_u16(full_res, bits); |
| 551 | return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits); |
| 552 | #else |
| 553 | return zig_subo_u32(res, lhs, rhs, bits); |
| 554 | #endif |
| 333 | 555 | } |
| 334 | 556 | |
| 335 | | static inline uint16_t zig_subw_u16(uint16_t lhs, uint16_t rhs, uint16_t max) { |
| 336 | | if (lhs < rhs) { |
| 337 | | return max - rhs - lhs + 1; |
| 338 | | } else { |
| 339 | | return lhs - rhs; |
| 340 | | } |
| 557 | static inline zig_bool zig_subo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) { |
| 558 | #if zig_has_builtin(sub_overflow) |
| 559 | zig_i16 full_res; |
| 560 | zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 561 | *res = zig_wrap_i16(full_res, bits); |
| 562 | return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits); |
| 563 | #else |
| 564 | return zig_subo_i32(res, lhs, rhs, bits); |
| 565 | #endif |
| 341 | 566 | } |
| 342 | 567 | |
| 343 | | static inline int16_t zig_subw_i16(int16_t lhs, int16_t rhs, int16_t min, int16_t max) { |
| 344 | | if ((lhs > 0) && (rhs < 0)) { |
| 345 | | int16_t thresh = lhs - max; |
| 346 | | if (rhs < thresh) { |
| 347 | | return min + (thresh - rhs - 1); |
| 348 | | } |
| 349 | | } else if ((lhs < 0) && (rhs > 0)) { |
| 350 | | int16_t thresh = lhs - min; |
| 351 | | if (rhs > thresh) { |
| 352 | | return max - (rhs - thresh - 1); |
| 353 | | } |
| 354 | | } |
| 355 | | return lhs - rhs; |
| 568 | static inline zig_bool zig_mulo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) { |
| 569 | #if zig_has_builtin(mul_overflow) |
| 570 | zig_u32 full_res; |
| 571 | zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 572 | *res = zig_wrap_u32(full_res, bits); |
| 573 | return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits); |
| 574 | #else |
| 575 | *res = zig_mulw_u32(lhs, rhs, bits); |
| 576 | return rhs != zig_as_u32(0) && lhs > zig_maxInt(u32, bits) / rhs; |
| 577 | #endif |
| 356 | 578 | } |
| 357 | 579 | |
| 358 | | static inline uint32_t zig_subw_u32(uint32_t lhs, uint32_t rhs, uint32_t max) { |
| 359 | | if (lhs < rhs) { |
| 360 | | return max - rhs - lhs + 1; |
| 361 | | } else { |
| 362 | | return lhs - rhs; |
| 363 | | } |
| 580 | zig_extern_c zig_i32 __mulosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow); |
| 581 | static inline zig_bool zig_mulo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) { |
| 582 | #if zig_has_builtin(mul_overflow) |
| 583 | zig_i32 full_res; |
| 584 | zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 585 | #else |
| 586 | zig_c_int overflow_int; |
| 587 | zig_u32 full_res = __mulosi4(lhs, rhs, &overflow_int); |
| 588 | zig_bool overflow = overflow_int != 0; |
| 589 | #endif |
| 590 | *res = zig_wrap_i32(full_res, bits); |
| 591 | return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits); |
| 364 | 592 | } |
| 365 | 593 | |
| 366 | | static inline int32_t zig_subw_i32(int32_t lhs, int32_t rhs, int32_t min, int32_t max) { |
| 367 | | if ((lhs > 0) && (rhs < 0)) { |
| 368 | | int32_t thresh = lhs - max; |
| 369 | | if (rhs < thresh) { |
| 370 | | return min + (thresh - rhs - 1); |
| 371 | | } |
| 372 | | } else if ((lhs < 0) && (rhs > 0)) { |
| 373 | | int32_t thresh = lhs - min; |
| 374 | | if (rhs > thresh) { |
| 375 | | return max - (rhs - thresh - 1); |
| 376 | | } |
| 377 | | } |
| 378 | | return lhs - rhs; |
| 594 | static inline zig_bool zig_mulo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) { |
| 595 | #if zig_has_builtin(mul_overflow) |
| 596 | zig_u64 full_res; |
| 597 | zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 598 | *res = zig_wrap_u64(full_res, bits); |
| 599 | return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits); |
| 600 | #else |
| 601 | *res = zig_mulw_u64(lhs, rhs, bits); |
| 602 | return rhs != zig_as_u64(0) && lhs > zig_maxInt(u64, bits) / rhs; |
| 603 | #endif |
| 379 | 604 | } |
| 380 | 605 | |
| 381 | | static inline uint64_t zig_subw_u64(uint64_t lhs, uint64_t rhs, uint64_t max) { |
| 382 | | if (lhs < rhs) { |
| 383 | | return max - rhs - lhs + 1; |
| 384 | | } else { |
| 385 | | return lhs - rhs; |
| 386 | | } |
| 606 | zig_extern_c zig_i64 __mulodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow); |
| 607 | static inline zig_bool zig_mulo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) { |
| 608 | #if zig_has_builtin(mul_overflow) |
| 609 | zig_i64 full_res; |
| 610 | zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 611 | #else |
| 612 | zig_c_int overflow_int; |
| 613 | zig_u64 full_res = __mulodi4(lhs, rhs, &overflow_int); |
| 614 | zig_bool overflow = overflow_int != 0; |
| 615 | #endif |
| 616 | *res = zig_wrap_i64(full_res, bits); |
| 617 | return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits); |
| 387 | 618 | } |
| 388 | 619 | |
| 389 | | static inline int64_t zig_subw_i64(int64_t lhs, int64_t rhs, int64_t min, int64_t max) { |
| 390 | | if ((lhs > 0) && (rhs < 0)) { |
| 391 | | int64_t thresh = lhs - max; |
| 392 | | if (rhs < thresh) { |
| 393 | | return min + (thresh - rhs - 1); |
| 394 | | } |
| 395 | | } else if ((lhs < 0) && (rhs > 0)) { |
| 396 | | int64_t thresh = lhs - min; |
| 397 | | if (rhs > thresh) { |
| 398 | | return max - (rhs - thresh - 1); |
| 399 | | } |
| 400 | | } |
| 401 | | return lhs - rhs; |
| 620 | static inline zig_bool zig_mulo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) { |
| 621 | #if zig_has_builtin(mul_overflow) |
| 622 | zig_u8 full_res; |
| 623 | zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 624 | *res = zig_wrap_u8(full_res, bits); |
| 625 | return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits); |
| 626 | #else |
| 627 | return zig_mulo_u32(res, lhs, rhs, bits); |
| 628 | #endif |
| 402 | 629 | } |
| 403 | 630 | |
| 404 | | static inline intptr_t zig_subw_isize(intptr_t lhs, intptr_t rhs, intptr_t min, intptr_t max) { |
| 405 | | return (intptr_t)(((uintptr_t)lhs) - ((uintptr_t)rhs)); |
| 631 | static inline zig_bool zig_mulo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) { |
| 632 | #if zig_has_builtin(mul_overflow) |
| 633 | zig_i8 full_res; |
| 634 | zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 635 | *res = zig_wrap_i8(full_res, bits); |
| 636 | return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits); |
| 637 | #else |
| 638 | return zig_mulo_i32(res, lhs, rhs, bits); |
| 639 | #endif |
| 406 | 640 | } |
| 407 | 641 | |
| 408 | | static inline short zig_subw_short(short lhs, short rhs, short min, short max) { |
| 409 | | return (short)(((unsigned short)lhs) - ((unsigned short)rhs)); |
| 642 | static inline zig_bool zig_mulo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) { |
| 643 | #if zig_has_builtin(mul_overflow) |
| 644 | zig_u16 full_res; |
| 645 | zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 646 | *res = zig_wrap_u16(full_res, bits); |
| 647 | return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits); |
| 648 | #else |
| 649 | return zig_mulo_u32(res, lhs, rhs, bits); |
| 650 | #endif |
| 410 | 651 | } |
| 411 | 652 | |
| 412 | | static inline int zig_subw_int(int lhs, int rhs, int min, int max) { |
| 413 | | return (int)(((unsigned)lhs) - ((unsigned)rhs)); |
| 653 | static inline zig_bool zig_mulo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) { |
| 654 | #if zig_has_builtin(mul_overflow) |
| 655 | zig_i16 full_res; |
| 656 | zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 657 | *res = zig_wrap_i16(full_res, bits); |
| 658 | return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits); |
| 659 | #else |
| 660 | return zig_mulo_i32(res, lhs, rhs, bits); |
| 661 | #endif |
| 414 | 662 | } |
| 415 | 663 | |
| 416 | | static inline long zig_subw_long(long lhs, long rhs, long min, long max) { |
| 417 | | return (long)(((unsigned long)lhs) - ((unsigned long)rhs)); |
| 664 | #define zig_int_builtins(w) \ |
| 665 | static inline zig_u##w zig_shlw_u##w(zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \ |
| 666 | return zig_wrap_u##w(zig_shl_u##w(lhs, rhs), bits); \ |
| 667 | } \ |
| 668 | \ |
| 669 | static inline zig_i##w zig_shlw_i##w(zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \ |
| 670 | return zig_wrap_i##w((zig_i##w)zig_shl_u##w((zig_u##w)lhs, (zig_u##w)rhs), bits); \ |
| 671 | } \ |
| 672 | \ |
| 673 | static inline zig_u##w zig_addw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 674 | return zig_wrap_u##w(lhs + rhs, bits); \ |
| 675 | } \ |
| 676 | \ |
| 677 | static inline zig_i##w zig_addw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 678 | return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs + (zig_u##w)rhs), bits); \ |
| 679 | } \ |
| 680 | \ |
| 681 | static inline zig_u##w zig_subw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 682 | return zig_wrap_u##w(lhs - rhs, bits); \ |
| 683 | } \ |
| 684 | \ |
| 685 | static inline zig_i##w zig_subw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 686 | return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs - (zig_u##w)rhs), bits); \ |
| 687 | } \ |
| 688 | \ |
| 689 | static inline zig_u##w zig_mulw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 690 | return zig_wrap_u##w(lhs * rhs, bits); \ |
| 691 | } \ |
| 692 | \ |
| 693 | static inline zig_i##w zig_mulw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 694 | return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs * (zig_u##w)rhs), bits); \ |
| 695 | } \ |
| 696 | \ |
| 697 | static inline zig_bool zig_shlo_u##w(zig_u##w *res, zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \ |
| 698 | *res = zig_shlw_u##w(lhs, rhs, bits); \ |
| 699 | return (lhs & zig_maxInt_u##w << (bits - rhs)) != zig_as_u##w(0); \ |
| 700 | } \ |
| 701 | \ |
| 702 | static inline zig_bool zig_shlo_i##w(zig_i##w *res, zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \ |
| 703 | *res = zig_shlw_i##w(lhs, rhs, bits); \ |
| 704 | zig_i##w mask = (zig_i##w)(zig_maxInt_u##w << (bits - rhs - 1)); \ |
| 705 | return (lhs & mask) != zig_as_i##w(0) && (lhs & mask) != mask; \ |
| 706 | } \ |
| 707 | \ |
| 708 | static inline zig_u##w zig_shls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 709 | zig_u##w res; \ |
| 710 | if (rhs >= bits) return lhs != zig_as_u##w(0) ? zig_maxInt(u##w, bits) : lhs; \ |
| 711 | return zig_shlo_u##w(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u##w, bits) : res; \ |
| 712 | } \ |
| 713 | \ |
| 714 | static inline zig_i##w zig_shls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 715 | zig_i##w res; \ |
| 716 | if ((zig_u##w)rhs < (zig_u##w)bits && !zig_shlo_i##w(&res, lhs, rhs, bits)) return res; \ |
| 717 | return lhs < zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \ |
| 718 | } \ |
| 719 | \ |
| 720 | static inline zig_u##w zig_adds_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 721 | zig_u##w res; \ |
| 722 | return zig_addo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \ |
| 723 | } \ |
| 724 | \ |
| 725 | static inline zig_i##w zig_adds_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 726 | zig_i##w res; \ |
| 727 | if (!zig_addo_i##w(&res, lhs, rhs, bits)) return res; \ |
| 728 | return res >= zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \ |
| 729 | } \ |
| 730 | \ |
| 731 | static inline zig_u##w zig_subs_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 732 | zig_u##w res; \ |
| 733 | return zig_subo_u##w(&res, lhs, rhs, bits) ? zig_minInt(u##w, bits) : res; \ |
| 734 | } \ |
| 735 | \ |
| 736 | static inline zig_i##w zig_subs_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 737 | zig_i##w res; \ |
| 738 | if (!zig_subo_i##w(&res, lhs, rhs, bits)) return res; \ |
| 739 | return res >= zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \ |
| 740 | } \ |
| 741 | \ |
| 742 | static inline zig_u##w zig_muls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 743 | zig_u##w res; \ |
| 744 | return zig_mulo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \ |
| 745 | } \ |
| 746 | \ |
| 747 | static inline zig_i##w zig_muls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 748 | zig_i##w res; \ |
| 749 | if (!zig_mulo_i##w(&res, lhs, rhs, bits)) return res; \ |
| 750 | return (lhs ^ rhs) < zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \ |
| 751 | } |
| 752 | zig_int_builtins(8) |
| 753 | zig_int_builtins(16) |
| 754 | zig_int_builtins(32) |
| 755 | zig_int_builtins(64) |
| 756 | |
| 757 | #define zig_builtin8(name, val) __builtin_##name(val) |
| 758 | typedef zig_c_uint zig_Builtin8; |
| 759 | |
| 760 | #define zig_builtin16(name, val) __builtin_##name(val) |
| 761 | typedef zig_c_uint zig_Builtin16; |
| 762 | |
| 763 | #if INT_MIN <= INT32_MIN |
| 764 | #define zig_builtin32(name, val) __builtin_##name(val) |
| 765 | typedef zig_c_uint zig_Builtin32; |
| 766 | #elif LONG_MIN <= INT32_MIN |
| 767 | #define zig_builtin32(name, val) __builtin_##name##l(val) |
| 768 | typedef zig_c_ulong zig_Builtin32; |
| 769 | #endif |
| 770 | |
| 771 | #if INT_MIN <= INT64_MIN |
| 772 | #define zig_builtin64(name, val) __builtin_##name(val) |
| 773 | typedef zig_c_uint zig_Builtin64; |
| 774 | #elif LONG_MIN <= INT64_MIN |
| 775 | #define zig_builtin64(name, val) __builtin_##name##l(val) |
| 776 | typedef zig_c_ulong zig_Builtin64; |
| 777 | #elif LLONG_MIN <= INT64_MIN |
| 778 | #define zig_builtin64(name, val) __builtin_##name##ll(val) |
| 779 | typedef zig_c_ulonglong zig_Builtin64; |
| 780 | #endif |
| 781 | |
| 782 | #if zig_has_builtin(clz) |
| 783 | #define zig_builtin_clz(w) \ |
| 784 | static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \ |
| 785 | if (val == 0) return bits; \ |
| 786 | return zig_builtin##w(clz, val) - (zig_bitSizeOf(zig_Builtin##w) - bits); \ |
| 787 | } \ |
| 788 | \ |
| 789 | static inline zig_u8 zig_clz_i##w(zig_i##w val, zig_u8 bits) { \ |
| 790 | return zig_clz_u##w((zig_u##w)val, bits); \ |
| 791 | } |
| 792 | zig_builtin_clz(8) |
| 793 | zig_builtin_clz(16) |
| 794 | zig_builtin_clz(32) |
| 795 | zig_builtin_clz(64) |
| 796 | #endif |
| 797 | |
| 798 | #if zig_has_builtin(ctz) |
| 799 | #define zig_builtin_ctz(w) \ |
| 800 | static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \ |
| 801 | if (val == 0) return bits; \ |
| 802 | return zig_builtin##w(ctz, val); \ |
| 803 | } \ |
| 804 | \ |
| 805 | static inline zig_u8 zig_ctz_i##w(zig_i##w val, zig_u8 bits) { \ |
| 806 | return zig_ctz_u##w((zig_u##w)val, bits); \ |
| 807 | } |
| 808 | zig_builtin_ctz(8) |
| 809 | zig_builtin_ctz(16) |
| 810 | zig_builtin_ctz(32) |
| 811 | zig_builtin_ctz(64) |
| 812 | #endif |
| 813 | |
| 814 | #if zig_has_builtin(popcount) |
| 815 | #define zig_builtin_popcount(w) \ |
| 816 | static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \ |
| 817 | (void)bits; \ |
| 818 | return zig_builtin##w(popcount, val); \ |
| 819 | } \ |
| 820 | \ |
| 821 | static inline zig_u8 zig_popcount_i##w(zig_i##w val, zig_u8 bits) { \ |
| 822 | \ |
| 823 | return zig_popcount_u##w((zig_u##w)val, bits); \ |
| 824 | } |
| 825 | zig_builtin_popcount(8) |
| 826 | zig_builtin_popcount(16) |
| 827 | zig_builtin_popcount(32) |
| 828 | zig_builtin_popcount(64) |
| 829 | #endif |
| 830 | |
| 831 | static inline zig_u8 zig_byte_swap_u8(zig_u8 val, zig_u8 bits) { |
| 832 | return zig_wrap_u8(val >> (8 - bits), bits); |
| 418 | 833 | } |
| 419 | 834 | |
| 420 | | static inline long long zig_subw_longlong(long long lhs, long long rhs, long long min, long long max) { |
| 421 | | return (long long)(((unsigned long long)lhs) - ((unsigned long long)rhs)); |
| 835 | static inline zig_i8 zig_byte_swap_i8(zig_i8 val, zig_u8 bits) { |
| 836 | return zig_wrap_i8((zig_i8)zig_byte_swap_u8((zig_u8)val, bits), bits); |
| 422 | 837 | } |
| 423 | 838 | |
| 424 | | static inline bool zig_addo_i8(int8_t lhs, int8_t rhs, int8_t *res, int8_t min, int8_t max) { |
| 425 | | #if defined(__GNUC__) && INT8_MAX == INT_MAX |
| 426 | | if (min == INT8_MIN && max == INT8_MAX) { |
| 427 | | return __builtin_sadd_overflow(lhs, rhs, (int*)res); |
| 428 | | } |
| 429 | | #elif defined(__GNUC__) && INT8_MAX == LONG_MAX |
| 430 | | if (min == INT8_MIN && max == INT8_MAX) { |
| 431 | | return __builtin_saddl_overflow(lhs, rhs, (long*)res); |
| 432 | | } |
| 433 | | #elif defined(__GNUC__) && INT8_MAX == LLONG_MAX |
| 434 | | if (min == INT8_MIN && max == INT8_MAX) { |
| 435 | | return __builtin_saddll_overflow(lhs, rhs, (long long*)res); |
| 436 | | } |
| 839 | static inline zig_u16 zig_byte_swap_u16(zig_u16 val, zig_u8 bits) { |
| 840 | zig_u16 full_res; |
| 841 | #if zig_has_builtin(bswap16) |
| 842 | full_res = __builtin_bswap16(val); |
| 843 | #else |
| 844 | full_res = (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 0)) << 8 | |
| 845 | (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 8)) >> 0; |
| 437 | 846 | #endif |
| 438 | | int16_t big_result = (int16_t)lhs + (int16_t)rhs; |
| 439 | | if (big_result > max) { |
| 440 | | *res = big_result - ((int16_t)max - (int16_t)min); |
| 441 | | return true; |
| 442 | | } |
| 443 | | if (big_result < min) { |
| 444 | | *res = big_result + ((int16_t)max - (int16_t)min); |
| 445 | | return true; |
| 446 | | } |
| 447 | | *res = big_result; |
| 448 | | return false; |
| 847 | return zig_wrap_u16(full_res >> (16 - bits), bits); |
| 449 | 848 | } |
| 450 | 849 | |
| 451 | | static inline bool zig_addo_i16(int16_t lhs, int16_t rhs, int16_t *res, int16_t min, int16_t max) { |
| 452 | | #if defined(__GNUC__) && INT16_MAX == INT_MAX |
| 453 | | if (min == INT16_MIN && max == INT16_MAX) { |
| 454 | | return __builtin_sadd_overflow(lhs, rhs, (int*)res); |
| 455 | | } |
| 456 | | #elif defined(__GNUC__) && INT16_MAX == LONG_MAX |
| 457 | | if (min == INT16_MIN && max == INT16_MAX) { |
| 458 | | return __builtin_saddl_overflow(lhs, rhs, (long*)res); |
| 459 | | } |
| 460 | | #elif defined(__GNUC__) && INT16_MAX == LLONG_MAX |
| 461 | | if (min == INT16_MIN && max == INT16_MAX) { |
| 462 | | return __builtin_saddll_overflow(lhs, rhs, (long long*)res); |
| 463 | | } |
| 464 | | #endif |
| 465 | | int32_t big_result = (int32_t)lhs + (int32_t)rhs; |
| 466 | | if (big_result > max) { |
| 467 | | *res = big_result - ((int32_t)max - (int32_t)min); |
| 468 | | return true; |
| 469 | | } |
| 470 | | if (big_result < min) { |
| 471 | | *res = big_result + ((int32_t)max - (int32_t)min); |
| 472 | | return true; |
| 473 | | } |
| 474 | | *res = big_result; |
| 475 | | return false; |
| 850 | static inline zig_i16 zig_byte_swap_i16(zig_i16 val, zig_u8 bits) { |
| 851 | return zig_wrap_i16((zig_i16)zig_byte_swap_u16((zig_u16)val, bits), bits); |
| 476 | 852 | } |
| 477 | 853 | |
| 478 | | static inline bool zig_addo_i32(int32_t lhs, int32_t rhs, int32_t *res, int32_t min, int32_t max) { |
| 479 | | #if defined(__GNUC__) && INT32_MAX == INT_MAX |
| 480 | | if (min == INT32_MIN && max == INT32_MAX) { |
| 481 | | return __builtin_sadd_overflow(lhs, rhs, (int*)res); |
| 482 | | } |
| 483 | | #elif defined(__GNUC__) && INT32_MAX == LONG_MAX |
| 484 | | if (min == INT32_MIN && max == INT32_MAX) { |
| 485 | | return __builtin_saddl_overflow(lhs, rhs, (long*)res); |
| 486 | | } |
| 487 | | #elif defined(__GNUC__) && INT32_MAX == LLONG_MAX |
| 488 | | if (min == INT32_MIN && max == INT32_MAX) { |
| 489 | | return __builtin_saddll_overflow(lhs, rhs, (long long*)res); |
| 490 | | } |
| 491 | | #endif |
| 492 | | int64_t big_result = (int64_t)lhs + (int64_t)rhs; |
| 493 | | if (big_result > max) { |
| 494 | | *res = big_result - ((int64_t)max - (int64_t)min); |
| 495 | | return true; |
| 496 | | } |
| 497 | | if (big_result < min) { |
| 498 | | *res = big_result + ((int64_t)max - (int64_t)min); |
| 499 | | return true; |
| 500 | | } |
| 501 | | *res = big_result; |
| 502 | | return false; |
| 503 | | } |
| 504 | | |
| 505 | | static inline bool zig_addo_i64(int64_t lhs, int64_t rhs, int64_t *res, int64_t min, int64_t max) { |
| 506 | | bool overflow; |
| 507 | | #if defined(__GNUC__) && INT64_MAX == INT_MAX |
| 508 | | overflow = __builtin_sadd_overflow(lhs, rhs, (int*)res); |
| 509 | | #elif defined(__GNUC__) && INT64_MAX == LONG_MAX |
| 510 | | overflow = __builtin_saddl_overflow(lhs, rhs, (long*)res); |
| 511 | | #elif defined(__GNUC__) && INT64_MAX == LLONG_MAX |
| 512 | | overflow = __builtin_saddll_overflow(lhs, rhs, (long long*)res); |
| 854 | static inline zig_u32 zig_byte_swap_u32(zig_u32 val, zig_u8 bits) { |
| 855 | zig_u32 full_res; |
| 856 | #if zig_has_builtin(bswap32) |
| 857 | full_res = __builtin_bswap32(val); |
| 513 | 858 | #else |
| 514 | | int int_overflow; |
| 515 | | *res = __addodi4(lhs, rhs, &int_overflow); |
| 516 | | overflow = int_overflow != 0; |
| 517 | | #endif |
| 518 | | if (!overflow) { |
| 519 | | if (*res > max) { |
| 520 | | // TODO adjust the result to be the truncated bits |
| 521 | | return true; |
| 522 | | } else if (*res < min) { |
| 523 | | // TODO adjust the result to be the truncated bits |
| 524 | | return true; |
| 525 | | } |
| 526 | | } |
| 527 | | return overflow; |
| 859 | full_res = (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 0)) << 16 | |
| 860 | (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 16)) >> 0; |
| 861 | #endif |
| 862 | return zig_wrap_u32(full_res >> (32 - bits), bits); |
| 528 | 863 | } |
| 529 | 864 | |
| 530 | | static inline bool zig_addo_i128(int128_t lhs, int128_t rhs, int128_t *res, int128_t min, int128_t max) { |
| 531 | | bool overflow; |
| 532 | | #if defined(__GNUC__) && INT128_MAX == INT_MAX |
| 533 | | overflow = __builtin_sadd_overflow(lhs, rhs, (int*)res); |
| 534 | | #elif defined(__GNUC__) && INT128_MAX == LONG_MAX |
| 535 | | overflow = __builtin_saddl_overflow(lhs, rhs, (long*)res); |
| 536 | | #elif defined(__GNUC__) && INT128_MAX == LLONG_MAX |
| 537 | | overflow = __builtin_saddll_overflow(lhs, rhs, (long long*)res); |
| 538 | | #else |
| 539 | | int int_overflow; |
| 540 | | *res = __addoti4(lhs, rhs, &int_overflow); |
| 541 | | overflow = int_overflow != 0; |
| 542 | | #endif |
| 543 | | if (!overflow) { |
| 544 | | if (*res > max) { |
| 545 | | // TODO adjust the result to be the truncated bits |
| 546 | | return true; |
| 547 | | } else if (*res < min) { |
| 548 | | // TODO adjust the result to be the truncated bits |
| 549 | | return true; |
| 550 | | } |
| 551 | | } |
| 552 | | return overflow; |
| 865 | static inline zig_i32 zig_byte_swap_i32(zig_i32 val, zig_u8 bits) { |
| 866 | return zig_wrap_i32((zig_i32)zig_byte_swap_u32((zig_u32)val, bits), bits); |
| 553 | 867 | } |
| 554 | 868 | |
| 555 | | static inline bool zig_addo_u8(uint8_t lhs, uint8_t rhs, uint8_t *res, uint8_t max) { |
| 556 | | #if defined(__GNUC__) && UINT8_MAX == UINT_MAX |
| 557 | | if (max == UINT8_MAX) { |
| 558 | | return __builtin_uadd_overflow(lhs, rhs, (unsigned int*)res); |
| 559 | | } |
| 560 | | #elif defined(__GNUC__) && UINT8_MAX == ULONG_MAX |
| 561 | | if (max == UINT8_MAX) { |
| 562 | | return __builtin_uaddl_overflow(lhs, rhs, (unsigned long*)res); |
| 563 | | } |
| 564 | | #elif defined(__GNUC__) && UINT8_MAX == ULLONG_MAX |
| 565 | | if (max == UINT8_MAX) { |
| 566 | | return __builtin_uaddll_overflow(lhs, rhs, (unsigned long long*)res); |
| 567 | | } |
| 869 | static inline zig_u64 zig_byte_swap_u64(zig_u64 val, zig_u8 bits) { |
| 870 | zig_u64 full_res; |
| 871 | #if zig_has_builtin(bswap64) |
| 872 | full_res = __builtin_bswap64(val); |
| 873 | #else |
| 874 | full_res = (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 0)) << 32 | |
| 875 | (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 32)) >> 0; |
| 568 | 876 | #endif |
| 569 | | uint16_t big_result = (uint16_t)lhs + (uint16_t)rhs; |
| 570 | | if (big_result > max) { |
| 571 | | *res = big_result - max - 1; |
| 572 | | return true; |
| 573 | | } |
| 574 | | *res = big_result; |
| 575 | | return false; |
| 877 | return zig_wrap_u64(full_res >> (64 - bits), bits); |
| 576 | 878 | } |
| 577 | 879 | |
| 578 | | static inline uint16_t zig_addo_u16(uint16_t lhs, uint16_t rhs, uint16_t *res, uint16_t max) { |
| 579 | | #if defined(__GNUC__) && UINT16_MAX == UINT_MAX |
| 580 | | if (max == UINT16_MAX) { |
| 581 | | return __builtin_uadd_overflow(lhs, rhs, (unsigned int*)res); |
| 582 | | } |
| 583 | | #elif defined(__GNUC__) && UINT16_MAX == ULONG_MAX |
| 584 | | if (max == UINT16_MAX) { |
| 585 | | return __builtin_uaddl_overflow(lhs, rhs, (unsigned long*)res); |
| 586 | | } |
| 587 | | #elif defined(__GNUC__) && UINT16_MAX == ULLONG_MAX |
| 588 | | if (max == UINT16_MAX) { |
| 589 | | return __builtin_uaddll_overflow(lhs, rhs, (unsigned long long*)res); |
| 590 | | } |
| 591 | | #endif |
| 592 | | uint32_t big_result = (uint32_t)lhs + (uint32_t)rhs; |
| 593 | | if (big_result > max) { |
| 594 | | *res = big_result - max - 1; |
| 595 | | return true; |
| 596 | | } |
| 597 | | *res = big_result; |
| 598 | | return false; |
| 880 | static inline zig_i64 zig_byte_swap_i64(zig_i64 val, zig_u8 bits) { |
| 881 | return zig_wrap_i64((zig_i64)zig_byte_swap_u64((zig_u64)val, bits), bits); |
| 599 | 882 | } |
| 600 | 883 | |
| 601 | | static inline uint32_t zig_addo_u32(uint32_t lhs, uint32_t rhs, uint32_t *res, uint32_t max) { |
| 602 | | #if defined(__GNUC__) && UINT32_MAX == UINT_MAX |
| 603 | | if (max == UINT32_MAX) { |
| 604 | | return __builtin_uadd_overflow(lhs, rhs, (unsigned int*)res); |
| 605 | | } |
| 606 | | #elif defined(__GNUC__) && UINT32_MAX == ULONG_MAX |
| 607 | | if (max == UINT32_MAX) { |
| 608 | | return __builtin_uaddl_overflow(lhs, rhs, (unsigned long*)res); |
| 609 | | } |
| 610 | | #elif defined(__GNUC__) && UINT32_MAX == ULLONG_MAX |
| 611 | | if (max == UINT32_MAX) { |
| 612 | | return __builtin_uaddll_overflow(lhs, rhs, (unsigned long long*)res); |
| 613 | | } |
| 884 | static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) { |
| 885 | zig_u8 full_res; |
| 886 | #if zig_has_builtin(bitreverse8) |
| 887 | full_res = __builtin_bitreverse8(val); |
| 888 | #else |
| 889 | static zig_u8 const lut[0x10] = { |
| 890 | 0b0000, 0b1000, 0b0100, 0b1100, |
| 891 | 0b0010, 0b1010, 0b0110, 0b1110, |
| 892 | 0b0001, 0b1001, 0b0101, 0b1101, |
| 893 | 0b0011, 0b1011, 0b0111, 0b1111, |
| 894 | }; |
| 895 | full_res = lut[val >> 0 & 0xF] << 4 | lut[val >> 4 & 0xF] << 0; |
| 614 | 896 | #endif |
| 615 | | uint64_t big_result = (uint64_t)lhs + (uint64_t)rhs; |
| 616 | | if (big_result > max) { |
| 617 | | *res = big_result - max - 1; |
| 618 | | return true; |
| 619 | | } |
| 620 | | *res = big_result; |
| 621 | | return false; |
| 622 | | } |
| 623 | | |
| 624 | | static inline uint64_t zig_addo_u64(uint64_t lhs, uint64_t rhs, uint64_t *res, uint64_t max) { |
| 625 | | bool overflow; |
| 626 | | #if defined(__GNUC__) && UINT64_MAX == UINT_MAX |
| 627 | | overflow = __builtin_uadd_overflow(lhs, rhs, (unsigned int*)res); |
| 628 | | #elif defined(__GNUC__) && UINT64_MAX == ULONG_MAX |
| 629 | | overflow = __builtin_uaddl_overflow(lhs, rhs, (unsigned long*)res); |
| 630 | | #elif defined(__GNUC__) && UINT64_MAX == ULLONG_MAX |
| 631 | | overflow = __builtin_uaddll_overflow(lhs, rhs, (unsigned long long*)res); |
| 897 | return zig_wrap_u8(full_res >> (8 - bits), bits); |
| 898 | } |
| 899 | |
| 900 | static inline zig_i8 zig_bit_reverse_i8(zig_i8 val, zig_u8 bits) { |
| 901 | return zig_wrap_i8((zig_i8)zig_bit_reverse_u8((zig_u8)val, bits), bits); |
| 902 | } |
| 903 | |
| 904 | static inline zig_u16 zig_bit_reverse_u16(zig_u16 val, zig_u8 bits) { |
| 905 | zig_u16 full_res; |
| 906 | #if zig_has_builtin(bitreverse16) |
| 907 | full_res = __builtin_bitreverse16(val); |
| 632 | 908 | #else |
| 633 | | int int_overflow; |
| 634 | | *res = __uaddodi4(lhs, rhs, &int_overflow); |
| 635 | | overflow = int_overflow != 0; |
| 909 | full_res = (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 0)) << 8 | |
| 910 | (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 8)) >> 0; |
| 636 | 911 | #endif |
| 637 | | if (*res > max && !overflow) { |
| 638 | | *res -= max - 1; |
| 639 | | return true; |
| 640 | | } |
| 641 | | return overflow; |
| 912 | return zig_wrap_u16(full_res >> (16 - bits), bits); |
| 642 | 913 | } |
| 643 | 914 | |
| 644 | | static inline uint128_t zig_addo_u128(uint128_t lhs, uint128_t rhs, uint128_t *res, uint128_t max) { |
| 645 | | int overflow; |
| 646 | | *res = __uaddoti4(lhs, rhs, &overflow); |
| 647 | | if (*res > max && overflow == 0) { |
| 648 | | *res -= max - 1; |
| 649 | | return true; |
| 650 | | } |
| 651 | | return overflow != 0; |
| 915 | static inline zig_i16 zig_bit_reverse_i16(zig_i16 val, zig_u8 bits) { |
| 916 | return zig_wrap_i16((zig_i16)zig_bit_reverse_u16((zig_u16)val, bits), bits); |
| 652 | 917 | } |
| 653 | 918 | |
| 654 | | static inline bool zig_subo_i8(int8_t lhs, int8_t rhs, int8_t *res, int8_t min, int8_t max) { |
| 655 | | #if defined(__GNUC__) && INT8_MAX == INT_MAX |
| 656 | | if (min == INT8_MIN && max == INT8_MAX) { |
| 657 | | return __builtin_ssub_overflow(lhs, rhs, (int*)res); |
| 658 | | } |
| 659 | | #elif defined(__GNUC__) && INT8_MAX == LONG_MAX |
| 660 | | if (min == INT8_MIN && max == INT8_MAX) { |
| 661 | | return __builtin_ssubl_overflow(lhs, rhs, (long*)res); |
| 662 | | } |
| 663 | | #elif defined(__GNUC__) && INT8_MAX == LLONG_MAX |
| 664 | | if (min == INT8_MIN && max == INT8_MAX) { |
| 665 | | return __builtin_ssubll_overflow(lhs, rhs, (long long*)res); |
| 666 | | } |
| 919 | static inline zig_u32 zig_bit_reverse_u32(zig_u32 val, zig_u8 bits) { |
| 920 | zig_u32 full_res; |
| 921 | #if zig_has_builtin(bitreverse32) |
| 922 | full_res = __builtin_bitreverse32(val); |
| 923 | #else |
| 924 | full_res = (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 0)) << 16 | |
| 925 | (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 16)) >> 0; |
| 667 | 926 | #endif |
| 668 | | int16_t big_result = (int16_t)lhs - (int16_t)rhs; |
| 669 | | if (big_result > max) { |
| 670 | | *res = big_result - ((int16_t)max - (int16_t)min); |
| 671 | | return true; |
| 672 | | } |
| 673 | | if (big_result < min) { |
| 674 | | *res = big_result + ((int16_t)max - (int16_t)min); |
| 675 | | return true; |
| 676 | | } |
| 677 | | *res = big_result; |
| 678 | | return false; |
| 927 | return zig_wrap_u32(full_res >> (32 - bits), bits); |
| 679 | 928 | } |
| 680 | 929 | |
| 681 | | static inline bool zig_subo_i16(int16_t lhs, int16_t rhs, int16_t *res, int16_t min, int16_t max) { |
| 682 | | #if defined(__GNUC__) && INT16_MAX == INT_MAX |
| 683 | | if (min == INT16_MIN && max == INT16_MAX) { |
| 684 | | return __builtin_ssub_overflow(lhs, rhs, (int*)res); |
| 685 | | } |
| 686 | | #elif defined(__GNUC__) && INT16_MAX == LONG_MAX |
| 687 | | if (min == INT16_MIN && max == INT16_MAX) { |
| 688 | | return __builtin_ssubl_overflow(lhs, rhs, (long*)res); |
| 689 | | } |
| 690 | | #elif defined(__GNUC__) && INT16_MAX == LLONG_MAX |
| 691 | | if (min == INT16_MIN && max == INT16_MAX) { |
| 692 | | return __builtin_ssubll_overflow(lhs, rhs, (long long*)res); |
| 693 | | } |
| 930 | static inline zig_i32 zig_bit_reverse_i32(zig_i32 val, zig_u8 bits) { |
| 931 | return zig_wrap_i32((zig_i32)zig_bit_reverse_u32((zig_u32)val, bits), bits); |
| 932 | } |
| 933 | |
| 934 | static inline zig_u64 zig_bit_reverse_u64(zig_u64 val, zig_u8 bits) { |
| 935 | zig_u64 full_res; |
| 936 | #if zig_has_builtin(bitreverse64) |
| 937 | full_res = __builtin_bitreverse64(val); |
| 938 | #else |
| 939 | full_res = (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 0)) << 32 | |
| 940 | (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 32)) >> 0; |
| 694 | 941 | #endif |
| 695 | | int32_t big_result = (int32_t)lhs - (int32_t)rhs; |
| 696 | | if (big_result > max) { |
| 697 | | *res = big_result - ((int32_t)max - (int32_t)min); |
| 698 | | return true; |
| 699 | | } |
| 700 | | if (big_result < min) { |
| 701 | | *res = big_result + ((int32_t)max - (int32_t)min); |
| 702 | | return true; |
| 703 | | } |
| 704 | | *res = big_result; |
| 705 | | return false; |
| 942 | return zig_wrap_u64(full_res >> (64 - bits), bits); |
| 706 | 943 | } |
| 707 | 944 | |
| 708 | | static inline bool zig_subo_i32(int32_t lhs, int32_t rhs, int32_t *res, int32_t min, int32_t max) { |
| 709 | | #if defined(__GNUC__) && INT32_MAX == INT_MAX |
| 710 | | if (min == INT32_MIN && max == INT32_MAX) { |
| 711 | | return __builtin_ssub_overflow(lhs, rhs, (int*)res); |
| 712 | | } |
| 713 | | #elif defined(__GNUC__) && INT32_MAX == LONG_MAX |
| 714 | | if (min == INT32_MIN && max == INT32_MAX) { |
| 715 | | return __builtin_ssubl_overflow(lhs, rhs, (long*)res); |
| 716 | | } |
| 717 | | #elif defined(__GNUC__) && INT32_MAX == LLONG_MAX |
| 718 | | if (min == INT32_MIN && max == INT32_MAX) { |
| 719 | | return __builtin_ssubll_overflow(lhs, rhs, (long long*)res); |
| 720 | | } |
| 945 | static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) { |
| 946 | return zig_wrap_i64((zig_i64)zig_bit_reverse_u64((zig_u64)val, bits), bits); |
| 947 | } |
| 948 | |
| 949 | /* ======================== 128-bit Integer Routines ======================== */ |
| 950 | |
| 951 | #if !defined(zig_has_int128) |
| 952 | # if defined(__SIZEOF_INT128__) |
| 953 | # define zig_has_int128 1 |
| 954 | # else |
| 955 | # define zig_has_int128 0 |
| 956 | # endif |
| 721 | 957 | #endif |
| 722 | | int64_t big_result = (int64_t)lhs - (int64_t)rhs; |
| 723 | | if (big_result > max) { |
| 724 | | *res = big_result - ((int64_t)max - (int64_t)min); |
| 725 | | return true; |
| 958 | |
| 959 | #if zig_has_int128 |
| 960 | |
| 961 | typedef unsigned __int128 zig_u128; |
| 962 | typedef signed __int128 zig_i128; |
| 963 | |
| 964 | #define zig_as_u128(hi, lo) ((zig_u128)(hi)<<64|(lo)) |
| 965 | #define zig_as_i128(hi, lo) ((zig_i128)zig_as_u128(hi, lo)) |
| 966 | #define zig_hi_u128(val) ((zig_u64)((val) >> 64)) |
| 967 | #define zig_lo_u128(val) ((zig_u64)((val) >> 0)) |
| 968 | #define zig_hi_i128(val) ((zig_i64)((val) >> 64)) |
| 969 | #define zig_lo_i128(val) ((zig_u64)((val) >> 0)) |
| 970 | #define zig_bitcast_u128(val) ((zig_u128)(val)) |
| 971 | #define zig_bitcast_i128(val) ((zig_i128)(val)) |
| 972 | #define zig_cmp_int128(ZigType, CType) \ |
| 973 | static inline zig_i8 zig_cmp_##ZigType(CType lhs, CType rhs) { \ |
| 974 | return (lhs > rhs) - (lhs < rhs); \ |
| 726 | 975 | } |
| 727 | | if (big_result < min) { |
| 728 | | *res = big_result + ((int64_t)max - (int64_t)min); |
| 729 | | return true; |
| 976 | #define zig_bit_int128(ZigType, CType, operation, operator) \ |
| 977 | static inline CType zig_##operation##_##ZigType(CType lhs, CType rhs) { \ |
| 978 | return lhs operator rhs; \ |
| 730 | 979 | } |
| 731 | | *res = big_result; |
| 732 | | return false; |
| 733 | | } |
| 734 | | |
| 735 | | static inline bool zig_subo_i64(int64_t lhs, int64_t rhs, int64_t *res, int64_t min, int64_t max) { |
| 736 | | bool overflow; |
| 737 | | #if defined(__GNUC__) && INT64_MAX == INT_MAX |
| 738 | | overflow = __builtin_ssub_overflow(lhs, rhs, (int*)res); |
| 739 | | #elif defined(__GNUC__) && INT64_MAX == LONG_MAX |
| 740 | | overflow = __builtin_ssubl_overflow(lhs, rhs, (long*)res); |
| 741 | | #elif defined(__GNUC__) && INT64_MAX == LLONG_MAX |
| 742 | | overflow = __builtin_ssubll_overflow(lhs, rhs, (long long*)res); |
| 980 | |
| 981 | #else /* zig_has_int128 */ |
| 982 | |
| 983 | #if __LITTLE_ENDIAN__ || _MSC_VER |
| 984 | typedef struct { zig_align(16) zig_u64 lo; zig_u64 hi; } zig_u128; |
| 985 | typedef struct { zig_align(16) zig_u64 lo; zig_i64 hi; } zig_i128; |
| 743 | 986 | #else |
| 744 | | int int_overflow; |
| 745 | | *res = __subodi4(lhs, rhs, &int_overflow); |
| 746 | | overflow = int_overflow != 0; |
| 747 | | #endif |
| 748 | | if (!overflow) { |
| 749 | | if (*res > max) { |
| 750 | | // TODO adjust the result to be the truncated bits |
| 751 | | return true; |
| 752 | | } else if (*res < min) { |
| 753 | | // TODO adjust the result to be the truncated bits |
| 754 | | return true; |
| 755 | | } |
| 987 | typedef struct { zig_align(16) zig_u64 hi; zig_u64 lo; } zig_u128; |
| 988 | typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128; |
| 989 | #endif |
| 990 | |
| 991 | #define zig_as_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) }) |
| 992 | #define zig_as_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) }) |
| 993 | #define zig_hi_u128(val) ((val).hi) |
| 994 | #define zig_lo_u128(val) ((val).lo) |
| 995 | #define zig_hi_i128(val) ((val).hi) |
| 996 | #define zig_lo_i128(val) ((val).lo) |
| 997 | #define zig_bitcast_u128(val) zig_as_u128((zig_u64)(val).hi, (val).lo) |
| 998 | #define zig_bitcast_i128(val) zig_as_i128((zig_i64)(val).hi, (val).lo) |
| 999 | #define zig_cmp_int128(ZigType, CType) \ |
| 1000 | static inline zig_c_int zig_cmp_##ZigType(CType lhs, CType rhs) { \ |
| 1001 | return (lhs.hi == rhs.hi) \ |
| 1002 | ? (lhs.lo > rhs.lo) - (lhs.lo < rhs.lo) \ |
| 1003 | : (lhs.hi > rhs.hi) - (lhs.hi < rhs.hi); \ |
| 1004 | } |
| 1005 | #define zig_bit_int128(ZigType, CType, operation, operator) \ |
| 1006 | static inline CType zig_##operation##_##ZigType(CType lhs, CType rhs) { \ |
| 1007 | return (CType){ .hi = lhs.hi operator rhs.hi, .lo = lhs.lo operator rhs.lo }; \ |
| 756 | 1008 | } |
| 757 | | return overflow; |
| 1009 | |
| 1010 | #endif /* zig_has_int128 */ |
| 1011 | |
| 1012 | #define zig_minInt_u128 zig_as_u128(zig_minInt_u64, zig_minInt_u64) |
| 1013 | #define zig_maxInt_u128 zig_as_u128(zig_maxInt_u64, zig_maxInt_u64) |
| 1014 | #define zig_minInt_i128 zig_as_i128(zig_minInt_i64, zig_minInt_u64) |
| 1015 | #define zig_maxInt_i128 zig_as_i128(zig_maxInt_i64, zig_maxInt_u64) |
| 1016 | |
| 1017 | zig_cmp_int128(u128, zig_u128) |
| 1018 | zig_cmp_int128(i128, zig_i128) |
| 1019 | |
| 1020 | zig_bit_int128(u128, zig_u128, and, &) |
| 1021 | zig_bit_int128(i128, zig_i128, and, &) |
| 1022 | |
| 1023 | zig_bit_int128(u128, zig_u128, or, |) |
| 1024 | zig_bit_int128(i128, zig_i128, or, |) |
| 1025 | |
| 1026 | zig_bit_int128(u128, zig_u128, xor, ^) |
| 1027 | zig_bit_int128(i128, zig_i128, xor, ^) |
| 1028 | |
| 1029 | static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs); |
| 1030 | |
| 1031 | #if zig_has_int128 |
| 1032 | |
| 1033 | static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) { |
| 1034 | return val ^ zig_maxInt(u128, bits); |
| 758 | 1035 | } |
| 759 | 1036 | |
| 760 | | static inline bool zig_subo_i128(int128_t lhs, int128_t rhs, int128_t *res, int128_t min, int128_t max) { |
| 761 | | bool overflow; |
| 762 | | #if defined(__GNUC__) && INT128_MAX == INT_MAX |
| 763 | | overflow = __builtin_ssub_overflow(lhs, rhs, (int*)res); |
| 764 | | #elif defined(__GNUC__) && INT128_MAX == LONG_MAX |
| 765 | | overflow = __builtin_ssubl_overflow(lhs, rhs, (long*)res); |
| 766 | | #elif defined(__GNUC__) && INT128_MAX == LLONG_MAX |
| 767 | | overflow = __builtin_ssubll_overflow(lhs, rhs, (long long*)res); |
| 768 | | #else |
| 769 | | int int_overflow; |
| 770 | | *res = __suboti4(lhs, rhs, &int_overflow); |
| 771 | | overflow = int_overflow != 0; |
| 772 | | #endif |
| 773 | | if (!overflow) { |
| 774 | | if (*res > max) { |
| 775 | | // TODO adjust the result to be the truncated bits |
| 776 | | return true; |
| 777 | | } else if (*res < min) { |
| 778 | | // TODO adjust the result to be the truncated bits |
| 779 | | return true; |
| 780 | | } |
| 781 | | } |
| 782 | | return overflow; |
| 1037 | static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) { |
| 1038 | (void)bits; |
| 1039 | return ~val; |
| 783 | 1040 | } |
| 784 | 1041 | |
| 785 | | static inline bool zig_subo_u8(uint8_t lhs, uint8_t rhs, uint8_t *res, uint8_t max) { |
| 786 | | #if defined(__GNUC__) && UINT8_MAX == UINT_MAX |
| 787 | | return __builtin_usub_overflow(lhs, rhs, (unsigned int*)res); |
| 788 | | #elif defined(__GNUC__) && UINT8_MAX == ULONG_MAX |
| 789 | | return __builtin_usubl_overflow(lhs, rhs, (unsigned long*)res); |
| 790 | | #elif defined(__GNUC__) && UINT8_MAX == ULLONG_MAX |
| 791 | | return __builtin_usubll_overflow(lhs, rhs, (unsigned long long*)res); |
| 792 | | #endif |
| 793 | | if (rhs > lhs) { |
| 794 | | *res = max - (rhs - lhs - 1); |
| 795 | | return true; |
| 796 | | } |
| 797 | | *res = lhs - rhs; |
| 798 | | return false; |
| 1042 | static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) { |
| 1043 | return lhs >> rhs; |
| 799 | 1044 | } |
| 800 | 1045 | |
| 801 | | static inline uint16_t zig_subo_u16(uint16_t lhs, uint16_t rhs, uint16_t *res, uint16_t max) { |
| 802 | | #if defined(__GNUC__) && UINT16_MAX == UINT_MAX |
| 803 | | return __builtin_usub_overflow(lhs, rhs, (unsigned int*)res); |
| 804 | | #elif defined(__GNUC__) && UINT16_MAX == ULONG_MAX |
| 805 | | return __builtin_usubl_overflow(lhs, rhs, (unsigned long*)res); |
| 806 | | #elif defined(__GNUC__) && UINT16_MAX == ULLONG_MAX |
| 807 | | return __builtin_usubll_overflow(lhs, rhs, (unsigned long long*)res); |
| 808 | | #endif |
| 809 | | if (rhs > lhs) { |
| 810 | | *res = max - (rhs - lhs - 1); |
| 811 | | return true; |
| 812 | | } |
| 813 | | *res = lhs - rhs; |
| 814 | | return false; |
| 815 | | } |
| 816 | | |
| 817 | | static inline uint32_t zig_subo_u32(uint32_t lhs, uint32_t rhs, uint32_t *res, uint32_t max) { |
| 818 | | if (max == UINT32_MAX) { |
| 819 | | #if defined(__GNUC__) && UINT32_MAX == UINT_MAX |
| 820 | | return __builtin_usub_overflow(lhs, rhs, (unsigned int*)res); |
| 821 | | #elif defined(__GNUC__) && UINT32_MAX == ULONG_MAX |
| 822 | | return __builtin_usubl_overflow(lhs, rhs, (unsigned long*)res); |
| 823 | | #elif defined(__GNUC__) && UINT32_MAX == ULLONG_MAX |
| 824 | | return __builtin_usubll_overflow(lhs, rhs, (unsigned long long*)res); |
| 825 | | #endif |
| 826 | | int int_overflow; |
| 827 | | *res = __usubosi4(lhs, rhs, &int_overflow); |
| 828 | | return int_overflow != 0; |
| 829 | | } else { |
| 830 | | if (rhs > lhs) { |
| 831 | | *res = max - (rhs - lhs - 1); |
| 832 | | return true; |
| 833 | | } |
| 834 | | *res = lhs - rhs; |
| 835 | | return false; |
| 836 | | } |
| 1046 | static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) { |
| 1047 | return lhs << rhs; |
| 837 | 1048 | } |
| 838 | 1049 | |
| 839 | | static inline uint64_t zig_subo_u64(uint64_t lhs, uint64_t rhs, uint64_t *res, uint64_t max) { |
| 840 | | if (max == UINT64_MAX) { |
| 841 | | #if defined(__GNUC__) && UINT64_MAX == UINT_MAX |
| 842 | | return __builtin_usub_overflow(lhs, rhs, (unsigned int*)res); |
| 843 | | #elif defined(__GNUC__) && UINT64_MAX == ULONG_MAX |
| 844 | | return __builtin_usubl_overflow(lhs, rhs, (unsigned long*)res); |
| 845 | | #elif defined(__GNUC__) && UINT64_MAX == ULLONG_MAX |
| 846 | | return __builtin_usubll_overflow(lhs, rhs, (unsigned long long*)res); |
| 847 | | #else |
| 848 | | int int_overflow; |
| 849 | | *res = __usubodi4(lhs, rhs, &int_overflow); |
| 850 | | return int_overflow != 0; |
| 851 | | #endif |
| 852 | | } else { |
| 853 | | if (rhs > lhs) { |
| 854 | | *res = max - (rhs - lhs - 1); |
| 855 | | return true; |
| 856 | | } |
| 857 | | *res = lhs - rhs; |
| 858 | | return false; |
| 859 | | } |
| 1050 | static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) { |
| 1051 | return lhs << rhs; |
| 860 | 1052 | } |
| 861 | 1053 | |
| 862 | | static inline uint128_t zig_subo_u128(uint128_t lhs, uint128_t rhs, uint128_t *res, uint128_t max) { |
| 863 | | if (max == UINT128_MAX) { |
| 864 | | int int_overflow; |
| 865 | | *res = __usuboti4(lhs, rhs, &int_overflow); |
| 866 | | return int_overflow != 0; |
| 867 | | } else { |
| 868 | | if (rhs > lhs) { |
| 869 | | *res = max - (rhs - lhs - 1); |
| 870 | | return true; |
| 871 | | } |
| 872 | | *res = lhs - rhs; |
| 873 | | return false; |
| 874 | | } |
| 1054 | static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1055 | return lhs + rhs; |
| 875 | 1056 | } |
| 876 | 1057 | |
| 877 | | static inline bool zig_mulo_i8(int8_t lhs, int8_t rhs, int8_t *res, int8_t min, int8_t max) { |
| 878 | | #if defined(__GNUC__) && INT8_MAX == INT_MAX |
| 879 | | if (min == INT8_MIN && max == INT8_MAX) { |
| 880 | | return __builtin_smul_overflow(lhs, rhs, (int*)res); |
| 881 | | } |
| 882 | | #elif defined(__GNUC__) && INT8_MAX == LONG_MAX |
| 883 | | if (min == INT8_MIN && max == INT8_MAX) { |
| 884 | | return __builtin_smull_overflow(lhs, rhs, (long*)res); |
| 885 | | } |
| 886 | | #elif defined(__GNUC__) && INT8_MAX == LLONG_MAX |
| 887 | | if (min == INT8_MIN && max == INT8_MAX) { |
| 888 | | return __builtin_smulll_overflow(lhs, rhs, (long long*)res); |
| 889 | | } |
| 890 | | #endif |
| 891 | | int16_t big_result = (int16_t)lhs * (int16_t)rhs; |
| 892 | | if (big_result > max) { |
| 893 | | *res = big_result - ((int16_t)max - (int16_t)min); |
| 894 | | return true; |
| 895 | | } |
| 896 | | if (big_result < min) { |
| 897 | | *res = big_result + ((int16_t)max - (int16_t)min); |
| 898 | | return true; |
| 899 | | } |
| 900 | | *res = big_result; |
| 901 | | return false; |
| 1058 | static inline zig_i128 zig_add_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1059 | return lhs + rhs; |
| 902 | 1060 | } |
| 903 | 1061 | |
| 904 | | static inline bool zig_mulo_i16(int16_t lhs, int16_t rhs, int16_t *res, int16_t min, int16_t max) { |
| 905 | | #if defined(__GNUC__) && INT16_MAX == INT_MAX |
| 906 | | if (min == INT16_MIN && max == INT16_MAX) { |
| 907 | | return __builtin_smul_overflow(lhs, rhs, (int*)res); |
| 908 | | } |
| 909 | | #elif defined(__GNUC__) && INT16_MAX == LONG_MAX |
| 910 | | if (min == INT16_MIN && max == INT16_MAX) { |
| 911 | | return __builtin_smull_overflow(lhs, rhs, (long*)res); |
| 912 | | } |
| 913 | | #elif defined(__GNUC__) && INT16_MAX == LLONG_MAX |
| 914 | | if (min == INT16_MIN && max == INT16_MAX) { |
| 915 | | return __builtin_smulll_overflow(lhs, rhs, (long long*)res); |
| 916 | | } |
| 917 | | #endif |
| 918 | | int32_t big_result = (int32_t)lhs * (int32_t)rhs; |
| 919 | | if (big_result > max) { |
| 920 | | *res = big_result - ((int32_t)max - (int32_t)min); |
| 921 | | return true; |
| 922 | | } |
| 923 | | if (big_result < min) { |
| 924 | | *res = big_result + ((int32_t)max - (int32_t)min); |
| 925 | | return true; |
| 926 | | } |
| 927 | | *res = big_result; |
| 928 | | return false; |
| 1062 | static inline zig_u128 zig_sub_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1063 | return lhs - rhs; |
| 929 | 1064 | } |
| 930 | 1065 | |
| 931 | | static inline bool zig_mulo_i32(int32_t lhs, int32_t rhs, int32_t *res, int32_t min, int32_t max) { |
| 932 | | #if defined(__GNUC__) && INT32_MAX == INT_MAX |
| 933 | | if (min == INT32_MIN && max == INT32_MAX) { |
| 934 | | return __builtin_smul_overflow(lhs, rhs, (int*)res); |
| 935 | | } |
| 936 | | #elif defined(__GNUC__) && INT32_MAX == LONG_MAX |
| 937 | | if (min == INT32_MIN && max == INT32_MAX) { |
| 938 | | return __builtin_smull_overflow(lhs, rhs, (long*)res); |
| 939 | | } |
| 940 | | #elif defined(__GNUC__) && INT32_MAX == LLONG_MAX |
| 941 | | if (min == INT32_MIN && max == INT32_MAX) { |
| 942 | | return __builtin_smulll_overflow(lhs, rhs, (long long*)res); |
| 943 | | } |
| 944 | | #endif |
| 945 | | int64_t big_result = (int64_t)lhs * (int64_t)rhs; |
| 946 | | if (big_result > max) { |
| 947 | | *res = big_result - ((int64_t)max - (int64_t)min); |
| 948 | | return true; |
| 949 | | } |
| 950 | | if (big_result < min) { |
| 951 | | *res = big_result + ((int64_t)max - (int64_t)min); |
| 952 | | return true; |
| 953 | | } |
| 954 | | *res = big_result; |
| 955 | | return false; |
| 956 | | } |
| 957 | | |
| 958 | | static inline bool zig_mulo_i64(int64_t lhs, int64_t rhs, int64_t *res, int64_t min, int64_t max) { |
| 959 | | bool overflow; |
| 960 | | #if defined(__GNUC__) && INT64_MAX == INT_MAX |
| 961 | | overflow = __builtin_smul_overflow(lhs, rhs, (int*)res); |
| 962 | | #elif defined(__GNUC__) && INT64_MAX == LONG_MAX |
| 963 | | overflow = __builtin_smull_overflow(lhs, rhs, (long*)res); |
| 964 | | #elif defined(__GNUC__) && INT64_MAX == LLONG_MAX |
| 965 | | overflow = __builtin_smulll_overflow(lhs, rhs, (long long*)res); |
| 966 | | #else |
| 967 | | int int_overflow; |
| 968 | | *res = __mulodi4(lhs, rhs, &int_overflow); |
| 969 | | overflow = int_overflow != 0; |
| 970 | | #endif |
| 971 | | if (!overflow) { |
| 972 | | if (*res > max) { |
| 973 | | // TODO adjust the result to be the truncated bits |
| 974 | | return true; |
| 975 | | } else if (*res < min) { |
| 976 | | // TODO adjust the result to be the truncated bits |
| 977 | | return true; |
| 978 | | } |
| 979 | | } |
| 980 | | return overflow; |
| 1066 | static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1067 | return lhs - rhs; |
| 981 | 1068 | } |
| 982 | 1069 | |
| 983 | | static inline bool zig_mulo_i128(int128_t lhs, int128_t rhs, int128_t *res, int128_t min, int128_t max) { |
| 984 | | bool overflow; |
| 985 | | #if defined(__GNUC__) && INT128_MAX == INT_MAX |
| 986 | | overflow = __builtin_smul_overflow(lhs, rhs, (int*)res); |
| 987 | | #elif defined(__GNUC__) && INT128_MAX == LONG_MAX |
| 988 | | overflow = __builtin_smull_overflow(lhs, rhs, (long*)res); |
| 989 | | #elif defined(__GNUC__) && INT128_MAX == LLONG_MAX |
| 990 | | overflow = __builtin_smulll_overflow(lhs, rhs, (long long*)res); |
| 991 | | #else |
| 992 | | int int_overflow; |
| 993 | | *res = __muloti4(lhs, rhs, &int_overflow); |
| 994 | | overflow = int_overflow != 0; |
| 995 | | #endif |
| 996 | | if (!overflow) { |
| 997 | | if (*res > max) { |
| 998 | | // TODO adjust the result to be the truncated bits |
| 999 | | return true; |
| 1000 | | } else if (*res < min) { |
| 1001 | | // TODO adjust the result to be the truncated bits |
| 1002 | | return true; |
| 1003 | | } |
| 1004 | | } |
| 1005 | | return overflow; |
| 1070 | static inline zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1071 | return lhs * rhs; |
| 1006 | 1072 | } |
| 1007 | 1073 | |
| 1008 | | static inline bool zig_mulo_u8(uint8_t lhs, uint8_t rhs, uint8_t *res, uint8_t max) { |
| 1009 | | #if defined(__GNUC__) && UINT8_MAX == UINT_MAX |
| 1010 | | if (max == UINT8_MAX) { |
| 1011 | | return __builtin_umul_overflow(lhs, rhs, (unsigned int*)res); |
| 1012 | | } |
| 1013 | | #elif defined(__GNUC__) && UINT8_MAX == ULONG_MAX |
| 1014 | | if (max == UINT8_MAX) { |
| 1015 | | return __builtin_umull_overflow(lhs, rhs, (unsigned long*)res); |
| 1016 | | } |
| 1017 | | #elif defined(__GNUC__) && UINT8_MAX == ULLONG_MAX |
| 1018 | | if (max == UINT8_MAX) { |
| 1019 | | return __builtin_umulll_overflow(lhs, rhs, (unsigned long long*)res); |
| 1020 | | } |
| 1021 | | #endif |
| 1022 | | uint16_t big_result = (uint16_t)lhs * (uint16_t)rhs; |
| 1023 | | if (big_result > max) { |
| 1024 | | *res = big_result - max - 1; |
| 1025 | | return true; |
| 1026 | | } |
| 1027 | | *res = big_result; |
| 1028 | | return false; |
| 1074 | static inline zig_i128 zig_mul_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1075 | return lhs * rhs; |
| 1029 | 1076 | } |
| 1030 | 1077 | |
| 1031 | | static inline uint16_t zig_mulo_u16(uint16_t lhs, uint16_t rhs, uint16_t *res, uint16_t max) { |
| 1032 | | #if defined(__GNUC__) && UINT16_MAX == UINT_MAX |
| 1033 | | if (max == UINT16_MAX) { |
| 1034 | | return __builtin_umul_overflow(lhs, rhs, (unsigned int*)res); |
| 1035 | | } |
| 1036 | | #elif defined(__GNUC__) && UINT16_MAX == ULONG_MAX |
| 1037 | | if (max == UINT16_MAX) { |
| 1038 | | return __builtin_umull_overflow(lhs, rhs, (unsigned long*)res); |
| 1039 | | } |
| 1040 | | #elif defined(__GNUC__) && UINT16_MAX == ULLONG_MAX |
| 1041 | | if (max == UINT16_MAX) { |
| 1042 | | return __builtin_umulll_overflow(lhs, rhs, (unsigned long long*)res); |
| 1043 | | } |
| 1044 | | #endif |
| 1045 | | uint32_t big_result = (uint32_t)lhs * (uint32_t)rhs; |
| 1046 | | if (big_result > max) { |
| 1047 | | *res = big_result - max - 1; |
| 1048 | | return true; |
| 1049 | | } |
| 1050 | | *res = big_result; |
| 1051 | | return false; |
| 1078 | static inline zig_u128 zig_div_trunc_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1079 | return lhs / rhs; |
| 1052 | 1080 | } |
| 1053 | 1081 | |
| 1054 | | static inline uint32_t zig_mulo_u32(uint32_t lhs, uint32_t rhs, uint32_t *res, uint32_t max) { |
| 1055 | | #if defined(__GNUC__) && UINT32_MAX == UINT_MAX |
| 1056 | | if (max == UINT32_MAX) { |
| 1057 | | return __builtin_umul_overflow(lhs, rhs, (unsigned int*)res); |
| 1058 | | } |
| 1059 | | #elif defined(__GNUC__) && UINT32_MAX == ULONG_MAX |
| 1060 | | if (max == UINT32_MAX) { |
| 1061 | | return __builtin_umull_overflow(lhs, rhs, (unsigned long*)res); |
| 1062 | | } |
| 1063 | | #elif defined(__GNUC__) && UINT32_MAX == ULLONG_MAX |
| 1064 | | if (max == UINT32_MAX) { |
| 1065 | | return __builtin_umulll_overflow(lhs, rhs, (unsigned long long*)res); |
| 1066 | | } |
| 1067 | | #endif |
| 1068 | | uint64_t big_result = (uint64_t)lhs * (uint64_t)rhs; |
| 1069 | | if (big_result > max) { |
| 1070 | | *res = big_result - max - 1; |
| 1071 | | return true; |
| 1072 | | } |
| 1073 | | *res = big_result; |
| 1074 | | return false; |
| 1075 | | } |
| 1076 | | |
| 1077 | | static inline uint64_t zig_mulo_u64(uint64_t lhs, uint64_t rhs, uint64_t *res, uint64_t max) { |
| 1078 | | bool overflow; |
| 1079 | | #if defined(__GNUC__) && UINT64_MAX == UINT_MAX |
| 1080 | | overflow = __builtin_umul_overflow(lhs, rhs, (unsigned int*)res); |
| 1081 | | #elif defined(__GNUC__) && UINT64_MAX == ULONG_MAX |
| 1082 | | overflow = __builtin_umull_overflow(lhs, rhs, (unsigned long*)res); |
| 1083 | | #elif defined(__GNUC__) && UINT64_MAX == ULLONG_MAX |
| 1084 | | overflow = __builtin_umulll_overflow(lhs, rhs, (unsigned long long*)res); |
| 1085 | | #else |
| 1086 | | int int_overflow; |
| 1087 | | *res = __umulodi4(lhs, rhs, &int_overflow); |
| 1088 | | overflow = int_overflow != 0; |
| 1089 | | #endif |
| 1090 | | if (*res > max && !overflow) { |
| 1091 | | *res -= max - 1; |
| 1092 | | return true; |
| 1093 | | } |
| 1094 | | return overflow; |
| 1082 | static inline zig_i128 zig_div_trunc_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1083 | return lhs / rhs; |
| 1095 | 1084 | } |
| 1096 | 1085 | |
| 1097 | | static inline uint128_t zig_mulo_u128(uint128_t lhs, uint128_t rhs, uint128_t *res, uint128_t max) { |
| 1098 | | int overflow; |
| 1099 | | *res = __umuloti4(lhs, rhs, &overflow); |
| 1100 | | if (*res > max && overflow == 0) { |
| 1101 | | *res -= max - 1; |
| 1102 | | return true; |
| 1103 | | } |
| 1104 | | return overflow != 0; |
| 1086 | static inline zig_u128 zig_rem_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1087 | return lhs % rhs; |
| 1105 | 1088 | } |
| 1106 | 1089 | |
| 1107 | | static inline float zig_bitcast_f32_u32(uint32_t arg) { |
| 1108 | | float dest; |
| 1109 | | memcpy(&dest, &arg, sizeof dest); |
| 1110 | | return dest; |
| 1090 | static inline zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1091 | return lhs % rhs; |
| 1111 | 1092 | } |
| 1112 | 1093 | |
| 1113 | | static inline float zig_bitcast_f64_u64(uint64_t arg) { |
| 1114 | | double dest; |
| 1115 | | memcpy(&dest, &arg, sizeof dest); |
| 1116 | | return dest; |
| 1094 | static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1095 | return zig_div_trunc_i128(lhs, rhs) - (((lhs ^ rhs) & zig_rem_i128(lhs, rhs)) < zig_as_i128(0, 0)); |
| 1117 | 1096 | } |
| 1118 | 1097 | |
| 1119 | | #define zig_add_sat_u(ZT, T) static inline T zig_adds_##ZT(T x, T y, T max) { \ |
| 1120 | | return (x > max - y) ? max : x + y; \ |
| 1121 | | } |
| 1122 | | |
| 1123 | | #define zig_add_sat_s(ZT, T, T2) static inline T zig_adds_##ZT(T2 x, T2 y, T2 min, T2 max) { \ |
| 1124 | | T2 res = x + y; \ |
| 1125 | | return (res < min) ? min : (res > max) ? max : res; \ |
| 1126 | | } |
| 1127 | | |
| 1128 | | zig_add_sat_u( u8, uint8_t) |
| 1129 | | zig_add_sat_s( i8, int8_t, int16_t) |
| 1130 | | zig_add_sat_u(u16, uint16_t) |
| 1131 | | zig_add_sat_s(i16, int16_t, int32_t) |
| 1132 | | zig_add_sat_u(u32, uint32_t) |
| 1133 | | zig_add_sat_s(i32, int32_t, int64_t) |
| 1134 | | zig_add_sat_u(u64, uint64_t) |
| 1135 | | zig_add_sat_s(i64, int64_t, int128_t) |
| 1136 | | zig_add_sat_s(isize, intptr_t, int128_t) |
| 1137 | | zig_add_sat_s(short, short, int) |
| 1138 | | zig_add_sat_s(int, int, long) |
| 1139 | | zig_add_sat_s(long, long, long long) |
| 1140 | | |
| 1141 | | #define zig_sub_sat_u(ZT, T) static inline T zig_subs_##ZT(T x, T y, T max) { \ |
| 1142 | | return (x > max + y) ? max : x - y; \ |
| 1098 | static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1099 | zig_i128 rem = zig_rem_i128(lhs, rhs); |
| 1100 | return rem + (((lhs ^ rhs) & rem) < zig_as_i128(0, 0) ? rhs : zig_as_i128(0, 0)); |
| 1143 | 1101 | } |
| 1144 | 1102 | |
| 1145 | | #define zig_sub_sat_s(ZT, T, T2) static inline T zig_subs_##ZT(T2 x, T2 y, T2 min, T2 max) { \ |
| 1146 | | T2 res = x - y; \ |
| 1147 | | return (res < min) ? min : (res > max) ? max : res; \ |
| 1148 | | } |
| 1149 | | |
| 1150 | | zig_sub_sat_u( u8, uint8_t) |
| 1151 | | zig_sub_sat_s( i8, int8_t, int16_t) |
| 1152 | | zig_sub_sat_u(u16, uint16_t) |
| 1153 | | zig_sub_sat_s(i16, int16_t, int32_t) |
| 1154 | | zig_sub_sat_u(u32, uint32_t) |
| 1155 | | zig_sub_sat_s(i32, int32_t, int64_t) |
| 1156 | | zig_sub_sat_u(u64, uint64_t) |
| 1157 | | zig_sub_sat_s(i64, int64_t, int128_t) |
| 1158 | | zig_sub_sat_s(isize, intptr_t, int128_t) |
| 1159 | | zig_sub_sat_s(short, short, int) |
| 1160 | | zig_sub_sat_s(int, int, long) |
| 1161 | | zig_sub_sat_s(long, long, long long) |
| 1162 | | |
| 1103 | #else /* zig_has_int128 */ |
| 1163 | 1104 | |
| 1164 | | #define zig_mul_sat_u(ZT, T, T2) static inline T zig_muls_##ZT(T2 x, T2 y, T2 max) { \ |
| 1165 | | T2 res = x * y; \ |
| 1166 | | return (res > max) ? max : res; \ |
| 1105 | static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) { |
| 1106 | return (zig_u128){ .hi = zig_not_u64(val.hi, bits - zig_as_u8(64)), .lo = zig_not_u64(val.lo, zig_as_u8(64)) }; |
| 1167 | 1107 | } |
| 1168 | 1108 | |
| 1169 | | #define zig_mul_sat_s(ZT, T, T2) static inline T zig_muls_##ZT(T2 x, T2 y, T2 min, T2 max) { \ |
| 1170 | | T2 res = x * y; \ |
| 1171 | | return (res < min) ? min : (res > max) ? max : res; \ |
| 1109 | static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) { |
| 1110 | return (zig_i128){ .hi = zig_not_i64(val.hi, bits - zig_as_u8(64)), .lo = zig_not_u64(val.lo, zig_as_u8(64)) }; |
| 1172 | 1111 | } |
| 1173 | 1112 | |
| 1174 | | zig_mul_sat_u(u8, uint8_t, uint16_t) |
| 1175 | | zig_mul_sat_s(i8, int8_t, int16_t) |
| 1176 | | zig_mul_sat_u(u16, uint16_t, uint32_t) |
| 1177 | | zig_mul_sat_s(i16, int16_t, int32_t) |
| 1178 | | zig_mul_sat_u(u32, uint32_t, uint64_t) |
| 1179 | | zig_mul_sat_s(i32, int32_t, int64_t) |
| 1180 | | zig_mul_sat_u(u64, uint64_t, uint128_t) |
| 1181 | | zig_mul_sat_s(i64, int64_t, int128_t) |
| 1182 | | zig_mul_sat_s(isize, intptr_t, int128_t) |
| 1183 | | zig_mul_sat_s(short, short, int) |
| 1184 | | zig_mul_sat_s(int, int, long) |
| 1185 | | zig_mul_sat_s(long, long, long long) |
| 1113 | static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) { |
| 1114 | if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = lhs.hi << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 }; |
| 1115 | return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs }; |
| 1116 | } |
| 1186 | 1117 | |
| 1187 | | #define zig_shl_sat_u(ZT, T, bits) static inline T zig_shls_##ZT(T x, T y, T max) { \ |
| 1188 | | if(x == 0) return 0; \ |
| 1189 | | T bits_set = 64 - __builtin_clzll(x); \ |
| 1190 | | return (bits_set + y > bits) ? max : x << y; \ |
| 1118 | static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) { |
| 1119 | if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = lhs.hi << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 }; |
| 1120 | return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs }; |
| 1191 | 1121 | } |
| 1192 | 1122 | |
| 1193 | | #define zig_shl_sat_s(ZT, T, bits) static inline T zig_shls_##ZT(T x, T y, T min, T max) { \ |
| 1194 | | if(x == 0) return 0; \ |
| 1195 | | T x_twos_comp = x < 0 ? -x : x; \ |
| 1196 | | T bits_set = 64 - __builtin_clzll(x_twos_comp); \ |
| 1197 | | T min_or_max = (x < 0) ? min : max; \ |
| 1198 | | return (y + bits_set > bits ) ? min_or_max : x << y; \ |
| 1123 | static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) { |
| 1124 | if (rhs >= zig_as_u8(64)) return (zig_i128){ .hi = lhs.hi << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 }; |
| 1125 | return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs }; |
| 1199 | 1126 | } |
| 1200 | 1127 | |
| 1201 | | zig_shl_sat_u(u8, uint8_t, 8) |
| 1202 | | zig_shl_sat_s(i8, int8_t, 7) |
| 1203 | | zig_shl_sat_u(u16, uint16_t, 16) |
| 1204 | | zig_shl_sat_s(i16, int16_t, 15) |
| 1205 | | zig_shl_sat_u(u32, uint32_t, 32) |
| 1206 | | zig_shl_sat_s(i32, int32_t, 31) |
| 1207 | | zig_shl_sat_u(u64, uint64_t, 64) |
| 1208 | | zig_shl_sat_s(i64, int64_t, 63) |
| 1209 | | zig_shl_sat_s(isize, intptr_t, ((sizeof(intptr_t)) * CHAR_BIT - 1)) |
| 1210 | | zig_shl_sat_s(short, short, ((sizeof(short )) * CHAR_BIT - 1)) |
| 1211 | | zig_shl_sat_s(int, int, ((sizeof(int )) * CHAR_BIT - 1)) |
| 1212 | | zig_shl_sat_s(long, long, ((sizeof(long )) * CHAR_BIT - 1)) |
| 1128 | static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1129 | zig_u128 res; |
| 1130 | res.hi = lhs.hi + rhs.hi + zig_addo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64); |
| 1131 | return res; |
| 1132 | } |
| 1213 | 1133 | |
| 1214 | | #define zig_bitsizeof(T) (CHAR_BIT * sizeof(T)) |
| 1215 | | #define zig_bit_mask(T, bit_width) \ |
| 1216 | | ((bit_width) == zig_bitsizeof(T) \ |
| 1217 | | ? ((T)-1) \ |
| 1218 | | : (((T)1 << (T)(bit_width)) - 1)) |
| 1134 | static inline zig_i128 zig_add_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1135 | zig_i128 res; |
| 1136 | res.hi = lhs.hi + rhs.hi + zig_addo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64); |
| 1137 | return res; |
| 1138 | } |
| 1219 | 1139 | |
| 1220 | | static inline int zig_clz(unsigned int value, uint8_t zig_type_bit_width) { |
| 1221 | | if (value == 0) return zig_type_bit_width; |
| 1222 | | return __builtin_clz(value) - zig_bitsizeof(unsigned int) + zig_type_bit_width; |
| 1140 | static inline zig_u128 zig_sub_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1141 | zig_u128 res; |
| 1142 | res.hi = lhs.hi - rhs.hi - zig_subo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64); |
| 1143 | return res; |
| 1223 | 1144 | } |
| 1224 | 1145 | |
| 1225 | | static inline int zig_clzl(unsigned long value, uint8_t zig_type_bit_width) { |
| 1226 | | if (value == 0) return zig_type_bit_width; |
| 1227 | | return __builtin_clzl(value) - zig_bitsizeof(unsigned long) + zig_type_bit_width; |
| 1146 | static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1147 | zig_i128 res; |
| 1148 | res.hi = lhs.hi - rhs.hi - zig_subo_u64(&res.lo, lhs.lo, rhs.lo, zig_maxInt_u64); |
| 1149 | return res; |
| 1228 | 1150 | } |
| 1229 | 1151 | |
| 1230 | | static inline int zig_clzll(unsigned long long value, uint8_t zig_type_bit_width) { |
| 1231 | | if (value == 0) return zig_type_bit_width; |
| 1232 | | return __builtin_clzll(value) - zig_bitsizeof(unsigned long long) + zig_type_bit_width; |
| 1152 | static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1153 | 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)); |
| 1233 | 1154 | } |
| 1234 | 1155 | |
| 1235 | | #define zig_clz_u8 zig_clz |
| 1236 | | #define zig_clz_i8 zig_clz |
| 1237 | | #define zig_clz_u16 zig_clz |
| 1238 | | #define zig_clz_i16 zig_clz |
| 1239 | | #define zig_clz_u32 zig_clzl |
| 1240 | | #define zig_clz_i32 zig_clzl |
| 1241 | | #define zig_clz_u64 zig_clzll |
| 1242 | | #define zig_clz_i64 zig_clzll |
| 1156 | static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1157 | zig_i128 rem = zig_rem_i128(lhs, rhs); |
| 1158 | return rem + (((lhs.hi ^ rhs.hi) & rem.hi) < zig_as_i64(0) ? rhs : zig_as_i128(0, 0)); |
| 1159 | } |
| 1160 | |
| 1161 | #endif /* zig_has_int128 */ |
| 1162 | |
| 1163 | #define zig_div_floor_u128 zig_div_trunc_u128 |
| 1164 | #define zig_mod_u128 zig_rem_u128 |
| 1243 | 1165 | |
| 1244 | | static inline int zig_clz_u128(uint128_t value, uint8_t zig_type_bit_width) { |
| 1245 | | if (value == 0) return zig_type_bit_width; |
| 1246 | | const uint128_t mask = zig_bit_mask(uint128_t, zig_type_bit_width); |
| 1247 | | const uint64_t hi = (value & mask) >> 64; |
| 1248 | | const uint64_t lo = (value & mask); |
| 1249 | | const int leading_zeroes = ( |
| 1250 | | hi != 0 ? __builtin_clzll(hi) : 64 + (lo != 0 ? __builtin_clzll(lo) : 64)); |
| 1251 | | return leading_zeroes - zig_bitsizeof(uint128_t) + zig_type_bit_width; |
| 1166 | static inline zig_i128 zig_shr_i128(zig_i128 lhs, zig_u8 rhs) { |
| 1167 | zig_i128 sign_mask = zig_cmp_i128(lhs, zig_as_i128(0, 0)) < 0 ? zig_as_i128(-1, UINT64_MAX) : zig_as_i128(0, 0); |
| 1168 | return zig_xor_i128(zig_bitcast_i128(zig_shr_u128(zig_bitcast_u128(zig_xor_i128(lhs, sign_mask)), rhs)), sign_mask); |
| 1252 | 1169 | } |
| 1253 | 1170 | |
| 1254 | | #define zig_clz_i128 zig_clz_u128 |
| 1171 | static inline zig_u128 zig_wrap_u128(zig_u128 val, zig_u8 bits) { |
| 1172 | return zig_and_u128(val, zig_maxInt(u128, bits)); |
| 1173 | } |
| 1255 | 1174 | |
| 1256 | | static inline int zig_ctz(unsigned int value, uint8_t zig_type_bit_width) { |
| 1257 | | if (value == 0) return zig_type_bit_width; |
| 1258 | | return __builtin_ctz(value & zig_bit_mask(unsigned int, zig_type_bit_width)); |
| 1175 | static inline zig_i128 zig_wrap_i128(zig_i128 val, zig_u8 bits) { |
| 1176 | return zig_as_i128(zig_wrap_i64(zig_hi_i128(val), bits - zig_as_u8(64)), zig_lo_i128(val)); |
| 1259 | 1177 | } |
| 1260 | 1178 | |
| 1261 | | static inline int zig_ctzl(unsigned long value, uint8_t zig_type_bit_width) { |
| 1262 | | if (value == 0) return zig_type_bit_width; |
| 1263 | | return __builtin_ctzl(value & zig_bit_mask(unsigned long, zig_type_bit_width)); |
| 1179 | static inline zig_u128 zig_shlw_u128(zig_u128 lhs, zig_u8 rhs, zig_u8 bits) { |
| 1180 | return zig_wrap_u128(zig_shl_u128(lhs, rhs), bits); |
| 1264 | 1181 | } |
| 1265 | 1182 | |
| 1266 | | static inline int zig_ctzll(unsigned long value, uint8_t zig_type_bit_width) { |
| 1267 | | if (value == 0) return zig_type_bit_width; |
| 1268 | | return __builtin_ctzll(value & zig_bit_mask(unsigned long, zig_type_bit_width)); |
| 1183 | static inline zig_i128 zig_shlw_i128(zig_i128 lhs, zig_u8 rhs, zig_u8 bits) { |
| 1184 | return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits); |
| 1269 | 1185 | } |
| 1270 | 1186 | |
| 1271 | | #define zig_ctz_u8 zig_ctz |
| 1272 | | #define zig_ctz_i8 zig_ctz |
| 1273 | | #define zig_ctz_u16 zig_ctz |
| 1274 | | #define zig_ctz_i16 zig_ctz |
| 1275 | | #define zig_ctz_u32 zig_ctzl |
| 1276 | | #define zig_ctz_i32 zig_ctzl |
| 1277 | | #define zig_ctz_u64 zig_ctzll |
| 1278 | | #define zig_ctz_i64 zig_ctzll |
| 1187 | static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u8 rhs, zig_u8 bits) { |
| 1188 | return zig_wrap_u128(zig_add_u128(lhs, rhs), bits); |
| 1189 | } |
| 1279 | 1190 | |
| 1280 | | static inline int zig_ctz_u128(uint128_t value, uint8_t zig_type_bit_width) { |
| 1281 | | const uint128_t mask = zig_bit_mask(uint128_t, zig_type_bit_width); |
| 1282 | | const uint64_t hi = (value & mask) >> 64; |
| 1283 | | const uint64_t lo = (value & mask); |
| 1284 | | return (lo != 0 ? __builtin_ctzll(lo) : 64 + (hi != 0 ? __builtin_ctzll(hi) : 64)); |
| 1191 | static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1192 | return zig_wrap_i128(zig_bitcast_i128(zig_add_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits); |
| 1285 | 1193 | } |
| 1286 | 1194 | |
| 1287 | | #define zig_ctz_i128 zig_ctz_u128 |
| 1195 | static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1196 | return zig_wrap_u128(zig_sub_u128(lhs, rhs), bits); |
| 1197 | } |
| 1288 | 1198 | |
| 1289 | | static inline int zig_popcount(unsigned int value, uint8_t zig_type_bit_width) { |
| 1290 | | return __builtin_popcount(value & zig_bit_mask(unsigned int, zig_type_bit_width)); |
| 1199 | static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1200 | return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits); |
| 1291 | 1201 | } |
| 1292 | 1202 | |
| 1293 | | static inline int zig_popcountl(unsigned long value, uint8_t zig_type_bit_width) { |
| 1294 | | return __builtin_popcountl(value & zig_bit_mask(unsigned long, zig_type_bit_width)); |
| 1203 | static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1204 | return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits); |
| 1295 | 1205 | } |
| 1296 | 1206 | |
| 1297 | | static inline int zig_popcountll(unsigned long value, uint8_t zig_type_bit_width) { |
| 1298 | | return __builtin_popcountll(value & zig_bit_mask(unsigned long, zig_type_bit_width)); |
| 1207 | static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1208 | return zig_wrap_i128(zig_bitcast_i128(zig_mul_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits); |
| 1299 | 1209 | } |
| 1300 | 1210 | |
| 1301 | | #define zig_popcount_u8 zig_popcount |
| 1302 | | #define zig_popcount_i8 zig_popcount |
| 1303 | | #define zig_popcount_u16 zig_popcount |
| 1304 | | #define zig_popcount_i16 zig_popcount |
| 1305 | | #define zig_popcount_u32 zig_popcountl |
| 1306 | | #define zig_popcount_i32 zig_popcountl |
| 1307 | | #define zig_popcount_u64 zig_popcountll |
| 1308 | | #define zig_popcount_i64 zig_popcountll |
| 1211 | #if zig_has_int128 |
| 1309 | 1212 | |
| 1310 | | static inline int zig_popcount_u128(uint128_t value, uint8_t zig_type_bit_width) { |
| 1311 | | const uint128_t mask = zig_bit_mask(uint128_t, zig_type_bit_width); |
| 1312 | | const uint64_t hi = (value & mask) >> 64; |
| 1313 | | const uint64_t lo = (value & mask); |
| 1314 | | return __builtin_popcountll(hi) + __builtin_popcountll(lo); |
| 1213 | static inline zig_bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, zig_u8 rhs, zig_u8 bits) { |
| 1214 | *res = zig_shlw_u128(lhs, rhs, bits); |
| 1215 | return zig_and_u128(lhs, zig_shl_u128(zig_maxInt_u128, bits - rhs)) != zig_as_u128(0, 0); |
| 1315 | 1216 | } |
| 1316 | 1217 | |
| 1317 | | #define zig_popcount_i128 zig_popcount_u128 |
| 1218 | static inline zig_bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, zig_u8 rhs, zig_u8 bits) { |
| 1219 | *res = zig_shlw_i128(lhs, rhs, bits); |
| 1220 | zig_i128 mask = zig_bitcast_i128(zig_shl_u128(zig_maxInt_u128, bits - rhs - zig_as_u8(1))); |
| 1221 | return zig_cmp_i128(zig_and_i128(lhs, mask), zig_as_i128(0, 0)) != 0 && |
| 1222 | zig_cmp_i128(zig_and_i128(lhs, mask), mask) != 0; |
| 1223 | } |
| 1318 | 1224 | |
| 1319 | | static inline bool zig_shlo_i8(int8_t lhs, int8_t rhs, int8_t *res, uint8_t bits) { |
| 1320 | | *res = lhs << rhs; |
| 1321 | | if (zig_clz_i8(lhs, bits) >= rhs) return false; |
| 1322 | | *res &= UINT8_MAX >> (8 - bits); |
| 1323 | | return true; |
| 1225 | static inline zig_bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1226 | #if zig_has_builtin(add_overflow) |
| 1227 | zig_u128 full_res; |
| 1228 | zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1229 | *res = zig_wrap_u128(full_res, bits); |
| 1230 | return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits); |
| 1231 | #else |
| 1232 | *res = zig_addw_u128(lhs, rhs, bits); |
| 1233 | return *res < lhs; |
| 1234 | #endif |
| 1324 | 1235 | } |
| 1325 | 1236 | |
| 1326 | | static inline bool zig_shlo_i16(int16_t lhs, int16_t rhs, int16_t *res, uint8_t bits) { |
| 1327 | | *res = lhs << rhs; |
| 1328 | | if (zig_clz_i16(lhs, bits) >= rhs) return false; |
| 1329 | | *res &= UINT16_MAX >> (16 - bits); |
| 1330 | | return true; |
| 1237 | zig_extern_c zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow); |
| 1238 | static inline zig_bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1239 | #if zig_has_builtin(add_overflow) |
| 1240 | zig_i128 full_res; |
| 1241 | zig_bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1242 | #else |
| 1243 | zig_c_int overflow_int; |
| 1244 | zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int); |
| 1245 | zig_bool overflow = overflow_int != 0; |
| 1246 | #endif |
| 1247 | *res = zig_wrap_i128(full_res, bits); |
| 1248 | return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits); |
| 1331 | 1249 | } |
| 1332 | 1250 | |
| 1333 | | static inline bool zig_shlo_i32(int32_t lhs, int32_t rhs, int32_t *res, uint8_t bits) { |
| 1334 | | *res = lhs << rhs; |
| 1335 | | if (zig_clz_i32(lhs, bits) >= rhs) return false; |
| 1336 | | *res &= UINT32_MAX >> (32 - bits); |
| 1337 | | return true; |
| 1251 | static inline zig_bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1252 | #if zig_has_builtin(sub_overflow) |
| 1253 | zig_u128 full_res; |
| 1254 | zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1255 | *res = zig_wrap_u128(full_res, bits); |
| 1256 | return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits); |
| 1257 | #else |
| 1258 | *res = zig_subw_u128(lhs, rhs, bits); |
| 1259 | return *res > lhs; |
| 1260 | #endif |
| 1338 | 1261 | } |
| 1339 | 1262 | |
| 1340 | | static inline bool zig_shlo_i64(int64_t lhs, int64_t rhs, int64_t *res, uint8_t bits) { |
| 1341 | | *res = lhs << rhs; |
| 1342 | | if (zig_clz_i64(lhs, bits) >= rhs) return false; |
| 1343 | | *res &= UINT64_MAX >> (64 - bits); |
| 1344 | | return true; |
| 1263 | zig_extern_c zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow); |
| 1264 | static inline zig_bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1265 | #if zig_has_builtin(sub_overflow) |
| 1266 | zig_i128 full_res; |
| 1267 | zig_bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1268 | #else |
| 1269 | zig_c_int overflow_int; |
| 1270 | zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int); |
| 1271 | zig_bool overflow = overflow_int != 0; |
| 1272 | #endif |
| 1273 | *res = zig_wrap_i128(full_res, bits); |
| 1274 | return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits); |
| 1345 | 1275 | } |
| 1346 | 1276 | |
| 1347 | | static inline bool zig_shlo_i128(int128_t lhs, int128_t rhs, int128_t *res, uint8_t bits) { |
| 1348 | | *res = lhs << rhs; |
| 1349 | | if (zig_clz_i128(lhs, bits) >= rhs) return false; |
| 1350 | | *res &= UINT128_MAX >> (128 - bits); |
| 1351 | | return true; |
| 1277 | static inline zig_bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1278 | #if zig_has_builtin(mul_overflow) |
| 1279 | zig_u128 full_res; |
| 1280 | zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1281 | *res = zig_wrap_u128(full_res, bits); |
| 1282 | return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits); |
| 1283 | #else |
| 1284 | *res = zig_mulw_u128(lhs, rhs, bits); |
| 1285 | return rhs != zig_as_u128(0, 0) && lhs > zig_maxInt(u128, bits) / rhs; |
| 1286 | #endif |
| 1352 | 1287 | } |
| 1353 | 1288 | |
| 1354 | | static inline bool zig_shlo_u8(uint8_t lhs, uint8_t rhs, uint8_t *res, uint8_t bits) { |
| 1355 | | *res = lhs << rhs; |
| 1356 | | if (zig_clz_u8(lhs, bits) >= rhs) return false; |
| 1357 | | *res &= UINT8_MAX >> (8 - bits); |
| 1358 | | return true; |
| 1289 | zig_extern_c zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow); |
| 1290 | static inline zig_bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1291 | #if zig_has_builtin(mul_overflow) |
| 1292 | zig_i128 full_res; |
| 1293 | zig_bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1294 | #else |
| 1295 | zig_c_int overflow_int; |
| 1296 | zig_i128 full_res = __muloti4(lhs, rhs, &overflow); |
| 1297 | zig_bool overflow = overflow_int != 0; |
| 1298 | #endif |
| 1299 | *res = zig_wrap_i128(full_res, bits); |
| 1300 | return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits); |
| 1359 | 1301 | } |
| 1360 | 1302 | |
| 1361 | | static inline uint16_t zig_shlo_u16(uint16_t lhs, uint16_t rhs, uint16_t *res, uint8_t bits) { |
| 1362 | | *res = lhs << rhs; |
| 1363 | | if (zig_clz_u16(lhs, bits) >= rhs) return false; |
| 1364 | | *res &= UINT16_MAX >> (16 - bits); |
| 1365 | | return true; |
| 1303 | #else /* zig_has_int128 */ |
| 1304 | |
| 1305 | static inline zig_bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs) { |
| 1306 | return zig_addo_u64(&res->hi, lhs.hi, rhs.hi, UINT64_MAX) | |
| 1307 | zig_addo_u64(&res->hi, res->hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, UINT64_MAX)); |
| 1366 | 1308 | } |
| 1367 | 1309 | |
| 1368 | | static inline uint32_t zig_shlo_u32(uint32_t lhs, uint32_t rhs, uint32_t *res, uint8_t bits) { |
| 1369 | | *res = lhs << rhs; |
| 1370 | | if (zig_clz_u32(lhs, bits) >= rhs) return false; |
| 1371 | | *res &= UINT32_MAX >> (32 - bits); |
| 1372 | | return true; |
| 1310 | static inline zig_bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs) { |
| 1311 | return zig_subo_u64(&res->hi, lhs.hi, rhs.hi, UINT64_MAX) | |
| 1312 | zig_subo_u64(&res->hi, res->hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, UINT64_MAX)); |
| 1373 | 1313 | } |
| 1374 | 1314 | |
| 1375 | | static inline uint64_t zig_shlo_u64(uint64_t lhs, uint64_t rhs, uint64_t *res, uint8_t bits) { |
| 1376 | | *res = lhs << rhs; |
| 1377 | | if (zig_clz_u64(lhs, bits) >= rhs) return false; |
| 1378 | | *res &= UINT64_MAX >> (64 - bits); |
| 1379 | | return true; |
| 1315 | #endif /* zig_has_int128 */ |
| 1316 | |
| 1317 | static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1318 | zig_u128 res; |
| 1319 | if (zig_cmp_u128(rhs, zig_as_u128(0, bits)) >= 0) |
| 1320 | return zig_cmp_u128(lhs, zig_as_u128(0, 0)) != 0 ? zig_maxInt(u128, bits) : lhs; |
| 1321 | return zig_shlo_u128(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u128, bits) : res; |
| 1380 | 1322 | } |
| 1381 | 1323 | |
| 1382 | | static inline uint128_t zig_shlo_u128(uint128_t lhs, uint128_t rhs, uint128_t *res, uint8_t bits) { |
| 1383 | | *res = lhs << rhs; |
| 1384 | | if (zig_clz_u128(lhs, bits) >= rhs) return false; |
| 1385 | | *res &= UINT128_MAX >> (128 - bits); |
| 1386 | | return true; |
| 1324 | static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1325 | zig_i128 res; |
| 1326 | if (zig_cmp_u128(zig_bitcast_u128(rhs), zig_as_u128(0, bits)) < 0 && !zig_shlo_i128(&res, lhs, rhs, bits)) return res; |
| 1327 | return zig_cmp_i128(lhs, zig_as_i128(0, 0)) < 0 ? zig_minInt(i128, bits) : zig_maxInt(i128, bits); |
| 1387 | 1328 | } |
| 1388 | 1329 | |
| 1389 | | #define zig_sign_extend(T) \ |
| 1390 | | static inline T zig_sign_extend_##T(T value, uint8_t zig_type_bit_width) { \ |
| 1391 | | const T m = (T)1 << (T)(zig_type_bit_width - 1); \ |
| 1392 | | return (value ^ m) - m; \ |
| 1393 | | } |
| 1330 | static inline zig_u128 zig_adds_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1331 | zig_u128 res; |
| 1332 | return zig_addo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res; |
| 1333 | } |
| 1394 | 1334 | |
| 1395 | | zig_sign_extend(uint8_t) |
| 1396 | | zig_sign_extend(uint16_t) |
| 1397 | | zig_sign_extend(uint32_t) |
| 1398 | | zig_sign_extend(uint64_t) |
| 1399 | | zig_sign_extend(uint128_t) |
| 1335 | static inline zig_i128 zig_adds_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1336 | zig_i128 res; |
| 1337 | if (!zig_addo_i128(&res, lhs, rhs, bits)) return res; |
| 1338 | return zig_cmp_i128(res, zig_as_i128(0, 0)) >= 0 ? zig_minInt(i128, bits) : zig_maxInt(i128, bits); |
| 1339 | } |
| 1400 | 1340 | |
| 1401 | | #define zig_byte_swap_u(ZigTypeBits, CTypeBits) \ |
| 1402 | | static inline uint##CTypeBits##_t zig_byte_swap_u##ZigTypeBits(uint##CTypeBits##_t value, uint8_t zig_type_bit_width) { \ |
| 1403 | | return __builtin_bswap##CTypeBits(value) >> (CTypeBits - zig_type_bit_width); \ |
| 1404 | | } |
| 1341 | static inline zig_u128 zig_subs_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1342 | zig_u128 res; |
| 1343 | return zig_subo_u128(&res, lhs, rhs, bits) ? zig_minInt(u128, bits) : res; |
| 1344 | } |
| 1405 | 1345 | |
| 1406 | | #define zig_byte_swap_s(ZigTypeBits, CTypeBits) \ |
| 1407 | | static inline int##CTypeBits##_t zig_byte_swap_i##ZigTypeBits(int##CTypeBits##_t value, uint8_t zig_type_bit_width) { \ |
| 1408 | | const uint##CTypeBits##_t swapped = zig_byte_swap_u##ZigTypeBits(value, zig_type_bit_width); \ |
| 1409 | | return zig_sign_extend_uint##CTypeBits##_t(swapped, zig_type_bit_width); \ |
| 1410 | | } |
| 1346 | static inline zig_i128 zig_subs_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1347 | zig_i128 res; |
| 1348 | if (!zig_subo_i128(&res, lhs, rhs, bits)) return res; |
| 1349 | return zig_cmp_i128(res, zig_as_i128(0, 0)) >= 0 ? zig_minInt(i128, bits) : zig_maxInt(i128, bits); |
| 1350 | } |
| 1351 | |
| 1352 | static inline zig_u128 zig_muls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1353 | zig_u128 res; |
| 1354 | return zig_mulo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res; |
| 1355 | } |
| 1356 | |
| 1357 | static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1358 | zig_i128 res; |
| 1359 | if (!zig_mulo_i128(&res, lhs, rhs, bits)) return res; |
| 1360 | return zig_cmp_i128(zig_xor_i128(lhs, rhs), zig_as_i128(0, 0)) < 0 ? zig_minInt(i128, bits) : zig_maxInt(i128, bits); |
| 1361 | } |
| 1362 | |
| 1363 | static inline zig_u8 zig_clz_u128(zig_u128 val, zig_u8 bits) { |
| 1364 | if (zig_hi_u128(val) != 0) return zig_clz_u64(zig_hi_u128(val), bits - zig_as_u8(64)); |
| 1365 | return zig_clz_u64(zig_lo_u128(val), zig_as_u8(64)) + zig_as_u8(64); |
| 1366 | } |
| 1367 | |
| 1368 | static inline zig_u8 zig_clz_i128(zig_i128 val, zig_u8 bits) { |
| 1369 | return zig_clz_u128(zig_bitcast_u128(val), bits); |
| 1370 | } |
| 1371 | |
| 1372 | static inline zig_u8 zig_ctz_u128(zig_u128 val, zig_u8 bits) { |
| 1373 | if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), zig_as_u8(64)); |
| 1374 | return zig_ctz_u64(zig_hi_u128(val), bits - zig_as_u8(64)) + zig_as_u8(64); |
| 1375 | } |
| 1376 | |
| 1377 | static inline zig_u8 zig_ctz_i128(zig_i128 val, zig_u8 bits) { |
| 1378 | return zig_ctz_u128(zig_bitcast_u128(val), bits); |
| 1379 | } |
| 1380 | |
| 1381 | static inline zig_u8 zig_popcount_u128(zig_u128 val, zig_u8 bits) { |
| 1382 | return zig_popcount_u64(zig_hi_u128(val), bits - zig_as_u8(64)) + |
| 1383 | zig_popcount_u64(zig_lo_u128(val), zig_as_u8(64)); |
| 1384 | } |
| 1385 | |
| 1386 | static inline zig_u8 zig_popcount_i128(zig_i128 val, zig_u8 bits) { |
| 1387 | return zig_popcount_u128(zig_bitcast_u128(val), bits); |
| 1388 | } |
| 1389 | |
| 1390 | static inline zig_u128 zig_byte_swap_u128(zig_u128 val, zig_u8 bits) { |
| 1391 | zig_u128 full_res; |
| 1392 | #if zig_has_builtin(bswap128) |
| 1393 | full_res = __builtin_bswap128(val); |
| 1394 | #else |
| 1395 | full_res = zig_as_u128(zig_byte_swap_u64(zig_lo_u128(val), zig_as_u8(64)), |
| 1396 | zig_byte_swap_u64(zig_hi_u128(val), zig_as_u8(64))); |
| 1397 | #endif |
| 1398 | return zig_shr_u128(full_res, zig_as_u8(128) - bits); |
| 1399 | } |
| 1411 | 1400 | |
| 1412 | | #define zig_byte_swap(ZigTypeBits, CTypeBits) \ |
| 1413 | | zig_byte_swap_u(ZigTypeBits, CTypeBits) \ |
| 1414 | | zig_byte_swap_s(ZigTypeBits, CTypeBits) |
| 1415 | | |
| 1416 | | zig_byte_swap( 8, 16) |
| 1417 | | zig_byte_swap(16, 16) |
| 1418 | | zig_byte_swap(32, 32) |
| 1419 | | zig_byte_swap(64, 64) |
| 1420 | | |
| 1421 | | static inline uint128_t zig_byte_swap_u128(uint128_t value, uint8_t zig_type_bit_width) { |
| 1422 | | const uint128_t mask = zig_bit_mask(uint128_t, zig_type_bit_width); |
| 1423 | | const uint128_t hi = __builtin_bswap64((uint64_t)(value >> 64)); |
| 1424 | | const uint128_t lo = __builtin_bswap64((uint64_t)value); |
| 1425 | | return (((lo << 64 | hi) >> (128 - zig_type_bit_width))) & mask; |
| 1426 | | } |
| 1427 | | |
| 1428 | | zig_byte_swap_s(128, 128) |
| 1429 | | |
| 1430 | | static const uint8_t zig_bit_reverse_lut[256] = { |
| 1431 | | 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, |
| 1432 | | 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, |
| 1433 | | 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, |
| 1434 | | 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, |
| 1435 | | 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, |
| 1436 | | 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, |
| 1437 | | 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca, |
| 1438 | | 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, |
| 1439 | | 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, |
| 1440 | | 0x36, 0xb6, 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, |
| 1441 | | 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1, |
| 1442 | | 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, |
| 1443 | | 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, |
| 1444 | | 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, |
| 1445 | | 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0x0d, 0x8d, 0x4d, 0xcd, |
| 1446 | | 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, |
| 1447 | | 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, |
| 1448 | | 0x33, 0xb3, 0x73, 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, |
| 1449 | | 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, |
| 1450 | | 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, |
| 1451 | | 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, |
| 1452 | | 0x3f, 0xbf, 0x7f, 0xff |
| 1453 | | }; |
| 1454 | | |
| 1455 | | static inline uint8_t zig_bit_reverse_u8(uint8_t value, uint8_t zig_type_bit_width) { |
| 1456 | | const uint8_t reversed = zig_bit_reverse_lut[value] >> (8 - zig_type_bit_width); |
| 1457 | | return zig_sign_extend_uint8_t(reversed, zig_type_bit_width); |
| 1458 | | } |
| 1459 | | |
| 1460 | | #define zig_bit_reverse_i8 zig_bit_reverse_u8 |
| 1461 | | |
| 1462 | | static inline uint16_t zig_bit_reverse_u16(uint16_t value, uint8_t zig_type_bit_width) { |
| 1463 | | const uint16_t swapped = zig_byte_swap_u16(value, zig_type_bit_width); |
| 1464 | | const uint16_t reversed = ( |
| 1465 | | ((uint16_t)zig_bit_reverse_lut[(swapped >> 0x08) & 0xff] << 0x08) | |
| 1466 | | ((uint16_t)zig_bit_reverse_lut[(swapped >> 0x00) & 0xff] << 0x00)); |
| 1467 | | return zig_sign_extend_uint16_t( |
| 1468 | | reversed & zig_bit_mask(uint16_t, zig_type_bit_width), |
| 1469 | | zig_type_bit_width); |
| 1470 | | } |
| 1471 | | |
| 1472 | | #define zig_bit_reverse_i16 zig_bit_reverse_u16 |
| 1473 | | |
| 1474 | | static inline uint32_t zig_bit_reverse_u32(uint32_t value, uint8_t zig_type_bit_width) { |
| 1475 | | const uint32_t swapped = zig_byte_swap_u32(value, zig_type_bit_width); |
| 1476 | | const uint32_t reversed = ( |
| 1477 | | ((uint32_t)zig_bit_reverse_lut[(swapped >> 0x18) & 0xff] << 0x18) | |
| 1478 | | ((uint32_t)zig_bit_reverse_lut[(swapped >> 0x10) & 0xff] << 0x10) | |
| 1479 | | ((uint32_t)zig_bit_reverse_lut[(swapped >> 0x08) & 0xff] << 0x08) | |
| 1480 | | ((uint32_t)zig_bit_reverse_lut[(swapped >> 0x00) & 0xff] << 0x00)); |
| 1481 | | return zig_sign_extend_uint32_t( |
| 1482 | | reversed & zig_bit_mask(uint32_t, zig_type_bit_width), |
| 1483 | | zig_type_bit_width); |
| 1484 | | } |
| 1485 | | |
| 1486 | | #define zig_bit_reverse_i32 zig_bit_reverse_u32 |
| 1487 | | |
| 1488 | | static inline uint64_t zig_bit_reverse_u64(uint64_t value, uint8_t zig_type_bit_width) { |
| 1489 | | const uint64_t swapped = zig_byte_swap_u64(value, zig_type_bit_width); |
| 1490 | | const uint64_t reversed = ( |
| 1491 | | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x38) & 0xff] << 0x38) | |
| 1492 | | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x30) & 0xff] << 0x30) | |
| 1493 | | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x28) & 0xff] << 0x28) | |
| 1494 | | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x20) & 0xff] << 0x20) | |
| 1495 | | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x18) & 0xff] << 0x18) | |
| 1496 | | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x10) & 0xff] << 0x10) | |
| 1497 | | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x08) & 0xff] << 0x08) | |
| 1498 | | ((uint64_t)zig_bit_reverse_lut[(swapped >> 0x00) & 0xff] << 0x00)); |
| 1499 | | return zig_sign_extend_uint64_t( |
| 1500 | | reversed & zig_bit_mask(uint64_t, zig_type_bit_width), |
| 1501 | | zig_type_bit_width); |
| 1502 | | } |
| 1503 | | |
| 1504 | | #define zig_bit_reverse_i64 zig_bit_reverse_u64 |
| 1505 | | |
| 1506 | | static inline uint128_t zig_bit_reverse_u128(uint128_t value, uint8_t zig_type_bit_width) { |
| 1507 | | const uint128_t swapped = zig_byte_swap_u128(value, zig_type_bit_width); |
| 1508 | | const uint128_t reversed = ( |
| 1509 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x78) & 0xff] << 0x78) | |
| 1510 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x70) & 0xff] << 0x70) | |
| 1511 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x68) & 0xff] << 0x68) | |
| 1512 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x60) & 0xff] << 0x60) | |
| 1513 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x58) & 0xff] << 0x58) | |
| 1514 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x50) & 0xff] << 0x50) | |
| 1515 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x48) & 0xff] << 0x48) | |
| 1516 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x40) & 0xff] << 0x40) | |
| 1517 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x38) & 0xff] << 0x38) | |
| 1518 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x30) & 0xff] << 0x30) | |
| 1519 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x28) & 0xff] << 0x28) | |
| 1520 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x20) & 0xff] << 0x20) | |
| 1521 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x18) & 0xff] << 0x18) | |
| 1522 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x10) & 0xff] << 0x10) | |
| 1523 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x08) & 0xff] << 0x08) | |
| 1524 | | ((uint128_t)zig_bit_reverse_lut[(swapped >> 0x00) & 0xff] << 0x00)); |
| 1525 | | return zig_sign_extend_uint128_t( |
| 1526 | | reversed & zig_bit_mask(uint128_t, zig_type_bit_width), |
| 1527 | | zig_type_bit_width); |
| 1528 | | } |
| 1529 | | |
| 1530 | | #define zig_bit_reverse_i128 zig_bit_reverse_u128 |
| 1401 | static inline zig_i128 zig_byte_swap_i128(zig_i128 val, zig_u8 bits) { |
| 1402 | return zig_byte_swap_u128(zig_bitcast_u128(val), bits); |
| 1403 | } |
| 1404 | |
| 1405 | static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, zig_u8 bits) { |
| 1406 | return zig_shr_u128(zig_as_u128(zig_bit_reverse_u64(zig_lo_u128(val), zig_as_u8(64)), |
| 1407 | zig_bit_reverse_u64(zig_hi_u128(val), zig_as_u8(64))), |
| 1408 | zig_as_u8(128) - bits); |
| 1409 | } |
| 1410 | |
| 1411 | static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) { |
| 1412 | return zig_bit_reverse_u128(zig_bitcast_u128(val), bits); |
| 1413 | } |
| 1414 | |
| 1415 | /* ========================== Float Point Routines ========================== */ |
| 1416 | |
| 1417 | static inline zig_f32 zig_bitcast_f32_u32(zig_u32 arg) { |
| 1418 | zig_f32 dest; |
| 1419 | memcpy(&dest, &arg, sizeof dest); |
| 1420 | return dest; |
| 1421 | } |
| 1422 | |
| 1423 | static inline zig_f64 zig_bitcast_f64_u64(zig_u64 arg) { |
| 1424 | zig_f64 dest; |
| 1425 | memcpy(&dest, &arg, sizeof dest); |
| 1426 | return dest; |
| 1427 | } |
| 1531 | 1428 | |
| 1532 | 1429 | static inline float zig_div_truncf(float numerator, float denominator) { |
| 1533 | 1430 | return __builtin_truncf(numerator / denominator); |
| ... | ... | @@ -1562,17 +1459,6 @@ static inline long double zig_div_truncl(long double numerator, long double deno |
| 1562 | 1459 | #define zig_div_floor_f80 zig_div_floorl |
| 1563 | 1460 | #define zig_div_floor_f128 zig_div_floorl |
| 1564 | 1461 | |
| 1565 | | #define zig_div_floor_u8 zig_div_floorf |
| 1566 | | #define zig_div_floor_i8 zig_div_floorf |
| 1567 | | #define zig_div_floor_u16 zig_div_floorf |
| 1568 | | #define zig_div_floor_i16 zig_div_floorf |
| 1569 | | #define zig_div_floor_u32 zig_div_floor |
| 1570 | | #define zig_div_floor_i32 zig_div_floor |
| 1571 | | #define zig_div_floor_u64 zig_div_floor |
| 1572 | | #define zig_div_floor_i64 zig_div_floor |
| 1573 | | #define zig_div_floor_u128 zig_div_floorl |
| 1574 | | #define zig_div_floor_i128 zig_div_floorl |
| 1575 | | |
| 1576 | 1462 | static inline float zig_modf(float numerator, float denominator) { |
| 1577 | 1463 | return (numerator - (zig_div_floorf(numerator, denominator) * denominator)); |
| 1578 | 1464 | } |
| ... | ... | @@ -1590,19 +1476,3 @@ static inline long double zig_modl(long double numerator, long double denominato |
| 1590 | 1476 | #define zig_mod_f64 zig_mod |
| 1591 | 1477 | #define zig_mod_f80 zig_modl |
| 1592 | 1478 | #define zig_mod_f128 zig_modl |
| 1593 | | |
| 1594 | | #define zig_mod_int(ZigType, CType) \ |
| 1595 | | static inline CType zig_mod_##ZigType(CType numerator, CType denominator) { \ |
| 1596 | | return (numerator - (zig_div_floor_##ZigType(numerator, denominator) * denominator)); \ |
| 1597 | | } |
| 1598 | | |
| 1599 | | zig_mod_int( u8, uint8_t) |
| 1600 | | zig_mod_int( i8, int8_t) |
| 1601 | | zig_mod_int( u16, uint16_t) |
| 1602 | | zig_mod_int( i16, int16_t) |
| 1603 | | zig_mod_int( u32, uint32_t) |
| 1604 | | zig_mod_int( i32, int32_t) |
| 1605 | | zig_mod_int( u64, uint64_t) |
| 1606 | | zig_mod_int( i64, int64_t) |
| 1607 | | zig_mod_int(u128, uint128_t) |
| 1608 | | zig_mod_int(i128, int128_t) |