blob: 217bd6927406abafefd0de69141a1a90da5fd945 [file] [log] [blame]
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_UTILS_H_
4#define ART_SRC_UTILS_H_
5
6#include "src/globals.h"
7
Carl Shapiro6b6b5f02011-06-21 15:05:09 -07008namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07009
Carl Shapiroa2e18e12011-06-21 18:57:55 -070010template<typename T>
11static inline bool IsPowerOfTwo(T x) {
12 return (x & (x - 1)) == 0;
13}
14
Carl Shapiroa2e18e12011-06-21 18:57:55 -070015template<typename T>
16static inline bool IsAligned(T x, int n) {
17 CHECK(IsPowerOfTwo(n));
18 return (x & (n - 1)) == 0;
19}
20
Carl Shapiroa2e18e12011-06-21 18:57:55 -070021template<typename T>
22static inline bool IsAligned(T* x, int n) {
23 return IsAligned(reinterpret_cast<uintptr_t>(x), n);
24}
25
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070026// Check whether an N-bit two's-complement representation can hold value.
27static inline bool IsInt(int N, word value) {
28 CHECK_LT(0, N);
29 CHECK_LT(N, kBitsPerWord);
30 word limit = static_cast<word>(1) << (N - 1);
31 return (-limit <= value) && (value < limit);
32}
33
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070034static inline bool IsUint(int N, word value) {
35 CHECK_LT(0, N);
36 CHECK_LT(N, kBitsPerWord);
37 word limit = static_cast<word>(1) << N;
38 return (0 <= value) && (value < limit);
39}
40
Carl Shapiroa2e18e12011-06-21 18:57:55 -070041static inline bool IsAbsoluteUint(int N, word value) {
42 CHECK_LT(0, N);
43 CHECK_LT(N, kBitsPerWord);
44 if (value < 0) value = -value;
45 return IsUint(N, value);
46}
47
Ian Rogersb033c752011-07-20 12:22:35 -070048static inline int32_t Low16Bits(int32_t value) {
49 return static_cast<int32_t>(value & 0xffff);
50}
51
52static inline int32_t High16Bits(int32_t value) {
53 return static_cast<int32_t>(value >> 16);
54}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070055
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070056static inline int32_t Low32Bits(int64_t value) {
57 return static_cast<int32_t>(value);
58}
59
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070060static inline int32_t High32Bits(int64_t value) {
61 return static_cast<int32_t>(value >> 32);
62}
63
Carl Shapiro61e019d2011-07-14 16:53:09 -070064template<typename T>
65static inline T RoundDown(T x, int n) {
66 CHECK(IsPowerOfTwo(n));
67 return (x & -n);
68}
69
70template<typename T>
71static inline T RoundUp(T x, int n) {
72 return RoundDown(x + n - 1, n);
73}
74
Carl Shapiroa2e18e12011-06-21 18:57:55 -070075// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070076// figure 3-3, page 48, where the function is called clp2.
77static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
78 x = x - 1;
79 x = x | (x >> 1);
80 x = x | (x >> 2);
81 x = x | (x >> 4);
82 x = x | (x >> 8);
83 x = x | (x >> 16);
84 return x + 1;
85}
86
87// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiroa2e18e12011-06-21 18:57:55 -070088// figure 5-2, page 66, where the function is called pop.
89static inline int CountOneBits(uint32_t x) {
90 x = x - ((x >> 1) & 0x55555555);
91 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
92 x = (x + (x >> 4)) & 0x0F0F0F0F;
93 x = x + (x >> 8);
94 x = x + (x >> 16);
95 return static_cast<int>(x & 0x0000003F);
96}
97
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070098} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070099
100#endif // ART_SRC_UTILS_H_