--- ray/src/common/portio.c 1996/11/15 16:20:35 2.5 +++ ray/src/common/portio.c 2006/12/23 17:27:45 2.14 @@ -1,23 +1,27 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: portio.c,v 2.14 2006/12/23 17:27:45 greg Exp $"; #endif - /* * Portable i/o for binary files + * + * External symbols declared in rtio.h */ -#include +#include "copyright.h" -#ifndef frexp -extern double frexp(); +#include "rtio.h" + +#include + +#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ +#undef getc +#undef putc +#define getc getc_unlocked +#define putc putc_unlocked #endif -#ifndef ldexp -extern double ldexp(); -#endif +void putstr(s, fp) /* write null-terminated string to fp */ register char *s; register FILE *fp; @@ -28,6 +32,7 @@ register FILE *fp; } +void putint(i, siz, fp) /* write a siz-byte integer to fp */ long i; register int siz; @@ -38,13 +43,23 @@ register FILE *fp; } +void putflt(f, fp) /* put out floating point number */ double f; FILE *fp; { + long m; int e; - putint((long)(frexp(f,&e)*0x7fffffff), 4, fp); + m = frexp(f, &e) * 0x7fffffff; + if (e > 127) { /* overflow */ + m = m > 0 ? (long)0x7fffffff : -(long)0x7fffffff; + e = 127; + } else if (e < -128) { /* underflow */ + m = 0; + e = 0; + } + putint(m, 4, fp); putint((long)e, 1, fp); } @@ -95,6 +110,10 @@ FILE *fp; double d; l = getint(4, fp); + if (l == 0) { + getc(fp); /* exactly zero -- ignore exponent */ + return(0.0); + } d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff); return(ldexp(d, (int)getint(1, fp))); }