Chromium Code Reviews| Index: src/IceUtils.h |
| diff --git a/src/IceUtils.h b/src/IceUtils.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..acb409cdd77bd5760d877ec40037c46d64128445 |
| --- /dev/null |
| +++ b/src/IceUtils.h |
| @@ -0,0 +1,58 @@ |
| +//===- subzero/src/IceUtils.h - Utility functions ---------------*- C++ -*-===// |
| +// |
| +// The Subzero Code Generator |
| +// |
| +// This file is distributed under the University of Illinois Open Source |
| +// License. See LICENSE.TXT for details. |
| +// |
| +//===----------------------------------------------------------------------===// |
| +// |
| +// This file declares some utility functions |
| +// |
| +//===----------------------------------------------------------------------===// |
| + |
| +#ifndef SUBZERO_SRC_ICEUTILS_H |
| +#define SUBZERO_SRC_ICEUTILS_H |
| + |
| +namespace Ice { |
| + |
| +// The number of bits in a byte... |
| +// TODO(jvoung): de-duplicate this? |
| +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
|
| + |
| +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
|
| + |
| +// Similar to bit_cast, but allows copying from types of unrelated |
| +// sizes. This method was introduced to enable the strict aliasing |
| +// optimizations of GCC 4.4. Basically, GCC mindlessly relies on |
| +// obscure details in the C++ standard that make reinterpret_cast |
| +// virtually useless. |
| +template <class D, class S> inline D bit_copy(const S &source) { |
| + D destination; |
| + // This use of memcpy is safe: source and destination cannot overlap. |
| + memcpy(&destination, reinterpret_cast<const void *>(&source), |
| + sizeof(destination)); |
| + return destination; |
| +} |
| + |
| +class Utils { |
| +public: |
| + // Check whether an N-bit two's-complement representation can hold value. |
| + template <typename T> static inline bool IsInt(int N, T value) { |
| + assert((0 < N) && |
| + (static_cast<unsigned int>(N) < (kBitsPerByte * sizeof(value)))); |
| + T limit = static_cast<T>(1) << (N - 1); |
| + return (-limit <= value) && (value < limit); |
| + } |
| + |
| + template <typename T> static inline bool IsUint(int N, T value) { |
| + assert((0 < N) && |
| + (static_cast<unsigned int>(N) < (kBitsPerByte * sizeof(value)))); |
| + T limit = static_cast<T>(1) << N; |
| + return (0 <= value) && (value < limit); |
| + } |
| +}; |
| + |
| +} // end of namespace Ice |
| + |
| +#endif // SUBZERO_SRC_ICEUTILS_H |