1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file defines some bit utilities.
6
7 #ifndef BASE_BITS_H_
8 #define BASE_BITS_H_
9
10 #include <stddef.h>
11 #include <stdint.h>
12
13 #include <bit>
14 #include <concepts>
15
16 #include "base/check.h"
17
18 namespace base::bits {
19
20 // Bit functions in <bit> are restricted to a specific set of types of unsigned
21 // integer; restrict functions in this file that are related to those in that
22 // header to match for consistency.
23 template <typename T>
24 concept UnsignedInteger =
25 std::unsigned_integral<T> && !std::same_as<T, bool> &&
26 !std::same_as<T, char> && !std::same_as<T, char8_t> &&
27 !std::same_as<T, char16_t> && !std::same_as<T, char32_t> &&
28 !std::same_as<T, wchar_t>;
29
30 // We want to migrate all users of these functions to use the unsigned type
31 // versions of the functions, but until they are all moved over, create a
32 // concept that captures all the types that must be supported for compatibility
33 // but that we want to remove.
34 template <typename T>
35 concept SignedIntegerDeprecatedDoNotUse =
36 std::integral<T> && !UnsignedInteger<T>;
37
38 // Returns true iff |value| is a power of 2.
39 //
40 // TODO(https://crbug.com/1414634): Replace with std::has_single_bit().
41 template <typename T>
42 requires UnsignedInteger<T>
IsPowerOfTwo(T value)43 constexpr bool IsPowerOfTwo(T value) {
44 // From "Hacker's Delight": Section 2.1 Manipulating Rightmost Bits.
45 //
46 // Only positive integers with a single bit set are powers of two. If only one
47 // bit is set in x (e.g. 0b00000100000000) then |x-1| will have that bit set
48 // to zero and all bits to its right set to 1 (e.g. 0b00000011111111). Hence
49 // |x & (x-1)| is 0 iff x is a power of two.
50 return value > 0 && (value & (value - 1)) == 0;
51 }
52
53 template <typename T>
54 requires SignedIntegerDeprecatedDoNotUse<T>
IsPowerOfTwoDeprecatedDoNotUse(T value)55 constexpr bool IsPowerOfTwoDeprecatedDoNotUse(T value) {
56 return value > 0 && (value & (value - 1)) == 0;
57 }
58
59 // Round down |size| to a multiple of alignment, which must be a power of two.
60 template <typename T>
61 requires UnsignedInteger<T>
AlignDown(T size,T alignment)62 inline constexpr T AlignDown(T size, T alignment) {
63 DCHECK(IsPowerOfTwo(alignment));
64 return size & ~(alignment - 1);
65 }
66
67 template <typename T>
68 requires SignedIntegerDeprecatedDoNotUse<T>
AlignDownDeprecatedDoNotUse(T size,T alignment)69 inline constexpr T AlignDownDeprecatedDoNotUse(T size, T alignment) {
70 DCHECK(IsPowerOfTwoDeprecatedDoNotUse(alignment));
71 return size & ~(alignment - 1);
72 }
73
74 // Move |ptr| back to the previous multiple of alignment, which must be a power
75 // of two. Defined for types where sizeof(T) is one byte.
76 template <typename T>
77 requires(sizeof(T) == 1)
AlignDown(T * ptr,uintptr_t alignment)78 inline T* AlignDown(T* ptr, uintptr_t alignment) {
79 return reinterpret_cast<T*>(
80 AlignDown(reinterpret_cast<uintptr_t>(ptr), alignment));
81 }
82
83 // Round up |size| to a multiple of alignment, which must be a power of two.
84 template <typename T>
85 requires UnsignedInteger<T>
AlignUp(T size,T alignment)86 inline constexpr T AlignUp(T size, T alignment) {
87 DCHECK(IsPowerOfTwo(alignment));
88 return (size + alignment - 1) & ~(alignment - 1);
89 }
90
91 template <typename T>
92 requires SignedIntegerDeprecatedDoNotUse<T>
AlignUpDeprecatedDoNotUse(T size,T alignment)93 inline constexpr T AlignUpDeprecatedDoNotUse(T size, T alignment) {
94 DCHECK(IsPowerOfTwoDeprecatedDoNotUse(alignment));
95 return (size + alignment - 1) & ~(alignment - 1);
96 }
97
98 // Advance |ptr| to the next multiple of alignment, which must be a power of
99 // two. Defined for types where sizeof(T) is one byte.
100 template <typename T>
101 requires(sizeof(T) == 1)
AlignUp(T * ptr,uintptr_t alignment)102 inline T* AlignUp(T* ptr, uintptr_t alignment) {
103 return reinterpret_cast<T*>(
104 AlignUp(reinterpret_cast<uintptr_t>(ptr), alignment));
105 }
106
107 // Returns the integer i such as 2^i <= n < 2^(i+1).
108 //
109 // A common use for this function is to measure the number of bits required to
110 // contain a value; for that case use std::bit_width().
111 //
112 // A common use for this function is to take its result and use it to left-shift
113 // a bit; instead of doing so, use std::bit_floor().
114 // TODO(https://crbug.com/1414634): Replace existing uses that do that.
Log2Floor(uint32_t n)115 constexpr int Log2Floor(uint32_t n) {
116 return 31 - std::countl_zero(n);
117 }
118
119 // Returns the integer i such as 2^(i-1) < n <= 2^i.
120 //
121 // A common use for this function is to measure the number of bits required to
122 // contain a value; for that case use std::bit_width().
123 //
124 // A common use for this function is to take its result and use it to left-shift
125 // a bit; instead of doing so, use std::bit_ceil().
126 // TODO(https://crbug.com/1414634): Replace existing uses that do that.
Log2Ceiling(uint32_t n)127 constexpr int Log2Ceiling(uint32_t n) {
128 // When n == 0, we want the function to return -1.
129 // When n == 0, (n - 1) will underflow to 0xFFFFFFFF, which is
130 // why the statement below starts with (n ? 32 : -1).
131 return (n ? 32 : -1) - std::countl_zero(n - 1);
132 }
133
134 // Returns a value of type T with a single bit set in the left-most position.
135 // Can be used instead of manually shifting a 1 to the left. Unlike the other
136 // functions in this file, usable for any integral type.
137 template <typename T>
138 requires std::integral<T>
LeftmostBit()139 constexpr T LeftmostBit() {
140 T one(1u);
141 return one << (8 * sizeof(T) - 1);
142 }
143
144 } // namespace base::bits
145
146 #endif // BASE_BITS_H_
147