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