OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_BASE_BITS_H_ | 5 #ifndef V8_BASE_BITS_H_ |
6 #define V8_BASE_BITS_H_ | 6 #define V8_BASE_BITS_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include "src/base/macros.h" | 9 #include "src/base/macros.h" |
10 #if V8_CC_MSVC | 10 #if V8_CC_MSVC |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 // is minint and |rhs| is -1, it returns minint. | 229 // is minint and |rhs| is -1, it returns minint. |
230 int32_t SignedDiv32(int32_t lhs, int32_t rhs); | 230 int32_t SignedDiv32(int32_t lhs, int32_t rhs); |
231 | 231 |
232 | 232 |
233 // SignedMod32(lhs, rhs) divides |lhs| by |rhs| and returns the remainder | 233 // SignedMod32(lhs, rhs) divides |lhs| by |rhs| and returns the remainder |
234 // truncated to int32. If either |rhs| is zero or |lhs| is minint and |rhs| | 234 // truncated to int32. If either |rhs| is zero or |lhs| is minint and |rhs| |
235 // is -1, it returns zero. | 235 // is -1, it returns zero. |
236 int32_t SignedMod32(int32_t lhs, int32_t rhs); | 236 int32_t SignedMod32(int32_t lhs, int32_t rhs); |
237 | 237 |
238 | 238 |
| 239 // UnsignedAddOverflow32(lhs,rhs,val) performs an unsigned summation of |lhs| |
| 240 // and |rhs| and stores the result into the variable pointed to by |val| and |
| 241 // returns true if the unsigned summation resulted in an overflow. |
| 242 inline bool UnsignedAddOverflow32(uint32_t lhs, uint32_t rhs, uint32_t* val) { |
| 243 #if V8_HAS_BUILTIN_SADD_OVERFLOW |
| 244 return __builtin_uadd_overflow(lhs, rhs, val); |
| 245 #else |
| 246 *val = lhs + rhs; |
| 247 return *val < (lhs | rhs); |
| 248 #endif |
| 249 } |
| 250 |
| 251 |
239 // UnsignedDiv32(lhs, rhs) divides |lhs| by |rhs| and returns the quotient | 252 // UnsignedDiv32(lhs, rhs) divides |lhs| by |rhs| and returns the quotient |
240 // truncated to uint32. If |rhs| is zero, then zero is returned. | 253 // truncated to uint32. If |rhs| is zero, then zero is returned. |
241 inline uint32_t UnsignedDiv32(uint32_t lhs, uint32_t rhs) { | 254 inline uint32_t UnsignedDiv32(uint32_t lhs, uint32_t rhs) { |
242 return rhs ? lhs / rhs : 0u; | 255 return rhs ? lhs / rhs : 0u; |
243 } | 256 } |
244 | 257 |
245 | 258 |
246 // UnsignedMod32(lhs, rhs) divides |lhs| by |rhs| and returns the remainder | 259 // UnsignedMod32(lhs, rhs) divides |lhs| by |rhs| and returns the remainder |
247 // truncated to uint32. If |rhs| is zero, then zero is returned. | 260 // truncated to uint32. If |rhs| is zero, then zero is returned. |
248 inline uint32_t UnsignedMod32(uint32_t lhs, uint32_t rhs) { | 261 inline uint32_t UnsignedMod32(uint32_t lhs, uint32_t rhs) { |
249 return rhs ? lhs % rhs : 0u; | 262 return rhs ? lhs % rhs : 0u; |
250 } | 263 } |
251 | 264 |
252 } // namespace bits | 265 } // namespace bits |
253 } // namespace base | 266 } // namespace base |
254 } // namespace v8 | 267 } // namespace v8 |
255 | 268 |
256 #endif // V8_BASE_BITS_H_ | 269 #endif // V8_BASE_BITS_H_ |
OLD | NEW |