Skip to content

Commit

Permalink
Quick test
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB authored Jul 4, 2024
1 parent 933b984 commit 02b5548
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions src/WebSocketProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <cstdlib>
#include <string_view>

// Quick test
#include <immintrin.h>

namespace uWS {

/* We should not overcomplicate these */
Expand Down Expand Up @@ -114,24 +117,59 @@ T cond_byte_swap(T value) {
// Optimized for predominantly 7-bit content by Alex Hultman, 2016
// Licensed as Zlib, like the rest of this project
// This runs about 40% faster than simdutf with g++ -mavx
static bool isValidUtf8(unsigned char *s, size_t length)
{
for (unsigned char *e = s + length; s != e; ) {
if (s + 16 <= e) {
uint64_t tmp[2];
memcpy(tmp, s, 16);
if (((tmp[0] & 0x8080808080808080) | (tmp[1] & 0x8080808080808080)) == 0) {
s += 16;
continue;
static bool isValidUtf8(unsigned char *s, size_t length) {
auto firstUtf8EscapeByte = [](unsigned char *s, unsigned char *e) {
// Align
if (s + 32 <= e) {
int mask = _mm256_movemask_epi8(_mm256_loadu_si256((const __m256i*)s));
if (mask) {
return s + __builtin_ctz(mask);
}
s += 32 - ((uintptr_t)s % 32);
} else {
// Worst path
while (s < e) {
if (*s & 0x80) {
return s;
}
s++;
}
return e;
}

while (!(*s & 0x80)) {
if (++s == e) {
return true;
while (s + 128 <= e) {
// Aligned
int mask = _mm256_movemask_epi8(_mm256_load_si256((const __m256i*)s));
if (mask) {
return s + __builtin_ctz(mask);
}
s += 32;
mask = _mm256_movemask_epi8(_mm256_load_si256((const __m256i*)s));
if (mask) {
return s + __builtin_ctz(mask);
}
s += 32;
mask = _mm256_movemask_epi8(_mm256_load_si256((const __m256i*)s));
if (mask) {
return s + __builtin_ctz(mask);
}
s += 32;
mask = _mm256_movemask_epi8(_mm256_load_si256((const __m256i*)s));
if (mask) {
return s + __builtin_ctz(mask);
}
s += 32;
}

// Exit
while ((*s & 0x80) == 0 && s < e) {
s++;
}
return s;
};

for (unsigned char *e = s + length; (s = (unsigned char *) firstUtf8EscapeByte(s, e)) != e; ) {

if ((s[0] & 0x60) == 0x40) {
if (s + 1 >= e || (s[1] & 0xc0) != 0x80 || (s[0] & 0xfe) == 0xc0) {
return false;
Expand Down

0 comments on commit 02b5548

Please sign in to comment.