From 19fe084974b9b40acf875f28499f158aba125c1a Mon Sep 17 00:00:00 2001 From: Al Date: Sun, 2 Apr 2017 13:41:57 -0400 Subject: [PATCH] [utils] adding non-branching sign functions --- src/float_utils.c | 9 +++++++++ src/float_utils.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/float_utils.c b/src/float_utils.c index 30cb5c5a..97154f79 100644 --- a/src/float_utils.c +++ b/src/float_utils.c @@ -14,6 +14,10 @@ inline bool float_equals(float a, float b) { return float_equals_epsilon(a, b, FLT_EPSILON); } +inline float fsign(float x) { + return (float)((x > 0.0f) - (x < 0.0f)); +} + inline bool double_equals_epsilon(double a, double b, double epsilon) { double diff = fabs(a - b); a = fabs(a); @@ -27,3 +31,8 @@ inline bool double_equals_epsilon(double a, double b, double epsilon) { inline bool double_equals(double a, double b) { return double_equals_epsilon(a, b, DBL_EPSILON); } + + +inline double sign(double x) { + return (double)((x > 0.0) - (x < 0.0)); +} diff --git a/src/float_utils.h b/src/float_utils.h index 2965fe9a..52f5f1b9 100644 --- a/src/float_utils.h +++ b/src/float_utils.h @@ -8,9 +8,11 @@ bool float_equals(float a, float b); bool float_equals_epsilon(float a, float b, float epsilon); +float fsign(float x); bool double_equals(double a, double b); bool double_equals_epsilon(double a, double b, double epsilon); +double sign(double x); #endif