blob: 67038cfc4a26ece666b0c68752bb9d1676a9eb8c [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
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "globals.h"
Elliott Hughesc7ac37f2011-08-12 12:21:58 -07007#include "stringprintf.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07008
Carl Shapiro6b6b5f02011-06-21 15:05:09 -07009namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070010
Carl Shapiroa2e18e12011-06-21 18:57:55 -070011template<typename T>
12static inline bool IsPowerOfTwo(T x) {
13 return (x & (x - 1)) == 0;
14}
15
Carl Shapiroa2e18e12011-06-21 18:57:55 -070016template<typename T>
17static inline bool IsAligned(T x, int n) {
18 CHECK(IsPowerOfTwo(n));
19 return (x & (n - 1)) == 0;
20}
21
Carl Shapiroa2e18e12011-06-21 18:57:55 -070022template<typename T>
23static inline bool IsAligned(T* x, int n) {
24 return IsAligned(reinterpret_cast<uintptr_t>(x), n);
25}
26
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070027// Check whether an N-bit two's-complement representation can hold value.
28static inline bool IsInt(int N, word value) {
29 CHECK_LT(0, N);
30 CHECK_LT(N, kBitsPerWord);
31 word limit = static_cast<word>(1) << (N - 1);
32 return (-limit <= value) && (value < limit);
33}
34
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070035static inline bool IsUint(int N, word value) {
36 CHECK_LT(0, N);
37 CHECK_LT(N, kBitsPerWord);
38 word limit = static_cast<word>(1) << N;
39 return (0 <= value) && (value < limit);
40}
41
Carl Shapiroa2e18e12011-06-21 18:57:55 -070042static inline bool IsAbsoluteUint(int N, word value) {
43 CHECK_LT(0, N);
44 CHECK_LT(N, kBitsPerWord);
45 if (value < 0) value = -value;
46 return IsUint(N, value);
47}
48
Ian Rogersb033c752011-07-20 12:22:35 -070049static inline int32_t Low16Bits(int32_t value) {
50 return static_cast<int32_t>(value & 0xffff);
51}
52
53static inline int32_t High16Bits(int32_t value) {
54 return static_cast<int32_t>(value >> 16);
55}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070056
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070057static inline int32_t Low32Bits(int64_t value) {
58 return static_cast<int32_t>(value);
59}
60
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070061static inline int32_t High32Bits(int64_t value) {
62 return static_cast<int32_t>(value >> 32);
63}
64
Carl Shapiro61e019d2011-07-14 16:53:09 -070065template<typename T>
66static inline T RoundDown(T x, int n) {
67 CHECK(IsPowerOfTwo(n));
68 return (x & -n);
69}
70
71template<typename T>
72static inline T RoundUp(T x, int n) {
73 return RoundDown(x + n - 1, n);
74}
75
Carl Shapiroa2e18e12011-06-21 18:57:55 -070076// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070077// figure 3-3, page 48, where the function is called clp2.
78static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
79 x = x - 1;
80 x = x | (x >> 1);
81 x = x | (x >> 2);
82 x = x | (x >> 4);
83 x = x | (x >> 8);
84 x = x | (x >> 16);
85 return x + 1;
86}
87
88// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiroa2e18e12011-06-21 18:57:55 -070089// figure 5-2, page 66, where the function is called pop.
90static inline int CountOneBits(uint32_t x) {
91 x = x - ((x >> 1) & 0x55555555);
92 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
93 x = (x + (x >> 4)) & 0x0F0F0F0F;
94 x = x + (x >> 8);
95 x = x + (x >> 16);
96 return static_cast<int>(x & 0x0000003F);
97}
98
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070099static inline std::string PrintableChar(uint16_t ch) {
100 std::string result;
101 if (ch >= ' ' && ch <= '~') {
102 // ASCII.
103 result += '\'';
104 result += ch;
105 result += '\'';
106 return result;
107 }
108 // Non-ASCII; show the code point.
109 StringAppendF(&result, "'\\u%04x'", ch);
110 return result;
111}
112
113template<typename StringT>
114static inline std::string PrintableString(const StringT& s) {
115 std::string result;
116 result += '"';
117 for (typename StringT::iterator it = s.begin(); it != s.end(); ++it) {
118 char ch = *it;
119 if (ch >= ' ' && ch <= '~') {
120 result += ch;
121 } else {
122 StringAppendF(&result, "\\x%02x", ch & 0xff);
123 }
124 }
125 result += '"';
126 return result;
127}
128
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700129} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700130
131#endif // ART_SRC_UTILS_H_