1 /* Round long double value to 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 __roundtol (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 int) ((i0
& 0xfffff) | 0x100000) >> j0
;
49 /* X is not integral. */
50 u_int32_t j
= i0
+ (0x80000 >> j0
);
52 result
= (long int) 0x80000 >> (20 - j0
);
54 result
= (j
| 0x100000) >> (20 - j0
);
58 else if (j0
>= 8 * sizeof (long int) || j0
> 51)
60 /* The number is too large. It is left implementation defined
62 result
= (long int) x
;
66 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 int) <= 4)
78 result
= (long int) x
;
80 result
= 1l << (j0
+ 1);
83 result
= (long int) ((i0
& 0xfffff) | 0x100000) << (j0
- 31);
87 result
= (long int) ((i0
& 0xfffff) | 0x100000) << (j0
- 31);
88 if (sizeof (long int) > 4 && j0
> 31)
89 result
|= j
>> (63 - j0
);
94 result
= (long int) ((i0
& 0xfffff) | 0x100000) << (j0
- 31);
95 if (sizeof (long int) > 4 && j0
> 31)
96 result
|= j
>> (63 - j0
);
100 return i0
& 0x80000000 ? -result
: result
;
104 __roundtol (long double x
)
107 u_int32_t se
, i1
, i0
;
110 GET_LDOUBLE_WORDS (se
, i0
, i1
, x
);
111 j0
= (se
& 0x7fff) - 0x3fff;
115 result
= j0
< -1 ? 0 : 1;
118 u_int32_t i
= 0x7fffffff >> j0
;
119 if (((i0
& i
) | i1
) == 0)
120 result
= (long int) i0
>> j0
;
123 /* X is not integral. */
124 u_int32_t j
= i0
+ (0x40000000 >> j0
);
126 result
= 0x80000000l
>> (30 - j0
);
128 result
= j
>> (31 - j0
);
132 else if ((unsigned int) j0
>= 8 * sizeof (long int) || j0
> 62)
134 /* The number is too large. It is left implementation defined
136 result
= (long int) x
;
140 u_int32_t i
= ((u_int32_t
) (0xffffffff)) >> (j0
- 31);
143 /* x is not integral. */
144 u_int32_t j
= i1
+ (0x80000000 >> (j0
- 31));
150 if (sizeof (long int) <= 4)
152 result
= (long int) x
;
154 result
= 1l << (j0
+ 1);
157 result
= (long int) i0
<< (j0
- 31);
161 result
= (long int) i0
<< (j0
- 31);
162 if (sizeof (long int) > 4 && j0
> 31)
163 result
|= j
>> (63 - j0
);
168 result
= (long int) i0
<< (j0
- 31);
169 if (sizeof (long int) > 4 && j0
> 31)
170 result
|= i1
>> (63 - j0
);
174 return se
& 0x8000 ? -result
: result
;
177 weak_alias (__roundtol
, roundtol
)