summaryrefslogtreecommitdiff
path: root/common/math_util.c
diff options
context:
space:
mode:
authorShawn Nematbakhsh <shawnn@chromium.org>2017-06-15 15:07:35 -0700
committerchrome-bot <chrome-bot@chromium.org>2017-06-19 15:33:09 -0700
commit90167c1764ab7c532b121bd08990b81631a3c896 (patch)
treecb2b1dadc7052437d071b872b6aff04df221d0d8 /common/math_util.c
parent0696dd1e68692ca8dae17b737e0d895561f65cd4 (diff)
downloadchrome-ec-90167c1764ab7c532b121bd08990b81631a3c896.tar.gz
math_util: Use HW instruction for int_sqrtf when available
Cortex-M4 supports a floating point square root function that takes 14 cycles to execute, which is a speed improvement over the existing binary search, and saves flash space. BUG=chromium:687624 BRANCH=None TEST=On kevin, verify that both sqrtf methods (binary search vs HW instruction) have identical results for fractional input (eg. sqrt(15.999999) = 3), except when floating point representation of square root rounds up to an integer. Verify identical results for all integers [-100, 16793602). Note that 16793602 is the first integer for which the floating point representation of sqrt rounds up to an integer. Also verify basic motion sense functions on kevin. Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org> Change-Id: I8521c9a28c958b340ca83c37342253e424df0c91 Reviewed-on: https://chromium-review.googlesource.com/537734 Commit-Ready: Shawn N <shawnn@chromium.org> Tested-by: Shawn N <shawnn@chromium.org> Reviewed-by: Shawn N <shawnn@chromium.org>
Diffstat (limited to 'common/math_util.c')
-rw-r--r--common/math_util.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/common/math_util.c b/common/math_util.c
index ad98aecdf5..6a49dc31c3 100644
--- a/common/math_util.c
+++ b/common/math_util.c
@@ -6,6 +6,7 @@
/* Common math functions. */
#include "common.h"
+#include "math.h"
#include "math_util.h"
#include "util.h"
@@ -73,7 +74,20 @@ fp_t arc_cos(fp_t x)
/**
* Integer square root.
*/
-int int_sqrtf(fp_inter_t x)
+#ifdef CONFIG_FPU
+/*
+ * Use library sqrtf instruction, if available, since it's usually much faster
+ * and smaller. On Cortex-M4, this becomes a single instruction which takes
+ * 14 cycles to execute. This produces identical results to binary search,
+ * except when the floating point representation of the square root rounds up
+ * to an integer.
+ */
+static inline int int_sqrtf(fp_inter_t x)
+{
+ return sqrtf(x);
+}
+#else
+static int int_sqrtf(fp_inter_t x)
{
int rmax = 0x7fffffff;
int rmin = 0;
@@ -110,6 +124,7 @@ int int_sqrtf(fp_inter_t x)
}
}
}
+#endif
int vector_magnitude(const vector_3_t v)
{