Browse Source

Plan 9 from Bell Labs 2013-03-26

David du Colombier 11 years ago
parent
commit
11e8c836fd

+ 0 - 1
sys/src/9/bcm/fpi.c

@@ -1 +0,0 @@
-#include "../omap/fpi.c"

+ 0 - 1
sys/src/9/bcm/fpi.h

@@ -1 +0,0 @@
-#include "../omap/fpi.h"

+ 1 - 1
sys/src/9/bcm/fpiarm.c

@@ -13,7 +13,7 @@
 #include	"ureg.h"
 
 #include	"arm.h"
-#include	"fpi.h"
+#include	"../port/fpi.h"
 
 #define ARM7500			/* emulate old pre-VFP opcodes */
 

+ 0 - 1
sys/src/9/bcm/fpimem.c

@@ -1 +0,0 @@
-#include "../omap/fpimem.c"

+ 1 - 1
sys/src/9/bcm/mkfile

@@ -106,7 +106,7 @@ arch.$O clock.$O fpiarm.$O main.$O mmu.$O screen.$O syscall.$O trap.$O: \
 
 archbcm.$O devether.$0: etherif.h ../port/netif.h
 archbcm.$O: ../port/flashif.h
-fpi.$O fpiarm.$O fpimem.$O: fpi.h
+fpi.$O fpiarm.$O fpimem.$O: ../port/fpi.h
 l.$O lexception.$O lproc.$O mmu.$O: arm.s mem.h
 main.$O: errstr.h init.h reboot.h
 devmouse.$O mouse.$O screen.$O: screen.h

+ 0 - 1
sys/src/9/bcm/random.c

@@ -1 +0,0 @@
-#include "../omap/random.c"

+ 1 - 1
sys/src/9/kw/fpiarm.c

@@ -13,7 +13,7 @@
 #include	"ureg.h"
 
 #include	"arm.h"
-#include	"fpi.h"
+#include	"../port/fpi.h"
 
 /* undef this if correct kernel r13 isn't in Ureg;
  * check calculation in fpiarm below

+ 1 - 1
sys/src/9/kw/mkfile

@@ -128,7 +128,7 @@ arch.$O clock.$O fpiarm.$O main.$O mmu.$O screen.$O sdscsi.$O syscall.$O \
 archkw.$O devether.$O ether1116.$O ethermii.$O: \
 	etherif.h ethermii.h ../port/netif.h
 archkw.$O devflash.$O flashkw.$O: ../port/flashif.h
-fpi.$O fpiarm.$O fpimem.$O: fpi.h
+fpi.$O fpiarm.$O fpimem.$O: ../port/fpi.h
 l.$O lexception.$O lproc.$O mmu.$O: arm.s arm.h mem.h
 main.$O:	errstr.h init.h reboot.h
 mouse.$O:	screen.h

+ 0 - 138
sys/src/9/kw/random.c

@@ -1,138 +0,0 @@
-#include	"u.h"
-#include	"../port/lib.h"
-#include	"mem.h"
-#include	"dat.h"
-#include	"fns.h"
-#include	"../port/error.h"
-
-
-struct Rb
-{
-	QLock;
-	Rendez	producer;
-	Rendez	consumer;
-	ulong	randomcount;
-	uchar	buf[128];
-	uchar	*ep;
-	uchar	*rp;
-	uchar	*wp;
-	uchar	next;
-	uchar	wakeme;
-	ushort	bits;
-	ulong	randn;
-} rb;
-
-static int
-rbnotfull(void*)
-{
-	int i;
-
-	i = rb.rp - rb.wp;
-	return i != 1 && i != (1 - sizeof(rb.buf));
-}
-
-static int
-rbnotempty(void*)
-{
-	return rb.wp != rb.rp;
-}
-
-static void
-genrandom(void*)
-{
-	up->basepri = PriNormal;
-	up->priority = up->basepri;
-
-	for(;;){
-		for(;;)
-			if(++rb.randomcount > 100000)
-				break;
-		if(anyhigher())
-			sched();
-		if(!rbnotfull(0))
-			sleep(&rb.producer, rbnotfull, 0);
-	}
-}
-
-/*
- *  produce random bits in a circular buffer
- */
-static void
-randomclock(void)
-{
-	if(rb.randomcount == 0 || !rbnotfull(0))
-		return;
-
-	rb.bits = (rb.bits<<2) ^ rb.randomcount;
-	rb.randomcount = 0;
-
-	rb.next++;
-	if(rb.next != 8/2)
-		return;
-	rb.next = 0;
-
-	*rb.wp ^= rb.bits;
-	if(rb.wp+1 == rb.ep)
-		rb.wp = rb.buf;
-	else
-		rb.wp = rb.wp+1;
-
-	if(rb.wakeme)
-		wakeup(&rb.consumer);
-}
-
-void
-randominit(void)
-{
-	addclock0link(randomclock, 1000/HZ);
-	rb.ep = rb.buf + sizeof(rb.buf);
-	rb.rp = rb.wp = rb.buf;
-	kproc("genrandom", genrandom, 0);
-}
-
-/*
- *  consume random bytes from a circular buffer
- */
-ulong
-randomread(void *xp, ulong n)
-{
-	uchar *e, *p;
-	ulong x;
-
-	p = xp;
-
-	if(waserror()){
-		qunlock(&rb);
-		nexterror();
-	}
-
-	qlock(&rb);
-	for(e = p + n; p < e; ){
-		if(rb.wp == rb.rp){
-			rb.wakeme = 1;
-			wakeup(&rb.producer);
-			sleep(&rb.consumer, rbnotempty, 0);
-			rb.wakeme = 0;
-			continue;
-		}
-
-		/*
-		 *  beating clocks will be predictable if
-		 *  they are synchronized.  Use a cheap pseudo
-		 *  random number generator to obscure any cycles.
-		 */
-		x = rb.randn*1103515245 ^ *rb.rp;
-		*p++ = rb.randn = x;
-
-		if(rb.rp+1 == rb.ep)
-			rb.rp = rb.buf;
-		else
-			rb.rp = rb.rp+1;
-	}
-	qunlock(&rb);
-	poperror();
-
-	wakeup(&rb.producer);
-
-	return n;
-}

+ 0 - 138
sys/src/9/mtx/random.c

