blob: 842db63a26ea957677386e70f60cf78682277efc [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"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07007#include "logging.h"
Elliott Hughesc7ac37f2011-08-12 12:21:58 -07008#include "stringprintf.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07009
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070010namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070011
Carl Shapiroa2e18e12011-06-21 18:57:55 -070012template<typename T>
13static inline bool IsPowerOfTwo(T x) {
14 return (x & (x - 1)) == 0;
15}
16
Carl Shapiroa2e18e12011-06-21 18:57:55 -070017template<typename T>
18static inline bool IsAligned(T x, int n) {
19 CHECK(IsPowerOfTwo(n));
20 return (x & (n - 1)) == 0;
21}
22
Carl Shapiroa2e18e12011-06-21 18:57:55 -070023template<typename T>
24static inline bool IsAligned(T* x, int n) {
25 return IsAligned(reinterpret_cast<uintptr_t>(x), n);
26}
27
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070028// Check whether an N-bit two's-complement representation can hold value.
29static inline bool IsInt(int N, word value) {
30 CHECK_LT(0, N);
31 CHECK_LT(N, kBitsPerWord);
32 word limit = static_cast<word>(1) << (N - 1);
33 return (-limit <= value) && (value < limit);
34}
35
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070036static inline bool IsUint(int N, word value) {
37 CHECK_LT(0, N);
38 CHECK_LT(N, kBitsPerWord);
39 word limit = static_cast<word>(1) << N;
40 return (0 <= value) && (value < limit);
41}
42
Carl Shapiroa2e18e12011-06-21 18:57:55 -070043static inline bool IsAbsoluteUint(int N, word value) {
44 CHECK_LT(0, N);
45 CHECK_LT(N, kBitsPerWord);
46 if (value < 0) value = -value;
47 return IsUint(N, value);
48}
49
Ian Rogersb033c752011-07-20 12:22:35 -070050static inline int32_t Low16Bits(int32_t value) {
51 return static_cast<int32_t>(value & 0xffff);
52}
53
54static inline int32_t High16Bits(int32_t value) {
55 return static_cast<int32_t>(value >> 16);
56}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070057
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070058static inline int32_t Low32Bits(int64_t value) {
59 return static_cast<int32_t>(value);
60}
61
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070062static inline int32_t High32Bits(int64_t value) {
63 return static_cast<int32_t>(value >> 32);
64}
65
Carl Shapiro61e019d2011-07-14 16:53:09 -070066template<typename T>
67static inline T RoundDown(T x, int n) {
68 CHECK(IsPowerOfTwo(n));
69 return (x & -n);
70}
71
72template<typename T>
73static inline T RoundUp(T x, int n) {
74 return RoundDown(x + n - 1, n);
75}
76
Carl Shapiroa2e18e12011-06-21 18:57:55 -070077// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070078// figure 3-3, page 48, where the function is called clp2.
79static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
80 x = x - 1;
81 x = x | (x >> 1);
82 x = x | (x >> 2);
83 x = x | (x >> 4);
84 x = x | (x >> 8);
85 x = x | (x >> 16);
86 return x + 1;
87}
88
89// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiroa2e18e12011-06-21 18:57:55 -070090// figure 5-2, page 66, where the function is called pop.
91static inline int CountOneBits(uint32_t x) {
92 x = x - ((x >> 1) & 0x55555555);
93 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
94 x = (x + (x >> 4)) & 0x0F0F0F0F;
95 x = x + (x >> 8);
96 x = x + (x >> 16);
97 return static_cast<int>(x & 0x0000003F);
98}
99
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700100#define CLZ(x) __builtin_clz(x)
101
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700102static inline bool NeedsEscaping(uint16_t ch) {
103 return (ch < ' ' || ch > '~');
104}
105
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700106static inline std::string PrintableChar(uint16_t ch) {
107 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700108 result += '\'';
109 if (NeedsEscaping(ch)) {
110 StringAppendF(&result, "\\u%04x", ch);
111 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700112 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700113 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700114 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700115 return result;
116}
117
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700118// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700119template<typename StringT>
120static inline std::string PrintableString(const StringT& s) {
121 std::string result;
122 result += '"';
123 for (typename StringT::iterator it = s.begin(); it != s.end(); ++it) {
124 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700125 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700126 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700127 } else {
128 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700129 }
130 }
131 result += '"';
132 return result;
133}
134
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700135} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700136
137#endif // ART_SRC_UTILS_H_