[Beignet] [PATCH] libocl: refine implementation of logb().

Ruiling Song ruiling.song at intel.com
Thu Jan 29 00:05:42 PST 2015


Avoid if-branching.

Signed-off-by: Ruiling Song <ruiling.song at intel.com>
---
 backend/src/libocl/tmpl/ocl_math.tmpl.cl |   14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/backend/src/libocl/tmpl/ocl_math.tmpl.cl b/backend/src/libocl/tmpl/ocl_math.tmpl.cl
index 49c4efa..3483945 100644
--- a/backend/src/libocl/tmpl/ocl_math.tmpl.cl
+++ b/backend/src/libocl/tmpl/ocl_math.tmpl.cl
@@ -1280,18 +1280,16 @@ OVERLOADABLE float logb(float x) {
   if (__ocl_math_fastpath_flag)
     return __gen_ocl_internal_fastpath_logb(x);
 
-union {float f; unsigned i;} u;
+  union {float f; unsigned i;} u;
   u.f = x;
   int e =  ((u.i & 0x7f800000) >> 23);
-  if(e == 0) {
+  float r1 = e-127;
+  float r2 = -INFINITY;
+  float r3 = x*x;
     /* sub normal or +/-0 */
-    return -INFINITY;
-  } else if(e == 0xff) {
+  float r = e == 0 ? r2 : r1;
     /* inf & nan */
-    return x*x;
-  } else {
-    return (float)(e-127);
-  }
+  return e == 0xff ? r3 : r;
 }
 
 OVERLOADABLE int ilogb(float x) {
-- 
1.7.10.4



More information about the Beignet mailing list