@@ -1,138 +0,0 @@
-#include	"u.h"
-#include	"../port/lib.h"
-#include	"mem.h"
-#include	"dat.h"
-#include	"fns.h"
-#include	"../port/error.h"
-
-
-struct Rb
-{
-	QLock;
-	Rendez	producer;
-	Rendez	consumer;
-	ulong	randomcount;
-	uchar	buf[1024];
-	uchar	*ep;
-	uchar	*rp;
-	uchar	*wp;
-	uchar	next;
-	uchar	wakeme;
-	ushort	bits;
-	ulong	randn;
-} rb;
-
-static int
-rbnotfull(void*)
-{
-	int i;
-
-	i = rb.rp - rb.wp;
-	return i != 1 && i != (1 - sizeof(rb.buf));
-}
-
-static int
-rbnotempty(void*)
-{
-	return rb.wp != rb.rp;
-}
-
-static void
-genrandom(void*)
-{
-	up->basepri = PriNormal;
-	up->priority = up->basepri;
-
-	for(;;){
-		for(;;)
-			if(++rb.randomcount > 100000)
-				break;
-		if(anyhigher())
-			sched();
-		if(!rbnotfull(0))
-			sleep(&rb.producer, rbnotfull, 0);
-	}
-}
-
-/*
- *  produce random bits in a circular buffer
- */
-static void
-randomclock(void)
-{
-	if(rb.randomcount == 0 || !rbnotfull(0))
-		return;
-
-	rb.bits = (rb.bits<<2) ^ rb.randomcount;
-	rb.randomcount = 0;
-
-	rb.next++;
-	if(rb.next != 8/2)
-		return;
-	rb.next = 0;
-
-	*rb.wp ^= rb.bits;
-	if(rb.wp+1 == rb.ep)
-		rb.wp = rb.buf;
-	else
-		rb.wp = rb.wp+1;
-
-	if(rb.wakeme)
-		wakeup(&rb.consumer);
-}
-
-void
-randominit(void)
-{
-	addclock0link(randomclock, 1000/HZ);
-	rb.ep = rb.buf + sizeof(rb.buf);
-	rb.rp = rb.wp = rb.buf;
-	kproc("genrandom", genrandom, 0);
-}
-
-/*
- *  consume random bytes from a circular buffer
- */
-ulong
-randomread(void *xp, ulong n)
-{
-	uchar *e, *p;
-	ulong x;
-
-	p = xp;
-
-	if(waserror()){
-		qunlock(&rb);
-		nexterror();
-	}
-
-	qlock(&rb);
-	for(e = p + n; p < e; ){
-		if(rb.wp == rb.rp){
-			rb.wakeme = 1;
-			wakeup(&rb.producer);
-			sleep(&rb.consumer, rbnotempty, 0);
-			rb.wakeme = 0;
-			continue;
-		}
-
-		/*
-		 *  beating clocks will be precictable if
-		 *  they are synchronized.  Use a cheap pseudo
-		 *  random number generator to obscure any cycles.
-		 */
-		x = rb.randn*1103515245 ^ *rb.rp;
-		*p++ = rb.randn = x;
-
-		if(rb.rp+1 == rb.ep)
-			rb.rp = rb.buf;
-		else
-			rb.rp = rb.rp+1;
-	}
-	qunlock(&rb);
-	poperror();
-
-	wakeup(&rb.producer);
-
-	return n;
-}

+ 0 - 300
sys/src/9/omap/fpi.c

