Skip to content

Commit

Permalink
refactor validate_ascii_fast function after CR comment
Browse files Browse the repository at this point in the history
Signed-off-by: iko1 <me@remotecpp.dev>
  • Loading branch information
iko1 committed Jun 18, 2023
1 parent da4f5a0 commit acbe2e2
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/core/detail/bitpacking.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,35 @@ bool validate_ascii_fast(const char* src, size_t len) {
size_t i = 0;

// Initialize a vector in which all the elements are set to zero.
vector signed char has_error = vec_splat_s8(0);
vector unsigned char has_error = vec_splat_s8(0);
if (len >= 16) {
for (; i <= len - 16; i += 16) {
// Load 16 bytes from buffer into a vector.
vector signed char current_bytes = vec_load_len((signed char*)(src + i), 16);
vector unsigned char current_bytes = vec_load_len((signed char*)(src + i), 16);
// Perform a bitwise OR operation between the current and the previously loaded contents.
has_error = vec_orc(has_error, current_bytes);
}
}

int error_mask = 0;
for (int i = 0; i < 16; i++) {
if (has_error[i] & 0x80) {
return false;
}
// Initialize a vector in which all the elements are set to an invalid ASCII value.
vector unsigned char rep_invalid_values = vec_splat_s8(0x80);

// Perform bitwise AND-complement operation between two vectors.
vector unsigned char andc_result = vec_andc(rep_invalid_values, has_error);

// Tests whether any of corresponding elements of the given vectors are not equal.
// After the bitwise operation, both vectors should be equal if ASCII values.
if (!vec_all_eq(rep_invalid_values, andc_result)) {
return false;
}

char tail_has_error = 0;
for (; i < len; i++) {
tail_has_error |= src[i];
if (src[i] & 0x80) {
return false;
}
}
error_mask |= (tail_has_error & 0x80);

return !error_mask;
return true;
}
#else
bool validate_ascii_fast(const char* src, size_t len) {
Expand Down

0 comments on commit acbe2e2

Please sign in to comment.