| 1 | #ifndef SIMDJSON_ARM64_BITMANIPULATION_H |
| 2 | #define SIMDJSON_ARM64_BITMANIPULATION_H |
| 3 | |
| 4 | namespace simdjson { |
| 5 | namespace SIMDJSON_IMPLEMENTATION { |
| 6 | namespace { |
| 7 | |
| 8 | // We sometimes call trailing_zero on inputs that are zero, |
| 9 | // but the algorithms do not end up using the returned value. |
| 10 | // Sadly, sanitizers are not smart enough to figure it out. |
| 11 | SIMDJSON_NO_SANITIZE_UNDEFINED |
| 12 | simdjson_inline int trailing_zeroes(uint64_t input_num) { |
| 13 | #ifdef SIMDJSON_REGULAR_VISUAL_STUDIO |
| 14 | unsigned long ret; |
| 15 | // Search the mask data from least significant bit (LSB) |
| 16 | // to the most significant bit (MSB) for a set bit (1). |
| 17 | _BitScanForward64(&ret, input_num); |
| 18 | return (int)ret; |
| 19 | #else // SIMDJSON_REGULAR_VISUAL_STUDIO |
| 20 | return __builtin_ctzll(input_num); |
| 21 | #endif // SIMDJSON_REGULAR_VISUAL_STUDIO |
| 22 | } |
| 23 | |
| 24 | /* result might be undefined when input_num is zero */ |
| 25 | simdjson_inline uint64_t clear_lowest_bit(uint64_t input_num) { |
| 26 | return input_num & (input_num-1); |
| 27 | } |
| 28 | |
| 29 | /* result might be undefined when input_num is zero */ |
| 30 | simdjson_inline int leading_zeroes(uint64_t input_num) { |
| 31 | #ifdef SIMDJSON_REGULAR_VISUAL_STUDIO |
| 32 | unsigned long leading_zero = 0; |
| 33 | // Search the mask data from most significant bit (MSB) |
| 34 | // to least significant bit (LSB) for a set bit (1). |
| 35 | if (_BitScanReverse64(&leading_zero, input_num)) |
| 36 | return (int)(63 - leading_zero); |
| 37 | else |
| 38 | return 64; |
| 39 | #else |
| 40 | return __builtin_clzll(input_num); |
| 41 | #endif// SIMDJSON_REGULAR_VISUAL_STUDIO |
| 42 | } |
| 43 | |
| 44 | /* result might be undefined when input_num is zero */ |
| 45 | simdjson_inline int count_ones(uint64_t input_num) { |
| 46 | return vaddv_u8(p0: vcnt_u8(vcreate_u8(input_num))); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | #if defined(__GNUC__) // catches clang and gcc |
| 51 | /** |
| 52 | * ARM has a fast 64-bit "bit reversal function" that is handy. However, |
| 53 | * it is not generally available as an intrinsic function under Visual |
| 54 | * Studio (though this might be changing). Even under clang/gcc, we |
| 55 | * apparently need to invoke inline assembly. |
| 56 | */ |
| 57 | /* |
| 58 | * We use SIMDJSON_PREFER_REVERSE_BITS as a hint that algorithms that |
| 59 | * work well with bit reversal may use it. |
| 60 | */ |
| 61 | #define SIMDJSON_PREFER_REVERSE_BITS 1 |
| 62 | |
| 63 | /* reverse the bits */ |
| 64 | simdjson_inline uint64_t reverse_bits(uint64_t input_num) { |
| 65 | uint64_t rev_bits; |
| 66 | __asm("rbit %0, %1" : "=r" (rev_bits) : "r" (input_num)); |
| 67 | return rev_bits; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Flips bit at index 63 - lz. Thus if you have 'leading_zeroes' leading zeroes, |
| 72 | * then this will set to zero the leading bit. It is possible for leading_zeroes to be |
| 73 | * greating or equal to 63 in which case we trigger undefined behavior, but the output |
| 74 | * of such undefined behavior is never used. |
| 75 | **/ |
| 76 | SIMDJSON_NO_SANITIZE_UNDEFINED |
| 77 | simdjson_inline uint64_t zero_leading_bit(uint64_t rev_bits, int leading_zeroes) { |
| 78 | return rev_bits ^ (uint64_t(0x8000000000000000) >> leading_zeroes); |
| 79 | } |
| 80 | |
| 81 | #endif |
| 82 | |
| 83 | simdjson_inline bool add_overflow(uint64_t value1, uint64_t value2, uint64_t *result) { |
| 84 | #ifdef SIMDJSON_REGULAR_VISUAL_STUDIO |
| 85 | *result = value1 + value2; |
| 86 | return *result < value1; |
| 87 | #else |
| 88 | return __builtin_uaddll_overflow(value1, value2, |
| 89 | reinterpret_cast<unsigned long long *>(result)); |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | } // unnamed namespace |
| 94 | } // namespace SIMDJSON_IMPLEMENTATION |
| 95 | } // namespace simdjson |
| 96 | |
| 97 | #endif // SIMDJSON_ARM64_BITMANIPULATION_H |
| 98 | |