1 | #ifndef SIMDJSON_WESTMERE_BITMANIPULATION_H |
2 | #define SIMDJSON_WESTMERE_BITMANIPULATION_H |
3 | |
4 | #include "simdjson/common_defs.h" |
5 | #include "simdjson/portability.h" |
6 | #ifdef IS_X86_64 |
7 | #include "westmere/intrinsics.h" |
8 | |
9 | TARGET_WESTMERE |
10 | namespace simdjson::westmere { |
11 | |
12 | #ifndef _MSC_VER |
13 | // We sometimes call trailing_zero on inputs that are zero, |
14 | // but the algorithms do not end up using the returned value. |
15 | // Sadly, sanitizers are not smart enough to figure it out. |
16 | __attribute__((no_sanitize("undefined" ))) // this is deliberate |
17 | #endif |
18 | /* result might be undefined when input_num is zero */ |
19 | really_inline int trailing_zeroes(uint64_t input_num) { |
20 | #ifdef _MSC_VER |
21 | unsigned long ret; |
22 | // Search the mask data from least significant bit (LSB) |
23 | // to the most significant bit (MSB) for a set bit (1). |
24 | _BitScanForward64(&ret, input_num); |
25 | return (int)ret; |
26 | #else |
27 | return __builtin_ctzll(input_num); |
28 | #endif// _MSC_VER |
29 | } |
30 | |
31 | /* result might be undefined when input_num is zero */ |
32 | really_inline uint64_t clear_lowest_bit(uint64_t input_num) { |
33 | return input_num & (input_num-1); |
34 | } |
35 | |
36 | /* result might be undefined when input_num is zero */ |
37 | really_inline int leading_zeroes(uint64_t input_num) { |
38 | #ifdef _MSC_VER |
39 | unsigned long leading_zero = 0; |
40 | // Search the mask data from most significant bit (MSB) |
41 | // to least significant bit (LSB) for a set bit (1). |
42 | if (_BitScanReverse64(&leading_zero, input_num)) |
43 | return (int)(63 - leading_zero); |
44 | else |
45 | return 64; |
46 | #else |
47 | return __builtin_clzll(input_num); |
48 | #endif// _MSC_VER |
49 | } |
50 | |
51 | really_inline int hamming(uint64_t input_num) { |
52 | #ifdef _MSC_VER |
53 | // note: we do not support legacy 32-bit Windows |
54 | return __popcnt64(input_num);// Visual Studio wants two underscores |
55 | #else |
56 | return _popcnt64(input_num); |
57 | #endif |
58 | } |
59 | |
60 | really_inline bool add_overflow(uint64_t value1, uint64_t value2, |
61 | uint64_t *result) { |
62 | #ifdef _MSC_VER |
63 | return _addcarry_u64(0, value1, value2, |
64 | reinterpret_cast<unsigned __int64 *>(result)); |
65 | #else |
66 | return __builtin_uaddll_overflow(value1, value2, |
67 | (unsigned long long *)result); |
68 | #endif |
69 | } |
70 | |
71 | #ifdef _MSC_VER |
72 | #pragma intrinsic(_umul128) |
73 | #endif |
74 | really_inline bool mul_overflow(uint64_t value1, uint64_t value2, |
75 | uint64_t *result) { |
76 | #ifdef _MSC_VER |
77 | uint64_t high; |
78 | *result = _umul128(value1, value2, &high); |
79 | return high; |
80 | #else |
81 | return __builtin_umulll_overflow(value1, value2, |
82 | (unsigned long long *)result); |
83 | #endif |
84 | } |
85 | |
86 | }// namespace simdjson::westmere |
87 | UNTARGET_REGION |
88 | |
89 | #endif |
90 | #endif // SIMDJSON_WESTMERE_BITMANIPULATION_H |
91 | |