summaryrefslogtreecommitdiff
path: root/runtime/base/bit_utils.h
diff options
context:
space:
mode:
author Andreas Gampe <agampe@google.com> 2015-08-17 20:52:26 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2015-08-17 20:52:26 +0000
commit18d54cda7d9a0e6846a1c7b9b72c07598f3d1855 (patch)
tree2f90bcd0192246a28fe840d749cd7c95acb393db /runtime/base/bit_utils.h
parent381e58a43c6833f7fa7018cf0545681d3ebea4c3 (diff)
parent151ab8d096be02b04391fd32460a31ee60ae2b0a (diff)
Merge "Revert "Revert "ART: DCHECK zero case for CLZ/CTZ"""
Diffstat (limited to 'runtime/base/bit_utils.h')
-rw-r--r--runtime/base/bit_utils.h23
1 files changed, 15 insertions, 8 deletions
diff --git a/runtime/base/bit_utils.h b/runtime/base/bit_utils.h
index 1da6750b45..1b0d774419 100644
--- a/runtime/base/bit_utils.h
+++ b/runtime/base/bit_utils.h
@@ -29,21 +29,28 @@ namespace art {
template<typename T>
static constexpr int CLZ(T x) {
static_assert(std::is_integral<T>::value, "T must be integral");
- // TODO: assert unsigned. There is currently many uses with signed values.
+ static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(sizeof(T) <= sizeof(long long), // NOLINT [runtime/int] [4]
"T too large, must be smaller than long long");
- return (sizeof(T) == sizeof(uint32_t))
- ? __builtin_clz(x) // TODO: __builtin_clz[ll] has undefined behavior for x=0
- : __builtin_clzll(x);
+ return
+ DCHECK_CONSTEXPR(x != 0, "x must not be zero", T(0))
+ (sizeof(T) == sizeof(uint32_t))
+ ? __builtin_clz(x)
+ : __builtin_clzll(x);
}
template<typename T>
static constexpr int CTZ(T x) {
static_assert(std::is_integral<T>::value, "T must be integral");
- // TODO: assert unsigned. There is currently many uses with signed values.
- return (sizeof(T) == sizeof(uint32_t))
- ? __builtin_ctz(x)
- : __builtin_ctzll(x);
+ // It is not unreasonable to ask for trailing zeros in a negative number. As such, do not check
+ // that T is an unsigned type.
+ static_assert(sizeof(T) <= sizeof(long long), // NOLINT [runtime/int] [4]
+ "T too large, must be smaller than long long");
+ return
+ DCHECK_CONSTEXPR(x != 0, "x must not be zero", T(0))
+ (sizeof(T) == sizeof(uint32_t))
+ ? __builtin_ctz(x)
+ : __builtin_ctzll(x);
}
template<typename T>