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. */
23 #include "math_private.h"
27 /* The `long double' is in fact the IEEE `double' type. */
30 __roundtoll (long double x
)
36 EXTRACT_WORDS (i0
, i1
, x
);
37 j0
= ((i0
>> 20) & 0x7ff) - 0x3ff;
41 result
= j0
< -1 ? 0 : ((i0
& 0x80000000) ? -1 : 1);
44 u_int32_t i
= 0xfffff >> j0
;
45 if (((i0
& i
) | i1
) == 0)
46 result
= (long long int) ((i0
& 0xfffff) | 0x100000) >> j0
;
49 /* X is not integral. */
50 u_int32_t j
= i0
+ (0x80000 >> j0
);
52 result
= (long long int) 0x80000 >> (20 - j0
);
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
62 result
= (long long int) x
;
66 u_int32_t i
= ((u_int32_t
) (0xffffffff)) >> (j0
- 20);
69 /* x is not integral. */
70 u_int32_t j
= i1
+ (0x80000000 >> (j0
- 20));
74 if ((j
& 0xfffff) == 0)
76 if (sizeof (long long int) <= 4)
78 result
= (long long int) x
;
80 result
= 1ll << (j0
+ 1);
83 result
= ((long long int) ((i0
& 0xfffff) | 0x100000)
88 result
= ((long long int) ((i0
& 0xfffff) | 0x100000)
90 if (sizeof (long long int) > 4 && j0
> 31)
91 result
|= j
>> (63 - j0
);
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
;
106 __roundtoll (long double x
)
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;
117 result
= j0
< -1 ? 0 : 1;
120 u_int32_t i
= 0x7fffffff >> j0
;
121 if (((i0
& i
) | i1
) == 0)
122 result
= (long long int) i0
>> j0
;
125 /* X is not integral. */
126 u_int32_t j
= i0
+ (0x40000000 >> j0
);
128 result
= 0x80000000l
>> (30 - j0
);
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
138 result
= (long long int) x
;
142 u_int32_t i
= ((u_int32_t
) (0xffffffff)) >> (j0
- 31);
145 /* x is not integral. */
146 u_int32_t j
= i1
+ (0x80000000 >> (j0
- 31));
152 if (sizeof (long long int) <= 4)
154 result
= (long long int) x
;
156 result
= 1ll << (j0
+ 1);
159 result
= (long long int) i0
<< (j0
- 31);
163 result
= (long long int) i0
<< (j0
- 31);
164 if (sizeof (long long int) > 4 && j0
> 31)
165 result
|= j
>> (63 - j0
);
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
;
179 weak_alias (__roundtoll
, roundtoll
)