Plan 9 from Bell Labs’s /usr/web/sources/contrib/tristan/root/sys/src/cmd/geo/shp/fn.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <fcall.h>

short
lgets(uchar *pt){
	short r;
	r =pt[0];
	r|=pt[1]<<8;
	return r;
}

long
bgetl(uchar *pt){
	long r;
	r =pt[0]<<24;
	r|=pt[1]<<16;
	r|=pt[2]<<8;
	r|=pt[3];
	return r;
}

long
lgetl(uchar *pt){
	long r;
	r =pt[0];
	r|=pt[1]<<8;
	r|=pt[2]<<16;
	r|=pt[3]<<24;
	return r;
}

vlong
bgetvl(uchar *pt){
	vlong r;
	r =pt[7];
	r|=pt[6]<<8;
	r|=pt[5]<<16;
	r|=pt[4]<<24;
	r|=(vlong)pt[3]<<32;
	r|=(vlong)pt[2]<<40;
	r|=(vlong)pt[1]<<48;
	r|=(vlong)pt[0]<<56;
	return r;
}

vlong
lgetvl(uchar *pt){
	vlong r;
	r =pt[0];
	r|=pt[1]<<8;
	r|=pt[2]<<16;
	r|=pt[3]<<24;
	r|=(vlong)pt[4]<<32;
	r|=(vlong)pt[5]<<40;
	r|=(vlong)pt[6]<<48;
	r|=(vlong)pt[7]<<56;
	return r;
}

#define EXP(a) ((a[7]&0x7f)<<4|(a[6]&0xf0)>>4)
#define SIGN(a) (a[7]&0x80?-1:1)
double
lgetd(uchar *pt){
	double r;
	if(EXP(pt)==0x7ff){	/* Inf or NaN */
		if(pt[0]|pt[1]|pt[2]|pt[3]|pt[4]|pt[5]|(pt[6]&0xf)) { /* NaN */
			/* how to produce a quiet NaN? */
			r = NaN();
		}else{
			r = Inf(SIGN(pt));
		}
	} else if(EXP(pt)==0){
		/* how to produce subnormal numbers? */
		r = SIGN(pt) * 0.;
	} else {
		r=(((((((pt[0]/256. + pt[1])/256. + pt[2])/256. + pt[3])/256. + pt[4])/256. + pt[5])/256.) + (pt[6]&0xf))/16. + 1.;
		r *= pow(2., EXP(pt)-1023.);
		r *= SIGN(pt);
	}
	return r;
}
#undef EXP
#undef SIGN

uvlong
fsize(int fd) {
	uchar buf[256]; /* probably big enough */
	int r;
	Dir d;

	r = fstat(fd,buf,sizeof(buf));
	r = convM2D(buf, r, &d, nil);
	if(r<BIT16SZ) return 0;	/* BAD! */
	return d.length;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].