OLD | NEW |
1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2011, 2012 Apple Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 3 // found in the LICENSE file. |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Library General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Library General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Library General Public License | |
16 * along with this library; see the file COPYING.LIB. If not, write to | |
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
18 * Boston, MA 02110-1301, USA. | |
19 * | |
20 */ | |
21 | 4 |
22 #ifndef ASCIIFastPath_h | 5 #include "platform/wtf/text/ASCIIFastPath.h" |
23 #define ASCIIFastPath_h | |
24 | 6 |
25 #include "wtf/Alignment.h" | 7 // The contents of this header was moved to platform/wtf as part of |
26 #include "wtf/CPU.h" | 8 // WTF migration project. See the following post for details: |
27 #include "wtf/StdLibExtras.h" | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
28 #include "wtf/text/Unicode.h" | |
29 #include <stdint.h> | |
30 | |
31 #if OS(MACOSX) && (CPU(X86) || CPU(X86_64)) | |
32 #include <emmintrin.h> | |
33 #endif | |
34 | |
35 namespace WTF { | |
36 | |
37 // Assuming that a pointer is the size of a "machine word", then | |
38 // uintptr_t is an integer type that is also a machine word. | |
39 typedef uintptr_t MachineWord; | |
40 const uintptr_t machineWordAlignmentMask = sizeof(MachineWord) - 1; | |
41 | |
42 inline bool isAlignedToMachineWord(const void* pointer) { | |
43 return !(reinterpret_cast<uintptr_t>(pointer) & machineWordAlignmentMask); | |
44 } | |
45 | |
46 template <typename T> | |
47 inline T* alignToMachineWord(T* pointer) { | |
48 return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(pointer) & | |
49 ~machineWordAlignmentMask); | |
50 } | |
51 | |
52 template <size_t size, typename CharacterType> | |
53 struct NonASCIIMask; | |
54 template <> | |
55 struct NonASCIIMask<4, UChar> { | |
56 static inline uint32_t value() { return 0xFF80FF80U; } | |
57 }; | |
58 template <> | |
59 struct NonASCIIMask<4, LChar> { | |
60 static inline uint32_t value() { return 0x80808080U; } | |
61 }; | |
62 template <> | |
63 struct NonASCIIMask<8, UChar> { | |
64 static inline uint64_t value() { return 0xFF80FF80FF80FF80ULL; } | |
65 }; | |
66 template <> | |
67 struct NonASCIIMask<8, LChar> { | |
68 static inline uint64_t value() { return 0x8080808080808080ULL; } | |
69 }; | |
70 | |
71 template <typename CharacterType> | |
72 inline bool isAllASCII(MachineWord word) { | |
73 return !(word & NonASCIIMask<sizeof(MachineWord), CharacterType>::value()); | |
74 } | |
75 | |
76 // Note: This function assume the input is likely all ASCII, and | |
77 // does not leave early if it is not the case. | |
78 template <typename CharacterType> | |
79 inline bool charactersAreAllASCII(const CharacterType* characters, | |
80 size_t length) { | |
81 DCHECK_GT(length, 0u); | |
82 MachineWord allCharBits = 0; | |
83 const CharacterType* end = characters + length; | |
84 | |
85 // Prologue: align the input. | |
86 while (!isAlignedToMachineWord(characters) && characters != end) { | |
87 allCharBits |= *characters; | |
88 ++characters; | |
89 } | |
90 | |
91 // Compare the values of CPU word size. | |
92 const CharacterType* wordEnd = alignToMachineWord(end); | |
93 const size_t loopIncrement = sizeof(MachineWord) / sizeof(CharacterType); | |
94 while (characters < wordEnd) { | |
95 allCharBits |= *(reinterpret_cast_ptr<const MachineWord*>(characters)); | |
96 characters += loopIncrement; | |
97 } | |
98 | |
99 // Process the remaining bytes. | |
100 while (characters != end) { | |
101 allCharBits |= *characters; | |
102 ++characters; | |
103 } | |
104 | |
105 MachineWord nonASCIIBitMask = | |
106 NonASCIIMask<sizeof(MachineWord), CharacterType>::value(); | |
107 return !(allCharBits & nonASCIIBitMask); | |
108 } | |
109 | |
110 inline void copyLCharsFromUCharSource(LChar* destination, | |
111 const UChar* source, | |
112 size_t length) { | |
113 #if OS(MACOSX) && (CPU(X86) || CPU(X86_64)) | |
114 const uintptr_t memoryAccessSize = | |
115 16; // Memory accesses on 16 byte (128 bit) alignment | |
116 const uintptr_t memoryAccessMask = memoryAccessSize - 1; | |
117 | |
118 size_t i = 0; | |
119 for (; i < length && !isAlignedTo<memoryAccessMask>(&source[i]); ++i) { | |
120 DCHECK(!(source[i] & 0xff00)); | |
121 destination[i] = static_cast<LChar>(source[i]); | |
122 } | |
123 | |
124 const uintptr_t sourceLoadSize = | |
125 32; // Process 32 bytes (16 UChars) each iteration | |
126 const size_t ucharsPerLoop = sourceLoadSize / sizeof(UChar); | |
127 if (length > ucharsPerLoop) { | |
128 const size_t endLength = length - ucharsPerLoop + 1; | |
129 for (; i < endLength; i += ucharsPerLoop) { | |
130 #if DCHECK_IS_ON() | |
131 for (unsigned checkIndex = 0; checkIndex < ucharsPerLoop; ++checkIndex) | |
132 DCHECK(!(source[i + checkIndex] & 0xff00)); | |
133 #endif | |
134 __m128i first8UChars = | |
135 _mm_load_si128(reinterpret_cast<const __m128i*>(&source[i])); | |
136 __m128i second8UChars = | |
137 _mm_load_si128(reinterpret_cast<const __m128i*>(&source[i + 8])); | |
138 __m128i packedChars = _mm_packus_epi16(first8UChars, second8UChars); | |
139 _mm_storeu_si128(reinterpret_cast<__m128i*>(&destination[i]), | |
140 packedChars); | |
141 } | |
142 } | |
143 | |
144 for (; i < length; ++i) { | |
145 DCHECK(!(source[i] & 0xff00)); | |
146 destination[i] = static_cast<LChar>(source[i]); | |
147 } | |
148 #elif COMPILER(GCC) && CPU(ARM_NEON) && \ | |
149 !(CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN)) && defined(NDEBUG) | |
150 const LChar* const end = destination + length; | |
151 const uintptr_t memoryAccessSize = 8; | |
152 | |
153 if (length >= (2 * memoryAccessSize) - 1) { | |
154 // Prefix: align dst on 64 bits. | |
155 const uintptr_t memoryAccessMask = memoryAccessSize - 1; | |
156 while (!isAlignedTo<memoryAccessMask>(destination)) | |
157 *destination++ = static_cast<LChar>(*source++); | |
158 | |
159 // Vector interleaved unpack, we only store the lower 8 bits. | |
160 const uintptr_t lengthLeft = end - destination; | |
161 const LChar* const simdEnd = end - (lengthLeft % memoryAccessSize); | |
162 do { | |
163 asm("vld2.8 { d0-d1 }, [%[SOURCE]] !\n\t" | |
164 "vst1.8 { d0 }, [%[DESTINATION],:64] !\n\t" | |
165 : [SOURCE] "+r"(source), [DESTINATION] "+r"(destination) | |
166 : | |
167 : "memory", "d0", "d1"); | |
168 } while (destination != simdEnd); | |
169 } | |
170 | |
171 while (destination != end) | |
172 *destination++ = static_cast<LChar>(*source++); | |
173 #else | |
174 for (size_t i = 0; i < length; ++i) { | |
175 DCHECK(!(source[i] & 0xff00)); | |
176 destination[i] = static_cast<LChar>(source[i]); | |
177 } | |
178 #endif | |
179 } | |
180 | |
181 } // namespace WTF | |
182 | |
183 #endif // ASCIIFastPath_h | |
OLD | NEW |