OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 the V8 project authors. All rights reserved. | |
Dan Ehrenberg
2016/12/02 23:37:39
Rather than moving this to src/runtime/runtime-uti
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "src/runtime/runtime-utils.h" | |
6 | |
7 #include "src/base/logging.h" | |
8 #include "src/utils.h" | |
9 | |
10 namespace v8 { | |
11 namespace internal { | |
12 | |
13 #ifdef DEBUG | |
14 bool CheckFastAsciiConvert(char* dst, const char* src, int length, bool changed, | |
15 bool is_to_lower) { | |
16 bool expected_changed = false; | |
17 for (int i = 0; i < length; i++) { | |
18 if (dst[i] == src[i]) continue; | |
19 expected_changed = true; | |
20 if (is_to_lower) { | |
21 DCHECK('A' <= src[i] && src[i] <= 'Z'); | |
22 DCHECK(dst[i] == src[i] + ('a' - 'A')); | |
23 } else { | |
24 DCHECK('a' <= src[i] && src[i] <= 'z'); | |
25 DCHECK(dst[i] == src[i] - ('a' - 'A')); | |
26 } | |
27 } | |
28 return (expected_changed == changed); | |
29 } | |
30 #endif | |
31 | |
32 template <bool is_lower> | |
jungshik at Google
2016/12/02 06:54:41
Initially, |is_lower| is a function argument, but
Dan Ehrenberg
2016/12/02 23:35:08
Sounds good to me! What kind of effect does it hav
| |
33 bool FastAsciiConvert(char* dst, const char* src, int length, | |
34 bool* changed_out) { | |
35 #ifdef DEBUG | |
36 char* saved_dst = dst; | |
37 const char* saved_src = src; | |
38 #endif | |
39 DisallowHeapAllocation no_gc; | |
40 // We rely on the distance between upper and lower case letters | |
41 // being a known power of 2. | |
42 DCHECK('a' - 'A' == (1 << 5)); | |
43 // Boundaries for the range of input characters than require conversion. | |
44 static const char lo = is_lower ? 'A' - 1 : 'a' - 1; | |
45 static const char hi = is_lower ? 'Z' + 1 : 'z' + 1; | |
46 bool changed = false; | |
47 uintptr_t or_acc = 0; | |
48 const char* const limit = src + length; | |
49 | |
50 // dst is newly allocated and always aligned. | |
51 DCHECK(IsAligned(reinterpret_cast<intptr_t>(dst), sizeof(uintptr_t))); | |
52 // Only attempt processing one word at a time if src is also aligned. | |
53 if (IsAligned(reinterpret_cast<intptr_t>(src), sizeof(uintptr_t))) { | |
54 // Process the prefix of the input that requires no conversion one aligned | |
55 // (machine) word at a time. | |
56 while (src <= limit - sizeof(uintptr_t)) { | |
57 const uintptr_t w = *reinterpret_cast<const uintptr_t*>(src); | |
58 or_acc |= w; | |
59 if (AsciiRangeMask(w, lo, hi) != 0) { | |
60 changed = true; | |
61 break; | |
62 } | |
63 *reinterpret_cast<uintptr_t*>(dst) = w; | |
64 src += sizeof(uintptr_t); | |
65 dst += sizeof(uintptr_t); | |
66 } | |
67 // Process the remainder of the input performing conversion when | |
68 // required one word at a time. | |
69 while (src <= limit - sizeof(uintptr_t)) { | |
70 const uintptr_t w = *reinterpret_cast<const uintptr_t*>(src); | |
71 or_acc |= w; | |
72 uintptr_t m = AsciiRangeMask(w, lo, hi); | |
73 // The mask has high (7th) bit set in every byte that needs | |
74 // conversion and we know that the distance between cases is | |
75 // 1 << 5. | |
76 *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2); | |
77 src += sizeof(uintptr_t); | |
78 dst += sizeof(uintptr_t); | |
79 } | |
80 } | |
81 // Process the last few bytes of the input (or the whole input if | |
82 // unaligned access is not supported). | |
83 while (src < limit) { | |
84 char c = *src; | |
85 or_acc |= c; | |
86 if (lo < c && c < hi) { | |
87 c ^= (1 << 5); | |
88 changed = true; | |
89 } | |
90 *dst = c; | |
91 ++src; | |
92 ++dst; | |
93 } | |
94 | |
95 if ((or_acc & kAsciiMask) != 0) return false; | |
96 | |
97 DCHECK( | |
98 CheckFastAsciiConvert(saved_dst, saved_src, length, changed, is_lower)); | |
99 | |
100 *changed_out = changed; | |
101 return true; | |
102 } | |
103 | |
104 template bool FastAsciiConvert<false>(char* dst, const char* src, int length, | |
105 bool* changed_out); | |
106 template bool FastAsciiConvert<true>(char* dst, const char* src, int length, | |
107 bool* changed_out); | |
108 | |
109 } // namespace internal | |
110 } // namespace v8 | |
OLD | NEW |