From 1559a978100762efd84666befd134d5975ae8d4b Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Fri, 31 Oct 2014 11:28:50 +0000
Subject: [PATCH] py: Add builtin round function.

Addresses issue #934.
---
 lib/libm/roundf.c             | 33 +++++++++++++++++++++++++++++++++
 py/builtin.c                  | 23 ++++++++++++++++++++++-
 py/builtin.h                  |  1 +
 py/builtintables.c            |  1 +
 py/qstrdefs.h                 |  1 +
 stmhal/Makefile               |  1 +
 tests/basics/builtin_round.py | 14 ++++++++++++++
 7 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 lib/libm/roundf.c
 create mode 100644 tests/basics/builtin_round.py

diff --git a/lib/libm/roundf.c b/lib/libm/roundf.c
new file mode 100644
index 000000000..3da1f0595
--- /dev/null
+++ b/lib/libm/roundf.c
@@ -0,0 +1,33 @@
+/*****************************************************************************/
+/*****************************************************************************/
+// roundf from musl-0.9.15
+/*****************************************************************************/
+/*****************************************************************************/
+
+#include "libm.h"
+
+float roundf(float x)
+{
+	union {float f; uint32_t i;} u = {x};
+	int e = u.i >> 23 & 0xff;
+	float_t y;
+
+	if (e >= 0x7f+23)
+		return x;
+	if (u.i >> 31)
+		x = -x;
+	if (e < 0x7f-1) {
+		FORCE_EVAL(x + 0x1p23f);
+		return 0*u.f;
+	}
+	y = (float)(x + 0x1p23f) - 0x1p23f - x;
+	if (y > 0.5f)
+		y = y + x - 1;
+	else if (y <= -0.5f)
+		y = y + x + 1;
+	else
+		y = y + x;
+	if (u.i >> 31)
+		y = -y;
+	return y;
+}
diff --git a/py/builtin.c b/py/builtin.c
index 133473d01..77b58575a 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -448,9 +448,30 @@ STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
     vstr_free(vstr);
     return s;
 }
-
 MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
 
+STATIC mp_obj_t mp_builtin_round(mp_obj_t o_in) {
+    // TODO support second arg
+    if (MP_OBJ_IS_INT(o_in)) {
+        return o_in;
+    }
+#if MICROPY_PY_BUILTINS_FLOAT
+    mp_float_t val = mp_obj_get_float(o_in);
+    mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val);
+    mp_int_t r = rounded;
+    // make rounded value even if it was halfway between ints
+    if (val - rounded == 0.5) {
+        r = (r + 1) & (~1);
+    } else if (val - rounded == -0.5) {
+        r &= ~1;
+    }
+#else
+    mp_int_t r = mp_obj_get_int(o_in);
+#endif
+    return mp_obj_new_int(r);
+}
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_round_obj, mp_builtin_round);
+
 STATIC mp_obj_t mp_builtin_sum(mp_uint_t n_args, const mp_obj_t *args) {
     assert(1 <= n_args && n_args <= 2);
     mp_obj_t value;
diff --git a/py/builtin.h b/py/builtin.h
index 762e10d9d..e102dab8e 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -61,6 +61,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj);
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj);
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj);
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin_repr_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_round_obj);
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj);
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
 
diff --git a/py/builtintables.c b/py/builtintables.c
index 2a5ced52d..b0d20417e 100644
--- a/py/builtintables.c
+++ b/py/builtintables.c
@@ -118,6 +118,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
     { MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_builtin_pow_obj },
     { MP_OBJ_NEW_QSTR(MP_QSTR_print), (mp_obj_t)&mp_builtin_print_obj },
     { MP_OBJ_NEW_QSTR(MP_QSTR_repr), (mp_obj_t)&mp_builtin_repr_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_round), (mp_obj_t)&mp_builtin_round_obj },
     { MP_OBJ_NEW_QSTR(MP_QSTR_sorted), (mp_obj_t)&mp_builtin_sorted_obj },
     { MP_OBJ_NEW_QSTR(MP_QSTR_sum), (mp_obj_t)&mp_builtin_sum_obj },
 
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index 64658b587..74a29d2ee 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -201,6 +201,7 @@ Q(range)
 Q(read)
 Q(repr)
 Q(reversed)
+Q(round)
 Q(sorted)
 Q(staticmethod)
 Q(sum)
diff --git a/stmhal/Makefile b/stmhal/Makefile
index f444f0045..b1308dc7d 100644
--- a/stmhal/Makefile
+++ b/stmhal/Makefile
@@ -68,6 +68,7 @@ SRC_LIB = $(addprefix lib/,\
 	libm/atanf.c \
 	libm/atan2f.c \
 	libm/fmodf.c \
+	libm/roundf.c \
 	)
 
 SRC_C = \
diff --git a/tests/basics/builtin_round.py b/tests/basics/builtin_round.py
new file mode 100644
index 000000000..5ced0c4cd
--- /dev/null
+++ b/tests/basics/builtin_round.py
@@ -0,0 +1,14 @@
+# test round()
+
+# check basic cases
+tests = [
+    False, True,
+    0, 1, -1, 10,
+    0.0, 1.0, 0.1, -0.1, 123.4, 123.6, -123.4, -123.6
+]
+for t in tests:
+    print(round(t))
+
+# check .5 cases
+for i in range(11):
+    print(round((i - 5) / 2))
-- 
GitLab