@@ -1,300 +0,0 @@
-/*
- * Floating Point Interpreter.
- * shamelessly stolen from an original by ark.
- */
-#include "fpi.h"
-
-void
-fpiround(Internal *i)
-{
-	unsigned long guard;
-
-	guard = i->l & GuardMask;
-	i->l &= ~GuardMask;
-	if(guard > (LsBit>>1) || (guard == (LsBit>>1) && (i->l & LsBit))){
-		i->l += LsBit;
-		if(i->l & CarryBit){
-			i->l &= ~CarryBit;
-			i->h++;
-			if(i->h & CarryBit){
-				if (i->h & 0x01)
-					i->l |= CarryBit;
-				i->l >>= 1;
-				i->h >>= 1;
-				i->e++;
-			}
-		}
-	}
-}
-
-static void
-matchexponents(Internal *x, Internal *y)
-{
-	int count;
-
-	count = y->e - x->e;
-	x->e = y->e;
-	if(count >= 2*FractBits){
-		x->l = x->l || x->h;
-		x->h = 0;
-		return;
-	}
-	if(count >= FractBits){
-		count -= FractBits;
-		x->l = x->h|(x->l != 0);
-		x->h = 0;
-	}
-	while(count > 0){
-		count--;
-		if(x->h & 0x01)
-			x->l |= CarryBit;
-		if(x->l & 0x01)
-			x->l |= 2;
-		x->l >>= 1;
-		x->h >>= 1;
-	}
-}
-
-static void
-shift(Internal *i)
-{
-	i->e--;
-	i->h <<= 1;
-	i->l <<= 1;
-	if(i->l & CarryBit){
-		i->l &= ~CarryBit;
-		i->h |= 0x01;
-	}
-}
-
-static void
-normalise(Internal *i)
-{
-	while((i->h & HiddenBit) == 0)
-		shift(i);
-}
-
-static void
-renormalise(Internal *i)
-{
-	if(i->e < -2 * FractBits)
-		i->e = -2 * FractBits;
-	while(i->e < 1){
-		i->e++;
-		if(i->h & 0x01)
-			i->l |= CarryBit;
-		i->h >>= 1;
-		i->l = (i->l>>1)|(i->l & 0x01);
-	}
-	if(i->e >= ExpInfinity)
-		SetInfinity(i);
-}
-
-void
-fpinormalise(Internal *x)
-{
-	if(!IsWeird(x) && !IsZero(x))
-		normalise(x);
-}
-
-void
-fpiadd(Internal *x, Internal *y, Internal *i)
-{
-	Internal *t;
-
-	i->s = x->s;
-	if(IsWeird(x) || IsWeird(y)){
-		if(IsNaN(x) || IsNaN(y))
-			SetQNaN(i);
-		else
-			SetInfinity(i);
-		return;
-	}
-	if(x->e > y->e){
-		t = x;
-		x = y;
-		y = t;
-	}
-	matchexponents(x, y);
-	i->e = x->e;
-	i->h = x->h + y->h;
-	i->l = x->l + y->l;
-	if(i->l & CarryBit){
-		i->h++;
-		i->l &= ~CarryBit;
-	}
-	if(i->h & (HiddenBit<<1)){
-		if(i->h & 0x01)
-			i->l |= CarryBit;
-		i->l = (i->l>>1)|(i->l & 0x01);
-		i->h >>= 1;
-		i->e++;
-	}
-	if(IsWeird(i))
-		SetInfinity(i);
-}
-
-void
-fpisub(Internal *x, Internal *y, Internal *i)
-{
-	Internal *t;
-
-	if(y->e < x->e
-	   || (y->e == x->e && (y->h < x->h || (y->h == x->h && y->l < x->l)))){
-		t = x;
-		x = y;
-		y = t;
-	}
-	i->s = y->s;
-	if(IsNaN(y)){
-		SetQNaN(i);
-		return;
-	}
-	if(IsInfinity(y)){
-		if(IsInfinity(x))
-			SetQNaN(i);
-		else
-			SetInfinity(i);
-		return;
-	}
-	matchexponents(x, y);
-	i->e = y->e;
-	i->h = y->h - x->h;
-	i->l = y->l - x->l;
-	if(i->l < 0){
-		i->l += CarryBit;
-		i->h--;
-	}
-	if(i->h == 0 && i->l == 0)
-		SetZero(i);
-	else while(i->e > 1 && (i->h & HiddenBit) == 0)
-		shift(i);
-}
-
-#define	CHUNK		(FractBits/2)
-#define	CMASK		((1<<CHUNK)-1)
-#define	HI(x)		((short)((x)>>CHUNK) & CMASK)
-#define	LO(x)		((short)(x) & CMASK)
-#define	SPILL(x)	((x)>>CHUNK)
-#define	M(x, y)		((long)a[x]*(long)b[y])
-#define	C(h, l)		(((long)((h) & CMASK)<<CHUNK)|((l) & CMASK))
-
-void
-fpimul(Internal *x, Internal *y, Internal *i)
-{
-	long a[4], b[4], c[7], f[4];
-
-	i->s = x->s^y->s;
-	if(IsWeird(x) || IsWeird(y)){
-		if(IsNaN(x) || IsNaN(y) || IsZero(x) || IsZero(y))
-			SetQNaN(i);
-		else
-			SetInfinity(i);
-		return;
-	}
-	else if(IsZero(x) || IsZero(y)){
-		SetZero(i);
-		return;
-	}
-	normalise(x);
-	normalise(y);
-	i->e = x->e + y->e - (ExpBias - 1);
-
-	a[0] = HI(x->h); b[0] = HI(y->h);
-	a[1] = LO(x->h); b[1] = LO(y->h);
-	a[2] = HI(x->l); b[2] = HI(y->l);
-	a[3] = LO(x->l); b[3] = LO(y->l);
-
-	c[6] =                               M(3, 3);
-	c[5] =                     M(2, 3) + M(3, 2) + SPILL(c[6]);
-	c[4] =           M(1, 3) + M(2, 2) + M(3, 1) + SPILL(c[5]);
-	c[3] = M(0, 3) + M(1, 2) + M(2, 1) + M(3, 0) + SPILL(c[4]);
-	c[2] = M(0, 2) + M(1, 1) + M(2, 0)           + SPILL(c[3]);
-	c[1] = M(0, 1) + M(1, 0)                     + SPILL(c[2]);
-	c[0] = M(0, 0)                               + SPILL(c[1]);
-
-	f[0] = c[0];
-	f[1] = C(c[1], c[2]);
-	f[2] = C(c[3], c[4]);
-	f[3] = C(c[5], c[6]);
-
-	if((f[0] & HiddenBit) == 0){
-		f[0] <<= 1;
-		f[1] <<= 1;
-		f[2] <<= 1;
-		f[3] <<= 1;
-		if(f[1] & CarryBit){
-			f[0] |= 1;
-			f[1] &= ~CarryBit;
-		}
-		if(f[2] & CarryBit){
-			f[1] |= 1;
-			f[2] &= ~CarryBit;
-		}
-		if(f[3] & CarryBit){
-			f[2] |= 1;
-			f[3] &= ~CarryBit;
-		}
-		i->e--;
-	}
-	i->h = f[0];
-	i->l = f[1];
-	if(f[2] || f[3])
-		i->l |= 1;
-	renormalise(i);
-}
-
-void
-fpidiv(Internal *x, Internal *y, Internal *i)
-{
-	i->s = x->s^y->s;
-	if(IsNaN(x) || IsNaN(y)
-	   || (IsInfinity(x) && IsInfinity(y)) || (IsZero(x) && IsZero(y))){
-		SetQNaN(i);
-		return;
-	}
-	else if(IsZero(x) || IsInfinity(y)){
-		SetInfinity(i);
-		return;
-	}
-	else if(IsInfinity(x) || IsZero(y)){
-		SetZero(i);
-		return;
-	}
-	normalise(x);
-	normalise(y);
-	i->h = 0;
-	i->l = 0;
-	i->e = y->e - x->e + (ExpBias + 2*FractBits - 1);
-	do{
-		if(y->h > x->h || (y->h == x->h && y->l >= x->l)){
-			i->l |= 0x01;
-			y->h -= x->h;
-			y->l -= x->l;
-			if(y->l < 0){
-				y->l += CarryBit;
-				y->h--;
-			}
-		}
-		shift(y);
-		shift(i);
-	}while ((i->h & HiddenBit) == 0);
-	if(y->h || y->l)
-		i->l |= 0x01;
-	renormalise(i);
-}
-
-int
-fpicmp(Internal *x, Internal *y)
-{
-	if(IsNaN(x) && IsNaN(y))
-		return 0;
-	if(IsInfinity(x) && IsInfinity(y))
-		return y->s - x->s;
-	if(x->e == y->e && x->h == y->h && x->l == y->l)
-		return y->s - x->s;
-	if(x->e < y->e
-	   || (x->e == y->e && (x->h < y->h || (x->h == y->h && x->l < y->l))))
-		return y->s ? 1: -1;
-	return x->s ? -1: 1;
-}

+ 0 - 61
sys/src/9/omap/fpi.h

@@ -1,61 +0,0 @@
-typedef long Word;
-typedef unsigned long Single;
-typedef struct {
-	unsigned long l;
-	unsigned long h;
-} Double;
-
-enum {
-	FractBits	= 28,
-	CarryBit	= 0x10000000,
-	HiddenBit	= 0x08000000,
-	MsBit		= HiddenBit,
-	NGuardBits	= 3,
-	GuardMask	= 0x07,
-	LsBit		= (1<<NGuardBits),
-
-	SingleExpBias	= 127,
-	SingleExpMax	= 255,
-	DoubleExpBias	= 1023,
-	DoubleExpMax	= 2047,
-
-	ExpBias		= DoubleExpBias,
-	ExpInfinity	= DoubleExpMax,
-};
-
-typedef struct {
-	unsigned char s;
-	short e;
-	long l;				/* 0000FFFFFFFFFFFFFFFFFFFFFFFFFGGG */
-	long h;				/* 0000HFFFFFFFFFFFFFFFFFFFFFFFFFFF */
-} Internal;
-
-#define IsWeird(n)	((n)->e >= ExpInfinity)
-#define	IsInfinity(n)	(IsWeird(n) && (n)->h == HiddenBit && (n)->l == 0)
-#define	SetInfinity(n)	((n)->e = ExpInfinity, (n)->h = HiddenBit, (n)->l = 0)
-#define IsNaN(n)	(IsWeird(n) && (((n)->h & ~HiddenBit) || (n)->l))
-#define	SetQNaN(n)	((n)->s = 0, (n)->e = ExpInfinity, 		\
-			 (n)->h = HiddenBit|(LsBit<<1), (n)->l = 0)
-#define IsZero(n)	((n)->e == 1 && (n)->h == 0 && (n)->l == 0)
-#define SetZero(n)	((n)->e = 1, (n)->h = 0, (n)->l = 0)
-
-/*
- * fpi.c
- */
-extern void fpiround(Internal *);
-extern void fpiadd(Internal *, Internal *, Internal *);
-extern void fpisub(Internal *, Internal *, Internal *);
-extern void fpimul(Internal *, Internal *, Internal *);
-extern void fpidiv(Internal *, Internal *, Internal *);
-extern int fpicmp(Internal *, Internal *);
-extern void fpinormalise(Internal*);
-
-/*
- * fpimem.c
- */
-extern void fpis2i(Internal *, void *);
-extern void fpid2i(Internal *, void *);
-extern void fpiw2i(Internal *, void *);
-extern void fpii2s(void *, Internal *);
-extern void fpii2d(void *, Internal *);
-extern void fpii2w(Word *, Internal *);

+ 1 - 1
sys/src/9/omap/fpiarm.c

@@ -13,7 +13,7 @@
 #include	"ureg.h"
 
 #include	"arm.h"
-#include	"fpi.h"
+#include	"../port/fpi.h"
 
 /* undef this if correct kernel r13 isn't in Ureg;
  * check calculation in fpiarm below

+ 0 - 136
sys/src/9/omap/fpimem.c

@@ -1,136 +0,0 @@
-#include "fpi.h"
-
-/*
- * the following routines depend on memory format, not the machine
- */
-
-void
-fpis2i(Internal *i, void *v)
-{
-	Single *s = v;
-
-	i->s = (*s & 0x80000000) ? 1: 0;
-	if((*s & ~0x80000000) == 0){
-		SetZero(i);
-		return;
-	}
-	i->e = ((*s>>23) & 0x00FF) - SingleExpBias + ExpBias;
-	i->h = (*s & 0x007FFFFF)<<(1+NGuardBits);
-	i->l = 0;
-	if(i->e)
-		i->h |= HiddenBit;
-	else
-		i->e++;
-}
-
-void
-fpid2i(Internal *i, void *v)
-{
-	Double *d = v;
-
-	i->s = (d->h & 0x80000000) ? 1: 0;
-	i->e = (d->h>>20) & 0x07FF;
-	i->h = ((d->h & 0x000FFFFF)<<(4+NGuardBits))|((d->l>>25) & 0x7F);
-	i->l = (d->l & 0x01FFFFFF)<<NGuardBits;
-	if(i->e)
-		i->h |= HiddenBit;
-	else
-		i->e++;
-}
-
-void
-fpiw2i(Internal *i, void *v)
-{
-	Word w, word = *(Word*)v;
-	short e;
-
-	if(word < 0){
-		i->s = 1;
-		word = -word;
-	}
-	else
-		i->s = 0;
-	if(word == 0){
-		SetZero(i);
-		return;
-	}
-	if(word > 0){
-		for (e = 0, w = word; w; w >>= 1, e++)
-			;
-	} else
-		e = 32;
-	if(e > FractBits){
-		i->h = word>>(e - FractBits);
-		i->l = (word & ((1<<(e - FractBits)) - 1))<<(2*FractBits - e);
-	}
-	else {
-		i->h = word<<(FractBits - e);
-		i->l = 0;
-	}
-	i->e = (e - 1) + ExpBias;
-}
-
-void
-fpii2s(void *v, Internal *i)
-{
-	short e;
-	Single *s = (Single*)v;
-
-	fpiround(i);
-	if(i->h & HiddenBit)
-		i->h &= ~HiddenBit;
-	else
-		i->e--;
-	*s = i->s ? 0x80000000: 0;
-	e = i->e;
-	if(e < ExpBias){
-		if(e <= (ExpBias - SingleExpBias))
-			return;
-		e = SingleExpBias - (ExpBias - e);
-	}
-	else  if(e >= (ExpBias + (SingleExpMax-SingleExpBias))){
-		*s |= SingleExpMax<<23;
-		return;
-	}
-	else
-		e = SingleExpBias + (e - ExpBias);
-	*s |= (e<<23)|(i->h>>(1+NGuardBits));
-}
-
-void
-fpii2d(void *v, Internal *i)
-{
-	Double *d = (Double*)v;
-
-	fpiround(i);
-	if(i->h & HiddenBit)
-		i->h &= ~HiddenBit;
-	else
-		i->e--;
-	i->l = ((i->h & GuardMask)<<25)|(i->l>>NGuardBits);
-	i->h >>= NGuardBits;
-	d->h = i->s ? 0x80000000: 0;
-	d->h |= (i->e<<20)|((i->h & 0x00FFFFFF)>>4);
-	d->l = (i->h<<28)|i->l;
-}
-
-void
-fpii2w(Word *word, Internal *i)
-{
-	Word w;
-	short e;
-
-	fpiround(i);
-	e = (i->e - ExpBias) + 1;
-	if(e <= 0)
-		w = 0;
-	else if(e > 31)
-		w = 0x7FFFFFFF;
-	else if(e > FractBits)
-		w = (i->h<<(e - FractBits))|(i->l>>(2*FractBits - e));
-	else
-		w = i->h>>(FractBits-e);
-	if(i->s)
-		w = -w;
-	*word = w;
-}

+ 1 - 1
sys/src/9/omap/mkfile

@@ -106,7 +106,7 @@ arch.$O clock.$O fpiarm.$O main.$O mmu.$O screen.$O sdscsi.$O syscall.$O \
 archomap.$O devether.$0 ether9221.$O: etherif.h ../port/netif.h
 archomap.$O devflash.$O flashbeagle.$O flashigep.$O: ../port/flashif.h
 ecc.$O flashbeagle.$O flashigep.$O: ../port/nandecc.h io.h
-fpi.$O fpiarm.$O fpimem.$O: fpi.h
+fpi.$O fpiarm.$O fpimem.$O: ../port/fpi.h
 l.$O lexception.$O lproc.$O mmu.$O: arm.s arm.h mem.h
 l.$O rebootcode.$O: cache.v7.s
 main.$O: errstr.h init.h reboot.h

+ 0 - 138
sys/src/9/omap/random.c

