simdjson 4.0.7
Ridiculously Fast JSON
Loading...
Searching...
No Matches
bitmanipulation.h
1#ifndef SIMDJSON_ARM64_BITMANIPULATION_H
2#define SIMDJSON_ARM64_BITMANIPULATION_H
3
4#ifndef SIMDJSON_CONDITIONAL_INCLUDE
5#include "simdjson/arm64/base.h"
6#include "simdjson/arm64/intrinsics.h"
7#endif // SIMDJSON_CONDITIONAL_INCLUDE
8
9namespace simdjson {
10namespace arm64 {
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// We sometimes call leading_zeroes on inputs that are zero,
39// but the algorithms do not end up using the returned value.
40// Sadly, sanitizers are not smart enough to figure it out.
41// Applies only when SIMDJSON_PREFER_REVERSE_BITS is defined and true.
42// (See below.)
43SIMDJSON_NO_SANITIZE_UNDEFINED
44/* result might be undefined when input_num is zero */
45simdjson_inline int leading_zeroes(uint64_t input_num) {
46#if SIMDJSON_REGULAR_VISUAL_STUDIO
47 unsigned long leading_zero = 0;
48 // Search the mask data from most significant bit (MSB)
49 // to least significant bit (LSB) for a set bit (1).
50 if (_BitScanReverse64(&leading_zero, input_num))
51 return (int)(63 - leading_zero);
52 else
53 return 64;
54#else
55 return __builtin_clzll(input_num);
56#endif// SIMDJSON_REGULAR_VISUAL_STUDIO
57}
58
59/* result might be undefined when input_num is zero */
60simdjson_inline int count_ones(uint64_t input_num) {
61 return vaddv_u8(vcnt_u8(vcreate_u8(input_num)));
62}
63
64
65#if defined(__GNUC__) // catches clang and gcc
72/*
73 * We use SIMDJSON_PREFER_REVERSE_BITS as a hint that algorithms that
74 * work well with bit reversal may use it.
75 */
76#define SIMDJSON_PREFER_REVERSE_BITS 1
77
78/* reverse the bits */
79simdjson_inline uint64_t reverse_bits(uint64_t input_num) {
80 uint64_t rev_bits;
81 __asm("rbit %0, %1" : "=r"(rev_bits) : "r"(input_num));
82 return rev_bits;
83}
84
91SIMDJSON_NO_SANITIZE_UNDEFINED
92simdjson_inline uint64_t zero_leading_bit(uint64_t rev_bits, int leading_zeroes) {
93 return rev_bits ^ (uint64_t(0x8000000000000000) >> leading_zeroes);
94}
95
96#endif
97
98simdjson_inline bool add_overflow(uint64_t value1, uint64_t value2, uint64_t *result) {
99#if SIMDJSON_REGULAR_VISUAL_STUDIO
100 *result = value1 + value2;
101 return *result < value1;
102#else
103 return __builtin_uaddll_overflow(value1, value2,
104 reinterpret_cast<unsigned long long *>(result));
105#endif
106}
107
108} // unnamed namespace
109} // namespace arm64
110} // namespace simdjson
111
112#endif // SIMDJSON_ARM64_BITMANIPULATION_H
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8