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

Ruiling Song ruiling.song at intel.com
Wed Jan 28 23:40:27 PST 2015


Avoid if-branching.

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

diff --git a/backend/src/libocl/src/ocl_geometric.cl b/backend/src/libocl/src/ocl_geometric.cl
index 07f1419..baa2f85 100644
--- a/backend/src/libocl/src/ocl_geometric.cl
+++ b/backend/src/libocl/src/ocl_geometric.cl
@@ -60,30 +60,23 @@ OVERLOADABLE float distance(float2 x, float2 y) { return length(x-y); }
 OVERLOADABLE float distance(float3 x, float3 y) { return length(x-y); }
 OVERLOADABLE float distance(float4 x, float4 y) { return length(x-y); }
 OVERLOADABLE float normalize(float x) {
-  union { float f; unsigned u; } u;
-  u.f = x;
-  if(u.u == 0)
-    return 0.f;
-  if(isnan(x))
-    return NAN;
-  return u.u < 0x7fffffff ? 1.f : -1.f;
+  float m = length(x);
+  m = m == 0.0f ? 1.0f : m;
+  return x / m;
 }
 OVERLOADABLE float2 normalize(float2 x) {
   float m = length(x);
-  if(m == 0)
-    return 0;
+  m = m == 0.0f ? 1.0f : m;
   return x / m;
 }
 OVERLOADABLE float3 normalize(float3 x) {
   float m = length(x);
-  if(m == 0)
-    return 0;
+  m = m == 0.0f ? 1.0f : m;
   return x / m;
 }
 OVERLOADABLE float4 normalize(float4 x) {
   float m = length(x);
-  if(m == 0)
-    return 0;
+  m = m == 0.0f ? 1.0f : m;
   return x / m;
 }
 
-- 
1.7.10.4



More information about the Beignet mailing list