Skip to content

Commit cd020e3

Browse files
committed
src: don't use __builtin_bswap16() and friends
Said builtins are not supported by older versions of apple-gcc, breaking the build on OS X 10.8. Fixes: nodejs#7618 Refs: nodejs#4290 Refs: nodejs#7157
1 parent fcae5e2 commit cd020e3

File tree

1 file changed

+10
-72
lines changed

1 file changed

+10
-72
lines changed

src/node_buffer.cc

Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,6 @@
5252
#define BUFFER_MALLOC(length) \
5353
zero_fill_all_buffers ? calloc(length, 1) : malloc(length)
5454

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-
8755
namespace node {
8856

8957
// if true, all Buffer and SlowBuffer instances will automatically zero-fill
@@ -1209,18 +1177,8 @@ void Swap16(const FunctionCallbackInfo<Value>& args) {
12091177

12101178
CHECK_EQ(ts_obj_length % 2, 0);
12111179

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]);
12241182
}
12251183

12261184
args.GetReturnValue().Set(args[0]);
@@ -1234,19 +1192,9 @@ void Swap32(const FunctionCallbackInfo<Value>& args) {
12341192

12351193
CHECK_EQ(ts_obj_length % 4, 0);
12361194

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]);
12501198
}
12511199

12521200
args.GetReturnValue().Set(args[0]);
@@ -1260,21 +1208,11 @@ void Swap64(const FunctionCallbackInfo<Value>& args) {
12601208

12611209
CHECK_EQ(ts_obj_length % 8, 0);
12621210

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]);
12781216
}
12791217

12801218
args.GetReturnValue().Set(args[0]);

0 commit comments

Comments
 (0)