summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorTing Shen <phoenixshen@google.com>2021-01-25 18:14:36 +0800
committerCommit Bot <commit-bot@chromium.org>2021-01-27 06:09:46 +0000
commit2b8e109ef452b5419f64262c895816245b3496f4 (patch)
tree47238fe0e0db2ad7e7935713709b6428eac13301 /test
parente9af813c36b7b411bf2a01cbc1b09d5fdec49b8a (diff)
downloadchrome-ec-2b8e109ef452b5419f64262c895816245b3496f4.tar.gz
math_util: fix incorrect int_sqrtf implementation
Correct the binary search interval for small x. The range should be at least [0, sqrt(2 ** 31)] ~= [0, 46341]. Also fixed some corner cases and added unit test for it. BUG=b:177384512 TEST=1)`watch -n 0.3 ectool motionsense lid_angle` verify the angle looks reasonable. 2) TEST_LIST_HOST=fp make runhosttests BRANCH=main Signed-off-by: Ting Shen <phoenixshen@google.com> Change-Id: I394fe3a59ac51ec4491a24399848f179c1074b95 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2646041 Tested-by: Ting Shen <phoenixshen@chromium.org> Reviewed-by: Yilin Yang (kerker) <kerker@chromium.org> Reviewed-by: Eric Yilun Lin <yllin@chromium.org> Commit-Queue: Ting Shen <phoenixshen@chromium.org>
Diffstat (limited to 'test')
-rw-r--r--test/fp.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/test/fp.c b/test/fp.c
index 1945c09189..a7c1a39e3c 100644
--- a/test/fp.c
+++ b/test/fp.c
@@ -120,6 +120,24 @@ static int test_fpv3_norm(void)
return EC_SUCCESS;
}
+static int test_int_sqrtf(void)
+{
+#ifndef CONFIG_FPU
+ TEST_ASSERT(int_sqrtf(0) == 0);
+ TEST_ASSERT(int_sqrtf(15) == 3);
+ TEST_ASSERT(int_sqrtf(25) == 5);
+ TEST_ASSERT(int_sqrtf(1111088889) == 33333);
+ TEST_ASSERT(int_sqrtf(123456789) == 11111);
+ TEST_ASSERT(int_sqrtf(1000000000000000005) == 1000000000);
+ /* Return zero for imaginary numbers */
+ TEST_ASSERT(int_sqrtf(-100) == 0);
+ /* Return INT32_MAX for input greater than INT32_MAX ^ 2 */
+ TEST_ASSERT(int_sqrtf(INT64_MAX) == INT32_MAX);
+#endif
+
+ return EC_SUCCESS;
+}
+
static int test_mat33_fp_init_zero(void)
{
const int N = 3;
@@ -308,6 +326,7 @@ void run_test(int argc, char **argv)
RUN_TEST(test_fpv3_dot);
RUN_TEST(test_fpv3_norm_squared);
RUN_TEST(test_fpv3_norm);
+ RUN_TEST(test_int_sqrtf);
RUN_TEST(test_mat33_fp_init_zero);
RUN_TEST(test_mat33_fp_init_diagonal);
RUN_TEST(test_mat33_fp_scalar_mul);