fix corner case when number is small
This commit is contained in:
parent
15f8bc6f37
commit
fbfd3158a7
@ -4471,7 +4471,8 @@ static short convertFp16SW(float fp32)
|
||||
|
||||
fp16Int16 result;
|
||||
result.i = 0;
|
||||
if( 0x477ff000 <= ( a.i & 0x7fffffff ) )
|
||||
unsigned int absolute = a.i & 0x7fffffff;
|
||||
if( 0x477ff000 <= absolute )
|
||||
{
|
||||
// Inf in Fp16
|
||||
result.i = result.i | 0x7C00;
|
||||
@ -4481,7 +4482,20 @@ static short convertFp16SW(float fp32)
|
||||
result.i = (short)( result.i | 0x200 | ( significand >> kShiftSignificand ) );
|
||||
}
|
||||
}
|
||||
else if ( ( a.i & 0x7fffffff ) <= 0x387fe000 )
|
||||
else if ( absolute < 0x33000001 )
|
||||
{
|
||||
// too small for fp16
|
||||
result.i = 0;
|
||||
}
|
||||
else if ( absolute < 0x33c00000 )
|
||||
{
|
||||
result.i = 1;
|
||||
}
|
||||
else if ( absolute < 0x34200001 )
|
||||
{
|
||||
result.i = 2;
|
||||
}
|
||||
else if ( absolute < 0x387fe000 )
|
||||
{
|
||||
// subnormal in Fp16
|
||||
int fp16Significand = significand | 0x800000;
|
||||
@ -4489,8 +4503,9 @@ static short convertFp16SW(float fp32)
|
||||
fp16Significand = fp16Significand >> bitShift;
|
||||
|
||||
// special cases to round up
|
||||
int threshold = 0x8000 + ( ( fp16Significand & 1 ) ? 0 : 1 );
|
||||
if( threshold <= ( significand & 0xffff ) )
|
||||
bitShift = exponent + 24;
|
||||
unsigned int threshold = ( ( 0x400000 >> bitShift ) | ( ( ( significand & ( 0x800000 >> bitShift ) ) >> ( 126 - a.fmt.exponent ) ) ^ 1 ) );
|
||||
if( threshold <= ( significand & ( 0xffffff >> ( exponent + 25 ) ) ) )
|
||||
{
|
||||
fp16Significand++;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user