-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simd::contains (the interfaces) (#2299)
Summary: Pull Request resolved: #2299 simdContains - everything but the actual handwritten algorithm. Reviewed By: Gownta Differential Revision: D63116101 fbshipit-source-id: 2c9b23f0111f0fa2f703ca13e8cd3a1097c685fd
- Loading branch information
1 parent
0d0e17a
commit b87bb9b
Showing
11 changed files
with
336 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <folly/algorithm/simd/Contains.h> | ||
|
||
#include <algorithm> | ||
#include <cstring> | ||
#include <folly/algorithm/simd/detail/ContainsImpl.h> | ||
|
||
namespace folly::simd::detail { | ||
|
||
bool containsU8(folly::span<const std::uint8_t> haystack, std::uint8_t needle) { | ||
return containsImpl(haystack, needle); | ||
} | ||
bool containsU16( | ||
folly::span<const std::uint16_t> haystack, std::uint16_t needle) { | ||
return containsImpl(haystack, needle); | ||
} | ||
bool containsU32( | ||
folly::span<const std::uint32_t> haystack, std::uint32_t needle) { | ||
return containsImpl(haystack, needle); | ||
} | ||
|
||
bool containsU64( | ||
folly::span<const std::uint64_t> haystack, std::uint64_t needle) { | ||
return containsImpl(haystack, needle); | ||
} | ||
|
||
} // namespace folly::simd::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <folly/CPortability.h> | ||
#include <folly/algorithm/simd/detail/Traits.h> | ||
|
||
#include <ranges> | ||
|
||
namespace folly::simd { | ||
namespace detail { | ||
|
||
// no overloading for easier of profiling. | ||
|
||
bool containsU8(folly::span<const std::uint8_t> haystack, std::uint8_t needle); | ||
bool containsU16( | ||
folly::span<const std::uint16_t> haystack, std::uint16_t needle); | ||
bool containsU32( | ||
folly::span<const std::uint32_t> haystack, std::uint32_t needle); | ||
bool containsU64( | ||
folly::span<const std::uint64_t> haystack, std::uint64_t needle); | ||
|
||
} // namespace detail | ||
|
||
struct contains_fn { | ||
template <std::ranges::contiguous_range R> | ||
requires simd::detail::has_integral_simd_friendly_equivalent< | ||
std::ranges::range_value_t<R>> | ||
FOLLY_ERASE bool operator()(R&& rng, std::ranges::range_value_t<R> x) const { | ||
auto castRng = simd::detail::asSimdFriendlyUint(folly::span(rng)); | ||
auto castX = simd::detail::asSimdFriendlyUint(x); | ||
|
||
using T = decltype(castX); | ||
|
||
if constexpr (std::is_same_v<T, std::uint8_t>) { | ||
return detail::containsU8(castRng, castX); | ||
} else if constexpr (std::is_same_v<T, std::uint16_t>) { | ||
return detail::containsU16(castRng, castX); | ||
} else if constexpr (std::is_same_v<T, std::uint32_t>) { | ||
return detail::containsU32(castRng, castX); | ||
} else { | ||
static_assert( | ||
std::is_same_v<T, std::uint64_t>, "internal error, unknown type"); | ||
return detail::containsU64(castRng, castX); | ||
} | ||
} | ||
}; | ||
|
||
inline constexpr contains_fn contains; | ||
|
||
} // namespace folly::simd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <cstring> | ||
#include <cwchar> | ||
#include <type_traits> | ||
|
||
#include <folly/CPortability.h> | ||
#include <folly/algorithm/simd/detail/SimdAnyOf.h> | ||
#include <folly/algorithm/simd/detail/SimdCharPlatform.h> | ||
#include <folly/container/span.h> | ||
|
||
namespace folly::simd::detail { | ||
|
||
/* | ||
* The functions in this file are FOLLY_ERASE to make sure | ||
* that the only place behind a call boundary is the explicit one. | ||
*/ | ||
|
||
template <typename T> | ||
FOLLY_ERASE bool containsImplStd(folly::span<const T> haystack, T needle) { | ||
static_assert( | ||
std::is_unsigned_v<T>, "we should only get here for uint8/16/32/64"); | ||
if constexpr (sizeof(T) == 1) { | ||
auto* ptr = reinterpret_cast<const char*>(haystack.data()); | ||
auto castNeedle = static_cast<char>(needle); | ||
if (haystack.empty()) { // memchr requires not null | ||
return false; | ||
} | ||
return std::memchr(ptr, castNeedle, haystack.size()) != nullptr; | ||
} else if constexpr (sizeof(T) == sizeof(wchar_t)) { | ||
auto* ptr = reinterpret_cast<const wchar_t*>(haystack.data()); | ||
auto castNeedle = static_cast<wchar_t>(needle); | ||
if (haystack.empty()) { // wmemchr requires not null | ||
return false; | ||
} | ||
return std::wmemchr(ptr, castNeedle, haystack.size()) != nullptr; | ||
} else { | ||
// Using find instead of any_of on an off chance that the standard library | ||
// will add some custom vectorization. | ||
// That wouldn't be possible for any_of because of the predicates. | ||
return std::find(haystack.begin(), haystack.end(), needle) != | ||
haystack.end(); | ||
} | ||
} | ||
|
||
template <typename T> | ||
constexpr bool hasHandwrittenContains() { | ||
return std::is_same_v<T, std::uint8_t> && | ||
!std::is_same_v<SimdCharPlatform, void>; | ||
} | ||
|
||
template <typename T> | ||
FOLLY_ERASE bool containsImplHandwritten( | ||
folly::span<const T> haystack, T needle) { | ||
static_assert(std::is_same_v<T, std::uint8_t>, ""); | ||
auto as_chars = folly::reinterpret_span_cast<const char>(haystack); | ||
return simdAnyOf<SimdCharPlatform, 4>( | ||
as_chars.data(), | ||
as_chars.data() + as_chars.size(), | ||
[&](SimdCharPlatform::reg_t x) { | ||
return SimdCharPlatform::equal(x, static_cast<char>(needle)); | ||
}); | ||
} | ||
|
||
template <typename T> | ||
FOLLY_ERASE bool containsImpl(folly::span<const T> haystack, T needle) { | ||
if constexpr (hasHandwrittenContains<T>()) { | ||
return containsImplHandwritten(haystack, needle); | ||
} else { | ||
return containsImplStd(haystack, needle); | ||
} | ||
} | ||
|
||
} // namespace folly::simd::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.