Skip to content

Instantly share code, notes, and snippets.

@Andersama
Last active May 8, 2021 04:02
Show Gist options
  • Select an option

  • Save Andersama/adac04ba9d84f60ca9c6e3feacd2c0a3 to your computer and use it in GitHub Desktop.

Select an option

Save Andersama/adac04ba9d84f60ca9c6e3feacd2c0a3 to your computer and use it in GitHub Desktop.
A quick and dirty sse based tokenizer
#pragma once
#include <cstdint>
#include <intrin.h>
#include <string_view>
#include <cstring>
#include <array>
constexpr uint8_t l_space = 0x1;
constexpr uint8_t h_space = 0x2;
constexpr uint8_t h_delch = 0x4;
constexpr uint8_t l_digit = 0x8;
constexpr uint8_t l_alpha = 0x10;
constexpr uint8_t h_alpha = 0x20;
constexpr uint8_t h_symbl = 0x40;
std::array<uint8_t, 16> h_val = {
l_space,
l_space,
h_space,
l_digit,
l_alpha,
h_alpha,
l_alpha,
h_alpha | h_delch,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0
};
std::array<uint8_t, 16> l_val = {
l_space | h_alpha | l_digit | h_space,
l_space | h_alpha | l_alpha | l_digit,
l_space | h_alpha | l_alpha | l_digit,
l_space | h_alpha | l_alpha | l_digit,
l_space | h_alpha | l_alpha | l_digit,
l_space | h_alpha | l_alpha | l_digit,
l_space | h_alpha | l_alpha | l_digit,
l_space | h_alpha | l_alpha | l_digit,
l_space | h_alpha | l_alpha | l_digit,
l_space | h_alpha | l_alpha | l_digit,
l_space | h_alpha | l_alpha,
l_space | l_alpha,
l_space | l_alpha,
l_space | l_alpha,
l_space | l_alpha,
l_space | l_alpha | h_delch
};
std::array<uint8_t, 16> l_nibble_lookup = {
0,
0,
1,
15,
2,
15,
15,
15,
3,
15,
15,
15,
15,
15,
15,
15
};
std::array<uint8_t, 16> h_nibble_lookup = {
0,
4,
5,
15,
6,
15,
15,
15,
7,
15,
15,
15,
15,
15,
15,
15
};
std::array<uint8_t, 16> r_classify = {
0,
0,
1,
2,
3,
3,
4,
5,
15,
15,
15,
15,
15,
15,
15,
15
};
__forceinline __m128i compress_nibble(__m128i v) noexcept {
__m128i low_mask = _mm_set1_epi8(0x0f);
__m128i hm = _mm_and_si128(_mm_srli_epi16(v, 4), low_mask);
__m128i lm = _mm_and_si128(v, low_mask);
__m128i h0 = _mm_shuffle_epi8(*(__m128i *)&h_nibble_lookup, hm);
__m128i l0 = _mm_shuffle_epi8(*(__m128i *)&l_nibble_lookup, lm);
return _mm_max_epu8(h0, l0);
}
__forceinline __m128i classify_char_16(__m128i m) noexcept {
__m128i low_mask = _mm_set1_epi8(0x0f);
__m128i hm = _mm_and_si128(_mm_srli_epi16(m, 4), low_mask);
__m128i lm = _mm_and_si128(m, low_mask);
__m128i h0 = _mm_shuffle_epi8(*(__m128i *)&h_val, hm);
__m128i l0 = _mm_shuffle_epi8(*(__m128i *)&l_val, lm);
__m128i lookup = _mm_and_si128(h0, l0);
// if 0'd out, these are punctuation / symbols
__m128i zmask = _mm_cmpeq_epi8(lookup, _mm_setzero_si128());
__m128i nlookup =
_mm_or_si128(lookup, _mm_and_si128(zmask, _mm_set1_epi8(h_symbl)));
// convert bitmask into 0-7
__m128i nnibble = compress_nibble(nlookup);
// convert 0-7 -> 0-5
return _mm_shuffle_epi8(*(__m128i *)&r_classify, nnibble);
}
__forceinline __m128i classify_char_16(std::string_view v) noexcept {
__m128i m = _mm_loadu_si128((__m128i *)(v.data()));
__m128i low_mask = _mm_set1_epi8(0x0f);
__m128i hm = _mm_and_si128(_mm_srli_epi16(m, 4), low_mask);
__m128i lm = _mm_and_si128(m, low_mask);
__m128i h0 = _mm_shuffle_epi8(*(__m128i *)&h_val, hm);
__m128i l0 = _mm_shuffle_epi8(*(__m128i *)&l_val, lm);
__m128i lookup = _mm_and_si128(h0, l0);
//if 0'd out, these are punctuation / symbols
__m128i zmask = _mm_cmpeq_epi8(lookup, _mm_setzero_si128());
__m128i nlookup = _mm_or_si128(lookup, _mm_and_si128(zmask, _mm_set1_epi8(h_symbl)));
//convert bitmask into 0-7
__m128i nnibble = compress_nibble(nlookup);
//convert 0-7 -> 0-5
return _mm_shuffle_epi8(*(__m128i *)&r_classify, nnibble);
};
__forceinline __m128i classify_char_16(char *data, size_t) noexcept {
__m128i m = _mm_loadu_si128((__m128i *)(data));
__m128i low_mask = _mm_set1_epi8(0x0f);
__m128i hm = _mm_and_si128(_mm_srli_epi16(m, 4), low_mask);
__m128i lm = _mm_and_si128(m, low_mask);
__m128i h0 = _mm_shuffle_epi8(*(__m128i *)&h_val, hm);
__m128i l0 = _mm_shuffle_epi8(*(__m128i *)&l_val, lm);
__m128i lookup = _mm_and_si128(h0, l0);
// if 0'd out, these are punctuation / symbols
__m128i zmask = _mm_cmpeq_epi8(lookup, _mm_setzero_si128());
__m128i nlookup =
_mm_or_si128(lookup, _mm_and_si128(zmask, _mm_set1_epi8(h_symbl)));
// convert bitmask into 0-7
__m128i nnibble = compress_nibble(nlookup);
// convert 0-7 -> 0-5
return _mm_shuffle_epi8(*(__m128i *)&r_classify, nnibble);
};
struct token {
uint8_t id;
uint8_t reserve_0;
uint8_t reserve_1;
uint8_t reserve_2;
const char *ptr;
size_t len;
};
// returns the next string view to tokenize, v is the input to tokenize, tkn is the output token
std::string_view get_token(std::string_view v, token &tkn) noexcept {
tkn.ptr = v.data();
tkn.len = 0;
__m128i reverse =
_mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
//_mm_set_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
if (tkn.len + 15 < v.size()) {
__m128i c = classify_char_16(v);
__m128i broadcast = _mm_setzero_si128();
__m128i c0 = _mm_shuffle_epi8(c, broadcast);
tkn.id = _mm_cvtsi128_si32(c) & 0xff;
__m128i comp = _mm_cmpeq_epi8(c, c0);
int mm = _mm_movemask_epi8(_mm_shuffle_epi8(comp, reverse));
int mask = (~(mm << 16)) | 0xffff;
// count how many we match
size_t lz_count = _lzcnt_u32(mask);
tkn.len += lz_count;
if (lz_count < 16) {
return v.substr(tkn.len);
}
//loop so long as we match the same token id
//the language defined by h_val and l_val helps to get close to:
// whitespace, del, [0-9], [a-zA-Z], symbols, classify_char_16 takes a few extra steps to get there
for (;tkn.len+15 < v.size();) {
__m128i c1 = classify_char_16(v.data()+tkn.len);
__m128i comp1 = _mm_cmpeq_epi8(c1, c0);
int mask1 = (~(_mm_movemask_epi8(_mm_shuffle_epi8(comp1, reverse)) << 16)) | 0xffff;
// count how many we match
size_t lz_count1 = _lzcnt_u32(mask1);
tkn.len += lz_count1;
// if we don't match a whole sse register, we can exit here otherwise we need to process another
if (lz_count1 < 16) {
return v.substr(tkn.len);
}
}
//if we get here we have a remainaing buffer
__m128i m = _mm_set1_epi8(' ');
std::memcpy(&m, v.data()+tkn.len, v.size()-tkn.len);
__m128i c1 = classify_char_16(m);
__m128i comp1 = _mm_cmpeq_epi8(c1, c0);
int mask1 = (~(_mm_movemask_epi8(_mm_shuffle_epi8(comp1, reverse)) << 16)) | 0xffff;
size_t lz_count1 = _lzcnt_u32(mm);
tkn.len += (lz_count1+tkn.len) > v.size() ? v.size()-tkn.len : lz_count1;
return v.substr(tkn.len);
} else {
__m128i m = _mm_set1_epi8(' ');
std::memcpy(&m, v.data(), v.size());
__m128i c = classify_char_16(m);
__m128i broadcast = _mm_setzero_si128();
__m128i c0 = _mm_shuffle_epi8(c, broadcast);
tkn.id = _mm_cvtsi128_si32(c) & 0xff;
__m128i comp = _mm_cmpeq_epi8(c, c0);
int mask1 = (~(_mm_movemask_epi8(_mm_shuffle_epi8(comp, reverse)) << 16)) | 0xffff;
// count how many we match
size_t lz_count = _lzcnt_u32(mask1);
tkn.len += (lz_count + tkn.len) > v.size() ? v.size() - tkn.len
: lz_count;
return v.substr(tkn.len);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment