shithub: riscv

Download patch

ref: 30c9e34c0d0a0247b0adb2c343e30240ed0e2779
parent: d552fed38514dc799c7fea95dfb632c8816c3f60
author: aiju <devnull@localhost>
date: Thu Sep 1 06:55:12 EDT 2016

pc: add cat() function

--- a/sys/man/1/pc
+++ b/sys/man/1/pc
@@ -93,6 +93,9 @@
 .I sbits(n)
 The minimum number of bits required to represent \fIn\fR as an signed number.
 .TP
+.I cat(a0,n0,...,aN,nN)
+Truncate each of the \fIa\fR arguments to \fIn\fR bits and concatenate their binary representation.
+.TP
 .I gcd(n,m)
 The greatest common divisor of \fIn\fR and \fIm\fR.
 .TP
--- a/sys/src/cmd/pc.y
+++ b/sys/src/cmd/pc.y
@@ -836,6 +836,36 @@
 	return a[0];
 }
 
+Num *
+fncat(int n, Num **a)
+{
+	int i, w;
+	Num *r;
+
+	if(n % 2 != 0){
+		error("cat: odd number of arguments");
+		i = 0;
+	fail:
+		for(; i < n; i++)
+			numdecref(a[i]);
+		return nil;
+	}
+	r = numalloc();
+	for(i = 0; i < n; i += 2){
+		if(toint(a[i+1], &w, 1)) goto fail;
+		mpleft(r, w, r);
+		if(a[i]->sign < 0 || mpsignif(a[i]) > w){
+			a[i] = nummod(a[i]);
+			mptrunc(a[i], w, a[i]);
+		}
+		r->b = basemax(r->b, a[i]->b);
+		mpor(r, a[i], r);
+		numdecref(a[i]);
+		numdecref(a[i+1]);
+	}
+	return r;
+}
+
 void
 main(int argc, char **argv)
 {
@@ -863,6 +893,7 @@
 	regfunc("minv", fnminv, 2);
 	regfunc("rand", fnrand, 1);
 	regfunc("rev", fnrev, 2);
+	regfunc("cat", fncat, -1);
 
 	prompt = 1;
 	ARGBEGIN{