shithub: riscv

Download patch

ref: cf2f2a884137b589f502ffc7af9de5ca78634356
parent: 193daffafb2ca564a47e52489cb082d77fa78872
author: aiju <devnull@localhost>
date: Sun Aug 28 06:49:41 EDT 2016

mp: fix mpnot and add mpasr

--- a/sys/man/2/mp
+++ b/sys/man/2/mp
@@ -127,6 +127,9 @@
 void	mpxtend(mpint *b, int n, mpint *res)
 .PP
 .B
+void	mpasr(mpint *b, int n, mpint *res)
+.PP
+.B
 void	mpmul(mpint *b1, mpint *b2, mpint *prod)
 .PP
 .B
@@ -583,6 +586,12 @@
 .TP
 .I mpnot
 .BR "res = ~b1" .
+.TP
+.I mpasr
+.BR "res = b>>shift"
+(\fImpasr\fR, unlike
+.IR mpright ,
+uses two's complement).
 .TP
 .I mptrunc
 truncates
--- a/sys/src/libmp/port/mplogic.c
+++ b/sys/src/libmp/port/mplogic.c
@@ -85,7 +85,8 @@
 mpnot(mpint *b, mpint *r)
 {
 	mpadd(b, mpone, r);
-	r->sign ^= -2;
+	if(r->top != 0)
+		r->sign ^= -2;
 }
 
 void
@@ -192,4 +193,16 @@
 			c = 0;
 	}
 	mpnorm(r);
+}
+
+void
+mpasr(mpint *b, int n, mpint *r)
+{
+	if(b->sign > 0 || n <= 0){
+		mpright(b, n, r);
+		return;
+	}
+	mpadd(b, mpone, r);
+	mpright(r, n, r);
+	mpsub(r, mpone, r);
 }