1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
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)
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.
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/>.
18 #ifndef SIMD_TESTS_BITS_CONVERSIONS_H_
19 #define SIMD_TESTS_BITS_CONVERSIONS_H_
22 // is_conversion_undefined
23 /* implementation-defined
24 * ======================
25 * §4.7 p3 (integral conversions)
26 * If the destination type is signed, the value is unchanged if it can be
27 * represented in the destination type (and bit-field width); otherwise, the
28 * value is implementation-defined.
32 * §4.9/1 (floating-point conversions)
33 * If the source value is neither exactly represented in the destination type
34 * nor between two adjacent destination values the result is undefined.
36 * §4.10/1 (floating-integral conversions)
37 * floating point type can be converted to integer type.
38 * The behavior is undefined if the truncated value cannot be
39 * represented in the destination type.
42 * integer can be converted to floating point type.
43 * If the value being converted is outside the range of values that can be
44 * represented, the behavior is undefined.
46 template <typename To
, typename From
>
48 is_conversion_undefined_impl(From x
, std::true_type
)
50 return x
> static_cast<long double>(std::__finite_max_v
<To
>)
51 || x
< static_cast<long double>(std::__finite_min_v
<To
>);
54 template <typename To
, typename From
>
56 is_conversion_undefined_impl(From
, std::false_type
)
59 template <typename To
, typename From
>
61 is_conversion_undefined(From x
)
63 static_assert(std::is_arithmetic
<From
>::value
,
64 "this overload is only meant for builtin arithmetic types");
65 return is_conversion_undefined_impl
<To
, From
>(
66 x
, std::integral_constant
<
67 bool, std::is_floating_point
<From
>::value
68 && (std::is_integral
<To
>::value
69 || (std::is_floating_point
<To
>::value
70 && sizeof(From
) > sizeof(To
)))>());
73 static_assert(is_conversion_undefined
<uint
>(float(0x100000000LL
)),
74 "testing my expectations of is_conversion_undefined");
75 static_assert(!is_conversion_undefined
<float>(0x100000000LL
),
76 "testing my expectations of is_conversion_undefined");
78 template <typename To
, typename T
, typename A
>
79 inline std::experimental::simd_mask
<T
, A
>
80 is_conversion_undefined(const std::experimental::simd
<T
, A
>& x
)
82 std::experimental::simd_mask
<T
, A
> k
= false;
83 for (std::size_t i
= 0; i
< x
.size(); ++i
)
84 k
[i
] = is_conversion_undefined(x
[i
]);
92 if constexpr (std::is_floating_point_v
<T
>)
95 return std::__finite_max_v
<T
> >> (std::__digits_v
<T
> / 2);
98 template <class U
, class T
, class UU
>
101 { return is_conversion_undefined
<T
>(U(x
)) ? U(0) : U(x
); }
103 template <class U
, class T
, class UU
>
106 { return is_conversion_undefined
<U
>(x
) ? U(0) : avoid_ub
<U
, T
>(x
); }
108 // conversion test input data
109 template <class U
, class T
>
110 static const std::array
<U
, 53> cvt_input_data
= {{
111 avoid_ub
<U
, T
>(0xc0000080U
),
112 avoid_ub
<U
, T
>(0xc0000081U
),
113 avoid_ub
<U
, T
>(0xc0000082U
),
114 avoid_ub
<U
, T
>(0xc0000084U
),
115 avoid_ub
<U
, T
>(0xc0000088U
),
116 avoid_ub
<U
, T
>(0xc0000090U
),
117 avoid_ub
<U
, T
>(0xc00000A0U
),
118 avoid_ub
<U
, T
>(0xc00000C0U
),
119 avoid_ub
<U
, T
>(0xc000017fU
),
120 avoid_ub
<U
, T
>(0xc0000180U
),
121 avoid_ub
<U
, T
>(0x100000001LL
),
122 avoid_ub
<U
, T
>(0x100000011LL
),
123 avoid_ub
<U
, T
>(0x100000111LL
),
124 avoid_ub
<U
, T
>(0x100001111LL
),
125 avoid_ub
<U
, T
>(0x100011111LL
),
126 avoid_ub
<U
, T
>(0x100111111LL
),
127 avoid_ub
<U
, T
>(0x101111111LL
),
128 avoid_ub
<U
, T
>(-0x100000001LL
),
129 avoid_ub
<U
, T
>(-0x100000011LL
),
130 avoid_ub
<U
, T
>(-0x100000111LL
),
131 avoid_ub
<U
, T
>(-0x100001111LL
),
132 avoid_ub
<U
, T
>(-0x100011111LL
),
133 avoid_ub
<U
, T
>(-0x100111111LL
),
134 avoid_ub
<U
, T
>(-0x101111111LL
),
135 avoid_ub
<U
, T
>(std::__norm_min_v
<U
>),
136 avoid_ub
<U
, T
>(std::__norm_min_v
<U
> + 1),
137 avoid_ub
<U
, T
>(std::__finite_min_v
<U
>),
138 avoid_ub
<U
, T
>(std::__finite_min_v
<U
> + 1),
141 avoid_ub
<U
, T
>(-100),
142 avoid_ub
<U
, T
>(-1000),
143 avoid_ub
<U
, T
>(-10000),
146 avoid_ub
<U
, T
>(genHalfBits
<U
>() - 1),
147 avoid_ub
<U
, T
>(genHalfBits
<U
>()),
148 avoid_ub
<U
, T
>(genHalfBits
<U
>() + 1),
149 avoid_ub
<U
, T
>(std::__finite_max_v
<U
> - 1),
150 avoid_ub
<U
, T
>(std::__finite_max_v
<U
>),
151 avoid_ub
<U
, T
>(std::__finite_max_v
<U
> - 0xff),
152 avoid_ub
<U
, T
>(std::__finite_max_v
<U
> - 0xff),
153 avoid_ub
<U
, T
>(std::__finite_max_v
<U
> - 0x55),
154 avoid_ub
<U
, T
>(-(std::__finite_min_v
<U
> + 1)),
155 avoid_ub
<U
, T
>(-std::__finite_max_v
<U
>),
156 avoid_ub
<U
, T
>(std::__finite_max_v
<U
> / std::pow(2., sizeof(T
) * 6 - 1)),
157 avoid_ub2
<U
, T
>(-std::__finite_max_v
<U
> / std::pow(2., sizeof(T
) * 6 - 1)),
158 avoid_ub
<U
, T
>(std::__finite_max_v
<U
> / std::pow(2., sizeof(T
) * 4 - 1)),
159 avoid_ub2
<U
, T
>(-std::__finite_max_v
<U
> / std::pow(2., sizeof(T
) * 4 - 1)),
160 avoid_ub
<U
, T
>(std::__finite_max_v
<U
> / std::pow(2., sizeof(T
) * 2 - 1)),
161 avoid_ub2
<U
, T
>(-std::__finite_max_v
<U
> / std::pow(2., sizeof(T
) * 2 - 1)),
162 avoid_ub
<U
, T
>(std::__finite_max_v
<T
> - 1),
163 avoid_ub
<U
, T
>(std::__finite_max_v
<T
> * 0.75),
166 template <class T
, class U
>
169 static constexpr size_t
171 { return cvt_input_data
<U
, T
>.size(); }
174 operator[](size_t i
) const
175 { return cvt_input_data
<U
, T
>[i
]; }
177 #endif // SIMD_TESTS_BITS_CONVERSIONS_H_