* sysdeps/ieee754/ldbl-128ibm/k_cosl.c (__kernel_cosl): Correct index
[glibc.git] / sysdeps / ieee754 / ldbl-128ibm / s_floorl.c
blob2be5e28698da9da547ba2fcd7ec6d76d7693afb4
1 /* Round to int long double floating-point values.
2 IBM extended format long double version.
3 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <math.h>
22 #include <fenv.h>
23 #include <math_ldbl_opt.h>
24 #include <float.h>
25 #include <ieee754.h>
28 #ifdef __STDC__
29 long double
30 __floorl (long double x)
31 #else
32 long double
33 __floorl (x)
34 long double x;
35 #endif
37 static const double TWO52 = 4503599627370496.0L;
38 int mode = fegetround();
39 union ibm_extended_long_double u;
41 u.d = x;
43 if (fabs (u.dd[0]) < TWO52)
45 double high = u.dd[0];
46 fesetround(FE_DOWNWARD);
47 if (high > 0.0)
49 high += TWO52;
50 high -= TWO52;
51 if (high == -0.0) high = 0.0;
53 else if (high < 0.0)
55 high -= TWO52;
56 high += TWO52;
57 if (high == 0.0) high = -0.0;
59 u.dd[0] = high;
60 u.dd[1] = 0.0;
61 fesetround(mode);
63 else if (fabs (u.dd[1]) < TWO52 && u.dd[1] != 0.0)
65 double high, low;
66 /* In this case we have to round the low double and handle any
67 adjustment to the high double that may be caused by rounding
68 (up). This is complicated by the fact that the high double
69 may already be rounded and the low double may have the
70 opposite sign to compensate. */
71 if (u.dd[0] > 0.0)
73 if (u.dd[1] > 0.0)
75 /* If the high/low doubles are the same sign then simply
76 round the low double. */
77 high = u.dd[0];
78 low = u.dd[1];
80 else if (u.dd[1] < 0.0)
82 /* Else the high double is pre rounded and we need to
83 adjust for that. */
84 high = nextafter (u.dd[0], 0.0);
85 low = u.dd[1] + (u.dd[0] - high);
87 fesetround(FE_DOWNWARD);
88 low += TWO52;
89 low -= TWO52;
91 else if (u.dd[0] < 0.0)
93 if (u.dd[1] < 0.0)
95 /* If the high/low doubles are the same sign then simply
96 round the low double. */
97 high = u.dd[0];
98 low = u.dd[1];
100 else if (u.dd[1] > 0.0)
102 /* Else the high double is pre rounded and we need to
103 adjust for that. */
104 high = nextafter (u.dd[0], 0.0);
105 low = u.dd[1] + (u.dd[0] - high);
107 fesetround(FE_DOWNWARD);
108 low -= TWO52;
109 low += TWO52;
111 fesetround(mode);
112 u.dd[0] = high + low;
113 u.dd[1] = high - u.dd[0] + low;
116 return u.d;
119 long_double_symbol (libm, __floorl, floorl);