Renamed jitcraft/ to src/, fixed some /Wp64 warnings.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft 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 * Foobar 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 * JITCraft. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_jitcraft_utility_h_
20 #define _include_jitcraft_utility_h_
29 # pragma warning(disable:4355)
34 static const size_t kMallocAlignment = sizeof(void *) * 2;
36 typedef uint8_t uint8;
37 typedef int32_t int32;
38 typedef uint32_t uint32;
40 typedef uint8 * Address;
42 static const size_t kKB = 1024;
43 static const size_t kMB = 1024 * kKB;
44 static const size_t kGB = 1024 * kMB;
66 void operator =(T *t) {
73 // Bob Jenkin's one-at-a-time hash function[1].
75 // [1] http://burtleburtle.net/bob/hash/doobs.html
76 class CharacterStreamHasher
81 CharacterStreamHasher()
91 void add(const char *s, size_t length) {
92 for (size_t i = 0; i < length; i++)
105 HashCharSequence(const char *s, size_t length)
107 CharacterStreamHasher hasher;
108 hasher.add(s, length);
109 return hasher.result();
120 _BitScanReverse(&rval, number);
122 _BitScanReverse64(&rval, number);
135 FindRightmostBit(size_t number)
142 _BitScanForward(&rval, number);
144 _BitScanForward64(&rval, number);
157 IsPowerOfTwo(size_t value)
161 return !(value & (value - 1));
165 Align(size_t count, size_t alignment)
167 assert(IsPowerOfTwo(alignment));
168 return count + (alignment - (count % alignment)) % alignment;
172 IsUint32AddSafe(unsigned a, unsigned b)
176 size_t log2_a = Log2(a);
177 size_t log2_b = Log2(b);
178 return (log2_a < sizeof(unsigned) * 8) &&
179 (log2_b < sizeof(unsigned) * 8);
183 IsUintPtrAddSafe(size_t a, size_t b)
187 size_t log2_a = Log2(a);
188 size_t log2_b = Log2(b);
189 return (log2_a < sizeof(size_t) * 8) &&
190 (log2_b < sizeof(size_t) * 8);
194 IsUint32MultiplySafe(unsigned a, unsigned b)
196 if (a <= 1 || b <= 1)
199 size_t log2_a = Log2(a);
200 size_t log2_b = Log2(b);
201 return log2_a + log2_b <= sizeof(unsigned) * 8;
205 IsUintPtrMultiplySafe(size_t a, size_t b)
207 if (a <= 1 || b <= 1)
210 size_t log2_a = Log2(a);
211 size_t log2_b = Log2(b);
212 return log2_a + log2_b <= sizeof(size_t) * 8;
215 #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
216 #define STATIC_ASSERT(cond) extern int static_assert_f(int a[(cond) ? 1 : -1])
218 #define IS_ALIGNED(addr, alignment) (!(uintptr_t(addr) & ((alignment) - 1)))
220 template <typename T>
222 IsAligned(T addr, size_t alignment)
224 assert(IsPowerOfTwo(alignment));
225 return !(uintptr_t(addr) & (alignment - 1));
228 static inline Address
229 AlignedBase(Address addr, size_t alignment)
231 assert(IsPowerOfTwo(alignment));
232 return Address(uintptr_t(addr) & ~(alignment - 1));
235 template <typename T> static inline T
236 Min(const T &t1, const T &t2)
238 return t1 < t2 ? t1 : t2;
243 #endif // _include_jitcraft_utility_h_