Index: src/runtime/runtime-utils.cc |
diff --git a/src/runtime/runtime-utils.cc b/src/runtime/runtime-utils.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ccfb90de78e2fe3cb901a772a948a14038d807b6 |
--- /dev/null |
+++ b/src/runtime/runtime-utils.cc |
@@ -0,0 +1,110 @@ |
+// Copyright 2016 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "src/runtime/runtime-utils.h" |
+ |
+#include "src/base/logging.h" |
+#include "src/utils.h" |
+ |
+namespace v8 { |
+namespace internal { |
+ |
+#ifdef DEBUG |
+bool CheckFastAsciiConvert(char* dst, const char* src, int length, bool changed, |
+ bool is_to_lower) { |
+ bool expected_changed = false; |
+ for (int i = 0; i < length; i++) { |
+ if (dst[i] == src[i]) continue; |
+ expected_changed = true; |
+ if (is_to_lower) { |
+ DCHECK('A' <= src[i] && src[i] <= 'Z'); |
+ DCHECK(dst[i] == src[i] + ('a' - 'A')); |
+ } else { |
+ DCHECK('a' <= src[i] && src[i] <= 'z'); |
+ DCHECK(dst[i] == src[i] - ('a' - 'A')); |
+ } |
+ } |
+ return (expected_changed == changed); |
+} |
+#endif |
+ |
+template <bool is_lower> |
+bool FastAsciiConvert(char* dst, const char* src, int length, |
+ bool* changed_out) { |
+#ifdef DEBUG |
+ char* saved_dst = dst; |
+ const char* saved_src = src; |
+#endif |
+ DisallowHeapAllocation no_gc; |
+ // We rely on the distance between upper and lower case letters |
+ // being a known power of 2. |
+ DCHECK('a' - 'A' == (1 << 5)); |
+ // Boundaries for the range of input characters than require conversion. |
+ static const char lo = is_lower ? 'A' - 1 : 'a' - 1; |
+ static const char hi = is_lower ? 'Z' + 1 : 'z' + 1; |
+ bool changed = false; |
+ uintptr_t or_acc = 0; |
+ const char* const limit = src + length; |
+ |
+ // dst is newly allocated and always aligned. |
+ DCHECK(IsAligned(reinterpret_cast<intptr_t>(dst), sizeof(uintptr_t))); |
+ // Only attempt processing one word at a time if src is also aligned. |
+ if (IsAligned(reinterpret_cast<intptr_t>(src), sizeof(uintptr_t))) { |
+ // Process the prefix of the input that requires no conversion one aligned |
+ // (machine) word at a time. |
+ while (src <= limit - sizeof(uintptr_t)) { |
+ const uintptr_t w = *reinterpret_cast<const uintptr_t*>(src); |
+ or_acc |= w; |
+ if (AsciiRangeMask(w, lo, hi) != 0) { |
+ changed = true; |
+ break; |
+ } |
+ *reinterpret_cast<uintptr_t*>(dst) = w; |
+ src += sizeof(uintptr_t); |
+ dst += sizeof(uintptr_t); |
+ } |
+ // Process the remainder of the input performing conversion when |
+ // required one word at a time. |
+ while (src <= limit - sizeof(uintptr_t)) { |
+ const uintptr_t w = *reinterpret_cast<const uintptr_t*>(src); |
+ or_acc |= w; |
+ uintptr_t m = AsciiRangeMask(w, lo, hi); |
+ // The mask has high (7th) bit set in every byte that needs |
+ // conversion and we know that the distance between cases is |
+ // 1 << 5. |
+ *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2); |
+ src += sizeof(uintptr_t); |
+ dst += sizeof(uintptr_t); |
+ } |
+ } |
+ // Process the last few bytes of the input (or the whole input if |
+ // unaligned access is not supported). |
+ while (src < limit) { |
+ char c = *src; |
+ or_acc |= c; |
+ if (lo < c && c < hi) { |
+ c ^= (1 << 5); |
+ changed = true; |
+ } |
+ *dst = c; |
+ ++src; |
+ ++dst; |
+ } |
+ |
+ if ((or_acc & kAsciiMask) != 0) return false; |
+ |
+ DCHECK( |
+ CheckFastAsciiConvert(saved_dst, saved_src, length, changed, is_lower)); |
+ |
+ *changed_out = changed; |
+ return true; |
+} |
+ |
+template bool FastAsciiConvert<false>(char* dst, const char* src, int length, |
+ bool* changed_out); |
+template bool FastAsciiConvert<true>(char* dst, const char* src, int length, |
+ bool* changed_out); |
+ |
+} // namespace internal |
+} // namespace v8 |