public static ushort FloatToHalfI( uint i )
{
int s = (int)( i >> 16 ) & 0x00008000;
int e = (int)( ( i >> 23 ) & 0x000000ff ) - ( 127 - 15 );
int m = (int)i & 0x007fffff;
if ( e <= 0 )
{
if ( e < -10 )
{
return 0;
}
m = ( m | 0x00800000 ) >> ( 1 - e );
return (ushort)( s | ( m >> 13 ) );
}
else if ( e == 0xff - ( 127 - 15 ) )
{
if ( m == 0 ) // Inf
{
return (ushort)( s | 0x7c00 );
}
else // NAN
{
m >>= 13;
return (ushort)( (uint)s | 0x7c00 | (uint)m | ( m == 0 ? 1u : 0u ) );
}
}
else
{
if ( e > 30 ) // Overflow
{
return (ushort)( s | 0x7c00 );
}
return (ushort)( s | ( e << 10 ) | ( m >> 13 ) );
}
}