simdjson  3.11.0
Ridiculously Fast JSON
bitmanipulation.h
1 #ifndef SIMDJSON_HASWELL_BITMANIPULATION_H
2 #define SIMDJSON_HASWELL_BITMANIPULATION_H
3 
4 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
5 #include "simdjson/haswell/base.h"
6 #include "simdjson/haswell/intrinsics.h"
7 #include "simdjson/haswell/bitmask.h"
8 #endif // SIMDJSON_CONDITIONAL_INCLUDE
9 
10 namespace simdjson {
11 namespace haswell {
12 namespace {
13 
14 // We sometimes call trailing_zero on inputs that are zero,
15 // but the algorithms do not end up using the returned value.
16 // Sadly, sanitizers are not smart enough to figure it out.
17 SIMDJSON_NO_SANITIZE_UNDEFINED
18 // This function can be used safely even if not all bytes have been
19 // initialized.
20 // See issue https://github.com/simdjson/simdjson/issues/1965
21 SIMDJSON_NO_SANITIZE_MEMORY
22 simdjson_inline int trailing_zeroes(uint64_t input_num) {
23 #if SIMDJSON_REGULAR_VISUAL_STUDIO
24  return (int)_tzcnt_u64(input_num);
25 #else // SIMDJSON_REGULAR_VISUAL_STUDIO
27  // You might expect the next line to be equivalent to
28  // return (int)_tzcnt_u64(input_num);
29  // but the generated code differs and might be less efficient?
31  return __builtin_ctzll(input_num);
32 #endif // SIMDJSON_REGULAR_VISUAL_STUDIO
33 }
34 
35 /* result might be undefined when input_num is zero */
36 simdjson_inline uint64_t clear_lowest_bit(uint64_t input_num) {
37  return _blsr_u64(input_num);
38 }
39 
40 /* result might be undefined when input_num is zero */
41 simdjson_inline int leading_zeroes(uint64_t input_num) {
42  return int(_lzcnt_u64(input_num));
43 }
44 
45 #if SIMDJSON_REGULAR_VISUAL_STUDIO
46 simdjson_inline unsigned __int64 count_ones(uint64_t input_num) {
47  // note: we do not support legacy 32-bit Windows in this kernel
48  return __popcnt64(input_num);// Visual Studio wants two underscores
49 }
50 #else
51 simdjson_inline long long int count_ones(uint64_t input_num) {
52  return _popcnt64(input_num);
53 }
54 #endif
55 
56 simdjson_inline bool add_overflow(uint64_t value1, uint64_t value2,
57  uint64_t *result) {
58 #if SIMDJSON_REGULAR_VISUAL_STUDIO
59  return _addcarry_u64(0, value1, value2,
60  reinterpret_cast<unsigned __int64 *>(result));
61 #else
62  return __builtin_uaddll_overflow(value1, value2,
63  reinterpret_cast<unsigned long long *>(result));
64 #endif
65 }
66 
67 } // unnamed namespace
68 } // namespace haswell
69 } // namespace simdjson
70 
71 #endif // SIMDJSON_HASWELL_BITMANIPULATION_H
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8