forked from facebook/folly
-
Notifications
You must be signed in to change notification settings - Fork 0
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) - restored.
Summary: Faster equivalent to ``` std::ranges::find(haystack, needle) != haystack.end(); ``` This diff is just the interfaces. Differential Revision: D63635498
- Loading branch information
1 parent
982404a
commit c77d902
Showing
9 changed files
with
347 additions
and
5 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,80 @@ | ||
/* | ||
* 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 <iterator> | ||
|
||
namespace folly::simd { | ||
namespace detail { | ||
|
||
// no overloading for easier 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); | ||
|
||
template <typename R> | ||
using std_range_value_t = typename std::iterator_traits<decltype(std::begin( | ||
std::declval<R&>()))>::value_type; | ||
|
||
} // namespace detail | ||
|
||
/** | ||
* folly::simd::contains | ||
* folly::simd::contains_fn | ||
* | ||
* A vectorized version of `std::ranges::find(r, x) != r.end()`. | ||
* Only works for "simd friendly cases" - | ||
* specifically the ones where the type can be reasonably cast | ||
* to `uint8/16/32/64_t`. | ||
* | ||
**/ | ||
struct contains_fn { | ||
template < | ||
typename R, | ||
typename = std::enable_if_t< | ||
std::is_invocable_v<detail::AsSimdFriendlyUintFn, R>>> | ||
FOLLY_ERASE bool operator()(R&& r, detail::std_range_value_t<R> x) const { | ||
auto castR = detail::asSimdFriendlyUint(folly::span(r)); | ||
auto castX = detail::asSimdFriendlyUint(x); | ||
|
||
using T = decltype(castX); | ||
|
||
if constexpr (std::is_same_v<T, std::uint8_t>) { | ||
return detail::containsU8(castR, castX); | ||
} else if constexpr (std::is_same_v<T, std::uint16_t>) { | ||
return detail::containsU16(castR, castX); | ||
} else if constexpr (std::is_same_v<T, std::uint32_t>) { | ||
return detail::containsU32(castR, castX); | ||
} else { | ||
static_assert( | ||
std::is_same_v<T, std::uint64_t>, "internal error, unknown type"); | ||
return detail::containsU64(castR, 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
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.