simdjson  3.1.5
Ridiculously Fast JSON
bitmanipulation.h
1 #ifndef SIMDJSON_HASWELL_BITMANIPULATION_H
2 #define SIMDJSON_HASWELL_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  return (int)_tzcnt_u64(input_num);
19 #else // SIMDJSON_REGULAR_VISUAL_STUDIO
21  // You might expect the next line to be equivalent to
22  // return (int)_tzcnt_u64(input_num);
23  // but the generated code differs and might be less efficient?
25  return __builtin_ctzll(input_num);
26 #endif // SIMDJSON_REGULAR_VISUAL_STUDIO
27 }
28 
29 /* result might be undefined when input_num is zero */
30 simdjson_inline uint64_t clear_lowest_bit(uint64_t input_num) {
31  return _blsr_u64(input_num);
32 }
33 
34 /* result might be undefined when input_num is zero */
35 simdjson_inline int leading_zeroes(uint64_t input_num) {
36  return int(_lzcnt_u64(input_num));
37 }
38 
39 #if SIMDJSON_REGULAR_VISUAL_STUDIO
40 simdjson_inline unsigned __int64 count_ones(uint64_t input_num) {
41  // note: we do not support legacy 32-bit Windows
42  return __popcnt64(input_num);// Visual Studio wants two underscores
43 }
44 #else
45 simdjson_inline long long int count_ones(uint64_t input_num) {
46  return _popcnt64(input_num);
47 }
48 #endif
49 
50 simdjson_inline bool add_overflow(uint64_t value1, uint64_t value2,
51  uint64_t *result) {
52 #if SIMDJSON_REGULAR_VISUAL_STUDIO
53  return _addcarry_u64(0, value1, value2,
54  reinterpret_cast<unsigned __int64 *>(result));
55 #else
56  return __builtin_uaddll_overflow(value1, value2,
57  reinterpret_cast<unsigned long long *>(result));
58 #endif
59 }
60 
61 } // unnamed namespace
62 } // namespace SIMDJSON_IMPLEMENTATION
63 } // namespace simdjson
64 
65 #endif // SIMDJSON_HASWELL_BITMANIPULATION_H
We want to support argument-dependent lookup (ADL).