OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef RUNTIME_PLATFORM_UTILS_H_ | 5 #ifndef RUNTIME_PLATFORM_UTILS_H_ |
6 #define RUNTIME_PLATFORM_UTILS_H_ | 6 #define RUNTIME_PLATFORM_UTILS_H_ |
7 | 7 |
8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
9 #include "platform/globals.h" | 9 #include "platform/globals.h" |
10 | 10 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 static inline bool WillAddOverflow(int64_t a, int64_t b) { | 176 static inline bool WillAddOverflow(int64_t a, int64_t b) { |
177 return ((b > 0) && (a > (kMaxInt64 - b))) || | 177 return ((b > 0) && (a > (kMaxInt64 - b))) || |
178 ((b < 0) && (a < (kMinInt64 - b))); | 178 ((b < 0) && (a < (kMinInt64 - b))); |
179 } | 179 } |
180 | 180 |
181 static inline bool WillSubOverflow(int64_t a, int64_t b) { | 181 static inline bool WillSubOverflow(int64_t a, int64_t b) { |
182 return ((b > 0) && (a < (kMinInt64 + b))) || | 182 return ((b > 0) && (a < (kMinInt64 + b))) || |
183 ((b < 0) && (a > (kMaxInt64 + b))); | 183 ((b < 0) && (a > (kMaxInt64 + b))); |
184 } | 184 } |
185 | 185 |
186 | |
187 // Adds two int64_t values with wrapping around | 186 // Adds two int64_t values with wrapping around |
188 // (two's complement arithmetic). | 187 // (two's complement arithmetic). |
189 static inline int64_t AddWithWrapAround(int64_t a, int64_t b) { | 188 static inline int64_t AddWithWrapAround(int64_t a, int64_t b) { |
190 // Avoid undefined behavior by doing arithmetic in the unsigned type. | 189 // Avoid undefined behavior by doing arithmetic in the unsigned type. |
191 return static_cast<int64_t>(static_cast<uint64_t>(a) + | 190 return static_cast<int64_t>(static_cast<uint64_t>(a) + |
192 static_cast<uint64_t>(b)); | 191 static_cast<uint64_t>(b)); |
193 } | 192 } |
194 | 193 |
195 | |
196 // Subtracts two int64_t values with wrapping around | 194 // Subtracts two int64_t values with wrapping around |
197 // (two's complement arithmetic). | 195 // (two's complement arithmetic). |
198 static inline int64_t SubWithWrapAround(int64_t a, int64_t b) { | 196 static inline int64_t SubWithWrapAround(int64_t a, int64_t b) { |
199 // Avoid undefined behavior by doing arithmetic in the unsigned type. | 197 // Avoid undefined behavior by doing arithmetic in the unsigned type. |
200 return static_cast<int64_t>(static_cast<uint64_t>(a) - | 198 return static_cast<int64_t>(static_cast<uint64_t>(a) - |
201 static_cast<uint64_t>(b)); | 199 static_cast<uint64_t>(b)); |
202 } | 200 } |
203 | 201 |
204 | |
205 // Multiplies two int64_t values with wrapping around | 202 // Multiplies two int64_t values with wrapping around |
206 // (two's complement arithmetic). | 203 // (two's complement arithmetic). |
207 static inline int64_t MulWithWrapAround(int64_t a, int64_t b) { | 204 static inline int64_t MulWithWrapAround(int64_t a, int64_t b) { |
208 // Avoid undefined behavior by doing arithmetic in the unsigned type. | 205 // Avoid undefined behavior by doing arithmetic in the unsigned type. |
209 return static_cast<int64_t>(static_cast<uint64_t>(a) * | 206 return static_cast<int64_t>(static_cast<uint64_t>(a) * |
210 static_cast<uint64_t>(b)); | 207 static_cast<uint64_t>(b)); |
211 } | 208 } |
212 | 209 |
213 | |
214 // Shifts int64_t value left. Supports any non-negative number of bits and | 210 // Shifts int64_t value left. Supports any non-negative number of bits and |
215 // silently discards shifted out bits. | 211 // silently discards shifted out bits. |
216 static inline int64_t ShiftLeftWithTruncation(int64_t a, int64_t b) { | 212 static inline int64_t ShiftLeftWithTruncation(int64_t a, int64_t b) { |
217 ASSERT(b >= 0); | 213 ASSERT(b >= 0); |
218 if (b >= kBitsPerInt64) { | 214 if (b >= kBitsPerInt64) { |
219 return 0; | 215 return 0; |
220 } | 216 } |
221 // Avoid undefined behavior by doing arithmetic in the unsigned type. | 217 // Avoid undefined behavior by doing arithmetic in the unsigned type. |
222 return static_cast<int64_t>(static_cast<uint64_t>(a) << b); | 218 return static_cast<int64_t>(static_cast<uint64_t>(a) << b); |
223 } | 219 } |
224 | 220 |
225 | |
226 // Utility functions for converting values from host endianness to | 221 // Utility functions for converting values from host endianness to |
227 // big or little endian values. | 222 // big or little endian values. |
228 static uint16_t HostToBigEndian16(uint16_t host_value); | 223 static uint16_t HostToBigEndian16(uint16_t host_value); |
229 static uint32_t HostToBigEndian32(uint32_t host_value); | 224 static uint32_t HostToBigEndian32(uint32_t host_value); |
230 static uint64_t HostToBigEndian64(uint64_t host_value); | 225 static uint64_t HostToBigEndian64(uint64_t host_value); |
231 static uint16_t HostToLittleEndian16(uint16_t host_value); | 226 static uint16_t HostToLittleEndian16(uint16_t host_value); |
232 static uint32_t HostToLittleEndian32(uint32_t host_value); | 227 static uint32_t HostToLittleEndian32(uint32_t host_value); |
233 static uint64_t HostToLittleEndian64(uint64_t host_value); | 228 static uint64_t HostToLittleEndian64(uint64_t host_value); |
234 | 229 |
235 static bool DoublesBitEqual(const double a, const double b) { | 230 static bool DoublesBitEqual(const double a, const double b) { |
(...skipping 19 matching lines...) Expand all Loading... |
255 #include "platform/utils_linux.h" | 250 #include "platform/utils_linux.h" |
256 #elif defined(HOST_OS_MACOS) | 251 #elif defined(HOST_OS_MACOS) |
257 #include "platform/utils_macos.h" | 252 #include "platform/utils_macos.h" |
258 #elif defined(HOST_OS_WINDOWS) | 253 #elif defined(HOST_OS_WINDOWS) |
259 #include "platform/utils_win.h" | 254 #include "platform/utils_win.h" |
260 #else | 255 #else |
261 #error Unknown target os. | 256 #error Unknown target os. |
262 #endif | 257 #endif |
263 | 258 |
264 #endif // RUNTIME_PLATFORM_UTILS_H_ | 259 #endif // RUNTIME_PLATFORM_UTILS_H_ |
OLD | NEW |