Update.
[glibc.git] / sysdeps / libm-ieee754 / s_roundtoll.c
blob49167236a6bd9b6036fc55939f09e8fd54184611
1 /* Round long double value to long long int.
2 Copyright (C) 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <math.h>
23 #include "math_private.h"
26 #ifdef NO_LONG_DOUBLE
27 /* The `long double' is in fact the IEEE `double' type. */
29 long long int
30 __roundtoll (long double x)
32 int32_t j0;
33 u_int32_t i1, i0;
34 long long int result;
36 EXTRACT_WORDS (i0, i1, x);
37 j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
38 if (j0 < 20)
40 if (j0 < 0)
41 result = j0 < -1 ? 0 : ((i0 & 0x80000000) ? -1 : 1);
42 else
44 u_int32_t i = 0xfffff >> j0;
45 if (((i0 & i) | i1) == 0)
46 result = (long long int) ((i0 & 0xfffff) | 0x100000) >> j0;
47 else
49 /* X is not integral. */
50 u_int32_t j = i0 + (0x80000 >> j0);
51 if (j < i0)
52 result = (long long int) 0x80000 >> (20 - j0);
53 else
54 result = (j | 0x100000) >> (20 - j0);
58 else if (j0 >= 8 * sizeof (long long int) || j0 > 51)
60 /* The number is too large. It is left implementation defined
61 what happens. */
62 result = (long long int) x;
64 else
66 u_int32_t i = ((u_int32_t) (0xffffffff)) >> (j0 - 20);
67 if ((i1 & i) != 0)
69 /* x is not integral. */
70 u_int32_t j = i1 + (0x80000000 >> (j0 - 20));
71 if (j < i1)
73 j = i0 + 1;
74 if ((j & 0xfffff) == 0)
76 if (sizeof (long long int) <= 4)
77 /* Overflow. */
78 result = (long long int) x;
79 else
80 result = 1ll << (j0 + 1);
82 else
83 result = ((long long int) ((i0 & 0xfffff) | 0x100000)
84 << (j0 - 31));
86 else
88 result = ((long long int) ((i0 & 0xfffff) | 0x100000)
89 << (j0 - 31));
90 if (sizeof (long long int) > 4 && j0 > 31)
91 result |= j >> (63 - j0);
94 else
96 result = (long long int) ((i0 & 0xfffff) | 0x100000) << (j0 - 31);
97 if (sizeof (long long int) > 4 && j0 > 31)
98 result |= i1 >> (63 - j0);
102 return i0 & 0x80000000 ? -result : result;
104 #else
105 long long int
106 __roundtoll (long double x)
108 int32_t j0;
109 u_int32_t se, i1, i0;
110 long long int result;
112 GET_LDOUBLE_WORDS (se, i0, i1, x);
113 j0 = (se & 0x7fff) - 0x3fff;
114 if (j0 < 31)
116 if (j0 < 0)
117 result = j0 < -1 ? 0 : 1;
118 else
120 u_int32_t i = 0x7fffffff >> j0;
121 if (((i0 & i) | i1) == 0)
122 result = (long long int) i0 >> j0;
123 else
125 /* X is not integral. */
126 u_int32_t j = i0 + (0x40000000 >> j0);
127 if (j < i0)
128 result = 0x80000000l >> (30 - j0);
129 else
130 result = j >> (31 - j0);
134 else if ((unsigned int) j0 >= 8 * sizeof (long long int) || j0 > 62)
136 /* The number is too large. It is left implementation defined
137 what happens. */
138 result = (long long int) x;
140 else
142 u_int32_t i = ((u_int32_t) (0xffffffff)) >> (j0 - 31);
143 if ((i1 & i) != 0)
145 /* x is not integral. */
146 u_int32_t j = i1 + (0x80000000 >> (j0 - 31));
147 if (j < i1)
149 j = i0 + 1;
150 if (j == 0)
152 if (sizeof (long long int) <= 4)
153 /* Overflow. */
154 result = (long long int) x;
155 else
156 result = 1ll << (j0 + 1);
158 else
159 result = (long long int) i0 << (j0 - 31);
161 else
163 result = (long long int) i0 << (j0 - 31);
164 if (sizeof (long long int) > 4 && j0 > 31)
165 result |= j >> (63 - j0);
168 else
170 result = (long long int) i0 << (j0 - 31);
171 if (sizeof (long long int) > 4 && j0 > 31)
172 result |= i1 >> (63 - j0);
176 return se & 0x8000 ? -result : result;
178 #endif
179 weak_alias (__roundtoll, roundtoll)