Skip to content

Possibly incorrect implementation of ILogB for BFloat16 and Half #123293

@vcsjones

Description

@vcsjones

ILogB is implemented on Half as such:

return MinExponent - (BitOperations.LeadingZeroCount(x.TrailingSignificand) - BiasedExponentLength);

and TrailingSignificand is a ushort

internal ushort TrailingSignificand

However, BitOperations.LeadingZeroCount has no overload that accepts a ushort. So it is implicitly widened to the uint overload, and calculates the leading zero count as if the number had a 32-bit width instead of 16-bit.

I believe that means that Half.ILogB may be off by 16 for subnormal values (and probably BFloat16).

For example, for Half.Epsilon I would expect the ilogb to be -24, but is reported as -40.

Metadata

Metadata

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions