| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// expensive: * [1-9] * * |
| 19 |
#include "bits/main.h" |
| 20 |
|
| 21 |
using std::experimental::simd_cast; |
| 22 |
using std::experimental::static_simd_cast; |
| 23 |
|
| 24 |
template <class T, size_t N> |
| 25 |
struct gen_cast |
| 26 |
{ |
| 27 |
std::array<T, N> data; |
| 28 |
|
| 29 |
template <class V> |
| 30 |
gen_cast(const V& v) |
| 31 |
{ |
| 32 |
for (size_t i = 0; i < V::size(); ++i) |
| 33 |
{ |
| 34 |
data[i] = static_cast<T>(v[i]); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
template <class I> |
| 39 |
constexpr T |
| 40 |
operator()(I) |
| 41 |
{ return data[I::value]; } |
| 42 |
}; |
| 43 |
|
| 44 |
template <class V, class To> |
| 45 |
struct gen_seq_t |
| 46 |
{ |
| 47 |
using From = typename V::value_type; |
| 48 |
const size_t N = cvt_input_data<From, To>.size(); |
| 49 |
size_t offset = 0; |
| 50 |
|
| 51 |
constexpr void |
| 52 |
operator++() |
| 53 |
{ offset += V::size(); } |
| 54 |
|
| 55 |
explicit constexpr operator bool() const |
| 56 |
{ return offset < N; } |
| 57 |
|
| 58 |
template <class I> |
| 59 |
constexpr From |
| 60 |
operator()(I) const |
| 61 |
{ |
| 62 |
size_t i = I::value + offset; |
| 63 |
return i < N ? cvt_input_data<From, To>[i] : From(i); |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
template <class To> |
| 68 |
struct foo |
| 69 |
{ |
| 70 |
template <class T> |
| 71 |
auto |
| 72 |
operator()(const T& v) -> decltype(simd_cast<To>(v)); |
| 73 |
}; |
| 74 |
|
| 75 |
template <typename V, typename To> |
| 76 |
void |
| 77 |
casts() |
| 78 |
{ |
| 79 |
using From = typename V::value_type; |
| 80 |
constexpr auto N = V::size(); |
| 81 |
if constexpr (N <= std::experimental::simd_abi::max_fixed_size<To>) |
| 82 |
{ |
| 83 |
using W = std::experimental::fixed_size_simd<To, N>; |
| 84 |
|
| 85 |
if constexpr (std::is_integral_v<From>) |
| 86 |
{ |
| 87 |
using A = typename V::abi_type; |
| 88 |
using TU = std::make_unsigned_t<From>; |
| 89 |
using TS = std::make_signed_t<From>; |
| 90 |
COMPARE(typeid(static_simd_cast<TU>(V())), |
| 91 |
typeid(std::experimental::simd<TU, A>)); |
| 92 |
COMPARE(typeid(static_simd_cast<TS>(V())), |
| 93 |
typeid(std::experimental::simd<TS, A>)); |
| 94 |
} |
| 95 |
|
| 96 |
using is_simd_cast_allowed |
| 97 |
= decltype(vir::test::sfinae_is_callable_t<const V&>(foo<To>())); |
| 98 |
|
| 99 |
COMPARE(is_simd_cast_allowed::value, |
| 100 |
std::__digits<From>::value <= std::__digits<To>::value |
| 101 |
&& std::__finite_max<From>::value |
| 102 |
<= std::__finite_max<To>::value |
| 103 |
&& !(std::is_signed<From>::value |
| 104 |
&& std::is_unsigned<To>::value)); |
| 105 |
|
| 106 |
if constexpr (is_simd_cast_allowed::value) |
| 107 |
{ |
| 108 |
for (gen_seq_t<V, To> gen_seq; gen_seq; ++gen_seq) |
| 109 |
{ |
| 110 |
const V seq(gen_seq); |
| 111 |
COMPARE(simd_cast<V>(seq), seq); |
| 112 |
COMPARE(simd_cast<W>(seq), W(gen_cast<To, N>(seq))) |
| 113 |
<< "seq = " << seq; |
| 114 |
auto test = simd_cast<To>(seq); |
| 115 |
// decltype(test) is not W if |
| 116 |
// a) V::abi_type is not fixed_size and |
| 117 |
// b.1) V::value_type and To are integral and of equal rank or |
| 118 |
// b.2) V::value_type and To are equal |
| 119 |
COMPARE(test, decltype(test)(gen_cast<To, N>(seq))); |
| 120 |
if (std::is_same<To, From>::value) |
| 121 |
{ |
| 122 |
COMPARE(typeid(decltype(test)), typeid(V)); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
for (gen_seq_t<V, To> gen_seq; gen_seq; ++gen_seq) |
| 128 |
{ |
| 129 |
const V seq(gen_seq); |
| 130 |
COMPARE(static_simd_cast<V>(seq), seq); |
| 131 |
COMPARE(static_simd_cast<W>(seq), W(gen_cast<To, N>(seq))) << '\n' |
| 132 |
<< seq; |
| 133 |
auto test = static_simd_cast<To>(seq); |
| 134 |
// decltype(test) is not W if |
| 135 |
// a) V::abi_type is not fixed_size and |
| 136 |
// b.1) V::value_type and To are integral and of equal rank or |
| 137 |
// b.2) V::value_type and To are equal |
| 138 |
COMPARE(test, decltype(test)(gen_cast<To, N>(seq))); |
| 139 |
if (std::is_same<To, From>::value) |
| 140 |
{ |
| 141 |
COMPARE(typeid(decltype(test)), typeid(V)); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
template <typename V> |
| 148 |
void |
| 149 |
test() |
| 150 |
{ |
| 151 |
casts<V, long double>(); |
| 152 |
casts<V, double>(); |
| 153 |
casts<V, float>(); |
| 154 |
casts<V, long long>(); |
| 155 |
casts<V, unsigned long long>(); |
| 156 |
casts<V, unsigned long>(); |
| 157 |
casts<V, long>(); |
| 158 |
casts<V, int>(); |
| 159 |
casts<V, unsigned int>(); |
| 160 |
casts<V, short>(); |
| 161 |
casts<V, unsigned short>(); |
| 162 |
casts<V, char>(); |
| 163 |
casts<V, signed char>(); |
| 164 |
casts<V, unsigned char>(); |
| 165 |
casts<V, char32_t>(); |
| 166 |
casts<V, char16_t>(); |
| 167 |
casts<V, wchar_t>(); |
| 168 |
} |