[Piglit] [PATCH 1/5] cl: Fix ulp tolerance comparison

Jan Vesely jano.vesely at gmail.com
Sun Oct 1 20:10:13 UTC 2017


This fails sin/cos/tan tests almost everywhere.

Signed-off-by: Jan Vesely <jano.vesely at gmail.com>
---
 tests/util/piglit-util-cl.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/tests/util/piglit-util-cl.c b/tests/util/piglit-util-cl.c
index 0211c1ff6..0cbc450ad 100644
--- a/tests/util/piglit-util-cl.c
+++ b/tests/util/piglit-util-cl.c
@@ -89,21 +89,27 @@ piglit_cl_probe_floating(float value, float expect,  uint32_t ulp)
 	union {
 		float f;
 		uint32_t u;
-	} v, e, t;
+	} v, e;
 
 	v.f = value;
 	e.f = expect;
-	t.u = ulp;
-	/* Treat infinity and nan seperately */
+
+	/* Treat infinity and nan separately */
 	if (probe_float_check_nan_inf(value, expect)) {
 		return true;
 	}
 
+	/* expect is correctly rounded, 1 ULP is the distance to next
+	 * representable value */
+	float direction = signbit(expect) ?  -INFINITY : INFINITY;
+	float one_ulp = nextafterf(expect, direction) - expect;
+	float tolerance = fabsf(ulp * one_ulp);
+
 	diff = fabsf(value - expect);
 
-	if(diff > ulp || isnan(value)) {
+	if (diff > tolerance || isnan(value)) {
 		printf("Expecting %f (0x%x) with tolerance %f (%u ulps), but got %f (0x%x)\n",
-		       e.f, e.u, t.f, t.u, v.f, v.u);
+		       e.f, e.u, tolerance, ulp, v.f, v.u);
 		return false;
 	}
 
@@ -117,21 +123,25 @@ piglit_cl_probe_double(double value, double expect, uint64_t ulp)
 	union {
 		double f;
 		uint64_t u;
-	} v, e, t;
+	} v, e;
 
 	v.f = value;
 	e.f = expect;
-	t.u = ulp;
-	/* Treat infinity and nan seperately */
+
+	/* Treat infinity and nan separately */
 	if (probe_float_check_nan_inf(value, expect)) {
 		return true;
 	}
 
-	diff = fabsl(value - expect);
+	double direction = signbit(expect) ?  -INFINITY : INFINITY;
+	double one_ulp = nextafter(expect, direction) - expect;
+	double tolerance = fabs(ulp * one_ulp);
+
+	diff = fabs(value - expect);
 
-	if(diff > ulp || isnan(value)) {
+	if(diff > tolerance || isnan(value)) {
 		printf("Expecting %f (0x%" PRIx64") with tolerance %f (%lu ulps), but got %f (0x%" PRIx64")\n",
-		       e.f, e.u, t.f, t.u, v.f, v.u);
+		       e.f, e.u, tolerance, ulp, v.f, v.u);
 		return false;
 	}
 
-- 
2.13.6



More information about the Piglit mailing list