Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
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/. */
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.
8 #include <emmintrin.h>
9 #include "nscore.h"
10 #include "nsAlgorithm.h"
11 #include "nsTextFragmentImpl.h"
12 #include <algorithm>
14 namespace mozilla {
15 namespace SSE2 {
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 }
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;
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 }
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 }
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 }
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 }
68 return -1;
69 }
71 } // namespace SSE2
72 } // namespace mozilla