16 #define VERIFY_BFLOAT16_BITS_EQUAL(h, bits) \
17 VERIFY_IS_EQUAL((numext::bit_cast<numext::uint16_t>(h)), (static_cast<numext::uint16_t>(bits)))
29 uint32_t src = (
sign << 31) + (exponent << 23) + (high_mantissa << 16) + low_mantissa;
30 memcpy(
static_cast<void*
>(&dest),
31 static_cast<const void*
>(&src),
sizeof(dest));
38 VERIFY_IS_EQUAL((internal::cast<bfloat16,T>(internal::cast<T,bfloat16>(-std::numeric_limits<T>::infinity()))), -std::numeric_limits<T>::infinity());
39 VERIFY_IS_EQUAL((internal::cast<bfloat16,T>(internal::cast<T,bfloat16>(std::numeric_limits<T>::infinity()))), std::numeric_limits<T>::infinity());
40 VERIFY_IS_EQUAL((internal::cast<bfloat16,T>(internal::cast<T,bfloat16>(
T(-1.0)))),
T(-1.0));
41 VERIFY_IS_EQUAL((internal::cast<bfloat16,T>(internal::cast<T,bfloat16>(
T(-0.5)))),
T(-0.5));
42 VERIFY_IS_EQUAL((internal::cast<bfloat16,T>(internal::cast<T,bfloat16>(
T(-0.0)))),
T(-0.0));
43 VERIFY_IS_EQUAL((internal::cast<bfloat16,T>(internal::cast<T,bfloat16>(
T(1.0)))),
T(1.0));
44 VERIFY_IS_EQUAL((internal::cast<bfloat16,T>(internal::cast<T,bfloat16>(
T(0.5)))),
T(0.5));
45 VERIFY_IS_EQUAL((internal::cast<bfloat16,T>(internal::cast<T,bfloat16>(
T(0.0)))),
T(0.0));
54 numext::bit_cast<bfloat16>(numext::bit_cast<numext::uint16_t>(
bfloat16(1.0
f))),
57 numext::bit_cast<bfloat16>(numext::bit_cast<numext::uint16_t>(
bfloat16(0.5
f))),
60 numext::bit_cast<bfloat16>(numext::bit_cast<numext::uint16_t>(
bfloat16(-0.33333
f))),
63 numext::bit_cast<bfloat16>(numext::bit_cast<numext::uint16_t>(
bfloat16(0.0
f))),
74 float val1 =
static_cast<float>(
bfloat16(__bfloat16_raw(0x3c00)));
75 float val2 =
static_cast<float>(
bfloat16(__bfloat16_raw(0x3c01)));
76 float val3 =
static_cast<float>(
bfloat16(__bfloat16_raw(0x3c02)));
117 test_roundtrip<float>();
118 test_roundtrip<double>();
119 test_roundtrip<std::complex<float> >();
120 test_roundtrip<std::complex<double> >();
124 for (
int i = 0;
i < 100;
i++)
a(
i) =
i + 1.25;
127 for (
int i = 0;
i < 100; ++
i) {
178 std::cout <<
"denorm min = " << (std::numeric_limits<bfloat16>::denorm_min)() <<
" (0x" << std::hex << numext::bit_cast<numext::uint16_t>((std::numeric_limits<bfloat16>::denorm_min)()) <<
")" << std::endl;
181 std::cout <<
"signaling nan = " << std::numeric_limits<bfloat16>::signaling_NaN() <<
" (0x" << std::hex << numext::bit_cast<numext::uint16_t>(std::numeric_limits<bfloat16>::signaling_NaN()) <<
")" << std::endl;
186 numext::bit_cast<numext::uint16_t>(std::numeric_limits<bfloat16>::infinity()),
187 numext::bit_cast<numext::uint16_t>(
bfloat16(std::numeric_limits<float>::infinity())) );
195 && ((numext::bit_cast<numext::uint16_t>(std::numeric_limits<bfloat16>::quiet_NaN()) & BFLOAT16_QUIET_BIT) > 0)
196 && ((numext::bit_cast<numext::uint16_t>(
bfloat16(std::numeric_limits<float>::quiet_NaN())) & BFLOAT16_QUIET_BIT) > 0) );
201 (
numext::isnan)(std::numeric_limits<bfloat16>::signaling_NaN())
203 && ((numext::bit_cast<numext::uint16_t>(std::numeric_limits<bfloat16>::signaling_NaN()) & BFLOAT16_QUIET_BIT) == 0) );
206 VERIFY( (std::numeric_limits<bfloat16>::denorm_min)() >
bfloat16(0.
f) );
334 Index size = internal::random<Index>(1,10);
336 ArrayXh
a1 = ArrayXh::Random(
size),
a2 = ArrayXh::Random(
size);
347 std::stringstream
ss;
357 MatrixXh Ah = MatrixXh::Random(
rows,
depth);
358 MatrixXh Bh = MatrixXh::Random(
depth,
cols);
359 MatrixXh Ch = MatrixXh::Random(
rows,
cols);
360 MatrixXf Af = Ah.cast<
float>();
361 MatrixXf Bf = Bh.cast<
float>();
362 MatrixXf Cf = Ch.cast<
float>();