From: Luke Kenneth Casson Leighton Date: Fri, 19 Jun 2020 21:16:07 +0000 (+0100) Subject: add trunc_div and trunc_rem functions X-Git-Tag: 24jan2021_ls180~53 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=9732bac97403bd4b4ca236a72297a181a87dcc50;p=nmutil.git add trunc_div and trunc_rem functions --- 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) + +