Skip to content

Conversation

emre-safa
Copy link
Collaborator

@emre-safa emre-safa commented Sep 3, 2025

conversion from single to half will be provided

@emre-safa emre-safa requested a review from yhmtsai September 3, 2025 09:43
@emre-safa emre-safa self-assigned this Sep 3, 2025
@emre-safa emre-safa added the 1:ST:ready-for-review This PR is ready for review label Sep 3, 2025
@ginkgo-bot ginkgo-bot added reg:testing This is related to testing. mod:core This is related to the core module. labels Sep 3, 2025
Copy link
Member

@yhmtsai yhmtsai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!
I will suggest you put comments to describe leading_zeros, new_exponent, new_significand operation.

}

auto leading_zeros =
__builtin_clz(f16_traits::significand_mask & data_) -
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
__builtin_clz(f16_traits::significand_mask & data_) -
__builtin_clz(static_cast<std::uint32_t>(f16_traits::significand_mask & data_)) -

Because __builtin_clz only support unsigned integer, making it more explicitly will help understanding.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally you will need to create a clz function in detail namespace to make it also support MSVC platform.
Check bitscanreverse and core/base/intrinsics.hpp
https://learn.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64?view=msvc-170

Comment on lines +454 to +457
auto new_exponent =
((conv::bias_change >> f32_traits::significand_bits) -
leading_zeros)
<< f32_traits::significand_bits;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto new_exponent =
((conv::bias_change >> f32_traits::significand_bits) -
leading_zeros)
<< f32_traits::significand_bits;
auto new_exponent =
conv::bias_change - (leading_zeros << f32_traits::significand_bits);

It also worth noting leading_zeros is alway less than bias change because we are at half -> float

Copy link
Member

@yhmtsai yhmtsai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
some improvement and the test suggestion

f16_traits::significand_bits) +
f32_traits::significand_bits -
f16_traits::significand_bits + 1;
if (tail_length > f32_traits::significand_bits + 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (tail_length > f32_traits::significand_bits + 1) {
// all significant (including implicitly leading 1) will be moved after representation field more than one digit (less than half) such that it will rounding to zero.
if (tail_length > f32_traits::significand_bits + 1) {

please feel free to rephrase the sentence

@ginkgo-bot
Copy link
Member

Error: The following files need to be formatted:

cuda/base/exception.cpp

You can find a formatting patch under Artifacts here or run format! if you have write access to Ginkgo

Copy link
Member

@yhmtsai yhmtsai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. some corner test are required

Comment on lines +421 to +423
(conv::bias_change - ((data_ & f32_traits::exponent_mask) >>
conv::significand_offset) >>
f16_traits::significand_bits) +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(conv::bias_change - ((data_ & f32_traits::exponent_mask) >>
conv::significand_offset) >>
f16_traits::significand_bits) +
((conv::bias_change - ((data_ & f32_traits::exponent_mask) >>
conv::significand_offset)) >>
f16_traits::significand_bits) +

to ensure the ordering and avoid warning

__builtin_clz(static_cast<std::uint32_t>(
f16_traits::significand_mask & data_)) -
f16_traits::exponent_bits - f16_traits::sign_bits -
8 * (sizeof(conv::result_bits) - sizeof(conv::source_bits));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
8 * (sizeof(conv::result_bits) - sizeof(conv::source_bits));
CHAR_BIT * (sizeof(conv::result_bits) - sizeof(conv::source_bits));

this is predefined macro from climits

}


TEST(FloatToHalf, RoundsUpToEvenNumber)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add another test from rounds down to even number by adding one last bit into the end to ensure we compare full tail before shift.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1:ST:ready-for-review This PR is ready for review mod:core This is related to the core module. reg:testing This is related to testing.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants