summaryrefslogtreecommitdiff
path: root/xpcom/string/nsReadableUtilsSSE2.cpp
blob: fe01d57afc06545590c3515842d025dc549e703a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <emmintrin.h>

#include "nsReadableUtilsImpl.h"

namespace mozilla {
namespace SSE2 {

static inline bool
is_zero (__m128i x)
{
  return
    _mm_movemask_epi8(_mm_cmpeq_epi8(x, _mm_setzero_si128())) == 0xffff;
}

int32_t
FirstNonASCII(const char16_t* aBegin, const char16_t* aEnd)
{
  const size_t kNumUnicharsPerVector = sizeof(__m128i) / sizeof(char16_t);
  typedef NonASCIIParameters<sizeof(size_t)> p;
  const size_t kMask = p::mask();
  const uintptr_t kXmmAlignMask = 0xf;
  const uint16_t kShortMask = 0xff80;
  const size_t kNumUnicharsPerWord = p::numUnicharsPerWord();

  const char16_t* idx = aBegin;

  // Align ourselves to a 16-byte boundary as required by _mm_load_si128
  for (; idx != aEnd && ((uintptr_t(idx) & kXmmAlignMask) != 0); idx++) {
    if (!IsASCII(*idx)) {
      return idx - aBegin;
    }
  }

  // Check one XMM register (16 bytes) at a time.
  const char16_t* vectWalkEnd = aligned(aEnd, kXmmAlignMask);
  __m128i vectmask = _mm_set1_epi16(static_cast<int16_t>(kShortMask));
  for (; idx != vectWalkEnd; idx += kNumUnicharsPerVector) {
    const __m128i vect = *reinterpret_cast<const __m128i*>(idx);
    if (!is_zero(_mm_and_si128(vect, vectmask))) {
      return idx - aBegin;
    }
  }

  // Check one word at a time.
  const char16_t* wordWalkEnd = aligned(aEnd, p::alignMask());
  for(; idx != wordWalkEnd; idx += kNumUnicharsPerWord) {
    const size_t word = *reinterpret_cast<const size_t*>(idx);
    if (word & kMask) {
      return idx - aBegin;
    }
  }

  // Take care of the remainder one character at a time.
  for (; idx != aEnd; idx++) {
    if (!IsASCII(*idx)) {
      return idx - aBegin;
    }
  }

  return -1;
}

} // namespace SSE2
} // namespace mozilla