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.
simdContains (the interfaces) (facebook#2299)
Summary: Pull Request resolved: facebook#2299 simdContains - everything but the actual handwritten algorithm. Differential Revision: D63116101
- Loading branch information
1 parent
0025cba
commit c48964c
Showing
8 changed files
with
306 additions
and
0 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,43 @@ | ||
/* | ||
* 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/SimdContains.h> | ||
|
||
#include <algorithm> | ||
#include <cstring> | ||
#include <folly/algorithm/simd/detail/SimdContainsImpl.h> | ||
|
||
namespace folly::simd_detail { | ||
|
||
bool simdContainsU8( | ||
folly::span<const std::uint8_t> haystack, std::uint8_t needle) { | ||
return simdContainsImpl(haystack, needle); | ||
} | ||
bool simdContainsU16( | ||
folly::span<const std::uint16_t> haystack, std::uint16_t needle) { | ||
return simdContainsImpl(haystack, needle); | ||
} | ||
bool simdContainsU32( | ||
folly::span<const std::uint32_t> haystack, std::uint32_t needle) { | ||
return simdContainsImpl(haystack, needle); | ||
} | ||
|
||
bool simdContainsU64( | ||
folly::span<const std::uint64_t> haystack, std::uint64_t needle) { | ||
return simdContainsImpl(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,63 @@ | ||
/* | ||
* 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/algorithm/simd/detail/Traits.h> | ||
|
||
#include <ranges> | ||
|
||
namespace folly { | ||
namespace simd_detail { | ||
|
||
// no overloading for easier of profiling. | ||
|
||
bool simdContainsU8( | ||
folly::span<const std::uint8_t> haystack, std::uint8_t needle); | ||
bool simdContainsU16( | ||
folly::span<const std::uint16_t> haystack, std::uint16_t needle); | ||
bool simdContainsU32( | ||
folly::span<const std::uint32_t> haystack, std::uint32_t needle); | ||
bool simdContainsU64( | ||
folly::span<const std::uint64_t> haystack, std::uint64_t needle); | ||
|
||
} // namespace simd_detail | ||
|
||
struct simd_contains_fn { | ||
template <std::ranges::contiguous_range R> | ||
requires detail::has_integral_simd_friendly_equivalent< | ||
std::ranges::range_value_t<R>> | ||
bool operator()(R&& rng, std::ranges::range_value_t<R> x) const { | ||
auto castRng = detail::asSimdFriendlyUint(folly::span(rng)); | ||
auto castX = detail::asSimdFriendlyUint(x); | ||
|
||
using T = decltype(castX); | ||
|
||
if constexpr (std::is_same_v<T, std::uint8_t>) { | ||
return simd_detail::simdContainsU8(castRng, castX); | ||
} else if constexpr (std::is_same_v<T, std::uint16_t>) { | ||
return simd_detail::simdContainsU16(castRng, castX); | ||
} else if constexpr (std::is_same_v<T, std::uint32_t>) { | ||
return simd_detail::simdContainsU32(castRng, castX); | ||
} else { | ||
static_assert( | ||
std::is_same_v<T, std::uint64_t>, "internal error, unknown type"); | ||
return simd_detail::simdContainsU64(castRng, castX); | ||
} | ||
} | ||
} inline constexpr simd_contains; | ||
|
||
} // namespace folly |
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,87 @@ | ||
/* | ||
* 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 funcitons in this file are FOLLY_ALWAYS_INLINE to make sure | ||
* that the only place behind a call boundary is the explicit one. | ||
*/ | ||
|
||
template <typename T> | ||
FOLLY_ALWAYS_INLINE bool simdContainsImplStd( | ||
folly::span<const T> haystack, T needle) { | ||
if constexpr (sizeof(T) == 1) { | ||
auto* ptr = reinterpret_cast<const char*>(haystack.data()); | ||
if (haystack.empty()) { // memchr requires not null | ||
return false; | ||
} | ||
return std::memchr(ptr, needle, haystack.size()) != nullptr; | ||
} else if constexpr (sizeof(T) == sizeof(wchar_t)) { | ||
auto* ptr = reinterpret_cast<const wchar_t*>(haystack.data()); | ||
if (haystack.empty()) { // wmemchr requires not null | ||
return false; | ||
} | ||
return std::wmemchr(ptr, needle, haystack.size()) != nullptr; | ||
} else { | ||
return std::any_of(haystack.begin(), haystack.end(), [needle](T x) { | ||
return x == needle; | ||
}); | ||
} | ||
} | ||
|
||
template <typename T> | ||
constexpr bool hasHandwrittenSimdContains() { | ||
return std::is_same_v<T, std::uint8_t> && | ||
!std::is_same_v<SimdCharPlatform, void>; | ||
} | ||
|
||
template <typename T> | ||
FOLLY_ALWAYS_INLINE bool simdContainsImplHandwritten( | ||
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_ALWAYS_INLINE bool simdContainsImpl( | ||
folly::span<const T> haystack, T needle) { | ||
if constexpr (hasHandwrittenSimdContains<T>()) { | ||
return simdContainsImplHandwritten(haystack, needle); | ||
} else { | ||
return simdContainsImplStd(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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/SimdContains.h> | ||
|
||
#include <folly/algorithm/simd/detail/SimdContainsImpl.h> | ||
|
||
#include <folly/portability/GTest.h> | ||
|
||
namespace folly { | ||
|
||
struct SimdContainsTest : ::testing::Test {}; | ||
|
||
template <typename T> | ||
void testSimdContainsVerify(std::span<T> haystack, T needle, bool expected) { | ||
bool actual1 = simd_contains(haystack, needle); | ||
ASSERT_EQ(expected, actual1); | ||
|
||
auto const_haystack = folly::static_span_cast<const T>(haystack); | ||
|
||
if constexpr ( | ||
std::is_same_v<T, std::uint8_t> || std::is_same_v<T, std::uint16_t> || | ||
std::is_same_v<T, std::uint32_t> || std::is_same_v<T, std::uint64_t>) { | ||
bool actual2 = simd_detail::simdContainsImplStd(const_haystack, needle); | ||
ASSERT_EQ(expected, actual2) << " haystack.size(): " << haystack.size(); | ||
} | ||
|
||
if constexpr (std::is_same_v<T, std::uint8_t>) { | ||
bool actual3 = | ||
simd_detail::simdContainsImplHandwritten(const_haystack, needle); | ||
ASSERT_EQ(expected, actual3) << " haystack.size(): " << haystack.size(); | ||
} | ||
} | ||
|
||
template <typename T> | ||
void testSimdContains() { | ||
for (std::size_t size = 0; size != 100; ++size) { | ||
std::vector<T> buf(size, T{0}); | ||
for (std::size_t offset = 0; offset != std::min(32UL, buf.size()); | ||
++offset) { | ||
folly::span searching(buf.begin() + offset, buf.end()); | ||
T needle{1}; | ||
testSimdContainsVerify(searching, needle, /*expected*/ false); | ||
|
||
for (auto& x : searching) { | ||
x = needle; | ||
testSimdContainsVerify(searching, needle, /*expected*/ true); | ||
x = 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
TEST_F(SimdContainsTest, AllTypes) { | ||
testSimdContains<std::int8_t>(); | ||
testSimdContains<std::int16_t>(); | ||
testSimdContains<std::int32_t>(); | ||
testSimdContains<std::int64_t>(); | ||
testSimdContains<std::uint8_t>(); | ||
testSimdContains<std::uint16_t>(); | ||
testSimdContains<std::uint32_t>(); | ||
testSimdContains<std::uint64_t>(); | ||
} | ||
|
||
} // namespace folly |