summaryrefslogtreecommitdiffstats
path: root/runtime
diff options
context:
space:
mode:
authorVladimir Marko <vmarko@google.com>2014-05-02 11:53:22 +0100
committerVladimir Marko <vmarko@google.com>2014-05-02 12:21:02 +0100
commit8194963098247be6bca9cc4a54dbfa65c73e8ccc (patch)
tree547cc708e06e6541676b17066023ae6f07b2049b /runtime
parent56a341a82ece9aa4f2a071629f3e1fd1adf988ae (diff)
downloadart-8194963098247be6bca9cc4a54dbfa65c73e8ccc.zip
art-8194963098247be6bca9cc4a54dbfa65c73e8ccc.tar.gz
art-8194963098247be6bca9cc4a54dbfa65c73e8ccc.tar.bz2
Replace CountOneBits and __builtin_popcount with POPCOUNT.
Clean up utils.h, make some functions constexpr. Change-Id: I2399100280cbce81c3c4f5765f0680c1ddcb5883
Diffstat (limited to 'runtime')
-rw-r--r--runtime/arch/arm/context_arm.cc4
-rw-r--r--runtime/arch/arm/fault_handler_arm.cc4
-rw-r--r--runtime/arch/arm64/context_arm64.cc4
-rw-r--r--runtime/arch/mips/context_mips.cc4
-rw-r--r--runtime/arch/x86/context_x86.cc2
-rw-r--r--runtime/arch/x86_64/context_x86_64.cc4
-rw-r--r--runtime/base/bit_vector.cc6
-rw-r--r--runtime/gc/accounting/gc_allocator.h2
-rw-r--r--runtime/mirror/class.cc4
-rw-r--r--runtime/runtime.cc16
-rw-r--r--runtime/stack.h5
-rw-r--r--runtime/utils.h67
-rw-r--r--runtime/vmap_table.h2
13 files changed, 62 insertions, 62 deletions
diff --git a/runtime/arch/arm/context_arm.cc b/runtime/arch/arm/context_arm.cc
index 102e126..0e1b25e 100644
--- a/runtime/arch/arm/context_arm.cc
+++ b/runtime/arch/arm/context_arm.cc
@@ -44,8 +44,8 @@ void ArmContext::FillCalleeSaves(const StackVisitor& fr) {
mirror::ArtMethod* method = fr.GetMethod();
uint32_t core_spills = method->GetCoreSpillMask();
uint32_t fp_core_spills = method->GetFpSpillMask();
- size_t spill_count = __builtin_popcount(core_spills);
- size_t fp_spill_count = __builtin_popcount(fp_core_spills);
+ size_t spill_count = POPCOUNT(core_spills);
+ size_t fp_spill_count = POPCOUNT(fp_core_spills);
size_t frame_size = method->GetFrameSizeInBytes();
if (spill_count > 0) {
// Lowest number spill is farthest away, walk registers and fill into context
diff --git a/runtime/arch/arm/fault_handler_arm.cc b/runtime/arch/arm/fault_handler_arm.cc
index 3bbec71..eddaa0b 100644
--- a/runtime/arch/arm/fault_handler_arm.cc
+++ b/runtime/arch/arm/fault_handler_arm.cc
@@ -233,9 +233,9 @@ bool StackOverflowHandler::Action(int sig, siginfo_t* info, void* context) {
mirror::ArtMethod* method = reinterpret_cast<mirror::ArtMethod*>(sc->arm_r0);
uint32_t spill_mask = method->GetCoreSpillMask();
- uint32_t numcores = __builtin_popcount(spill_mask);
+ uint32_t numcores = POPCOUNT(spill_mask);
uint32_t fp_spill_mask = method->GetFpSpillMask();
- uint32_t numfps = __builtin_popcount(fp_spill_mask);
+ uint32_t numfps = POPCOUNT(fp_spill_mask);
uint32_t spill_size = (numcores + numfps) * 4;
LOG(DEBUG) << "spill size: " << spill_size;
uint8_t* prevframe = prevsp + spill_size;
diff --git a/runtime/arch/arm64/context_arm64.cc b/runtime/arch/arm64/context_arm64.cc
index c96ff60..0890fa9 100644
--- a/runtime/arch/arm64/context_arm64.cc
+++ b/runtime/arch/arm64/context_arm64.cc
@@ -47,8 +47,8 @@ void Arm64Context::FillCalleeSaves(const StackVisitor& fr) {
mirror::ArtMethod* method = fr.GetMethod();
uint32_t core_spills = method->GetCoreSpillMask();
uint32_t fp_core_spills = method->GetFpSpillMask();
- size_t spill_count = __builtin_popcount(core_spills);
- size_t fp_spill_count = __builtin_popcount(fp_core_spills);
+ size_t spill_count = POPCOUNT(core_spills);
+ size_t fp_spill_count = POPCOUNT(fp_core_spills);
size_t frame_size = method->GetFrameSizeInBytes();
if (spill_count > 0) {
diff --git a/runtime/arch/mips/context_mips.cc b/runtime/arch/mips/context_mips.cc
index b957708..0950e71 100644
--- a/runtime/arch/mips/context_mips.cc
+++ b/runtime/arch/mips/context_mips.cc
@@ -43,8 +43,8 @@ void MipsContext::FillCalleeSaves(const StackVisitor& fr) {
mirror::ArtMethod* method = fr.GetMethod();
uint32_t core_spills = method->GetCoreSpillMask();
uint32_t fp_core_spills = method->GetFpSpillMask();
- size_t spill_count = __builtin_popcount(core_spills);
- size_t fp_spill_count = __builtin_popcount(fp_core_spills);
+ size_t spill_count = POPCOUNT(core_spills);
+ size_t fp_spill_count = POPCOUNT(fp_core_spills);
size_t frame_size = method->GetFrameSizeInBytes();
if (spill_count > 0) {
// Lowest number spill is farthest away, walk registers and fill into context.
diff --git a/runtime/arch/x86/context_x86.cc b/runtime/arch/x86/context_x86.cc
index 5cf3001..c68d76a 100644
--- a/runtime/arch/x86/context_x86.cc
+++ b/runtime/arch/x86/context_x86.cc
@@ -38,7 +38,7 @@ void X86Context::Reset() {
void X86Context::FillCalleeSaves(const StackVisitor& fr) {
mirror::ArtMethod* method = fr.GetMethod();
uint32_t core_spills = method->GetCoreSpillMask();
- size_t spill_count = __builtin_popcount(core_spills);
+ size_t spill_count = POPCOUNT(core_spills);
DCHECK_EQ(method->GetFpSpillMask(), 0u);
size_t frame_size = method->GetFrameSizeInBytes();
if (spill_count > 0) {
diff --git a/runtime/arch/x86_64/context_x86_64.cc b/runtime/arch/x86_64/context_x86_64.cc
index 3f1f86d..29a7065 100644
--- a/runtime/arch/x86_64/context_x86_64.cc
+++ b/runtime/arch/x86_64/context_x86_64.cc
@@ -42,8 +42,8 @@ void X86_64Context::FillCalleeSaves(const StackVisitor& fr) {
mirror::ArtMethod* method = fr.GetMethod();
uint32_t core_spills = method->GetCoreSpillMask();
uint32_t fp_core_spills = method->GetFpSpillMask();
- size_t spill_count = __builtin_popcount(core_spills);
- size_t fp_spill_count = __builtin_popcount(fp_core_spills);
+ size_t spill_count = POPCOUNT(core_spills);
+ size_t fp_spill_count = POPCOUNT(fp_core_spills);
size_t frame_size = method->GetFrameSizeInBytes();
if (spill_count > 0) {
// Lowest number spill is farthest away, walk registers and fill into context.
diff --git a/runtime/base/bit_vector.cc b/runtime/base/bit_vector.cc
index 12c0352..3df5101 100644
--- a/runtime/base/bit_vector.cc
+++ b/runtime/base/bit_vector.cc
@@ -201,7 +201,7 @@ void BitVector::Subtract(const BitVector *src) {
uint32_t BitVector::NumSetBits() const {
uint32_t count = 0;
for (uint32_t word = 0; word < storage_size_; word++) {
- count += __builtin_popcount(storage_[word]);
+ count += POPCOUNT(storage_[word]);
}
return count;
}
@@ -331,10 +331,10 @@ uint32_t BitVector::NumSetBits(const uint32_t* storage, uint32_t end) {
uint32_t count = 0u;
for (uint32_t word = 0u; word < word_end; word++) {
- count += __builtin_popcount(storage[word]);
+ count += POPCOUNT(storage[word]);
}
if (partial_word_bits != 0u) {
- count += __builtin_popcount(storage[word_end] & ~(0xffffffffu << partial_word_bits));
+ count += POPCOUNT(storage[word_end] & ~(0xffffffffu << partial_word_bits));
}
return count;
}
diff --git a/runtime/gc/accounting/gc_allocator.h b/runtime/gc/accounting/gc_allocator.h
index 4fe9367..7dd7cca 100644
--- a/runtime/gc/accounting/gc_allocator.h
+++ b/runtime/gc/accounting/gc_allocator.h
@@ -73,7 +73,7 @@ class GcAllocatorImpl : public std::allocator<T> {
// GCAllocatorImpl<T> if kMeasureGCMemoryOverhead is true, std::allocator<T> otherwise.
template <typename T>
class GcAllocator : public TypeStaticIf<kMeasureGcMemoryOverhead, GcAllocatorImpl<T>,
- std::allocator<T> >::value {
+ std::allocator<T> >::type {
};
} // namespace accounting
diff --git a/runtime/mirror/class.cc b/runtime/mirror/class.cc
index 64a849b..662303e 100644
--- a/runtime/mirror/class.cc
+++ b/runtime/mirror/class.cc
@@ -257,7 +257,7 @@ void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
count += c->NumReferenceInstanceFieldsDuringLinking();
}
- CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
+ CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count);
}
// Not called within a transaction.
SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
@@ -268,7 +268,7 @@ void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
if (new_reference_offsets != CLASS_WALK_SUPER) {
// Sanity check that the number of bits set in the reference offset bitmap
// agrees with the number of references
- CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
+ CHECK_EQ((size_t)POPCOUNT(new_reference_offsets),
NumReferenceStaticFieldsDuringLinking());
}
// Not called within a transaction.
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 20df78e..fbc0460 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -1010,8 +1010,8 @@ mirror::ArtMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_se
(1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
(1 << art::arm::S30) | (1 << art::arm::S31);
uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
- size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
- __builtin_popcount(fp_spills) /* fprs */ +
+ size_t frame_size = RoundUp((POPCOUNT(core_spills) /* gprs */ +
+ POPCOUNT(fp_spills) /* fprs */ +
1 /* Method* */) * kArmPointerSize, kStackAlignment);
method->SetFrameSizeInBytes(frame_size);
method->SetCoreSpillMask(core_spills);
@@ -1024,7 +1024,7 @@ mirror::ArtMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_se
uint32_t all_spills = (1 << art::mips::S0) | (1 << art::mips::S1);
uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
(type == kSaveAll ? all_spills : 0) | (1 << art::mips::RA);
- size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
+ size_t frame_size = RoundUp((POPCOUNT(core_spills) /* gprs */ +
(type == kRefsAndArgs ? 0 : 3) + 1 /* Method* */) *
kMipsPointerSize, kStackAlignment);
method->SetFrameSizeInBytes(frame_size);
@@ -1035,7 +1035,7 @@ mirror::ArtMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_se
uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
(1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
- size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
+ size_t frame_size = RoundUp((POPCOUNT(core_spills) /* gprs */ +
1 /* Method* */) * kX86PointerSize, kStackAlignment);
method->SetFrameSizeInBytes(frame_size);
method->SetCoreSpillMask(core_spills);
@@ -1054,8 +1054,8 @@ mirror::ArtMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_se
(1 << art::x86_64::XMM3) | (1 << art::x86_64::XMM4) | (1 << art::x86_64::XMM5) |
(1 << art::x86_64::XMM6) | (1 << art::x86_64::XMM7);
uint32_t fp_spills = (type == kRefsAndArgs ? fp_arg_spills : 0);
- size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
- __builtin_popcount(fp_spills) /* fprs */ +
+ size_t frame_size = RoundUp((POPCOUNT(core_spills) /* gprs */ +
+ POPCOUNT(fp_spills) /* fprs */ +
1 /* Method* */) * kX86_64PointerSize, kStackAlignment);
method->SetFrameSizeInBytes(frame_size);
method->SetCoreSpillMask(core_spills);
@@ -1094,8 +1094,8 @@ mirror::ArtMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_se
(1 << art::arm64::D31);
uint32_t fp_spills = fp_ref_spills | (type == kRefsAndArgs ? fp_arg_spills: 0)
| (type == kSaveAll ? fp_all_spills : 0);
- size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
- __builtin_popcount(fp_spills) /* fprs */ +
+ size_t frame_size = RoundUp((POPCOUNT(core_spills) /* gprs */ +
+ POPCOUNT(fp_spills) /* fprs */ +
1 /* Method* */) * kArm64PointerSize, kStackAlignment);
method->SetFrameSizeInBytes(frame_size);
method->SetCoreSpillMask(core_spills);
diff --git a/runtime/stack.h b/runtime/stack.h
index afc4f25..73a823a 100644
--- a/runtime/stack.h
+++ b/runtime/stack.h
@@ -25,6 +25,7 @@
#include "instruction_set.h"
#include "mirror/object.h"
#include "mirror/object_reference.h"
+#include "utils.h"
#include "verify_object.h"
#include <stdint.h>
@@ -638,8 +639,8 @@ class StackVisitor {
size_t frame_size, int reg, InstructionSet isa) {
DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
DCHECK_NE(reg, static_cast<int>(kVRegInvalid));
- int spill_size = __builtin_popcount(core_spills) * GetBytesPerGprSpillLocation(isa)
- + __builtin_popcount(fp_spills) * GetBytesPerFprSpillLocation(isa)
+ int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
+ + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
+ sizeof(uint32_t); // Filler.
int num_ins = code_item->ins_size_;
int num_regs = code_item->registers_size_ - num_ins;
diff --git a/runtime/utils.h b/runtime/utils.h
index 4b2f230..14a532e 100644
--- a/runtime/utils.h
+++ b/runtime/utils.h
@@ -47,7 +47,7 @@ enum TimeUnit {
};
template<typename T>
-static inline bool IsPowerOfTwo(T x) {
+static constexpr bool IsPowerOfTwo(T x) {
return (x & (x - 1)) == 0;
}
@@ -115,39 +115,46 @@ static inline uint32_t High32Bits(uint64_t value) {
}
// A static if which determines whether to return type A or B based on the condition boolean.
-template <const bool condition, typename A, typename B>
+template <bool condition, typename A, typename B>
struct TypeStaticIf {
- typedef A value;
+ typedef A type;
};
// Specialization to handle the false case.
template <typename A, typename B>
struct TypeStaticIf<false, A, B> {
- typedef B value;
+ typedef B type;
+};
+
+// Type identity.
+template <typename T>
+struct TypeIdentity {
+ typedef T type;
};
// For rounding integers.
template<typename T>
-static inline T RoundDown(T x, int n) {
- DCHECK(IsPowerOfTwo(n));
- return (x & -n);
+static constexpr T RoundDown(T x, typename TypeIdentity<T>::type n) {
+ return
+ // DCHECK(IsPowerOfTwo(n)) in a form acceptable in a constexpr function:
+ (kIsDebugBuild && !IsPowerOfTwo(n)) ? (LOG(FATAL) << n << " isn't a power of 2", T(0))
+ : (x & -n);
}
template<typename T>
-static inline T RoundUp(T x, int n) {
+static constexpr T RoundUp(T x, typename TypeIdentity<T>::type n) {
return RoundDown(x + n - 1, n);
}
// For aligning pointers.
template<typename T>
-static inline T* AlignDown(T* x, int n) {
- CHECK(IsPowerOfTwo(n));
- return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(x) & -static_cast<uintptr_t>(n));
+static inline T* AlignDown(T* x, uintptr_t n) {
+ return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n));
}
template<typename T>
-static inline T* AlignUp(T* x, int n) {
- return AlignDown(reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(x) + static_cast<uintptr_t>(n - 1)), n);
+static inline T* AlignUp(T* x, uintptr_t n) {
+ return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n));
}
// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
@@ -162,33 +169,25 @@ static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
return x + 1;
}
-// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
-// figure 5-2, page 66, where the function is called pop.
-static inline int CountOneBits(uint32_t x) {
- x = x - ((x >> 1) & 0x55555555);
- x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
- x = (x + (x >> 4)) & 0x0F0F0F0F;
- x = x + (x >> 8);
- x = x + (x >> 16);
- return static_cast<int>(x & 0x0000003F);
+template<typename T>
+static constexpr int CLZ(T x) {
+ return (sizeof(T) == sizeof(uint32_t))
+ ? __builtin_clz(x)
+ : __builtin_clzll(x);
}
template<typename T>
-static inline int CLZ(T x) {
- if (sizeof(T) == sizeof(uint32_t)) {
- return __builtin_clz(x);
- } else {
- return __builtin_clzll(x);
- }
+static constexpr int CTZ(T x) {
+ return (sizeof(T) == sizeof(uint32_t))
+ ? __builtin_ctz(x)
+ : __builtin_ctzll(x);
}
template<typename T>
-static inline int CTZ(T x) {
- if (sizeof(T) == sizeof(uint32_t)) {
- return __builtin_ctz(x);
- } else {
- return __builtin_ctzll(x);
- }
+static constexpr int POPCOUNT(T x) {
+ return (sizeof(T) == sizeof(uint32_t))
+ ? __builtin_popcount(x)
+ : __builtin_popcountll(x);
}
static inline uint32_t PointerToLowMemUInt32(const void* p) {
diff --git a/runtime/vmap_table.h b/runtime/vmap_table.h
index 2fbaebe..9821753 100644
--- a/runtime/vmap_table.h
+++ b/runtime/vmap_table.h
@@ -99,7 +99,7 @@ class VmapTable {
}
matches++;
}
- CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
+ CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(POPCOUNT(spill_mask)));
uint32_t spill_shifts = 0;
while (matches != (vmap_offset + 1)) {
DCHECK_NE(spill_mask, 0u);