xref: /minix3/lib/libm/src/s_floor.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
12fe8fb19SBen Gras /* @(#)s_floor.c 5.1 93/09/24 */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras  * ====================================================
42fe8fb19SBen Gras  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
52fe8fb19SBen Gras  *
62fe8fb19SBen Gras  * Developed at SunPro, a Sun Microsystems, Inc. business.
72fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this
82fe8fb19SBen Gras  * software is freely granted, provided that this notice
92fe8fb19SBen Gras  * is preserved.
102fe8fb19SBen Gras  * ====================================================
112fe8fb19SBen Gras  */
122fe8fb19SBen Gras 
132fe8fb19SBen Gras #include <sys/cdefs.h>
142fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
15*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_floor.c,v 1.14 2013/11/11 23:57:34 joerg Exp $");
162fe8fb19SBen Gras #endif
172fe8fb19SBen Gras 
182fe8fb19SBen Gras /*
192fe8fb19SBen Gras  * floor(x)
202fe8fb19SBen Gras  * Return x rounded toward -inf to integral value
212fe8fb19SBen Gras  * Method:
222fe8fb19SBen Gras  *	Bit twiddling.
232fe8fb19SBen Gras  * Exception:
242fe8fb19SBen Gras  *	Inexact flag raised if x not equal to floor(x).
252fe8fb19SBen Gras  */
262fe8fb19SBen Gras 
272fe8fb19SBen Gras #include "math.h"
282fe8fb19SBen Gras #include "math_private.h"
292fe8fb19SBen Gras 
30*84d9c625SLionel Sambuc #ifndef __HAVE_LONG_DOUBLE
31*84d9c625SLionel Sambuc __strong_alias(_floorl, floor)
32*84d9c625SLionel Sambuc __weak_alias(floorl, floor)
33*84d9c625SLionel Sambuc #endif
34*84d9c625SLionel Sambuc 
352fe8fb19SBen Gras static const double huge = 1.0e300;
362fe8fb19SBen Gras 
372fe8fb19SBen Gras double
floor(double x)382fe8fb19SBen Gras floor(double x)
392fe8fb19SBen Gras {
402fe8fb19SBen Gras 	int32_t i0,i1,jj0;
412fe8fb19SBen Gras 	u_int32_t i,j;
422fe8fb19SBen Gras 	EXTRACT_WORDS(i0,i1,x);
432fe8fb19SBen Gras 	jj0 = ((i0>>20)&0x7ff)-0x3ff;
442fe8fb19SBen Gras 	if(jj0<20) {
452fe8fb19SBen Gras 	    if(jj0<0) { 	/* raise inexact if x != 0 */
462fe8fb19SBen Gras 		if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
472fe8fb19SBen Gras 		    if(i0>=0) {i0=i1=0;}
482fe8fb19SBen Gras 		    else if(((i0&0x7fffffff)|i1)!=0)
492fe8fb19SBen Gras 			{ i0=0xbff00000;i1=0;}
502fe8fb19SBen Gras 		}
512fe8fb19SBen Gras 	    } else {
522fe8fb19SBen Gras 		i = (0x000fffff)>>jj0;
532fe8fb19SBen Gras 		if(((i0&i)|i1)==0) return x; /* x is integral */
542fe8fb19SBen Gras 		if(huge+x>0.0) {	/* raise inexact flag */
552fe8fb19SBen Gras 		    if(i0<0) i0 += (0x00100000)>>jj0;
562fe8fb19SBen Gras 		    i0 &= (~i); i1=0;
572fe8fb19SBen Gras 		}
582fe8fb19SBen Gras 	    }
592fe8fb19SBen Gras 	} else if (jj0>51) {
602fe8fb19SBen Gras 	    if(jj0==0x400) return x+x;	/* inf or NaN */
612fe8fb19SBen Gras 	    else return x;		/* x is integral */
622fe8fb19SBen Gras 	} else {
632fe8fb19SBen Gras 	    i = ((u_int32_t)(0xffffffff))>>(jj0-20);
642fe8fb19SBen Gras 	    if((i1&i)==0) return x;	/* x is integral */
652fe8fb19SBen Gras 	    if(huge+x>0.0) { 		/* raise inexact flag */
662fe8fb19SBen Gras 		if(i0<0) {
672fe8fb19SBen Gras 		    if(jj0==20) i0+=1;
682fe8fb19SBen Gras 		    else {
692fe8fb19SBen Gras 			j = i1+(1<<(52-jj0));
702fe8fb19SBen Gras 			if(j<(u_int32_t)i1) i0 +=1 ; 	/* got a carry */
712fe8fb19SBen Gras 			i1=j;
722fe8fb19SBen Gras 		    }
732fe8fb19SBen Gras 		}
742fe8fb19SBen Gras 		i1 &= (~i);
752fe8fb19SBen Gras 	    }
762fe8fb19SBen Gras 	}
772fe8fb19SBen Gras 	INSERT_WORDS(x,i0,i1);
782fe8fb19SBen Gras 	return x;
792fe8fb19SBen Gras }
80