tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CharacterDataBufferGeneric.h (2062B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_dom_CharacterDataBufferGeneric_h
      8 #define mozilla_dom_CharacterDataBufferGeneric_h
      9 
     10 #include <algorithm>
     11 
     12 #include "CharacterDataBufferGenericFwd.h"
     13 #include "CharacterDataBufferImpl.h"
     14 #include "nscore.h"
     15 
     16 namespace mozilla {
     17 
     18 template <class Arch>
     19 int32_t FirstNon8Bit(const char16_t* str, const char16_t* end) {
     20  const uint32_t numUnicharsPerVector = xsimd::batch<int16_t, Arch>::size;
     21  using p = Non8BitParameters<sizeof(size_t)>;
     22  const size_t mask = p::mask();
     23  const uint32_t numUnicharsPerWord = p::numUnicharsPerWord();
     24  const int32_t len = end - str;
     25  int32_t i = 0;
     26 
     27  // Align ourselves to the Arch boundary
     28  int32_t alignLen = std::min(
     29      len, int32_t(((-NS_PTR_TO_INT32(str)) & (Arch::alignment() - 1)) /
     30                   sizeof(char16_t)));
     31  for (; i < alignLen; i++) {
     32    if (str[i] > 255) return i;
     33  }
     34 
     35  // Check one batch at a time.
     36  const int32_t vectWalkEnd =
     37      ((len - i) / numUnicharsPerVector) * numUnicharsPerVector;
     38  const uint16_t shortMask = 0xff00;
     39  xsimd::batch<int16_t, Arch> vectmask(static_cast<int16_t>(shortMask));
     40  for (; i < vectWalkEnd; i += numUnicharsPerVector) {
     41    const auto vect = xsimd::batch<int16_t, Arch>::load_aligned(str + i);
     42    if (xsimd::any((vect & vectmask) != 0)) return i;
     43  }
     44 
     45  // Check one word at a time.
     46  const int32_t wordWalkEnd =
     47      ((len - i) / numUnicharsPerWord) * numUnicharsPerWord;
     48  for (; i < wordWalkEnd; i += numUnicharsPerWord) {
     49    const size_t word = *reinterpret_cast<const size_t*>(str + i);
     50    if (word & mask) return i;
     51  }
     52 
     53  // Take care of the remainder one character at a time.
     54  for (; i < len; i++) {
     55    if (str[i] > 255) {
     56      return i;
     57    }
     58  }
     59 
     60  return -1;
     61 }
     62 
     63 }  // namespace mozilla
     64 
     65 #endif