1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
// V8 Typed Array implementation.
// (c) Dean McNamee <dean@gmail.com>, 2012.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#ifndef V8_TYPED_ARRAY_BSWAP_H_
#define V8_TYPED_ARRAY_BSWAP_H_
// Windows will always be little endian (including ARM), so we just need to
// worry about gcc.
#if defined (__ppc__) || defined (__ppc64__) || defined(__ARMEB__)
#define V8_TYPED_ARRAY_BIG_ENDIAN 1
#else
#define V8_TYPED_ARRAY_LITTLE_ENDIAN 1
#endif
#if defined (_MSC_VER) && (_MSC_VER < 1600)
typedef unsigned char uint8_t;
typedef signed char int8_t;
typedef unsigned __int16 uint16_t;
typedef signed __int16 int16_t;
typedef unsigned __int32 uint32_t;
typedef signed __int32 int32_t;
typedef unsigned __int64 uint64_t;
typedef signed __int64 int64_t;
// Definitions to avoid ICU redefinition issue
#define U_HAVE_INT8_T 1
#define U_HAVE_UINT8_T 1
#define U_HAVE_INT16_T 1
#define U_HAVE_UINT16_T 1
#define U_HAVE_INT32_T 1
#define U_HAVE_UINT32_T 1
#define U_HAVE_INT64_T 1
#define U_HAVE_UINT64_T 1
#else
#include <stdint.h>
#endif
#if defined (_MSC_VER)
#define V8_TYPED_ARRAY_BSWAP16 _byteswap_ushort
#define V8_TYPED_ARRAY_BSWAP32 _byteswap_ulong
#define V8_TYPED_ARRAY_BSWAP64 _byteswap_uint64
#else
// On LLVM based compilers we can feature test, but for GCC we unfortunately
// have to rely on the version. Additionally __builtin_bswap32/64 were added
// in GCC 4.3, but __builtin_bswap16 was not added until GCC 4.8.
// We should be able to assume GCC/LLVM here (and can use ULL constants, etc).
// Fallback swap macros taken from QEMU bswap.h
#ifdef __has_builtin
#define V8_TYPED_ARRAY_BSWAP_HAS_BUILTIN(x) __has_builtin(x)
#define V8_TYPED_ARRAY_BSWAP_HAS_BUILTIN16(x) __has_builtin(x)
#else
#define V8_TYPED_ARRAY_BSWAP_HAS_BUILTIN(x) (defined(__GNUC__) && \
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
#define V8_TYPED_ARRAY_BSWAP_HAS_BUILTIN16(x) (defined(__GNUC__) && \
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
#endif
#if V8_TYPED_ARRAY_BSWAP_HAS_BUILTIN(__builtin_bswap64)
#define V8_TYPED_ARRAY_BSWAP64 __builtin_bswap64
#else
#define V8_TYPED_ARRAY_BSWAP64(x) \
({ \
uint64_t __x = (x); \
((uint64_t)( \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
})
#endif
#if V8_TYPED_ARRAY_BSWAP_HAS_BUILTIN(__builtin_bswap32)
#define V8_TYPED_ARRAY_BSWAP32 __builtin_bswap32
#else
#define V8_TYPED_ARRAY_BSWAP32(x) \
({ \
uint32_t __x = (x); \
((uint32_t)( \
(((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
(((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
(((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
(((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
})
#endif
#if V8_TYPED_ARRAY_BSWAP_HAS_BUILTIN16(__builtin_bswap16)
#define V8_TYPED_ARRAY_BSWAP16 __builtin_bswap16
#else
#define V8_TYPED_ARRAY_BSWAP16(x) \
({ \
uint16_t __x = (x); \
((uint16_t)( \
(((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
(((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
})
#endif
#endif
namespace v8_typed_array {
template <typename T>
inline T SwapBytes(T x) {
typedef char NoSwapBytesForType[sizeof(T) == 0 ? 1 : -1];
return 0;
}
template <>
inline signed char SwapBytes(signed char x) { return x; }
template <>
inline unsigned char SwapBytes(unsigned char x) { return x; }
template <>
inline uint16_t SwapBytes(uint16_t x) { return V8_TYPED_ARRAY_BSWAP16(x); }
template <>
inline int16_t SwapBytes(int16_t x) { return V8_TYPED_ARRAY_BSWAP16(x); }
template <>
inline uint32_t SwapBytes(uint32_t x) { return V8_TYPED_ARRAY_BSWAP32(x); }
template <>
inline int32_t SwapBytes(int32_t x) { return V8_TYPED_ARRAY_BSWAP32(x); }
template <>
inline uint64_t SwapBytes(uint64_t x) { return V8_TYPED_ARRAY_BSWAP64(x); }
template <>
inline int64_t SwapBytes(int64_t x) { return V8_TYPED_ARRAY_BSWAP64(x); }
template <typename T> // General implementation for all non-FP types.
inline T LoadAndSwapBytes(void* ptr) {
T val;
memcpy(&val, ptr, sizeof(T));
return SwapBytes(val);
}
template <>
inline float LoadAndSwapBytes<float>(void* ptr) {
typedef char VerifySizesAreEqual[sizeof(uint32_t) == sizeof(float) ? 1 : -1];
uint32_t swappable;
float val;
memcpy(&swappable, ptr, sizeof(swappable));
swappable = SwapBytes(swappable);
memcpy(&val, &swappable, sizeof(swappable));
return val;
}
template <>
inline double LoadAndSwapBytes<double>(void* ptr) {
typedef char VerifySizesAreEqual[sizeof(uint64_t) == sizeof(double) ? 1 : -1];
uint64_t swappable;
double val;
memcpy(&swappable, ptr, sizeof(swappable));
swappable = SwapBytes(swappable);
memcpy(&val, &swappable, sizeof(swappable));
return val;
}
template <typename T> // General implementation for all non-FP types.
inline void SwapBytesAndStore(void* ptr, T val) {
val = SwapBytes(val);
memcpy(ptr, &val, sizeof(T));
}
template <>
inline void SwapBytesAndStore(void* ptr, float val) {
typedef char VerifySizesAreEqual[sizeof(uint32_t) == sizeof(float) ? 1 : -1];
uint32_t swappable;
memcpy(&swappable, &val, sizeof(swappable));
swappable = SwapBytes(swappable);
memcpy(ptr, &swappable, sizeof(swappable));
}
template <>
inline void SwapBytesAndStore(void* ptr, double val) {
typedef char VerifySizesAreEqual[sizeof(uint64_t) == sizeof(double) ? 1 : -1];
uint64_t swappable;
memcpy(&swappable, &val, sizeof(swappable));
swappable = SwapBytes(swappable);
memcpy(ptr, &swappable, sizeof(swappable));
}
} // namespace v8_typed_array
#endif // V8_TYPED_ARRAY_BSWAP_H_
|