1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
// Copyright (C) 2020-2023 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#ifndef SIMD_TESTS_BITS_CONVERSIONS_H_
#define SIMD_TESTS_BITS_CONVERSIONS_H_
#include <array>
// is_conversion_undefined
/* implementation-defined
* ======================
* §4.7 p3 (integral conversions)
* If the destination type is signed, the value is unchanged if it can be
* represented in the destination type (and bit-field width); otherwise, the
* value is implementation-defined.
*
* undefined
* =========
* §4.9/1 (floating-point conversions)
* If the source value is neither exactly represented in the destination type
* nor between two adjacent destination values the result is undefined.
*
* §4.10/1 (floating-integral conversions)
* floating point type can be converted to integer type.
* The behavior is undefined if the truncated value cannot be
* represented in the destination type.
*
* §4.10/2
* integer can be converted to floating point type.
* If the value being converted is outside the range of values that can be
* represented, the behavior is undefined.
*/
template <typename To, typename From>
constexpr bool
is_conversion_undefined_impl(From x, std::true_type)
{
return x > static_cast<long double>(std::__finite_max_v<To>)
|| x < static_cast<long double>(std::__finite_min_v<To>);
}
template <typename To, typename From>
constexpr bool
is_conversion_undefined_impl(From, std::false_type)
{ return false; }
template <typename To, typename From>
constexpr bool
is_conversion_undefined(From x)
{
static_assert(std::is_arithmetic<From>::value,
"this overload is only meant for builtin arithmetic types");
return is_conversion_undefined_impl<To, From>(
x, std::integral_constant<
bool, std::is_floating_point<From>::value
&& (std::is_integral<To>::value
|| (std::is_floating_point<To>::value
&& sizeof(From) > sizeof(To)))>());
}
static_assert(is_conversion_undefined<uint>(float(0x100000000LL)),
"testing my expectations of is_conversion_undefined");
static_assert(!is_conversion_undefined<float>(0x100000000LL),
"testing my expectations of is_conversion_undefined");
template <typename To, typename T, typename A>
inline std::experimental::simd_mask<T, A>
is_conversion_undefined(const std::experimental::simd<T, A>& x)
{
std::experimental::simd_mask<T, A> k = false;
for (std::size_t i = 0; i < x.size(); ++i)
k[i] = is_conversion_undefined(x[i]);
return k;
}
template <class T>
constexpr T
genHalfBits()
{
if constexpr (std::is_floating_point_v<T>)
return 0;
else
return std::__finite_max_v<T> >> (std::__digits_v<T> / 2);
}
template <class U, class T, class UU>
constexpr U
avoid_ub(UU x)
{ return is_conversion_undefined<T>(U(x)) ? U(0) : U(x); }
template <class U, class T, class UU>
constexpr U
avoid_ub2(UU x)
{ return is_conversion_undefined<U>(x) ? U(0) : avoid_ub<U, T>(x); }
// conversion test input data
template <class U, class T>
static const std::array<U, 53> cvt_input_data = {{
avoid_ub<U, T>(0xc0000080U),
avoid_ub<U, T>(0xc0000081U),
avoid_ub<U, T>(0xc0000082U),
avoid_ub<U, T>(0xc0000084U),
avoid_ub<U, T>(0xc0000088U),
avoid_ub<U, T>(0xc0000090U),
avoid_ub<U, T>(0xc00000A0U),
avoid_ub<U, T>(0xc00000C0U),
avoid_ub<U, T>(0xc000017fU),
avoid_ub<U, T>(0xc0000180U),
avoid_ub<U, T>(0x100000001LL),
avoid_ub<U, T>(0x100000011LL),
avoid_ub<U, T>(0x100000111LL),
avoid_ub<U, T>(0x100001111LL),
avoid_ub<U, T>(0x100011111LL),
avoid_ub<U, T>(0x100111111LL),
avoid_ub<U, T>(0x101111111LL),
avoid_ub<U, T>(-0x100000001LL),
avoid_ub<U, T>(-0x100000011LL),
avoid_ub<U, T>(-0x100000111LL),
avoid_ub<U, T>(-0x100001111LL),
avoid_ub<U, T>(-0x100011111LL),
avoid_ub<U, T>(-0x100111111LL),
avoid_ub<U, T>(-0x101111111LL),
avoid_ub<U, T>(std::__norm_min_v<U>),
avoid_ub<U, T>(std::__norm_min_v<U> + 1),
avoid_ub<U, T>(std::__finite_min_v<U>),
avoid_ub<U, T>(std::__finite_min_v<U> + 1),
avoid_ub<U, T>(-1),
avoid_ub<U, T>(-10),
avoid_ub<U, T>(-100),
avoid_ub<U, T>(-1000),
avoid_ub<U, T>(-10000),
avoid_ub<U, T>(0),
avoid_ub<U, T>(1),
avoid_ub<U, T>(genHalfBits<U>() - 1),
avoid_ub<U, T>(genHalfBits<U>()),
avoid_ub<U, T>(genHalfBits<U>() + 1),
avoid_ub<U, T>(std::__finite_max_v<U> - 1),
avoid_ub<U, T>(std::__finite_max_v<U>),
avoid_ub<U, T>(std::__finite_max_v<U> - 0xff),
avoid_ub<U, T>(std::__finite_max_v<U> - 0xff),
avoid_ub<U, T>(std::__finite_max_v<U> - 0x55),
avoid_ub<U, T>(-(std::__finite_min_v<U> + 1)),
avoid_ub<U, T>(-std::__finite_max_v<U>),
avoid_ub<U, T>(std::__finite_max_v<U> / std::pow(2., sizeof(T) * 6 - 1)),
avoid_ub2<U, T>(-std::__finite_max_v<U> / std::pow(2., sizeof(T) * 6 - 1)),
avoid_ub<U, T>(std::__finite_max_v<U> / std::pow(2., sizeof(T) * 4 - 1)),
avoid_ub2<U, T>(-std::__finite_max_v<U> / std::pow(2., sizeof(T) * 4 - 1)),
avoid_ub<U, T>(std::__finite_max_v<U> / std::pow(2., sizeof(T) * 2 - 1)),
avoid_ub2<U, T>(-std::__finite_max_v<U> / std::pow(2., sizeof(T) * 2 - 1)),
avoid_ub<U, T>(std::__finite_max_v<T> - 1),
avoid_ub<U, T>(std::__finite_max_v<T> * 0.75),
}};
template <class T, class U>
struct cvt_inputs
{
static constexpr size_t
size()
{ return cvt_input_data<U, T>.size(); }
U
operator[](size_t i) const
{ return cvt_input_data<U, T>[i]; }
};
#endif // SIMD_TESTS_BITS_CONVERSIONS_H_
|