@@ -1,138 +0,0 @@
-#include	"u.h"
-#include	"../port/lib.h"
-#include	"mem.h"
-#include	"dat.h"
-#include	"fns.h"
-#include	"../port/error.h"
-
-
-struct Rb
-{
-	QLock;
-	Rendez	producer;
-	Rendez	consumer;
-	ulong	randomcount;
-	uchar	buf[128];
-	uchar	*ep;
-	uchar	*rp;
-	uchar	*wp;
-	uchar	next;
-	uchar	wakeme;
-	ushort	bits;
-	ulong	randn;
-} rb;
-
-static int
-rbnotfull(void*)
-{
-	int i;
-
-	i = rb.rp - rb.wp;
-	return i != 1 && i != (1 - sizeof(rb.buf));
-}
-
-static int
-rbnotempty(void*)
-{
-	return rb.wp != rb.rp;
-}
-
-static void
-genrandom(void*)
-{
-	up->basepri = PriNormal;
-	up->priority = up->basepri;
-
-	for(;;){
-		for(;;)
-			if(++rb.randomcount > 100000)
-				break;
-		if(anyhigher())
-			sched();
-		if(!rbnotfull(0))
-			sleep(&rb.producer, rbnotfull, 0);
-	}
-}
-
-/*
- *  produce random bits in a circular buffer
- */
-static void
-randomclock(void)
-{
-	if(rb.randomcount == 0 || !rbnotfull(0))
-		return;
-
-	rb.bits = (rb.bits<<2) ^ rb.randomcount;
-	rb.randomcount = 0;
-
-	rb.next++;
-	if(rb.next != 8/2)
-		return;
-	rb.next = 0;
-
-	*rb.wp ^= rb.bits;
-	if(rb.wp+1 == rb.ep)
-		rb.wp = rb.buf;
-	else
-		rb.wp = rb.wp+1;
-
-	if(rb.wakeme)
-		wakeup(&rb.consumer);
-}
-
-void
-randominit(void)
-{
-	addclock0link(randomclock, 1000/HZ);
-	rb.ep = rb.buf + sizeof(rb.buf);
-	rb.rp = rb.wp = rb.buf;
-	kproc("genrandom", genrandom, 0);
-}
-
-/*
- *  consume random bytes from a circular buffer
- */
-ulong
-randomread(void *xp, ulong n)
-{
-	uchar *e, *p;
-	ulong x;
-
-	p = xp;
-
-	if(waserror()){
-		qunlock(&rb);
-		nexterror();
-	}
-
-	qlock(&rb);
-	for(e = p + n; p < e; ){
-		if(rb.wp == rb.rp){
-			rb.wakeme = 1;
-			wakeup(&rb.producer);
-			sleep(&rb.consumer, rbnotempty, 0);
-			rb.wakeme = 0;
-			continue;
-		}
-
-		/*
-		 *  beating clocks will be predictable if
-		 *  they are synchronized.  Use a cheap pseudo
-		 *  random number generator to obscure any cycles.
-		 */
-		x = rb.randn*1103515245 ^ *rb.rp;
-		*p++ = rb.randn = x;
-
-		if(rb.rp+1 == rb.ep)
-			rb.rp = rb.buf;
-		else
-			rb.rp = rb.rp+1;
-	}
-	qunlock(&rb);
-	poperror();
-
-	wakeup(&rb.producer);
-
-	return n;
-}

+ 0 - 0
sys/src/9/kw/fpi.c → sys/src/9/port/fpi.c


+ 0 - 0
sys/src/9/kw/fpi.h → sys/src/9/port/fpi.h


+ 0 - 0
sys/src/9/kw/fpimem.c → sys/src/9/port/fpimem.c


+ 0 - 0
sys/src/9/pc/random.c → sys/src/9/port/random.c


+ 0 - 138
sys/src/9/ppc/random.c

@@ -1,138 +0,0 @@
-#include	"u.h"
-#include	"../port/lib.h"
-#include	"mem.h"
-#include	"dat.h"
-#include	"fns.h"
-#include	"../port/error.h"
-
-
-struct Rb
-{
-	QLock;
-	Rendez	producer;
-	Rendez	consumer;
-	ulong	randomcount;
-	uchar	buf[1024];
-	uchar	*ep;
-	uchar	*rp;
-	uchar	*wp;
-	uchar	next;
-	uchar	wakeme;
-	ushort	bits;
-	ulong	randn;
-} rb;
-
-static int
-rbnotfull(void*)
-{
-	int i;
-
-	i = rb.rp - rb.wp;
-	return i != 1 && i != (1 - sizeof(rb.buf));
-}
-
-static int
-rbnotempty(void*)
-{
-	return rb.wp != rb.rp;
-}
-
-static void
-genrandom(void*)
-{
-	up->basepri = PriNormal;
-	up->priority = up->basepri;
-
-	for(;;){
-		for(;;)
-			if(++rb.randomcount > 100000)
-				break;
-		if(anyhigher())
-			sched();
-		if(!rbnotfull(0))
-			sleep(&rb.producer, rbnotfull, 0);
-	}
-}
-
-/*
- *  produce random bits in a circular buffer
- */
-static void
-randomclock(void)
-{
-	if(rb.randomcount == 0 || !rbnotfull(0))
-		return;
-
-	rb.bits = (rb.bits<<2) ^ rb.randomcount;
-	rb.randomcount = 0;
-
-	rb.next++;
-	if(rb.next != 8/2)
-		return;
-	rb.next = 0;
-
-	*rb.wp ^= rb.bits;
-	if(rb.wp+1 == rb.ep)
-		rb.wp = rb.buf;
-	else
-		rb.wp = rb.wp+1;
-
-	if(rb.wakeme)
-		wakeup(&rb.consumer);
-}
-
-void
-randominit(void)
-{
-	addclock0link(randomclock, 1000/HZ);
-	rb.ep = rb.buf + sizeof(rb.buf);
-	rb.rp = rb.wp = rb.buf;
-	kproc("genrandom", genrandom, 0);
-}
-
-/*
- *  consume random bytes from a circular buffer
- */
-ulong
-randomread(void *xp, ulong n)
-{
-	uchar *e, *p;
-	ulong x;
-
-	p = xp;
-
-	if(waserror()){
-		qunlock(&rb);
-		nexterror();
-	}
-
-	qlock(&rb);
-	for(e = p + n; p < e; ){
-		if(rb.wp == rb.rp){
-			rb.wakeme = 1;
-			wakeup(&rb.producer);
-			sleep(&rb.consumer, rbnotempty, 0);
-			rb.wakeme = 0;
-			continue;
-		}
-
-		/*
-		 *  beating clocks will be precictable if
-		 *  they are synchronized.  Use a cheap pseudo
-		 *  random number generator to obscure any cycles.
-		 */
-		x = rb.randn*1103515245 ^ *rb.rp;
-		*p++ = rb.randn = x;
-
-		if(rb.rp+1 == rb.ep)
-			rb.rp = rb.buf;
-		else
-			rb.rp = rb.rp+1;
-	}
-	qunlock(&rb);
-	poperror();
-
-	wakeup(&rb.producer);
-
-	return n;
-}

+ 0 - 300
sys/src/9/teg2/fpi.c

@@ -1,300 +0,0 @@
-/*
- * Floating Point Interpreter.
- * shamelessly stolen from an original by ark.
- */
-#include "fpi.h"
-
-void
-fpiround(Internal *i)
-{
-	unsigned long guard;
-
-	guard = i->l & GuardMask;
-	i->l &= ~GuardMask;
-	if(guard > (LsBit>>1) || (guard == (LsBit>>1) && (i->l & LsBit))){
-		i->l += LsBit;
-		if(i->l & CarryBit){
-			i->l &= ~CarryBit;
-			i->h++;
-			if(i->h & CarryBit){
-				if (i->h & 0x01)
-					i->l |= CarryBit;
-				i->l >>= 1;
-				i->h >>= 1;
-				i->e++;
-			}
-		}
-	}
-}
-
-static void
-matchexponents(Internal *x, Internal *y)
-{
-	int count;
-
-	count = y->e - x->e;
-	x->e = y->e;
-	if(count >= 2*FractBits){
-		x->l = x->l || x->h;
-		x->h = 0;
-		return;
-	}
-	if(count >= FractBits){
-		count -= FractBits;
-		x->l = x->h|(x->l != 0);
-		x->h = 0;
-	}
-	while(count > 0){
-		count--;
-		if(x->h & 0x01)
-			x->l |= CarryBit;
-		if(x->l & 0x01)
-			x->l |= 2;
-		x->l >>= 1;
-		x->h >>= 1;
-	}
-}
-
-static void
-shift(Internal *i)
-{
-	i->e--;
-	i->h <<= 1;
-	i->l <<= 1;
-	if(i->l & CarryBit){
-		i->l &= ~CarryBit;
-		i->h |= 0x01;
-	}
-}
-
-static void
-normalise(Internal *i)
-{
-	while((i->h & HiddenBit) == 0)
-		shift(i);
-}
-
-static void
-renormalise(Internal *i)
-{
-	if(i->e < -2 * FractBits)
-		i->e = -2 * FractBits;
-	while(i->e < 1){
-		i->e++;
-		if(i->h & 0x01)
-			i->l |= CarryBit;
-		i->h >>= 1;
-		i->l = (i->l>>1)|(i->l & 0x01);
-	}
-	if(i->e >= ExpInfinity)
-		SetInfinity(i);
-}
-
-void
-fpinormalise(Internal *x)
-{
-	if(!IsWeird(x) && !IsZero(x))
-		normalise(x);
-}
-
-void
-fpiadd(Internal *x, Internal *y, Internal *i)
-{
-	Internal *t;
-
-	i->s = x->s;
-	if(IsWeird(x) || IsWeird(y)){
-		if(IsNaN(x) || IsNaN(y))
-			SetQNaN(i);
-		else
-			SetInfinity(i);
-		return;
-	}
-	if(x->e > y->e){
-		t = x;
-		x = y;
-		y = t;
-	}
-	matchexponents(x, y);
-	i->e = x->e;
-	i->h = x->h + y->h;
-	i->l = x->l + y->l;
-	if(i->l & CarryBit){
-		i->h++;
-		i->l &= ~CarryBit;
-	}
-	if(i->h & (HiddenBit<<1)){
-		if(i->h & 0x01)
-			i->l |= CarryBit;
-		i->l = (i->l>>1)|(i->l & 0x01);
-		i->h >>= 1;
-		i->e++;
-	}
-	if(IsWeird(i))
-		SetInfinity(i);
-}
-
-void
-fpisub(Internal *x, Internal *y, Internal *i)
-{
-	Internal *t;
-
-	if(y->e < x->e
-	   || (y->e == x->e && (y->h < x->h || (y->h == x->h && y->l < x->l)))){
-		t = x;
-		x = y;
-		y = t;
-	}
-	i->s = y->s;
-	if(IsNaN(y)){
-		SetQNaN(i);
-		return;
-	}
-	if(IsInfinity(y)){
-		if(IsInfinity(x))
-			SetQNaN(i);
-		else
-			SetInfinity(i);
-		return;
-	}
-	matchexponents(x, y);
-	i->e = y->e;
-	i->h = y->h - x->h;
-	i->l = y->l - x->l;
-	if(i->l < 0){
-		i->l += CarryBit;
-		i->h--;
-	}
-	if(i->h == 0 && i->l == 0)
-		SetZero(i);
-	else while(i->e > 1 && (i->h & HiddenBit) == 0)
-		shift(i);
-}
-
-#define	CHUNK		(FractBits/2)
-#define	CMASK		((1<<CHUNK)-1)
-#define	HI(x)		((short)((x)>>CHUNK) & CMASK)
-#define	LO(x)		((short)(x) & CMASK)
-#define	SPILL(x)	((x)>>CHUNK)
-#define	M(x, y)		((long)a[x]*(long)b[y])
-#define	C(h, l)		(((long)((h) & CMASK)<<CHUNK)|((l) & CMASK))
-
-void
-fpimul(Internal *x, Internal *y, Internal *i)
-{
-	long a[4], b[4], c[7], f[4];
-
-	i->s = x->s^y->s;
-	if(IsWeird(x) || IsWeird(y)){
-		if(IsNaN(x) || IsNaN(y) || IsZero(x) || IsZero(y))
-			SetQNaN(i);
-		else
-			SetInfinity(i);
-		return;
-	}
-	else if(IsZero(x) || IsZero(y)){
-		SetZero(i);
-		return;
-	}
-	normalise(x);
-	normalise(y);
-	i->e = x->e + y->e - (ExpBias - 1);
-
-	a[0] = HI(x->h); b[0] = HI(y->h);
-	a[1] = LO(x->h); b[1] = LO(y->h);
-	a[2] = HI(x->l); b[2] = HI(y->l);
-	a[3] = LO(x->l); b[3] = LO(y->l);
-
-	c[6] =                               M(3, 3);
-	c[5] =                     M(2, 3) + M(3, 2) + SPILL(c[6]);
-	c[4] =           M(1, 3) + M(2, 2) + M(3, 1) + SPILL(c[5]);
-	c[3] = M(0, 3) + M(1, 2) + M(2, 1) + M(3, 0) + SPILL(c[4]);
-	c[2] = M(0, 2) + M(1, 1) + M(2, 0)           + SPILL(c[3]);
-	c[1] = M(0, 1) + M(1, 0)                     + SPILL(c[2]);
-	c[0] = M(0, 0)                               + SPILL(c[1]);
-
-	f[0] = c[0];
-	f[1] = C(c[1], c[2]);
-	f[2] = C(c[3], c[4]);
-	f[3] = C(c[5], c[6]);
-
-	if((f[0] & HiddenBit) == 0){
-		f[0] <<= 1;
-		f[1] <<= 1;
-		f[2] <<= 1;
-		f[3] <<= 1;
-		if(f[1] & CarryBit){
-			f[0] |= 1;
-			f[1] &= ~CarryBit;
-		}
-		if(f[2] & CarryBit){
-			f[1] |= 1;
-			f[2] &= ~CarryBit;
-		}
-		if(f[3] & CarryBit){
-			f[2] |= 1;
-			f[3] &= ~CarryBit;
-		}
-		i->e--;
-	}
-	i->h = f[0];
-	i->l = f[1];
-	if(f[2] || f[3])
-		i->l |= 1;
-	renormalise(i);
-}
-
-void
-fpidiv(Internal *x, Internal *y, Internal *i)
-{
-	i->s = x->s^y->s;
-	if(IsNaN(x) || IsNaN(y)
-	   || (IsInfinity(x) && IsInfinity(y)) || (IsZero(x) && IsZero(y))){
-		SetQNaN(i);
-		return;
-	}
-	else if(IsZero(x) || IsInfinity(y)){
-		SetInfinity(i);
-		return;
-	}
-	else if(IsInfinity(x) || IsZero(y)){
-		SetZero(i);
-		return;
-	}
-	normalise(x);
-	normalise(y);
-	i->h = 0;
-	i->l = 0;
-	i->e = y->e - x->e + (ExpBias + 2*FractBits - 1);
-	do{
-		if(y->h > x->h || (y->h == x->h && y->l >= x->l)){
-			i->l |= 0x01;
-			y->h -= x->h;
-			y->l -= x->l;
-			if(y->l < 0){
-				y->l += CarryBit;
-				y->h--;
-			}
-		}
-		shift(y);
-		shift(i);
-	}while ((i->h & HiddenBit) == 0);
-	if(y->h || y->l)
-		i->l |= 0x01;
-	renormalise(i);
-}
-
-int
-fpicmp(Internal *x, Internal *y)
-{
-	if(IsNaN(x) && IsNaN(y))
-		return 0;
-	if(IsInfinity(x) && IsInfinity(y))
-		return y->s - x->s;
-	if(x->e == y->e && x->h == y->h && x->l == y->l)
-		return y->s - x->s;
-	if(x->e < y->e
-	   || (x->e == y->e && (x->h < y->h || (x->h == y->h && x->l < y->l))))
-		return y->s ? 1: -1;
-	return x->s ? -1: 1;
-}

