simdjson  3.1.6
Ridiculously Fast JSON
bitmanipulation.h
1 #ifndef SIMDJSON_PPC64_BITMANIPULATION_H
2 #define SIMDJSON_PPC64_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 // This function can be used safely even if not all bytes have been
13 // initialized.
14 // See issue https://github.com/simdjson/simdjson/issues/1965
15 SIMDJSON_NO_SANITIZE_MEMORY
16 simdjson_inline int trailing_zeroes(uint64_t input_num) {
17 #if SIMDJSON_REGULAR_VISUAL_STUDIO
18  unsigned long ret;
19  // Search the mask data from least significant bit (LSB)
20  // to the most significant bit (MSB) for a set bit (1).
21  _BitScanForward64(&ret, input_num);
22  return (int)ret;
23 #else // SIMDJSON_REGULAR_VISUAL_STUDIO
24  return __builtin_ctzll(input_num);
25 #endif // SIMDJSON_REGULAR_VISUAL_STUDIO
26 }
27 
28 /* result might be undefined when input_num is zero */
29 simdjson_inline uint64_t clear_lowest_bit(uint64_t input_num) {
30  return input_num & (input_num - 1);
31 }
32 
33 /* result might be undefined when input_num is zero */
34 simdjson_inline int leading_zeroes(uint64_t input_num) {
35 #if SIMDJSON_REGULAR_VISUAL_STUDIO
36  unsigned long leading_zero = 0;
37  // Search the mask data from most significant bit (MSB)
38  // to least significant bit (LSB) for a set bit (1).
39  if (_BitScanReverse64(&leading_zero, input_num))
40  return (int)(63 - leading_zero);
41  else
42  return 64;
43 #else
44  return __builtin_clzll(input_num);
45 #endif // SIMDJSON_REGULAR_VISUAL_STUDIO
46 }
47 
48 #if SIMDJSON_REGULAR_VISUAL_STUDIO
49 simdjson_inline int count_ones(uint64_t input_num) {
50  // note: we do not support legacy 32-bit Windows
51  return __popcnt64(input_num); // Visual Studio wants two underscores
52 }
53 #else
54 simdjson_inline int count_ones(uint64_t input_num) {
55  return __builtin_popcountll(input_num);
56 }
57 #endif
58 
59 simdjson_inline bool add_overflow(uint64_t value1, uint64_t value2,
60  uint64_t *result) {
61 #if SIMDJSON_REGULAR_VISUAL_STUDIO
62  *result = value1 + value2;
63  return *result < value1;
64 #else
65  return __builtin_uaddll_overflow(value1, value2,
66  reinterpret_cast<unsigned long long *>(result));
67 #endif
68 }
69 
70 } // unnamed namespace
71 } // namespace SIMDJSON_IMPLEMENTATION
72 } // namespace simdjson
73 
74 #endif // SIMDJSON_PPC64_BITMANIPULATION_H
We want to support argument-dependent lookup (ADL).