13a8617a8SJordan K. Hubbard /*
23a8617a8SJordan K. Hubbard * ====================================================
33a8617a8SJordan K. Hubbard * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
43a8617a8SJordan K. Hubbard *
53a8617a8SJordan K. Hubbard * Developed at SunPro, a Sun Microsystems, Inc. business.
63a8617a8SJordan K. Hubbard * Permission to use, copy, modify, and distribute this
73a8617a8SJordan K. Hubbard * software is freely granted, provided that this notice
83a8617a8SJordan K. Hubbard * is preserved.
93a8617a8SJordan K. Hubbard * ====================================================
103a8617a8SJordan K. Hubbard */
113a8617a8SJordan K. Hubbard
123a8617a8SJordan K. Hubbard /*
133a8617a8SJordan K. Hubbard * floor(x)
143a8617a8SJordan K. Hubbard * Return x rounded toward -inf to integral value
153a8617a8SJordan K. Hubbard * Method:
163a8617a8SJordan K. Hubbard * Bit twiddling.
173a8617a8SJordan K. Hubbard * Exception:
183a8617a8SJordan K. Hubbard * Inexact flag raised if x not equal to floor(x).
193a8617a8SJordan K. Hubbard */
203a8617a8SJordan K. Hubbard
2163b4a1f8SBruce Evans #include <float.h>
2263b4a1f8SBruce Evans
233a8617a8SJordan K. Hubbard #include "math.h"
243a8617a8SJordan K. Hubbard #include "math_private.h"
253a8617a8SJordan K. Hubbard
263a8617a8SJordan K. Hubbard static const double huge = 1.0e300;
273a8617a8SJordan K. Hubbard
2859b19ff1SAlfred Perlstein double
floor(double x)293819e840SPeter Wemm floor(double x)
303a8617a8SJordan K. Hubbard {
313a8617a8SJordan K. Hubbard int32_t i0,i1,j0;
323a8617a8SJordan K. Hubbard u_int32_t i,j;
333a8617a8SJordan K. Hubbard EXTRACT_WORDS(i0,i1,x);
343a8617a8SJordan K. Hubbard j0 = ((i0>>20)&0x7ff)-0x3ff;
353a8617a8SJordan K. Hubbard if(j0<20) {
363a8617a8SJordan K. Hubbard if(j0<0) { /* raise inexact if x != 0 */
373a8617a8SJordan K. Hubbard if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
383a8617a8SJordan K. Hubbard if(i0>=0) {i0=i1=0;}
393a8617a8SJordan K. Hubbard else if(((i0&0x7fffffff)|i1)!=0)
403a8617a8SJordan K. Hubbard { i0=0xbff00000;i1=0;}
413a8617a8SJordan K. Hubbard }
423a8617a8SJordan K. Hubbard } else {
433a8617a8SJordan K. Hubbard i = (0x000fffff)>>j0;
443a8617a8SJordan K. Hubbard if(((i0&i)|i1)==0) return x; /* x is integral */
453a8617a8SJordan K. Hubbard if(huge+x>0.0) { /* raise inexact flag */
463a8617a8SJordan K. Hubbard if(i0<0) i0 += (0x00100000)>>j0;
473a8617a8SJordan K. Hubbard i0 &= (~i); i1=0;
483a8617a8SJordan K. Hubbard }
493a8617a8SJordan K. Hubbard }
503a8617a8SJordan K. Hubbard } else if (j0>51) {
513a8617a8SJordan K. Hubbard if(j0==0x400) return x+x; /* inf or NaN */
523a8617a8SJordan K. Hubbard else return x; /* x is integral */
533a8617a8SJordan K. Hubbard } else {
543a8617a8SJordan K. Hubbard i = ((u_int32_t)(0xffffffff))>>(j0-20);
553a8617a8SJordan K. Hubbard if((i1&i)==0) return x; /* x is integral */
563a8617a8SJordan K. Hubbard if(huge+x>0.0) { /* raise inexact flag */
573a8617a8SJordan K. Hubbard if(i0<0) {
583a8617a8SJordan K. Hubbard if(j0==20) i0+=1;
593a8617a8SJordan K. Hubbard else {
603a8617a8SJordan K. Hubbard j = i1+(1<<(52-j0));
613a8617a8SJordan K. Hubbard if(j<i1) i0 +=1 ; /* got a carry */
623a8617a8SJordan K. Hubbard i1=j;
633a8617a8SJordan K. Hubbard }
643a8617a8SJordan K. Hubbard }
653a8617a8SJordan K. Hubbard i1 &= (~i);
663a8617a8SJordan K. Hubbard }
673a8617a8SJordan K. Hubbard }
683a8617a8SJordan K. Hubbard INSERT_WORDS(x,i0,i1);
693a8617a8SJordan K. Hubbard return x;
703a8617a8SJordan K. Hubbard }
715014f8deSBruce Evans
725014f8deSBruce Evans #if LDBL_MANT_DIG == 53
735014f8deSBruce Evans __weak_reference(floor, floorl);
745014f8deSBruce Evans #endif
75