Fix warnings when building with clang.
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_
25 #include "../include/keima_util.h"
31 # pragma warning(disable:4355)
36 static const size_t kMallocAlignment = sizeof(void *) * 2;
38 typedef uint8_t uint8;
39 typedef int32_t int32;
40 typedef uint32_t uint32;
41 typedef int64_t int64;
42 typedef uint64_t uint64;
44 typedef uint8 * Address;
46 static const size_t kKB = 1024;
47 static const size_t kMB = 1024 * kKB;
48 static const size_t kGB = 1024 * kMB;
72 T *operator *() const {
75 void operator =(T *t) {
82 // Bob Jenkin's one-at-a-time hash function[1].
84 // [1] http://burtleburtle.net/bob/hash/doobs.html
85 class CharacterStreamHasher
90 CharacterStreamHasher()
100 void add(const char *s, size_t length) {
101 for (size_t i = 0; i < length; i++)
107 hash ^= (hash >> 11);
108 hash += (hash << 15);
114 HashCharSequence(const char *s, size_t length)
116 CharacterStreamHasher hasher;
117 hasher.add(s, length);
118 return hasher.result();
121 // From http://burtleburtle.net/bob/hash/integer.html
125 a = (a ^ 61) ^ (a >> 16);
133 // From http://www.cris.com/~Ttwang/tech/inthash.htm
137 key = (~key) + (key << 18); // key = (key << 18) - key - 1;
138 key = key ^ (uint64(key) >> 31);
139 key = key * 21; // key = (key + (key << 2)) + (key << 4);
140 key = key ^ (uint64(key) >> 11);
141 key = key + (key << 6);
142 key = key ^ (uint64(key) >> 22);
149 #if defined(KE_32BIT)
150 return HashInt32(reinterpret_cast<int32>(p));
151 #elif defined(KE_64BIT)
152 return HashInt64(reinterpret_cast<int64>(p));
164 _BitScanReverse(&rval, number);
166 _BitScanReverse64(&rval, number);
179 FindRightmostBit(size_t number)
186 _BitScanForward(&rval, number);
188 _BitScanForward64(&rval, number);
201 IsPowerOfTwo(size_t value)
205 return !(value & (value - 1));
209 Align(size_t count, size_t alignment)
211 assert(IsPowerOfTwo(alignment));
212 return count + (alignment - (count % alignment)) % alignment;
216 IsUint32AddSafe(unsigned a, unsigned b)
220 size_t log2_a = Log2(a);
221 size_t log2_b = Log2(b);
222 return (log2_a < sizeof(unsigned) * 8) &&
223 (log2_b < sizeof(unsigned) * 8);
227 IsUintPtrAddSafe(size_t a, size_t b)
231 size_t log2_a = Log2(a);
232 size_t log2_b = Log2(b);
233 return (log2_a < sizeof(size_t) * 8) &&
234 (log2_b < sizeof(size_t) * 8);
238 IsUint32MultiplySafe(unsigned a, unsigned b)
240 if (a <= 1 || b <= 1)
243 size_t log2_a = Log2(a);
244 size_t log2_b = Log2(b);
245 return log2_a + log2_b <= sizeof(unsigned) * 8;
249 IsUintPtrMultiplySafe(size_t a, size_t b)
251 if (a <= 1 || b <= 1)
254 size_t log2_a = Log2(a);
255 size_t log2_b = Log2(b);
256 return log2_a + log2_b <= sizeof(size_t) * 8;
259 #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
260 #define STATIC_ASSERT(cond) extern int static_assert_f(int a[(cond) ? 1 : -1])
262 #define IS_ALIGNED(addr, alignment) (!(uintptr_t(addr) & ((alignment) - 1)))
264 template <typename T>
266 IsAligned(T addr, size_t alignment)
268 assert(IsPowerOfTwo(alignment));
269 return !(uintptr_t(addr) & (alignment - 1));
272 static inline Address
273 AlignedBase(Address addr, size_t alignment)
275 assert(IsPowerOfTwo(alignment));
276 return Address(uintptr_t(addr) & ~(alignment - 1));
279 template <typename T> static inline T
280 Min(const T &t1, const T &t2)
282 return t1 < t2 ? t1 : t2;
285 template <typename T> static inline T
286 Max(const T &t1, const T &t2)
288 return t1 > t2 ? t1 : t2;
291 #define OFFSETOF(Class, Member) reinterpret_cast<size_t>(&((Class *)NULL)->Member)
295 #endif // _include_jitcraft_utility_h_