|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 // This file should only be compiled if you're on x86 or x86_64. Additionally, |
|
6 // you'll need to compile this file with -msse2 if you're using gcc. |
|
7 |
|
8 #include <emmintrin.h> |
|
9 #include "nscore.h" |
|
10 #include "nsAlgorithm.h" |
|
11 #include "nsTextFragmentImpl.h" |
|
12 #include <algorithm> |
|
13 |
|
14 namespace mozilla { |
|
15 namespace SSE2 { |
|
16 |
|
17 static inline bool |
|
18 is_zero (__m128i x) |
|
19 { |
|
20 return |
|
21 _mm_movemask_epi8(_mm_cmpeq_epi8(x, _mm_setzero_si128())) == 0xffff; |
|
22 } |
|
23 |
|
24 int32_t |
|
25 FirstNon8Bit(const char16_t *str, const char16_t *end) |
|
26 { |
|
27 const uint32_t numUnicharsPerVector = 8; |
|
28 typedef Non8BitParameters<sizeof(size_t)> p; |
|
29 const size_t mask = p::mask(); |
|
30 const uint32_t numUnicharsPerWord = p::numUnicharsPerWord(); |
|
31 const int32_t len = end - str; |
|
32 int32_t i = 0; |
|
33 |
|
34 // Align ourselves to a 16-byte boundary, as required by _mm_load_si128 |
|
35 // (i.e. MOVDQA). |
|
36 int32_t alignLen = |
|
37 std::min(len, int32_t(((-NS_PTR_TO_INT32(str)) & 0xf) / sizeof(char16_t))); |
|
38 for (; i < alignLen; i++) { |
|
39 if (str[i] > 255) |
|
40 return i; |
|
41 } |
|
42 |
|
43 // Check one XMM register (16 bytes) at a time. |
|
44 const int32_t vectWalkEnd = ((len - i) / numUnicharsPerVector) * numUnicharsPerVector; |
|
45 const uint16_t shortMask = 0xff00; |
|
46 __m128i vectmask = _mm_set1_epi16(static_cast<int16_t>(shortMask)); |
|
47 for(; i < vectWalkEnd; i += numUnicharsPerVector) { |
|
48 const __m128i vect = *reinterpret_cast<const __m128i*>(str + i); |
|
49 if (!is_zero(_mm_and_si128(vect, vectmask))) |
|
50 return i; |
|
51 } |
|
52 |
|
53 // Check one word at a time. |
|
54 const int32_t wordWalkEnd = ((len - i) / numUnicharsPerWord) * numUnicharsPerWord; |
|
55 for(; i < wordWalkEnd; i += numUnicharsPerWord) { |
|
56 const size_t word = *reinterpret_cast<const size_t*>(str + i); |
|
57 if (word & mask) |
|
58 return i; |
|
59 } |
|
60 |
|
61 // Take care of the remainder one character at a time. |
|
62 for (; i < len; i++) { |
|
63 if (str[i] > 255) { |
|
64 return i; |
|
65 } |
|
66 } |
|
67 |
|
68 return -1; |
|
69 } |
|
70 |
|
71 } // namespace SSE2 |
|
72 } // namespace mozilla |