Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AVX2 Support #431

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/ada/common_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ namespace ada {
} while (0)
#endif

#if defined(__AVX2__)
#define ADA_AVX2 1
#endif

#if defined(__SSE2__) || defined(__x86_64__) || defined(__x86_64) || \
(defined(_M_AMD64) || defined(_M_X64) || \
(defined(_M_IX86_FP) && _M_IX86_FP == 2))
Expand Down
30 changes: 30 additions & 0 deletions src/unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ ADA_POP_DISABLE_WARNINGS
#include <algorithm>
#if ADA_NEON
#include <arm_neon.h>
#elif ADA_AVX2
#include <immintrin.h>
#elif ADA_SSE2
#include <emmintrin.h>
#endif
Expand Down Expand Up @@ -68,6 +70,34 @@ ada_really_inline bool has_tabs_or_newline(
}
return vmaxvq_u8(running) != 0;
}
#elif ADA_AVX2
ada_really_inline bool has_tabs_or_newline(
std::string_view user_input) noexcept {
size_t i = 0;
const __m256i mask1 = _mm256_set1_epi8('\r');
const __m256i mask2 = _mm256_set1_epi8('\n');
const __m256i mask3 = _mm256_set1_epi8('\t');
__m256i running{0};
for (; i + 31 < user_input.size(); i += 32) {
__m256i word = _mm256_loadu_si256((const __m256i*)(user_input.data() + i));
running = _mm256_or_si256(
_mm256_or_si256(running,
_mm256_or_si256(_mm256_cmpeq_epi8(word, mask1),
_mm256_cmpeq_epi8(word, mask2))),
_mm256_cmpeq_epi8(word, mask3));
}
if (i < user_input.size()) {
alignas(32) uint8_t buffer[32]{};
memcpy(buffer, user_input.data() + i, user_input.size() - i);
__m256i word = _mm256_load_si256((const __m256i*)buffer);
running = _mm256_or_si256(
_mm256_or_si256(running,
_mm256_or_si256(_mm256_cmpeq_epi8(word, mask1),
_mm256_cmpeq_epi8(word, mask2))),
_mm256_cmpeq_epi8(word, mask3));
}
return _mm256_movemask_epi8(running) != 0;
}
#elif ADA_SSE2
ada_really_inline bool has_tabs_or_newline(
std::string_view user_input) noexcept {
Expand Down