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 NO_SANITIZE_UNDEFINED
trailing_zeroes(uint64_t input_num)12 simdjson_really_inline int trailing_zeroes(uint64_t input_num) {
13 #ifdef SIMDJSON_REGULAR_VISUAL_STUDIO
14   return (int)_tzcnt_u64(input_num);
15 #else // SIMDJSON_REGULAR_VISUAL_STUDIO
16   ////////
17   // You might expect the next line to be equivalent to
18   // return (int)_tzcnt_u64(input_num);
19   // but the generated code differs and might be less efficient?
20   ////////
21   return __builtin_ctzll(input_num);
22 #endif // SIMDJSON_REGULAR_VISUAL_STUDIO
23 }
24 
25 /* result might be undefined when input_num is zero */
clear_lowest_bit(uint64_t input_num)26 simdjson_really_inline uint64_t clear_lowest_bit(uint64_t input_num) {
27   return _blsr_u64(input_num);
28 }
29 
30 /* result might be undefined when input_num is zero */
leading_zeroes(uint64_t input_num)31 simdjson_really_inline int leading_zeroes(uint64_t input_num) {
32   return int(_lzcnt_u64(input_num));
33 }
34 
35 #ifdef SIMDJSON_REGULAR_VISUAL_STUDIO
count_ones(uint64_t input_num)36 simdjson_really_inline unsigned __int64 count_ones(uint64_t input_num) {
37   // note: we do not support legacy 32-bit Windows
38   return __popcnt64(input_num);// Visual Studio wants two underscores
39 }
40 #else
count_ones(uint64_t input_num)41 simdjson_really_inline long long int count_ones(uint64_t input_num) {
42   return _popcnt64(input_num);
43 }
44 #endif
45 
add_overflow(uint64_t value1,uint64_t value2,uint64_t * result)46 simdjson_really_inline bool add_overflow(uint64_t value1, uint64_t value2,
47                                 uint64_t *result) {
48 #ifdef SIMDJSON_REGULAR_VISUAL_STUDIO
49   return _addcarry_u64(0, value1, value2,
50                        reinterpret_cast<unsigned __int64 *>(result));
51 #else
52   return __builtin_uaddll_overflow(value1, value2,
53                                    reinterpret_cast<unsigned long long *>(result));
54 #endif
55 }
56 
57 } // unnamed namespace
58 } // namespace SIMDJSON_IMPLEMENTATION
59 } // namespace simdjson
60 
61 #endif // SIMDJSON_HASWELL_BITMANIPULATION_H
62