OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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_UTILS_H_ | 5 #ifndef V8_UTILS_H_ |
6 #define V8_UTILS_H_ | 6 #define V8_UTILS_H_ |
7 | 7 |
8 #include <limits.h> | 8 #include <limits.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 1530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1541 } | 1541 } |
1542 | 1542 |
1543 static inline uint32_t ReadUnalignedUInt32(const void* p) { | 1543 static inline uint32_t ReadUnalignedUInt32(const void* p) { |
1544 return ReadUnalignedValue<uint32_t>(p); | 1544 return ReadUnalignedValue<uint32_t>(p); |
1545 } | 1545 } |
1546 | 1546 |
1547 static inline void WriteUnalignedUInt32(void* p, uint32_t value) { | 1547 static inline void WriteUnalignedUInt32(void* p, uint32_t value) { |
1548 WriteUnalignedValue(p, value); | 1548 WriteUnalignedValue(p, value); |
1549 } | 1549 } |
1550 | 1550 |
1551 template <typename V> | |
1552 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
| |
1553 #if defined(V8_TARGET_LITTLE_ENDIAN) | |
1554 return ReadUnalignedValue(p); | |
1555 #elif defined(V8_TARGET_BIG_ENDIAN) | |
1556 V ret = 0; | |
1557 const byte* src = reinterpret_cast<const byte*>(p); | |
1558 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
| |
1559 for (size_t i = 0; i < sizeof(V); i++) { | |
1560 dst[i] = src[sizeof(V) - i - 1]; | |
1561 } | |
1562 return ret; | |
1563 #endif // V8_TARGET_LITTLE_ENDIAN | |
1564 } | |
1565 | |
1566 template <typename V> | |
1567 static inline void WriteLittleEndianValue(void* p, V value) { | |
1568 #if defined(V8_TARGET_LITTLE_ENDIAN) | |
1569 WriteUnalignedValue(p, value); | |
1570 #elif defined(V8_TARGET_BIG_ENDIAN) | |
1571 byte* src = reinterpret_cast<byte*>(&value); | |
1572 byte* dst = reinterpret_cast<byte*>(p); | |
1573 for (size_t i = 0; i < sizeof(V); i++) { | |
1574 dst[i] = src[sizeof(V) - i - 1]; | |
1575 } | |
1576 #endif // V8_TARGET_LITTLE_ENDIAN | |
1577 } | |
1551 } // namespace internal | 1578 } // namespace internal |
1552 } // namespace v8 | 1579 } // namespace v8 |
1553 | 1580 |
1554 #endif // V8_UTILS_H_ | 1581 #endif // V8_UTILS_H_ |
OLD | NEW |