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.
Summary: Pull Request resolved: facebook#2279 extracting the common "simd-friendly" type helpers. Differential Revision: D61205292
- Loading branch information
1 parent
7981155
commit 6a3f5f2
Showing
7 changed files
with
208 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,95 @@ | ||
/* | ||
* 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 <concepts> | ||
#include <span> | ||
#include <type_traits> | ||
|
||
namespace folly::detail { | ||
|
||
struct regular_nonesuch {}; | ||
|
||
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 { | ||
return regular_nonesuch{}; | ||
} | ||
} 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{}; | ||
} else { | ||
return regular_nonesuch{}; | ||
} | ||
} else { | ||
return regular_nonesuch{}; | ||
} | ||
} | ||
|
||
template <bool Cond, typename To> | ||
using add_const_if_t = std::conditional_t<Cond, const To, To>; | ||
|
||
template <typename T> | ||
concept has_simd_friendly_equivalent = | ||
!std:: | ||
is_same_v<regular_nonesuch, decltype(findSimdFriendlyEquivalent<T>())>; | ||
|
||
template <has_simd_friendly_equivalent T> | ||
using simd_friendly_equivalent_t = add_const_if_t< | ||
std::is_const_v<T>, | ||
decltype(findSimdFriendlyEquivalent<std::remove_const_t<T>>())>; | ||
|
||
template <has_simd_friendly_equivalent T> | ||
requires std::integral<T> | ||
using integral_simd_friendly_equivalent = simd_friendly_equivalent_t<T>; | ||
|
||
template <has_simd_friendly_equivalent T, std::size_t Extend> | ||
auto asSimdFriendly(std::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); | ||
} | ||
|
||
} // 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,88 @@ | ||
/* | ||
* 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 { | ||
|
||
TEST(FollySimdTraits, SimdFriendlyEquivalent) { | ||
// 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 | ||
{ | ||
auto call = []<typename T>(T) -> simd_friendly_equivalent_t<T> { | ||
return {}; | ||
}; | ||
|
||
static_assert(std::invocable<decltype(call), int>); | ||
|
||
struct NotSimdFriendly {}; | ||
static_assert(!std::invocable<decltype(call), NotSimdFriendly>); | ||
} | ||
} | ||
|
||
TEST(FollySimdTraits, AsSimdFriendly) { | ||
enum SomeEnum : int { Foo = 1, Bar, Baz }; | ||
|
||
static_assert(asSimdFriendly(SomeEnum::Foo) == 1); | ||
|
||
std::array arr{SomeEnum::Foo, SomeEnum::Bar, SomeEnum::Baz}; | ||
std::span<int, 3> castSpan = asSimdFriendly(std::span(arr)); | ||
ASSERT_THAT(castSpan, testing::ElementsAre(1, 2, 3)); | ||
} | ||
|
||
} // namespace folly::detail |