Skip to content

Commit

Permalink
buffer: add SIMD Neon optimization for byteLength
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed May 15, 2023
1 parent c9ec72d commit a52d532
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
#include <cstring>
#include <climits>

#if defined(__aarch64__) || defined(_M_ARM64)
#define NODE_HAS_SIMD_NEON 1
#endif

#if NODE_HAS_SIMD_NEON
#include <arm_neon.h>
#endif

#define THROW_AND_RETURN_UNLESS_BUFFER(env, obj) \
THROW_AND_RETURN_IF_NOT_BUFFER(env, obj, "argument") \

Expand Down Expand Up @@ -741,6 +749,36 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(args[0].As<String>()->Utf8Length(env->isolate()));
}

#if NODE_HAS_SIMD_NEON
uint32_t FastByteLengthUtf8(Local<Value> receiver,
const v8::FastOneByteString& source) {
const auto data = reinterpret_cast<const uint8_t*>(source.data);
uint8x16_t result_vector = vdupq_n_u8(0);
size_t i = 0;

for (; i < source.length; i += 16) {
// load 16 bytes from data
uint8x16_t values = vld1q_u8(data + i);

// extract the high bits using 0x80 mask
uint8x16_t high_bits = vshrq_n_u8(values, 7);

// accumulate the high bits to result_vector
result_vector = vqaddq_u8(result_vector, high_bits);
}

// sum the elements in the result_vector
uint64x2_t sum64 = vpaddlq_u32(vpaddlq_u16(vpaddlq_u8(result_vector)));

uint32_t answer = vgetq_lane_u64(sum64, 0) + vgetq_lane_u64(sum64, 1);

for (; i < source.length; ++i) {
answer += (data[i] >> 7);
}

return answer + source.length;
}
#else
uint32_t FastByteLengthUtf8(Local<Value> receiver,
const v8::FastOneByteString& source) {
uint32_t result = 0;
Expand All @@ -752,6 +790,7 @@ uint32_t FastByteLengthUtf8(Local<Value> receiver,
result += length;
return result;
}
#endif

static v8::CFunction fast_byte_length_utf8(
v8::CFunction::Make(FastByteLengthUtf8));
Expand Down

0 comments on commit a52d532

Please sign in to comment.