-
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.
Summary: Pull Request resolved: #2279 extracting the common "simd-friendly" type helpers. Differential Revision: D61205292
- Loading branch information
1 parent
dc28381
commit 344e22a
Showing
7 changed files
with
266 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,6 @@ cpp_library( | |
exported_deps = [ | ||
":movemask", | ||
"//folly:portability", | ||
"//folly/algorithm/simd/detail:traits", | ||
], | ||
) |
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,100 @@ | ||
/* | ||
* 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/Memory.h> | ||
#include <folly/Traits.h> | ||
#include <folly/container/span.h> | ||
|
||
#include <concepts> | ||
#include <type_traits> | ||
|
||
namespace folly::detail { | ||
|
||
template <typename T> | ||
auto findSimdFriendlyEquivalent() { | ||
if constexpr (std::is_enum_v<T>) { | ||
return findSimdFriendlyEquivalent<std::underlying_type_t<T>>(); | ||
} else if constexpr (std::is_floating_point_v<T>) { | ||
if constexpr (sizeof(T) == 4) { | ||
return float{}; | ||
} else { | ||
return double{}; | ||
} | ||
} else if constexpr (std::is_signed_v<T>) { | ||
if constexpr (sizeof(T) == 1) { | ||
return std::int8_t{}; | ||
} else if constexpr (sizeof(T) == 2) { | ||
return std::int16_t{}; | ||
} else if constexpr (sizeof(T) == 4) { | ||
return std::int32_t{}; | ||
} else if constexpr (sizeof(T) == 8) { | ||
return std::int64_t{}; | ||
} | ||
} else if constexpr (std::is_unsigned_v<T>) { | ||
if constexpr (sizeof(T) == 1) { | ||
return std::uint8_t{}; | ||
} else if constexpr (sizeof(T) == 2) { | ||
return std::uint16_t{}; | ||
} else if constexpr (sizeof(T) == 4) { | ||
return std::uint32_t{}; | ||
} else if constexpr (sizeof(T) == 8) { | ||
return std::uint64_t{}; | ||
} | ||
} | ||
} | ||
|
||
template <typename T> | ||
concept has_simd_friendly_equivalent = | ||
!std::is_same_v<void, decltype(findSimdFriendlyEquivalent<T>())>; | ||
|
||
template <has_simd_friendly_equivalent T> | ||
using simd_friendly_equivalent_t = folly::like_t< // | ||
T, | ||
decltype(findSimdFriendlyEquivalent<std::remove_const_t<T>>())>; | ||
|
||
template <typename T> | ||
concept has_integral_simd_friendly_equivalent = | ||
has_simd_friendly_equivalent<T> && // have to explicitly specify this for | ||
// subsumption to work | ||
std::integral<simd_friendly_equivalent_t<T>>; | ||
|
||
template <has_integral_simd_friendly_equivalent T> | ||
using integral_simd_friendly_equivalent = simd_friendly_equivalent_t<T>; | ||
|
||
template <has_simd_friendly_equivalent T, std::size_t Extend> | ||
auto asSimdFriendly(folly::span<T, Extend> s) { | ||
return folly::reinterpret_span_cast<simd_friendly_equivalent_t<T>>(s); | ||
} | ||
|
||
template <has_simd_friendly_equivalent T> | ||
constexpr auto asSimdFriendly(T x) { | ||
return static_cast<simd_friendly_equivalent_t<T>>(x); | ||
} | ||
|
||
template <has_simd_friendly_equivalent T, std::size_t Extend> | ||
auto asSimdFriendlyUint(folly::span<T, Extend> s) { | ||
return folly::reinterpret_span_cast< | ||
folly::like_t<T, uint_bits_t<sizeof(T) * 8>>>(s); | ||
} | ||
|
||
template <has_simd_friendly_equivalent T> | ||
constexpr auto asSimdFriendlyUint(T x) { | ||
return static_cast<uint_bits_t<sizeof(T) * 8>>(x); | ||
} | ||
|
||
} // namespace folly::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,139 @@ | ||
/* | ||
* 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/detail/Traits.h> | ||
|
||
#include <folly/portability/GMock.h> | ||
#include <folly/portability/GTest.h> | ||
|
||
namespace folly::detail { | ||
|
||
struct FollySimdTraitsTest : testing::Test {}; | ||
|
||
namespace simd_friendly_equivalent_test { | ||
|
||
// ints | ||
static_assert( | ||
std::is_same_v<std::int8_t, simd_friendly_equivalent_t<signed char>>); | ||
static_assert( | ||
std::is_same_v<std::uint8_t, simd_friendly_equivalent_t<unsigned char>>); | ||
|
||
static_assert(std::is_same_v<std::int16_t, simd_friendly_equivalent_t<short>>); | ||
static_assert( | ||
std::is_same_v<std::uint16_t, simd_friendly_equivalent_t<unsigned short>>); | ||
|
||
static_assert(std::is_same_v<std::int32_t, simd_friendly_equivalent_t<int>>); | ||
static_assert( | ||
std::is_same_v<std::uint32_t, simd_friendly_equivalent_t<unsigned int>>); | ||
|
||
static_assert( | ||
std::is_same_v<std::int64_t, simd_friendly_equivalent_t<std::int64_t>>); | ||
static_assert( | ||
std::is_same_v<std::uint64_t, simd_friendly_equivalent_t<std::uint64_t>>); | ||
|
||
// floats | ||
static_assert(std::is_same_v<float, simd_friendly_equivalent_t<float>>); | ||
static_assert(std::is_same_v<double, simd_friendly_equivalent_t<double>>); | ||
|
||
// enum | ||
enum SomeInt {}; | ||
enum class SomeIntClass : std::int32_t {}; | ||
|
||
static_assert( | ||
std::is_same_v<std::uint32_t, simd_friendly_equivalent_t<SomeInt>>); | ||
static_assert( | ||
std::is_same_v<std::int32_t, simd_friendly_equivalent_t<SomeIntClass>>); | ||
|
||
// const | ||
|
||
static_assert( | ||
std::is_same_v<const std::int32_t, simd_friendly_equivalent_t<const int>>); | ||
|
||
// sfinae | ||
constexpr auto sfinae_call = | ||
[]<typename T>(T) -> simd_friendly_equivalent_t<T> { return {}; }; | ||
|
||
static_assert(std::invocable<decltype(sfinae_call), int>); | ||
|
||
struct NotSimdFriendly {}; | ||
static_assert(!std::invocable<decltype(sfinae_call), NotSimdFriendly>); | ||
|
||
} // namespace simd_friendly_equivalent_test | ||
|
||
namespace integral_simd_friendly_equivalent_test { | ||
|
||
static_assert(std::is_same_v< // | ||
std::int8_t, | ||
integral_simd_friendly_equivalent<signed char>>); | ||
|
||
struct Overloading { | ||
constexpr int operator()(auto) { return 0; } | ||
constexpr int operator()(has_simd_friendly_equivalent auto) { return 1; } | ||
constexpr int operator()(has_integral_simd_friendly_equivalent auto) { | ||
return 2; | ||
} | ||
}; | ||
|
||
// Subsumption tests | ||
struct NotSimdFriendly {}; | ||
enum class SomeInt {}; | ||
|
||
static_assert(Overloading{}(NotSimdFriendly{}) == 0); | ||
static_assert(Overloading{}(float{}) == 1); | ||
static_assert(Overloading{}(int{}) == 2); | ||
static_assert(Overloading{}(SomeInt{}) == 2); | ||
|
||
} // namespace integral_simd_friendly_equivalent_test | ||
|
||
TEST_F(FollySimdTraitsTest, AsSimdFriendly) { | ||
enum SomeEnum : int { Foo = 1, Bar, Baz }; | ||
|
||
static_assert(asSimdFriendly(SomeEnum::Foo) == 1); | ||
|
||
std::array arr{SomeEnum::Foo, SomeEnum::Bar, SomeEnum::Baz}; | ||
folly::span<int, 3> castSpan = asSimdFriendly(folly::span(arr)); | ||
ASSERT_THAT(castSpan, testing::ElementsAre(1, 2, 3)); | ||
} | ||
|
||
template <typename T, typename U> | ||
void isSameTest(const T&, const U&) = delete; | ||
|
||
template <typename T> | ||
void isSameTest(const T&, const T&) {} | ||
|
||
template <typename From, typename To> | ||
void asSimdFriendlyUintTypeTest() { | ||
isSameTest(asSimdFriendlyUint(From{}), To{}); | ||
isSameTest(asSimdFriendlyUint(std::span<From>{}), std::span<To>{}); | ||
isSameTest( | ||
asSimdFriendlyUint(std::span<const From>{}), std::span<const To>{}); | ||
} | ||
|
||
TEST_F(FollySimdTraitsTest, AsSimdFriendlyUint) { | ||
enum SomeEnum : int { Foo = 1, Bar, Baz }; | ||
|
||
static_assert(asSimdFriendlyUint(SomeEnum::Foo) == 1U); | ||
|
||
asSimdFriendlyUintTypeTest<char, std::uint8_t>(); | ||
asSimdFriendlyUintTypeTest<short, std::uint16_t>(); | ||
asSimdFriendlyUintTypeTest<int, std::uint32_t>(); | ||
asSimdFriendlyUintTypeTest<unsigned, std::uint32_t>(); | ||
asSimdFriendlyUintTypeTest<float, std::uint32_t>(); | ||
asSimdFriendlyUintTypeTest<int64_t, std::uint64_t>(); | ||
asSimdFriendlyUintTypeTest<double, std::uint64_t>(); | ||
} | ||
|
||
} // namespace folly::detail |