Sort #include lines
[folly.git] / folly / detail / RangeSse42.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/detail/RangeSse42.h>
18
19 #include <glog/logging.h>
20
21 #include <folly/Portability.h>
22
23 //  Essentially, two versions of this file: one with an SSE42 implementation
24 //  and one with a fallback implementation. We determine which version to use by
25 //  testing for the presence of the required headers.
26 //
27 //  TODO: Maybe this should be done by the build system....
28 #if !FOLLY_SSE_PREREQ(4, 2)
29 namespace folly {
30 namespace detail {
31 size_t qfind_first_byte_of_sse42(const StringPieceLite haystack,
32                                  const StringPieceLite needles) {
33   return qfind_first_byte_of_nosse(haystack, needles);
34 }
35 }
36 }
37 # else
38 #include <cstdint>
39 #include <limits>
40 #include <string>
41
42 #include <emmintrin.h>
43 #include <nmmintrin.h>
44 #include <smmintrin.h>
45
46 #include <folly/Likely.h>
47
48 //  GCC 4.9 with ASAN has a problem: a function with no_sanitize_address calling
49 //  a function with always_inline fails to build. The _mm_* functions are marked
50 //  always_inline.
51 //  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
52 #if defined FOLLY_SANITIZE_ADDRESS && \
53     FOLLY_SANITIZE_ADDRESS == 1 && \
54     __GNUC_PREREQ(4, 9)
55 # define _mm_load_si128(p) (*(p))
56 # define _mm_loadu_si128(p) ((__m128i)__builtin_ia32_loaddqu((const char*)(p)))
57 # ifdef _mm_cmpestri
58 #  undef _mm_cmpestri
59 # endif
60 # define _mm_cmpestri(a, b, c, d, e) \
61   __builtin_ia32_pcmpestri128((__v16qi)(a), b, (__v16qi)(c), d, e)
62 #endif
63
64 namespace folly {
65 namespace detail {
66
67 // It's okay if pages are bigger than this (as powers of two), but they should
68 // not be smaller.
69 static constexpr size_t kMinPageSize = 4096;
70 static_assert(kMinPageSize >= 16,
71               "kMinPageSize must be at least SSE register size");
72
73 template <typename T>
74 static inline uintptr_t page_for(T* addr) {
75   return reinterpret_cast<uintptr_t>(addr) / kMinPageSize;
76 }
77
78 static inline size_t nextAlignedIndex(const char* arr) {
79    auto firstPossible = reinterpret_cast<uintptr_t>(arr) + 1;
80    return 1 +                       // add 1 because the index starts at 'arr'
81      ((firstPossible + 15) & ~0xF)  // round up to next multiple of 16
82      - firstPossible;
83 }
84
85 static size_t qfind_first_byte_of_needles16(const StringPieceLite haystack,
86                                             const StringPieceLite needles)
87   FOLLY_DISABLE_ADDRESS_SANITIZER;
88
89 // helper method for case where needles.size() <= 16
90 size_t qfind_first_byte_of_needles16(const StringPieceLite haystack,
91                                      const StringPieceLite needles) {
92   DCHECK_GT(haystack.size(), 0u);
93   DCHECK_GT(needles.size(), 0u);
94   DCHECK_LE(needles.size(), 16u);
95   if ((needles.size() <= 2 && haystack.size() >= 256) ||
96       // must bail if we can't even SSE-load a single segment of haystack
97       (haystack.size() < 16 &&
98        page_for(haystack.end() - 1) != page_for(haystack.data() + 15)) ||
99       // can't load needles into SSE register if it could cross page boundary
100       page_for(needles.end() - 1) != page_for(needles.data() + 15)) {
101     return detail::qfind_first_byte_of_nosse(haystack, needles);
102   }
103
104   auto arr2 = _mm_loadu_si128(
105       reinterpret_cast<const __m128i*>(needles.data()));
106   // do an unaligned load for first block of haystack
107   auto arr1 = _mm_loadu_si128(
108       reinterpret_cast<const __m128i*>(haystack.data()));
109   auto index =
110       _mm_cmpestri(arr2, int(needles.size()), arr1, int(haystack.size()), 0);
111   if (index < 16) {
112     return size_t(index);
113   }
114
115   // Now, we can do aligned loads hereafter...
116   size_t i = nextAlignedIndex(haystack.data());
117   for (; i < haystack.size(); i+= 16) {
118     arr1 =
119         _mm_load_si128(reinterpret_cast<const __m128i*>(haystack.data() + i));
120     index = _mm_cmpestri(
121         arr2, int(needles.size()), arr1, int(haystack.size() - i), 0);
122     if (index < 16) {
123       return i + index;
124     }
125   }
126   return std::string::npos;
127 }
128
129 template <bool HAYSTACK_ALIGNED>
130 size_t scanHaystackBlock(const StringPieceLite haystack,
131                          const StringPieceLite needles,
132                          uint64_t idx)
133 // Turn off ASAN because the "arr2 = ..." assignment in the loop below reads
134 // up to 15 bytes beyond end of the buffer in #needles#.  That is ok because
135 // ptr2 is always 16-byte aligned, so the read can never span a page boundary.
136 // Also, the extra data that may be read is never actually used.
137   FOLLY_DISABLE_ADDRESS_SANITIZER;
138
139 // Scans a 16-byte block of haystack (starting at blockStartIdx) to find first
140 // needle. If HAYSTACK_ALIGNED, then haystack must be 16byte aligned.
141 // If !HAYSTACK_ALIGNED, then caller must ensure that it is safe to load the
142 // block.
143 template <bool HAYSTACK_ALIGNED>
144 size_t scanHaystackBlock(const StringPieceLite haystack,
145                          const StringPieceLite needles,
146                          uint64_t blockStartIdx) {
147   DCHECK_GT(needles.size(), 16u); // should handled by *needles16() method
148   DCHECK(blockStartIdx + 16 <= haystack.size() ||
149          (page_for(haystack.data() + blockStartIdx) ==
150           page_for(haystack.data() + blockStartIdx + 15)));
151
152   __m128i arr1;
153   if (HAYSTACK_ALIGNED) {
154     arr1 = _mm_load_si128(
155         reinterpret_cast<const __m128i*>(haystack.data() + blockStartIdx));
156   } else {
157     arr1 = _mm_loadu_si128(
158         reinterpret_cast<const __m128i*>(haystack.data() + blockStartIdx));
159   }
160
161   // This load is safe because needles.size() >= 16
162   auto arr2 = _mm_loadu_si128(
163       reinterpret_cast<const __m128i*>(needles.data()));
164   auto b =
165       _mm_cmpestri(arr2, 16, arr1, int(haystack.size() - blockStartIdx), 0);
166
167   size_t j = nextAlignedIndex(needles.data());
168   for (; j < needles.size(); j += 16) {
169     arr2 = _mm_load_si128(
170         reinterpret_cast<const __m128i*>(needles.data() + j));
171
172     auto index = _mm_cmpestri(
173         arr2,
174         int(needles.size() - j),
175         arr1,
176         int(haystack.size() - blockStartIdx),
177         0);
178     b = std::min(index, b);
179   }
180
181   if (b < 16) {
182     return blockStartIdx + b;
183   }
184   return std::string::npos;
185 }
186
187 size_t qfind_first_byte_of_sse42(const StringPieceLite haystack,
188                                  const StringPieceLite needles);
189
190 size_t qfind_first_byte_of_sse42(const StringPieceLite haystack,
191                                  const StringPieceLite needles) {
192   if (UNLIKELY(needles.empty() || haystack.empty())) {
193     return std::string::npos;
194   } else if (needles.size() <= 16) {
195     // we can save some unnecessary load instructions by optimizing for
196     // the common case of needles.size() <= 16
197     return qfind_first_byte_of_needles16(haystack, needles);
198   }
199
200   if (haystack.size() < 16 &&
201       page_for(haystack.end() - 1) != page_for(haystack.data() + 16)) {
202     // We can't safely SSE-load haystack. Use a different approach.
203     if (haystack.size() <= 2) {
204       return qfind_first_byte_of_std(haystack, needles);
205     }
206     return qfind_first_byte_of_byteset(haystack, needles);
207   }
208
209   auto ret = scanHaystackBlock<false>(haystack, needles, 0);
210   if (ret != std::string::npos) {
211     return ret;
212   }
213
214   size_t i = nextAlignedIndex(haystack.data());
215   for (; i < haystack.size(); i += 16) {
216     ret = scanHaystackBlock<true>(haystack, needles, i);
217     if (ret != std::string::npos) {
218       return ret;
219     }
220   }
221
222   return std::string::npos;
223 }
224 }
225 }
226 #endif