Chromium Code Reviews| 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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 | 186 |
| 187 // Adds two int64_t values with wrapping around | |
| 188 // (two's complement arithmetic). | |
| 189 static inline int64_t AddWithWrapAround(int64_t a, int64_t b) { | |
| 190 // Avoid UB by doing arithmetic in the unsigned type. | |
|
regis
2017/07/07 20:10:30
What does UB stand for?
alexmarkov
2017/07/07 20:46:38
UB = Undefined Behavior. Do you think I should avo
regis
2017/07/07 21:46:52
You can spell it out for clarity. I did not know U
alexmarkov
2017/07/07 22:28:44
Changed UB to "undefined behavior".
As far as I k
regis
2017/07/07 22:38:25
Two's complement arithmetic can ignore the sign fo
alexmarkov
2017/07/07 23:17:58
I agree about division, but still curious about mu
| |
| 191 return static_cast<int64_t>(static_cast<uint64_t>(a) + | |
| 192 static_cast<uint64_t>(b)); | |
| 193 } | |
| 194 | |
| 195 | |
| 196 // Subtracts two int64_t values with wrapping around | |
| 197 // (two's complement arithmetic). | |
| 198 static inline int64_t SubWithWrapAround(int64_t a, int64_t b) { | |
| 199 // Avoid UB by doing arithmetic in the unsigned type. | |
| 200 return static_cast<int64_t>(static_cast<uint64_t>(a) - | |
| 201 static_cast<uint64_t>(b)); | |
| 202 } | |
| 203 | |
| 204 | |
| 205 // Multiplies two int64_t values with wrapping around | |
| 206 // (two's complement arithmetic). | |
| 207 static inline int64_t MulWithWrapAround(int64_t a, int64_t b) { | |
| 208 // Avoid UB by doing arithmetic in the unsigned type. | |
| 209 return static_cast<int64_t>(static_cast<uint64_t>(a) * | |
| 210 static_cast<uint64_t>(b)); | |
| 211 } | |
| 212 | |
| 213 | |
| 214 // Shifts int64_t value left. Supports any non-negative number of bits and | |
| 215 // silently discards shifted out bits. | |
| 216 static inline int64_t ShiftLeftWithTruncation(int64_t a, int64_t b) { | |
| 217 ASSERT(b >= 0); | |
| 218 if (b >= kBitsPerInt64) { | |
| 219 return 0; | |
| 220 } | |
| 221 // Avoid UB by doing arithmetic in the unsigned type. | |
| 222 return static_cast<int64_t>(static_cast<uint64_t>(a) << b); | |
| 223 } | |
| 224 | |
| 225 | |
| 187 // Utility functions for converting values from host endianness to | 226 // Utility functions for converting values from host endianness to |
| 188 // big or little endian values. | 227 // big or little endian values. |
| 189 static uint16_t HostToBigEndian16(uint16_t host_value); | 228 static uint16_t HostToBigEndian16(uint16_t host_value); |
| 190 static uint32_t HostToBigEndian32(uint32_t host_value); | 229 static uint32_t HostToBigEndian32(uint32_t host_value); |
| 191 static uint64_t HostToBigEndian64(uint64_t host_value); | 230 static uint64_t HostToBigEndian64(uint64_t host_value); |
| 192 static uint16_t HostToLittleEndian16(uint16_t host_value); | 231 static uint16_t HostToLittleEndian16(uint16_t host_value); |
| 193 static uint32_t HostToLittleEndian32(uint32_t host_value); | 232 static uint32_t HostToLittleEndian32(uint32_t host_value); |
| 194 static uint64_t HostToLittleEndian64(uint64_t host_value); | 233 static uint64_t HostToLittleEndian64(uint64_t host_value); |
| 195 | 234 |
| 196 static bool DoublesBitEqual(const double a, const double b) { | 235 static bool DoublesBitEqual(const double a, const double b) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 216 #include "platform/utils_linux.h" | 255 #include "platform/utils_linux.h" |
| 217 #elif defined(HOST_OS_MACOS) | 256 #elif defined(HOST_OS_MACOS) |
| 218 #include "platform/utils_macos.h" | 257 #include "platform/utils_macos.h" |
| 219 #elif defined(HOST_OS_WINDOWS) | 258 #elif defined(HOST_OS_WINDOWS) |
| 220 #include "platform/utils_win.h" | 259 #include "platform/utils_win.h" |
| 221 #else | 260 #else |
| 222 #error Unknown target os. | 261 #error Unknown target os. |
| 223 #endif | 262 #endif |
| 224 | 263 |
| 225 #endif // RUNTIME_PLATFORM_UTILS_H_ | 264 #endif // RUNTIME_PLATFORM_UTILS_H_ |
| OLD | NEW |