blob: ddecef9856ab3f0fd38442647f4573c093c26c92 [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 Hughes46b92ba2011-08-12 17:57:34 -070099static inline bool NeedsEscaping(uint16_t ch) {
100 return (ch < ' ' || ch > '~');
101}
102
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700103static inline std::string PrintableChar(uint16_t ch) {
104 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700105 result += '\'';
106 if (NeedsEscaping(ch)) {
107 StringAppendF(&result, "\\u%04x", ch);
108 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700109 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700110 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700111 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700112 return result;
113}
114
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700115// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700116template<typename StringT>
117static inline std::string PrintableString(const StringT& s) {
118 std::string result;
119 result += '"';
120 for (typename StringT::iterator it = s.begin(); it != s.end(); ++it) {
121 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700122 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700123 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700124 } else {
125 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700126 }
127 }
128 result += '"';
129 return result;
130}
131
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700132} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700133
134#endif // ART_SRC_UTILS_H_