|
| 1 | +#ifndef BIGDECIMAL_BITS_H |
| 2 | +#define BIGDECIMAL_BITS_H |
| 3 | + |
| 4 | +#include "feature.h" |
| 5 | +#include "static_assert.h" |
| 6 | + |
| 7 | +#if defined(HAVE_X86INTRIN_H) |
| 8 | +# include <x86intrin.h> /* for _lzcnt_u64 */ |
| 9 | +#elif defined(_MSC_VER) && _MSC_VER >= 1310 |
| 10 | +# include <intrin.h> /* for the following intrinsics */ |
| 11 | +#endif |
| 12 | + |
| 13 | +#if defined(_MSC_VER) && defined(__AVX2__) |
| 14 | +# pragma intrinsic(__lzcnt) |
| 15 | +# pragma intrinsic(__lzcnt64) |
| 16 | +#endif |
| 17 | + |
| 18 | +#define MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max) ( \ |
| 19 | + (a) == 0 ? 0 : \ |
| 20 | + (a) == -1 ? (b) < -(max) : \ |
| 21 | + (a) > 0 ? \ |
| 22 | + ((b) > 0 ? (max) / (a) < (b) : (min) / (a) > (b)) : \ |
| 23 | + ((b) > 0 ? (min) / (a) < (b) : (max) / (a) > (b))) |
| 24 | + |
| 25 | +#ifdef HAVE_UINT128_T |
| 26 | +# define bit_length(x) \ |
| 27 | + (unsigned int) \ |
| 28 | + (sizeof(x) <= sizeof(int32_t) ? 32 - nlz_int32((uint32_t)(x)) : \ |
| 29 | + sizeof(x) <= sizeof(int64_t) ? 64 - nlz_int64((uint64_t)(x)) : \ |
| 30 | + 128 - nlz_int128((uint128_t)(x))) |
| 31 | +#else |
| 32 | +# define bit_length(x) \ |
| 33 | + (unsigned int) \ |
| 34 | + (sizeof(x) <= sizeof(int32_t) ? 32 - nlz_int32((uint32_t)(x)) : \ |
| 35 | + 64 - nlz_int64((uint64_t)(x))) |
| 36 | +#endif |
| 37 | + |
| 38 | +static inline unsigned nlz_int32(uint32_t x); |
| 39 | +static inline unsigned nlz_int64(uint64_t x); |
| 40 | +#ifdef HAVE_UINT128_T |
| 41 | +static inline unsigned nlz_int128(uint128_t x); |
| 42 | +#endif |
| 43 | + |
| 44 | +static inline unsigned int |
| 45 | +nlz_int32(uint32_t x) |
| 46 | +{ |
| 47 | +#if defined(_MSC_VER) && defined(__AVX2__) |
| 48 | + /* Note: It seems there is no such thing like __LZCNT__ predefined in MSVC. |
| 49 | + * AMD CPUs have had this instruction for decades (since K10) but for |
| 50 | + * Intel, Haswell is the oldest one. We need to use __AVX2__ for maximum |
| 51 | + * safety. */ |
| 52 | + return (unsigned int)__lzcnt(x); |
| 53 | + |
| 54 | +#elif defined(__x86_64__) && defined(__LZCNT__) /* && ! defined(MJIT_HEADER) */ |
| 55 | + return (unsigned int)_lzcnt_u32(x); |
| 56 | + |
| 57 | +#elif defined(_MSC_VER) && _MSC_VER >= 1400 /* &&! defined(__AVX2__) */ |
| 58 | + unsigned long r; |
| 59 | + return _BitScanReverse(&r, x) ? (31 - (int)r) : 32; |
| 60 | + |
| 61 | +#elif __has_builtin(__builtin_clz) |
| 62 | + STATIC_ASSERT(sizeof_int, sizeof(int) * CHAR_BIT == 32); |
| 63 | + return x ? (unsigned int)__builtin_clz(x) : 32; |
| 64 | + |
| 65 | +#else |
| 66 | + uint32_t y; |
| 67 | + unsigned n = 32; |
| 68 | + y = x >> 16; if (y) {n -= 16; x = y;} |
| 69 | + y = x >> 8; if (y) {n -= 8; x = y;} |
| 70 | + y = x >> 4; if (y) {n -= 4; x = y;} |
| 71 | + y = x >> 2; if (y) {n -= 2; x = y;} |
| 72 | + y = x >> 1; if (y) {return n - 2;} |
| 73 | + return (unsigned int)(n - x); |
| 74 | +#endif |
| 75 | +} |
| 76 | + |
| 77 | +static inline unsigned int |
| 78 | +nlz_int64(uint64_t x) |
| 79 | +{ |
| 80 | +#if defined(_MSC_VER) && defined(__AVX2__) |
| 81 | + return (unsigned int)__lzcnt64(x); |
| 82 | + |
| 83 | +#elif defined(__x86_64__) && defined(__LZCNT__) /* && ! defined(MJIT_HEADER) */ |
| 84 | + return (unsigned int)_lzcnt_u64(x); |
| 85 | + |
| 86 | +#elif defined(_WIN64) && defined(_MSC_VER) && _MSC_VER >= 1400 /* &&! defined(__AVX2__) */ |
| 87 | + unsigned long r; |
| 88 | + return _BitScanReverse64(&r, x) ? (63u - (unsigned int)r) : 64; |
| 89 | + |
| 90 | +#elif __has_builtin(__builtin_clzl) |
| 91 | + if (x == 0) { |
| 92 | + return 64; |
| 93 | + } |
| 94 | + else if (sizeof(long) * CHAR_BIT == 64) { |
| 95 | + return (unsigned int)__builtin_clzl((unsigned long)x); |
| 96 | + } |
| 97 | + else if (sizeof(long long) * CHAR_BIT == 64) { |
| 98 | + return (unsigned int)__builtin_clzll((unsigned long long)x); |
| 99 | + } |
| 100 | + else { |
| 101 | + /* :FIXME: Is there a way to make this branch a compile-time error? */ |
| 102 | + __builtin_unreachable(); |
| 103 | + } |
| 104 | + |
| 105 | +#else |
| 106 | + uint64_t y; |
| 107 | + unsigned int n = 64; |
| 108 | + y = x >> 32; if (y) {n -= 32; x = y;} |
| 109 | + y = x >> 16; if (y) {n -= 16; x = y;} |
| 110 | + y = x >> 8; if (y) {n -= 8; x = y;} |
| 111 | + y = x >> 4; if (y) {n -= 4; x = y;} |
| 112 | + y = x >> 2; if (y) {n -= 2; x = y;} |
| 113 | + y = x >> 1; if (y) {return n - 2;} |
| 114 | + return (unsigned int)(n - x); |
| 115 | + |
| 116 | +#endif |
| 117 | +} |
| 118 | + |
| 119 | +#ifdef HAVE_UINT128_T |
| 120 | +static inline unsigned int |
| 121 | +nlz_int128(uint128_t x) |
| 122 | +{ |
| 123 | + uint64_t y = (uint64_t)(x >> 64); |
| 124 | + |
| 125 | + if (x == 0) { |
| 126 | + return 128; |
| 127 | + } |
| 128 | + else if (y == 0) { |
| 129 | + return (unsigned int)nlz_int64(x) + 64; |
| 130 | + } |
| 131 | + else { |
| 132 | + return (unsigned int)nlz_int64(y); |
| 133 | + } |
| 134 | +} |
| 135 | +#endif |
| 136 | + |
| 137 | +#endif /* BIGDECIMAL_BITS_H */ |
0 commit comments