whoops trunc_div returning neg/neg result rather than abs/abs
[nmutil.git] / src / nmutil / divmod.py
1 # this is a POWER ISA 3.0B compatible div function
2 # however it is also the c, c++, rust, java *and* x86 way of doing things
3 def trunc_div(n, d):
4 abs_n = abs(n)
5 abs_d = abs(d)
6 abs_q = abs_n // abs_d
7 #print ("trunc_div", n.value, d.value,
8 # abs_n.value, abs_d.value, abs_q.value,
9 # n == abs_n, d == abs_d)
10 if (n == abs_n) == (d == abs_d):
11 return abs_q
12 return -abs_q
13
14
15 # this is a POWER ISA 3.0B compatible mod / remainder function
16 # however it is also the c, c++, rust, java *and* x86 way of doing things
17 def trunc_rem(n, d):
18 return n - d * trunc_div(n, d)
19
20