Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / experimental / __simd / utility.h
blob847d006629c8d3b79dca13aacb2fdd0a6e12e556
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_EXPERIMENTAL___SIMD_UTILITY_H
11 #define _LIBCPP_EXPERIMENTAL___SIMD_UTILITY_H
13 #include <__type_traits/is_arithmetic.h>
14 #include <__type_traits/is_const.h>
15 #include <__type_traits/is_constant_evaluated.h>
16 #include <__type_traits/is_convertible.h>
17 #include <__type_traits/is_same.h>
18 #include <__type_traits/is_unsigned.h>
19 #include <__type_traits/is_volatile.h>
20 #include <__type_traits/void_t.h>
21 #include <__utility/declval.h>
22 #include <__utility/integer_sequence.h>
23 #include <cstdint>
24 #include <limits>
26 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
29 #if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
31 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL
32 inline namespace parallelism_v2 {
33 template <class _Tp>
34 inline constexpr bool __is_vectorizable_v =
35 is_arithmetic_v<_Tp> && !is_const_v<_Tp> && !is_volatile_v<_Tp> && !is_same_v<_Tp, bool>;
37 template <class _Tp>
38 _LIBCPP_HIDE_FROM_ABI auto __choose_mask_type() {
39 if constexpr (sizeof(_Tp) == 1) {
40 return uint8_t{};
41 } else if constexpr (sizeof(_Tp) == 2) {
42 return uint16_t{};
43 } else if constexpr (sizeof(_Tp) == 4) {
44 return uint32_t{};
45 } else if constexpr (sizeof(_Tp) == 8) {
46 return uint64_t{};
48 # ifndef _LIBCPP_HAS_NO_INT128
49 else if constexpr (sizeof(_Tp) == 16) {
50 return __uint128_t{};
52 # endif
53 else
54 static_assert(sizeof(_Tp) == 0, "Unexpected size");
57 template <class _Tp>
58 _LIBCPP_HIDE_FROM_ABI auto constexpr __set_all_bits(bool __v) {
59 return __v ? (numeric_limits<decltype(__choose_mask_type<_Tp>())>::max()) : 0;
62 template <class _From, class _To, class = void>
63 inline constexpr bool __is_non_narrowing_convertible_v = false;
65 template <class _From, class _To>
66 inline constexpr bool __is_non_narrowing_convertible_v<_From, _To, std::void_t<decltype(_To{std::declval<_From>()})>> =
67 true;
69 template <class _Tp, class _Up>
70 inline constexpr bool __can_broadcast_v =
71 (__is_vectorizable_v<_Up> && __is_non_narrowing_convertible_v<_Up, _Tp>) ||
72 (!__is_vectorizable_v<_Up> && is_convertible_v<_Up, _Tp>) || is_same_v<_Up, int> ||
73 (is_same_v<_Up, unsigned int> && is_unsigned_v<_Tp>);
75 template <class _Tp, class _Generator, std::size_t _Idx, class = void>
76 inline constexpr bool __is_well_formed = false;
78 template <class _Tp, class _Generator, std::size_t _Idx>
79 inline constexpr bool
80 __is_well_formed<_Tp,
81 _Generator,
82 _Idx,
83 std::void_t<decltype(std::declval<_Generator>()(integral_constant<size_t, _Idx>()))>> =
84 __can_broadcast_v<_Tp, decltype(std::declval<_Generator>()(integral_constant<size_t, _Idx>()))>;
86 template <class _Tp, class _Generator, std::size_t... _Idxes>
87 _LIBCPP_HIDE_FROM_ABI constexpr bool __can_generate(index_sequence<_Idxes...>) {
88 return (true && ... && __is_well_formed<_Tp, _Generator, _Idxes>);
91 template <class _Tp, class _Generator, std::size_t _Size>
92 inline constexpr bool __can_generate_v = experimental::__can_generate<_Tp, _Generator>(make_index_sequence<_Size>());
94 } // namespace parallelism_v2
95 _LIBCPP_END_NAMESPACE_EXPERIMENTAL
97 #endif // _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
99 _LIBCPP_POP_MACROS
101 #endif // _LIBCPP_EXPERIMENTAL___SIMD_UTILITY_H