+ 0 - 61
sys/src/9/teg2/fpi.h

@@ -1,61 +0,0 @@
-typedef long Word;
-typedef unsigned long Single;
-typedef struct {
-	unsigned long l;
-	unsigned long h;
-} Double;
-
-enum {
-	FractBits	= 28,
-	CarryBit	= 0x10000000,
-	HiddenBit	= 0x08000000,
-	MsBit		= HiddenBit,
-	NGuardBits	= 3,
-	GuardMask	= 0x07,
-	LsBit		= (1<<NGuardBits),
-
-	SingleExpBias	= 127,
-	SingleExpMax	= 255,
-	DoubleExpBias	= 1023,
-	DoubleExpMax	= 2047,
-
-	ExpBias		= DoubleExpBias,
-	ExpInfinity	= DoubleExpMax,
-};
-
-typedef struct {
-	unsigned char s;
-	short e;
-	long l;				/* 0000FFFFFFFFFFFFFFFFFFFFFFFFFGGG */
-	long h;				/* 0000HFFFFFFFFFFFFFFFFFFFFFFFFFFF */
-} Internal;
-
-#define IsWeird(n)	((n)->e >= ExpInfinity)
-#define	IsInfinity(n)	(IsWeird(n) && (n)->h == HiddenBit && (n)->l == 0)
-#define	SetInfinity(n)	((n)->e = ExpInfinity, (n)->h = HiddenBit, (n)->l = 0)
-#define IsNaN(n)	(IsWeird(n) && (((n)->h & ~HiddenBit) || (n)->l))
-#define	SetQNaN(n)	((n)->s = 0, (n)->e = ExpInfinity, 		\
-			 (n)->h = HiddenBit|(LsBit<<1), (n)->l = 0)
-#define IsZero(n)	((n)->e == 1 && (n)->h == 0 && (n)->l == 0)
-#define SetZero(n)	((n)->e = 1, (n)->h = 0, (n)->l = 0)
-
-/*
- * fpi.c
- */
-extern void fpiround(Internal *);
-extern void fpiadd(Internal *, Internal *, Internal *);
-extern void fpisub(Internal *, Internal *, Internal *);
-extern void fpimul(Internal *, Internal *, Internal *);
-extern void fpidiv(Internal *, Internal *, Internal *);
-extern int fpicmp(Internal *, Internal *);
-extern void fpinormalise(Internal*);
-
-/*
- * fpimem.c
- */
-extern void fpis2i(Internal *, void *);
-extern void fpid2i(Internal *, void *);
-extern void fpiw2i(Internal *, void *);
-extern void fpii2s(void *, Internal *);
-extern void fpii2d(void *, Internal *);
-extern void fpii2w(Word *, Internal *);

+ 1 - 1
sys/src/9/teg2/fpiarm.c

@@ -13,7 +13,7 @@
 #include	"ureg.h"
 
 #include	"arm.h"
-#include	"fpi.h"
+#include	"../port/fpi.h"
 
 #define ARM7500			/* emulate old pre-VFP opcodes */
 

+ 0 - 136
sys/src/9/teg2/fpimem.c

