-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement decodeASCII and decodeLatin1 to share C code
- Loading branch information
Showing
6 changed files
with
103 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2021 Andrew Lelechenko <[email protected]> | ||
*/ | ||
|
||
#include <string.h> | ||
#include <stdint.h> | ||
#include <sys/types.h> | ||
#ifdef __x86_64__ | ||
#include <emmintrin.h> | ||
#include <xmmintrin.h> | ||
#endif | ||
#include <stdbool.h> | ||
|
||
/* | ||
_hs_text_is_ascii takes a UTF-8 encoded buffer, | ||
and returns the length of the ASCII-compatible prefix. | ||
*/ | ||
const size_t _hs_text_is_ascii(const uint8_t *src0, const uint8_t *srcend){ | ||
const uint8_t *src = src0; | ||
|
||
#ifdef __x86_64__ | ||
// I experimented with larger vector registers, | ||
// but did not notice any measurable speed up, so let's keep it simple. | ||
while (src < srcend - 15){ | ||
__m128i w128 = _mm_loadu_si128((__m128i *)src); | ||
// Which bytes are < 128? | ||
uint16_t mask = _mm_movemask_epi8(w128); | ||
if (mask) break; | ||
src+= 16; | ||
} | ||
#endif | ||
|
||
while (src < srcend - 7){ | ||
uint64_t w64; | ||
memcpy(&w64, src, sizeof(uint64_t)); | ||
if (w64 & 0x8080808080808080ULL) break; | ||
src+= 8; | ||
} | ||
|
||
while (src < srcend){ | ||
uint8_t leadByte = *src; | ||
if(leadByte >= 0x80) break; | ||
src++; | ||
} | ||
|
||
return src - src0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters