Lines Matching refs:T
31 template <typename T>
33 static_assert(std::is_integral<T>::value, "T must be integral"); in BitSizeOf()
34 using unsigned_type = typename std::make_unsigned<T>::type; in BitSizeOf()
35 static_assert(sizeof(T) == sizeof(unsigned_type), "Unexpected type size mismatch!"); in BitSizeOf()
41 template <typename T>
42 constexpr size_t BitSizeOf(T /*x*/) { in BitSizeOf() argument
43 return BitSizeOf<T>(); in BitSizeOf()
46 template<typename T>
47 constexpr int CLZ(T x) { in CLZ()
48 static_assert(std::is_integral<T>::value, "T must be integral"); in CLZ()
49 static_assert(std::is_unsigned<T>::value, "T must be unsigned"); in CLZ()
50 static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); in CLZ()
51 static_assert(sizeof(T) == sizeof(uint64_t) || sizeof(T) <= sizeof(uint32_t), in CLZ()
54 constexpr bool is_64_bit = (sizeof(T) == sizeof(uint64_t)); in CLZ()
56 is_64_bit ? 0u : std::numeric_limits<uint32_t>::digits - std::numeric_limits<T>::digits; in CLZ()
61 template<typename T>
62 constexpr int JAVASTYLE_CLZ(T x) { in JAVASTYLE_CLZ()
63 static_assert(std::is_integral<T>::value, "T must be integral"); in JAVASTYLE_CLZ()
64 using unsigned_type = typename std::make_unsigned<T>::type; in JAVASTYLE_CLZ()
65 return (x == 0) ? BitSizeOf<T>() : CLZ(static_cast<unsigned_type>(x)); in JAVASTYLE_CLZ()
68 template<typename T>
69 constexpr int CTZ(T x) { in CTZ()
70 static_assert(std::is_integral<T>::value, "T must be integral"); in CTZ()
73 static_assert(sizeof(T) == sizeof(uint64_t) || sizeof(T) <= sizeof(uint32_t), in CTZ()
75 DCHECK_NE(x, static_cast<T>(0)); in CTZ()
76 return (sizeof(T) == sizeof(uint64_t)) ? __builtin_ctzll(x) : __builtin_ctz(x); in CTZ()
80 template<typename T>
81 constexpr int JAVASTYLE_CTZ(T x) { in JAVASTYLE_CTZ()
82 static_assert(std::is_integral<T>::value, "T must be integral"); in JAVASTYLE_CTZ()
83 using unsigned_type = typename std::make_unsigned<T>::type; in JAVASTYLE_CTZ()
84 return (x == 0) ? BitSizeOf<T>() : CTZ(static_cast<unsigned_type>(x)); in JAVASTYLE_CTZ()
88 template<typename T>
89 constexpr int POPCOUNT(T x) { in POPCOUNT()
90 return (sizeof(T) == sizeof(uint32_t)) ? __builtin_popcount(x) : __builtin_popcountll(x); in POPCOUNT()
94 template<typename T>
95 constexpr T BSWAP(T x) { in BSWAP()
96 if (sizeof(T) == sizeof(uint16_t)) { in BSWAP()
98 } else if (sizeof(T) == sizeof(uint32_t)) { in BSWAP()
106 template <typename T>
107 constexpr ssize_t MostSignificantBit(T value) { in MostSignificantBit()
108 static_assert(std::is_integral<T>::value, "T must be integral"); in MostSignificantBit()
109 static_assert(std::is_unsigned<T>::value, "T must be unsigned"); in MostSignificantBit()
110 static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); in MostSignificantBit()
111 return (value == 0) ? -1 : std::numeric_limits<T>::digits - 1 - CLZ(value); in MostSignificantBit()
115 template <typename T>
116 constexpr ssize_t LeastSignificantBit(T value) { in LeastSignificantBit()
117 static_assert(std::is_integral<T>::value, "T must be integral"); in LeastSignificantBit()
118 static_assert(std::is_unsigned<T>::value, "T must be unsigned"); in LeastSignificantBit()
123 template <typename T>
124 constexpr size_t MinimumBitsToStore(T value) { in MinimumBitsToStore()
128 template <typename T>
129 constexpr T RoundUpToPowerOfTwo(T x) { in RoundUpToPowerOfTwo()
130 static_assert(std::is_integral<T>::value, "T must be integral"); in RoundUpToPowerOfTwo()
131 static_assert(std::is_unsigned<T>::value, "T must be unsigned"); in RoundUpToPowerOfTwo()
133 return (x < 2u) ? x : static_cast<T>(1u) << (std::numeric_limits<T>::digits - CLZ(x - 1u)); in RoundUpToPowerOfTwo()
137 template <typename T>
138 constexpr T TruncToPowerOfTwo(T val) { in TruncToPowerOfTwo()
139 static_assert(std::is_integral<T>::value, "T must be integral"); in TruncToPowerOfTwo()
140 static_assert(std::is_unsigned<T>::value, "T must be unsigned"); in TruncToPowerOfTwo()
141 return (val != 0) ? static_cast<T>(1u) << (BitSizeOf<T>() - CLZ(val) - 1u) : 0; in TruncToPowerOfTwo()
144 template<typename T>
145 constexpr bool IsPowerOfTwo(T x) { in IsPowerOfTwo()
146 static_assert(std::is_integral<T>::value, "T must be integral"); in IsPowerOfTwo()
151 template<typename T>
152 constexpr int WhichPowerOf2(T x) { in WhichPowerOf2()
153 static_assert(std::is_integral<T>::value, "T must be integral"); in WhichPowerOf2()
161 template<typename T>
162 constexpr T RoundDown(T x, typename Identity<T>::type n) WARN_UNUSED;
164 template<typename T>
165 constexpr T RoundDown(T x, typename Identity<T>::type n) { in RoundDown()
170 template<typename T>
171 constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) WARN_UNUSED;
173 template<typename T>
174 constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) { in RoundUp()
179 template<typename T>
180 inline T* AlignDown(T* x, uintptr_t n) WARN_UNUSED;
182 template<typename T>
183 inline T* AlignDown(T* x, uintptr_t n) { in AlignDown()
184 return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n)); in AlignDown()
187 template<typename T>
188 inline T* AlignUp(T* x, uintptr_t n) WARN_UNUSED;
190 template<typename T>
191 inline T* AlignUp(T* x, uintptr_t n) { in AlignUp()
192 return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n)); in AlignUp()
195 template<int n, typename T>
196 constexpr bool IsAligned(T x) { in IsAligned()
201 template<int n, typename T>
202 inline bool IsAligned(T* x) { in IsAligned()
206 template<typename T>
207 inline bool IsAlignedParam(T x, int n) { in IsAlignedParam()
211 template<typename T>
212 inline bool IsAlignedParam(T* x, int n) { in IsAlignedParam()
245 template <typename T>
246 inline bool IsInt(size_t N, T value) { in IsInt()
247 if (N == BitSizeOf<T>()) { in IsInt()
251 CHECK_LT(N, BitSizeOf<T>()); in IsInt()
252 T limit = static_cast<T>(1) << (N - 1u); in IsInt()
257 template <typename T>
258 constexpr T GetIntLimit(size_t bits) { in GetIntLimit()
260 DCHECK_LT(bits, BitSizeOf<T>()); in GetIntLimit()
261 return static_cast<T>(1) << (bits - 1); in GetIntLimit()
264 template <size_t kBits, typename T>
265 constexpr bool IsInt(T value) { in IsInt()
267 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); in IsInt()
268 static_assert(std::is_signed<T>::value, "Needs a signed type."); in IsInt()
271 return (kBits == BitSizeOf<T>()) ? in IsInt()
273 (-GetIntLimit<T>(kBits) <= value) && (value < GetIntLimit<T>(kBits)); in IsInt()
276 template <size_t kBits, typename T>
277 constexpr bool IsUint(T value) { in IsUint()
279 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); in IsUint()
280 static_assert(std::is_integral<T>::value, "Needs an integral type."); in IsUint()
285 using unsigned_type = typename std::make_unsigned<T>::type; in IsUint()
287 (kBits == BitSizeOf<T>() || in IsUint()
291 template <size_t kBits, typename T>
292 constexpr bool IsAbsoluteUint(T value) { in IsAbsoluteUint()
293 static_assert(kBits <= BitSizeOf<T>(), "kBits must be <= max."); in IsAbsoluteUint()
294 static_assert(std::is_integral<T>::value, "Needs an integral type."); in IsAbsoluteUint()
295 using unsigned_type = typename std::make_unsigned<T>::type; in IsAbsoluteUint()
296 return (kBits == BitSizeOf<T>()) in IsAbsoluteUint()
304 template <typename T>
305 constexpr T MaxInt(size_t bits) { in MaxInt()
306 DCHECK(std::is_unsigned<T>::value || bits > 0u) << "bits cannot be zero for signed."; in MaxInt()
307 DCHECK_LE(bits, BitSizeOf<T>()); in MaxInt()
308 using unsigned_type = typename std::make_unsigned<T>::type; in MaxInt()
309 return bits == BitSizeOf<T>() in MaxInt()
310 ? std::numeric_limits<T>::max() in MaxInt()
311 : std::is_signed<T>::value in MaxInt()
312 ? ((bits == 1u) ? 0 : static_cast<T>(MaxInt<unsigned_type>(bits - 1))) in MaxInt()
313 : static_cast<T>(UINT64_C(1) << bits) - static_cast<T>(1); in MaxInt()
316 template <typename T>
317 constexpr T MinInt(size_t bits) { in MinInt()
318 DCHECK(std::is_unsigned<T>::value || bits > 0) << "bits cannot be zero for signed."; in MinInt()
319 DCHECK_LE(bits, BitSizeOf<T>()); in MinInt()
320 return bits == BitSizeOf<T>() in MinInt()
321 ? std::numeric_limits<T>::min() in MinInt()
322 : std::is_signed<T>::value in MinInt()
323 ? ((bits == 1u) ? -1 : static_cast<T>(-1) - MaxInt<T>(bits)) in MinInt()
324 : static_cast<T>(0); in MinInt()
335 template <typename T>
336 inline static T HighestOneBitValue(T opnd) { in HighestOneBitValue()
337 using unsigned_type = typename std::make_unsigned<T>::type; in HighestOneBitValue()
338 T res; in HighestOneBitValue()
342 int bit_position = BitSizeOf<T>() - (CLZ(static_cast<unsigned_type>(opnd)) + 1); in HighestOneBitValue()
343 res = static_cast<T>(UINT64_C(1) << bit_position); in HighestOneBitValue()
349 template <typename T, bool left>
350 inline static T Rot(T opnd, int distance) { in Rot()
351 int mask = BitSizeOf<T>() - 1; in Rot()
354 using unsigned_type = typename std::make_unsigned<T>::type; in Rot()
390 template <typename T = size_t>
391 inline static constexpr std::make_unsigned_t<T> MaskLeastSignificant(size_t bits) { in MaskLeastSignificant()
392 DCHECK_GE(BitSizeOf<T>(), bits) << "Bits out of range for type T"; in MaskLeastSignificant()
393 using unsigned_T = std::make_unsigned_t<T>; in MaskLeastSignificant()
394 if (bits >= BitSizeOf<T>()) { in MaskLeastSignificant()
417 template <typename T>
418 inline static constexpr T BitFieldClear(T value, size_t lsb, size_t width) { in BitFieldClear()
420 const auto val = static_cast<std::make_unsigned_t<T>>(value); in BitFieldClear()
421 const auto mask = MaskLeastSignificant<T>(width); in BitFieldClear()
423 return static_cast<T>(val & ~(mask << lsb)); in BitFieldClear()
444 template <typename T, typename T2>
445 inline static constexpr T BitFieldInsert(T value, T2 data, size_t lsb, size_t width) { in BitFieldInsert()
456 return static_cast<T>(value_cleared | ((data & data_mask) << lsb)); in BitFieldInsert()
481 template <typename T>
482 inline static constexpr T BitFieldExtract(T value, size_t lsb, size_t width) { in BitFieldExtract()
484 const auto val = static_cast<std::make_unsigned_t<T>>(value); in BitFieldExtract()
486 const T bitfield_unsigned = in BitFieldExtract()
487 static_cast<T>((val >> lsb) & MaskLeastSignificant<T>(width)); in BitFieldExtract()
488 if (std::is_signed<T>::value) { in BitFieldExtract()
491 return static_cast<T>(0); in BitFieldExtract()
495 const auto ones_negmask = ~MaskLeastSignificant<T>(width); in BitFieldExtract()
496 return static_cast<T>(bitfield_unsigned | ones_negmask); in BitFieldExtract()