[Beignet] [PATCH] libocl: refine implementation of sign().
Ruiling Song
ruiling.song at intel.com
Wed Jan 28 23:18:11 PST 2015
Avoid if-branching.
Signed-off-by: Ruiling Song <ruiling.song at intel.com>
---
backend/src/libocl/tmpl/ocl_common.tmpl.cl | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/backend/src/libocl/tmpl/ocl_common.tmpl.cl b/backend/src/libocl/tmpl/ocl_common.tmpl.cl
index db7b0d8..77bd2d3 100644
--- a/backend/src/libocl/tmpl/ocl_common.tmpl.cl
+++ b/backend/src/libocl/tmpl/ocl_common.tmpl.cl
@@ -17,6 +17,7 @@
*/
#include "ocl_common.h"
#include "ocl_float.h"
+#include "ocl_relational.h"
/////////////////////////////////////////////////////////////////////////////
// Common Functions
@@ -55,11 +56,12 @@ OVERLOADABLE float smoothstep(float e0, float e1, float x) {
}
OVERLOADABLE float sign(float x) {
- if(x > 0)
- return 1;
- if(x < 0)
- return -1;
- if(x == -0.f)
- return -0.f;
- return 0.f;
+ union {float f; unsigned u;} ieee;
+ ieee.f = x;
+ unsigned k = ieee.u;
+ float r = (k&0x80000000) ? -1.0f : 1.0f;
+ // differentiate +0.0f -0.0f
+ float s = 0.0f * r;
+ s = (x == 0.0f) ? s : r;
+ return isnan(x) ? 0.0f : s;
}
--
1.7.10.4
More information about the Beignet
mailing list