summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/gen/_ftoll.c
blob: 66d71f566c576ab321cc407ed7cd620c5ad45a7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include "lint.h"
#include <sys/isa_defs.h>
#include <floatingpoint.h>
#include <limits.h>
#include "libc.h"

/*
 * Ensure that this "portable" code is only used on big-endian ISAs
 */
#if !defined(_BIG_ENDIAN) || defined(_LITTLE_ENDIAN)
#error	"big-endian only!"
#endif

/*
 * Convert a double precision floating point number into a 64-bit int.
 */
long long
__dtoll(double dval)
{
	int i0, i1;		/* bitslam */
	int exp;		/* exponent */
	int m0;			/* most significant word of mantissa */
	unsigned int m1;	/* least sig. word of mantissa */
	unsigned int _fp_current_exceptions = 0;
	union {
		int i[2];
		double d;
	} u;

	/*
	 * Extract the exponent and check boundary conditions.
	 * Notice that the exponent is equal to the bit number where
	 * we want the most significant bit to live.
	 */
	u.d = dval;
	i0 = u.i[0];
	i1 = u.i[1];

	exp = ((i0 >> 20) & 0x7ff) - 0x3ff;
	if (exp < 0) {
		return ((long long)0); /* abs(x) < 1.0, so round to 0 */
	} else if (exp > 62) {
		/*
		 * fp_invalid NOT raised if <i0,i1> == LLONG_MIN
		 */
		if (i0 >= 0 || exp != 63 || (i0 & 0xfffff) != 0 || i1 != 0) {
			/*
			 * abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
			 * overflow, Inf, NaN set fp_invalid exception
			 */
			_fp_current_exceptions |= (1 << (int)fp_invalid);
			(void) _Q_set_exception(_fp_current_exceptions);
		}
		if (i0 < 0)
			return (LLONG_MIN);
		else
			return (LLONG_MAX); /* MAXLONG */
	}

	/* Extract the mantissa. */

	m0 = 0x40000000 | ((i0 << 10) & 0x3ffffc00) | ((i1 >> 22) & 0x3ff);
	m1 = i1 << 10;

	/*
	 * The most significant bit of the mantissa is now in bit 62 of m0:m1.
	 * Shift right by (62 - exp) bits.
	 */
	switch (exp) {
	case 62:
		break;
	case 30:
		m1 = m0;
		m0 = 0;
		break;
	default:
		if (exp > 30) {
			m1 = (m0 << (exp - 30)) |
			    (m1 >> (62 - exp)) & ~(UINT_MAX << (exp - 30));
			m0 >>= 62 - exp;
		} else {
			m1 = m0 >> (30 - exp);
			m0 = 0;
		}
		break;
	}

	if (i0 < 0) {
		m0 = ~m0;
		m1 = ~m1;
		if (++m1 == 0)
			m0++;
	}

	(void) _Q_set_exception(_fp_current_exceptions);
	return ((long long)(((unsigned long long)m0 << 32) | m1));
}

/*
 * Convert a floating point number into a 64-bit int.
 */
