149393c00Smartynas /* @(#)s_floor.c 5.1 93/09/24 */
249393c00Smartynas /*
349393c00Smartynas * ====================================================
449393c00Smartynas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
549393c00Smartynas *
649393c00Smartynas * Developed at SunPro, a Sun Microsystems, Inc. business.
749393c00Smartynas * Permission to use, copy, modify, and distribute this
849393c00Smartynas * software is freely granted, provided that this notice
949393c00Smartynas * is preserved.
1049393c00Smartynas * ====================================================
1149393c00Smartynas */
1249393c00Smartynas
1349393c00Smartynas /*
1449393c00Smartynas * floorl(x)
1549393c00Smartynas * Return x rounded toward -inf to integral value
1649393c00Smartynas * Method:
1749393c00Smartynas * Bit twiddling.
1849393c00Smartynas * Exception:
1949393c00Smartynas * Inexact flag raised if x not equal to floor(x).
2049393c00Smartynas */
2149393c00Smartynas
2249393c00Smartynas #include <math.h>
2349393c00Smartynas
2449393c00Smartynas #include "math_private.h"
2549393c00Smartynas
2649393c00Smartynas static const long double huge = 1.0e4930L;
2749393c00Smartynas
2849393c00Smartynas long double
floorl(long double x)2949393c00Smartynas floorl(long double x)
3049393c00Smartynas {
3149393c00Smartynas int64_t i0,i1,jj0;
3249393c00Smartynas u_int64_t i,j;
3349393c00Smartynas GET_LDOUBLE_WORDS64(i0,i1,x);
3449393c00Smartynas jj0 = ((i0>>48)&0x7fff)-0x3fff;
3549393c00Smartynas if(jj0<48) {
3649393c00Smartynas if(jj0<0) { /* raise inexact if x != 0 */
3700ee142fSmartynas if(huge+x>0.0) {
3800ee142fSmartynas if(i0>=0)
3900ee142fSmartynas return 0.0L;
4049393c00Smartynas else if(((i0&0x7fffffffffffffffLL)|i1)!=0)
4100ee142fSmartynas return -1.0L;
4249393c00Smartynas }
4349393c00Smartynas } else {
4449393c00Smartynas i = (0x0000ffffffffffffULL)>>jj0;
4549393c00Smartynas if(((i0&i)|i1)==0) return x; /* x is integral */
4649393c00Smartynas if(huge+x>0.0) { /* raise inexact flag */
4749393c00Smartynas if(i0<0) i0 += (0x0001000000000000LL)>>jj0;
4849393c00Smartynas i0 &= (~i); i1=0;
4949393c00Smartynas }
5049393c00Smartynas }
5149393c00Smartynas } else if (jj0>111) {
5249393c00Smartynas if(jj0==0x4000) return x+x; /* inf or NaN */
5349393c00Smartynas else return x; /* x is integral */
5449393c00Smartynas } else {
5549393c00Smartynas i = -1ULL>>(jj0-48);
5649393c00Smartynas if((i1&i)==0) return x; /* x is integral */
5749393c00Smartynas if(huge+x>0.0) { /* raise inexact flag */
5849393c00Smartynas if(i0<0) {
5949393c00Smartynas if(jj0==48) i0+=1;
6049393c00Smartynas else {
6149393c00Smartynas j = i1+(1LL<<(112-jj0));
6249393c00Smartynas if(j<i1) i0 +=1 ; /* got a carry */
6349393c00Smartynas i1=j;
6449393c00Smartynas }
6549393c00Smartynas }
6649393c00Smartynas i1 &= (~i);
6749393c00Smartynas }
6849393c00Smartynas }
6949393c00Smartynas SET_LDOUBLE_WORDS64(x,i0,i1);
7049393c00Smartynas return x;
7149393c00Smartynas }
72*2f2c0062Sguenther DEF_STD(floorl);
73