@@ -1,136 +0,0 @@
-#include "fpi.h"
-
-/*
- * the following routines depend on memory format, not the machine
- */
-
-void
-fpis2i(Internal *i, void *v)
-{
-	Single *s = v;
-
-	i->s = (*s & 0x80000000) ? 1: 0;
-	if((*s & ~0x80000000) == 0){
-		SetZero(i);
-		return;
-	}
-	i->e = ((*s>>23) & 0x00FF) - SingleExpBias + ExpBias;
-	i->h = (*s & 0x007FFFFF)<<(1+NGuardBits);
-	i->l = 0;
-	if(i->e)
-		i->h |= HiddenBit;
-	else
-		i->e++;
-}
-
-void
-fpid2i(Internal *i, void *v)
-{
-	Double *d = v;
-
-	i->s = (d->h & 0x80000000) ? 1: 0;
-	i->e = (d->h>>20) & 0x07FF;
-	i->h = ((d->h & 0x000FFFFF)<<(4+NGuardBits))|((d->l>>25) & 0x7F);
-	i->l = (d->l & 0x01FFFFFF)<<NGuardBits;
-	if(i->e)
-		i->h |= HiddenBit;
-	else
-		i->e++;
-}
-
-void
-fpiw2i(Internal *i, void *v)
-{
-	Word w, word = *(Word*)v;
-	short e;
-
-	if(word < 0){
-		i->s = 1;
-		word = -word;
-	}
-	else
-		i->s = 0;
-	if(word == 0){
-		SetZero(i);
-		return;
-	}
-	if(word > 0){
-		for (e = 0, w = word; w; w >>= 1, e++)
-			;
-	} else
-		e = 32;
-	if(e > FractBits){
-		i->h = word>>(e - FractBits);
-		i->l = (word & ((1<<(e - FractBits)) - 1))<<(2*FractBits - e);
-	}
-	else {
-		i->h = word<<(FractBits - e);
-		i->l = 0;
-	}
-	i->e = (e - 1) + ExpBias;
-}
-
-void
-fpii2s(void *v, Internal *i)
-{
-	short e;
-	Single *s = (Single*)v;
-
-	fpiround(i);
-	if(i->h & HiddenBit)
-		i->h &= ~HiddenBit;
-	else
-		i->e--;
-	*s = i->s ? 0x80000000: 0;
-	e = i->e;
-	if(e < ExpBias){
-		if(e <= (ExpBias - SingleExpBias))
-			return;
-		e = SingleExpBias - (ExpBias - e);
-	}
-	else  if(e >= (ExpBias + (SingleExpMax-SingleExpBias))){
-		*s |= SingleExpMax<<23;
-		return;
-	}
-	else
-		e = SingleExpBias + (e - ExpBias);
-	*s |= (e<<23)|(i->h>>(1+NGuardBits));
-}
-
-void
-fpii2d(void *v, Internal *i)
-{
-	Double *d = (Double*)v;
-
-	fpiround(i);
-	if(i->h & HiddenBit)
-		i->h &= ~HiddenBit;
-	else
-		i->e--;
-	i->l = ((i->h & GuardMask)<<25)|(i->l>>NGuardBits);
-	i->h >>= NGuardBits;
-	d->h = i->s ? 0x80000000: 0;
-	d->h |= (i->e<<20)|((i->h & 0x00FFFFFF)>>4);
-	d->l = (i->h<<28)|i->l;
-}
-
-void
-fpii2w(Word *word, Internal *i)
-{
-	Word w;
-	short e;
-
-	fpiround(i);
-	e = (i->e - ExpBias) + 1;
-	if(e <= 0)
-		w = 0;
-	else if(e > 31)
-		w = 0x7FFFFFFF;
-	else if(e > FractBits)
-		w = (i->h<<(e - FractBits))|(i->l>>(2*FractBits - e));
-	else
-		w = i->h>>(FractBits-e);
-	if(i->s)
-		w = -w;
-	*word = w;
-}

+ 1 - 1
sys/src/9/teg2/mkfile

@@ -120,7 +120,7 @@ arch.$O clock.$O fpiarm.$O main.$O mmu.$O screen.$O sdscsi.$O syscall.$O \
 archtegra.$O devether.$0 ether9221.$O: etherif.h ../port/netif.h
 archtegra.$O devflash.$O flashtegra.$O flashigep.$O: ../port/flashif.h
 ecc.$O flashtegra.$O flashigep.$O: ../port/nandecc.h io.h
-fpi.$O fpiarm.$O fpimem.$O: fpi.h
+fpi.$O fpiarm.$O fpimem.$O: ../port/fpi.h
 l.$O lexception.$O lproc.$O mmu.$O: arm.s mem.h
 l.$O rebootcode.$O: cache.v7.s
 main.$O: errstr.h init.h reboot.h

+ 0 - 138
sys/src/9/teg2/random.c

@@ -1,138 +0,0 @@
-#include	"u.h"
-#include	"../port/lib.h"
-#include	"mem.h"
-#include	"dat.h"
-#include	"fns.h"
-#include	"../port/error.h"
-
-
-struct Rb
-{
-	QLock;
-	Rendez	producer;
-	Rendez	consumer;
-	ulong	randomcount;
-	uchar	buf[128];
-	uchar	*ep;
-	uchar	*rp;
-	uchar	*wp;
-	uchar	next;
-	uchar	wakeme;
-	ushort	bits;
-	ulong	randn;
-} rb;
-
-static int
-rbnotfull(void*)
-{
-	int i;
-
-	i = rb.rp - rb.wp;
-	return i != 1 && i != (1 - sizeof(rb.buf));
-}
-
-static int
-rbnotempty(void*)
-{
-	return rb.wp != rb.rp;
-}
-
-static void
-genrandom(void*)
-{
-	up->basepri = PriNormal;
-	up->priority = up->basepri;
-
-	for(;;){
-		for(;;)
-			if(++rb.randomcount > 100000)
-				break;
-		if(anyhigher())
-			sched();
-		if(!rbnotfull(0))
-			sleep(&rb.producer, rbnotfull, 0);
-	}
-}
-
-/*
- *  produce random bits in a circular buffer
- */
-static void
-randomclock(void)
-{
-	if(rb.randomcount == 0 || !rbnotfull(0))
-		return;
-
-	rb.bits = (rb.bits<<2) ^ rb.randomcount;
-	rb.randomcount = 0;
-
-	rb.next++;
-	if(rb.next != 8/2)
-		return;
-	rb.next = 0;
-
-	*rb.wp ^= rb.bits;
-	if(rb.wp+1 == rb.ep)
-		rb.wp = rb.buf;
-	else
-		rb.wp = rb.wp+1;
-
-	if(rb.wakeme)
-		wakeup(&rb.consumer);
-}
-
-void
-randominit(void)
-{
-	addclock0link(randomclock, 1000/HZ);
-	rb.ep = rb.buf + sizeof(rb.buf);
-	rb.rp = rb.wp = rb.buf;
-	kproc("genrandom", genrandom, 0);
-}
-
-/*
- *  consume random bytes from a circular buffer
- */
-ulong
-randomread(void *xp, ulong n)
-{
-	uchar *e, *p;
-	ulong x;
-
-	p = xp;
-
-	if(waserror()){
-		qunlock(&rb);
-		nexterror();
-	}
-
-	qlock(&rb);
-	for(e = p + n; p < e; ){
-		if(rb.wp == rb.rp){
-			rb.wakeme = 1;
-			wakeup(&rb.producer);
-			sleep(&rb.consumer, rbnotempty, 0);
-			rb.wakeme = 0;
-			continue;
-		}
-
-		/*
-		 *  beating clocks will be predictable if
-		 *  they are synchronized.  Use a cheap pseudo
-		 *  random number generator to obscure any cycles.
-		 */
-		x = rb.randn*1103515245 ^ *rb.rp;
-		*p++ = rb.randn = x;
-
-		if(rb.rp+1 == rb.ep)
-			rb.rp = rb.buf;
-		else
-			rb.rp = rb.rp+1;
-	}
-	qunlock(&rb);
-	poperror();
-
-	wakeup(&rb.producer);
-
-	return n;
-}