| ... | ... | @@ -1,8 +1,11 @@ |
| 1 | 1 | #undef linux |
| 2 | 2 | |
| 3 | #ifndef __STDC_WANT_IEC_60559_TYPES_EXT__ |
| 3 | 4 | #define __STDC_WANT_IEC_60559_TYPES_EXT__ |
| 5 | #endif |
| 4 | 6 | #include <float.h> |
| 5 | 7 | #include <limits.h> |
| 8 | #include <stdarg.h> |
| 6 | 9 | #include <stddef.h> |
| 7 | 10 | #include <stdint.h> |
| 8 | 11 | |
| ... | ... | @@ -34,6 +37,14 @@ typedef char bool; |
| 34 | 37 | #define zig_has_attribute(attribute) 0 |
| 35 | 38 | #endif |
| 36 | 39 | |
| 40 | #if __LITTLE_ENDIAN__ || _MSC_VER |
| 41 | #define zig_little_endian 1 |
| 42 | #define zig_big_endian 0 |
| 43 | #else |
| 44 | #define zig_little_endian 0 |
| 45 | #define zig_big_endian 1 |
| 46 | #endif |
| 47 | |
| 37 | 48 | #if __STDC_VERSION__ >= 201112L |
| 38 | 49 | #define zig_threadlocal _Thread_local |
| 39 | 50 | #elif defined(__GNUC__) |
| ... | ... | @@ -75,6 +86,32 @@ typedef char bool; |
| 75 | 86 | #define zig_cold |
| 76 | 87 | #endif |
| 77 | 88 | |
| 89 | #if zig_has_attribute(flatten) |
| 90 | #define zig_maybe_flatten __attribute__((flatten)) |
| 91 | #else |
| 92 | #define zig_maybe_flatten |
| 93 | #endif |
| 94 | |
| 95 | #if zig_has_attribute(noinline) |
| 96 | #define zig_never_inline __attribute__((noinline)) zig_maybe_flatten |
| 97 | #elif defined(_MSC_VER) |
| 98 | #define zig_never_inline __declspec(noinline) zig_maybe_flatten |
| 99 | #else |
| 100 | #define zig_never_inline zig_never_inline_unavailable |
| 101 | #endif |
| 102 | |
| 103 | #if zig_has_attribute(not_tail_called) |
| 104 | #define zig_never_tail __attribute__((not_tail_called)) zig_never_inline |
| 105 | #else |
| 106 | #define zig_never_tail zig_never_tail_unavailable |
| 107 | #endif |
| 108 | |
| 109 | #if zig_has_attribute(always_inline) |
| 110 | #define zig_always_tail __attribute__((musttail)) |
| 111 | #else |
| 112 | #define zig_always_tail zig_always_tail_unavailable |
| 113 | #endif |
| 114 | |
| 78 | 115 | #if __STDC_VERSION__ >= 199901L |
| 79 | 116 | #define zig_restrict restrict |
| 80 | 117 | #elif defined(__GNUC__) |
| ... | ... | @@ -151,10 +188,16 @@ typedef char bool; |
| 151 | 188 | #define zig_export(sig, symbol, name) __asm(name " = " symbol) |
| 152 | 189 | #endif |
| 153 | 190 | |
| 191 | #if zig_has_builtin(trap) |
| 192 | #define zig_trap() __builtin_trap() |
| 193 | #elif defined(__i386__) || defined(__x86_64__) |
| 194 | #define zig_trap() __asm__ volatile("ud2"); |
| 195 | #else |
| 196 | #define zig_trap() raise(SIGTRAP) |
| 197 | #endif |
| 198 | |
| 154 | 199 | #if zig_has_builtin(debugtrap) |
| 155 | 200 | #define zig_breakpoint() __builtin_debugtrap() |
| 156 | | #elif zig_has_builtin(trap) || defined(zig_gnuc) |
| 157 | | #define zig_breakpoint() __builtin_trap() |
| 158 | 201 | #elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) |
| 159 | 202 | #define zig_breakpoint() __debugbreak() |
| 160 | 203 | #elif defined(__i386__) || defined(__x86_64__) |
| ... | ... | @@ -286,701 +329,656 @@ typedef char bool; |
| 286 | 329 | #endif |
| 287 | 330 | |
| 288 | 331 | #if __STDC_VERSION__ >= 201112L |
| 289 | | #define zig_noreturn _Noreturn void |
| 332 | #define zig_noreturn _Noreturn |
| 290 | 333 | #elif zig_has_attribute(noreturn) || defined(zig_gnuc) |
| 291 | | #define zig_noreturn __attribute__((noreturn)) void |
| 334 | #define zig_noreturn __attribute__((noreturn)) |
| 292 | 335 | #elif _MSC_VER |
| 293 | | #define zig_noreturn __declspec(noreturn) void |
| 336 | #define zig_noreturn __declspec(noreturn) |
| 294 | 337 | #else |
| 295 | | #define zig_noreturn void |
| 338 | #define zig_noreturn |
| 296 | 339 | #endif |
| 297 | 340 | |
| 298 | 341 | #define zig_bitSizeOf(T) (CHAR_BIT * sizeof(T)) |
| 299 | 342 | |
| 300 | | typedef uintptr_t zig_usize; |
| 301 | | typedef intptr_t zig_isize; |
| 302 | | typedef signed short int zig_c_short; |
| 303 | | typedef unsigned short int zig_c_ushort; |
| 304 | | typedef signed int zig_c_int; |
| 305 | | typedef unsigned int zig_c_uint; |
| 306 | | typedef signed long int zig_c_long; |
| 307 | | typedef unsigned long int zig_c_ulong; |
| 308 | | typedef signed long long int zig_c_longlong; |
| 309 | | typedef unsigned long long int zig_c_ulonglong; |
| 310 | | |
| 311 | | typedef uint8_t zig_u8; |
| 312 | | typedef int8_t zig_i8; |
| 313 | | typedef uint16_t zig_u16; |
| 314 | | typedef int16_t zig_i16; |
| 315 | | typedef uint32_t zig_u32; |
| 316 | | typedef int32_t zig_i32; |
| 317 | | typedef uint64_t zig_u64; |
| 318 | | typedef int64_t zig_i64; |
| 319 | | |
| 320 | | #define zig_as_u8(val) UINT8_C(val) |
| 321 | | #define zig_as_i8(val) INT8_C(val) |
| 322 | | #define zig_as_u16(val) UINT16_C(val) |
| 323 | | #define zig_as_i16(val) INT16_C(val) |
| 324 | | #define zig_as_u32(val) UINT32_C(val) |
| 325 | | #define zig_as_i32(val) INT32_C(val) |
| 326 | | #define zig_as_u64(val) UINT64_C(val) |
| 327 | | #define zig_as_i64(val) INT64_C(val) |
| 328 | | |
| 329 | | #define zig_minInt_u8 zig_as_u8(0) |
| 330 | | #define zig_maxInt_u8 UINT8_MAX |
| 343 | #define zig_compiler_rt_abbrev_uint32_t si |
| 344 | #define zig_compiler_rt_abbrev_int32_t si |
| 345 | #define zig_compiler_rt_abbrev_uint64_t di |
| 346 | #define zig_compiler_rt_abbrev_int64_t di |
| 347 | #define zig_compiler_rt_abbrev_zig_u128 ti |
| 348 | #define zig_compiler_rt_abbrev_zig_i128 ti |
| 349 | #define zig_compiler_rt_abbrev_zig_f16 hf |
| 350 | #define zig_compiler_rt_abbrev_zig_f32 sf |
| 351 | #define zig_compiler_rt_abbrev_zig_f64 df |
| 352 | #define zig_compiler_rt_abbrev_zig_f80 xf |
| 353 | #define zig_compiler_rt_abbrev_zig_f128 tf |
| 354 | |
| 355 | zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, size_t); |
| 356 | zig_extern void *memset (void *, int, size_t); |
| 357 | |
| 358 | /* ===================== 8/16/32/64-bit Integer Support ===================== */ |
| 359 | |
| 360 | #if __STDC_VERSION__ >= 199901L || _MSC_VER |
| 361 | #include <stdint.h> |
| 362 | #else |
| 363 | |
| 364 | #if SCHAR_MIN == ~0x7F && SCHAR_MAX == 0x7F && UCHAR_MAX == 0xFF |
| 365 | typedef unsigned char uint8_t; |
| 366 | typedef signed char int8_t; |
| 367 | #define INT8_C(c) c |
| 368 | #define UINT8_C(c) c##U |
| 369 | #elif SHRT_MIN == ~0x7F && SHRT_MAX == 0x7F && USHRT_MAX == 0xFF |
| 370 | typedef unsigned short uint8_t; |
| 371 | typedef signed short int8_t; |
| 372 | #define INT8_C(c) c |
| 373 | #define UINT8_C(c) c##U |
| 374 | #elif INT_MIN == ~0x7F && INT_MAX == 0x7F && UINT_MAX == 0xFF |
| 375 | typedef unsigned int uint8_t; |
| 376 | typedef signed int int8_t; |
| 377 | #define INT8_C(c) c |
| 378 | #define UINT8_C(c) c##U |
| 379 | #elif LONG_MIN == ~0x7F && LONG_MAX == 0x7F && ULONG_MAX == 0xFF |
| 380 | typedef unsigned long uint8_t; |
| 381 | typedef signed long int8_t; |
| 382 | #define INT8_C(c) c##L |
| 383 | #define UINT8_C(c) c##LU |
| 384 | #elif LLONG_MIN == ~0x7F && LLONG_MAX == 0x7F && ULLONG_MAX == 0xFF |
| 385 | typedef unsigned long long uint8_t; |
| 386 | typedef signed long long int8_t; |
| 387 | #define INT8_C(c) c##LL |
| 388 | #define UINT8_C(c) c##LLU |
| 389 | #endif |
| 390 | #define INT8_MIN (~INT8_C(0x7F)) |
| 391 | #define INT8_MAX ( INT8_C(0x7F)) |
| 392 | #define UINT8_MAX ( INT8_C(0xFF)) |
| 393 | |
| 394 | #if SCHAR_MIN == ~0x7FFF && SCHAR_MAX == 0x7FFF && UCHAR_MAX == 0xFFFF |
| 395 | typedef unsigned char uint16_t; |
| 396 | typedef signed char int16_t; |
| 397 | #define INT16_C(c) c |
| 398 | #define UINT16_C(c) c##U |
| 399 | #elif SHRT_MIN == ~0x7FFF && SHRT_MAX == 0x7FFF && USHRT_MAX == 0xFFFF |
| 400 | typedef unsigned short uint16_t; |
| 401 | typedef signed short int16_t; |
| 402 | #define INT16_C(c) c |
| 403 | #define UINT16_C(c) c##U |
| 404 | #elif INT_MIN == ~0x7FFF && INT_MAX == 0x7FFF && UINT_MAX == 0xFFFF |
| 405 | typedef unsigned int uint16_t; |
| 406 | typedef signed int int16_t; |
| 407 | #define INT16_C(c) c |
| 408 | #define UINT16_C(c) c##U |
| 409 | #elif LONG_MIN == ~0x7FFF && LONG_MAX == 0x7FFF && ULONG_MAX == 0xFFFF |
| 410 | typedef unsigned long uint16_t; |
| 411 | typedef signed long int16_t; |
| 412 | #define INT16_C(c) c##L |
| 413 | #define UINT16_C(c) c##LU |
| 414 | #elif LLONG_MIN == ~0x7FFF && LLONG_MAX == 0x7FFF && ULLONG_MAX == 0xFFFF |
| 415 | typedef unsigned long long uint16_t; |
| 416 | typedef signed long long int16_t; |
| 417 | #define INT16_C(c) c##LL |
| 418 | #define UINT16_C(c) c##LLU |
| 419 | #endif |
| 420 | #define INT16_MIN (~INT16_C(0x7FFF)) |
| 421 | #define INT16_MAX ( INT16_C(0x7FFF)) |
| 422 | #define UINT16_MAX ( INT16_C(0xFFFF)) |
| 423 | |
| 424 | #if SCHAR_MIN == ~0x7FFFFFFF && SCHAR_MAX == 0x7FFFFFFF && UCHAR_MAX == 0xFFFFFFFF |
| 425 | typedef unsigned char uint32_t; |
| 426 | typedef signed char int32_t; |
| 427 | #define INT32_C(c) c |
| 428 | #define UINT32_C(c) c##U |
| 429 | #elif SHRT_MIN == ~0x7FFFFFFF && SHRT_MAX == 0x7FFFFFFF && USHRT_MAX == 0xFFFFFFFF |
| 430 | typedef unsigned short uint32_t; |
| 431 | typedef signed short int32_t; |
| 432 | #define INT32_C(c) c |
| 433 | #define UINT32_C(c) c##U |
| 434 | #elif INT_MIN == ~0x7FFFFFFF && INT_MAX == 0x7FFFFFFF && UINT_MAX == 0xFFFFFFFF |
| 435 | typedef unsigned int uint32_t; |
| 436 | typedef signed int int32_t; |
| 437 | #define INT32_C(c) c |
| 438 | #define UINT32_C(c) c##U |
| 439 | #elif LONG_MIN == ~0x7FFFFFFF && LONG_MAX == 0x7FFFFFFF && ULONG_MAX == 0xFFFFFFFF |
| 440 | typedef unsigned long uint32_t; |
| 441 | typedef signed long int32_t; |
| 442 | #define INT32_C(c) c##L |
| 443 | #define UINT32_C(c) c##LU |
| 444 | #elif LLONG_MIN == ~0x7FFFFFFF && LLONG_MAX == 0x7FFFFFFF && ULLONG_MAX == 0xFFFFFFFF |
| 445 | typedef unsigned long long uint32_t; |
| 446 | typedef signed long long int32_t; |
| 447 | #define INT32_C(c) c##LL |
| 448 | #define UINT32_C(c) c##LLU |
| 449 | #endif |
| 450 | #define INT32_MIN (~INT32_C(0x7FFFFFFF)) |
| 451 | #define INT32_MAX ( INT32_C(0x7FFFFFFF)) |
| 452 | #define UINT32_MAX ( INT32_C(0xFFFFFFFF)) |
| 453 | |
| 454 | #if SCHAR_MIN == ~0x7FFFFFFFFFFFFFFF && SCHAR_MAX == 0x7FFFFFFFFFFFFFFF && UCHAR_MAX == 0xFFFFFFFFFFFFFFFF |
| 455 | typedef unsigned char uint64_t; |
| 456 | typedef signed char int64_t; |
| 457 | #define INT64_C(c) c |
| 458 | #define UINT64_C(c) c##U |
| 459 | #elif SHRT_MIN == ~0x7FFFFFFFFFFFFFFF && SHRT_MAX == 0x7FFFFFFFFFFFFFFF && USHRT_MAX == 0xFFFFFFFFFFFFFFFF |
| 460 | typedef unsigned short uint64_t; |
| 461 | typedef signed short int64_t; |
| 462 | #define INT64_C(c) c |
| 463 | #define UINT64_C(c) c##U |
| 464 | #elif INT_MIN == ~0x7FFFFFFFFFFFFFFF && INT_MAX == 0x7FFFFFFFFFFFFFFF && UINT_MAX == 0xFFFFFFFFFFFFFFFF |
| 465 | typedef unsigned int uint64_t; |
| 466 | typedef signed int int64_t; |
| 467 | #define INT64_C(c) c |
| 468 | #define UINT64_C(c) c##U |
| 469 | #elif LONG_MIN == ~0x7FFFFFFFFFFFFFFF && LONG_MAX == 0x7FFFFFFFFFFFFFFF && ULONG_MAX == 0xFFFFFFFFFFFFFFFF |
| 470 | typedef unsigned long uint64_t; |
| 471 | typedef signed long int64_t; |
| 472 | #define INT64_C(c) c##L |
| 473 | #define UINT64_C(c) c##LU |
| 474 | #elif LLONG_MIN == ~0x7FFFFFFFFFFFFFFF && LLONG_MAX == 0x7FFFFFFFFFFFFFFF && ULLONG_MAX == 0xFFFFFFFFFFFFFFFF |
| 475 | typedef unsigned long long uint64_t; |
| 476 | typedef signed long long int64_t; |
| 477 | #define INT64_C(c) c##LL |
| 478 | #define UINT64_C(c) c##LLU |
| 479 | #endif |
| 480 | #define INT64_MIN (~INT64_C(0x7FFFFFFFFFFFFFFF)) |
| 481 | #define INT64_MAX ( INT64_C(0x7FFFFFFFFFFFFFFF)) |
| 482 | #define UINT64_MAX ( INT64_C(0xFFFFFFFFFFFFFFFF)) |
| 483 | |
| 484 | typedef size_t uintptr_t; |
| 485 | typedef ptrdiff_t intptr_t; |
| 486 | |
| 487 | #endif |
| 488 | |
| 331 | 489 | #define zig_minInt_i8 INT8_MIN |
| 332 | 490 | #define zig_maxInt_i8 INT8_MAX |
| 333 | | #define zig_minInt_u16 zig_as_u16(0) |
| 334 | | #define zig_maxInt_u16 UINT16_MAX |
| 491 | #define zig_minInt_u8 UINT8_C(0) |
| 492 | #define zig_maxInt_u8 UINT8_MAX |
| 335 | 493 | #define zig_minInt_i16 INT16_MIN |
| 336 | 494 | #define zig_maxInt_i16 INT16_MAX |
| 337 | | #define zig_minInt_u32 zig_as_u32(0) |
| 338 | | #define zig_maxInt_u32 UINT32_MAX |
| 495 | #define zig_minInt_u16 UINT16_C(0) |
| 496 | #define zig_maxInt_u16 UINT16_MAX |
| 339 | 497 | #define zig_minInt_i32 INT32_MIN |
| 340 | 498 | #define zig_maxInt_i32 INT32_MAX |
| 341 | | #define zig_minInt_u64 zig_as_u64(0) |
| 342 | | #define zig_maxInt_u64 UINT64_MAX |
| 499 | #define zig_minInt_u32 UINT32_C(0) |
| 500 | #define zig_maxInt_u32 UINT32_MAX |
| 343 | 501 | #define zig_minInt_i64 INT64_MIN |
| 344 | 502 | #define zig_maxInt_i64 INT64_MAX |
| 503 | #define zig_minInt_u64 UINT64_C(0) |
| 504 | #define zig_maxInt_u64 UINT64_MAX |
| 345 | 505 | |
| 346 | | #define zig_compiler_rt_abbrev_u32 si |
| 347 | | #define zig_compiler_rt_abbrev_i32 si |
| 348 | | #define zig_compiler_rt_abbrev_u64 di |
| 349 | | #define zig_compiler_rt_abbrev_i64 di |
| 350 | | #define zig_compiler_rt_abbrev_u128 ti |
| 351 | | #define zig_compiler_rt_abbrev_i128 ti |
| 352 | | #define zig_compiler_rt_abbrev_f16 hf |
| 353 | | #define zig_compiler_rt_abbrev_f32 sf |
| 354 | | #define zig_compiler_rt_abbrev_f64 df |
| 355 | | #define zig_compiler_rt_abbrev_f80 xf |
| 356 | | #define zig_compiler_rt_abbrev_f128 tf |
| 357 | | |
| 358 | | zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, zig_usize); |
| 359 | | zig_extern void *memset (void *, int, zig_usize); |
| 360 | | |
| 361 | | /* ==================== 8/16/32/64-bit Integer Routines ===================== */ |
| 362 | | |
| 363 | | #define zig_maxInt(Type, bits) zig_shr_##Type(zig_maxInt_##Type, (zig_bitSizeOf(zig_##Type) - bits)) |
| 364 | | #define zig_expand_maxInt(Type, bits) zig_maxInt(Type, bits) |
| 365 | | #define zig_minInt(Type, bits) zig_not_##Type(zig_maxInt(Type, bits), bits) |
| 366 | | #define zig_expand_minInt(Type, bits) zig_minInt(Type, bits) |
| 506 | #define zig_intLimit(s, w, limit, bits) zig_shr_##s##w(zig_##limit##Int_##s##w, w - (bits)) |
| 507 | #define zig_minInt_i(w, bits) zig_intLimit(i, w, min, bits) |
| 508 | #define zig_maxInt_i(w, bits) zig_intLimit(i, w, max, bits) |
| 509 | #define zig_minInt_u(w, bits) zig_intLimit(u, w, min, bits) |
| 510 | #define zig_maxInt_u(w, bits) zig_intLimit(u, w, max, bits) |
| 367 | 511 | |
| 368 | 512 | #define zig_int_operator(Type, RhsType, operation, operator) \ |
| 369 | | static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##RhsType rhs) { \ |
| 513 | static inline Type zig_##operation(Type lhs, RhsType rhs) { \ |
| 370 | 514 | return lhs operator rhs; \ |
| 371 | 515 | } |
| 372 | 516 | #define zig_int_basic_operator(Type, operation, operator) \ |
| 373 | | zig_int_operator(Type, Type, operation, operator) |
| 517 | zig_int_operator(Type, Type, operation, operator) |
| 374 | 518 | #define zig_int_shift_operator(Type, operation, operator) \ |
| 375 | | zig_int_operator(Type, u8, operation, operator) |
| 519 | zig_int_operator(Type, uint8_t, operation, operator) |
| 376 | 520 | #define zig_int_helpers(w) \ |
| 377 | | zig_int_basic_operator(u##w, and, &) \ |
| 378 | | zig_int_basic_operator(i##w, and, &) \ |
| 379 | | zig_int_basic_operator(u##w, or, |) \ |
| 380 | | zig_int_basic_operator(i##w, or, |) \ |
| 381 | | zig_int_basic_operator(u##w, xor, ^) \ |
| 382 | | zig_int_basic_operator(i##w, xor, ^) \ |
| 383 | | zig_int_shift_operator(u##w, shl, <<) \ |
| 384 | | zig_int_shift_operator(i##w, shl, <<) \ |
| 385 | | zig_int_shift_operator(u##w, shr, >>) \ |
| 521 | zig_int_basic_operator(uint##w##_t, and_u##w, &) \ |
| 522 | zig_int_basic_operator( int##w##_t, and_i##w, &) \ |
| 523 | zig_int_basic_operator(uint##w##_t, or_u##w, |) \ |
| 524 | zig_int_basic_operator( int##w##_t, or_i##w, |) \ |
| 525 | zig_int_basic_operator(uint##w##_t, xor_u##w, ^) \ |
| 526 | zig_int_basic_operator( int##w##_t, xor_i##w, ^) \ |
| 527 | zig_int_shift_operator(uint##w##_t, shl_u##w, <<) \ |
| 528 | zig_int_shift_operator( int##w##_t, shl_i##w, <<) \ |
| 529 | zig_int_shift_operator(uint##w##_t, shr_u##w, >>) \ |
| 386 | 530 | \ |
| 387 | | static inline zig_i##w zig_shr_i##w(zig_i##w lhs, zig_u8 rhs) { \ |
| 388 | | zig_i##w sign_mask = lhs < zig_as_i##w(0) ? -zig_as_i##w(1) : zig_as_i##w(0); \ |
| 531 | static inline int##w##_t zig_shr_i##w(int##w##_t lhs, uint8_t rhs) { \ |
| 532 | int##w##_t sign_mask = lhs < INT##w##_C(0) ? -INT##w##_C(1) : INT##w##_C(0); \ |
| 389 | 533 | return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \ |
| 390 | 534 | } \ |
| 391 | 535 | \ |
| 392 | | static inline zig_u##w zig_not_u##w(zig_u##w val, zig_u8 bits) { \ |
| 393 | | return val ^ zig_maxInt(u##w, bits); \ |
| 536 | static inline uint##w##_t zig_not_u##w(uint##w##_t val, uint8_t bits) { \ |
| 537 | return val ^ zig_maxInt_u(w, bits); \ |
| 394 | 538 | } \ |
| 395 | 539 | \ |
| 396 | | static inline zig_i##w zig_not_i##w(zig_i##w val, zig_u8 bits) { \ |
| 540 | static inline int##w##_t zig_not_i##w(int##w##_t val, uint8_t bits) { \ |
| 397 | 541 | (void)bits; \ |
| 398 | 542 | return ~val; \ |
| 399 | 543 | } \ |
| 400 | 544 | \ |
| 401 | | static inline zig_u##w zig_wrap_u##w(zig_u##w val, zig_u8 bits) { \ |
| 402 | | return val & zig_maxInt(u##w, bits); \ |
| 545 | static inline uint##w##_t zig_wrap_u##w(uint##w##_t val, uint8_t bits) { \ |
| 546 | return val & zig_maxInt_u(w, bits); \ |
| 403 | 547 | } \ |
| 404 | 548 | \ |
| 405 | | static inline zig_i##w zig_wrap_i##w(zig_i##w val, zig_u8 bits) { \ |
| 406 | | return (val & zig_as_u##w(1) << (bits - zig_as_u8(1))) != 0 \ |
| 407 | | ? val | zig_minInt(i##w, bits) : val & zig_maxInt(i##w, bits); \ |
| 549 | static inline int##w##_t zig_wrap_i##w(int##w##_t val, uint8_t bits) { \ |
| 550 | return (val & UINT##w##_C(1) << (bits - UINT8_C(1))) != 0 \ |
| 551 | ? val | zig_minInt_i(w, bits) : val & zig_maxInt_i(w, bits); \ |
| 408 | 552 | } \ |
| 409 | 553 | \ |
| 410 | | zig_int_basic_operator(u##w, div_floor, /) \ |
| 554 | zig_int_basic_operator(uint##w##_t, div_floor_u##w, /) \ |
| 411 | 555 | \ |
| 412 | | static inline zig_i##w zig_div_floor_i##w(zig_i##w lhs, zig_i##w rhs) { \ |
| 413 | | return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < zig_as_i##w(0)); \ |
| 556 | static inline int##w##_t zig_div_floor_i##w(int##w##_t lhs, int##w##_t rhs) { \ |
| 557 | return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < INT##w##_C(0)); \ |
| 414 | 558 | } \ |
| 415 | 559 | \ |
| 416 | | zig_int_basic_operator(u##w, mod, %) \ |
| 560 | zig_int_basic_operator(uint##w##_t, mod_u##w, %) \ |
| 417 | 561 | \ |
| 418 | | static inline zig_i##w zig_mod_i##w(zig_i##w lhs, zig_i##w rhs) { \ |
| 419 | | zig_i##w rem = lhs % rhs; \ |
| 420 | | return rem + (((lhs ^ rhs) & rem) < zig_as_i##w(0) ? rhs : zig_as_i##w(0)); \ |
| 562 | static inline int##w##_t zig_mod_i##w(int##w##_t lhs, int##w##_t rhs) { \ |
| 563 | int##w##_t rem = lhs % rhs; \ |
| 564 | return rem + (((lhs ^ rhs) & rem) < INT##w##_C(0) ? rhs : INT##w##_C(0)); \ |
| 421 | 565 | } \ |
| 422 | 566 | \ |
| 423 | | static inline zig_u##w zig_shlw_u##w(zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \ |
| 567 | static inline uint##w##_t zig_shlw_u##w(uint##w##_t lhs, uint8_t rhs, uint8_t bits) { \ |
| 424 | 568 | return zig_wrap_u##w(zig_shl_u##w(lhs, rhs), bits); \ |
| 425 | 569 | } \ |
| 426 | 570 | \ |
| 427 | | static inline zig_i##w zig_shlw_i##w(zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \ |
| 428 | | return zig_wrap_i##w((zig_i##w)zig_shl_u##w((zig_u##w)lhs, (zig_u##w)rhs), bits); \ |
| 571 | static inline int##w##_t zig_shlw_i##w(int##w##_t lhs, uint8_t rhs, uint8_t bits) { \ |
| 572 | return zig_wrap_i##w((int##w##_t)zig_shl_u##w((uint##w##_t)lhs, (uint##w##_t)rhs), bits); \ |
| 429 | 573 | } \ |
| 430 | 574 | \ |
| 431 | | static inline zig_u##w zig_addw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 575 | static inline uint##w##_t zig_addw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 432 | 576 | return zig_wrap_u##w(lhs + rhs, bits); \ |
| 433 | 577 | } \ |
| 434 | 578 | \ |
| 435 | | static inline zig_i##w zig_addw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 436 | | return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs + (zig_u##w)rhs), bits); \ |
| 579 | static inline int##w##_t zig_addw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \ |
| 580 | return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs + (uint##w##_t)rhs), bits); \ |
| 437 | 581 | } \ |
| 438 | 582 | \ |
| 439 | | static inline zig_u##w zig_subw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 583 | static inline uint##w##_t zig_subw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 440 | 584 | return zig_wrap_u##w(lhs - rhs, bits); \ |
| 441 | 585 | } \ |
| 442 | 586 | \ |
| 443 | | static inline zig_i##w zig_subw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 444 | | return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs - (zig_u##w)rhs), bits); \ |
| 587 | static inline int##w##_t zig_subw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \ |
| 588 | return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs - (uint##w##_t)rhs), bits); \ |
| 445 | 589 | } \ |
| 446 | 590 | \ |
| 447 | | static inline zig_u##w zig_mulw_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 591 | static inline uint##w##_t zig_mulw_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 448 | 592 | return zig_wrap_u##w(lhs * rhs, bits); \ |
| 449 | 593 | } \ |
| 450 | 594 | \ |
| 451 | | static inline zig_i##w zig_mulw_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 452 | | return zig_wrap_i##w((zig_i##w)((zig_u##w)lhs * (zig_u##w)rhs), bits); \ |
| 595 | static inline int##w##_t zig_mulw_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \ |
| 596 | return zig_wrap_i##w((int##w##_t)((uint##w##_t)lhs * (uint##w##_t)rhs), bits); \ |
| 453 | 597 | } |
| 454 | 598 | zig_int_helpers(8) |
| 455 | 599 | zig_int_helpers(16) |
| 456 | 600 | zig_int_helpers(32) |
| 457 | 601 | zig_int_helpers(64) |
| 458 | 602 | |
| 459 | | static inline bool zig_addo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) { |
| 603 | static inline bool zig_addo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) { |
| 460 | 604 | #if zig_has_builtin(add_overflow) || defined(zig_gnuc) |
| 461 | | zig_u32 full_res; |
| 605 | uint32_t full_res; |
| 462 | 606 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 463 | 607 | *res = zig_wrap_u32(full_res, bits); |
| 464 | | return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits); |
| 608 | return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits); |
| 465 | 609 | #else |
| 466 | 610 | *res = zig_addw_u32(lhs, rhs, bits); |
| 467 | 611 | return *res < lhs; |
| 468 | 612 | #endif |
| 469 | 613 | } |
| 470 | 614 | |
| 471 | | static inline void zig_vaddo_u32(zig_u8 *ov, zig_u32 *res, int n, |
| 472 | | const zig_u32 *lhs, const zig_u32 *rhs, zig_u8 bits) |
| 473 | | { |
| 474 | | for (int i = 0; i < n; ++i) ov[i] = zig_addo_u32(&res[i], lhs[i], rhs[i], bits); |
| 475 | | } |
| 476 | | |
| 477 | | zig_extern zig_i32 __addosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow); |
| 478 | | static inline bool zig_addo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) { |
| 615 | zig_extern int32_t __addosi4(int32_t lhs, int32_t rhs, int *overflow); |
| 616 | static inline bool zig_addo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) { |
| 479 | 617 | #if zig_has_builtin(add_overflow) || defined(zig_gnuc) |
| 480 | | zig_i32 full_res; |
| 618 | int32_t full_res; |
| 481 | 619 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 482 | 620 | #else |
| 483 | | zig_c_int overflow_int; |
| 484 | | zig_i32 full_res = __addosi4(lhs, rhs, &overflow_int); |
| 621 | int overflow_int; |
| 622 | int32_t full_res = __addosi4(lhs, rhs, &overflow_int); |
| 485 | 623 | bool overflow = overflow_int != 0; |
| 486 | 624 | #endif |
| 487 | 625 | *res = zig_wrap_i32(full_res, bits); |
| 488 | | return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits); |
| 489 | | } |
| 490 | | |
| 491 | | static inline void zig_vaddo_i32(zig_u8 *ov, zig_i32 *res, int n, |
| 492 | | const zig_i32 *lhs, const zig_i32 *rhs, zig_u8 bits) |
| 493 | | { |
| 494 | | for (int i = 0; i < n; ++i) ov[i] = zig_addo_i32(&res[i], lhs[i], rhs[i], bits); |
| 626 | return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits); |
| 495 | 627 | } |
| 496 | 628 | |
| 497 | | static inline bool zig_addo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) { |
| 629 | static inline bool zig_addo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) { |
| 498 | 630 | #if zig_has_builtin(add_overflow) || defined(zig_gnuc) |
| 499 | | zig_u64 full_res; |
| 631 | uint64_t full_res; |
| 500 | 632 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 501 | 633 | *res = zig_wrap_u64(full_res, bits); |
| 502 | | return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits); |
| 634 | return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits); |
| 503 | 635 | #else |
| 504 | 636 | *res = zig_addw_u64(lhs, rhs, bits); |
| 505 | 637 | return *res < lhs; |
| 506 | 638 | #endif |
| 507 | 639 | } |
| 508 | 640 | |
| 509 | | static inline void zig_vaddo_u64(zig_u8 *ov, zig_u64 *res, int n, |
| 510 | | const zig_u64 *lhs, const zig_u64 *rhs, zig_u8 bits) |
| 511 | | { |
| 512 | | for (int i = 0; i < n; ++i) ov[i] = zig_addo_u64(&res[i], lhs[i], rhs[i], bits); |
| 513 | | } |
| 514 | | |
| 515 | | zig_extern zig_i64 __addodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow); |
| 516 | | static inline bool zig_addo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) { |
| 641 | zig_extern int64_t __addodi4(int64_t lhs, int64_t rhs, int *overflow); |
| 642 | static inline bool zig_addo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) { |
| 517 | 643 | #if zig_has_builtin(add_overflow) || defined(zig_gnuc) |
| 518 | | zig_i64 full_res; |
| 644 | int64_t full_res; |
| 519 | 645 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 520 | 646 | #else |
| 521 | | zig_c_int overflow_int; |
| 522 | | zig_i64 full_res = __addodi4(lhs, rhs, &overflow_int); |
| 647 | int overflow_int; |
| 648 | int64_t full_res = __addodi4(lhs, rhs, &overflow_int); |
| 523 | 649 | bool overflow = overflow_int != 0; |
| 524 | 650 | #endif |
| 525 | 651 | *res = zig_wrap_i64(full_res, bits); |
| 526 | | return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits); |
| 527 | | } |
| 528 | | |
| 529 | | static inline void zig_vaddo_i64(zig_u8 *ov, zig_i64 *res, int n, |
| 530 | | const zig_i64 *lhs, const zig_i64 *rhs, zig_u8 bits) |
| 531 | | { |
| 532 | | for (int i = 0; i < n; ++i) ov[i] = zig_addo_i64(&res[i], lhs[i], rhs[i], bits); |
| 652 | return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits); |
| 533 | 653 | } |
| 534 | 654 | |
| 535 | | static inline bool zig_addo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) { |
| 655 | static inline bool zig_addo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) { |
| 536 | 656 | #if zig_has_builtin(add_overflow) || defined(zig_gnuc) |
| 537 | | zig_u8 full_res; |
| 657 | uint8_t full_res; |
| 538 | 658 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 539 | 659 | *res = zig_wrap_u8(full_res, bits); |
| 540 | | return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits); |
| 660 | return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits); |
| 541 | 661 | #else |
| 542 | | zig_u32 full_res; |
| 662 | uint32_t full_res; |
| 543 | 663 | bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits); |
| 544 | | *res = (zig_u8)full_res; |
| 664 | *res = (uint8_t)full_res; |
| 545 | 665 | return overflow; |
| 546 | 666 | #endif |
| 547 | 667 | } |
| 548 | 668 | |
| 549 | | static inline void zig_vaddo_u8(zig_u8 *ov, zig_u8 *res, int n, |
| 550 | | const zig_u8 *lhs, const zig_u8 *rhs, zig_u8 bits) |
| 551 | | { |
| 552 | | for (int i = 0; i < n; ++i) ov[i] = zig_addo_u8(&res[i], lhs[i], rhs[i], bits); |
| 553 | | } |
| 554 | | |
| 555 | | static inline bool zig_addo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) { |
| 669 | static inline bool zig_addo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits) { |
| 556 | 670 | #if zig_has_builtin(add_overflow) || defined(zig_gnuc) |
| 557 | | zig_i8 full_res; |
| 671 | int8_t full_res; |
| 558 | 672 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 559 | 673 | *res = zig_wrap_i8(full_res, bits); |
| 560 | | return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits); |
| 674 | return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits); |
| 561 | 675 | #else |
| 562 | | zig_i32 full_res; |
| 676 | int32_t full_res; |
| 563 | 677 | bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits); |
| 564 | | *res = (zig_i8)full_res; |
| 678 | *res = (int8_t)full_res; |
| 565 | 679 | return overflow; |
| 566 | 680 | #endif |
| 567 | 681 | } |
| 568 | 682 | |
| 569 | | static inline void zig_vaddo_i8(zig_u8 *ov, zig_i8 *res, int n, |
| 570 | | const zig_i8 *lhs, const zig_i8 *rhs, zig_u8 bits) |
| 571 | | { |
| 572 | | for (int i = 0; i < n; ++i) ov[i] = zig_addo_i8(&res[i], lhs[i], rhs[i], bits); |
| 573 | | } |
| 574 | | |
| 575 | | static inline bool zig_addo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) { |
| 683 | static inline bool zig_addo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8_t bits) { |
| 576 | 684 | #if zig_has_builtin(add_overflow) || defined(zig_gnuc) |
| 577 | | zig_u16 full_res; |
| 685 | uint16_t full_res; |
| 578 | 686 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 579 | 687 | *res = zig_wrap_u16(full_res, bits); |
| 580 | | return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits); |
| 688 | return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits); |
| 581 | 689 | #else |
| 582 | | zig_u32 full_res; |
| 690 | uint32_t full_res; |
| 583 | 691 | bool overflow = zig_addo_u32(&full_res, lhs, rhs, bits); |
| 584 | | *res = (zig_u16)full_res; |
| 692 | *res = (uint16_t)full_res; |
| 585 | 693 | return overflow; |
| 586 | 694 | #endif |
| 587 | 695 | } |
| 588 | 696 | |
| 589 | | static inline void zig_vaddo_u16(zig_u8 *ov, zig_u16 *res, int n, |
| 590 | | const zig_u16 *lhs, const zig_u16 *rhs, zig_u8 bits) |
| 591 | | { |
| 592 | | for (int i = 0; i < n; ++i) ov[i] = zig_addo_u16(&res[i], lhs[i], rhs[i], bits); |
| 593 | | } |
| 594 | | |
| 595 | | static inline bool zig_addo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) { |
| 697 | static inline bool zig_addo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t bits) { |
| 596 | 698 | #if zig_has_builtin(add_overflow) || defined(zig_gnuc) |
| 597 | | zig_i16 full_res; |
| 699 | int16_t full_res; |
| 598 | 700 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 599 | 701 | *res = zig_wrap_i16(full_res, bits); |
| 600 | | return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits); |
| 702 | return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits); |
| 601 | 703 | #else |
| 602 | | zig_i32 full_res; |
| 704 | int32_t full_res; |
| 603 | 705 | bool overflow = zig_addo_i32(&full_res, lhs, rhs, bits); |
| 604 | | *res = (zig_i16)full_res; |
| 706 | *res = (int16_t)full_res; |
| 605 | 707 | return overflow; |
| 606 | 708 | #endif |
| 607 | 709 | } |
| 608 | 710 | |
| 609 | | static inline void zig_vaddo_i16(zig_u8 *ov, zig_i16 *res, int n, |
| 610 | | const zig_i16 *lhs, const zig_i16 *rhs, zig_u8 bits) |
| 611 | | { |
| 612 | | for (int i = 0; i < n; ++i) ov[i] = zig_addo_i16(&res[i], lhs[i], rhs[i], bits); |
| 613 | | } |
| 614 | | |
| 615 | | static inline bool zig_subo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) { |
| 711 | static inline bool zig_subo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) { |
| 616 | 712 | #if zig_has_builtin(sub_overflow) || defined(zig_gnuc) |
| 617 | | zig_u32 full_res; |
| 713 | uint32_t full_res; |
| 618 | 714 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 619 | 715 | *res = zig_wrap_u32(full_res, bits); |
| 620 | | return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits); |
| 716 | return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits); |
| 621 | 717 | #else |
| 622 | 718 | *res = zig_subw_u32(lhs, rhs, bits); |
| 623 | 719 | return *res > lhs; |
| 624 | 720 | #endif |
| 625 | 721 | } |
| 626 | 722 | |
| 627 | | static inline void zig_vsubo_u32(zig_u8 *ov, zig_u32 *res, int n, |
| 628 | | const zig_u32 *lhs, const zig_u32 *rhs, zig_u8 bits) |
| 629 | | { |
| 630 | | for (int i = 0; i < n; ++i) ov[i] = zig_subo_u32(&res[i], lhs[i], rhs[i], bits); |
| 631 | | } |
| 632 | | |
| 633 | | zig_extern zig_i32 __subosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow); |
| 634 | | static inline bool zig_subo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) { |
| 723 | zig_extern int32_t __subosi4(int32_t lhs, int32_t rhs, int *overflow); |
| 724 | static inline bool zig_subo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) { |
| 635 | 725 | #if zig_has_builtin(sub_overflow) || defined(zig_gnuc) |
| 636 | | zig_i32 full_res; |
| 726 | int32_t full_res; |
| 637 | 727 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 638 | 728 | #else |
| 639 | | zig_c_int overflow_int; |
| 640 | | zig_i32 full_res = __subosi4(lhs, rhs, &overflow_int); |
| 729 | int overflow_int; |
| 730 | int32_t full_res = __subosi4(lhs, rhs, &overflow_int); |
| 641 | 731 | bool overflow = overflow_int != 0; |
| 642 | 732 | #endif |
| 643 | 733 | *res = zig_wrap_i32(full_res, bits); |
| 644 | | return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits); |
| 734 | return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits); |
| 645 | 735 | } |
| 646 | 736 | |
| 647 | | static inline void zig_vsubo_i32(zig_u8 *ov, zig_i32 *res, int n, |
| 648 | | const zig_i32 *lhs, const zig_i32 *rhs, zig_u8 bits) |
| 649 | | { |
| 650 | | for (int i = 0; i < n; ++i) ov[i] = zig_subo_i32(&res[i], lhs[i], rhs[i], bits); |
| 651 | | } |
| 652 | | |
| 653 | | static inline bool zig_subo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) { |
| 737 | static inline bool zig_subo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) { |
| 654 | 738 | #if zig_has_builtin(sub_overflow) || defined(zig_gnuc) |
| 655 | | zig_u64 full_res; |
| 739 | uint64_t full_res; |
| 656 | 740 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 657 | 741 | *res = zig_wrap_u64(full_res, bits); |
| 658 | | return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits); |
| 742 | return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits); |
| 659 | 743 | #else |
| 660 | 744 | *res = zig_subw_u64(lhs, rhs, bits); |
| 661 | 745 | return *res > lhs; |
| 662 | 746 | #endif |
| 663 | 747 | } |
| 664 | 748 | |
| 665 | | static inline void zig_vsubo_u64(zig_u8 *ov, zig_u64 *res, int n, |
| 666 | | const zig_u64 *lhs, const zig_u64 *rhs, zig_u8 bits) |
| 667 | | { |
| 668 | | for (int i = 0; i < n; ++i) ov[i] = zig_subo_u64(&res[i], lhs[i], rhs[i], bits); |
| 669 | | } |
| 670 | | |
| 671 | | zig_extern zig_i64 __subodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow); |
| 672 | | static inline bool zig_subo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) { |
| 749 | zig_extern int64_t __subodi4(int64_t lhs, int64_t rhs, int *overflow); |
| 750 | static inline bool zig_subo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) { |
| 673 | 751 | #if zig_has_builtin(sub_overflow) || defined(zig_gnuc) |
| 674 | | zig_i64 full_res; |
| 752 | int64_t full_res; |
| 675 | 753 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 676 | 754 | #else |
| 677 | | zig_c_int overflow_int; |
| 678 | | zig_i64 full_res = __subodi4(lhs, rhs, &overflow_int); |
| 755 | int overflow_int; |
| 756 | int64_t full_res = __subodi4(lhs, rhs, &overflow_int); |
| 679 | 757 | bool overflow = overflow_int != 0; |
| 680 | 758 | #endif |
| 681 | 759 | *res = zig_wrap_i64(full_res, bits); |
| 682 | | return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits); |
| 760 | return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits); |
| 683 | 761 | } |
| 684 | 762 | |
| 685 | | static inline void zig_vsubo_i64(zig_u8 *ov, zig_i64 *res, int n, |
| 686 | | const zig_i64 *lhs, const zig_i64 *rhs, zig_u8 bits) |
| 687 | | { |
| 688 | | for (int i = 0; i < n; ++i) ov[i] = zig_subo_i64(&res[i], lhs[i], rhs[i], bits); |
| 689 | | } |
| 690 | | |
| 691 | | static inline bool zig_subo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) { |
| 763 | static inline bool zig_subo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) { |
| 692 | 764 | #if zig_has_builtin(sub_overflow) || defined(zig_gnuc) |
| 693 | | zig_u8 full_res; |
| 765 | uint8_t full_res; |
| 694 | 766 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 695 | 767 | *res = zig_wrap_u8(full_res, bits); |
| 696 | | return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits); |
| 768 | return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits); |
| 697 | 769 | #else |
| 698 | | zig_u32 full_res; |
| 770 | uint32_t full_res; |
| 699 | 771 | bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits); |
| 700 | | *res = (zig_u8)full_res; |
| 772 | *res = (uint8_t)full_res; |
| 701 | 773 | return overflow; |
| 702 | 774 | #endif |
| 703 | 775 | } |
| 704 | 776 | |
| 705 | | static inline void zig_vsubo_u8(zig_u8 *ov, zig_u8 *res, int n, |
| 706 | | const zig_u8 *lhs, const zig_u8 *rhs, zig_u8 bits) |
| 707 | | { |
| 708 | | for (int i = 0; i < n; ++i) ov[i] = zig_subo_u8(&res[i], lhs[i], rhs[i], bits); |
| 709 | | } |
| 710 | | |
| 711 | | static inline bool zig_subo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) { |
| 777 | static inline bool zig_subo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits) { |
| 712 | 778 | #if zig_has_builtin(sub_overflow) || defined(zig_gnuc) |
| 713 | | zig_i8 full_res; |
| 779 | int8_t full_res; |
| 714 | 780 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 715 | 781 | *res = zig_wrap_i8(full_res, bits); |
| 716 | | return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits); |
| 782 | return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits); |
| 717 | 783 | #else |
| 718 | | zig_i32 full_res; |
| 784 | int32_t full_res; |
| 719 | 785 | bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits); |
| 720 | | *res = (zig_i8)full_res; |
| 786 | *res = (int8_t)full_res; |
| 721 | 787 | return overflow; |
| 722 | 788 | #endif |
| 723 | 789 | } |
| 724 | 790 | |
| 725 | | static inline void zig_vsubo_i8(zig_u8 *ov, zig_i8 *res, int n, |
| 726 | | const zig_i8 *lhs, const zig_i8 *rhs, zig_u8 bits) |
| 727 | | { |
| 728 | | for (int i = 0; i < n; ++i) ov[i] = zig_subo_i8(&res[i], lhs[i], rhs[i], bits); |
| 729 | | } |
| 730 | | |
| 731 | | |
| 732 | | static inline bool zig_subo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) { |
| 791 | static inline bool zig_subo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8_t bits) { |
| 733 | 792 | #if zig_has_builtin(sub_overflow) || defined(zig_gnuc) |
| 734 | | zig_u16 full_res; |
| 793 | uint16_t full_res; |
| 735 | 794 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 736 | 795 | *res = zig_wrap_u16(full_res, bits); |
| 737 | | return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits); |
| 796 | return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits); |
| 738 | 797 | #else |
| 739 | | zig_u32 full_res; |
| 798 | uint32_t full_res; |
| 740 | 799 | bool overflow = zig_subo_u32(&full_res, lhs, rhs, bits); |
| 741 | | *res = (zig_u16)full_res; |
| 800 | *res = (uint16_t)full_res; |
| 742 | 801 | return overflow; |
| 743 | 802 | #endif |
| 744 | 803 | } |
| 745 | 804 | |
| 746 | | static inline void zig_vsubo_u16(zig_u8 *ov, zig_u16 *res, int n, |
| 747 | | const zig_u16 *lhs, const zig_u16 *rhs, zig_u8 bits) |
| 748 | | { |
| 749 | | for (int i = 0; i < n; ++i) ov[i] = zig_subo_u16(&res[i], lhs[i], rhs[i], bits); |
| 750 | | } |
| 751 | | |
| 752 | | |
| 753 | | static inline bool zig_subo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) { |
| 805 | static inline bool zig_subo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t bits) { |
| 754 | 806 | #if zig_has_builtin(sub_overflow) || defined(zig_gnuc) |
| 755 | | zig_i16 full_res; |
| 807 | int16_t full_res; |
| 756 | 808 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 757 | 809 | *res = zig_wrap_i16(full_res, bits); |
| 758 | | return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits); |
| 810 | return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits); |
| 759 | 811 | #else |
| 760 | | zig_i32 full_res; |
| 812 | int32_t full_res; |
| 761 | 813 | bool overflow = zig_subo_i32(&full_res, lhs, rhs, bits); |
| 762 | | *res = (zig_i16)full_res; |
| 814 | *res = (int16_t)full_res; |
| 763 | 815 | return overflow; |
| 764 | 816 | #endif |
| 765 | 817 | } |
| 766 | 818 | |
| 767 | | static inline void zig_vsubo_i16(zig_u8 *ov, zig_i16 *res, int n, |
| 768 | | const zig_i16 *lhs, const zig_i16 *rhs, zig_u8 bits) |
| 769 | | { |
| 770 | | for (int i = 0; i < n; ++i) ov[i] = zig_subo_i16(&res[i], lhs[i], rhs[i], bits); |
| 771 | | } |
| 772 | | |
| 773 | | static inline bool zig_mulo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_u8 bits) { |
| 819 | static inline bool zig_mulo_u32(uint32_t *res, uint32_t lhs, uint32_t rhs, uint8_t bits) { |
| 774 | 820 | #if zig_has_builtin(mul_overflow) || defined(zig_gnuc) |
| 775 | | zig_u32 full_res; |
| 821 | uint32_t full_res; |
| 776 | 822 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 777 | 823 | *res = zig_wrap_u32(full_res, bits); |
| 778 | | return overflow || full_res < zig_minInt(u32, bits) || full_res > zig_maxInt(u32, bits); |
| 824 | return overflow || full_res < zig_minInt_u(32, bits) || full_res > zig_maxInt_u(32, bits); |
| 779 | 825 | #else |
| 780 | 826 | *res = zig_mulw_u32(lhs, rhs, bits); |
| 781 | | return rhs != zig_as_u32(0) && lhs > zig_maxInt(u32, bits) / rhs; |
| 827 | return rhs != UINT32_C(0) && lhs > zig_maxInt_u(32, bits) / rhs; |
| 782 | 828 | #endif |
| 783 | 829 | } |
| 784 | 830 | |
| 785 | | static inline void zig_vmulo_u32(zig_u8 *ov, zig_u32 *res, int n, |
| 786 | | const zig_u32 *lhs, const zig_u32 *rhs, zig_u8 bits) |
| 787 | | { |
| 788 | | for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u32(&res[i], lhs[i], rhs[i], bits); |
| 789 | | } |
| 790 | | |
| 791 | | zig_extern zig_i32 __mulosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow); |
| 792 | | static inline bool zig_mulo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) { |
| 831 | zig_extern int32_t __mulosi4(int32_t lhs, int32_t rhs, int *overflow); |
| 832 | static inline bool zig_mulo_i32(int32_t *res, int32_t lhs, int32_t rhs, uint8_t bits) { |
| 793 | 833 | #if zig_has_builtin(mul_overflow) || defined(zig_gnuc) |
| 794 | | zig_i32 full_res; |
| 834 | int32_t full_res; |
| 795 | 835 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 796 | 836 | #else |
| 797 | | zig_c_int overflow_int; |
| 798 | | zig_i32 full_res = __mulosi4(lhs, rhs, &overflow_int); |
| 837 | int overflow_int; |
| 838 | int32_t full_res = __mulosi4(lhs, rhs, &overflow_int); |
| 799 | 839 | bool overflow = overflow_int != 0; |
| 800 | 840 | #endif |
| 801 | 841 | *res = zig_wrap_i32(full_res, bits); |
| 802 | | return overflow || full_res < zig_minInt(i32, bits) || full_res > zig_maxInt(i32, bits); |
| 803 | | } |
| 804 | | |
| 805 | | static inline void zig_vmulo_i32(zig_u8 *ov, zig_i32 *res, int n, |
| 806 | | const zig_i32 *lhs, const zig_i32 *rhs, zig_u8 bits) |
| 807 | | { |
| 808 | | for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i32(&res[i], lhs[i], rhs[i], bits); |
| 842 | return overflow || full_res < zig_minInt_i(32, bits) || full_res > zig_maxInt_i(32, bits); |
| 809 | 843 | } |
| 810 | 844 | |
| 811 | | static inline bool zig_mulo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_u8 bits) { |
| 845 | static inline bool zig_mulo_u64(uint64_t *res, uint64_t lhs, uint64_t rhs, uint8_t bits) { |
| 812 | 846 | #if zig_has_builtin(mul_overflow) || defined(zig_gnuc) |
| 813 | | zig_u64 full_res; |
| 847 | uint64_t full_res; |
| 814 | 848 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 815 | 849 | *res = zig_wrap_u64(full_res, bits); |
| 816 | | return overflow || full_res < zig_minInt(u64, bits) || full_res > zig_maxInt(u64, bits); |
| 850 | return overflow || full_res < zig_minInt_u(64, bits) || full_res > zig_maxInt_u(64, bits); |
| 817 | 851 | #else |
| 818 | 852 | *res = zig_mulw_u64(lhs, rhs, bits); |
| 819 | | return rhs != zig_as_u64(0) && lhs > zig_maxInt(u64, bits) / rhs; |
| 853 | return rhs != UINT64_C(0) && lhs > zig_maxInt_u(64, bits) / rhs; |
| 820 | 854 | #endif |
| 821 | 855 | } |
| 822 | 856 | |
| 823 | | static inline void zig_vmulo_u64(zig_u8 *ov, zig_u64 *res, int n, |
| 824 | | const zig_u64 *lhs, const zig_u64 *rhs, zig_u8 bits) |
| 825 | | { |
| 826 | | for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u64(&res[i], lhs[i], rhs[i], bits); |
| 827 | | } |
| 828 | | |
| 829 | | zig_extern zig_i64 __mulodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow); |
| 830 | | static inline bool zig_mulo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) { |
| 857 | zig_extern int64_t __mulodi4(int64_t lhs, int64_t rhs, int *overflow); |
| 858 | static inline bool zig_mulo_i64(int64_t *res, int64_t lhs, int64_t rhs, uint8_t bits) { |
| 831 | 859 | #if zig_has_builtin(mul_overflow) || defined(zig_gnuc) |
| 832 | | zig_i64 full_res; |
| 860 | int64_t full_res; |
| 833 | 861 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 834 | 862 | #else |
| 835 | | zig_c_int overflow_int; |
| 836 | | zig_i64 full_res = __mulodi4(lhs, rhs, &overflow_int); |
| 863 | int overflow_int; |
| 864 | int64_t full_res = __mulodi4(lhs, rhs, &overflow_int); |
| 837 | 865 | bool overflow = overflow_int != 0; |
| 838 | 866 | #endif |
| 839 | 867 | *res = zig_wrap_i64(full_res, bits); |
| 840 | | return overflow || full_res < zig_minInt(i64, bits) || full_res > zig_maxInt(i64, bits); |
| 868 | return overflow || full_res < zig_minInt_i(64, bits) || full_res > zig_maxInt_i(64, bits); |
| 841 | 869 | } |
| 842 | 870 | |
| 843 | | static inline void zig_vmulo_i64(zig_u8 *ov, zig_i64 *res, int n, |
| 844 | | const zig_i64 *lhs, const zig_i64 *rhs, zig_u8 bits) |
| 845 | | { |
| 846 | | for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i64(&res[i], lhs[i], rhs[i], bits); |
| 847 | | } |
| 848 | | |
| 849 | | static inline bool zig_mulo_u8(zig_u8 *res, zig_u8 lhs, zig_u8 rhs, zig_u8 bits) { |
| 871 | static inline bool zig_mulo_u8(uint8_t *res, uint8_t lhs, uint8_t rhs, uint8_t bits) { |
| 850 | 872 | #if zig_has_builtin(mul_overflow) || defined(zig_gnuc) |
| 851 | | zig_u8 full_res; |
| 873 | uint8_t full_res; |
| 852 | 874 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 853 | 875 | *res = zig_wrap_u8(full_res, bits); |
| 854 | | return overflow || full_res < zig_minInt(u8, bits) || full_res > zig_maxInt(u8, bits); |
| 876 | return overflow || full_res < zig_minInt_u(8, bits) || full_res > zig_maxInt_u(8, bits); |
| 855 | 877 | #else |
| 856 | | zig_u32 full_res; |
| 878 | uint32_t full_res; |
| 857 | 879 | bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits); |
| 858 | | *res = (zig_u8)full_res; |
| 880 | *res = (uint8_t)full_res; |
| 859 | 881 | return overflow; |
| 860 | 882 | #endif |
| 861 | 883 | } |
| 862 | 884 | |
| 863 | | static inline void zig_vmulo_u8(zig_u8 *ov, zig_u8 *res, int n, |
| 864 | | const zig_u8 *lhs, const zig_u8 *rhs, zig_u8 bits) |
| 865 | | { |
| 866 | | for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u8(&res[i], lhs[i], rhs[i], bits); |
| 867 | | } |
| 868 | | |
| 869 | | static inline bool zig_mulo_i8(zig_i8 *res, zig_i8 lhs, zig_i8 rhs, zig_u8 bits) { |
| 885 | static inline bool zig_mulo_i8(int8_t *res, int8_t lhs, int8_t rhs, uint8_t bits) { |
| 870 | 886 | #if zig_has_builtin(mul_overflow) || defined(zig_gnuc) |
| 871 | | zig_i8 full_res; |
| 887 | int8_t full_res; |
| 872 | 888 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 873 | 889 | *res = zig_wrap_i8(full_res, bits); |
| 874 | | return overflow || full_res < zig_minInt(i8, bits) || full_res > zig_maxInt(i8, bits); |
| 890 | return overflow || full_res < zig_minInt_i(8, bits) || full_res > zig_maxInt_i(8, bits); |
| 875 | 891 | #else |
| 876 | | zig_i32 full_res; |
| 892 | int32_t full_res; |
| 877 | 893 | bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits); |
| 878 | | *res = (zig_i8)full_res; |
| 894 | *res = (int8_t)full_res; |
| 879 | 895 | return overflow; |
| 880 | 896 | #endif |
| 881 | 897 | } |
| 882 | 898 | |
| 883 | | static inline void zig_vmulo_i8(zig_u8 *ov, zig_i8 *res, int n, |
| 884 | | const zig_i8 *lhs, const zig_i8 *rhs, zig_u8 bits) |
| 885 | | { |
| 886 | | for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i8(&res[i], lhs[i], rhs[i], bits); |
| 887 | | } |
| 888 | | |
| 889 | | static inline bool zig_mulo_u16(zig_u16 *res, zig_u16 lhs, zig_u16 rhs, zig_u8 bits) { |
| 899 | static inline bool zig_mulo_u16(uint16_t *res, uint16_t lhs, uint16_t rhs, uint8_t bits) { |
| 890 | 900 | #if zig_has_builtin(mul_overflow) || defined(zig_gnuc) |
| 891 | | zig_u16 full_res; |
| 901 | uint16_t full_res; |
| 892 | 902 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 893 | 903 | *res = zig_wrap_u16(full_res, bits); |
| 894 | | return overflow || full_res < zig_minInt(u16, bits) || full_res > zig_maxInt(u16, bits); |
| 904 | return overflow || full_res < zig_minInt_u(16, bits) || full_res > zig_maxInt_u(16, bits); |
| 895 | 905 | #else |
| 896 | | zig_u32 full_res; |
| 906 | uint32_t full_res; |
| 897 | 907 | bool overflow = zig_mulo_u32(&full_res, lhs, rhs, bits); |
| 898 | | *res = (zig_u16)full_res; |
| 908 | *res = (uint16_t)full_res; |
| 899 | 909 | return overflow; |
| 900 | 910 | #endif |
| 901 | 911 | } |
| 902 | 912 | |
| 903 | | static inline void zig_vmulo_u16(zig_u8 *ov, zig_u16 *res, int n, |
| 904 | | const zig_u16 *lhs, const zig_u16 *rhs, zig_u8 bits) |
| 905 | | { |
| 906 | | for (int i = 0; i < n; ++i) ov[i] = zig_mulo_u16(&res[i], lhs[i], rhs[i], bits); |
| 907 | | } |
| 908 | | |
| 909 | | static inline bool zig_mulo_i16(zig_i16 *res, zig_i16 lhs, zig_i16 rhs, zig_u8 bits) { |
| 913 | static inline bool zig_mulo_i16(int16_t *res, int16_t lhs, int16_t rhs, uint8_t bits) { |
| 910 | 914 | #if zig_has_builtin(mul_overflow) || defined(zig_gnuc) |
| 911 | | zig_i16 full_res; |
| 915 | int16_t full_res; |
| 912 | 916 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 913 | 917 | *res = zig_wrap_i16(full_res, bits); |
| 914 | | return overflow || full_res < zig_minInt(i16, bits) || full_res > zig_maxInt(i16, bits); |
| 918 | return overflow || full_res < zig_minInt_i(16, bits) || full_res > zig_maxInt_i(16, bits); |
| 915 | 919 | #else |
| 916 | | zig_i32 full_res; |
| 920 | int32_t full_res; |
| 917 | 921 | bool overflow = zig_mulo_i32(&full_res, lhs, rhs, bits); |
| 918 | | *res = (zig_i16)full_res; |
| 922 | *res = (int16_t)full_res; |
| 919 | 923 | return overflow; |
| 920 | 924 | #endif |
| 921 | 925 | } |
| 922 | 926 | |
| 923 | | static inline void zig_vmulo_i16(zig_u8 *ov, zig_i16 *res, int n, |
| 924 | | const zig_i16 *lhs, const zig_i16 *rhs, zig_u8 bits) |
| 925 | | { |
| 926 | | for (int i = 0; i < n; ++i) ov[i] = zig_mulo_i16(&res[i], lhs[i], rhs[i], bits); |
| 927 | | } |
| 928 | | |
| 929 | 927 | #define zig_int_builtins(w) \ |
| 930 | | static inline bool zig_shlo_u##w(zig_u##w *res, zig_u##w lhs, zig_u8 rhs, zig_u8 bits) { \ |
| 928 | static inline bool zig_shlo_u##w(uint##w##_t *res, uint##w##_t lhs, uint8_t rhs, uint8_t bits) { \ |
| 931 | 929 | *res = zig_shlw_u##w(lhs, rhs, bits); \ |
| 932 | | return lhs > zig_maxInt(u##w, bits) >> rhs; \ |
| 930 | return lhs > zig_maxInt_u(w, bits) >> rhs; \ |
| 933 | 931 | } \ |
| 934 | 932 | \ |
| 935 | | static inline bool zig_shlo_i##w(zig_i##w *res, zig_i##w lhs, zig_u8 rhs, zig_u8 bits) { \ |
| 933 | static inline bool zig_shlo_i##w(int##w##_t *res, int##w##_t lhs, uint8_t rhs, uint8_t bits) { \ |
| 936 | 934 | *res = zig_shlw_i##w(lhs, rhs, bits); \ |
| 937 | | zig_i##w mask = (zig_i##w)(zig_maxInt_u##w << (bits - rhs - 1)); \ |
| 938 | | return (lhs & mask) != zig_as_i##w(0) && (lhs & mask) != mask; \ |
| 935 | int##w##_t mask = (int##w##_t)(UINT##w##_MAX << (bits - rhs - 1)); \ |
| 936 | return (lhs & mask) != INT##w##_C(0) && (lhs & mask) != mask; \ |
| 939 | 937 | } \ |
| 940 | 938 | \ |
| 941 | | static inline zig_u##w zig_shls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 942 | | zig_u##w res; \ |
| 943 | | if (rhs >= bits) return lhs != zig_as_u##w(0) ? zig_maxInt(u##w, bits) : lhs; \ |
| 944 | | return zig_shlo_u##w(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u##w, bits) : res; \ |
| 939 | static inline uint##w##_t zig_shls_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 940 | uint##w##_t res; \ |
| 941 | if (rhs >= bits) return lhs != UINT##w##_C(0) ? zig_maxInt_u(w, bits) : lhs; \ |
| 942 | return zig_shlo_u##w(&res, lhs, (uint8_t)rhs, bits) ? zig_maxInt_u(w, bits) : res; \ |
| 945 | 943 | } \ |
| 946 | 944 | \ |
| 947 | | static inline zig_i##w zig_shls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 948 | | zig_i##w res; \ |
| 949 | | if ((zig_u##w)rhs < (zig_u##w)bits && !zig_shlo_i##w(&res, lhs, rhs, bits)) return res; \ |
| 950 | | return lhs < zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \ |
| 945 | static inline int##w##_t zig_shls_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \ |
| 946 | int##w##_t res; \ |
| 947 | if ((uint##w##_t)rhs < (uint##w##_t)bits && !zig_shlo_i##w(&res, lhs, (uint8_t)rhs, bits)) return res; \ |
| 948 | return lhs < INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \ |
| 951 | 949 | } \ |
| 952 | 950 | \ |
| 953 | | static inline zig_u##w zig_adds_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 954 | | zig_u##w res; \ |
| 955 | | return zig_addo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \ |
| 951 | static inline uint##w##_t zig_adds_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 952 | uint##w##_t res; \ |
| 953 | return zig_addo_u##w(&res, lhs, rhs, bits) ? zig_maxInt_u(w, bits) : res; \ |
| 956 | 954 | } \ |
| 957 | 955 | \ |
| 958 | | static inline zig_i##w zig_adds_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 959 | | zig_i##w res; \ |
| 956 | static inline int##w##_t zig_adds_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \ |
| 957 | int##w##_t res; \ |
| 960 | 958 | if (!zig_addo_i##w(&res, lhs, rhs, bits)) return res; \ |
| 961 | | return res >= zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \ |
| 959 | return res >= INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \ |
| 962 | 960 | } \ |
| 963 | 961 | \ |
| 964 | | static inline zig_u##w zig_subs_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 965 | | zig_u##w res; \ |
| 966 | | return zig_subo_u##w(&res, lhs, rhs, bits) ? zig_minInt(u##w, bits) : res; \ |
| 962 | static inline uint##w##_t zig_subs_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 963 | uint##w##_t res; \ |
| 964 | return zig_subo_u##w(&res, lhs, rhs, bits) ? zig_minInt_u(w, bits) : res; \ |
| 967 | 965 | } \ |
| 968 | 966 | \ |
| 969 | | static inline zig_i##w zig_subs_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 970 | | zig_i##w res; \ |
| 967 | static inline int##w##_t zig_subs_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \ |
| 968 | int##w##_t res; \ |
| 971 | 969 | if (!zig_subo_i##w(&res, lhs, rhs, bits)) return res; \ |
| 972 | | return res >= zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \ |
| 970 | return res >= INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \ |
| 973 | 971 | } \ |
| 974 | 972 | \ |
| 975 | | static inline zig_u##w zig_muls_u##w(zig_u##w lhs, zig_u##w rhs, zig_u8 bits) { \ |
| 976 | | zig_u##w res; \ |
| 977 | | return zig_mulo_u##w(&res, lhs, rhs, bits) ? zig_maxInt(u##w, bits) : res; \ |
| 973 | static inline uint##w##_t zig_muls_u##w(uint##w##_t lhs, uint##w##_t rhs, uint8_t bits) { \ |
| 974 | uint##w##_t res; \ |
| 975 | return zig_mulo_u##w(&res, lhs, rhs, bits) ? zig_maxInt_u(w, bits) : res; \ |
| 978 | 976 | } \ |
| 979 | 977 | \ |
| 980 | | static inline zig_i##w zig_muls_i##w(zig_i##w lhs, zig_i##w rhs, zig_u8 bits) { \ |
| 981 | | zig_i##w res; \ |
| 978 | static inline int##w##_t zig_muls_i##w(int##w##_t lhs, int##w##_t rhs, uint8_t bits) { \ |
| 979 | int##w##_t res; \ |
| 982 | 980 | if (!zig_mulo_i##w(&res, lhs, rhs, bits)) return res; \ |
| 983 | | return (lhs ^ rhs) < zig_as_i##w(0) ? zig_minInt(i##w, bits) : zig_maxInt(i##w, bits); \ |
| 981 | return (lhs ^ rhs) < INT##w##_C(0) ? zig_minInt_i(w, bits) : zig_maxInt_i(w, bits); \ |
| 984 | 982 | } |
| 985 | 983 | zig_int_builtins(8) |
| 986 | 984 | zig_int_builtins(16) |
| ... | ... | @@ -988,89 +986,89 @@ zig_int_builtins(32) |
| 988 | 986 | zig_int_builtins(64) |
| 989 | 987 | |
| 990 | 988 | #define zig_builtin8(name, val) __builtin_##name(val) |
| 991 | | typedef zig_c_uint zig_Builtin8; |
| 989 | typedef unsigned int zig_Builtin8; |
| 992 | 990 | |
| 993 | 991 | #define zig_builtin16(name, val) __builtin_##name(val) |
| 994 | | typedef zig_c_uint zig_Builtin16; |
| 992 | typedef unsigned int zig_Builtin16; |
| 995 | 993 | |
| 996 | 994 | #if INT_MIN <= INT32_MIN |
| 997 | 995 | #define zig_builtin32(name, val) __builtin_##name(val) |
| 998 | | typedef zig_c_uint zig_Builtin32; |
| 996 | typedef unsigned int zig_Builtin32; |
| 999 | 997 | #elif LONG_MIN <= INT32_MIN |
| 1000 | 998 | #define zig_builtin32(name, val) __builtin_##name##l(val) |
| 1001 | | typedef zig_c_ulong zig_Builtin32; |
| 999 | typedef unsigned long zig_Builtin32; |
| 1002 | 1000 | #endif |
| 1003 | 1001 | |
| 1004 | 1002 | #if INT_MIN <= INT64_MIN |
| 1005 | 1003 | #define zig_builtin64(name, val) __builtin_##name(val) |
| 1006 | | typedef zig_c_uint zig_Builtin64; |
| 1004 | typedef unsigned int zig_Builtin64; |
| 1007 | 1005 | #elif LONG_MIN <= INT64_MIN |
| 1008 | 1006 | #define zig_builtin64(name, val) __builtin_##name##l(val) |
| 1009 | | typedef zig_c_ulong zig_Builtin64; |
| 1007 | typedef unsigned long zig_Builtin64; |
| 1010 | 1008 | #elif LLONG_MIN <= INT64_MIN |
| 1011 | 1009 | #define zig_builtin64(name, val) __builtin_##name##ll(val) |
| 1012 | | typedef zig_c_ulonglong zig_Builtin64; |
| 1010 | typedef unsigned long long zig_Builtin64; |
| 1013 | 1011 | #endif |
| 1014 | 1012 | |
| 1015 | | static inline zig_u8 zig_byte_swap_u8(zig_u8 val, zig_u8 bits) { |
| 1013 | static inline uint8_t zig_byte_swap_u8(uint8_t val, uint8_t bits) { |
| 1016 | 1014 | return zig_wrap_u8(val >> (8 - bits), bits); |
| 1017 | 1015 | } |
| 1018 | 1016 | |
| 1019 | | static inline zig_i8 zig_byte_swap_i8(zig_i8 val, zig_u8 bits) { |
| 1020 | | return zig_wrap_i8((zig_i8)zig_byte_swap_u8((zig_u8)val, bits), bits); |
| 1017 | static inline int8_t zig_byte_swap_i8(int8_t val, uint8_t bits) { |
| 1018 | return zig_wrap_i8((int8_t)zig_byte_swap_u8((uint8_t)val, bits), bits); |
| 1021 | 1019 | } |
| 1022 | 1020 | |
| 1023 | | static inline zig_u16 zig_byte_swap_u16(zig_u16 val, zig_u8 bits) { |
| 1024 | | zig_u16 full_res; |
| 1021 | static inline uint16_t zig_byte_swap_u16(uint16_t val, uint8_t bits) { |
| 1022 | uint16_t full_res; |
| 1025 | 1023 | #if zig_has_builtin(bswap16) || defined(zig_gnuc) |
| 1026 | 1024 | full_res = __builtin_bswap16(val); |
| 1027 | 1025 | #else |
| 1028 | | full_res = (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 0), 8) << 8 | |
| 1029 | | (zig_u16)zig_byte_swap_u8((zig_u8)(val >> 8), 8) >> 0; |
| 1026 | full_res = (uint16_t)zig_byte_swap_u8((uint8_t)(val >> 0), 8) << 8 | |
| 1027 | (uint16_t)zig_byte_swap_u8((uint8_t)(val >> 8), 8) >> 0; |
| 1030 | 1028 | #endif |
| 1031 | 1029 | return zig_wrap_u16(full_res >> (16 - bits), bits); |
| 1032 | 1030 | } |
| 1033 | 1031 | |
| 1034 | | static inline zig_i16 zig_byte_swap_i16(zig_i16 val, zig_u8 bits) { |
| 1035 | | return zig_wrap_i16((zig_i16)zig_byte_swap_u16((zig_u16)val, bits), bits); |
| 1032 | static inline int16_t zig_byte_swap_i16(int16_t val, uint8_t bits) { |
| 1033 | return zig_wrap_i16((int16_t)zig_byte_swap_u16((uint16_t)val, bits), bits); |
| 1036 | 1034 | } |
| 1037 | 1035 | |
| 1038 | | static inline zig_u32 zig_byte_swap_u32(zig_u32 val, zig_u8 bits) { |
| 1039 | | zig_u32 full_res; |
| 1036 | static inline uint32_t zig_byte_swap_u32(uint32_t val, uint8_t bits) { |
| 1037 | uint32_t full_res; |
| 1040 | 1038 | #if zig_has_builtin(bswap32) || defined(zig_gnuc) |
| 1041 | 1039 | full_res = __builtin_bswap32(val); |
| 1042 | 1040 | #else |
| 1043 | | full_res = (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 0), 16) << 16 | |
| 1044 | | (zig_u32)zig_byte_swap_u16((zig_u16)(val >> 16), 16) >> 0; |
| 1041 | full_res = (uint32_t)zig_byte_swap_u16((uint16_t)(val >> 0), 16) << 16 | |
| 1042 | (uint32_t)zig_byte_swap_u16((uint16_t)(val >> 16), 16) >> 0; |
| 1045 | 1043 | #endif |
| 1046 | 1044 | return zig_wrap_u32(full_res >> (32 - bits), bits); |
| 1047 | 1045 | } |
| 1048 | 1046 | |
| 1049 | | static inline zig_i32 zig_byte_swap_i32(zig_i32 val, zig_u8 bits) { |
| 1050 | | return zig_wrap_i32((zig_i32)zig_byte_swap_u32((zig_u32)val, bits), bits); |
| 1047 | static inline int32_t zig_byte_swap_i32(int32_t val, uint8_t bits) { |
| 1048 | return zig_wrap_i32((int32_t)zig_byte_swap_u32((uint32_t)val, bits), bits); |
| 1051 | 1049 | } |
| 1052 | 1050 | |
| 1053 | | static inline zig_u64 zig_byte_swap_u64(zig_u64 val, zig_u8 bits) { |
| 1054 | | zig_u64 full_res; |
| 1051 | static inline uint64_t zig_byte_swap_u64(uint64_t val, uint8_t bits) { |
| 1052 | uint64_t full_res; |
| 1055 | 1053 | #if zig_has_builtin(bswap64) || defined(zig_gnuc) |
| 1056 | 1054 | full_res = __builtin_bswap64(val); |
| 1057 | 1055 | #else |
| 1058 | | full_res = (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 0), 32) << 32 | |
| 1059 | | (zig_u64)zig_byte_swap_u32((zig_u32)(val >> 32), 32) >> 0; |
| 1056 | full_res = (uint64_t)zig_byte_swap_u32((uint32_t)(val >> 0), 32) << 32 | |
| 1057 | (uint64_t)zig_byte_swap_u32((uint32_t)(val >> 32), 32) >> 0; |
| 1060 | 1058 | #endif |
| 1061 | 1059 | return zig_wrap_u64(full_res >> (64 - bits), bits); |
| 1062 | 1060 | } |
| 1063 | 1061 | |
| 1064 | | static inline zig_i64 zig_byte_swap_i64(zig_i64 val, zig_u8 bits) { |
| 1065 | | return zig_wrap_i64((zig_i64)zig_byte_swap_u64((zig_u64)val, bits), bits); |
| 1062 | static inline int64_t zig_byte_swap_i64(int64_t val, uint8_t bits) { |
| 1063 | return zig_wrap_i64((int64_t)zig_byte_swap_u64((uint64_t)val, bits), bits); |
| 1066 | 1064 | } |
| 1067 | 1065 | |
| 1068 | | static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) { |
| 1069 | | zig_u8 full_res; |
| 1066 | static inline uint8_t zig_bit_reverse_u8(uint8_t val, uint8_t bits) { |
| 1067 | uint8_t full_res; |
| 1070 | 1068 | #if zig_has_builtin(bitreverse8) |
| 1071 | 1069 | full_res = __builtin_bitreverse8(val); |
| 1072 | 1070 | #else |
| 1073 | | static zig_u8 const lut[0x10] = { |
| 1071 | static uint8_t const lut[0x10] = { |
| 1074 | 1072 | 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, |
| 1075 | 1073 | 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf |
| 1076 | 1074 | }; |
| ... | ... | @@ -1079,62 +1077,62 @@ static inline zig_u8 zig_bit_reverse_u8(zig_u8 val, zig_u8 bits) { |
| 1079 | 1077 | return zig_wrap_u8(full_res >> (8 - bits), bits); |
| 1080 | 1078 | } |
| 1081 | 1079 | |
| 1082 | | static inline zig_i8 zig_bit_reverse_i8(zig_i8 val, zig_u8 bits) { |
| 1083 | | return zig_wrap_i8((zig_i8)zig_bit_reverse_u8((zig_u8)val, bits), bits); |
| 1080 | static inline int8_t zig_bit_reverse_i8(int8_t val, uint8_t bits) { |
| 1081 | return zig_wrap_i8((int8_t)zig_bit_reverse_u8((uint8_t)val, bits), bits); |
| 1084 | 1082 | } |
| 1085 | 1083 | |
| 1086 | | static inline zig_u16 zig_bit_reverse_u16(zig_u16 val, zig_u8 bits) { |
| 1087 | | zig_u16 full_res; |
| 1084 | static inline uint16_t zig_bit_reverse_u16(uint16_t val, uint8_t bits) { |
| 1085 | uint16_t full_res; |
| 1088 | 1086 | #if zig_has_builtin(bitreverse16) |
| 1089 | 1087 | full_res = __builtin_bitreverse16(val); |
| 1090 | 1088 | #else |
| 1091 | | full_res = (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 0), 8) << 8 | |
| 1092 | | (zig_u16)zig_bit_reverse_u8((zig_u8)(val >> 8), 8) >> 0; |
| 1089 | full_res = (uint16_t)zig_bit_reverse_u8((uint8_t)(val >> 0), 8) << 8 | |
| 1090 | (uint16_t)zig_bit_reverse_u8((uint8_t)(val >> 8), 8) >> 0; |
| 1093 | 1091 | #endif |
| 1094 | 1092 | return zig_wrap_u16(full_res >> (16 - bits), bits); |
| 1095 | 1093 | } |
| 1096 | 1094 | |
| 1097 | | static inline zig_i16 zig_bit_reverse_i16(zig_i16 val, zig_u8 bits) { |
| 1098 | | return zig_wrap_i16((zig_i16)zig_bit_reverse_u16((zig_u16)val, bits), bits); |
| 1095 | static inline int16_t zig_bit_reverse_i16(int16_t val, uint8_t bits) { |
| 1096 | return zig_wrap_i16((int16_t)zig_bit_reverse_u16((uint16_t)val, bits), bits); |
| 1099 | 1097 | } |
| 1100 | 1098 | |
| 1101 | | static inline zig_u32 zig_bit_reverse_u32(zig_u32 val, zig_u8 bits) { |
| 1102 | | zig_u32 full_res; |
| 1099 | static inline uint32_t zig_bit_reverse_u32(uint32_t val, uint8_t bits) { |
| 1100 | uint32_t full_res; |
| 1103 | 1101 | #if zig_has_builtin(bitreverse32) |
| 1104 | 1102 | full_res = __builtin_bitreverse32(val); |
| 1105 | 1103 | #else |
| 1106 | | full_res = (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 0), 16) << 16 | |
| 1107 | | (zig_u32)zig_bit_reverse_u16((zig_u16)(val >> 16), 16) >> 0; |
| 1104 | full_res = (uint32_t)zig_bit_reverse_u16((uint16_t)(val >> 0), 16) << 16 | |
| 1105 | (uint32_t)zig_bit_reverse_u16((uint16_t)(val >> 16), 16) >> 0; |
| 1108 | 1106 | #endif |
| 1109 | 1107 | return zig_wrap_u32(full_res >> (32 - bits), bits); |
| 1110 | 1108 | } |
| 1111 | 1109 | |
| 1112 | | static inline zig_i32 zig_bit_reverse_i32(zig_i32 val, zig_u8 bits) { |
| 1113 | | return zig_wrap_i32((zig_i32)zig_bit_reverse_u32((zig_u32)val, bits), bits); |
| 1110 | static inline int32_t zig_bit_reverse_i32(int32_t val, uint8_t bits) { |
| 1111 | return zig_wrap_i32((int32_t)zig_bit_reverse_u32((uint32_t)val, bits), bits); |
| 1114 | 1112 | } |
| 1115 | 1113 | |
| 1116 | | static inline zig_u64 zig_bit_reverse_u64(zig_u64 val, zig_u8 bits) { |
| 1117 | | zig_u64 full_res; |
| 1114 | static inline uint64_t zig_bit_reverse_u64(uint64_t val, uint8_t bits) { |
| 1115 | uint64_t full_res; |
| 1118 | 1116 | #if zig_has_builtin(bitreverse64) |
| 1119 | 1117 | full_res = __builtin_bitreverse64(val); |
| 1120 | 1118 | #else |
| 1121 | | full_res = (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 0), 32) << 32 | |
| 1122 | | (zig_u64)zig_bit_reverse_u32((zig_u32)(val >> 32), 32) >> 0; |
| 1119 | full_res = (uint64_t)zig_bit_reverse_u32((uint32_t)(val >> 0), 32) << 32 | |
| 1120 | (uint64_t)zig_bit_reverse_u32((uint32_t)(val >> 32), 32) >> 0; |
| 1123 | 1121 | #endif |
| 1124 | 1122 | return zig_wrap_u64(full_res >> (64 - bits), bits); |
| 1125 | 1123 | } |
| 1126 | 1124 | |
| 1127 | | static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) { |
| 1128 | | return zig_wrap_i64((zig_i64)zig_bit_reverse_u64((zig_u64)val, bits), bits); |
| 1125 | static inline int64_t zig_bit_reverse_i64(int64_t val, uint8_t bits) { |
| 1126 | return zig_wrap_i64((int64_t)zig_bit_reverse_u64((uint64_t)val, bits), bits); |
| 1129 | 1127 | } |
| 1130 | 1128 | |
| 1131 | 1129 | #define zig_builtin_popcount_common(w) \ |
| 1132 | | static inline zig_u8 zig_popcount_i##w(zig_i##w val, zig_u8 bits) { \ |
| 1133 | | return zig_popcount_u##w((zig_u##w)val, bits); \ |
| 1130 | static inline uint8_t zig_popcount_i##w(int##w##_t val, uint8_t bits) { \ |
| 1131 | return zig_popcount_u##w((uint##w##_t)val, bits); \ |
| 1134 | 1132 | } |
| 1135 | 1133 | #if zig_has_builtin(popcount) || defined(zig_gnuc) |
| 1136 | 1134 | #define zig_builtin_popcount(w) \ |
| 1137 | | static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \ |
| 1135 | static inline uint8_t zig_popcount_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1138 | 1136 | (void)bits; \ |
| 1139 | 1137 | return zig_builtin##w(popcount, val); \ |
| 1140 | 1138 | } \ |
| ... | ... | @@ -1142,12 +1140,12 @@ static inline zig_i64 zig_bit_reverse_i64(zig_i64 val, zig_u8 bits) { |
| 1142 | 1140 | zig_builtin_popcount_common(w) |
| 1143 | 1141 | #else |
| 1144 | 1142 | #define zig_builtin_popcount(w) \ |
| 1145 | | static inline zig_u8 zig_popcount_u##w(zig_u##w val, zig_u8 bits) { \ |
| 1143 | static inline uint8_t zig_popcount_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1146 | 1144 | (void)bits; \ |
| 1147 | | zig_u##w temp = val - ((val >> 1) & (zig_maxInt_u##w / 3)); \ |
| 1148 | | temp = (temp & (zig_maxInt_u##w / 5)) + ((temp >> 2) & (zig_maxInt_u##w / 5)); \ |
| 1149 | | temp = (temp + (temp >> 4)) & (zig_maxInt_u##w / 17); \ |
| 1150 | | return temp * (zig_maxInt_u##w / 255) >> (w - 8); \ |
| 1145 | uint##w##_t temp = val - ((val >> 1) & (UINT##w##_MAX / 3)); \ |
| 1146 | temp = (temp & (UINT##w##_MAX / 5)) + ((temp >> 2) & (UINT##w##_MAX / 5)); \ |
| 1147 | temp = (temp + (temp >> 4)) & (UINT##w##_MAX / 17); \ |
| 1148 | return temp * (UINT##w##_MAX / 255) >> (w - 8); \ |
| 1151 | 1149 | } \ |
| 1152 | 1150 | \ |
| 1153 | 1151 | zig_builtin_popcount_common(w) |
| ... | ... | @@ -1158,12 +1156,12 @@ zig_builtin_popcount(32) |
| 1158 | 1156 | zig_builtin_popcount(64) |
| 1159 | 1157 | |
| 1160 | 1158 | #define zig_builtin_ctz_common(w) \ |
| 1161 | | static inline zig_u8 zig_ctz_i##w(zig_i##w val, zig_u8 bits) { \ |
| 1162 | | return zig_ctz_u##w((zig_u##w)val, bits); \ |
| 1159 | static inline uint8_t zig_ctz_i##w(int##w##_t val, uint8_t bits) { \ |
| 1160 | return zig_ctz_u##w((uint##w##_t)val, bits); \ |
| 1163 | 1161 | } |
| 1164 | 1162 | #if zig_has_builtin(ctz) || defined(zig_gnuc) |
| 1165 | 1163 | #define zig_builtin_ctz(w) \ |
| 1166 | | static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \ |
| 1164 | static inline uint8_t zig_ctz_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1167 | 1165 | if (val == 0) return bits; \ |
| 1168 | 1166 | return zig_builtin##w(ctz, val); \ |
| 1169 | 1167 | } \ |
| ... | ... | @@ -1171,7 +1169,7 @@ zig_builtin_popcount(64) |
| 1171 | 1169 | zig_builtin_ctz_common(w) |
| 1172 | 1170 | #else |
| 1173 | 1171 | #define zig_builtin_ctz(w) \ |
| 1174 | | static inline zig_u8 zig_ctz_u##w(zig_u##w val, zig_u8 bits) { \ |
| 1172 | static inline uint8_t zig_ctz_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1175 | 1173 | return zig_popcount_u##w(zig_not_u##w(val, bits) & zig_subw_u##w(val, 1, bits), bits); \ |
| 1176 | 1174 | } \ |
| 1177 | 1175 | \ |
| ... | ... | @@ -1183,12 +1181,12 @@ zig_builtin_ctz(32) |
| 1183 | 1181 | zig_builtin_ctz(64) |
| 1184 | 1182 | |
| 1185 | 1183 | #define zig_builtin_clz_common(w) \ |
| 1186 | | static inline zig_u8 zig_clz_i##w(zig_i##w val, zig_u8 bits) { \ |
| 1187 | | return zig_clz_u##w((zig_u##w)val, bits); \ |
| 1184 | static inline uint8_t zig_clz_i##w(int##w##_t val, uint8_t bits) { \ |
| 1185 | return zig_clz_u##w((uint##w##_t)val, bits); \ |
| 1188 | 1186 | } |
| 1189 | 1187 | #if zig_has_builtin(clz) || defined(zig_gnuc) |
| 1190 | 1188 | #define zig_builtin_clz(w) \ |
| 1191 | | static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \ |
| 1189 | static inline uint8_t zig_clz_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1192 | 1190 | if (val == 0) return bits; \ |
| 1193 | 1191 | return zig_builtin##w(clz, val) - (zig_bitSizeOf(zig_Builtin##w) - bits); \ |
| 1194 | 1192 | } \ |
| ... | ... | @@ -1196,7 +1194,7 @@ zig_builtin_ctz(64) |
| 1196 | 1194 | zig_builtin_clz_common(w) |
| 1197 | 1195 | #else |
| 1198 | 1196 | #define zig_builtin_clz(w) \ |
| 1199 | | static inline zig_u8 zig_clz_u##w(zig_u##w val, zig_u8 bits) { \ |
| 1197 | static inline uint8_t zig_clz_u##w(uint##w##_t val, uint8_t bits) { \ |
| 1200 | 1198 | return zig_ctz_u##w(zig_bit_reverse_u##w(val, bits), bits); \ |
| 1201 | 1199 | } \ |
| 1202 | 1200 | \ |
| ... | ... | @@ -1207,7 +1205,7 @@ zig_builtin_clz(16) |
| 1207 | 1205 | zig_builtin_clz(32) |
| 1208 | 1206 | zig_builtin_clz(64) |
| 1209 | 1207 | |
| 1210 | | /* ======================== 128-bit Integer Routines ======================== */ |
| 1208 | /* ======================== 128-bit Integer Support ========================= */ |
| 1211 | 1209 | |
| 1212 | 1210 | #if !defined(zig_has_int128) |
| 1213 | 1211 | # if defined(__SIZEOF_INT128__) |
| ... | ... | @@ -1222,18 +1220,18 @@ zig_builtin_clz(64) |
| 1222 | 1220 | typedef unsigned __int128 zig_u128; |
| 1223 | 1221 | typedef signed __int128 zig_i128; |
| 1224 | 1222 | |
| 1225 | | #define zig_as_u128(hi, lo) ((zig_u128)(hi)<<64|(lo)) |
| 1226 | | #define zig_as_i128(hi, lo) ((zig_i128)zig_as_u128(hi, lo)) |
| 1227 | | #define zig_as_constant_u128(hi, lo) zig_as_u128(hi, lo) |
| 1228 | | #define zig_as_constant_i128(hi, lo) zig_as_i128(hi, lo) |
| 1229 | | #define zig_hi_u128(val) ((zig_u64)((val) >> 64)) |
| 1230 | | #define zig_lo_u128(val) ((zig_u64)((val) >> 0)) |
| 1231 | | #define zig_hi_i128(val) ((zig_i64)((val) >> 64)) |
| 1232 | | #define zig_lo_i128(val) ((zig_u64)((val) >> 0)) |
| 1223 | #define zig_make_u128(hi, lo) ((zig_u128)(hi)<<64|(lo)) |
| 1224 | #define zig_make_i128(hi, lo) ((zig_i128)zig_make_u128(hi, lo)) |
| 1225 | #define zig_init_u128(hi, lo) zig_make_u128(hi, lo) |
| 1226 | #define zig_init_i128(hi, lo) zig_make_i128(hi, lo) |
| 1227 | #define zig_hi_u128(val) ((uint64_t)((val) >> 64)) |
| 1228 | #define zig_lo_u128(val) ((uint64_t)((val) >> 0)) |
| 1229 | #define zig_hi_i128(val) (( int64_t)((val) >> 64)) |
| 1230 | #define zig_lo_i128(val) ((uint64_t)((val) >> 0)) |
| 1233 | 1231 | #define zig_bitcast_u128(val) ((zig_u128)(val)) |
| 1234 | 1232 | #define zig_bitcast_i128(val) ((zig_i128)(val)) |
| 1235 | 1233 | #define zig_cmp_int128(Type) \ |
| 1236 | | static inline zig_i32 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 1234 | static inline int32_t zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 1237 | 1235 | return (lhs > rhs) - (lhs < rhs); \ |
| 1238 | 1236 | } |
| 1239 | 1237 | #define zig_bit_int128(Type, operation, operator) \ |
| ... | ... | @@ -1243,32 +1241,32 @@ typedef signed __int128 zig_i128; |
| 1243 | 1241 | |
| 1244 | 1242 | #else /* zig_has_int128 */ |
| 1245 | 1243 | |
| 1246 | | #if __LITTLE_ENDIAN__ || _MSC_VER |
| 1247 | | typedef struct { zig_align(16) zig_u64 lo; zig_u64 hi; } zig_u128; |
| 1248 | | typedef struct { zig_align(16) zig_u64 lo; zig_i64 hi; } zig_i128; |
| 1244 | #if zig_little_endian |
| 1245 | typedef struct { zig_align(16) uint64_t lo; uint64_t hi; } zig_u128; |
| 1246 | typedef struct { zig_align(16) uint64_t lo; int64_t hi; } zig_i128; |
| 1249 | 1247 | #else |
| 1250 | | typedef struct { zig_align(16) zig_u64 hi; zig_u64 lo; } zig_u128; |
| 1251 | | typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128; |
| 1248 | typedef struct { zig_align(16) uint64_t hi; uint64_t lo; } zig_u128; |
| 1249 | typedef struct { zig_align(16) int64_t hi; uint64_t lo; } zig_i128; |
| 1252 | 1250 | #endif |
| 1253 | 1251 | |
| 1254 | | #define zig_as_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) }) |
| 1255 | | #define zig_as_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) }) |
| 1252 | #define zig_make_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) }) |
| 1253 | #define zig_make_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) }) |
| 1256 | 1254 | |
| 1257 | | #if _MSC_VER |
| 1258 | | #define zig_as_constant_u128(hi, lo) { .h##i = (hi), .l##o = (lo) } |
| 1259 | | #define zig_as_constant_i128(hi, lo) { .h##i = (hi), .l##o = (lo) } |
| 1260 | | #else |
| 1261 | | #define zig_as_constant_u128(hi, lo) zig_as_u128(hi, lo) |
| 1262 | | #define zig_as_constant_i128(hi, lo) zig_as_i128(hi, lo) |
| 1255 | #if _MSC_VER /* MSVC doesn't allow struct literals in constant expressions */ |
| 1256 | #define zig_init_u128(hi, lo) { .h##i = (hi), .l##o = (lo) } |
| 1257 | #define zig_init_i128(hi, lo) { .h##i = (hi), .l##o = (lo) } |
| 1258 | #else /* But non-MSVC doesn't like the unprotected commas */ |
| 1259 | #define zig_init_u128(hi, lo) zig_make_u128(hi, lo) |
| 1260 | #define zig_init_i128(hi, lo) zig_make_i128(hi, lo) |
| 1263 | 1261 | #endif |
| 1264 | 1262 | #define zig_hi_u128(val) ((val).hi) |
| 1265 | 1263 | #define zig_lo_u128(val) ((val).lo) |
| 1266 | 1264 | #define zig_hi_i128(val) ((val).hi) |
| 1267 | 1265 | #define zig_lo_i128(val) ((val).lo) |
| 1268 | | #define zig_bitcast_u128(val) zig_as_u128((zig_u64)(val).hi, (val).lo) |
| 1269 | | #define zig_bitcast_i128(val) zig_as_i128((zig_i64)(val).hi, (val).lo) |
| 1266 | #define zig_bitcast_u128(val) zig_make_u128((uint64_t)(val).hi, (val).lo) |
| 1267 | #define zig_bitcast_i128(val) zig_make_i128(( int64_t)(val).hi, (val).lo) |
| 1270 | 1268 | #define zig_cmp_int128(Type) \ |
| 1271 | | static inline zig_i32 zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 1269 | static inline int32_t zig_cmp_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 1272 | 1270 | return (lhs.hi == rhs.hi) \ |
| 1273 | 1271 | ? (lhs.lo > rhs.lo) - (lhs.lo < rhs.lo) \ |
| 1274 | 1272 | : (lhs.hi > rhs.hi) - (lhs.hi < rhs.hi); \ |
| ... | ... | @@ -1280,10 +1278,10 @@ typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128; |
| 1280 | 1278 | |
| 1281 | 1279 | #endif /* zig_has_int128 */ |
| 1282 | 1280 | |
| 1283 | | #define zig_minInt_u128 zig_as_u128(zig_minInt_u64, zig_minInt_u64) |
| 1284 | | #define zig_maxInt_u128 zig_as_u128(zig_maxInt_u64, zig_maxInt_u64) |
| 1285 | | #define zig_minInt_i128 zig_as_i128(zig_minInt_i64, zig_minInt_u64) |
| 1286 | | #define zig_maxInt_i128 zig_as_i128(zig_maxInt_i64, zig_maxInt_u64) |
| 1281 | #define zig_minInt_u128 zig_make_u128(zig_minInt_u64, zig_minInt_u64) |
| 1282 | #define zig_maxInt_u128 zig_make_u128(zig_maxInt_u64, zig_maxInt_u64) |
| 1283 | #define zig_minInt_i128 zig_make_i128(zig_minInt_i64, zig_minInt_u64) |
| 1284 | #define zig_maxInt_i128 zig_make_i128(zig_maxInt_i64, zig_maxInt_u64) |
| 1287 | 1285 | |
| 1288 | 1286 | zig_cmp_int128(u128) |
| 1289 | 1287 | zig_cmp_int128(i128) |
| ... | ... | @@ -1297,28 +1295,33 @@ zig_bit_int128(i128, or, |) |
| 1297 | 1295 | zig_bit_int128(u128, xor, ^) |
| 1298 | 1296 | zig_bit_int128(i128, xor, ^) |
| 1299 | 1297 | |
| 1300 | | static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs); |
| 1298 | static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs); |
| 1301 | 1299 | |
| 1302 | 1300 | #if zig_has_int128 |
| 1303 | 1301 | |
| 1304 | | static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) { |
| 1305 | | return val ^ zig_maxInt(u128, bits); |
| 1302 | static inline zig_u128 zig_not_u128(zig_u128 val, uint8_t bits) { |
| 1303 | return val ^ zig_maxInt_u(128, bits); |
| 1306 | 1304 | } |
| 1307 | 1305 | |
| 1308 | | static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) { |
| 1306 | static inline zig_i128 zig_not_i128(zig_i128 val, uint8_t bits) { |
| 1309 | 1307 | (void)bits; |
| 1310 | 1308 | return ~val; |
| 1311 | 1309 | } |
| 1312 | 1310 | |
| 1313 | | static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) { |
| 1311 | static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) { |
| 1314 | 1312 | return lhs >> rhs; |
| 1315 | 1313 | } |
| 1316 | 1314 | |
| 1317 | | static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) { |
| 1315 | static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) { |
| 1318 | 1316 | return lhs << rhs; |
| 1319 | 1317 | } |
| 1320 | 1318 | |
| 1321 | | static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) { |
| 1319 | static inline zig_i128 zig_shr_i128(zig_i128 lhs, uint8_t rhs) { |
| 1320 | zig_i128 sign_mask = lhs < zig_make_i128(0, 0) ? -zig_make_i128(0, 1) : zig_make_i128(0, 0); |
| 1321 | return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; |
| 1322 | } |
| 1323 | |
| 1324 | static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) { |
| 1322 | 1325 | return lhs << rhs; |
| 1323 | 1326 | } |
| 1324 | 1327 | |
| ... | ... | @@ -1363,40 +1366,46 @@ static inline zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1363 | 1366 | } |
| 1364 | 1367 | |
| 1365 | 1368 | static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1366 | | return zig_div_trunc_i128(lhs, rhs) - (((lhs ^ rhs) & zig_rem_i128(lhs, rhs)) < zig_as_i128(0, 0)); |
| 1369 | return zig_div_trunc_i128(lhs, rhs) - (((lhs ^ rhs) & zig_rem_i128(lhs, rhs)) < zig_make_i128(0, 0)); |
| 1367 | 1370 | } |
| 1368 | 1371 | |
| 1369 | 1372 | static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1370 | 1373 | zig_i128 rem = zig_rem_i128(lhs, rhs); |
| 1371 | | return rem + (((lhs ^ rhs) & rem) < zig_as_i128(0, 0) ? rhs : zig_as_i128(0, 0)); |
| 1374 | return rem + (((lhs ^ rhs) & rem) < zig_make_i128(0, 0) ? rhs : zig_make_i128(0, 0)); |
| 1372 | 1375 | } |
| 1373 | 1376 | |
| 1374 | 1377 | #else /* zig_has_int128 */ |
| 1375 | 1378 | |
| 1376 | | static inline zig_u128 zig_not_u128(zig_u128 val, zig_u8 bits) { |
| 1377 | | return (zig_u128){ .hi = zig_not_u64(val.hi, bits - zig_as_u8(64)), .lo = zig_not_u64(val.lo, zig_as_u8(64)) }; |
| 1379 | static inline zig_u128 zig_not_u128(zig_u128 val, uint8_t bits) { |
| 1380 | return (zig_u128){ .hi = zig_not_u64(val.hi, bits - UINT8_C(64)), .lo = zig_not_u64(val.lo, UINT8_C(64)) }; |
| 1378 | 1381 | } |
| 1379 | 1382 | |
| 1380 | | static inline zig_i128 zig_not_i128(zig_i128 val, zig_u8 bits) { |
| 1381 | | return (zig_i128){ .hi = zig_not_i64(val.hi, bits - zig_as_u8(64)), .lo = zig_not_u64(val.lo, zig_as_u8(64)) }; |
| 1383 | static inline zig_i128 zig_not_i128(zig_i128 val, uint8_t bits) { |
| 1384 | return (zig_i128){ .hi = zig_not_i64(val.hi, bits - UINT8_C(64)), .lo = zig_not_u64(val.lo, UINT8_C(64)) }; |
| 1382 | 1385 | } |
| 1383 | 1386 | |
| 1384 | | static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) { |
| 1385 | | if (rhs == zig_as_u8(0)) return lhs; |
| 1386 | | if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = zig_minInt_u64, .lo = lhs.hi >> (rhs - zig_as_u8(64)) }; |
| 1387 | | return (zig_u128){ .hi = lhs.hi >> rhs, .lo = lhs.hi << (zig_as_u8(64) - rhs) | lhs.lo >> rhs }; |
| 1387 | static inline zig_u128 zig_shr_u128(zig_u128 lhs, uint8_t rhs) { |
| 1388 | if (rhs == UINT8_C(0)) return lhs; |
| 1389 | if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = zig_minInt_u64, .lo = lhs.hi >> (rhs - UINT8_C(64)) }; |
| 1390 | return (zig_u128){ .hi = lhs.hi >> rhs, .lo = lhs.hi << (UINT8_C(64) - rhs) | lhs.lo >> rhs }; |
| 1388 | 1391 | } |
| 1389 | 1392 | |
| 1390 | | static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) { |
| 1391 | | if (rhs == zig_as_u8(0)) return lhs; |
| 1392 | | if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = lhs.lo << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 }; |
| 1393 | | return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs }; |
| 1393 | static inline zig_u128 zig_shl_u128(zig_u128 lhs, uint8_t rhs) { |
| 1394 | if (rhs == UINT8_C(0)) return lhs; |
| 1395 | if (rhs >= UINT8_C(64)) return (zig_u128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 }; |
| 1396 | return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs }; |
| 1394 | 1397 | } |
| 1395 | 1398 | |
| 1396 | | static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) { |
| 1397 | | if (rhs == zig_as_u8(0)) return lhs; |
| 1398 | | if (rhs >= zig_as_u8(64)) return (zig_i128){ .hi = lhs.lo << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 }; |
| 1399 | | return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs }; |
| 1399 | static inline zig_i128 zig_shr_i128(zig_i128 lhs, uint8_t rhs) { |
| 1400 | if (rhs == UINT8_C(0)) return lhs; |
| 1401 | if (rhs >= UINT8_C(64)) return (zig_i128){ .hi = zig_shr_i64(lhs.hi, 63), .lo = zig_shr_i64(lhs.hi, (rhs - UINT8_C(64))) }; |
| 1402 | return (zig_i128){ .hi = zig_shr_i64(lhs.hi, rhs), .lo = lhs.lo >> rhs | (uint64_t)lhs.hi << (UINT8_C(64) - rhs) }; |
| 1403 | } |
| 1404 | |
| 1405 | static inline zig_i128 zig_shl_i128(zig_i128 lhs, uint8_t rhs) { |
| 1406 | if (rhs == UINT8_C(0)) return lhs; |
| 1407 | if (rhs >= UINT8_C(64)) return (zig_i128){ .hi = lhs.lo << (rhs - UINT8_C(64)), .lo = zig_minInt_u64 }; |
| 1408 | return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (UINT8_C(64) - rhs), .lo = lhs.lo << rhs }; |
| 1400 | 1409 | } |
| 1401 | 1410 | |
| 1402 | 1411 | static inline zig_u128 zig_add_u128(zig_u128 lhs, zig_u128 rhs) { |
| ... | ... | @@ -1424,14 +1433,14 @@ static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1424 | 1433 | } |
| 1425 | 1434 | |
| 1426 | 1435 | zig_extern zig_i128 __multi3(zig_i128 lhs, zig_i128 rhs); |
| 1427 | | static zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1428 | | return zig_bitcast_u128(__multi3(zig_bitcast_i128(lhs), zig_bitcast_i128(rhs))); |
| 1429 | | } |
| 1430 | | |
| 1431 | 1436 | static zig_i128 zig_mul_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1432 | 1437 | return __multi3(lhs, rhs); |
| 1433 | 1438 | } |
| 1434 | 1439 | |
| 1440 | static zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1441 | return zig_bitcast_u128(zig_mul_i128(zig_bitcast_i128(lhs), zig_bitcast_i128(rhs))); |
| 1442 | } |
| 1443 | |
| 1435 | 1444 | zig_extern zig_u128 __udivti3(zig_u128 lhs, zig_u128 rhs); |
| 1436 | 1445 | static zig_u128 zig_div_trunc_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1437 | 1446 | return __udivti3(lhs, rhs); |
| ... | ... | @@ -1454,11 +1463,11 @@ static zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1454 | 1463 | |
| 1455 | 1464 | static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1456 | 1465 | zig_i128 rem = zig_rem_i128(lhs, rhs); |
| 1457 | | return zig_add_i128(rem, (((lhs.hi ^ rhs.hi) & rem.hi) < zig_as_i64(0) ? rhs : zig_as_i128(0, 0))); |
| 1466 | return zig_add_i128(rem, ((lhs.hi ^ rhs.hi) & rem.hi) < INT64_C(0) ? rhs : zig_make_i128(0, 0)); |
| 1458 | 1467 | } |
| 1459 | 1468 | |
| 1460 | 1469 | static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1461 | | return zig_sub_i128(zig_div_trunc_i128(lhs, rhs), zig_as_i128(0, zig_cmp_i128(zig_and_i128(zig_xor_i128(lhs, rhs), zig_rem_i128(lhs, rhs)), zig_as_i128(0, 0)) < zig_as_i32(0))); |
| 1470 | return zig_sub_i128(zig_div_trunc_i128(lhs, rhs), zig_make_i128(0, zig_cmp_i128(zig_and_i128(zig_xor_i128(lhs, rhs), zig_rem_i128(lhs, rhs)), zig_make_i128(0, 0)) < INT32_C(0))); |
| 1462 | 1471 | } |
| 1463 | 1472 | |
| 1464 | 1473 | #endif /* zig_has_int128 */ |
| ... | ... | @@ -1471,326 +1480,1265 @@ static inline zig_u128 zig_nand_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1471 | 1480 | } |
| 1472 | 1481 | |
| 1473 | 1482 | static inline zig_u128 zig_min_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1474 | | return zig_cmp_u128(lhs, rhs) < zig_as_i32(0) ? lhs : rhs; |
| 1483 | return zig_cmp_u128(lhs, rhs) < INT32_C(0) ? lhs : rhs; |
| 1475 | 1484 | } |
| 1476 | 1485 | |
| 1477 | 1486 | static inline zig_i128 zig_min_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1478 | | return zig_cmp_i128(lhs, rhs) < zig_as_i32(0) ? lhs : rhs; |
| 1487 | return zig_cmp_i128(lhs, rhs) < INT32_C(0) ? lhs : rhs; |
| 1479 | 1488 | } |
| 1480 | 1489 | |
| 1481 | 1490 | static inline zig_u128 zig_max_u128(zig_u128 lhs, zig_u128 rhs) { |
| 1482 | | return zig_cmp_u128(lhs, rhs) > zig_as_i32(0) ? lhs : rhs; |
| 1491 | return zig_cmp_u128(lhs, rhs) > INT32_C(0) ? lhs : rhs; |
| 1483 | 1492 | } |
| 1484 | 1493 | |
| 1485 | 1494 | static inline zig_i128 zig_max_i128(zig_i128 lhs, zig_i128 rhs) { |
| 1486 | | return zig_cmp_i128(lhs, rhs) > zig_as_i32(0) ? lhs : rhs; |
| 1495 | return zig_cmp_i128(lhs, rhs) > INT32_C(0) ? lhs : rhs; |
| 1487 | 1496 | } |
| 1488 | 1497 | |
| 1489 | | static inline zig_i128 zig_shr_i128(zig_i128 lhs, zig_u8 rhs) { |
| 1490 | | zig_i128 sign_mask = zig_cmp_i128(lhs, zig_as_i128(0, 0)) < zig_as_i32(0) ? zig_sub_i128(zig_as_i128(0, 0), zig_as_i128(0, 1)) : zig_as_i128(0, 0); |
| 1491 | | return zig_xor_i128(zig_bitcast_i128(zig_shr_u128(zig_bitcast_u128(zig_xor_i128(lhs, sign_mask)), rhs)), sign_mask); |
| 1498 | static inline zig_u128 zig_wrap_u128(zig_u128 val, uint8_t bits) { |
| 1499 | return zig_and_u128(val, zig_maxInt_u(128, bits)); |
| 1492 | 1500 | } |
| 1493 | 1501 | |
| 1494 | | static inline zig_u128 zig_wrap_u128(zig_u128 val, zig_u8 bits) { |
| 1495 | | return zig_and_u128(val, zig_maxInt(u128, bits)); |
| 1502 | static inline zig_i128 zig_wrap_i128(zig_i128 val, uint8_t bits) { |
| 1503 | if (bits > UINT8_C(64)) return zig_make_i128(zig_wrap_i64(zig_hi_i128(val), bits - UINT8_C(64)), zig_lo_i128(val)); |
| 1504 | int64_t lo = zig_wrap_i64((int64_t)zig_lo_i128(val), bits); |
| 1505 | return zig_make_i128(zig_shr_i64(lo, 63), (uint64_t)lo); |
| 1496 | 1506 | } |
| 1497 | 1507 | |
| 1498 | | static inline zig_i128 zig_wrap_i128(zig_i128 val, zig_u8 bits) { |
| 1499 | | return zig_as_i128(zig_wrap_i64(zig_hi_i128(val), bits - zig_as_u8(64)), zig_lo_i128(val)); |
| 1500 | | } |
| 1501 | | |
| 1502 | | static inline zig_u128 zig_shlw_u128(zig_u128 lhs, zig_u8 rhs, zig_u8 bits) { |
| 1508 | static inline zig_u128 zig_shlw_u128(zig_u128 lhs, uint8_t rhs, uint8_t bits) { |
| 1503 | 1509 | return zig_wrap_u128(zig_shl_u128(lhs, rhs), bits); |
| 1504 | 1510 | } |
| 1505 | 1511 | |
| 1506 | | static inline zig_i128 zig_shlw_i128(zig_i128 lhs, zig_u8 rhs, zig_u8 bits) { |
| 1512 | static inline zig_i128 zig_shlw_i128(zig_i128 lhs, uint8_t rhs, uint8_t bits) { |
| 1507 | 1513 | return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), rhs)), bits); |
| 1508 | 1514 | } |
| 1509 | 1515 | |
| 1510 | | static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1516 | static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1511 | 1517 | return zig_wrap_u128(zig_add_u128(lhs, rhs), bits); |
| 1512 | 1518 | } |
| 1513 | 1519 | |
| 1514 | | static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1520 | static inline zig_i128 zig_addw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1515 | 1521 | return zig_wrap_i128(zig_bitcast_i128(zig_add_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits); |
| 1516 | 1522 | } |
| 1517 | 1523 | |
| 1518 | | static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1524 | static inline zig_u128 zig_subw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1519 | 1525 | return zig_wrap_u128(zig_sub_u128(lhs, rhs), bits); |
| 1520 | 1526 | } |
| 1521 | 1527 | |
| 1522 | | static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1528 | static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1523 | 1529 | return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits); |
| 1524 | 1530 | } |
| 1525 | 1531 | |
| 1526 | | static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1532 | static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1527 | 1533 | return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits); |
| 1528 | 1534 | } |
| 1529 | 1535 | |
| 1530 | | static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1536 | static inline zig_i128 zig_mulw_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1531 | 1537 | return zig_wrap_i128(zig_bitcast_i128(zig_mul_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits); |
| 1532 | 1538 | } |
| 1533 | 1539 | |
| 1534 | 1540 | #if zig_has_int128 |
| 1535 | 1541 | |
| 1536 | | static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1542 | static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1537 | 1543 | #if zig_has_builtin(add_overflow) |
| 1538 | 1544 | zig_u128 full_res; |
| 1539 | 1545 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1540 | 1546 | *res = zig_wrap_u128(full_res, bits); |
| 1541 | | return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits); |
| 1547 | return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits); |
| 1542 | 1548 | #else |
| 1543 | 1549 | *res = zig_addw_u128(lhs, rhs, bits); |
| 1544 | 1550 | return *res < lhs; |
| 1545 | 1551 | #endif |
| 1546 | 1552 | } |
| 1547 | 1553 | |
| 1548 | | zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow); |
| 1549 | | static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1554 | zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, int *overflow); |
| 1555 | static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1550 | 1556 | #if zig_has_builtin(add_overflow) |
| 1551 | 1557 | zig_i128 full_res; |
| 1552 | 1558 | bool overflow = __builtin_add_overflow(lhs, rhs, &full_res); |
| 1553 | 1559 | #else |
| 1554 | | zig_c_int overflow_int; |
| 1560 | int overflow_int; |
| 1555 | 1561 | zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int); |
| 1556 | 1562 | bool overflow = overflow_int != 0; |
| 1557 | 1563 | #endif |
| 1558 | 1564 | *res = zig_wrap_i128(full_res, bits); |
| 1559 | | return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits); |
| 1565 | return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits); |
| 1560 | 1566 | } |
| 1561 | 1567 | |
| 1562 | | static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1568 | static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1563 | 1569 | #if zig_has_builtin(sub_overflow) |
| 1564 | 1570 | zig_u128 full_res; |
| 1565 | 1571 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1566 | 1572 | *res = zig_wrap_u128(full_res, bits); |
| 1567 | | return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits); |
| 1573 | return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits); |
| 1568 | 1574 | #else |
| 1569 | 1575 | *res = zig_subw_u128(lhs, rhs, bits); |
| 1570 | 1576 | return *res > lhs; |
| 1571 | 1577 | #endif |
| 1572 | 1578 | } |
| 1573 | 1579 | |
| 1574 | | zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow); |
| 1575 | | static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1580 | zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, int *overflow); |
| 1581 | static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1576 | 1582 | #if zig_has_builtin(sub_overflow) |
| 1577 | 1583 | zig_i128 full_res; |
| 1578 | 1584 | bool overflow = __builtin_sub_overflow(lhs, rhs, &full_res); |
| 1579 | 1585 | #else |
| 1580 | | zig_c_int overflow_int; |
| 1586 | int overflow_int; |
| 1581 | 1587 | zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int); |
| 1582 | 1588 | bool overflow = overflow_int != 0; |
| 1583 | 1589 | #endif |
| 1584 | 1590 | *res = zig_wrap_i128(full_res, bits); |
| 1585 | | return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits); |
| 1591 | return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits); |
| 1586 | 1592 | } |
| 1587 | 1593 | |
| 1588 | | static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1594 | static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1589 | 1595 | #if zig_has_builtin(mul_overflow) |
| 1590 | 1596 | zig_u128 full_res; |
| 1591 | 1597 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1592 | 1598 | *res = zig_wrap_u128(full_res, bits); |
| 1593 | | return overflow || full_res < zig_minInt(u128, bits) || full_res > zig_maxInt(u128, bits); |
| 1599 | return overflow || full_res < zig_minInt_u(128, bits) || full_res > zig_maxInt_u(128, bits); |
| 1594 | 1600 | #else |
| 1595 | 1601 | *res = zig_mulw_u128(lhs, rhs, bits); |
| 1596 | | return rhs != zig_as_u128(0, 0) && lhs > zig_maxInt(u128, bits) / rhs; |
| 1602 | return rhs != zig_make_u128(0, 0) && lhs > zig_maxInt_u(128, bits) / rhs; |
| 1597 | 1603 | #endif |
| 1598 | 1604 | } |
| 1599 | 1605 | |
| 1600 | | zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow); |
| 1601 | | static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1606 | zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow); |
| 1607 | static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1602 | 1608 | #if zig_has_builtin(mul_overflow) |
| 1603 | 1609 | zig_i128 full_res; |
| 1604 | 1610 | bool overflow = __builtin_mul_overflow(lhs, rhs, &full_res); |
| 1605 | 1611 | #else |
| 1606 | | zig_c_int overflow_int; |
| 1612 | int overflow_int; |
| 1607 | 1613 | zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int); |
| 1608 | 1614 | bool overflow = overflow_int != 0; |
| 1609 | 1615 | #endif |
| 1610 | 1616 | *res = zig_wrap_i128(full_res, bits); |
| 1611 | | return overflow || full_res < zig_minInt(i128, bits) || full_res > zig_maxInt(i128, bits); |
| 1617 | return overflow || full_res < zig_minInt_i(128, bits) || full_res > zig_maxInt_i(128, bits); |
| 1612 | 1618 | } |
| 1613 | 1619 | |
| 1614 | 1620 | #else /* zig_has_int128 */ |
| 1615 | 1621 | |
| 1616 | | static inline bool zig_overflow_u128(bool overflow, zig_u128 full_res, zig_u8 bits) { |
| 1617 | | return overflow || |
| 1618 | | zig_cmp_u128(full_res, zig_minInt(u128, bits)) < zig_as_i32(0) || |
| 1619 | | zig_cmp_u128(full_res, zig_maxInt(u128, bits)) > zig_as_i32(0); |
| 1622 | static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1623 | uint64_t hi; |
| 1624 | bool overflow = zig_addo_u64(&hi, lhs.hi, rhs.hi, bits - 64); |
| 1625 | return overflow ^ zig_addo_u64(&res->hi, hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64); |
| 1620 | 1626 | } |
| 1621 | 1627 | |
| 1622 | | static inline bool zig_overflow_i128(bool overflow, zig_i128 full_res, zig_u8 bits) { |
| 1623 | | return overflow || |
| 1624 | | zig_cmp_i128(full_res, zig_minInt(i128, bits)) < zig_as_i32(0) || |
| 1625 | | zig_cmp_i128(full_res, zig_maxInt(i128, bits)) > zig_as_i32(0); |
| 1628 | static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1629 | int64_t hi; |
| 1630 | bool overflow = zig_addo_i64(&hi, lhs.hi, rhs.hi, bits - 64); |
| 1631 | return overflow ^ zig_addo_i64(&res->hi, hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64); |
| 1626 | 1632 | } |
| 1627 | 1633 | |
| 1628 | | static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1629 | | zig_u128 full_res; |
| 1630 | | bool overflow = |
| 1631 | | zig_addo_u64(&full_res.hi, lhs.hi, rhs.hi, 64) | |
| 1632 | | zig_addo_u64(&full_res.hi, full_res.hi, zig_addo_u64(&full_res.lo, lhs.lo, rhs.lo, 64), 64); |
| 1633 | | *res = zig_wrap_u128(full_res, bits); |
| 1634 | | return zig_overflow_u128(overflow, full_res, bits); |
| 1634 | static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1635 | uint64_t hi; |
| 1636 | bool overflow = zig_subo_u64(&hi, lhs.hi, rhs.hi, bits - 64); |
| 1637 | return overflow ^ zig_subo_u64(&res->hi, hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64); |
| 1635 | 1638 | } |
| 1636 | 1639 | |
| 1637 | | zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow); |
| 1638 | | static inline bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1639 | | zig_c_int overflow_int; |
| 1640 | | zig_i128 full_res = __addoti4(lhs, rhs, &overflow_int); |
| 1641 | | *res = zig_wrap_i128(full_res, bits); |
| 1642 | | return zig_overflow_i128(overflow_int, full_res, bits); |
| 1640 | static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1641 | int64_t hi; |
| 1642 | bool overflow = zig_subo_i64(&hi, lhs.hi, rhs.hi, bits - 64); |
| 1643 | return overflow ^ zig_subo_i64(&res->hi, hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, 64), bits - 64); |
| 1643 | 1644 | } |
| 1644 | 1645 | |
| 1645 | | static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1646 | | zig_u128 full_res; |
| 1647 | | bool overflow = |
| 1648 | | zig_subo_u64(&full_res.hi, lhs.hi, rhs.hi, 64) | |
| 1649 | | zig_subo_u64(&full_res.hi, full_res.hi, zig_subo_u64(&full_res.lo, lhs.lo, rhs.lo, 64), 64); |
| 1650 | | *res = zig_wrap_u128(full_res, bits); |
| 1651 | | return zig_overflow_u128(overflow, full_res, bits); |
| 1652 | | } |
| 1653 | | |
| 1654 | | zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow); |
| 1655 | | static inline bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1656 | | zig_c_int overflow_int; |
| 1657 | | zig_i128 full_res = __suboti4(lhs, rhs, &overflow_int); |
| 1658 | | *res = zig_wrap_i128(full_res, bits); |
| 1659 | | return zig_overflow_i128(overflow_int, full_res, bits); |
| 1660 | | } |
| 1661 | | |
| 1662 | | static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1646 | static inline bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1663 | 1647 | *res = zig_mulw_u128(lhs, rhs, bits); |
| 1664 | | return zig_cmp_u128(*res, zig_as_u128(0, 0)) != zig_as_i32(0) && |
| 1665 | | zig_cmp_u128(lhs, zig_div_trunc_u128(zig_maxInt(u128, bits), rhs)) > zig_as_i32(0); |
| 1648 | return zig_cmp_u128(*res, zig_make_u128(0, 0)) != INT32_C(0) && |
| 1649 | zig_cmp_u128(lhs, zig_div_trunc_u128(zig_maxInt_u(128, bits), rhs)) > INT32_C(0); |
| 1666 | 1650 | } |
| 1667 | 1651 | |
| 1668 | | zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow); |
| 1669 | | static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1670 | | zig_c_int overflow_int; |
| 1652 | zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, int *overflow); |
| 1653 | static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1654 | int overflow_int; |
| 1671 | 1655 | zig_i128 full_res = __muloti4(lhs, rhs, &overflow_int); |
| 1656 | bool overflow = overflow_int != 0 || |
| 1657 | zig_cmp_i128(full_res, zig_minInt_i(128, bits)) < INT32_C(0) || |
| 1658 | zig_cmp_i128(full_res, zig_maxInt_i(128, bits)) > INT32_C(0); |
| 1672 | 1659 | *res = zig_wrap_i128(full_res, bits); |
| 1673 | | return zig_overflow_i128(overflow_int, full_res, bits); |
| 1660 | return overflow; |
| 1674 | 1661 | } |
| 1675 | 1662 | |
| 1676 | 1663 | #endif /* zig_has_int128 */ |
| 1677 | 1664 | |
| 1678 | | static inline bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, zig_u8 rhs, zig_u8 bits) { |
| 1665 | static inline bool zig_shlo_u128(zig_u128 *res, zig_u128 lhs, uint8_t rhs, uint8_t bits) { |
| 1679 | 1666 | *res = zig_shlw_u128(lhs, rhs, bits); |
| 1680 | | return zig_cmp_u128(lhs, zig_shr_u128(zig_maxInt(u128, bits), rhs)) > zig_as_i32(0); |
| 1667 | return zig_cmp_u128(lhs, zig_shr_u128(zig_maxInt_u(128, bits), rhs)) > INT32_C(0); |
| 1681 | 1668 | } |
| 1682 | 1669 | |
| 1683 | | static inline bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, zig_u8 rhs, zig_u8 bits) { |
| 1670 | static inline bool zig_shlo_i128(zig_i128 *res, zig_i128 lhs, uint8_t rhs, uint8_t bits) { |
| 1684 | 1671 | *res = zig_shlw_i128(lhs, rhs, bits); |
| 1685 | | zig_i128 mask = zig_bitcast_i128(zig_shl_u128(zig_maxInt_u128, bits - rhs - zig_as_u8(1))); |
| 1686 | | return zig_cmp_i128(zig_and_i128(lhs, mask), zig_as_i128(0, 0)) != zig_as_i32(0) && |
| 1687 | | zig_cmp_i128(zig_and_i128(lhs, mask), mask) != zig_as_i32(0); |
| 1672 | zig_i128 mask = zig_bitcast_i128(zig_shl_u128(zig_maxInt_u128, bits - rhs - UINT8_C(1))); |
| 1673 | return zig_cmp_i128(zig_and_i128(lhs, mask), zig_make_i128(0, 0)) != INT32_C(0) && |
| 1674 | zig_cmp_i128(zig_and_i128(lhs, mask), mask) != INT32_C(0); |
| 1688 | 1675 | } |
| 1689 | 1676 | |
| 1690 | | static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1677 | static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1691 | 1678 | zig_u128 res; |
| 1692 | | if (zig_cmp_u128(rhs, zig_as_u128(0, bits)) >= zig_as_i32(0)) |
| 1693 | | return zig_cmp_u128(lhs, zig_as_u128(0, 0)) != zig_as_i32(0) ? zig_maxInt(u128, bits) : lhs; |
| 1694 | | |
| 1695 | | #if zig_has_int128 |
| 1696 | | return zig_shlo_u128(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u128, bits) : res; |
| 1697 | | #else |
| 1698 | | return zig_shlo_u128(&res, lhs, (zig_u8)rhs.lo, bits) ? zig_maxInt(u128, bits) : res; |
| 1699 | | #endif |
| 1679 | if (zig_cmp_u128(rhs, zig_make_u128(0, bits)) >= INT32_C(0)) |
| 1680 | return zig_cmp_u128(lhs, zig_make_u128(0, 0)) != INT32_C(0) ? zig_maxInt_u(128, bits) : lhs; |
| 1681 | return zig_shlo_u128(&res, lhs, (uint8_t)zig_lo_u128(rhs), bits) ? zig_maxInt_u(128, bits) : res; |
| 1700 | 1682 | } |
| 1701 | 1683 | |
| 1702 | | static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1684 | static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1703 | 1685 | zig_i128 res; |
| 1704 | | if (zig_cmp_u128(zig_bitcast_u128(rhs), zig_as_u128(0, bits)) < zig_as_i32(0) && !zig_shlo_i128(&res, lhs, zig_lo_i128(rhs), bits)) return res; |
| 1705 | | return zig_cmp_i128(lhs, zig_as_i128(0, 0)) < zig_as_i32(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits); |
| 1686 | if (zig_cmp_u128(zig_bitcast_u128(rhs), zig_make_u128(0, bits)) < INT32_C(0) && !zig_shlo_i128(&res, lhs, (uint8_t)zig_lo_i128(rhs), bits)) return res; |
| 1687 | return zig_cmp_i128(lhs, zig_make_i128(0, 0)) < INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits); |
| 1706 | 1688 | } |
| 1707 | 1689 | |
| 1708 | | static inline zig_u128 zig_adds_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1690 | static inline zig_u128 zig_adds_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1709 | 1691 | zig_u128 res; |
| 1710 | | return zig_addo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res; |
| 1692 | return zig_addo_u128(&res, lhs, rhs, bits) ? zig_maxInt_u(128, bits) : res; |
| 1711 | 1693 | } |
| 1712 | 1694 | |
| 1713 | | static inline zig_i128 zig_adds_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1695 | static inline zig_i128 zig_adds_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1714 | 1696 | zig_i128 res; |
| 1715 | 1697 | if (!zig_addo_i128(&res, lhs, rhs, bits)) return res; |
| 1716 | | return zig_cmp_i128(res, zig_as_i128(0, 0)) >= zig_as_i32(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits); |
| 1698 | return zig_cmp_i128(res, zig_make_i128(0, 0)) >= INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits); |
| 1717 | 1699 | } |
| 1718 | 1700 | |
| 1719 | | static inline zig_u128 zig_subs_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1701 | static inline zig_u128 zig_subs_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1720 | 1702 | zig_u128 res; |
| 1721 | | return zig_subo_u128(&res, lhs, rhs, bits) ? zig_minInt(u128, bits) : res; |
| 1703 | return zig_subo_u128(&res, lhs, rhs, bits) ? zig_minInt_u(128, bits) : res; |
| 1722 | 1704 | } |
| 1723 | 1705 | |
| 1724 | | static inline zig_i128 zig_subs_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1706 | static inline zig_i128 zig_subs_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1725 | 1707 | zig_i128 res; |
| 1726 | 1708 | if (!zig_subo_i128(&res, lhs, rhs, bits)) return res; |
| 1727 | | return zig_cmp_i128(res, zig_as_i128(0, 0)) >= zig_as_i32(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits); |
| 1709 | return zig_cmp_i128(res, zig_make_i128(0, 0)) >= INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits); |
| 1728 | 1710 | } |
| 1729 | 1711 | |
| 1730 | | static inline zig_u128 zig_muls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) { |
| 1712 | static inline zig_u128 zig_muls_u128(zig_u128 lhs, zig_u128 rhs, uint8_t bits) { |
| 1731 | 1713 | zig_u128 res; |
| 1732 | | return zig_mulo_u128(&res, lhs, rhs, bits) ? zig_maxInt(u128, bits) : res; |
| 1714 | return zig_mulo_u128(&res, lhs, rhs, bits) ? zig_maxInt_u(128, bits) : res; |
| 1733 | 1715 | } |
| 1734 | 1716 | |
| 1735 | | static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) { |
| 1717 | static inline zig_i128 zig_muls_i128(zig_i128 lhs, zig_i128 rhs, uint8_t bits) { |
| 1736 | 1718 | zig_i128 res; |
| 1737 | 1719 | if (!zig_mulo_i128(&res, lhs, rhs, bits)) return res; |
| 1738 | | return zig_cmp_i128(zig_xor_i128(lhs, rhs), zig_as_i128(0, 0)) < zig_as_i32(0) ? zig_minInt(i128, bits) : zig_maxInt(i128, bits); |
| 1720 | return zig_cmp_i128(zig_xor_i128(lhs, rhs), zig_make_i128(0, 0)) < INT32_C(0) ? zig_minInt_i(128, bits) : zig_maxInt_i(128, bits); |
| 1739 | 1721 | } |
| 1740 | 1722 | |
| 1741 | | static inline zig_u8 zig_clz_u128(zig_u128 val, zig_u8 bits) { |
| 1742 | | if (bits <= zig_as_u8(64)) return zig_clz_u64(zig_lo_u128(val), bits); |
| 1743 | | if (zig_hi_u128(val) != 0) return zig_clz_u64(zig_hi_u128(val), bits - zig_as_u8(64)); |
| 1744 | | return zig_clz_u64(zig_lo_u128(val), zig_as_u8(64)) + (bits - zig_as_u8(64)); |
| 1723 | static inline uint8_t zig_clz_u128(zig_u128 val, uint8_t bits) { |
| 1724 | if (bits <= UINT8_C(64)) return zig_clz_u64(zig_lo_u128(val), bits); |
| 1725 | if (zig_hi_u128(val) != 0) return zig_clz_u64(zig_hi_u128(val), bits - UINT8_C(64)); |
| 1726 | return zig_clz_u64(zig_lo_u128(val), UINT8_C(64)) + (bits - UINT8_C(64)); |
| 1745 | 1727 | } |
| 1746 | 1728 | |
| 1747 | | static inline zig_u8 zig_clz_i128(zig_i128 val, zig_u8 bits) { |
| 1729 | static inline uint8_t zig_clz_i128(zig_i128 val, uint8_t bits) { |
| 1748 | 1730 | return zig_clz_u128(zig_bitcast_u128(val), bits); |
| 1749 | 1731 | } |
| 1750 | 1732 | |
| 1751 | | static inline zig_u8 zig_ctz_u128(zig_u128 val, zig_u8 bits) { |
| 1752 | | if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), zig_as_u8(64)); |
| 1753 | | return zig_ctz_u64(zig_hi_u128(val), bits - zig_as_u8(64)) + zig_as_u8(64); |
| 1733 | static inline uint8_t zig_ctz_u128(zig_u128 val, uint8_t bits) { |
| 1734 | if (zig_lo_u128(val) != 0) return zig_ctz_u64(zig_lo_u128(val), UINT8_C(64)); |
| 1735 | return zig_ctz_u64(zig_hi_u128(val), bits - UINT8_C(64)) + UINT8_C(64); |
| 1754 | 1736 | } |
| 1755 | 1737 | |
| 1756 | | static inline zig_u8 zig_ctz_i128(zig_i128 val, zig_u8 bits) { |
| 1738 | static inline uint8_t zig_ctz_i128(zig_i128 val, uint8_t bits) { |
| 1757 | 1739 | return zig_ctz_u128(zig_bitcast_u128(val), bits); |
| 1758 | 1740 | } |
| 1759 | 1741 | |
| 1760 | | static inline zig_u8 zig_popcount_u128(zig_u128 val, zig_u8 bits) { |
| 1761 | | return zig_popcount_u64(zig_hi_u128(val), bits - zig_as_u8(64)) + |
| 1762 | | zig_popcount_u64(zig_lo_u128(val), zig_as_u8(64)); |
| 1742 | static inline uint8_t zig_popcount_u128(zig_u128 val, uint8_t bits) { |
| 1743 | return zig_popcount_u64(zig_hi_u128(val), bits - UINT8_C(64)) + |
| 1744 | zig_popcount_u64(zig_lo_u128(val), UINT8_C(64)); |
| 1763 | 1745 | } |
| 1764 | 1746 | |
| 1765 | | static inline zig_u8 zig_popcount_i128(zig_i128 val, zig_u8 bits) { |
| 1747 | static inline uint8_t zig_popcount_i128(zig_i128 val, uint8_t bits) { |
| 1766 | 1748 | return zig_popcount_u128(zig_bitcast_u128(val), bits); |
| 1767 | 1749 | } |
| 1768 | 1750 | |
| 1769 | | static inline zig_u128 zig_byte_swap_u128(zig_u128 val, zig_u8 bits) { |
| 1751 | static inline zig_u128 zig_byte_swap_u128(zig_u128 val, uint8_t bits) { |
| 1770 | 1752 | zig_u128 full_res; |
| 1771 | 1753 | #if zig_has_builtin(bswap128) |
| 1772 | 1754 | full_res = __builtin_bswap128(val); |
| 1773 | 1755 | #else |
| 1774 | | full_res = zig_as_u128(zig_byte_swap_u64(zig_lo_u128(val), zig_as_u8(64)), |
| 1775 | | zig_byte_swap_u64(zig_hi_u128(val), zig_as_u8(64))); |
| 1756 | full_res = zig_make_u128(zig_byte_swap_u64(zig_lo_u128(val), UINT8_C(64)), |
| 1757 | zig_byte_swap_u64(zig_hi_u128(val), UINT8_C(64))); |
| 1776 | 1758 | #endif |
| 1777 | | return zig_shr_u128(full_res, zig_as_u8(128) - bits); |
| 1759 | return zig_shr_u128(full_res, UINT8_C(128) - bits); |
| 1778 | 1760 | } |
| 1779 | 1761 | |
| 1780 | | static inline zig_i128 zig_byte_swap_i128(zig_i128 val, zig_u8 bits) { |
| 1762 | static inline zig_i128 zig_byte_swap_i128(zig_i128 val, uint8_t bits) { |
| 1781 | 1763 | return zig_bitcast_i128(zig_byte_swap_u128(zig_bitcast_u128(val), bits)); |
| 1782 | 1764 | } |
| 1783 | 1765 | |
| 1784 | | static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, zig_u8 bits) { |
| 1785 | | return zig_shr_u128(zig_as_u128(zig_bit_reverse_u64(zig_lo_u128(val), zig_as_u8(64)), |
| 1786 | | zig_bit_reverse_u64(zig_hi_u128(val), zig_as_u8(64))), |
| 1787 | | zig_as_u8(128) - bits); |
| 1766 | static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, uint8_t bits) { |
| 1767 | return zig_shr_u128(zig_make_u128(zig_bit_reverse_u64(zig_lo_u128(val), UINT8_C(64)), |
| 1768 | zig_bit_reverse_u64(zig_hi_u128(val), UINT8_C(64))), |
| 1769 | UINT8_C(128) - bits); |
| 1788 | 1770 | } |
| 1789 | 1771 | |
| 1790 | | static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) { |
| 1772 | static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, uint8_t bits) { |
| 1791 | 1773 | return zig_bitcast_i128(zig_bit_reverse_u128(zig_bitcast_u128(val), bits)); |
| 1792 | 1774 | } |
| 1793 | 1775 | |
| 1776 | /* ========================== Big Integer Support =========================== */ |
| 1777 | |
| 1778 | static inline uint16_t zig_int_bytes(uint16_t bits) { |
| 1779 | uint16_t bytes = (bits + CHAR_BIT - 1) / CHAR_BIT; |
| 1780 | uint16_t alignment = ZIG_TARGET_MAX_INT_ALIGNMENT; |
| 1781 | while (alignment / 2 >= bytes) alignment /= 2; |
| 1782 | return (bytes + alignment - 1) / alignment * alignment; |
| 1783 | } |
| 1784 | |
| 1785 | static inline int32_t zig_cmp_big(const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 1786 | const uint8_t *lhs_bytes = lhs; |
| 1787 | const uint8_t *rhs_bytes = rhs; |
| 1788 | uint16_t byte_offset = 0; |
| 1789 | bool do_signed = is_signed; |
| 1790 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 1791 | |
| 1792 | #if zig_little_endian |
| 1793 | byte_offset = remaining_bytes; |
| 1794 | #endif |
| 1795 | |
| 1796 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 1797 | int32_t limb_cmp; |
| 1798 | |
| 1799 | #if zig_little_endian |
| 1800 | byte_offset -= 128 / CHAR_BIT; |
| 1801 | #endif |
| 1802 | |
| 1803 | if (do_signed) { |
| 1804 | zig_i128 lhs_limb; |
| 1805 | zig_i128 rhs_limb; |
| 1806 | |
| 1807 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1808 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1809 | limb_cmp = zig_cmp_i128(lhs_limb, rhs_limb); |
| 1810 | do_signed = false; |
| 1811 | } else { |
| 1812 | zig_u128 lhs_limb; |
| 1813 | zig_u128 rhs_limb; |
| 1814 | |
| 1815 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1816 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1817 | limb_cmp = zig_cmp_u128(lhs_limb, rhs_limb); |
| 1818 | } |
| 1819 | |
| 1820 | if (limb_cmp != 0) return limb_cmp; |
| 1821 | remaining_bytes -= 128 / CHAR_BIT; |
| 1822 | |
| 1823 | #if zig_big_endian |
| 1824 | byte_offset += 128 / CHAR_BIT; |
| 1825 | #endif |
| 1826 | } |
| 1827 | |
| 1828 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 1829 | #if zig_little_endian |
| 1830 | byte_offset -= 64 / CHAR_BIT; |
| 1831 | #endif |
| 1832 | |
| 1833 | if (do_signed) { |
| 1834 | int64_t lhs_limb; |
| 1835 | int64_t rhs_limb; |
| 1836 | |
| 1837 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1838 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1839 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 1840 | do_signed = false; |
| 1841 | } else { |
| 1842 | uint64_t lhs_limb; |
| 1843 | uint64_t rhs_limb; |
| 1844 | |
| 1845 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1846 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1847 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 1848 | } |
| 1849 | |
| 1850 | remaining_bytes -= 64 / CHAR_BIT; |
| 1851 | |
| 1852 | #if zig_big_endian |
| 1853 | byte_offset += 64 / CHAR_BIT; |
| 1854 | #endif |
| 1855 | } |
| 1856 | |
| 1857 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 1858 | #if zig_little_endian |
| 1859 | byte_offset -= 32 / CHAR_BIT; |
| 1860 | #endif |
| 1861 | |
| 1862 | if (do_signed) { |
| 1863 | int32_t lhs_limb; |
| 1864 | int32_t rhs_limb; |
| 1865 | |
| 1866 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1867 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1868 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 1869 | do_signed = false; |
| 1870 | } else { |
| 1871 | uint32_t lhs_limb; |
| 1872 | uint32_t rhs_limb; |
| 1873 | |
| 1874 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1875 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1876 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 1877 | } |
| 1878 | |
| 1879 | remaining_bytes -= 32 / CHAR_BIT; |
| 1880 | |
| 1881 | #if zig_big_endian |
| 1882 | byte_offset += 32 / CHAR_BIT; |
| 1883 | #endif |
| 1884 | } |
| 1885 | |
| 1886 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 1887 | #if zig_little_endian |
| 1888 | byte_offset -= 16 / CHAR_BIT; |
| 1889 | #endif |
| 1890 | |
| 1891 | if (do_signed) { |
| 1892 | int16_t lhs_limb; |
| 1893 | int16_t rhs_limb; |
| 1894 | |
| 1895 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1896 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1897 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 1898 | do_signed = false; |
| 1899 | } else { |
| 1900 | uint16_t lhs_limb; |
| 1901 | uint16_t rhs_limb; |
| 1902 | |
| 1903 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1904 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1905 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 1906 | } |
| 1907 | |
| 1908 | remaining_bytes -= 16 / CHAR_BIT; |
| 1909 | |
| 1910 | #if zig_big_endian |
| 1911 | byte_offset += 16 / CHAR_BIT; |
| 1912 | #endif |
| 1913 | } |
| 1914 | |
| 1915 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 1916 | #if zig_little_endian |
| 1917 | byte_offset -= 8 / CHAR_BIT; |
| 1918 | #endif |
| 1919 | |
| 1920 | if (do_signed) { |
| 1921 | int8_t lhs_limb; |
| 1922 | int8_t rhs_limb; |
| 1923 | |
| 1924 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1925 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1926 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 1927 | do_signed = false; |
| 1928 | } else { |
| 1929 | uint8_t lhs_limb; |
| 1930 | uint8_t rhs_limb; |
| 1931 | |
| 1932 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1933 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1934 | if (lhs_limb != rhs_limb) return (lhs_limb > rhs_limb) - (lhs_limb < rhs_limb); |
| 1935 | } |
| 1936 | |
| 1937 | remaining_bytes -= 8 / CHAR_BIT; |
| 1938 | |
| 1939 | #if zig_big_endian |
| 1940 | byte_offset += 8 / CHAR_BIT; |
| 1941 | #endif |
| 1942 | } |
| 1943 | |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
| 1947 | static inline bool zig_addo_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 1948 | uint8_t *res_bytes = res; |
| 1949 | const uint8_t *lhs_bytes = lhs; |
| 1950 | const uint8_t *rhs_bytes = rhs; |
| 1951 | uint16_t byte_offset = 0; |
| 1952 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 1953 | uint16_t top_bits = remaining_bytes * 8 - bits; |
| 1954 | bool overflow = false; |
| 1955 | |
| 1956 | #if zig_big_endian |
| 1957 | byte_offset = remaining_bytes; |
| 1958 | #endif |
| 1959 | |
| 1960 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 1961 | uint16_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0); |
| 1962 | |
| 1963 | #if zig_big_endian |
| 1964 | byte_offset -= 128 / CHAR_BIT; |
| 1965 | #endif |
| 1966 | |
| 1967 | if (remaining_bytes == 128 / CHAR_BIT && is_signed) { |
| 1968 | zig_i128 res_limb; |
| 1969 | zig_i128 tmp_limb; |
| 1970 | zig_i128 lhs_limb; |
| 1971 | zig_i128 rhs_limb; |
| 1972 | bool limb_overflow; |
| 1973 | |
| 1974 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1975 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1976 | limb_overflow = zig_addo_i128(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 1977 | overflow = limb_overflow ^ zig_addo_i128(&res_limb, tmp_limb, zig_make_i128(INT64_C(0), overflow ? UINT64_C(1) : UINT64_C(0)), limb_bits); |
| 1978 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 1979 | } else { |
| 1980 | zig_u128 res_limb; |
| 1981 | zig_u128 tmp_limb; |
| 1982 | zig_u128 lhs_limb; |
| 1983 | zig_u128 rhs_limb; |
| 1984 | bool limb_overflow; |
| 1985 | |
| 1986 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 1987 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 1988 | limb_overflow = zig_addo_u128(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 1989 | overflow = limb_overflow ^ zig_addo_u128(&res_limb, tmp_limb, zig_make_u128(UINT64_C(0), overflow ? UINT64_C(1) : UINT64_C(0)), limb_bits); |
| 1990 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 1991 | } |
| 1992 | |
| 1993 | remaining_bytes -= 128 / CHAR_BIT; |
| 1994 | |
| 1995 | #if zig_little_endian |
| 1996 | byte_offset += 128 / CHAR_BIT; |
| 1997 | #endif |
| 1998 | } |
| 1999 | |
| 2000 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 2001 | uint16_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0); |
| 2002 | |
| 2003 | #if zig_big_endian |
| 2004 | byte_offset -= 64 / CHAR_BIT; |
| 2005 | #endif |
| 2006 | |
| 2007 | if (remaining_bytes == 64 / CHAR_BIT && is_signed) { |
| 2008 | int64_t res_limb; |
| 2009 | int64_t tmp_limb; |
| 2010 | int64_t lhs_limb; |
| 2011 | int64_t rhs_limb; |
| 2012 | bool limb_overflow; |
| 2013 | |
| 2014 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2015 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2016 | limb_overflow = zig_addo_i64(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2017 | overflow = limb_overflow ^ zig_addo_i64(&res_limb, tmp_limb, overflow ? INT64_C(1) : INT64_C(0), limb_bits); |
| 2018 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2019 | } else { |
| 2020 | uint64_t res_limb; |
| 2021 | uint64_t tmp_limb; |
| 2022 | uint64_t lhs_limb; |
| 2023 | uint64_t rhs_limb; |
| 2024 | bool limb_overflow; |
| 2025 | |
| 2026 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2027 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2028 | limb_overflow = zig_addo_u64(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2029 | overflow = limb_overflow ^ zig_addo_u64(&res_limb, tmp_limb, overflow ? UINT64_C(1) : UINT64_C(0), limb_bits); |
| 2030 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2031 | } |
| 2032 | |
| 2033 | remaining_bytes -= 64 / CHAR_BIT; |
| 2034 | |
| 2035 | #if zig_little_endian |
| 2036 | byte_offset += 64 / CHAR_BIT; |
| 2037 | #endif |
| 2038 | } |
| 2039 | |
| 2040 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 2041 | uint16_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0); |
| 2042 | |
| 2043 | #if zig_big_endian |
| 2044 | byte_offset -= 32 / CHAR_BIT; |
| 2045 | #endif |
| 2046 | |
| 2047 | if (remaining_bytes == 32 / CHAR_BIT && is_signed) { |
| 2048 | int32_t res_limb; |
| 2049 | int32_t tmp_limb; |
| 2050 | int32_t lhs_limb; |
| 2051 | int32_t rhs_limb; |
| 2052 | bool limb_overflow; |
| 2053 | |
| 2054 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2055 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2056 | limb_overflow = zig_addo_i32(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2057 | overflow = limb_overflow ^ zig_addo_i32(&res_limb, tmp_limb, overflow ? INT32_C(1) : INT32_C(0), limb_bits); |
| 2058 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2059 | } else { |
| 2060 | uint32_t res_limb; |
| 2061 | uint32_t tmp_limb; |
| 2062 | uint32_t lhs_limb; |
| 2063 | uint32_t rhs_limb; |
| 2064 | bool limb_overflow; |
| 2065 | |
| 2066 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2067 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2068 | limb_overflow = zig_addo_u32(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2069 | overflow = limb_overflow ^ zig_addo_u32(&res_limb, tmp_limb, overflow ? UINT32_C(1) : UINT32_C(0), limb_bits); |
| 2070 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2071 | } |
| 2072 | |
| 2073 | remaining_bytes -= 32 / CHAR_BIT; |
| 2074 | |
| 2075 | #if zig_little_endian |
| 2076 | byte_offset += 32 / CHAR_BIT; |
| 2077 | #endif |
| 2078 | } |
| 2079 | |
| 2080 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 2081 | uint16_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0); |
| 2082 | |
| 2083 | #if zig_big_endian |
| 2084 | byte_offset -= 16 / CHAR_BIT; |
| 2085 | #endif |
| 2086 | |
| 2087 | if (remaining_bytes == 16 / CHAR_BIT && is_signed) { |
| 2088 | int16_t res_limb; |
| 2089 | int16_t tmp_limb; |
| 2090 | int16_t lhs_limb; |
| 2091 | int16_t rhs_limb; |
| 2092 | bool limb_overflow; |
| 2093 | |
| 2094 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2095 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2096 | limb_overflow = zig_addo_i16(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2097 | overflow = limb_overflow ^ zig_addo_i16(&res_limb, tmp_limb, overflow ? INT16_C(1) : INT16_C(0), limb_bits); |
| 2098 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2099 | } else { |
| 2100 | uint16_t res_limb; |
| 2101 | uint16_t tmp_limb; |
| 2102 | uint16_t lhs_limb; |
| 2103 | uint16_t rhs_limb; |
| 2104 | bool limb_overflow; |
| 2105 | |
| 2106 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2107 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2108 | limb_overflow = zig_addo_u16(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2109 | overflow = limb_overflow ^ zig_addo_u16(&res_limb, tmp_limb, overflow ? UINT16_C(1) : UINT16_C(0), limb_bits); |
| 2110 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2111 | } |
| 2112 | |
| 2113 | remaining_bytes -= 16 / CHAR_BIT; |
| 2114 | |
| 2115 | #if zig_little_endian |
| 2116 | byte_offset += 16 / CHAR_BIT; |
| 2117 | #endif |
| 2118 | } |
| 2119 | |
| 2120 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 2121 | uint16_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0); |
| 2122 | |
| 2123 | #if zig_big_endian |
| 2124 | byte_offset -= 8 / CHAR_BIT; |
| 2125 | #endif |
| 2126 | |
| 2127 | if (remaining_bytes == 8 / CHAR_BIT && is_signed) { |
| 2128 | int8_t res_limb; |
| 2129 | int8_t tmp_limb; |
| 2130 | int8_t lhs_limb; |
| 2131 | int8_t rhs_limb; |
| 2132 | bool limb_overflow; |
| 2133 | |
| 2134 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2135 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2136 | limb_overflow = zig_addo_i8(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2137 | overflow = limb_overflow ^ zig_addo_i8(&res_limb, tmp_limb, overflow ? INT8_C(1) : INT8_C(0), limb_bits); |
| 2138 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2139 | } else { |
| 2140 | uint8_t res_limb; |
| 2141 | uint8_t tmp_limb; |
| 2142 | uint8_t lhs_limb; |
| 2143 | uint8_t rhs_limb; |
| 2144 | bool limb_overflow; |
| 2145 | |
| 2146 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2147 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2148 | limb_overflow = zig_addo_u8(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2149 | overflow = limb_overflow ^ zig_addo_u8(&res_limb, tmp_limb, overflow ? UINT8_C(1) : UINT8_C(0), limb_bits); |
| 2150 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2151 | } |
| 2152 | |
| 2153 | remaining_bytes -= 8 / CHAR_BIT; |
| 2154 | |
| 2155 | #if zig_little_endian |
| 2156 | byte_offset += 8 / CHAR_BIT; |
| 2157 | #endif |
| 2158 | } |
| 2159 | |
| 2160 | return overflow; |
| 2161 | } |
| 2162 | |
| 2163 | static inline bool zig_subo_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 2164 | uint8_t *res_bytes = res; |
| 2165 | const uint8_t *lhs_bytes = lhs; |
| 2166 | const uint8_t *rhs_bytes = rhs; |
| 2167 | uint16_t byte_offset = 0; |
| 2168 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 2169 | uint16_t top_bits = remaining_bytes * 8 - bits; |
| 2170 | bool overflow = false; |
| 2171 | |
| 2172 | #if zig_big_endian |
| 2173 | byte_offset = remaining_bytes; |
| 2174 | #endif |
| 2175 | |
| 2176 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 2177 | uint16_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0); |
| 2178 | |
| 2179 | #if zig_big_endian |
| 2180 | byte_offset -= 128 / CHAR_BIT; |
| 2181 | #endif |
| 2182 | |
| 2183 | if (remaining_bytes == 128 / CHAR_BIT && is_signed) { |
| 2184 | zig_i128 res_limb; |
| 2185 | zig_i128 tmp_limb; |
| 2186 | zig_i128 lhs_limb; |
| 2187 | zig_i128 rhs_limb; |
| 2188 | bool limb_overflow; |
| 2189 | |
| 2190 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2191 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2192 | limb_overflow = zig_subo_i128(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2193 | overflow = limb_overflow ^ zig_subo_i128(&res_limb, tmp_limb, zig_make_i128(INT64_C(0), overflow ? UINT64_C(1) : UINT64_C(0)), limb_bits); |
| 2194 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2195 | } else { |
| 2196 | zig_u128 res_limb; |
| 2197 | zig_u128 tmp_limb; |
| 2198 | zig_u128 lhs_limb; |
| 2199 | zig_u128 rhs_limb; |
| 2200 | bool limb_overflow; |
| 2201 | |
| 2202 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2203 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2204 | limb_overflow = zig_subo_u128(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2205 | overflow = limb_overflow ^ zig_subo_u128(&res_limb, tmp_limb, zig_make_u128(UINT64_C(0), overflow ? UINT64_C(1) : UINT64_C(0)), limb_bits); |
| 2206 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2207 | } |
| 2208 | |
| 2209 | remaining_bytes -= 128 / CHAR_BIT; |
| 2210 | |
| 2211 | #if zig_little_endian |
| 2212 | byte_offset += 128 / CHAR_BIT; |
| 2213 | #endif |
| 2214 | } |
| 2215 | |
| 2216 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 2217 | uint16_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0); |
| 2218 | |
| 2219 | #if zig_big_endian |
| 2220 | byte_offset -= 64 / CHAR_BIT; |
| 2221 | #endif |
| 2222 | |
| 2223 | if (remaining_bytes == 64 / CHAR_BIT && is_signed) { |
| 2224 | int64_t res_limb; |
| 2225 | int64_t tmp_limb; |
| 2226 | int64_t lhs_limb; |
| 2227 | int64_t rhs_limb; |
| 2228 | bool limb_overflow; |
| 2229 | |
| 2230 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2231 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2232 | limb_overflow = zig_subo_i64(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2233 | overflow = limb_overflow ^ zig_subo_i64(&res_limb, tmp_limb, overflow ? INT64_C(1) : INT64_C(0), limb_bits); |
| 2234 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2235 | } else { |
| 2236 | uint64_t res_limb; |
| 2237 | uint64_t tmp_limb; |
| 2238 | uint64_t lhs_limb; |
| 2239 | uint64_t rhs_limb; |
| 2240 | bool limb_overflow; |
| 2241 | |
| 2242 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2243 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2244 | limb_overflow = zig_subo_u64(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2245 | overflow = limb_overflow ^ zig_subo_u64(&res_limb, tmp_limb, overflow ? UINT64_C(1) : UINT64_C(0), limb_bits); |
| 2246 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2247 | } |
| 2248 | |
| 2249 | remaining_bytes -= 64 / CHAR_BIT; |
| 2250 | |
| 2251 | #if zig_little_endian |
| 2252 | byte_offset += 64 / CHAR_BIT; |
| 2253 | #endif |
| 2254 | } |
| 2255 | |
| 2256 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 2257 | uint16_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0); |
| 2258 | |
| 2259 | #if zig_big_endian |
| 2260 | byte_offset -= 32 / CHAR_BIT; |
| 2261 | #endif |
| 2262 | |
| 2263 | if (remaining_bytes == 32 / CHAR_BIT && is_signed) { |
| 2264 | int32_t res_limb; |
| 2265 | int32_t tmp_limb; |
| 2266 | int32_t lhs_limb; |
| 2267 | int32_t rhs_limb; |
| 2268 | bool limb_overflow; |
| 2269 | |
| 2270 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2271 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2272 | limb_overflow = zig_subo_i32(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2273 | overflow = limb_overflow ^ zig_subo_i32(&res_limb, tmp_limb, overflow ? INT32_C(1) : INT32_C(0), limb_bits); |
| 2274 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2275 | } else { |
| 2276 | uint32_t res_limb; |
| 2277 | uint32_t tmp_limb; |
| 2278 | uint32_t lhs_limb; |
| 2279 | uint32_t rhs_limb; |
| 2280 | bool limb_overflow; |
| 2281 | |
| 2282 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2283 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2284 | limb_overflow = zig_subo_u32(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2285 | overflow = limb_overflow ^ zig_subo_u32(&res_limb, tmp_limb, overflow ? UINT32_C(1) : UINT32_C(0), limb_bits); |
| 2286 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2287 | } |
| 2288 | |
| 2289 | remaining_bytes -= 32 / CHAR_BIT; |
| 2290 | |
| 2291 | #if zig_little_endian |
| 2292 | byte_offset += 32 / CHAR_BIT; |
| 2293 | #endif |
| 2294 | } |
| 2295 | |
| 2296 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 2297 | uint16_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0); |
| 2298 | |
| 2299 | #if zig_big_endian |
| 2300 | byte_offset -= 16 / CHAR_BIT; |
| 2301 | #endif |
| 2302 | |
| 2303 | if (remaining_bytes == 16 / CHAR_BIT && is_signed) { |
| 2304 | int16_t res_limb; |
| 2305 | int16_t tmp_limb; |
| 2306 | int16_t lhs_limb; |
| 2307 | int16_t rhs_limb; |
| 2308 | bool limb_overflow; |
| 2309 | |
| 2310 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2311 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2312 | limb_overflow = zig_subo_i16(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2313 | overflow = limb_overflow ^ zig_subo_i16(&res_limb, tmp_limb, overflow ? INT16_C(1) : INT16_C(0), limb_bits); |
| 2314 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2315 | } else { |
| 2316 | uint16_t res_limb; |
| 2317 | uint16_t tmp_limb; |
| 2318 | uint16_t lhs_limb; |
| 2319 | uint16_t rhs_limb; |
| 2320 | bool limb_overflow; |
| 2321 | |
| 2322 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2323 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2324 | limb_overflow = zig_subo_u16(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2325 | overflow = limb_overflow ^ zig_subo_u16(&res_limb, tmp_limb, overflow ? UINT16_C(1) : UINT16_C(0), limb_bits); |
| 2326 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2327 | } |
| 2328 | |
| 2329 | remaining_bytes -= 16 / CHAR_BIT; |
| 2330 | |
| 2331 | #if zig_little_endian |
| 2332 | byte_offset += 16 / CHAR_BIT; |
| 2333 | #endif |
| 2334 | } |
| 2335 | |
| 2336 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 2337 | uint16_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0); |
| 2338 | |
| 2339 | #if zig_big_endian |
| 2340 | byte_offset -= 8 / CHAR_BIT; |
| 2341 | #endif |
| 2342 | |
| 2343 | if (remaining_bytes == 8 / CHAR_BIT && is_signed) { |
| 2344 | int8_t res_limb; |
| 2345 | int8_t tmp_limb; |
| 2346 | int8_t lhs_limb; |
| 2347 | int8_t rhs_limb; |
| 2348 | bool limb_overflow; |
| 2349 | |
| 2350 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2351 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2352 | limb_overflow = zig_subo_i8(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2353 | overflow = limb_overflow ^ zig_subo_i8(&res_limb, tmp_limb, overflow ? INT8_C(1) : INT8_C(0), limb_bits); |
| 2354 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2355 | } else { |
| 2356 | uint8_t res_limb; |
| 2357 | uint8_t tmp_limb; |
| 2358 | uint8_t lhs_limb; |
| 2359 | uint8_t rhs_limb; |
| 2360 | bool limb_overflow; |
| 2361 | |
| 2362 | memcpy(&lhs_limb, &lhs_bytes[byte_offset], sizeof(lhs_limb)); |
| 2363 | memcpy(&rhs_limb, &rhs_bytes[byte_offset], sizeof(rhs_limb)); |
| 2364 | limb_overflow = zig_subo_u8(&tmp_limb, lhs_limb, rhs_limb, limb_bits); |
| 2365 | overflow = limb_overflow ^ zig_subo_u8(&res_limb, tmp_limb, overflow ? UINT8_C(1) : UINT8_C(0), limb_bits); |
| 2366 | memcpy(&res_bytes[byte_offset], &res_limb, sizeof(res_limb)); |
| 2367 | } |
| 2368 | |
| 2369 | remaining_bytes -= 8 / CHAR_BIT; |
| 2370 | |
| 2371 | #if zig_little_endian |
| 2372 | byte_offset += 8 / CHAR_BIT; |
| 2373 | #endif |
| 2374 | } |
| 2375 | |
| 2376 | return overflow; |
| 2377 | } |
| 2378 | |
| 2379 | static inline void zig_addw_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 2380 | (void)zig_addo_big(res, lhs, rhs, is_signed, bits); |
| 2381 | } |
| 2382 | |
| 2383 | static inline void zig_subw_big(void *res, const void *lhs, const void *rhs, bool is_signed, uint16_t bits) { |
| 2384 | (void)zig_subo_big(res, lhs, rhs, is_signed, bits); |
| 2385 | } |
| 2386 | |
| 2387 | static inline uint16_t zig_clz_big(const void *val, bool is_signed, uint16_t bits) { |
| 2388 | const uint8_t *val_bytes = val; |
| 2389 | uint16_t byte_offset = 0; |
| 2390 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 2391 | uint16_t skip_bits = remaining_bytes * 8 - bits; |
| 2392 | uint16_t total_lz = 0; |
| 2393 | uint16_t limb_lz; |
| 2394 | (void)is_signed; |
| 2395 | |
| 2396 | #if zig_little_endian |
| 2397 | byte_offset = remaining_bytes; |
| 2398 | #endif |
| 2399 | |
| 2400 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 2401 | #if zig_little_endian |
| 2402 | byte_offset -= 128 / CHAR_BIT; |
| 2403 | #endif |
| 2404 | |
| 2405 | { |
| 2406 | zig_u128 val_limb; |
| 2407 | |
| 2408 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2409 | limb_lz = zig_clz_u128(val_limb, 128 - skip_bits); |
| 2410 | } |
| 2411 | |
| 2412 | total_lz += limb_lz; |
| 2413 | if (limb_lz < 128 - skip_bits) return total_lz; |
| 2414 | skip_bits = 0; |
| 2415 | remaining_bytes -= 128 / CHAR_BIT; |
| 2416 | |
| 2417 | #if zig_big_endian |
| 2418 | byte_offset += 128 / CHAR_BIT; |
| 2419 | #endif |
| 2420 | } |
| 2421 | |
| 2422 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 2423 | #if zig_little_endian |
| 2424 | byte_offset -= 64 / CHAR_BIT; |
| 2425 | #endif |
| 2426 | |
| 2427 | { |
| 2428 | uint64_t val_limb; |
| 2429 | |
| 2430 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2431 | limb_lz = zig_clz_u64(val_limb, 64 - skip_bits); |
| 2432 | } |
| 2433 | |
| 2434 | total_lz += limb_lz; |
| 2435 | if (limb_lz < 64 - skip_bits) return total_lz; |
| 2436 | skip_bits = 0; |
| 2437 | remaining_bytes -= 64 / CHAR_BIT; |
| 2438 | |
| 2439 | #if zig_big_endian |
| 2440 | byte_offset += 64 / CHAR_BIT; |
| 2441 | #endif |
| 2442 | } |
| 2443 | |
| 2444 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 2445 | #if zig_little_endian |
| 2446 | byte_offset -= 32 / CHAR_BIT; |
| 2447 | #endif |
| 2448 | |
| 2449 | { |
| 2450 | uint32_t val_limb; |
| 2451 | |
| 2452 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2453 | limb_lz = zig_clz_u32(val_limb, 32 - skip_bits); |
| 2454 | } |
| 2455 | |
| 2456 | total_lz += limb_lz; |
| 2457 | if (limb_lz < 32 - skip_bits) return total_lz; |
| 2458 | skip_bits = 0; |
| 2459 | remaining_bytes -= 32 / CHAR_BIT; |
| 2460 | |
| 2461 | #if zig_big_endian |
| 2462 | byte_offset += 32 / CHAR_BIT; |
| 2463 | #endif |
| 2464 | } |
| 2465 | |
| 2466 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 2467 | #if zig_little_endian |
| 2468 | byte_offset -= 16 / CHAR_BIT; |
| 2469 | #endif |
| 2470 | |
| 2471 | { |
| 2472 | uint16_t val_limb; |
| 2473 | |
| 2474 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2475 | limb_lz = zig_clz_u16(val_limb, 16 - skip_bits); |
| 2476 | } |
| 2477 | |
| 2478 | total_lz += limb_lz; |
| 2479 | if (limb_lz < 16 - skip_bits) return total_lz; |
| 2480 | skip_bits = 0; |
| 2481 | remaining_bytes -= 16 / CHAR_BIT; |
| 2482 | |
| 2483 | #if zig_big_endian |
| 2484 | byte_offset += 16 / CHAR_BIT; |
| 2485 | #endif |
| 2486 | } |
| 2487 | |
| 2488 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 2489 | #if zig_little_endian |
| 2490 | byte_offset -= 8 / CHAR_BIT; |
| 2491 | #endif |
| 2492 | |
| 2493 | { |
| 2494 | uint8_t val_limb; |
| 2495 | |
| 2496 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2497 | limb_lz = zig_clz_u8(val_limb, 8 - skip_bits); |
| 2498 | } |
| 2499 | |
| 2500 | total_lz += limb_lz; |
| 2501 | if (limb_lz < 8 - skip_bits) return total_lz; |
| 2502 | skip_bits = 0; |
| 2503 | remaining_bytes -= 8 / CHAR_BIT; |
| 2504 | |
| 2505 | #if zig_big_endian |
| 2506 | byte_offset += 8 / CHAR_BIT; |
| 2507 | #endif |
| 2508 | } |
| 2509 | |
| 2510 | return total_lz; |
| 2511 | } |
| 2512 | |
| 2513 | static inline uint16_t zig_ctz_big(const void *val, bool is_signed, uint16_t bits) { |
| 2514 | const uint8_t *val_bytes = val; |
| 2515 | uint16_t byte_offset = 0; |
| 2516 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 2517 | uint16_t total_tz = 0; |
| 2518 | uint16_t limb_tz; |
| 2519 | (void)is_signed; |
| 2520 | |
| 2521 | #if zig_big_endian |
| 2522 | byte_offset = remaining_bytes; |
| 2523 | #endif |
| 2524 | |
| 2525 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 2526 | #if zig_big_endian |
| 2527 | byte_offset -= 128 / CHAR_BIT; |
| 2528 | #endif |
| 2529 | |
| 2530 | { |
| 2531 | zig_u128 val_limb; |
| 2532 | |
| 2533 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2534 | limb_tz = zig_ctz_u128(val_limb, 128); |
| 2535 | } |
| 2536 | |
| 2537 | total_tz += limb_tz; |
| 2538 | if (limb_tz < 128) return total_tz; |
| 2539 | remaining_bytes -= 128 / CHAR_BIT; |
| 2540 | |
| 2541 | #if zig_little_endian |
| 2542 | byte_offset += 128 / CHAR_BIT; |
| 2543 | #endif |
| 2544 | } |
| 2545 | |
| 2546 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 2547 | #if zig_big_endian |
| 2548 | byte_offset -= 64 / CHAR_BIT; |
| 2549 | #endif |
| 2550 | |
| 2551 | { |
| 2552 | uint64_t val_limb; |
| 2553 | |
| 2554 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2555 | limb_tz = zig_ctz_u64(val_limb, 64); |
| 2556 | } |
| 2557 | |
| 2558 | total_tz += limb_tz; |
| 2559 | if (limb_tz < 64) return total_tz; |
| 2560 | remaining_bytes -= 64 / CHAR_BIT; |
| 2561 | |
| 2562 | #if zig_little_endian |
| 2563 | byte_offset += 64 / CHAR_BIT; |
| 2564 | #endif |
| 2565 | } |
| 2566 | |
| 2567 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 2568 | #if zig_big_endian |
| 2569 | byte_offset -= 32 / CHAR_BIT; |
| 2570 | #endif |
| 2571 | |
| 2572 | { |
| 2573 | uint32_t val_limb; |
| 2574 | |
| 2575 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2576 | limb_tz = zig_ctz_u32(val_limb, 32); |
| 2577 | } |
| 2578 | |
| 2579 | total_tz += limb_tz; |
| 2580 | if (limb_tz < 32) return total_tz; |
| 2581 | remaining_bytes -= 32 / CHAR_BIT; |
| 2582 | |
| 2583 | #if zig_little_endian |
| 2584 | byte_offset += 32 / CHAR_BIT; |
| 2585 | #endif |
| 2586 | } |
| 2587 | |
| 2588 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 2589 | #if zig_big_endian |
| 2590 | byte_offset -= 16 / CHAR_BIT; |
| 2591 | #endif |
| 2592 | |
| 2593 | { |
| 2594 | uint16_t val_limb; |
| 2595 | |
| 2596 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2597 | limb_tz = zig_ctz_u16(val_limb, 16); |
| 2598 | } |
| 2599 | |
| 2600 | total_tz += limb_tz; |
| 2601 | if (limb_tz < 16) return total_tz; |
| 2602 | remaining_bytes -= 16 / CHAR_BIT; |
| 2603 | |
| 2604 | #if zig_little_endian |
| 2605 | byte_offset += 16 / CHAR_BIT; |
| 2606 | #endif |
| 2607 | } |
| 2608 | |
| 2609 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 2610 | #if zig_big_endian |
| 2611 | byte_offset -= 8 / CHAR_BIT; |
| 2612 | #endif |
| 2613 | |
| 2614 | { |
| 2615 | uint8_t val_limb; |
| 2616 | |
| 2617 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2618 | limb_tz = zig_ctz_u8(val_limb, 8); |
| 2619 | } |
| 2620 | |
| 2621 | total_tz += limb_tz; |
| 2622 | if (limb_tz < 8) return total_tz; |
| 2623 | remaining_bytes -= 8 / CHAR_BIT; |
| 2624 | |
| 2625 | #if zig_little_endian |
| 2626 | byte_offset += 8 / CHAR_BIT; |
| 2627 | #endif |
| 2628 | } |
| 2629 | |
| 2630 | return total_tz; |
| 2631 | } |
| 2632 | |
| 2633 | static inline uint16_t zig_popcount_big(const void *val, bool is_signed, uint16_t bits) { |
| 2634 | const uint8_t *val_bytes = val; |
| 2635 | uint16_t byte_offset = 0; |
| 2636 | uint16_t remaining_bytes = zig_int_bytes(bits); |
| 2637 | uint16_t total_pc = 0; |
| 2638 | (void)is_signed; |
| 2639 | |
| 2640 | #if zig_big_endian |
| 2641 | byte_offset = remaining_bytes; |
| 2642 | #endif |
| 2643 | |
| 2644 | while (remaining_bytes >= 128 / CHAR_BIT) { |
| 2645 | #if zig_big_endian |
| 2646 | byte_offset -= 128 / CHAR_BIT; |
| 2647 | #endif |
| 2648 | |
| 2649 | { |
| 2650 | zig_u128 val_limb; |
| 2651 | |
| 2652 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2653 | total_pc += zig_popcount_u128(val_limb, 128); |
| 2654 | } |
| 2655 | |
| 2656 | remaining_bytes -= 128 / CHAR_BIT; |
| 2657 | |
| 2658 | #if zig_little_endian |
| 2659 | byte_offset += 128 / CHAR_BIT; |
| 2660 | #endif |
| 2661 | } |
| 2662 | |
| 2663 | while (remaining_bytes >= 64 / CHAR_BIT) { |
| 2664 | #if zig_big_endian |
| 2665 | byte_offset -= 64 / CHAR_BIT; |
| 2666 | #endif |
| 2667 | |
| 2668 | { |
| 2669 | uint64_t val_limb; |
| 2670 | |
| 2671 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2672 | total_pc += zig_popcount_u64(val_limb, 64); |
| 2673 | } |
| 2674 | |
| 2675 | remaining_bytes -= 64 / CHAR_BIT; |
| 2676 | |
| 2677 | #if zig_little_endian |
| 2678 | byte_offset += 64 / CHAR_BIT; |
| 2679 | #endif |
| 2680 | } |
| 2681 | |
| 2682 | while (remaining_bytes >= 32 / CHAR_BIT) { |
| 2683 | #if zig_big_endian |
| 2684 | byte_offset -= 32 / CHAR_BIT; |
| 2685 | #endif |
| 2686 | |
| 2687 | { |
| 2688 | uint32_t val_limb; |
| 2689 | |
| 2690 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2691 | total_pc += zig_popcount_u32(val_limb, 32); |
| 2692 | } |
| 2693 | |
| 2694 | remaining_bytes -= 32 / CHAR_BIT; |
| 2695 | |
| 2696 | #if zig_little_endian |
| 2697 | byte_offset += 32 / CHAR_BIT; |
| 2698 | #endif |
| 2699 | } |
| 2700 | |
| 2701 | while (remaining_bytes >= 16 / CHAR_BIT) { |
| 2702 | #if zig_big_endian |
| 2703 | byte_offset -= 16 / CHAR_BIT; |
| 2704 | #endif |
| 2705 | |
| 2706 | { |
| 2707 | uint16_t val_limb; |
| 2708 | |
| 2709 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2710 | total_pc = zig_popcount_u16(val_limb, 16); |
| 2711 | } |
| 2712 | |
| 2713 | remaining_bytes -= 16 / CHAR_BIT; |
| 2714 | |
| 2715 | #if zig_little_endian |
| 2716 | byte_offset += 16 / CHAR_BIT; |
| 2717 | #endif |
| 2718 | } |
| 2719 | |
| 2720 | while (remaining_bytes >= 8 / CHAR_BIT) { |
| 2721 | #if zig_big_endian |
| 2722 | byte_offset -= 8 / CHAR_BIT; |
| 2723 | #endif |
| 2724 | |
| 2725 | { |
| 2726 | uint8_t val_limb; |
| 2727 | |
| 2728 | memcpy(&val_limb, &val_bytes[byte_offset], sizeof(val_limb)); |
| 2729 | total_pc = zig_popcount_u8(val_limb, 8); |
| 2730 | } |
| 2731 | |
| 2732 | remaining_bytes -= 8 / CHAR_BIT; |
| 2733 | |
| 2734 | #if zig_little_endian |
| 2735 | byte_offset += 8 / CHAR_BIT; |
| 2736 | #endif |
| 2737 | } |
| 2738 | |
| 2739 | return total_pc; |
| 2740 | } |
| 2741 | |
| 1794 | 2742 | /* ========================= Floating Point Support ========================= */ |
| 1795 | 2743 | |
| 1796 | 2744 | #if _MSC_VER |
| ... | ... | @@ -1810,252 +2758,253 @@ static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) { |
| 1810 | 2758 | |
| 1811 | 2759 | #if (zig_has_builtin(nan) && zig_has_builtin(nans) && zig_has_builtin(inf)) || defined(zig_gnuc) |
| 1812 | 2760 | #define zig_has_float_builtins 1 |
| 1813 | | #define zig_as_special_f16(sign, name, arg, repr) sign zig_as_f16(__builtin_##name, )(arg) |
| 1814 | | #define zig_as_special_f32(sign, name, arg, repr) sign zig_as_f32(__builtin_##name, )(arg) |
| 1815 | | #define zig_as_special_f64(sign, name, arg, repr) sign zig_as_f64(__builtin_##name, )(arg) |
| 1816 | | #define zig_as_special_f80(sign, name, arg, repr) sign zig_as_f80(__builtin_##name, )(arg) |
| 1817 | | #define zig_as_special_f128(sign, name, arg, repr) sign zig_as_f128(__builtin_##name, )(arg) |
| 1818 | | #define zig_as_special_c_longdouble(sign, name, arg, repr) sign zig_as_c_longdouble(__builtin_##name, )(arg) |
| 2761 | #define zig_make_special_f16(sign, name, arg, repr) sign zig_make_f16(__builtin_##name, )(arg) |
| 2762 | #define zig_make_special_f32(sign, name, arg, repr) sign zig_make_f32(__builtin_##name, )(arg) |
| 2763 | #define zig_make_special_f64(sign, name, arg, repr) sign zig_make_f64(__builtin_##name, )(arg) |
| 2764 | #define zig_make_special_f80(sign, name, arg, repr) sign zig_make_f80(__builtin_##name, )(arg) |
| 2765 | #define zig_make_special_f128(sign, name, arg, repr) sign zig_make_f128(__builtin_##name, )(arg) |
| 1819 | 2766 | #else |
| 1820 | 2767 | #define zig_has_float_builtins 0 |
| 1821 | | #define zig_as_special_f16(sign, name, arg, repr) zig_float_from_repr_f16(repr) |
| 1822 | | #define zig_as_special_f32(sign, name, arg, repr) zig_float_from_repr_f32(repr) |
| 1823 | | #define zig_as_special_f64(sign, name, arg, repr) zig_float_from_repr_f64(repr) |
| 1824 | | #define zig_as_special_f80(sign, name, arg, repr) zig_float_from_repr_f80(repr) |
| 1825 | | #define zig_as_special_f128(sign, name, arg, repr) zig_float_from_repr_f128(repr) |
| 1826 | | #define zig_as_special_c_longdouble(sign, name, arg, repr) zig_float_from_repr_c_longdouble(repr) |
| 2768 | #define zig_make_special_f16(sign, name, arg, repr) zig_float_from_repr_f16(repr) |
| 2769 | #define zig_make_special_f32(sign, name, arg, repr) zig_float_from_repr_f32(repr) |
| 2770 | #define zig_make_special_f64(sign, name, arg, repr) zig_float_from_repr_f64(repr) |
| 2771 | #define zig_make_special_f80(sign, name, arg, repr) zig_float_from_repr_f80(repr) |
| 2772 | #define zig_make_special_f128(sign, name, arg, repr) zig_float_from_repr_f128(repr) |
| 1827 | 2773 | #endif |
| 1828 | 2774 | |
| 1829 | 2775 | #define zig_has_f16 1 |
| 1830 | 2776 | #define zig_bitSizeOf_f16 16 |
| 2777 | typedef int16_t zig_repr_f16; |
| 1831 | 2778 | #define zig_libc_name_f16(name) __##name##h |
| 1832 | | #define zig_as_special_constant_f16(sign, name, arg, repr) zig_as_special_f16(sign, name, arg, repr) |
| 2779 | #define zig_init_special_f16(sign, name, arg, repr) zig_make_special_f16(sign, name, arg, repr) |
| 1833 | 2780 | #if FLT_MANT_DIG == 11 |
| 1834 | 2781 | typedef float zig_f16; |
| 1835 | | #define zig_as_f16(fp, repr) fp##f |
| 2782 | #define zig_make_f16(fp, repr) fp##f |
| 1836 | 2783 | #elif DBL_MANT_DIG == 11 |
| 1837 | 2784 | typedef double zig_f16; |
| 1838 | | #define zig_as_f16(fp, repr) fp |
| 2785 | #define zig_make_f16(fp, repr) fp |
| 1839 | 2786 | #elif LDBL_MANT_DIG == 11 |
| 1840 | 2787 | #define zig_bitSizeOf_c_longdouble 16 |
| 2788 | #ifndef ZIG_TARGET_ABI_MSVC |
| 2789 | typedef zig_repr_f16 zig_repr_c_longdouble; |
| 2790 | #endif |
| 1841 | 2791 | typedef long double zig_f16; |
| 1842 | | #define zig_as_f16(fp, repr) fp##l |
| 2792 | #define zig_make_f16(fp, repr) fp##l |
| 1843 | 2793 | #elif FLT16_MANT_DIG == 11 && (zig_has_builtin(inff16) || defined(zig_gnuc)) |
| 1844 | 2794 | typedef _Float16 zig_f16; |
| 1845 | | #define zig_as_f16(fp, repr) fp##f16 |
| 2795 | #define zig_make_f16(fp, repr) fp##f16 |
| 1846 | 2796 | #elif defined(__SIZEOF_FP16__) |
| 1847 | 2797 | typedef __fp16 zig_f16; |
| 1848 | | #define zig_as_f16(fp, repr) fp##f16 |
| 2798 | #define zig_make_f16(fp, repr) fp##f16 |
| 1849 | 2799 | #else |
| 1850 | 2800 | #undef zig_has_f16 |
| 1851 | 2801 | #define zig_has_f16 0 |
| 1852 | | #define zig_repr_f16 i16 |
| 1853 | | typedef zig_i16 zig_f16; |
| 1854 | | #define zig_as_f16(fp, repr) repr |
| 1855 | | #undef zig_as_special_f16 |
| 1856 | | #define zig_as_special_f16(sign, name, arg, repr) repr |
| 1857 | | #undef zig_as_special_constant_f16 |
| 1858 | | #define zig_as_special_constant_f16(sign, name, arg, repr) repr |
| 2802 | #define zig_bitSizeOf_repr_f16 16 |
| 2803 | typedef int16_t zig_f16; |
| 2804 | #define zig_make_f16(fp, repr) repr |
| 2805 | #undef zig_make_special_f16 |
| 2806 | #define zig_make_special_f16(sign, name, arg, repr) repr |
| 2807 | #undef zig_init_special_f16 |
| 2808 | #define zig_init_special_f16(sign, name, arg, repr) repr |
| 1859 | 2809 | #endif |
| 1860 | 2810 | |
| 1861 | 2811 | #define zig_has_f32 1 |
| 1862 | 2812 | #define zig_bitSizeOf_f32 32 |
| 2813 | typedef int32_t zig_repr_f32; |
| 1863 | 2814 | #define zig_libc_name_f32(name) name##f |
| 1864 | 2815 | #if _MSC_VER |
| 1865 | | #define zig_as_special_constant_f32(sign, name, arg, repr) sign zig_as_f32(zig_msvc_flt_##name, ) |
| 2816 | #define zig_init_special_f32(sign, name, arg, repr) sign zig_make_f32(zig_msvc_flt_##name, ) |
| 1866 | 2817 | #else |
| 1867 | | #define zig_as_special_constant_f32(sign, name, arg, repr) zig_as_special_f32(sign, name, arg, repr) |
| 2818 | #define zig_init_special_f32(sign, name, arg, repr) zig_make_special_f32(sign, name, arg, repr) |
| 1868 | 2819 | #endif |
| 1869 | 2820 | #if FLT_MANT_DIG == 24 |
| 1870 | 2821 | typedef float zig_f32; |
| 1871 | | #define zig_as_f32(fp, repr) fp##f |
| 2822 | #define zig_make_f32(fp, repr) fp##f |
| 1872 | 2823 | #elif DBL_MANT_DIG == 24 |
| 1873 | 2824 | typedef double zig_f32; |
| 1874 | | #define zig_as_f32(fp, repr) fp |
| 2825 | #define zig_make_f32(fp, repr) fp |
| 1875 | 2826 | #elif LDBL_MANT_DIG == 24 |
| 1876 | 2827 | #define zig_bitSizeOf_c_longdouble 32 |
| 2828 | #ifndef ZIG_TARGET_ABI_MSVC |
| 2829 | typedef zig_repr_f32 zig_repr_c_longdouble; |
| 2830 | #endif |
| 1877 | 2831 | typedef long double zig_f32; |
| 1878 | | #define zig_as_f32(fp, repr) fp##l |
| 2832 | #define zig_make_f32(fp, repr) fp##l |
| 1879 | 2833 | #elif FLT32_MANT_DIG == 24 |
| 1880 | 2834 | typedef _Float32 zig_f32; |
| 1881 | | #define zig_as_f32(fp, repr) fp##f32 |
| 2835 | #define zig_make_f32(fp, repr) fp##f32 |
| 1882 | 2836 | #else |
| 1883 | 2837 | #undef zig_has_f32 |
| 1884 | 2838 | #define zig_has_f32 0 |
| 1885 | | #define zig_repr_f32 i32 |
| 1886 | | typedef zig_i32 zig_f32; |
| 1887 | | #define zig_as_f32(fp, repr) repr |
| 1888 | | #undef zig_as_special_f32 |
| 1889 | | #define zig_as_special_f32(sign, name, arg, repr) repr |
| 1890 | | #undef zig_as_special_constant_f32 |
| 1891 | | #define zig_as_special_constant_f32(sign, name, arg, repr) repr |
| 2839 | #define zig_bitSizeOf_repr_f32 32 |
| 2840 | typedef int32_t zig_f32; |
| 2841 | #define zig_make_f32(fp, repr) repr |
| 2842 | #undef zig_make_special_f32 |
| 2843 | #define zig_make_special_f32(sign, name, arg, repr) repr |
| 2844 | #undef zig_init_special_f32 |
| 2845 | #define zig_init_special_f32(sign, name, arg, repr) repr |
| 1892 | 2846 | #endif |
| 1893 | 2847 | |
| 1894 | 2848 | #define zig_has_f64 1 |
| 1895 | 2849 | #define zig_bitSizeOf_f64 64 |
| 2850 | typedef int64_t zig_repr_f64; |
| 1896 | 2851 | #define zig_libc_name_f64(name) name |
| 1897 | 2852 | #if _MSC_VER |
| 1898 | 2853 | #ifdef ZIG_TARGET_ABI_MSVC |
| 1899 | 2854 | #define zig_bitSizeOf_c_longdouble 64 |
| 2855 | #ifndef ZIG_TARGET_ABI_MSVC |
| 2856 | typedef zig_repr_f64 zig_repr_c_longdouble; |
| 1900 | 2857 | #endif |
| 1901 | | #define zig_as_special_constant_f64(sign, name, arg, repr) sign zig_as_f64(zig_msvc_flt_##name, ) |
| 2858 | #endif |
| 2859 | #define zig_init_special_f64(sign, name, arg, repr) sign zig_make_f64(zig_msvc_flt_##name, ) |
| 1902 | 2860 | #else /* _MSC_VER */ |
| 1903 | | #define zig_as_special_constant_f64(sign, name, arg, repr) zig_as_special_f64(sign, name, arg, repr) |
| 2861 | #define zig_init_special_f64(sign, name, arg, repr) zig_make_special_f64(sign, name, arg, repr) |
| 1904 | 2862 | #endif /* _MSC_VER */ |
| 1905 | 2863 | #if FLT_MANT_DIG == 53 |
| 1906 | 2864 | typedef float zig_f64; |
| 1907 | | #define zig_as_f64(fp, repr) fp##f |
| 2865 | #define zig_make_f64(fp, repr) fp##f |
| 1908 | 2866 | #elif DBL_MANT_DIG == 53 |
| 1909 | 2867 | typedef double zig_f64; |
| 1910 | | #define zig_as_f64(fp, repr) fp |
| 2868 | #define zig_make_f64(fp, repr) fp |
| 1911 | 2869 | #elif LDBL_MANT_DIG == 53 |
| 1912 | 2870 | #define zig_bitSizeOf_c_longdouble 64 |
| 2871 | #ifndef ZIG_TARGET_ABI_MSVC |
| 2872 | typedef zig_repr_f64 zig_repr_c_longdouble; |
| 2873 | #endif |
| 1913 | 2874 | typedef long double zig_f64; |
| 1914 | | #define zig_as_f64(fp, repr) fp##l |
| 2875 | #define zig_make_f64(fp, repr) fp##l |
| 1915 | 2876 | #elif FLT64_MANT_DIG == 53 |
| 1916 | 2877 | typedef _Float64 zig_f64; |
| 1917 | | #define zig_as_f64(fp, repr) fp##f64 |
| 2878 | #define zig_make_f64(fp, repr) fp##f64 |
| 1918 | 2879 | #elif FLT32X_MANT_DIG == 53 |
| 1919 | 2880 | typedef _Float32x zig_f64; |
| 1920 | | #define zig_as_f64(fp, repr) fp##f32x |
| 2881 | #define zig_make_f64(fp, repr) fp##f32x |
| 1921 | 2882 | #else |
| 1922 | 2883 | #undef zig_has_f64 |
| 1923 | 2884 | #define zig_has_f64 0 |
| 1924 | | #define zig_repr_f64 i64 |
| 1925 | | typedef zig_i64 zig_f64; |
| 1926 | | #define zig_as_f64(fp, repr) repr |
| 1927 | | #undef zig_as_special_f64 |
| 1928 | | #define zig_as_special_f64(sign, name, arg, repr) repr |
| 1929 | | #undef zig_as_special_constant_f64 |
| 1930 | | #define zig_as_special_constant_f64(sign, name, arg, repr) repr |
| 2885 | #define zig_bitSizeOf_repr_f64 64 |
| 2886 | typedef int64_t zig_f64; |
| 2887 | #define zig_make_f64(fp, repr) repr |
| 2888 | #undef zig_make_special_f64 |
| 2889 | #define zig_make_special_f64(sign, name, arg, repr) repr |
| 2890 | #undef zig_init_special_f64 |
| 2891 | #define zig_init_special_f64(sign, name, arg, repr) repr |
| 1931 | 2892 | #endif |
| 1932 | 2893 | |
| 1933 | 2894 | #define zig_has_f80 1 |
| 1934 | 2895 | #define zig_bitSizeOf_f80 80 |
| 2896 | typedef zig_i128 zig_repr_f80; |
| 1935 | 2897 | #define zig_libc_name_f80(name) __##name##x |
| 1936 | | #define zig_as_special_constant_f80(sign, name, arg, repr) zig_as_special_f80(sign, name, arg, repr) |
| 2898 | #define zig_init_special_f80(sign, name, arg, repr) zig_make_special_f80(sign, name, arg, repr) |
| 1937 | 2899 | #if FLT_MANT_DIG == 64 |
| 1938 | 2900 | typedef float zig_f80; |
| 1939 | | #define zig_as_f80(fp, repr) fp##f |
| 2901 | #define zig_make_f80(fp, repr) fp##f |
| 1940 | 2902 | #elif DBL_MANT_DIG == 64 |
| 1941 | 2903 | typedef double zig_f80; |
| 1942 | | #define zig_as_f80(fp, repr) fp |
| 2904 | #define zig_make_f80(fp, repr) fp |
| 1943 | 2905 | #elif LDBL_MANT_DIG == 64 |
| 1944 | 2906 | #define zig_bitSizeOf_c_longdouble 80 |
| 2907 | #ifndef ZIG_TARGET_ABI_MSVC |
| 2908 | typedef zig_repr_f80 zig_repr_c_longdouble; |
| 2909 | #endif |
| 1945 | 2910 | typedef long double zig_f80; |
| 1946 | | #define zig_as_f80(fp, repr) fp##l |
| 2911 | #define zig_make_f80(fp, repr) fp##l |
| 1947 | 2912 | #elif FLT80_MANT_DIG == 64 |
| 1948 | 2913 | typedef _Float80 zig_f80; |
| 1949 | | #define zig_as_f80(fp, repr) fp##f80 |
| 2914 | #define zig_make_f80(fp, repr) fp##f80 |
| 1950 | 2915 | #elif FLT64X_MANT_DIG == 64 |
| 1951 | 2916 | typedef _Float64x zig_f80; |
| 1952 | | #define zig_as_f80(fp, repr) fp##f64x |
| 2917 | #define zig_make_f80(fp, repr) fp##f64x |
| 1953 | 2918 | #elif defined(__SIZEOF_FLOAT80__) |
| 1954 | 2919 | typedef __float80 zig_f80; |
| 1955 | | #define zig_as_f80(fp, repr) fp##l |
| 2920 | #define zig_make_f80(fp, repr) fp##l |
| 1956 | 2921 | #else |
| 1957 | 2922 | #undef zig_has_f80 |
| 1958 | 2923 | #define zig_has_f80 0 |
| 1959 | | #define zig_repr_f80 i128 |
| 2924 | #define zig_bitSizeOf_repr_f80 128 |
| 1960 | 2925 | typedef zig_i128 zig_f80; |
| 1961 | | #define zig_as_f80(fp, repr) repr |
| 1962 | | #undef zig_as_special_f80 |
| 1963 | | #define zig_as_special_f80(sign, name, arg, repr) repr |
| 1964 | | #undef zig_as_special_constant_f80 |
| 1965 | | #define zig_as_special_constant_f80(sign, name, arg, repr) repr |
| 2926 | #define zig_make_f80(fp, repr) repr |
| 2927 | #undef zig_make_special_f80 |
| 2928 | #define zig_make_special_f80(sign, name, arg, repr) repr |
| 2929 | #undef zig_init_special_f80 |
| 2930 | #define zig_init_special_f80(sign, name, arg, repr) repr |
| 1966 | 2931 | #endif |
| 1967 | 2932 | |
| 1968 | 2933 | #define zig_has_f128 1 |
| 1969 | 2934 | #define zig_bitSizeOf_f128 128 |
| 2935 | typedef zig_i128 zig_repr_f128; |
| 1970 | 2936 | #define zig_libc_name_f128(name) name##q |
| 1971 | | #define zig_as_special_constant_f128(sign, name, arg, repr) zig_as_special_f128(sign, name, arg, repr) |
| 2937 | #define zig_init_special_f128(sign, name, arg, repr) zig_make_special_f128(sign, name, arg, repr) |
| 1972 | 2938 | #if FLT_MANT_DIG == 113 |
| 1973 | 2939 | typedef float zig_f128; |
| 1974 | | #define zig_as_f128(fp, repr) fp##f |
| 2940 | #define zig_make_f128(fp, repr) fp##f |
| 1975 | 2941 | #elif DBL_MANT_DIG == 113 |
| 1976 | 2942 | typedef double zig_f128; |
| 1977 | | #define zig_as_f128(fp, repr) fp |
| 2943 | #define zig_make_f128(fp, repr) fp |
| 1978 | 2944 | #elif LDBL_MANT_DIG == 113 |
| 1979 | 2945 | #define zig_bitSizeOf_c_longdouble 128 |
| 2946 | #ifndef ZIG_TARGET_ABI_MSVC |
| 2947 | typedef zig_repr_f128 zig_repr_c_longdouble; |
| 2948 | #endif |
| 1980 | 2949 | typedef long double zig_f128; |
| 1981 | | #define zig_as_f128(fp, repr) fp##l |
| 2950 | #define zig_make_f128(fp, repr) fp##l |
| 1982 | 2951 | #elif FLT128_MANT_DIG == 113 |
| 1983 | 2952 | typedef _Float128 zig_f128; |
| 1984 | | #define zig_as_f128(fp, repr) fp##f128 |
| 2953 | #define zig_make_f128(fp, repr) fp##f128 |
| 1985 | 2954 | #elif FLT64X_MANT_DIG == 113 |
| 1986 | 2955 | typedef _Float64x zig_f128; |
| 1987 | | #define zig_as_f128(fp, repr) fp##f64x |
| 2956 | #define zig_make_f128(fp, repr) fp##f64x |
| 1988 | 2957 | #elif defined(__SIZEOF_FLOAT128__) |
| 1989 | 2958 | typedef __float128 zig_f128; |
| 1990 | | #define zig_as_f128(fp, repr) fp##q |
| 1991 | | #undef zig_as_special_f128 |
| 1992 | | #define zig_as_special_f128(sign, name, arg, repr) sign __builtin_##name##f128(arg) |
| 2959 | #define zig_make_f128(fp, repr) fp##q |
| 2960 | #undef zig_make_special_f128 |
| 2961 | #define zig_make_special_f128(sign, name, arg, repr) sign __builtin_##name##f128(arg) |
| 1993 | 2962 | #else |
| 1994 | 2963 | #undef zig_has_f128 |
| 1995 | 2964 | #define zig_has_f128 0 |
| 1996 | | #define zig_repr_f128 i128 |
| 2965 | #define zig_bitSizeOf_repr_f128 128 |
| 1997 | 2966 | typedef zig_i128 zig_f128; |
| 1998 | | #define zig_as_f128(fp, repr) repr |
| 1999 | | #undef zig_as_special_f128 |
| 2000 | | #define zig_as_special_f128(sign, name, arg, repr) repr |
| 2001 | | #undef zig_as_special_constant_f128 |
| 2002 | | #define zig_as_special_constant_f128(sign, name, arg, repr) repr |
| 2003 | | #endif |
| 2004 | | |
| 2005 | | #define zig_has_c_longdouble 1 |
| 2006 | | |
| 2007 | | #ifdef ZIG_TARGET_ABI_MSVC |
| 2008 | | #define zig_libc_name_c_longdouble(name) name |
| 2009 | | #else |
| 2010 | | #define zig_libc_name_c_longdouble(name) name##l |
| 2967 | #define zig_make_f128(fp, repr) repr |
| 2968 | #undef zig_make_special_f128 |
| 2969 | #define zig_make_special_f128(sign, name, arg, repr) repr |
| 2970 | #undef zig_init_special_f128 |
| 2971 | #define zig_init_special_f128(sign, name, arg, repr) repr |
| 2011 | 2972 | #endif |
| 2012 | 2973 | |
| 2013 | | #define zig_as_special_constant_c_longdouble(sign, name, arg, repr) zig_as_special_c_longdouble(sign, name, arg, repr) |
| 2014 | 2974 | #ifdef zig_bitSizeOf_c_longdouble |
| 2015 | 2975 | |
| 2976 | #define zig_has_c_longdouble 1 |
| 2016 | 2977 | #ifdef ZIG_TARGET_ABI_MSVC |
| 2017 | | typedef double zig_c_longdouble; |
| 2018 | 2978 | #undef zig_bitSizeOf_c_longdouble |
| 2019 | 2979 | #define zig_bitSizeOf_c_longdouble 64 |
| 2020 | | #define zig_as_c_longdouble(fp, repr) fp |
| 2980 | typedef zig_f64 zig_c_longdouble; |
| 2981 | typedef zig_repr_f64 zig_repr_c_longdouble; |
| 2021 | 2982 | #else |
| 2022 | 2983 | typedef long double zig_c_longdouble; |
| 2023 | | #define zig_as_c_longdouble(fp, repr) fp##l |
| 2024 | 2984 | #endif |
| 2025 | 2985 | |
| 2026 | 2986 | #else /* zig_bitSizeOf_c_longdouble */ |
| 2027 | 2987 | |
| 2028 | | #undef zig_has_c_longdouble |
| 2029 | 2988 | #define zig_has_c_longdouble 0 |
| 2030 | | #define zig_bitSizeOf_c_longdouble 80 |
| 2031 | | #define zig_compiler_rt_abbrev_c_longdouble zig_compiler_rt_abbrev_f80 |
| 2032 | | #define zig_repr_c_longdouble i128 |
| 2033 | | typedef zig_i128 zig_c_longdouble; |
| 2034 | | #define zig_as_c_longdouble(fp, repr) repr |
| 2035 | | #undef zig_as_special_c_longdouble |
| 2036 | | #define zig_as_special_c_longdouble(sign, name, arg, repr) repr |
| 2037 | | #undef zig_as_special_constant_c_longdouble |
| 2038 | | #define zig_as_special_constant_c_longdouble(sign, name, arg, repr) repr |
| 2989 | #define zig_bitSizeOf_repr_c_longdouble 128 |
| 2990 | typedef zig_f128 zig_c_longdouble; |
| 2991 | typedef zig_repr_f128 zig_repr_c_longdouble; |
| 2039 | 2992 | |
| 2040 | 2993 | #endif /* zig_bitSizeOf_c_longdouble */ |
| 2041 | 2994 | |
| 2042 | 2995 | #if !zig_has_float_builtins |
| 2043 | | #define zig_float_from_repr(Type, ReprType) \ |
| 2044 | | static inline zig_##Type zig_float_from_repr_##Type(zig_##ReprType repr) { \ |
| 2045 | | return *((zig_##Type*)&repr); \ |
| 2996 | #define zig_float_from_repr(Type) \ |
| 2997 | static inline zig_##Type zig_float_from_repr_##Type(zig_repr_##Type repr) { \ |
| 2998 | zig_##Type result; \ |
| 2999 | memcpy(&result, &repr, sizeof(result)); \ |
| 3000 | return result; \ |
| 2046 | 3001 | } |
| 2047 | 3002 | |
| 2048 | | zig_float_from_repr(f16, u16) |
| 2049 | | zig_float_from_repr(f32, u32) |
| 2050 | | zig_float_from_repr(f64, u64) |
| 2051 | | zig_float_from_repr(f80, u128) |
| 2052 | | zig_float_from_repr(f128, u128) |
| 2053 | | #if zig_bitSizeOf_c_longdouble == 80 |
| 2054 | | zig_float_from_repr(c_longdouble, u128) |
| 2055 | | #else |
| 2056 | | #define zig_expand_float_from_repr(Type, ReprType) zig_float_from_repr(Type, ReprType) |
| 2057 | | zig_expand_float_from_repr(c_longdouble, zig_expand_concat(u, zig_bitSizeOf_c_longdouble)) |
| 2058 | | #endif |
| 3003 | zig_float_from_repr(f16) |
| 3004 | zig_float_from_repr(f32) |
| 3005 | zig_float_from_repr(f64) |
| 3006 | zig_float_from_repr(f80) |
| 3007 | zig_float_from_repr(f128) |
| 2059 | 3008 | #endif |
| 2060 | 3009 | |
| 2061 | 3010 | #define zig_cast_f16 (zig_f16) |
| ... | ... | @@ -2064,41 +3013,42 @@ zig_expand_float_from_repr(c_longdouble, zig_expand_concat(u, zig_bitSizeOf_c_lo |
| 2064 | 3013 | |
| 2065 | 3014 | #if _MSC_VER && !zig_has_f128 |
| 2066 | 3015 | #define zig_cast_f80 |
| 2067 | | #define zig_cast_c_longdouble |
| 2068 | 3016 | #define zig_cast_f128 |
| 2069 | 3017 | #else |
| 2070 | 3018 | #define zig_cast_f80 (zig_f80) |
| 2071 | | #define zig_cast_c_longdouble (zig_c_longdouble) |
| 2072 | 3019 | #define zig_cast_f128 (zig_f128) |
| 2073 | 3020 | #endif |
| 2074 | 3021 | |
| 2075 | 3022 | #define zig_convert_builtin(ResType, operation, ArgType, version) \ |
| 2076 | | zig_extern zig_##ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \ |
| 2077 | | zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(zig_##ArgType); |
| 2078 | | zig_convert_builtin(f16, trunc, f32, 2) |
| 2079 | | zig_convert_builtin(f16, trunc, f64, 2) |
| 2080 | | zig_convert_builtin(f16, trunc, f80, 2) |
| 2081 | | zig_convert_builtin(f16, trunc, f128, 2) |
| 2082 | | zig_convert_builtin(f32, extend, f16, 2) |
| 2083 | | zig_convert_builtin(f32, trunc, f64, 2) |
| 2084 | | zig_convert_builtin(f32, trunc, f80, 2) |
| 2085 | | zig_convert_builtin(f32, trunc, f128, 2) |
| 2086 | | zig_convert_builtin(f64, extend, f16, 2) |
| 2087 | | zig_convert_builtin(f64, extend, f32, 2) |
| 2088 | | zig_convert_builtin(f64, trunc, f80, 2) |
| 2089 | | zig_convert_builtin(f64, trunc, f128, 2) |
| 2090 | | zig_convert_builtin(f80, extend, f16, 2) |
| 2091 | | zig_convert_builtin(f80, extend, f32, 2) |
| 2092 | | zig_convert_builtin(f80, extend, f64, 2) |
| 2093 | | zig_convert_builtin(f80, trunc, f128, 2) |
| 2094 | | zig_convert_builtin(f128, extend, f16, 2) |
| 2095 | | zig_convert_builtin(f128, extend, f32, 2) |
| 2096 | | zig_convert_builtin(f128, extend, f64, 2) |
| 2097 | | zig_convert_builtin(f128, extend, f80, 2) |
| 3023 | zig_extern ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \ |
| 3024 | zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(ArgType); |
| 3025 | zig_convert_builtin(zig_f16, trunc, zig_f32, 2) |
| 3026 | zig_convert_builtin(zig_f16, trunc, zig_f64, 2) |
| 3027 | zig_convert_builtin(zig_f16, trunc, zig_f80, 2) |
| 3028 | zig_convert_builtin(zig_f16, trunc, zig_f128, 2) |
| 3029 | zig_convert_builtin(zig_f32, extend, zig_f16, 2) |
| 3030 | zig_convert_builtin(zig_f32, trunc, zig_f64, 2) |
| 3031 | zig_convert_builtin(zig_f32, trunc, zig_f80, 2) |
| 3032 | zig_convert_builtin(zig_f32, trunc, zig_f128, 2) |
| 3033 | zig_convert_builtin(zig_f64, extend, zig_f16, 2) |
| 3034 | zig_convert_builtin(zig_f64, extend, zig_f32, 2) |
| 3035 | zig_convert_builtin(zig_f64, trunc, zig_f80, 2) |
| 3036 | zig_convert_builtin(zig_f64, trunc, zig_f128, 2) |
| 3037 | zig_convert_builtin(zig_f80, extend, zig_f16, 2) |
| 3038 | zig_convert_builtin(zig_f80, extend, zig_f32, 2) |
| 3039 | zig_convert_builtin(zig_f80, extend, zig_f64, 2) |
| 3040 | zig_convert_builtin(zig_f80, trunc, zig_f128, 2) |
| 3041 | zig_convert_builtin(zig_f128, extend, zig_f16, 2) |
| 3042 | zig_convert_builtin(zig_f128, extend, zig_f32, 2) |
| 3043 | zig_convert_builtin(zig_f128, extend, zig_f64, 2) |
| 3044 | zig_convert_builtin(zig_f128, extend, zig_f80, 2) |
| 2098 | 3045 | |
| 2099 | 3046 | #define zig_float_negate_builtin_0(Type) \ |
| 2100 | 3047 | static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \ |
| 2101 | | return zig_expand_concat(zig_xor_, zig_repr_##Type)(arg, zig_expand_minInt(zig_repr_##Type, zig_bitSizeOf_##Type)); \ |
| 3048 | return zig_expand_concat(zig_xor_i, zig_bitSizeOf_repr_##Type)( \ |
| 3049 | arg, \ |
| 3050 | zig_minInt_i(zig_bitSizeOf_repr_##Type, zig_bitSizeOf_##Type) \ |
| 3051 | ); \ |
| 2102 | 3052 | } |
| 2103 | 3053 | #define zig_float_negate_builtin_1(Type) \ |
| 2104 | 3054 | static inline zig_##Type zig_neg_##Type(zig_##Type arg) { \ |
| ... | ... | @@ -2106,28 +3056,28 @@ zig_convert_builtin(f128, extend, f80, 2) |
| 2106 | 3056 | } |
| 2107 | 3057 | |
| 2108 | 3058 | #define zig_float_less_builtin_0(Type, operation) \ |
| 2109 | | zig_extern zig_i32 zig_expand_concat(zig_expand_concat(__##operation, \ |
| 2110 | | zig_compiler_rt_abbrev_##Type), 2)(zig_##Type, zig_##Type); \ |
| 2111 | | static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 2112 | | return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_##Type), 2)(lhs, rhs); \ |
| 3059 | zig_extern int32_t zig_expand_concat(zig_expand_concat(__##operation, \ |
| 3060 | zig_compiler_rt_abbrev_zig_##Type), 2)(zig_##Type, zig_##Type); \ |
| 3061 | static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 3062 | return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_zig_##Type), 2)(lhs, rhs); \ |
| 2113 | 3063 | } |
| 2114 | 3064 | #define zig_float_less_builtin_1(Type, operation) \ |
| 2115 | | static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 3065 | static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 2116 | 3066 | return (!(lhs <= rhs) - (lhs < rhs)); \ |
| 2117 | 3067 | } |
| 2118 | 3068 | |
| 2119 | 3069 | #define zig_float_greater_builtin_0(Type, operation) \ |
| 2120 | 3070 | zig_float_less_builtin_0(Type, operation) |
| 2121 | 3071 | #define zig_float_greater_builtin_1(Type, operation) \ |
| 2122 | | static inline zig_i32 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 3072 | static inline int32_t zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 2123 | 3073 | return ((lhs > rhs) - !(lhs >= rhs)); \ |
| 2124 | 3074 | } |
| 2125 | 3075 | |
| 2126 | 3076 | #define zig_float_binary_builtin_0(Type, operation, operator) \ |
| 2127 | 3077 | zig_extern zig_##Type zig_expand_concat(zig_expand_concat(__##operation, \ |
| 2128 | | zig_compiler_rt_abbrev_##Type), 3)(zig_##Type, zig_##Type); \ |
| 3078 | zig_compiler_rt_abbrev_zig_##Type), 3)(zig_##Type, zig_##Type); \ |
| 2129 | 3079 | static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| 2130 | | return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_##Type), 3)(lhs, rhs); \ |
| 3080 | return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_zig_##Type), 3)(lhs, rhs); \ |
| 2131 | 3081 | } |
| 2132 | 3082 | #define zig_float_binary_builtin_1(Type, operation, operator) \ |
| 2133 | 3083 | static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \ |
| ... | ... | @@ -2135,18 +3085,18 @@ zig_convert_builtin(f128, extend, f80, 2) |
| 2135 | 3085 | } |
| 2136 | 3086 | |
| 2137 | 3087 | #define zig_float_builtins(Type) \ |
| 2138 | | zig_convert_builtin(i32, fix, Type, ) \ |
| 2139 | | zig_convert_builtin(u32, fixuns, Type, ) \ |
| 2140 | | zig_convert_builtin(i64, fix, Type, ) \ |
| 2141 | | zig_convert_builtin(u64, fixuns, Type, ) \ |
| 2142 | | zig_convert_builtin(i128, fix, Type, ) \ |
| 2143 | | zig_convert_builtin(u128, fixuns, Type, ) \ |
| 2144 | | zig_convert_builtin(Type, float, i32, ) \ |
| 2145 | | zig_convert_builtin(Type, floatun, u32, ) \ |
| 2146 | | zig_convert_builtin(Type, float, i64, ) \ |
| 2147 | | zig_convert_builtin(Type, floatun, u64, ) \ |
| 2148 | | zig_convert_builtin(Type, float, i128, ) \ |
| 2149 | | zig_convert_builtin(Type, floatun, u128, ) \ |
| 3088 | zig_convert_builtin( int32_t, fix, zig_##Type, ) \ |
| 3089 | zig_convert_builtin(uint32_t, fixuns, zig_##Type, ) \ |
| 3090 | zig_convert_builtin( int64_t, fix, zig_##Type, ) \ |
| 3091 | zig_convert_builtin(uint64_t, fixuns, zig_##Type, ) \ |
| 3092 | zig_convert_builtin(zig_i128, fix, zig_##Type, ) \ |
| 3093 | zig_convert_builtin(zig_u128, fixuns, zig_##Type, ) \ |
| 3094 | zig_convert_builtin(zig_##Type, float, int32_t, ) \ |
| 3095 | zig_convert_builtin(zig_##Type, floatun, uint32_t, ) \ |
| 3096 | zig_convert_builtin(zig_##Type, float, int64_t, ) \ |
| 3097 | zig_convert_builtin(zig_##Type, floatun, uint64_t, ) \ |
| 3098 | zig_convert_builtin(zig_##Type, float, zig_i128, ) \ |
| 3099 | zig_convert_builtin(zig_##Type, floatun, zig_u128, ) \ |
| 2150 | 3100 | zig_expand_concat(zig_float_negate_builtin_, zig_has_##Type)(Type) \ |
| 2151 | 3101 | zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, cmp) \ |
| 2152 | 3102 | zig_expand_concat(zig_float_less_builtin_, zig_has_##Type)(Type, ne) \ |
| ... | ... | @@ -2194,155 +3144,162 @@ zig_float_builtins(f32) |
| 2194 | 3144 | zig_float_builtins(f64) |
| 2195 | 3145 | zig_float_builtins(f80) |
| 2196 | 3146 | zig_float_builtins(f128) |
| 2197 | | zig_float_builtins(c_longdouble) |
| 2198 | 3147 | |
| 2199 | 3148 | #if _MSC_VER && (_M_IX86 || _M_X64) |
| 2200 | 3149 | |
| 2201 | 3150 | // TODO: zig_msvc_atomic_load should load 32 bit without interlocked on x86, and load 64 bit without interlocked on x64 |
| 2202 | 3151 | |
| 2203 | | #define zig_msvc_atomics(Type, suffix) \ |
| 2204 | | static inline bool zig_msvc_cmpxchg_##Type(zig_##Type volatile* obj, zig_##Type* expected, zig_##Type desired) { \ |
| 2205 | | zig_##Type comparand = *expected; \ |
| 2206 | | zig_##Type initial = _InterlockedCompareExchange##suffix(obj, desired, comparand); \ |
| 3152 | #define zig_msvc_atomics(ZigType, Type, suffix) \ |
| 3153 | static inline bool zig_msvc_cmpxchg_##ZigType(Type volatile* obj, Type* expected, Type desired) { \ |
| 3154 | Type comparand = *expected; \ |
| 3155 | Type initial = _InterlockedCompareExchange##suffix(obj, desired, comparand); \ |
| 2207 | 3156 | bool exchanged = initial == comparand; \ |
| 2208 | 3157 | if (!exchanged) { \ |
| 2209 | 3158 | *expected = initial; \ |
| 2210 | 3159 | } \ |
| 2211 | 3160 | return exchanged; \ |
| 2212 | 3161 | } \ |
| 2213 | | static inline zig_##Type zig_msvc_atomicrmw_xchg_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 3162 | static inline Type zig_msvc_atomicrmw_xchg_##ZigType(Type volatile* obj, Type value) { \ |
| 2214 | 3163 | return _InterlockedExchange##suffix(obj, value); \ |
| 2215 | 3164 | } \ |
| 2216 | | static inline zig_##Type zig_msvc_atomicrmw_add_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 3165 | static inline Type zig_msvc_atomicrmw_add_##ZigType(Type volatile* obj, Type value) { \ |
| 2217 | 3166 | return _InterlockedExchangeAdd##suffix(obj, value); \ |
| 2218 | 3167 | } \ |
| 2219 | | static inline zig_##Type zig_msvc_atomicrmw_sub_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 3168 | static inline Type zig_msvc_atomicrmw_sub_##ZigType(Type volatile* obj, Type value) { \ |
| 2220 | 3169 | bool success = false; \ |
| 2221 | | zig_##Type new; \ |
| 2222 | | zig_##Type prev; \ |
| 3170 | Type new; \ |
| 3171 | Type prev; \ |
| 2223 | 3172 | while (!success) { \ |
| 2224 | 3173 | prev = *obj; \ |
| 2225 | 3174 | new = prev - value; \ |
| 2226 | | success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \ |
| 3175 | success = zig_msvc_cmpxchg_##ZigType(obj, &prev, new); \ |
| 2227 | 3176 | } \ |
| 2228 | 3177 | return prev; \ |
| 2229 | 3178 | } \ |
| 2230 | | static inline zig_##Type zig_msvc_atomicrmw_or_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 3179 | static inline Type zig_msvc_atomicrmw_or_##ZigType(Type volatile* obj, Type value) { \ |
| 2231 | 3180 | return _InterlockedOr##suffix(obj, value); \ |
| 2232 | 3181 | } \ |
| 2233 | | static inline zig_##Type zig_msvc_atomicrmw_xor_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 3182 | static inline Type zig_msvc_atomicrmw_xor_##ZigType(Type volatile* obj, Type value) { \ |
| 2234 | 3183 | return _InterlockedXor##suffix(obj, value); \ |
| 2235 | 3184 | } \ |
| 2236 | | static inline zig_##Type zig_msvc_atomicrmw_and_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 3185 | static inline Type zig_msvc_atomicrmw_and_##ZigType(Type volatile* obj, Type value) { \ |
| 2237 | 3186 | return _InterlockedAnd##suffix(obj, value); \ |
| 2238 | 3187 | } \ |
| 2239 | | static inline zig_##Type zig_msvc_atomicrmw_nand_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 3188 | static inline Type zig_msvc_atomicrmw_nand_##ZigType(Type volatile* obj, Type value) { \ |
| 2240 | 3189 | bool success = false; \ |
| 2241 | | zig_##Type new; \ |
| 2242 | | zig_##Type prev; \ |
| 3190 | Type new; \ |
| 3191 | Type prev; \ |
| 2243 | 3192 | while (!success) { \ |
| 2244 | 3193 | prev = *obj; \ |
| 2245 | 3194 | new = ~(prev & value); \ |
| 2246 | | success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \ |
| 3195 | success = zig_msvc_cmpxchg_##ZigType(obj, &prev, new); \ |
| 2247 | 3196 | } \ |
| 2248 | 3197 | return prev; \ |
| 2249 | 3198 | } \ |
| 2250 | | static inline zig_##Type zig_msvc_atomicrmw_min_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 3199 | static inline Type zig_msvc_atomicrmw_min_##ZigType(Type volatile* obj, Type value) { \ |
| 2251 | 3200 | bool success = false; \ |
| 2252 | | zig_##Type new; \ |
| 2253 | | zig_##Type prev; \ |
| 3201 | Type new; \ |
| 3202 | Type prev; \ |
| 2254 | 3203 | while (!success) { \ |
| 2255 | 3204 | prev = *obj; \ |
| 2256 | 3205 | new = value < prev ? value : prev; \ |
| 2257 | | success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \ |
| 3206 | success = zig_msvc_cmpxchg_##ZigType(obj, &prev, new); \ |
| 2258 | 3207 | } \ |
| 2259 | 3208 | return prev; \ |
| 2260 | 3209 | } \ |
| 2261 | | static inline zig_##Type zig_msvc_atomicrmw_max_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 3210 | static inline Type zig_msvc_atomicrmw_max_##ZigType(Type volatile* obj, Type value) { \ |
| 2262 | 3211 | bool success = false; \ |
| 2263 | | zig_##Type new; \ |
| 2264 | | zig_##Type prev; \ |
| 3212 | Type new; \ |
| 3213 | Type prev; \ |
| 2265 | 3214 | while (!success) { \ |
| 2266 | 3215 | prev = *obj; \ |
| 2267 | 3216 | new = value > prev ? value : prev; \ |
| 2268 | | success = zig_msvc_cmpxchg_##Type(obj, &prev, new); \ |
| 3217 | success = zig_msvc_cmpxchg_##ZigType(obj, &prev, new); \ |
| 2269 | 3218 | } \ |
| 2270 | 3219 | return prev; \ |
| 2271 | 3220 | } \ |
| 2272 | | static inline void zig_msvc_atomic_store_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 3221 | static inline void zig_msvc_atomic_store_##ZigType(Type volatile* obj, Type value) { \ |
| 2273 | 3222 | _InterlockedExchange##suffix(obj, value); \ |
| 2274 | 3223 | } \ |
| 2275 | | static inline zig_##Type zig_msvc_atomic_load_##Type(zig_##Type volatile* obj) { \ |
| 3224 | static inline Type zig_msvc_atomic_load_##ZigType(Type volatile* obj) { \ |
| 2276 | 3225 | return _InterlockedOr##suffix(obj, 0); \ |
| 2277 | 3226 | } |
| 2278 | 3227 | |
| 2279 | | zig_msvc_atomics(u8, 8) |
| 2280 | | zig_msvc_atomics(i8, 8) |
| 2281 | | zig_msvc_atomics(u16, 16) |
| 2282 | | zig_msvc_atomics(i16, 16) |
| 2283 | | zig_msvc_atomics(u32, ) |
| 2284 | | zig_msvc_atomics(i32, ) |
| 3228 | zig_msvc_atomics( u8, uint8_t, 8) |
| 3229 | zig_msvc_atomics( i8, int8_t, 8) |
| 3230 | zig_msvc_atomics(u16, uint16_t, 16) |
| 3231 | zig_msvc_atomics(i16, int16_t, 16) |
| 3232 | zig_msvc_atomics(u32, uint32_t, ) |
| 3233 | zig_msvc_atomics(i32, int32_t, ) |
| 2285 | 3234 | |
| 2286 | 3235 | #if _M_X64 |
| 2287 | | zig_msvc_atomics(u64, 64) |
| 2288 | | zig_msvc_atomics(i64, 64) |
| 3236 | zig_msvc_atomics(u64, uint64_t, 64) |
| 3237 | zig_msvc_atomics(i64, int64_t, 64) |
| 2289 | 3238 | #endif |
| 2290 | 3239 | |
| 2291 | 3240 | #define zig_msvc_flt_atomics(Type, ReprType, suffix) \ |
| 2292 | 3241 | static inline bool zig_msvc_cmpxchg_##Type(zig_##Type volatile* obj, zig_##Type* expected, zig_##Type desired) { \ |
| 2293 | | zig_##ReprType comparand = *((zig_##ReprType*)expected); \ |
| 2294 | | zig_##ReprType initial = _InterlockedCompareExchange##suffix((zig_##ReprType volatile*)obj, *((zig_##ReprType*)&desired), comparand); \ |
| 2295 | | bool exchanged = initial == comparand; \ |
| 2296 | | if (!exchanged) { \ |
| 2297 | | *expected = *((zig_##Type*)&initial); \ |
| 2298 | | } \ |
| 2299 | | return exchanged; \ |
| 3242 | ReprType exchange; \ |
| 3243 | ReprType comparand; \ |
| 3244 | ReprType initial; \ |
| 3245 | bool success; \ |
| 3246 | memcpy(&comparand, expected, sizeof(comparand)); \ |
| 3247 | memcpy(&exchange, &desired, sizeof(exchange)); \ |
| 3248 | initial = _InterlockedCompareExchange##suffix((ReprType volatile*)obj, exchange, comparand); \ |
| 3249 | success = initial == comparand; \ |
| 3250 | if (!success) memcpy(expected, &initial, sizeof(*expected)); \ |
| 3251 | return success; \ |
| 2300 | 3252 | } \ |
| 2301 | 3253 | static inline zig_##Type zig_msvc_atomicrmw_xchg_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 2302 | | zig_##ReprType initial = _InterlockedExchange##suffix((zig_##ReprType volatile*)obj, *((zig_##ReprType*)&value)); \ |
| 2303 | | return *((zig_##Type*)&initial); \ |
| 3254 | ReprType repr; \ |
| 3255 | ReprType initial; \ |
| 3256 | zig_##Type result; \ |
| 3257 | memcpy(&repr, &value, sizeof(repr)); \ |
| 3258 | initial = _InterlockedExchange##suffix((ReprType volatile*)obj, repr); \ |
| 3259 | memcpy(&result, &initial, sizeof(result)); \ |
| 3260 | return result; \ |
| 2304 | 3261 | } \ |
| 2305 | 3262 | static inline zig_##Type zig_msvc_atomicrmw_add_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 2306 | | bool success = false; \ |
| 2307 | | zig_##ReprType new; \ |
| 2308 | | zig_##Type prev; \ |
| 2309 | | while (!success) { \ |
| 2310 | | prev = *obj; \ |
| 2311 | | new = prev + value; \ |
| 2312 | | success = zig_msvc_cmpxchg_##Type(obj, &prev, *((zig_##ReprType*)&new)); \ |
| 2313 | | } \ |
| 2314 | | return prev; \ |
| 3263 | ReprType repr; \ |
| 3264 | zig_##Type expected; \ |
| 3265 | zig_##Type desired; \ |
| 3266 | repr = *(ReprType volatile*)obj; \ |
| 3267 | memcpy(&expected, &repr, sizeof(expected)); \ |
| 3268 | do { \ |
| 3269 | desired = expected + value; \ |
| 3270 | } while (!zig_msvc_cmpxchg_##Type(obj, &expected, desired)); \ |
| 3271 | return expected; \ |
| 2315 | 3272 | } \ |
| 2316 | 3273 | static inline zig_##Type zig_msvc_atomicrmw_sub_##Type(zig_##Type volatile* obj, zig_##Type value) { \ |
| 2317 | | bool success = false; \ |
| 2318 | | zig_##ReprType new; \ |
| 2319 | | zig_##Type prev; \ |
| 2320 | | while (!success) { \ |
| 2321 | | prev = *obj; \ |
| 2322 | | new = prev - value; \ |
| 2323 | | success = zig_msvc_cmpxchg_##Type(obj, &prev, *((zig_##ReprType*)&new)); \ |
| 2324 | | } \ |
| 2325 | | return prev; \ |
| 3274 | ReprType repr; \ |
| 3275 | zig_##Type expected; \ |
| 3276 | zig_##Type desired; \ |
| 3277 | repr = *(ReprType volatile*)obj; \ |
| 3278 | memcpy(&expected, &repr, sizeof(expected)); \ |
| 3279 | do { \ |
| 3280 | desired = expected - value; \ |
| 3281 | } while (!zig_msvc_cmpxchg_##Type(obj, &expected, desired)); \ |
| 3282 | return expected; \ |
| 2326 | 3283 | } |
| 2327 | 3284 | |
| 2328 | | zig_msvc_flt_atomics(f32, u32, ) |
| 3285 | zig_msvc_flt_atomics(f32, uint32_t, ) |
| 2329 | 3286 | #if _M_X64 |
| 2330 | | zig_msvc_flt_atomics(f64, u64, 64) |
| 3287 | zig_msvc_flt_atomics(f64, uint64_t, 64) |
| 2331 | 3288 | #endif |
| 2332 | 3289 | |
| 2333 | 3290 | #if _M_IX86 |
| 2334 | 3291 | static inline void zig_msvc_atomic_barrier() { |
| 2335 | | zig_i32 barrier; |
| 3292 | int32_t barrier; |
| 2336 | 3293 | __asm { |
| 2337 | 3294 | xchg barrier, eax |
| 2338 | 3295 | } |
| 2339 | 3296 | } |
| 2340 | 3297 | |
| 2341 | | static inline void* zig_msvc_atomicrmw_xchg_p32(void** obj, zig_u32* arg) { |
| 3298 | static inline void* zig_msvc_atomicrmw_xchg_p32(void** obj, void* arg) { |
| 2342 | 3299 | return _InterlockedExchangePointer(obj, arg); |
| 2343 | 3300 | } |
| 2344 | 3301 | |
| 2345 | | static inline void zig_msvc_atomic_store_p32(void** obj, zig_u32* arg) { |
| 3302 | static inline void zig_msvc_atomic_store_p32(void** obj, void* arg) { |
| 2346 | 3303 | _InterlockedExchangePointer(obj, arg); |
| 2347 | 3304 | } |
| 2348 | 3305 | |
| ... | ... | @@ -2360,11 +3317,11 @@ static inline bool zig_msvc_cmpxchg_p32(void** obj, void** expected, void* desir |
| 2360 | 3317 | return exchanged; |
| 2361 | 3318 | } |
| 2362 | 3319 | #else /* _M_IX86 */ |
| 2363 | | static inline void* zig_msvc_atomicrmw_xchg_p64(void** obj, zig_u64* arg) { |
| 3320 | static inline void* zig_msvc_atomicrmw_xchg_p64(void** obj, void* arg) { |
| 2364 | 3321 | return _InterlockedExchangePointer(obj, arg); |
| 2365 | 3322 | } |
| 2366 | 3323 | |
| 2367 | | static inline void zig_msvc_atomic_store_p64(void** obj, zig_u64* arg) { |
| 3324 | static inline void zig_msvc_atomic_store_p64(void** obj, void* arg) { |
| 2368 | 3325 | _InterlockedExchangePointer(obj, arg); |
| 2369 | 3326 | } |
| 2370 | 3327 | |
| ... | ... | @@ -2383,11 +3340,11 @@ static inline bool zig_msvc_cmpxchg_p64(void** obj, void** expected, void* desir |
| 2383 | 3340 | } |
| 2384 | 3341 | |
| 2385 | 3342 | static inline bool zig_msvc_cmpxchg_u128(zig_u128 volatile* obj, zig_u128* expected, zig_u128 desired) { |
| 2386 | | return _InterlockedCompareExchange128((zig_i64 volatile*)obj, desired.hi, desired.lo, (zig_i64*)expected); |
| 3343 | return _InterlockedCompareExchange128((int64_t volatile*)obj, desired.hi, desired.lo, (int64_t*)expected); |
| 2387 | 3344 | } |
| 2388 | 3345 | |
| 2389 | 3346 | static inline bool zig_msvc_cmpxchg_i128(zig_i128 volatile* obj, zig_i128* expected, zig_i128 desired) { |
| 2390 | | return _InterlockedCompareExchange128((zig_i64 volatile*)obj, desired.hi, desired.lo, (zig_u64*)expected); |
| 3347 | return _InterlockedCompareExchange128((int64_t volatile*)obj, desired.hi, desired.lo, (uint64_t*)expected); |
| 2391 | 3348 | } |
| 2392 | 3349 | |
| 2393 | 3350 | #define zig_msvc_atomics_128xchg(Type) \ |
| ... | ... | @@ -2429,7 +3386,7 @@ zig_msvc_atomics_128op(u128, max) |
| 2429 | 3386 | |
| 2430 | 3387 | #endif /* _MSC_VER && (_M_IX86 || _M_X64) */ |
| 2431 | 3388 | |
| 2432 | | /* ========================= Special Case Intrinsics ========================= */ |
| 3389 | /* ======================== Special Case Intrinsics ========================= */ |
| 2433 | 3390 | |
| 2434 | 3391 | #if (_MSC_VER && _M_X64) || defined(__x86_64__) |
| 2435 | 3392 | |
| ... | ... | @@ -2459,8 +3416,8 @@ static inline void* zig_x86_windows_teb(void) { |
| 2459 | 3416 | |
| 2460 | 3417 | #if (_MSC_VER && (_M_IX86 || _M_X64)) || defined(__i386__) || defined(__x86_64__) |
| 2461 | 3418 | |
| 2462 | | static inline void zig_x86_cpuid(zig_u32 leaf_id, zig_u32 subid, zig_u32* eax, zig_u32* ebx, zig_u32* ecx, zig_u32* edx) { |
| 2463 | | zig_u32 cpu_info[4]; |
| 3419 | static inline void zig_x86_cpuid(uint32_t leaf_id, uint32_t subid, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx) { |
| 3420 | uint32_t cpu_info[4]; |
| 2464 | 3421 | #if _MSC_VER |
| 2465 | 3422 | __cpuidex(cpu_info, leaf_id, subid); |
| 2466 | 3423 | #else |
| ... | ... | @@ -2472,12 +3429,12 @@ static inline void zig_x86_cpuid(zig_u32 leaf_id, zig_u32 subid, zig_u32* eax, z |
| 2472 | 3429 | *edx = cpu_info[3]; |
| 2473 | 3430 | } |
| 2474 | 3431 | |
| 2475 | | static inline zig_u32 zig_x86_get_xcr0(void) { |
| 3432 | static inline uint32_t zig_x86_get_xcr0(void) { |
| 2476 | 3433 | #if _MSC_VER |
| 2477 | | return (zig_u32)_xgetbv(0); |
| 3434 | return (uint32_t)_xgetbv(0); |
| 2478 | 3435 | #else |
| 2479 | | zig_u32 eax; |
| 2480 | | zig_u32 edx; |
| 3436 | uint32_t eax; |
| 3437 | uint32_t edx; |
| 2481 | 3438 | __asm__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0)); |
| 2482 | 3439 | return eax; |
| 2483 | 3440 | #endif |