| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,6 +35,7 @@ | |||
| 35 | 35 | #include "v8-fast-api-calls.h" | |
| 36 | 36 | #include "v8.h" | |
| 37 | 37 | ||
| 38 | + #include <stdint.h> | ||
| 38 | 39 | #include <climits> | |
| 39 | 40 | #include <cstring> | |
| 40 | 41 | #include "nbytes.h" | |
@@ -741,13 +742,40 @@ uint32_t FastByteLengthUtf8(Local<Value> receiver, | |||
| 741 | 742 | if (source.length > 128) { | |
| 742 | 743 | return simdutf::utf8_length_from_latin1(source.data, source.length); | |
| 743 | 744 | } | |
| 745 | + | ||
| 744 | 746 | uint32_t length = source.length; | |
| 745 | - uint32_t result = length; | ||
| 746 | - const uint8_t* data = reinterpret_cast<const uint8_t*>(source.data); | ||
| 747 | - for (uint32_t i = 0; i < length; ++i) { | ||
| 748 | - result += (data[i] >> 7); | ||
| 747 | + const auto input = reinterpret_cast<const uint8_t*>(source.data); | ||
| 748 | + | ||
| 749 | + uint32_t answer = length; | ||
| 750 | + uint32_t i = 0; | ||
| 751 | + | ||
| 752 | + auto pop = [](uint64_t v) { | ||
| 753 | + return static_cast<size_t>(((v >> 7) & UINT64_C(0x0101010101010101)) * | ||
| 754 | + UINT64_C(0x0101010101010101) >> | ||
| 755 | + 56); | ||
| 756 | + }; | ||
| 757 | + | ||
| 758 | + for (; i + 32 <= length; i += 32) { | ||
| 759 | + uint64_t v; | ||
| 760 | + memcpy(&v, input + i, 8); | ||
| 761 | + answer += pop(v); | ||
| 762 | + memcpy(&v, input + i + 8, 8); | ||
| 763 | + answer += pop(v); | ||
| 764 | + memcpy(&v, input + i + 16, 8); | ||
| 765 | + answer += pop(v); | ||
| 766 | + memcpy(&v, input + i + 24, 8); | ||
| 767 | + answer += pop(v); | ||
| 768 | + } | ||
| 769 | + for (; i + 8 <= length; i += 8) { | ||
| 770 | + uint64_t v; | ||
| 771 | + memcpy(&v, input + i, 8); | ||
| 772 | + answer += pop(v); | ||
| 749 | 773 | } | |
| 750 | - return result; | ||
| 774 | + for (; i + 1 <= length; i += 1) { | ||
| 775 | + answer += input[i] >> 7; | ||
| 776 | + } | ||
| 777 | + | ||
| 778 | + return answer; | ||
| 751 | 779 | } | |
| 752 | 780 | ||
| 753 | 781 | static v8::CFunction fast_byte_length_utf8( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments