Permit half overflow within allowable ULP (#600)

* Permit half overflow within allowable ULP

Modify the algorithm for calculating half precision ULP error so
that it duplicates the behaviour of the single precision ULP algorithm,
in regards to allowing overflow within the defined ULP error.

In the case where the test value is infinity, but the reference is
finite, pretend the test value is 63336.0 and calculate the ULP error
against that.

Encountered this while testing half precision `hypot()` in PR !529,
for inputs `hypot(-48864.0, 43648.0)` which has reference
`65519.755799`. With RTE rounding this only just rounds to `65504` as half,
and returning INF is currently infinite ULP error. Using the leniency
introduced by this change however the error is `~0.5` within the `2` ULP
bounds defined by the spec.

* Run clang-format over changes

Code now conforms to style guidelines and allows `check-format.sh` to pass.
This commit is contained in:
Ewan Crawford
2020-10-28 10:13:40 +00:00
committed by GitHub
parent f162c8b5ef
commit 55976fad35
3 changed files with 17 additions and 58 deletions

View File

@@ -307,8 +307,6 @@ const char *GetQueuePropertyName(cl_command_queue_properties property)
#define scalbnl(_a, _i ) ldexpl( _a, _i )
#endif
static float Ulp_Error_Half_Float( float test, double reference );
// taken from math tests
#define HALF_MIN_EXP -13
#define HALF_MANT_DIG 11
@@ -327,6 +325,23 @@ static float Ulp_Error_Half_Float( float test, double reference )
// results.
double testVal = test;
if (isinf(reference))
{
if (testVal == reference) return 0.0f;
return (float)(testVal - reference);
}
if (isinf(testVal))
{
// Allow overflow within the limit of the allowed ulp error. Towards
// that end we pretend the test value is actually 2**16, the next value
// that would appear in the number line if half had sufficient range.
testVal = copysign(65536.0, testVal);
}
if( u.u & 0x000fffffffffffffULL )
{ // Non-power of two and NaN
if( isnan( reference ) && isnan( test ) )
@@ -339,14 +354,6 @@ static float Ulp_Error_Half_Float( float test, double reference )
return (float) scalbn( testVal - reference, ulp_exp );
}
if( isinf( reference ) )
{
if( (double) test == reference )
return 0.0f;
return (float) (testVal - reference );
}
// reference is a normal power of two or a zero
int ulp_exp = HALF_MANT_DIG - 1 - MAX( ilogb( reference) - 1, HALF_MIN_EXP-1 );