Skip to content

Commit 1bcfb05

Browse files
committed
minor improvements and additional test
1 parent fc1ad57 commit 1bcfb05

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

core/test/base/half.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ TEST(FloatToHalf, RoundsDownToEvenNumber)
154154
ASSERT_EQ(get_bits(x), get_bits("1" "00000" "0000111010"));
155155
}
156156

157+
TEST(FloatToHalf, RoundsDownToEvenNumber2)
158+
{
159+
half x = create_from_bits<float>("1" "01101100" "11010100000000000000001");
160+
161+
ASSERT_EQ(get_bits(x), get_bits("1" "00000" "0000111011"));
162+
}
163+
157164

158165
TEST(FloatToHalf, LargestNumberThatConvertsToZero)
159166
{

include/ginkgo/core/base/half.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,9 @@ class alignas(std::uint16_t) half {
418418
// gap to fp16 denormal exponents (+1 from normal to denormal
419419
// exponent base)
420420
const auto gap_to_fp16 =
421-
(conv::bias_change - ((data_ & f32_traits::exponent_mask) >>
422-
conv::significand_offset) >>
421+
((conv::bias_change -
422+
((data_ & f32_traits::exponent_mask) >>
423+
conv::significand_offset)) >>
423424
f16_traits::significand_bits) +
424425
1;
425426

@@ -491,11 +492,11 @@ class alignas(std::uint16_t) half {
491492

492493
leading_zeros = f16_traits::significand_bits - index - 1;
493494
#else
494-
leading_zeros =
495-
__builtin_clz(static_cast<std::uint32_t>(
496-
f16_traits::significand_mask & data_)) -
497-
f16_traits::exponent_bits - f16_traits::sign_bits -
498-
8 * (sizeof(conv::result_bits) - sizeof(conv::source_bits));
495+
leading_zeros = __builtin_clz(static_cast<std::uint32_t>(
496+
f16_traits::significand_mask & data_)) -
497+
f16_traits::exponent_bits - f16_traits::sign_bits -
498+
CHAR_BIT * (sizeof(conv::result_bits) -
499+
sizeof(conv::source_bits));
499500
#endif
500501

501502
// Computes the new exponent, 0xxxxxxxx000...00

0 commit comments

Comments
 (0)