MCPcopy Create free account
hub / github.com/apache/kvrocks / SignedBitfieldPlus

Function SignedBitfieldPlus

src/common/bitfield_util.cc:39–79  ·  view source on GitHub ↗

See also https://github.com/redis/redis/blob/7f4bae817614988c43c3024402d16edcbf3b3277/src/bitops.c#L325

Source from the content-addressed store, hash-verified

37
38// See also https://github.com/redis/redis/blob/7f4bae817614988c43c3024402d16edcbf3b3277/src/bitops.c#L325
39StatusOr<bool> SignedBitfieldPlus(uint64_t value, int64_t incr, uint8_t bits, BitfieldOverflowBehavior overflow,
40 uint64_t *dst) {
41 Status bits_status(BitfieldEncoding::CheckSupportedBitLengths(BitfieldEncoding::Type::kSigned, bits));
42 if (!bits_status) {
43 return bits_status;
44 }
45
46 auto max = std::numeric_limits<int64_t>::max();
47 if (bits != 64) {
48 max = (static_cast<int64_t>(1) << (bits - 1)) - 1;
49 }
50 int64_t min = -max - 1;
51
52 int64_t signed_value = CastToSignedWithoutBitChanges(value);
53 int64_t max_incr = CastToSignedWithoutBitChanges(static_cast<uint64_t>(max) - value);
54 int64_t min_incr = min - signed_value;
55
56 if (signed_value > max || (bits != 64 && incr > max_incr) || (signed_value >= 0 && incr >= 0 && incr > max_incr)) {
57 if (overflow == BitfieldOverflowBehavior::kWrap) {
58 *dst = WrappedSignedBitfieldPlus(value, incr, bits);
59 } else if (overflow == BitfieldOverflowBehavior::kSat) {
60 *dst = max;
61 } else {
62 CHECK(overflow == BitfieldOverflowBehavior::kFail);
63 }
64 return true;
65 } else if (signed_value < min || (bits != 64 && incr < min_incr) ||
66 (signed_value < 0 && incr < 0 && incr < min_incr)) {
67 if (overflow == BitfieldOverflowBehavior::kWrap) {
68 *dst = WrappedSignedBitfieldPlus(value, incr, bits);
69 } else if (overflow == BitfieldOverflowBehavior::kSat) {
70 *dst = min;
71 } else {
72 CHECK(overflow == BitfieldOverflowBehavior::kFail);
73 }
74 return true;
75 }
76
77 *dst = signed_value + incr;
78 return false;
79}
80
81static uint64_t WrappedUnsignedBitfieldPlus(uint64_t value, int64_t incr, uint8_t bits) {
82 uint64_t mask = std::numeric_limits<uint64_t>::max() << bits;

Callers 1

BitfieldPlusFunction · 0.85

Calls 2

Tested by

no test coverage detected