xref: /netbsd-src/lib/libm/src/s_rintf.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /* s_rintf.c -- float version of s_rint.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4 
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 #include <sys/cdefs.h>
17 #if defined(LIBM_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: s_rintf.c,v 1.5 1997/10/09 11:33:13 lukem Exp $");
19 #endif
20 
21 #include "math.h"
22 #include "math_private.h"
23 
24 #ifdef __STDC__
25 static const float
26 #else
27 static float
28 #endif
29 TWO23[2]={
30   8.3886080000e+06, /* 0x4b000000 */
31  -8.3886080000e+06, /* 0xcb000000 */
32 };
33 
34 #ifdef __STDC__
35 	float rintf(float x)
36 #else
37 	float rintf(x)
38 	float x;
39 #endif
40 {
41 	int32_t i0,j0,sx;
42 	u_int32_t i,i1;
43 	float w,t;
44 	GET_FLOAT_WORD(i0,x);
45 	sx = (i0>>31)&1;
46 	j0 = ((i0>>23)&0xff)-0x7f;
47 	if(j0<23) {
48 	    if(j0<0) {
49 		if((i0&0x7fffffff)==0) return x;
50 		i1 = (i0&0x07fffff);
51 		i0 &= 0xfff00000;
52 		i0 |= ((i1|-i1)>>9)&0x400000;
53 		SET_FLOAT_WORD(x,i0);
54 	        w = TWO23[sx]+x;
55 	        t =  w-TWO23[sx];
56 		GET_FLOAT_WORD(i0,t);
57 		SET_FLOAT_WORD(t,(i0&0x7fffffff)|(sx<<31));
58 	        return t;
59 	    } else {
60 		i = (0x007fffff)>>j0;
61 		if((i0&i)==0) return x; /* x is integral */
62 		i>>=1;
63 		if((i0&i)!=0) i0 = (i0&(~i))|((0x100000)>>j0);
64 	    }
65 	} else {
66 	    if(j0==0x80) return x+x;	/* inf or NaN */
67 	    else return x;		/* x is integral */
68 	}
69 	SET_FLOAT_WORD(x,i0);
70 	w = TWO23[sx]+x;
71 	return w-TWO23[sx];
72 }
73