Skip to content

Commit

Permalink
folly::simd::contains, all types. (facebook#2306)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#2306

extended the SimdCharPlatform => SimdPlatform to support all unsigned types.

Reviewed By: Gownta

Differential Revision: D63548781
  • Loading branch information
DenisYaroshevskiy authored and facebook-github-bot committed Oct 3, 2024
1 parent 0794fb9 commit a19e10d
Show file tree
Hide file tree
Showing 17 changed files with 543 additions and 338 deletions.
7 changes: 4 additions & 3 deletions folly/algorithm/simd/detail/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ cpp_library(
)

cpp_library(
name = "simd_char_platform",
headers = ["SimdCharPlatform.h"],
name = "simd_platform",
headers = ["SimdPlatform.h"],
exported_deps = [
"//folly:portability",
"//folly/algorithm/simd:ignore",
Expand All @@ -31,7 +31,7 @@ cpp_library(
headers = ["ContainsImpl.h"],
exported_deps = [
":simd_any_of",
":simd_char_platform",
":simd_platform",
"//folly:c_portability",
"//folly/container:span",
],
Expand All @@ -45,6 +45,7 @@ cpp_library(
"//folly:c_portability",
"//folly:traits",
"//folly/algorithm/simd:ignore",
"//folly/lang:align",
],
)

Expand Down
20 changes: 10 additions & 10 deletions folly/algorithm/simd/detail/ContainsImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <folly/CPortability.h>
#include <folly/algorithm/simd/detail/SimdAnyOf.h>
#include <folly/algorithm/simd/detail/SimdCharPlatform.h>
#include <folly/algorithm/simd/detail/SimdPlatform.h>
#include <folly/container/span.h>

namespace folly::simd::detail {
Expand Down Expand Up @@ -62,20 +62,20 @@ FOLLY_ERASE bool containsImplStd(folly::span<const T> haystack, T needle) {

template <typename T>
constexpr bool hasHandwrittenContains() {
return std::is_same_v<T, std::uint8_t> &&
!std::is_same_v<SimdCharPlatform, void>;
return !std::is_same_v<SimdPlatform<T>, void> &&
(std::is_same_v<std::uint8_t, T> || std::is_same_v<std::uint16_t, T> ||
std::is_same_v<std::uint32_t, T> || std::is_same_v<std::uint64_t, T>);
}

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));
static_assert(!std::is_same_v<SimdPlatform<T>, void>, "");
return simdAnyOf<SimdPlatform<T>, 4>(
haystack.data(),
haystack.data() + haystack.size(),
[&](typename SimdPlatform<T>::reg_t x) {
return SimdPlatform<T>::equal(x, static_cast<T>(needle));
});
}

Expand Down
267 changes: 0 additions & 267 deletions folly/algorithm/simd/detail/SimdCharPlatform.h

This file was deleted.

10 changes: 4 additions & 6 deletions folly/algorithm/simd/detail/SimdForEach.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <folly/Traits.h>
#include <folly/algorithm/simd/Ignore.h>
#include <folly/algorithm/simd/detail/UnrollUtils.h>
#include <folly/lang/Align.h>

#include <array>
#include <cstdint>
Expand Down Expand Up @@ -67,15 +68,12 @@ FOLLY_ALWAYS_INLINE void simdForEachAligning(
/**
* previousAlignedAddress
*
* Given a pointer returns a closest pointer aligned to a given size.
* (it just masks out some lower bits)
* Given a pointer returns a closest pointer aligned to a given size
* (in elements).
*/
template <typename T>
FOLLY_ALWAYS_INLINE T* previousAlignedAddress(T* ptr, int to) {
std::uintptr_t uptr = reinterpret_cast<std::uintptr_t>(ptr);
std::uintptr_t uto = static_cast<std::uintptr_t>(to);
uptr &= ~(uto - 1);
return reinterpret_cast<T*>(uptr);
return align_floor(ptr, sizeof(T) * to);
}

/**
Expand Down
Loading

0 comments on commit a19e10d

Please sign in to comment.