Add ref-expr test.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
7 * SourcePawn is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * SourcePawn. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_jitcraft_utility_h_
20 #define _include_jitcraft_utility_h_
24 #include "../include/keima_util.h"
30 # pragma warning(disable:4355)
35 static const size_t kMallocAlignment = sizeof(void *) * 2;
37 typedef uint8_t uint8;
38 typedef int32_t int32;
39 typedef uint32_t uint32;
40 typedef int64_t int64;
41 typedef uint64_t uint64;
43 typedef uint8 * Address;
45 static const size_t kKB = 1024;
46 static const size_t kMB = 1024 * kKB;
47 static const size_t kGB = 1024 * kMB;
69 void operator =(T *t) {
76 // Bob Jenkin's one-at-a-time hash function[1].
78 // [1] http://burtleburtle.net/bob/hash/doobs.html
79 class CharacterStreamHasher
84 CharacterStreamHasher()
94 void add(const char *s, size_t length) {
95 for (size_t i = 0; i < length; i++)
101 hash ^= (hash >> 11);
102 hash += (hash << 15);
108 HashCharSequence(const char *s, size_t length)
110 CharacterStreamHasher hasher;
111 hasher.add(s, length);
112 return hasher.result();
115 // From http://burtleburtle.net/bob/hash/integer.html
119 a = (a ^ 61) ^ (a >> 16);
127 // From http://www.cris.com/~Ttwang/tech/inthash.htm
131 key = (~key) + (key << 18); // key = (key << 18) - key - 1;
132 key = key ^ (uint64(key) >> 31);
133 key = key * 21; // key = (key + (key << 2)) + (key << 4);
134 key = key ^ (uint64(key) >> 11);
135 key = key + (key << 6);
136 key = key ^ (uint64(key) >> 22);
143 #if defined(KE_32BIT)
144 return HashInt32(reinterpret_cast<int32>(p));
145 #elif defined(KE_64BIT)
146 return HashInt64(reinterpret_cast<int64>(p));
158 _BitScanReverse(&rval, number);
160 _BitScanReverse64(&rval, number);
173 FindRightmostBit(size_t number)
180 _BitScanForward(&rval, number);
182 _BitScanForward64(&rval, number);
195 IsPowerOfTwo(size_t value)
199 return !(value & (value - 1));
203 Align(size_t count, size_t alignment)
205 assert(IsPowerOfTwo(alignment));
206 return count + (alignment - (count % alignment)) % alignment;
210 IsUint32AddSafe(unsigned a, unsigned b)
214 size_t log2_a = Log2(a);
215 size_t log2_b = Log2(b);
216 return (log2_a < sizeof(unsigned) * 8) &&
217 (log2_b < sizeof(unsigned) * 8);
221 IsUintPtrAddSafe(size_t a, size_t b)
225 size_t log2_a = Log2(a);
226 size_t log2_b = Log2(b);
227 return (log2_a < sizeof(size_t) * 8) &&
228 (log2_b < sizeof(size_t) * 8);
232 IsUint32MultiplySafe(unsigned a, unsigned b)
234 if (a <= 1 || b <= 1)
237 size_t log2_a = Log2(a);
238 size_t log2_b = Log2(b);
239 return log2_a + log2_b <= sizeof(unsigned) * 8;
243 IsUintPtrMultiplySafe(size_t a, size_t b)
245 if (a <= 1 || b <= 1)
248 size_t log2_a = Log2(a);
249 size_t log2_b = Log2(b);
250 return log2_a + log2_b <= sizeof(size_t) * 8;
253 #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
254 #define STATIC_ASSERT(cond) extern int static_assert_f(int a[(cond) ? 1 : -1])
256 #define IS_ALIGNED(addr, alignment) (!(uintptr_t(addr) & ((alignment) - 1)))
258 template <typename T>
260 IsAligned(T addr, size_t alignment)
262 assert(IsPowerOfTwo(alignment));
263 return !(uintptr_t(addr) & (alignment - 1));
266 static inline Address
267 AlignedBase(Address addr, size_t alignment)
269 assert(IsPowerOfTwo(alignment));
270 return Address(uintptr_t(addr) & ~(alignment - 1));
273 template <typename T> static inline T
274 Min(const T &t1, const T &t2)
276 return t1 < t2 ? t1 : t2;
279 #define OFFSETOF(Class, Member) reinterpret_cast<size_t>(&((Class *)NULL)->Member)
283 #endif // _include_jitcraft_utility_h_