From 9732bac97403bd4b4ca236a72297a181a87dcc50 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Fri, 19 Jun 2020 22:16:07 +0100 Subject: [PATCH] add trunc_div and trunc_rem functions --- src/nmutil/divmod.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/nmutil/divmod.py diff --git a/src/nmutil/divmod.py b/src/nmutil/divmod.py new file mode 100644 index 0000000..2dc3196 --- /dev/null +++ b/src/nmutil/divmod.py @@ -0,0 +1,17 @@ +# this is a POWER ISA 3.0B compatible div function +# however it is also the c, c++, rust, java *and* x86 way of doing things +def trunc_div(n, d): + abs_n = abs(n) + abs_d = abs(d) + abs_q = n // d + if (n < 0) == (d < 0): + return abs_q + return -abs_q + + +# this is a POWER ISA 3.0B compatible mod / remainder function +# however it is also the c, c++, rust, java *and* x86 way of doing things +def trunc_rem(n, d): + return n - d * trunc_div(n, d) + + -- 2.30.2