shithub: riscv

Download patch

ref: 8dc43a951c051422a63b4ed6298bf84c0896edc2
parent: 454d26a0e4f91d03bb8f1f4f3f7dbe56d722deab
author: aiju <devnull@localhost>
date: Fri May 4 17:18:46 EDT 2018

mpdiv: fix sign bug

--- a/sys/src/libmp/port/mpdiv.c
+++ b/sys/src/libmp/port/mpdiv.c
@@ -9,7 +9,7 @@
 void
 mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder)
 {
-	int j, s, vn, sign;
+	int j, s, vn, sign, qsign, rsign;
 	mpdigit qd, *up, *vp, *qp;
 	mpint *u, *v, *t;
 
@@ -49,6 +49,13 @@
 			mpassign(mpzero, quotient);
 		return;
 	}
+	
+	qsign = divisor->sign * dividend->sign;
+	rsign = dividend->sign;
+	if(quotient != nil)
+		quotient->sign = qsign;
+	if(remainder != nil)
+		remainder->sign = rsign;
 
 	// D1: shift until divisor, v, has hi bit set (needed to make trial
 	//     divisor accurate)
@@ -121,14 +128,11 @@
 	if(qp != nil){
 		assert((quotient->flags & MPtimesafe) == 0);
 		mpnorm(quotient);
-		if(dividend->sign != divisor->sign)
-			quotient->sign = -1;
 	}
 
 	if(remainder != nil){
 		assert((remainder->flags & MPtimesafe) == 0);
 		mpright(u, s, remainder);	// u is the remainder shifted
-		remainder->sign = dividend->sign;
 	}
 
 	mpfree(t);