52
52
#define BUFFER_MALLOC (length ) \
53
53
zero_fill_all_buffers ? calloc(length, 1 ) : malloc(length)
54
54
55
- #if defined(__GNUC__) || defined(__clang__)
56
- #define BSWAP_INTRINSIC_2 (x ) __builtin_bswap16(x)
57
- #define BSWAP_INTRINSIC_4 (x ) __builtin_bswap32(x)
58
- #define BSWAP_INTRINSIC_8 (x ) __builtin_bswap64(x)
59
- #elif defined(__linux__)
60
- #include < byteswap.h>
61
- #define BSWAP_INTRINSIC_2 (x ) bswap_16(x)
62
- #define BSWAP_INTRINSIC_4 (x ) bswap_32(x)
63
- #define BSWAP_INTRINSIC_8 (x ) bswap_64(x)
64
- #elif defined(_MSC_VER)
65
- #include < intrin.h>
66
- #define BSWAP_INTRINSIC_2 (x ) _byteswap_ushort(x);
67
- #define BSWAP_INTRINSIC_4 (x ) _byteswap_ulong(x);
68
- #define BSWAP_INTRINSIC_8 (x ) _byteswap_uint64(x);
69
- #else
70
- #define BSWAP_INTRINSIC_2 (x ) ((x) << 8 ) | ((x) >> 8 )
71
- #define BSWAP_INTRINSIC_4 (x ) \
72
- (((x) & 0xFF ) << 24 ) | \
73
- (((x) & 0xFF00 ) << 8 ) | \
74
- (((x) >> 8 ) & 0xFF00 ) | \
75
- (((x) >> 24 ) & 0xFF )
76
- #define BSWAP_INTRINSIC_8 (x ) \
77
- (((x) & 0xFF00000000000000ull ) >> 56 ) | \
78
- (((x) & 0x00FF000000000000ull ) >> 40 ) | \
79
- (((x) & 0x0000FF0000000000ull ) >> 24 ) | \
80
- (((x) & 0x000000FF00000000ull ) >> 8 ) | \
81
- (((x) & 0x00000000FF000000ull ) << 8 ) | \
82
- (((x) & 0x0000000000FF0000ull ) << 24 ) | \
83
- (((x) & 0x000000000000FF00ull ) << 40 ) | \
84
- (((x) & 0x00000000000000FFull ) << 56 )
85
- #endif
86
-
87
55
namespace node {
88
56
89
57
// if true, all Buffer and SlowBuffer instances will automatically zero-fill
@@ -1209,18 +1177,8 @@ void Swap16(const FunctionCallbackInfo<Value>& args) {
1209
1177
1210
1178
CHECK_EQ (ts_obj_length % 2 , 0 );
1211
1179
1212
- int align = reinterpret_cast <uintptr_t >(ts_obj_data) % sizeof (uint16_t );
1213
-
1214
- if (align == 0 ) {
1215
- uint16_t * data16 = reinterpret_cast <uint16_t *>(ts_obj_data);
1216
- size_t len16 = ts_obj_length / 2 ;
1217
- for (size_t i = 0 ; i < len16; i++) {
1218
- data16[i] = BSWAP_INTRINSIC_2 (data16[i]);
1219
- }
1220
- } else {
1221
- for (size_t i = 0 ; i < ts_obj_length; i += 2 ) {
1222
- std::swap (ts_obj_data[i], ts_obj_data[i + 1 ]);
1223
- }
1180
+ for (size_t i = 0 ; i < ts_obj_length; i += 2 ) {
1181
+ std::swap (ts_obj_data[i], ts_obj_data[i + 1 ]);
1224
1182
}
1225
1183
1226
1184
args.GetReturnValue ().Set (args[0 ]);
@@ -1234,19 +1192,9 @@ void Swap32(const FunctionCallbackInfo<Value>& args) {
1234
1192
1235
1193
CHECK_EQ (ts_obj_length % 4 , 0 );
1236
1194
1237
- int align = reinterpret_cast <uintptr_t >(ts_obj_data) % sizeof (uint32_t );
1238
-
1239
- if (align == 0 ) {
1240
- uint32_t * data32 = reinterpret_cast <uint32_t *>(ts_obj_data);
1241
- size_t len32 = ts_obj_length / 4 ;
1242
- for (size_t i = 0 ; i < len32; i++) {
1243
- data32[i] = BSWAP_INTRINSIC_4 (data32[i]);
1244
- }
1245
- } else {
1246
- for (size_t i = 0 ; i < ts_obj_length; i += 4 ) {
1247
- std::swap (ts_obj_data[i], ts_obj_data[i + 3 ]);
1248
- std::swap (ts_obj_data[i + 1 ], ts_obj_data[i + 2 ]);
1249
- }
1195
+ for (size_t i = 0 ; i < ts_obj_length; i += 4 ) {
1196
+ std::swap (ts_obj_data[i], ts_obj_data[i + 3 ]);
1197
+ std::swap (ts_obj_data[i + 1 ], ts_obj_data[i + 2 ]);
1250
1198
}
1251
1199
1252
1200
args.GetReturnValue ().Set (args[0 ]);
@@ -1260,21 +1208,11 @@ void Swap64(const FunctionCallbackInfo<Value>& args) {
1260
1208
1261
1209
CHECK_EQ (ts_obj_length % 8 , 0 );
1262
1210
1263
- int align = reinterpret_cast <uintptr_t >(ts_obj_data) % sizeof (uint64_t );
1264
-
1265
- if (align == 0 ) {
1266
- uint64_t * data64 = reinterpret_cast <uint64_t *>(ts_obj_data);
1267
- size_t len32 = ts_obj_length / 8 ;
1268
- for (size_t i = 0 ; i < len32; i++) {
1269
- data64[i] = BSWAP_INTRINSIC_8 (data64[i]);
1270
- }
1271
- } else {
1272
- for (size_t i = 0 ; i < ts_obj_length; i += 8 ) {
1273
- std::swap (ts_obj_data[i], ts_obj_data[i + 7 ]);
1274
- std::swap (ts_obj_data[i + 1 ], ts_obj_data[i + 6 ]);
1275
- std::swap (ts_obj_data[i + 2 ], ts_obj_data[i + 5 ]);
1276
- std::swap (ts_obj_data[i + 3 ], ts_obj_data[i + 4 ]);
1277
- }
1211
+ for (size_t i = 0 ; i < ts_obj_length; i += 8 ) {
1212
+ std::swap (ts_obj_data[i], ts_obj_data[i + 7 ]);
1213
+ std::swap (ts_obj_data[i + 1 ], ts_obj_data[i + 6 ]);
1214
+ std::swap (ts_obj_data[i + 2 ], ts_obj_data[i + 5 ]);
1215
+ std::swap (ts_obj_data[i + 3 ], ts_obj_data[i + 4 ]);
1278
1216
}
1279
1217
1280
1218
args.GetReturnValue ().Set (args[0 ]);
0 commit comments