long long
__ftoll(float fval)
{
	int i0;
	int exp;		/* exponent */
	int m0;			/* most significant word of mantissa */
	unsigned int m1;	/* least sig. word of mantissa */
	unsigned int _fp_current_exceptions = 0;
	union {
		int i;
		float f;
	} u;

	/*
	 * Extract the exponent and check boundary conditions.
	 * Notice that the exponent is equal to the bit number where
	 * we want the most significant bit to live.
	 */
	u.f = fval;
	i0 = u.i;

	exp = ((i0 >> 23) & 0xff) - 0x7f;
	if (exp < 0) {
		return ((long long) 0); /* abs(x) < 1.0, so round to 0 */
	} else if (exp > 62)  {
		/*
		 * fp_invalid NOT raised if <i0> == LLONG_MIN
		 */
		if (i0 >= 0 || exp != 63 || (i0 & 0x7fffff) != 0) {
			/*
			 * abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
			 * overflow, Inf, NaN set fp_invalid exception
			 */
			_fp_current_exceptions |= (1 << (int)fp_invalid);
			(void) _Q_set_exception(_fp_current_exceptions);
		}
		if (i0 < 0)
			return (LLONG_MIN);
		else
			return (LLONG_MAX); /* MAXLONG */
	}

	/* Extract the mantissa. */

	m0 = 0x40000000 | (i0 << 7) & 0x3fffff80;
	m1 = 0;

	/*
	 * The most significant bit of the mantissa is now in bit 62 of m0:m1.
	 * Shift right by (62 - exp) bits.
	 */
	switch (exp) {
	case 62:
		break;
	case 30:
		m1 = m0;
		m0 = 0;
		break;
	default:
		if (exp > 30) {
			m1 = m0 << (exp - 30);
			m0 >>= 62 - exp;
		} else {
			m1 = m0 >> (30 - exp);
			m0 = 0;
		}
		break;
	}

	if (i0 < 0) {
		m0 = ~m0;
		m1 = ~m1;
		if (++m1 == 0)
			m0++;
	}

	(void) _Q_set_exception(_fp_current_exceptions);
	return ((long long)(((unsigned long long)m0 << 32) | m1));
}

/*
 * Convert an extended precision floating point number into a 64-bit int.
 */
long long
_Q_qtoll(long double longdbl)
{
	int i0;
	unsigned int i1, i2;	/* a long double is 128-bit in length */
	int *plngdbl = (int *)&longdbl;
	int exp;		/* exponent */
	int m0;			/* most significant word of mantissa */
	unsigned int m1;	/* least sig. word of mantissa */
	unsigned int _fp_current_exceptions = 0;

	/*
	 * Only 96-bits of precision used
	 */
	i0 = plngdbl[0];
	i1 = plngdbl[1];
	i2 = plngdbl[2];

	/*
	 * Extract the exponent and check boundary conditions.
	 * Notice that the exponent is equal to the bit number where
	 * we want the most significant bit to live.
	 */
	exp = ((i0 >> 16) & 0x7fff) - 0x3fff;
	if (exp < 0) {
		return ((long long)0); /* abs(x) < 1.0, so round to 0 */
	} else if (exp > 62)	{
		/*
		 * fp_invalid NOT raised if <i0,i1,i2,i3> when chopped to
		 * 64 bits == LLONG_MIN
		 */
		if (i0 >= 0 || exp != 63 || (i0 & 0xffff) != 0 || i1 != 0 ||
		    (i2 & 0xfffe0000) != 0) {
			/*
			 * abs(x) > MAXLLONG; return {MIN,MAX}LLONG and as
			 * overflow, Inf, NaN set fp_invalid exception
			 */
			_fp_current_exceptions |= (1 << (int)fp_invalid);
			(void) _Q_set_exception(_fp_current_exceptions);
		}
		if (i0 < 0)
			return (LLONG_MIN);
		else
			return (LLONG_MAX); /* MAXLONG */
	}

	/* Extract the mantissa. */

	m0 = 0x40000000 | ((i0 << 14) & 0x3fffc000) | ((i1 >> 18) & 0x3fff);
	m1 = (i1 << 14) | ((i2 >> 18) & 0x3fff);

	/*
	 * The most significant bit of the mantissa is now in bit 62 of m0:m1.
	 * Shift right by (62 - exp) bits.
	 */
	switch (exp) {
	case 62:
		break;
	case 30:
		m1 = m0;
		m0 = 0;
		break;
	default:
		if (exp > 30) {
			m1 = (m0 << (exp - 30)) |
			    (m1 >> (62 - exp)) & ~(UINT_MAX << (exp - 30));
			m0 >>= 62 - exp;
		} else {
			m1 = m0 >> (30 - exp);
			m0 = 0;
		}
		break;
	}

	if (i0 < 0) {
		m0 = ~m0;
		m1 = ~m1;
		if (++m1 == 0)
			m0++;
	}

	(void) _Q_set_exception(_fp_current_exceptions);
	return ((long long)(((unsigned long long)m0 << 32) | m1));
}