OLD | NEW |
---|---|
(Empty) | |
1 //===- subzero/src/IceUtils.h - Utility functions ---------------*- C++ -*-===// | |
2 // | |
3 // The Subzero Code Generator | |
4 // | |
5 // This file is distributed under the University of Illinois Open Source | |
6 // License. See LICENSE.TXT for details. | |
7 // | |
8 //===----------------------------------------------------------------------===// | |
9 // | |
10 // This file declares some utility functions | |
11 // | |
12 //===----------------------------------------------------------------------===// | |
13 | |
14 #ifndef SUBZERO_SRC_ICEUTILS_H | |
15 #define SUBZERO_SRC_ICEUTILS_H | |
16 | |
17 namespace Ice { | |
18 | |
19 // The number of bits in a byte... | |
20 // TODO(jvoung): de-duplicate this? | |
21 const uint32_t kBitsPerByte = 8; | |
Jim Stichnoth
2014/09/04 20:57:48
C++ CHAR_BIT for translator-time uses, and we also
jvoung (off chromium)
2014/09/08 21:39:00
Done. I removed one use that wasn't used, and adju
| |
22 | |
23 const uint32_t kWordSize = sizeof(uint32_t); | |
Jim Stichnoth
2014/09/04 20:57:48
Make it clear whether this is translator-time or r
jvoung (off chromium)
2014/09/08 21:39:00
Hmm, I need to look into this more. Maybe this sho
| |
24 | |
25 // Similar to bit_cast, but allows copying from types of unrelated | |
26 // sizes. This method was introduced to enable the strict aliasing | |
27 // optimizations of GCC 4.4. Basically, GCC mindlessly relies on | |
28 // obscure details in the C++ standard that make reinterpret_cast | |
29 // virtually useless. | |
30 template <class D, class S> inline D bit_copy(const S &source) { | |
31 D destination; | |
32 // This use of memcpy is safe: source and destination cannot overlap. | |
33 memcpy(&destination, reinterpret_cast<const void *>(&source), | |
34 sizeof(destination)); | |
35 return destination; | |
36 } | |
37 | |
38 class Utils { | |
39 public: | |
40 // Check whether an N-bit two's-complement representation can hold value. | |
41 template <typename T> static inline bool IsInt(int N, T value) { | |
42 assert((0 < N) && | |
43 (static_cast<unsigned int>(N) < (kBitsPerByte * sizeof(value)))); | |
44 T limit = static_cast<T>(1) << (N - 1); | |
45 return (-limit <= value) && (value < limit); | |
46 } | |
47 | |
48 template <typename T> static inline bool IsUint(int N, T value) { | |
49 assert((0 < N) && | |
50 (static_cast<unsigned int>(N) < (kBitsPerByte * sizeof(value)))); | |
51 T limit = static_cast<T>(1) << N; | |
52 return (0 <= value) && (value < limit); | |
53 } | |
54 }; | |
55 | |
56 } // end of namespace Ice | |
57 | |
58 #endif // SUBZERO_SRC_ICEUTILS_H | |
OLD | NEW |