diff options
author | Gwendal Grignou <gwendal@chromium.org> | 2015-09-09 08:53:55 -0700 |
---|---|---|
committer | ChromeOS Commit Bot <chromeos-commit-bot@chromium.org> | 2016-08-25 20:58:07 +0000 |
commit | 226e4987187988da1284fd07ac3a85a968b61657 (patch) | |
tree | 5848369646834bd9cc7bc65eb468adda11ec04b4 | |
parent | 01e404fe6de03e89bc301992de4ca11086167f2b (diff) | |
download | chrome-ec-226e4987187988da1284fd07ac3a85a968b61657.tar.gz |
UPSTREAM: common: math: Use float when CONFIG_FPU is set.
When hardware FPU is set, use it.
BRANCH=smaug, samus
BUG=chrome-os-partner:39900, b:27849483
TEST=compile.
Change-Id: I5b42bd23a7ee7549e538eff075756b58f79756f2
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/299516
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Reviewed-by: Alec Berg <alecaberg@chromium.org>
(cherry picked from commit d896a005d4a11abbd7803526dad6d927218a28f7)
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/370534
-rw-r--r-- | include/math_util.h | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/include/math_util.h b/include/math_util.h index 9c98ca3220..37de95084b 100644 --- a/include/math_util.h +++ b/include/math_util.h @@ -8,6 +8,19 @@ #ifndef __CROS_MATH_UTIL_H #define __CROS_MATH_UTIL_H +#ifdef CONFIG_FPU +typedef float fp_t; +typedef float fp_inter_t; + +/* Conversion to/from fixed-point */ +#define INT_TO_FP(x) ((float)(x)) +#define FP_TO_INT(x) ((int32_t)(x)) +/* Float to fixed-point, only for compile-time constants and unit tests */ +#define FLOAT_TO_FP(x) ((float)(x)) +/* Fixed-point to float, for unit tests */ +#define FP_TO_FLOAT(x) ((float)(x)) + +#else /* Fixed-point type */ typedef int32_t fp_t; @@ -24,12 +37,24 @@ typedef int64_t fp_inter_t; #define FLOAT_TO_FP(x) ((fp_t)((x) * (float)(1<<FP_BITS))) /* Fixed-point to float, for unit tests */ #define FP_TO_FLOAT(x) ((float)(x) / (float)(1<<FP_BITS)) +#endif /* * Fixed-point addition and subtraction can be done directly, because they * work identically. */ +#ifdef CONFIG_FPU +static inline fp_t fp_mul(fp_t a, fp_t b) +{ + return a * b; +} + +static inline fp_t fp_div(fp_t a, fp_t b) +{ + return a / b; +} +#else /** * Multiplication - return (a * b) */ @@ -45,6 +70,7 @@ static inline fp_t fp_div(fp_t a, fp_t b) { return (fp_t)(((fp_inter_t)a << FP_BITS) / b); } +#endif /** * Square (a * a) @@ -59,7 +85,7 @@ static inline fp_t fp_sq(fp_t a) */ static inline fp_t fp_abs(fp_t a) { - return (a >= 0 ? a : -a); + return (a >= INT_TO_FP(0) ? a : -a); } /* |