Category : C Source Code
Archive   : CEPHES22.ZIP
Filename : QFLOOR.C

 
Output of file : QFLOOR.C contained in archive : CEPHES22.ZIP

/* qfloor - largest integer not greater than x
* qround - nearest integer to x
*/

#include "mconf.h"
#include "qhead.h"
#ifdef DEC
#define void int
#endif

extern short qone[], qhalf[];

static short bmask[] = {
0xffff,
0xfffe,
0xfffc,
0xfff8,
0xfff0,
0xffe0,
0xffc0,
0xff80,
0xff00,
0xfe00,
0xfc00,
0xf800,
0xf000,
0xe000,
0xc000,
0x8000,
0x0000,
};


void qfloor( x, y )
short x[], y[];
{
short t[NQ];
register unsigned short *p;
short e, i;

qmov( x, t );
if( t[1] == 0 )
{
qclear( y );
return;
}
e = t[1] - 0x4000;

if( e <= 0 )
{
if( t[0] < 0 )
{
qmov( qone, y );
qneg( y );
}
else
{
qclear( y );
}
return;
}

/* number of bits to clear out */
e = NBITS - e;

qmov( t, y );
p = (unsigned short *)&y[NQ-1];

while( e >= 16 )
{
*p-- = 0;
e -= 16;
}

/* clear the remaining bits */
*p &= bmask[e];

/* truncate negatives toward minus infinity */
if( t[0] < 0 )
{
if( qcmp( t, y ) != 0 )
qsub( qone, y, y );
}
}





double qround(x, y)
short *x, *y;
{
short z[NQ], f[NQ];
int r;

qfloor( x, z );
qsub( z, x, f );
r = qcmp( f, qhalf );
if( r > 0 )
goto rndup;
if( r == 0 )
{
/* round to even */
z[1] -= 1;
qfloor( z, f );
z[1] += 1;
f[1] += 1;
if( qcmp(z,f) != 0 )
{
rndup: qadd( qone, z, z );
}
}
qmov( z, y );
}


  3 Responses to “Category : C Source Code
Archive   : CEPHES22.ZIP
Filename : QFLOOR.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/