add trunc_div and trunc_rem functions
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 19 Jun 2020 21:16:07 +0000 (22:16 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 19 Jun 2020 21:16:07 +0000 (22:16 +0100)
src/nmutil/divmod.py [new file with mode: 0644]

diff --git a/src/nmutil/divmod.py b/src/nmutil/divmod.py
new file mode 100644 (file)
index 0000000..2dc3196
--- /dev/null
@@ -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)
+
+