| OLD | NEW |
| 1 // Copyright (c) 2014, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dartino 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 #ifndef SRC_SHARED_UTILS_H_ | 5 #ifndef SRC_SHARED_UTILS_H_ |
| 6 #define SRC_SHARED_UTILS_H_ | 6 #define SRC_SHARED_UTILS_H_ |
| 7 | 7 |
| 8 #include "src/shared/assert.h" | 8 #include "src/shared/assert.h" |
| 9 #include "src/shared/atomic.h" | 9 #include "src/shared/atomic.h" |
| 10 #include "src/shared/globals.h" | 10 #include "src/shared/globals.h" |
| 11 | 11 |
| 12 namespace fletch { | 12 namespace dartino { |
| 13 | 13 |
| 14 class Mutex; | 14 class Mutex; |
| 15 | 15 |
| 16 class PrintInterceptor { | 16 class PrintInterceptor { |
| 17 public: | 17 public: |
| 18 PrintInterceptor() : next_(NULL) {} | 18 PrintInterceptor() : next_(NULL) {} |
| 19 virtual ~PrintInterceptor() { | 19 virtual ~PrintInterceptor() { |
| 20 delete next_; | 20 delete next_; |
| 21 next_ = NULL; | 21 next_ = NULL; |
| 22 } | 22 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 static bool IsInt8(word value) { return (-128 <= value) && (value < 128); } | 102 static bool IsInt8(word value) { return (-128 <= value) && (value < 128); } |
| 103 | 103 |
| 104 static bool IsUint8(word value) { return (0 <= value) && (value < 256); } | 104 static bool IsUint8(word value) { return (0 <= value) && (value < 256); } |
| 105 | 105 |
| 106 static bool IsInt16(word value) { | 106 static bool IsInt16(word value) { |
| 107 return (-32768 <= value) && (value < 32768); | 107 return (-32768 <= value) && (value < 32768); |
| 108 } | 108 } |
| 109 | 109 |
| 110 static bool IsUint16(word value) { return (0 <= value) && (value < 65536); } | 110 static bool IsUint16(word value) { return (0 <= value) && (value < 65536); } |
| 111 | 111 |
| 112 #ifdef FLETCH64 | 112 #ifdef DARTINO64 |
| 113 static bool IsInt32(word value) { | 113 static bool IsInt32(word value) { |
| 114 return (-(WORD_C(1) << 31) <= value) && (value < (WORD_C(1) << 31)); | 114 return (-(WORD_C(1) << 31) <= value) && (value < (WORD_C(1) << 31)); |
| 115 } | 115 } |
| 116 | 116 |
| 117 static bool IsUint32(word value) { | 117 static bool IsUint32(word value) { |
| 118 return (0 <= value) && (value < (WORD_C(1) << 32)); | 118 return (0 <= value) && (value < (WORD_C(1) << 32)); |
| 119 } | 119 } |
| 120 #endif | 120 #endif |
| 121 | 121 |
| 122 static bool SignedAddOverflow(word lhs, word rhs, word* val) { | 122 static bool SignedAddOverflow(word lhs, word rhs, word* val) { |
| 123 #if FLETCH_HAS_BUILTIN_SADDL_OVERFLOW | 123 #if DARTINO_HAS_BUILTIN_SADDL_OVERFLOW |
| 124 return __builtin_saddl_overflow(lhs, rhs, val); | 124 return __builtin_saddl_overflow(lhs, rhs, val); |
| 125 #else | 125 #else |
| 126 uword res = static_cast<uword>(lhs) + static_cast<uword>(rhs); | 126 uword res = static_cast<uword>(lhs) + static_cast<uword>(rhs); |
| 127 *val = bit_cast<uword>(res); | 127 *val = bit_cast<uword>(res); |
| 128 word bit = (res ^ lhs) & (res ^ rhs) & (UWORD_C(1) << (kBitsPerWord - 1)); | 128 word bit = (res ^ lhs) & (res ^ rhs) & (UWORD_C(1) << (kBitsPerWord - 1)); |
| 129 return bit != 0; | 129 return bit != 0; |
| 130 #endif | 130 #endif |
| 131 } | 131 } |
| 132 | 132 |
| 133 static bool SignedSubOverflow(word lhs, word rhs, word* val) { | 133 static bool SignedSubOverflow(word lhs, word rhs, word* val) { |
| 134 #if FLETCH_HAS_BUILTIN_SSUBL_OVERFLOW | 134 #if DARTINO_HAS_BUILTIN_SSUBL_OVERFLOW |
| 135 return __builtin_ssubl_overflow(lhs, rhs, val); | 135 return __builtin_ssubl_overflow(lhs, rhs, val); |
| 136 #else | 136 #else |
| 137 uword res = static_cast<uword>(lhs) - static_cast<uword>(rhs); | 137 uword res = static_cast<uword>(lhs) - static_cast<uword>(rhs); |
| 138 *val = bit_cast<word>(res); | 138 *val = bit_cast<word>(res); |
| 139 uword bit = (res ^ lhs) & (res ^ ~rhs) & (UWORD_C(1) << (kBitsPerWord - 1)); | 139 uword bit = (res ^ lhs) & (res ^ ~rhs) & (UWORD_C(1) << (kBitsPerWord - 1)); |
| 140 return bit != 0; | 140 return bit != 0; |
| 141 #endif | 141 #endif |
| 142 } | 142 } |
| 143 | 143 |
| 144 static int HighestBit(int64 v) { | 144 static int HighestBit(int64 v) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 173 // Flip bits if negative (-1 becomes 0). | 173 // Flip bits if negative (-1 becomes 0). |
| 174 value ^= value >> 63; | 174 value ^= value >> 63; |
| 175 return (value == 0) ? 0 : (Utils::HighestBit(value) + 1); | 175 return (value == 0) ? 0 : (Utils::HighestBit(value) + 1); |
| 176 } | 176 } |
| 177 | 177 |
| 178 static bool Signed64BitMulMightOverflow(int64 lhs, int64 rhs) { | 178 static bool Signed64BitMulMightOverflow(int64 lhs, int64 rhs) { |
| 179 return (Utils::HighestBit(lhs) + Utils::HighestBit(rhs)) >= 62; | 179 return (Utils::HighestBit(lhs) + Utils::HighestBit(rhs)) >= 62; |
| 180 } | 180 } |
| 181 | 181 |
| 182 static bool SignedMulOverflow(word lhs, word rhs, word* val) { | 182 static bool SignedMulOverflow(word lhs, word rhs, word* val) { |
| 183 #if FLETCH_HAS_BUILTIN_SMULL_OVERFLOW | 183 #if DARTINO_HAS_BUILTIN_SMULL_OVERFLOW |
| 184 return __builtin_smull_overflow(lhs, rhs, val); | 184 return __builtin_smull_overflow(lhs, rhs, val); |
| 185 #else | 185 #else |
| 186 #ifdef FLETCH64 | 186 #ifdef DARTINO64 |
| 187 if (Signed64BitMulMightOverflow(lhs, rhs)) return true; | 187 if (Signed64BitMulMightOverflow(lhs, rhs)) return true; |
| 188 *val = lhs * rhs; | 188 *val = lhs * rhs; |
| 189 return false; | 189 return false; |
| 190 #else | 190 #else |
| 191 word res = lhs * rhs; | 191 word res = lhs * rhs; |
| 192 bool overflow = (res != static_cast<int64>(lhs) * rhs); | 192 bool overflow = (res != static_cast<int64>(lhs) * rhs); |
| 193 if (overflow) return true; | 193 if (overflow) return true; |
| 194 *val = res; | 194 *val = res; |
| 195 return false; | 195 return false; |
| 196 #endif // FLETCH64 | 196 #endif // DARTINO64 |
| 197 #endif // FLETCH_HAS_BUILTIN_SMULL_OVERFLOW | 197 #endif // DARTINO_HAS_BUILTIN_SMULL_OVERFLOW |
| 198 } | 198 } |
| 199 | 199 |
| 200 // Read a 32-bit integer from the buffer, as little endian. | 200 // Read a 32-bit integer from the buffer, as little endian. |
| 201 static inline int32 ReadInt32(uint8* buffer) { | 201 static inline int32 ReadInt32(uint8* buffer) { |
| 202 return reinterpret_cast<int32*>(buffer)[0]; | 202 return reinterpret_cast<int32*>(buffer)[0]; |
| 203 } | 203 } |
| 204 | 204 |
| 205 // Write a 32-bit integer to the buffer, as little endian. | 205 // Write a 32-bit integer to the buffer, as little endian. |
| 206 static inline void WriteInt32(uint8* buffer, int32 value) { | 206 static inline void WriteInt32(uint8* buffer, int32 value) { |
| 207 reinterpret_cast<int32*>(buffer)[0] = value; | 207 reinterpret_cast<int32*>(buffer)[0] = value; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 | 273 |
| 274 // Returns a uword with the bool field value encoded based on the | 274 // Returns a uword with the bool field value encoded based on the |
| 275 // original value. Only the single bit corresponding to this bool | 275 // original value. Only the single bit corresponding to this bool |
| 276 // field will be changed. | 276 // field will be changed. |
| 277 static uword update(bool value, uword original) { | 277 static uword update(bool value, uword original) { |
| 278 const uword mask = 1U << position; | 278 const uword mask = 1U << position; |
| 279 return value ? original | mask : original & ~mask; | 279 return value ? original | mask : original & ~mask; |
| 280 } | 280 } |
| 281 }; | 281 }; |
| 282 | 282 |
| 283 } // namespace fletch | 283 } // namespace dartino |
| 284 | 284 |
| 285 #endif // SRC_SHARED_UTILS_H_ | 285 #endif // SRC_SHARED_UTILS_H_ |
| OLD | NEW |