diff options
author | Yilun Lin <yllin@google.com> | 2018-09-26 18:14:39 +0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-10-04 12:55:52 -0700 |
commit | ece03ab4d09b157c5e6f3c4fe0446678c0d8684b (patch) | |
tree | d55ae0f3b6eddb16ce718f7eeb2230345a7aabee | |
parent | d14d434d1981325aeeb127742cc507763e40dc5a (diff) | |
download | chrome-ec-ece03ab4d09b157c5e6f3c4fe0446678c0d8684b.tar.gz |
math_util: Support fp_div_dbz which handling division-by-zero error.
Fixed-point numbers has limited value range. It is very easy to
be trapped in a division-by-zero error especially when doing
magnetometer calculation. We only use fixed-point operations for
motion sensors now, so the precision and correctness for these
operations is not the most important point to consider. Here
we just let divided-by-zero result becomes INT32_MAX, to prevent
the system failure.
TEST=undef CONFIG_FPU, build, flash on reef, and test magnetometer, and
see the system doesn't crash.
BUG=b:113364863
BRANCH=None
Change-Id: I0ab657b2132666eefa9f3a04043ce29f0096d238
Signed-off-by: Yilun Lin <yllin@google.com>
Reviewed-on: https://chromium-review.googlesource.com/1248421
Commit-Ready: Yilun Lin <yllin@chromium.org>
Tested-by: Yilun Lin <yllin@chromium.org>
Reviewed-by: Nicolas Boichat <drinkcat@chromium.org>
-rw-r--r-- | include/math_util.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/include/math_util.h b/include/math_util.h index 9e6b73aac7..942aaf8908 100644 --- a/include/math_util.h +++ b/include/math_util.h @@ -56,6 +56,12 @@ static inline fp_t fp_div(fp_t a, fp_t b) { return a / b; } + +/* Don't handle divided-by-zero with FPU, since this should be rare. */ +static inline fp_t fp_div_dbz(fp_t a, fp_t b) +{ + return fp_div(a, b); +} #else /** * Multiplication - return (a * b) @@ -72,6 +78,24 @@ static inline fp_t fp_div(fp_t a, fp_t b) { return (fp_t)(((fp_inter_t)a << FP_BITS) / b); } + +/** + * Division which handles division-by-zero - returns (a / b) if b != 0, + * INT32_MAX if b == 0. + */ +static inline fp_t fp_div_dbz(fp_t a, fp_t b) +{ + /* + * Fixed-point numbers has limited value range. It is very easy to + * be trapped in a divided-by-zero error especially when doing + * magnetometer calculation. We only use fixed-point operations for + * motion sensors now, so the precision and correctness for these + * operations is not the most important point to consider. Here + * we just let divided-by-zero result becomes INT32_MAX, to prevent + * the system failure. + */ + return b == FLOAT_TO_FP(0) ? INT32_MAX : fp_div(a, b); +} #endif /** |