simdjson  3.11.0
Ridiculously Fast JSON
bitmanipulation.h
1 #ifndef SIMDJSON_PPC64_BITMANIPULATION_H
2 #define SIMDJSON_PPC64_BITMANIPULATION_H
3 
4 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
5 #include "simdjson/ppc64/base.h"
6 #endif // SIMDJSON_CONDITIONAL_INCLUDE
7 
8 namespace simdjson {
9 namespace ppc64 {
10 namespace {
11 
12 // We sometimes call trailing_zero on inputs that are zero,
13 // but the algorithms do not end up using the returned value.
14 // Sadly, sanitizers are not smart enough to figure it out.
15 SIMDJSON_NO_SANITIZE_UNDEFINED
16 // This function can be used safely even if not all bytes have been
17 // initialized.
18 // See issue https://github.com/simdjson/simdjson/issues/1965
19 SIMDJSON_NO_SANITIZE_MEMORY
20 simdjson_inline int trailing_zeroes(uint64_t input_num) {
21 #if SIMDJSON_REGULAR_VISUAL_STUDIO
22  unsigned long ret;
23  // Search the mask data from least significant bit (LSB)
24  // to the most significant bit (MSB) for a set bit (1).
25  _BitScanForward64(&ret, input_num);
26  return (int)ret;
27 #else // SIMDJSON_REGULAR_VISUAL_STUDIO
28  return __builtin_ctzll(input_num);
29 #endif // SIMDJSON_REGULAR_VISUAL_STUDIO
30 }
31 
32 /* result might be undefined when input_num is zero */
33 simdjson_inline uint64_t clear_lowest_bit(uint64_t input_num) {
34  return input_num & (input_num - 1);
35 }
36 
37 /* result might be undefined when input_num is zero */
38 simdjson_inline int leading_zeroes(uint64_t input_num) {
39 #if SIMDJSON_REGULAR_VISUAL_STUDIO
40  unsigned long leading_zero = 0;
41  // Search the mask data from most significant bit (MSB)
42  // to least significant bit (LSB) for a set bit (1).
43  if (_BitScanReverse64(&leading_zero, input_num))
44  return (int)(63 - leading_zero);
45  else
46  return 64;
47 #else
48  return __builtin_clzll(input_num);
49 #endif // SIMDJSON_REGULAR_VISUAL_STUDIO
50 }
51 
52 #if SIMDJSON_REGULAR_VISUAL_STUDIO
53 simdjson_inline int count_ones(uint64_t input_num) {
54  // note: we do not support legacy 32-bit Windows in this kernel
55  return __popcnt64(input_num); // Visual Studio wants two underscores
56 }
57 #else
58 simdjson_inline int count_ones(uint64_t input_num) {
59  return __builtin_popcountll(input_num);
60 }
61 #endif
62 
63 simdjson_inline bool add_overflow(uint64_t value1, uint64_t value2,
64  uint64_t *result) {
65 #if SIMDJSON_REGULAR_VISUAL_STUDIO
66  *result = value1 + value2;
67  return *result < value1;
68 #else
69  return __builtin_uaddll_overflow(value1, value2,
70  reinterpret_cast<unsigned long long *>(result));
71 #endif
72 }
73 
74 } // unnamed namespace
75 } // namespace ppc64
76 } // namespace simdjson
77 
78 #endif // SIMDJSON_PPC64_BITMANIPULATION_H
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8