simdjson  3.9.2
Ridiculously Fast JSON
bitmanipulation.h
1 #ifndef SIMDJSON_ARM64_BITMANIPULATION_H
2 #define SIMDJSON_ARM64_BITMANIPULATION_H
3 
4 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
5 #include "simdjson/arm64/base.h"
6 #include "simdjson/arm64/intrinsics.h"
7 #endif // SIMDJSON_CONDITIONAL_INCLUDE
8 
9 namespace simdjson {
10 namespace arm64 {
11 namespace {
12 
13 // We sometimes call trailing_zero on inputs that are zero,
14 // but the algorithms do not end up using the returned value.
15 // Sadly, sanitizers are not smart enough to figure it out.
16 SIMDJSON_NO_SANITIZE_UNDEFINED
17 // This function can be used safely even if not all bytes have been
18 // initialized.
19 // See issue https://github.com/simdjson/simdjson/issues/1965
20 SIMDJSON_NO_SANITIZE_MEMORY
21 simdjson_inline int trailing_zeroes(uint64_t input_num) {
22 #ifdef SIMDJSON_REGULAR_VISUAL_STUDIO
23  unsigned long ret;
24  // Search the mask data from least significant bit (LSB)
25  // to the most significant bit (MSB) for a set bit (1).
26  _BitScanForward64(&ret, input_num);
27  return (int)ret;
28 #else // SIMDJSON_REGULAR_VISUAL_STUDIO
29  return __builtin_ctzll(input_num);
30 #endif // SIMDJSON_REGULAR_VISUAL_STUDIO
31 }
32 
33 /* result might be undefined when input_num is zero */
34 simdjson_inline uint64_t clear_lowest_bit(uint64_t input_num) {
35  return input_num & (input_num-1);
36 }
37 
38 /* result might be undefined when input_num is zero */
39 simdjson_inline int leading_zeroes(uint64_t input_num) {
40 #ifdef SIMDJSON_REGULAR_VISUAL_STUDIO
41  unsigned long leading_zero = 0;
42  // Search the mask data from most significant bit (MSB)
43  // to least significant bit (LSB) for a set bit (1).
44  if (_BitScanReverse64(&leading_zero, input_num))
45  return (int)(63 - leading_zero);
46  else
47  return 64;
48 #else
49  return __builtin_clzll(input_num);
50 #endif// SIMDJSON_REGULAR_VISUAL_STUDIO
51 }
52 
53 /* result might be undefined when input_num is zero */
54 simdjson_inline int count_ones(uint64_t input_num) {
55  return vaddv_u8(vcnt_u8(vcreate_u8(input_num)));
56 }
57 
58 
59 #if defined(__GNUC__) // catches clang and gcc
66 /*
67  * We use SIMDJSON_PREFER_REVERSE_BITS as a hint that algorithms that
68  * work well with bit reversal may use it.
69  */
70 #define SIMDJSON_PREFER_REVERSE_BITS 1
71 
72 /* reverse the bits */
73 simdjson_inline uint64_t reverse_bits(uint64_t input_num) {
74  uint64_t rev_bits;
75  __asm("rbit %0, %1" : "=r"(rev_bits) : "r"(input_num));
76  return rev_bits;
77 }
78 
85 SIMDJSON_NO_SANITIZE_UNDEFINED
86 simdjson_inline uint64_t zero_leading_bit(uint64_t rev_bits, int leading_zeroes) {
87  return rev_bits ^ (uint64_t(0x8000000000000000) >> leading_zeroes);
88 }
89 
90 #endif
91 
92 simdjson_inline bool add_overflow(uint64_t value1, uint64_t value2, uint64_t *result) {
93 #ifdef SIMDJSON_REGULAR_VISUAL_STUDIO
94  *result = value1 + value2;
95  return *result < value1;
96 #else
97  return __builtin_uaddll_overflow(value1, value2,
98  reinterpret_cast<unsigned long long *>(result));
99 #endif
100 }
101 
102 } // unnamed namespace
103 } // namespace arm64
104 } // namespace simdjson
105 
106 #endif // SIMDJSON_ARM64_BITMANIPULATION_H
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8