[Beignet] [PATCH] fix builtin function "ilogb"

Xing, Homer homer.xing at intel.com
Wed Nov 6 23:50:38 PST 2013


I have come up with a reason, why not to use  __gen_ocl_rndd(native_log2(x)). Because "__gen_ocl_rndd(native_log2(x))" produces a result from 1.4e-45 with more error than the spec says.

-----Original Message-----
From: Yang, Rong R 
Sent: Thursday, November 7, 2013 1:08 PM
To: Xing, Homer; beignet at lists.freedesktop.org
Subject: RE: [Beignet] [PATCH] fix builtin function "ilogb"

Hi:

  Why not using __gen_ocl_rndd(native_log2(x)) for common case?

-----Original Message-----
From: beignet-bounces at lists.freedesktop.org [mailto:beignet-bounces at lists.freedesktop.org] On Behalf Of Homer Hsing
Sent: Tuesday, November 05, 2013 3:48 PM
To: beignet at lists.freedesktop.org
Subject: [Beignet] [PATCH] fix builtin function "ilogb"

add FP_ILOGB0, FP_ILOGBNAN
return FP_ILOGB0 for zero.
return FP_ILOGBNAN for nan.
return INT_MAX for inf.
also improve function code for other cases.

Signed-off-by: Homer Hsing <homer.xing at intel.com>
---
 backend/src/ocl_stdlib.tmpl.h | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/backend/src/ocl_stdlib.tmpl.h b/backend/src/ocl_stdlib.tmpl.h index a1f365c..01216ae 100644
--- a/backend/src/ocl_stdlib.tmpl.h
+++ b/backend/src/ocl_stdlib.tmpl.h
@@ -1303,7 +1303,28 @@ INLINE_OVERLOADABLE float native_log10(float x) {  }  INLINE_OVERLOADABLE float log1p(float x) { return native_log(x + 1); }  INLINE_OVERLOADABLE float logb(float x) { return __gen_ocl_rndd(native_log2(x)); } -INLINE_OVERLOADABLE int ilogb(float x) { return __gen_ocl_rndd(native_log2(x)); }
+#define FP_ILOGB0 (-0x7FFFFFFF-1)
+#define FP_ILOGBNAN FP_ILOGB0
+INLINE_OVERLOADABLE int ilogb(float x) {
+  union { int i; float f; } u;
+  if (isnan(x))
+    return FP_ILOGBNAN;
+  if (isinf(x))
+    return 0x7FFFFFFF;
+  u.f = x;
+  u.i &= 0x7fffffff;
+  if (u.i == 0)
+    return FP_ILOGB0;
+  if (u.i >= 0x800000)
+    return (u.i >> 23) - 127;
+  int r = -126;
+  int a = u.i & 0x7FFFFF;
+  while(a < 0x800000) {
+    a <<= 1;
+    r --;
+  }
+  return r;
+}
 INLINE_OVERLOADABLE float nan(uint code) {
   return NAN;
 }
--
1.8.3.2

_______________________________________________
Beignet mailing list
Beignet at lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/beignet


More information about the Beignet mailing list