Update.
[glibc.git] / sysdeps / libm-ieee754 / s_roundtol.c
blobbc0ceae0f85baad2550a30d386cc04191a20ddd2
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. */
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 int
30 __roundtol (long double x)
32 int32_t j0;
33 u_int32_t i1, i0;
34 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 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 int) 0x80000 >> (20 - j0);
53 else
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
61 what happens. */
62 result = (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 int) <= 4)
77 /* Overflow. */
78 result = (long int) x;
79 else
80 result = 1l << (j0 + 1);
82 else
83 result = (long int) ((i0 & 0xfffff) | 0x100000) << (j0 - 31);
85 else
87 result = (long int) ((i0 & 0xfffff) | 0x100000) << (j0 - 31);
88 if (sizeof (long int) > 4 && j0 > 31)
89 result |= j >> (63 - j0);
92 else
94 result = (long int) ((i0 & 0xfffff) | 0x100000) << (j0 - 31);
95 if (sizeof (long int) > 4 && j0 > 31)
96 result |= i1 >> (63 - j0);
100 return i0 & 0x80000000 ? -result : result;
102 #else
103 long int
104 __roundtol (long double x)
106 int32_t j0;
107 u_int32_t se, i1, i0;
108 long int result;
110 GET_LDOUBLE_WORDS (se, i0, i1, x);
111 j0 = (se & 0x7fff) - 0x3fff;
112 if (j0 < 31)
114 if (j0 < 0)
115 result = j0 < -1 ? 0 : 1;
116 else
118 u_int32_t i = 0x7fffffff >> j0;
119 if (((i0 & i) | i1) == 0)
120 result = (long int) i0 >> j0;
121 else
123 /* X is not integral. */
124 u_int32_t j = i0 + (0x40000000 >> j0);
125 if (j < i0)
126 result = 0x80000000l >> (30 - j0);
127 else
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
135 what happens. */
136 result = (long int) x;
138 else
140 u_int32_t i = ((u_int32_t) (0xffffffff)) >> (j0 - 31);
141 if ((i1 & i) != 0)
143 /* x is not integral. */
144 u_int32_t j = i1 + (0x80000000 >> (j0 - 31));
145 if (j < i1)
147 j = i0 + 1;
148 if (j == 0)
150 if (sizeof (long int) <= 4)
151 /* Overflow. */
152 result = (long int) x;
153 else
154 result = 1l << (j0 + 1);
156 else
157 result = (long int) i0 << (j0 - 31);
159 else
161 result = (long int) i0 << (j0 - 31);
162 if (sizeof (long int) > 4 && j0 > 31)
163 result |= j >> (63 - j0);
166 else
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;
176 #endif
177 weak_alias (__roundtol, roundtol)