Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(823)

Side by Side Diff: src/IceUtils.h

Issue 1575873006: Subzero: Fix g++ warnings. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Code review changes Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/IceTypeConverter.h ('k') | src/PNaClTranslator.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- subzero/src/IceUtils.h - Utility functions ---------------*- C++ -*-===// 1 //===- subzero/src/IceUtils.h - Utility functions ---------------*- C++ -*-===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 /// 9 ///
10 /// \file 10 /// \file
11 /// \brief Defines some utility functions. 11 /// \brief Defines some utility functions.
12 /// 12 ///
13 //===----------------------------------------------------------------------===// 13 //===----------------------------------------------------------------------===//
14 14
15 #ifndef SUBZERO_SRC_ICEUTILS_H 15 #ifndef SUBZERO_SRC_ICEUTILS_H
16 #define SUBZERO_SRC_ICEUTILS_H 16 #define SUBZERO_SRC_ICEUTILS_H
17 17
18 #include <climits> 18 #include <climits>
19 #include <cmath> // std::signbit() 19 #include <cmath> // std::signbit()
20 20
21 namespace Ice { 21 namespace Ice {
22 namespace Utils {
22 23
23 /// Similar to bit_cast, but allows copying from types of unrelated sizes. This 24 /// Allows copying from types of unrelated sizes. This method was introduced to
24 /// method was introduced to enable the strict aliasing optimizations of GCC 25 /// enable the strict aliasing optimizations of GCC 4.4. Basically, GCC
25 /// 4.4. Basically, GCC mindlessly relies on obscure details in the C++ standard 26 /// mindlessly relies on obscure details in the C++ standard that make
26 /// that make reinterpret_cast virtually useless. 27 /// reinterpret_cast virtually useless.
27 template <class D, class S> inline D bit_copy(const S &source) { 28 template <typename D, typename S> inline D bitCopy(const S &Source) {
28 D destination; 29 static_assert(sizeof(D) <= sizeof(S),
30 "bitCopy between incompatible type widths");
31 static_assert(!std::is_pointer<S>::value, "");
32 D Destination;
29 // This use of memcpy is safe: source and destination cannot overlap. 33 // This use of memcpy is safe: source and destination cannot overlap.
30 memcpy(&destination, reinterpret_cast<const void *>(&source), 34 memcpy(&Destination, reinterpret_cast<const void *>(&Source), sizeof(D));
31 sizeof(destination)); 35 return Destination;
32 return destination;
33 } 36 }
34 37
35 class Utils { 38 /// Check whether an N-bit two's-complement representation can hold value.
Jim Stichnoth 2016/01/13 19:30:33 Changes here and below are only indentation change
36 Utils() = delete; 39 template <typename T> static inline bool IsInt(int N, T value) {
37 Utils(const Utils &) = delete; 40 assert((0 < N) &&
38 Utils &operator=(const Utils &) = delete; 41 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(value))));
42 T limit = static_cast<T>(1) << (N - 1);
43 return (-limit <= value) && (value < limit);
44 }
39 45
40 public: 46 template <typename T> static inline bool IsUint(int N, T value) {
41 /// Check whether an N-bit two's-complement representation can hold value. 47 assert((0 < N) &&
42 template <typename T> static inline bool IsInt(int N, T value) { 48 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(value))));
43 assert((0 < N) && 49 T limit = static_cast<T>(1) << N;
44 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(value)))); 50 return (0 <= value) && (value < limit);
45 T limit = static_cast<T>(1) << (N - 1); 51 }
46 return (-limit <= value) && (value < limit);
47 }
48 52
49 template <typename T> static inline bool IsUint(int N, T value) { 53 /// Check whether the magnitude of value fits in N bits, i.e., whether an
50 assert((0 < N) && 54 /// (N+1)-bit sign-magnitude representation can hold value.
51 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(value)))); 55 template <typename T> static inline bool IsAbsoluteUint(int N, T Value) {
52 T limit = static_cast<T>(1) << N; 56 assert((0 < N) &&
53 return (0 <= value) && (value < limit); 57 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(Value))));
54 } 58 if (Value < 0)
59 Value = -Value;
60 return IsUint(N, Value);
61 }
55 62
56 /// Check whether the magnitude of value fits in N bits, i.e., whether an 63 /// Return true if the addition X + Y will cause integer overflow for integers
57 /// (N+1)-bit sign-magnitude representation can hold value. 64 /// of type T.
58 template <typename T> static inline bool IsAbsoluteUint(int N, T Value) { 65 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) {
59 assert((0 < N) && 66 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) ||
60 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(Value)))); 67 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y)));
61 if (Value < 0) 68 }
62 Value = -Value;
63 return IsUint(N, Value);
64 }
65 69
66 /// Return true if the addition X + Y will cause integer overflow for integers 70 /// Adds x to y and stores the result in sum. Returns true if the addition
67 /// of type T. 71 /// overflowed.
68 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { 72 static inline bool add_overflow(uint32_t x, uint32_t y, uint32_t *sum) {
69 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) || 73 static_assert(std::is_same<uint32_t, unsigned>::value, "Must match type");
70 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); 74 #if __has_builtin(__builtin_uadd_overflow)
71 } 75 return __builtin_uadd_overflow(x, y, sum);
76 #else
77 *sum = x + y;
78 return WouldOverflowAdd(x, y);
79 #endif
80 }
72 81
73 /// Adds x to y and stores the result in sum. Returns true if the addition 82 /// Return true if X is already aligned by N, where N is a power of 2.
74 /// overflowed. 83 template <typename T> static inline bool IsAligned(T X, intptr_t N) {
75 static inline bool add_overflow(uint32_t x, uint32_t y, uint32_t *sum) { 84 assert(llvm::isPowerOf2_64(N));
76 static_assert(std::is_same<uint32_t, unsigned>::value, "Must match type"); 85 return (X & (N - 1)) == 0;
77 #if __has_builtin(__builtin_uadd_overflow) 86 }
78 return __builtin_uadd_overflow(x, y, sum);
79 #else
80 *sum = x + y;
81 return WouldOverflowAdd(x, y);
82 #endif
83 }
84 87
85 /// Return true if X is already aligned by N, where N is a power of 2. 88 /// Return Value adjusted to the next highest multiple of Alignment.
86 template <typename T> static inline bool IsAligned(T X, intptr_t N) { 89 static inline uint32_t applyAlignment(uint32_t Value, uint32_t Alignment) {
87 assert(llvm::isPowerOf2_64(N)); 90 assert(llvm::isPowerOf2_32(Alignment));
88 return (X & (N - 1)) == 0; 91 return (Value + Alignment - 1) & -Alignment;
89 } 92 }
90 93
91 /// Return Value adjusted to the next highest multiple of Alignment. 94 /// Return amount which must be added to adjust Pos to the next highest
92 static inline uint32_t applyAlignment(uint32_t Value, uint32_t Alignment) { 95 /// multiple of Align.
93 assert(llvm::isPowerOf2_32(Alignment)); 96 static inline uint64_t OffsetToAlignment(uint64_t Pos, uint64_t Align) {
94 return (Value + Alignment - 1) & -Alignment; 97 assert(llvm::isPowerOf2_64(Align));
95 } 98 uint64_t Mod = Pos & (Align - 1);
99 if (Mod == 0)
100 return 0;
101 return Align - Mod;
102 }
96 103
97 /// Return amount which must be added to adjust Pos to the next highest 104 /// Rotate the value bit pattern to the left by shift bits.
98 /// multiple of Align. 105 /// Precondition: 0 <= shift < 32
99 static inline uint64_t OffsetToAlignment(uint64_t Pos, uint64_t Align) { 106 static inline uint32_t rotateLeft32(uint32_t value, uint32_t shift) {
100 assert(llvm::isPowerOf2_64(Align)); 107 if (shift == 0)
101 uint64_t Mod = Pos & (Align - 1); 108 return value;
102 if (Mod == 0) 109 return (value << shift) | (value >> (32 - shift));
103 return 0; 110 }
104 return Align - Mod;
105 }
106 111
107 /// Rotate the value bit pattern to the left by shift bits. 112 /// Rotate the value bit pattern to the right by shift bits.
108 /// Precondition: 0 <= shift < 32 113 static inline uint32_t rotateRight32(uint32_t value, uint32_t shift) {
109 static inline uint32_t rotateLeft32(uint32_t value, uint32_t shift) { 114 if (shift == 0)
110 if (shift == 0) 115 return value;
111 return value; 116 return (value >> shift) | (value << (32 - shift));
112 return (value << shift) | (value >> (32 - shift)); 117 }
113 }
114 118
115 /// Rotate the value bit pattern to the right by shift bits. 119 /// Returns true if Val is +0.0. It requires T to be a floating point type.
116 static inline uint32_t rotateRight32(uint32_t value, uint32_t shift) { 120 template <typename T> static bool isPositiveZero(T Val) {
117 if (shift == 0) 121 static_assert(std::is_floating_point<T>::value,
118 return value; 122 "Input type must be floating point");
119 return (value >> shift) | (value << (32 - shift)); 123 return Val == 0 && !std::signbit(Val);
120 } 124 }
121 125
122 /// Returns true if Val is +0.0. It requires T to be a floating point type. 126 } // end of namespace Utils
123 template <typename T> static bool isPositiveZero(T Val) {
124 static_assert(std::is_floating_point<T>::value,
125 "Input type must be floating point");
126 return Val == 0 && !std::signbit(Val);
127 }
128 };
129
130 } // end of namespace Ice 127 } // end of namespace Ice
131 128
132 #endif // SUBZERO_SRC_ICEUTILS_H 129 #endif // SUBZERO_SRC_ICEUTILS_H
OLDNEW
« no previous file with comments | « src/IceTypeConverter.h ('k') | src/PNaClTranslator.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698