Chromium Code Reviews| Index: src/utils.h |
| diff --git a/src/utils.h b/src/utils.h |
| index 9a601416ffd837e48209a3bf5eec80d70d024346..9108fe90d46eaa4089886a11f0a3b86474bc2da4 100644 |
| --- a/src/utils.h |
| +++ b/src/utils.h |
| @@ -1548,6 +1548,33 @@ static inline void WriteUnalignedUInt32(void* p, uint32_t value) { |
| WriteUnalignedValue(p, value); |
| } |
| +template <typename V> |
| +static inline V ReadLittleEndianValue(const void* p) { |
|
titzer
2016/06/06 07:44:00
Is it possible to unify these routines with those
ivica.bogosavljevic
2016/06/13 14:59:02
I suggest we keep ReadLittleEndianValue and WriteL
|
| +#if defined(V8_TARGET_LITTLE_ENDIAN) |
| + return ReadUnalignedValue(p); |
| +#elif defined(V8_TARGET_BIG_ENDIAN) |
| + V ret = 0; |
| + const byte* src = reinterpret_cast<const byte*>(p); |
| + byte* dst = reinterpret_cast<byte*>(&ret); |
|
titzer
2016/06/06 07:44:00
That kind of type punning is undefined behavior in
ivica.bogosavljevic
2016/06/13 14:59:02
For big-endian implementation, we need to swap byt
|
| + for (size_t i = 0; i < sizeof(V); i++) { |
| + dst[i] = src[sizeof(V) - i - 1]; |
| + } |
| + return ret; |
| +#endif // V8_TARGET_LITTLE_ENDIAN |
| +} |
| + |
| +template <typename V> |
| +static inline void WriteLittleEndianValue(void* p, V value) { |
| +#if defined(V8_TARGET_LITTLE_ENDIAN) |
| + WriteUnalignedValue(p, value); |
| +#elif defined(V8_TARGET_BIG_ENDIAN) |
| + byte* src = reinterpret_cast<byte*>(&value); |
| + byte* dst = reinterpret_cast<byte*>(p); |
| + for (size_t i = 0; i < sizeof(V); i++) { |
| + dst[i] = src[sizeof(V) - i - 1]; |
| + } |
| +#endif // V8_TARGET_LITTLE_ENDIAN |
| +} |
| } // namespace internal |
| } // namespace v8 |