xref: /illumos-gate/usr/src/lib/libc/port/fp/decimal_bin.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Conversion from decimal to binary floating point
31  */
32 
33 #pragma weak decimal_to_single = _decimal_to_single
34 #pragma weak decimal_to_double = _decimal_to_double
35 #pragma weak decimal_to_extended = _decimal_to_extended
36 #pragma weak decimal_to_quadruple = _decimal_to_quadruple
37 
38 #include "synonyms.h"
39 #include <stdlib.h>
40 #include "base_conversion.h"
41 
42 /*
43  * Convert the integer part of a nonzero base-10^4 _big_float *pd
44  * to base 2^16 in **ppb.  The converted value is accurate to nsig
45  * significant bits.  On exit, *sticky is nonzero if *pd had a
46  * nonzero fractional part.  If pd->exponent > 0 and **ppb is not
47  * large enough to hold the final converted value (i.e., the con-
48  * verted significand scaled by 10^pd->exponent), then on exit,
49  * *ppb will point to a newly allocated _big_float, which must be
50  * freed by the caller.  (The number of significant bits we need
51  * should fit in pb, but __big_float_times_power may allocate new
52  * storage anyway because the exact product could require more than
53  * 16000 bits.)
54  *
55  * This routine does not check that **ppb is large enough to hold
56  * the result of converting the significand of *pd.
57  */
58 static void
59 __big_decimal_to_big_binary(_big_float *pd, int nsig, _big_float **ppb,
60     int *sticky)
61 {
62 	_big_float	*pb;
63 	int		i, j, len, s;
64 	unsigned int	carry;
65 
66 	pb = *ppb;
67 
68 	/* convert pd a digit at a time, most significant first */
69 	if (pd->bexponent + ((pd->blength - 1) << 2) >= 0) {
70 		pb->bsignificand[0] = pd->bsignificand[pd->blength - 1];
71 		len = 1;
72 		for (i = pd->blength - 2; i >= 0 &&
73 		    pd->bexponent + (i << 2) >= 0; i--) {
74 			/* multiply pb by 10^4 and add next digit */
75 			carry = pd->bsignificand[i];
76 			for (j = 0; j < len; j++) {
77 				carry += (unsigned int)pb->bsignificand[j]
78 				    * 10000;
79 				pb->bsignificand[j] = carry & 0xffff;
80 				carry >>= 16;
81 			}
82 			if (carry)
83 				pb->bsignificand[j++] = carry;
84 			len = j;
85 		}
86 	} else {
87 		i = pd->blength - 1;
88 		len = 0;
89 	}
90 
91 	/* convert any partial digit */
92 	if (i >= 0 && pd->bexponent + (i << 2) > -4) {
93 		s = pd->bexponent + (i << 2) + 4;
94 		/* multiply pb by 10^s and add partial digit */
95 		carry = pd->bsignificand[i];
96 		if (s == 1) {
97 			s = carry % 1000;
98 			carry = carry / 1000;
99 			for (j = 0; j < len; j++) {
100 				carry += (unsigned int)pb->bsignificand[j]
101 				    * 10;
102 				pb->bsignificand[j] = carry & 0xffff;
103 				carry >>= 16;
104 			}
105 		} else if (s == 2) {
106 			s = carry % 100;
107 			carry = carry / 100;
108 			for (j = 0; j < len; j++) {
109 				carry += (unsigned int)pb->bsignificand[j]
110 				    * 100;
111 				pb->bsignificand[j] = carry & 0xffff;
112 				carry >>= 16;
113 			}
114 		} else {
115 			s = carry % 10;
116 			carry = carry / 10;
117 			for (j = 0; j < len; j++) {
118 				carry += (unsigned int)pb->bsignificand[j]
119 				    * 1000;
120 				pb->bsignificand[j] = carry & 0xffff;
121 				carry >>= 16;
122 			}
123 		}
124 		if (carry)
125 			pb->bsignificand[j++] = carry;
126 		len = j;
127 		i--;
128 	} else {
129 		s = 0;
130 	}
131 
132 	pb->blength = len;
133 	pb->bexponent = 0;
134 
135 	/* continue accumulating sticky flag */
136 	while (i >= 0)
137 		s |= pd->bsignificand[i--];
138 	*sticky = s;
139 
140 	if (pd->bexponent > 0) {
141 		/* scale pb by 10^pd->exponent */
142 		__big_float_times_power(pb, 10, pd->bexponent, nsig, ppb);
143 	}
144 }
145 
146 /*
147  * Convert the decimal_record *pd to an unpacked datum *px accurately
148  * enough that *px can be rounded correctly to sigbits significant bits.
149  * (We may assume sigbits <= 113.)
150  */
151 static void
152 __decimal_to_unpacked(unpacked *px, decimal_record *pd, int sigbits)
153 {
154 	_big_float	d, b, *pbd, *pbb;
155 	char		*ds;
156 	int		ids, i, ix, exp, ndigs;
157 	int		sticky, powtwo, sigdigits;
158 
159 	px->sign = pd->sign;
160 	px->fpclass = pd->fpclass;
161 	ds = pd->ds;
162 	ndigs = pd->ndigits;
163 	exp = pd->exponent;
164 
165 	/* remove trailing zeroes */
166 	while (ndigs > 0 && ds[ndigs - 1] == '0') {
167 		exp++;
168 		ndigs--;
169 	}
170 	if (ndigs < 1) {
171 		/* nothing left */
172 		px->fpclass = fp_zero;
173 		return;
174 	}
175 
176 	/* convert remaining digits to a base-10^4 _big_float */
177 	d.bsize = _BIG_FLOAT_SIZE;
178 	d.bexponent = exp;
179 	d.blength = (ndigs + 3) >> 2;
180 	i = d.blength - 1;
181 	ids = ndigs - (d.blength << 2);
182 	switch (ids) {
183 	case -1:
184 		d.bsignificand[i] = 100 * ds[ids + 1] +
185 		    10 * ds[ids + 2] + ds[ids + 3] - 111 * '0';
186 		i--;
187 		ids += 4;
188 		break;
189 
190 	case -2:
191 		d.bsignificand[i] = 10 * ds[ids + 2] + ds[ids + 3] - 11 * '0';
192 		i--;
193 		ids += 4;
194 		break;
195 
196 	case -3:
197 		d.bsignificand[i] = ds[ids + 3] - '0';
198 		i--;
199 		ids += 4;
200 		break;
201 	}
202 	while (i >= 0) {
203 		d.bsignificand[i] = 1000 * ds[ids] + 100 * ds[ids + 1] +
204 		    10 * ds[ids + 2] + ds[ids + 3] - 1111 * '0';
205 		i--;
206 		ids += 4;
207 	}
208 
209 	pbd = &d;
210 	powtwo = 0;
211 
212 	/* pre-scale to get the bits we want into the integer part */
213 	if (exp < 0) {
214 		/* i is a lower bound on log10(x) */
215 		i = exp + ndigs - 1;
216 		if (i <= 0 || ((i * 217705) >> 16) < sigbits + 2) {
217 			/*
218 			 * Scale by 2^(sigbits + 2 + u) where
219 			 * u is an upper bound on -log2(x).
220 			 */
221 			powtwo = sigbits + 2;
222 			if (i < 0)
223 				powtwo += ((-i * 217706) + 65535) >> 16;
224 			else if (i > 0)
225 				powtwo -= (i * 217705) >> 16;
226 			/*
227 			 * Take sigdigits large enough to get
228 			 * all integral digits correct.
229 			 */
230 			sigdigits = i + 1 + (((powtwo * 19729) + 65535) >> 16);
231 			__big_float_times_power(&d, 2, powtwo, sigdigits, &pbd);
232 		}
233 	}
234 
235 	/* convert to base 2^16 */
236 	b.bsize = _BIG_FLOAT_SIZE;
237 	pbb = &b;
238 	__big_decimal_to_big_binary(pbd, sigbits + 2, &pbb, &sticky);
239 
240 	/* adjust pbb->bexponent based on the scale factor above */
241 	pbb->bexponent -= powtwo;
242 
243 	/* convert to unpacked */
244 	ix = 0;
245 	for (i = pbb->blength - 1; i > 0 && ix < 5; i -= 2) {
246 		px->significand[ix++] = (pbb->bsignificand[i] << 16) |
247 		    pbb->bsignificand[i - 1];
248 	}
249 	if (ix < 5) {
250 		/* pad with zeroes */
251 		if (i == 0)
252 			px->significand[ix++] = pbb->bsignificand[i] << 16;
253 		while (ix < 5)
254 			px->significand[ix++] = 0;
255 	} else {
256 		/* truncate and set a sticky bit if necessary */
257 		while (i >= 0 && pbb->bsignificand[i] == 0)
258 			i--;
259 		if (i >= 0)
260 			px->significand[4] |= 1;
261 	}
262 	if (sticky | pd->more)
263 		px->significand[4] |= 1;
264 	px->exponent = pbb->bexponent + (pbb->blength << 4) - 1;
265 
266 	/* normalize so the most significant bit is set */
267 	while (px->significand[0] < 0x80000000u) {
268 		px->significand[0] = (px->significand[0] << 1) |
269 		    (px->significand[1] >> 31);
270 		px->significand[1] = (px->significand[1] << 1) |
271 		    (px->significand[2] >> 31);
272 		px->significand[2] = (px->significand[2] << 1) |
273 		    (px->significand[3] >> 31);
274 		px->significand[3] = (px->significand[3] << 1) |
275 		    (px->significand[4] >> 31);
276 		px->significand[4] <<= 1;
277 		px->exponent--;
278 	}
279 
280 	if (pbd != &d)
281 		(void) free((void *)pbd);
282 	if (pbb != &b)
283 		(void) free((void *)pbb);
284 }
285 
286 /*
287  * Convert a string s consisting of n <= 18 ASCII decimal digits
288  * to an integer value in double precision format, and set *pe
289  * to the number of rounding errors incurred (0 or 1).
290  */
291 static double
292 __digits_to_double(char *s, int n, int *pe)
293 {
294 	int	i, acc;
295 	double	t, th, tl;
296 
297 	if (n <= 9) {
298 		acc = s[0] - '0';
299 		for (i = 1; i < n; i++) {
300 			/* acc <- 10 * acc + next digit */
301 			acc = (acc << 1) + (acc << 3) + s[i] - '0';
302 		}
303 		t = (double)acc;
304 		*pe = 0;
305 	} else {
306 		acc = s[0] - '0';
307 		for (i = 1; i < (n - 9); i++) {
308 			/* acc <- 10 * acc + next digit */
309 			acc = (acc << 1) + (acc << 3) + s[i] - '0';
310 		}
311 		th = 1.0e9 * (double)acc;	/* this will be exact */
312 		acc = s[n - 9] - '0';
313 		for (i = n - 8; i < n; i++) {
314 			/* acc <- 10 * acc + next digit */
315 			acc = (acc << 1) + (acc << 3) + s[i] - '0';
316 		}
317 		tl = (double)acc;
318 		/* add and indicate whether or not the sum is exact */
319 		t = th + tl;
320 		*pe = ((t - th) == tl)? 0 : 1;
321 	}
322 	return (t);
323 }
324 
325 static union {
326 	int	i[2];
327 	double	d;
328 } C[] = {
329 #ifdef _LITTLE_ENDIAN
330 	{ 0x00000000, 0x3cc40000 },
331 #else
332 	{ 0x3cc40000, 0x00000000 },	/* 5 * 2^-53 */
333 #endif
334 };
335 
336 #define	five2m53	C[0].d
337 
338 static int
339 __fast_decimal_to_single(single *px, decimal_mode *pm, decimal_record *pd,
340     fp_exception_field_type *ps)
341 {
342 	double			dds, delta, ddsplus, ddsminus, df1;
343 	int			n, exp, rounded, e;
344 	float			f1, f2;
345 	__ieee_flags_type	fb;
346 
347 	if (pm->rd != fp_nearest)
348 		return (0);
349 
350 	exp = pd->exponent;
351 	if (pd->ndigits <= 18) {
352 		rounded = 0;
353 		n = pd->ndigits;
354 	} else {
355 		rounded = 1;
356 		n = 18;
357 		exp += pd->ndigits - 18;
358 	}
359 	/*
360 	 * exp must be in the range of the table, and the result
361 	 * must not underflow or overflow.
362 	 */
363 	if (exp < -__TBL_TENS_MAX || exp + n < -36 || exp + n > 38)
364 		return (0);
365 
366 	__get_ieee_flags(&fb);
367 	dds = __digits_to_double(pd->ds, n, &e);
368 	if (e != 0)
369 		rounded = 1;
370 	if (exp > 0) {
371 		/* small positive exponent */
372 		if (exp > __TBL_TENS_EXACT)
373 			rounded = 1;
374 		if (rounded) {
375 			dds *= __tbl_tens[exp];
376 		} else {
377 			dds = __mul_set(dds, __tbl_tens[exp], &e);
378 			if (e)
379 				rounded = 1;
380 		}
381 	} else if (exp < 0) {
382 		/* small negative exponent */
383 		if (-exp > __TBL_TENS_EXACT)
384 			rounded = 1;
385 		if (rounded) {
386 			dds /= __tbl_tens[-exp];
387 		} else {
388 			dds = __div_set(dds, __tbl_tens[-exp], &e);
389 			if (e)
390 				rounded = 1;
391 		}
392 	}
393 
394 	/*
395 	 * At this point dds may have four rounding errors due to
396 	 * (i) truncation of pd->ds to 18 digits, (ii) inexact con-
397 	 * version of pd->ds to binary, (iii) scaling by a power of
398 	 * ten that is not exactly representable, and (iv) roundoff
399 	 * error in the multiplication.  Below we will incur one more
400 	 * rounding error when we add or subtract delta and dds.  We
401 	 * construct delta so that even after this last rounding error,
402 	 * ddsplus is an upper bound on the exact value and ddsminus
403 	 * is a lower bound.  Then as long as both of these quantities
404 	 * round to the same single precision number, that number
405 	 * will be the correctly rounded single precision result.
406 	 * (If any rounding errors have been committed, then we must
407 	 * also be certain that the result can't be exact.)
408 	 */
409 	delta = five2m53 * dds;
410 	ddsplus = dds + delta;
411 	ddsminus = dds - delta;
412 	f1 = (float)(ddsplus);
413 	f2 = (float)(ddsminus);
414 	df1 = f1;
415 	__set_ieee_flags(&fb);
416 	if (f1 != f2)
417 		return (0);
418 	if (rounded) {
419 		/*
420 		 * If ddsminus <= f1 <= ddsplus, the result might be
421 		 * exact; we have to convert the long way to be sure.
422 		 */
423 		if (ddsminus <= df1 && df1 <= ddsplus)
424 			return (0);
425 		*ps = (1 << fp_inexact);
426 	} else {
427 		*ps = (df1 == dds)? 0 : (1 << fp_inexact);
428 	}
429 	*px = (pd->sign)? -f1 : f1;
430 	return (1);
431 }
432 
433 /*
434  * Attempt conversion to double using floating-point arithmetic.
435  * Return 1 if it works (at most one rounding error), 0 if it doesn't.
436  */
437 static int
438 __fast_decimal_to_double(double *px, decimal_mode *pm, decimal_record *pd,
439     fp_exception_field_type *ps)
440 {
441 	double			dds;
442 	int			e;
443 	__ieee_flags_type	fb;
444 
445 	if (pm->rd != fp_nearest || pd->ndigits > 18 || pd->exponent
446 	    > __TBL_TENS_EXACT || pd->exponent < -__TBL_TENS_EXACT)
447 		return (0);
448 
449 	__get_ieee_flags(&fb);
450 	dds = __digits_to_double(pd->ds, pd->ndigits, &e);
451 	if (e != 0) {
452 		__set_ieee_flags(&fb);
453 		return (0);
454 	}
455 	if (pd->exponent > 0)
456 		dds = __mul_set(dds, __tbl_tens[pd->exponent], &e);
457 	else if (pd->exponent < 0)
458 		dds = __div_set(dds, __tbl_tens[-pd->exponent], &e);
459 	*px = (pd->sign)? -dds : dds;
460 	*ps = (e)? (1 << fp_inexact) : 0;
461 	__set_ieee_flags(&fb);
462 	return (1);
463 }
464 
465 /* PUBLIC FUNCTIONS */
466 
467 /*
468  * The following routines convert the decimal record *pd to a floating
469  * point value *px observing the rounding mode specified in pm->rd and
470  * passing back any floating point exceptions that occur in *ps.
471  *
472  * pd->sign and pd->fpclass are always taken into account.  pd->exponent
473  * and pd->ds are used when pd->fpclass is fp_normal or fp_subnormal.
474  * In these cases, pd->ds must contain a null-terminated string of one
475  * or more ASCII digits, the first of which is not zero, and pd->ndigits
476  * must equal the length of this string.  If m is the integer represented
477  * by the string pd->ds, then *px will be set to a correctly rounded
478  * approximation to
479  *
480  *   -1**(pd->sign) * m * 10**(pd->exponent)
481  *
482  * (If pd->more != 0 then additional nonzero digits are assumed to follow
483  * those in pd->ds, so m is effectively replaced by m + epsilon in the
484  * expression above.)
485  *
486  * For example, if pd->exponent == -2 and pd->ds holds "1234", then *px
487  * will be a correctly rounded approximation to 12.34.
488  *
489  * Note that the only mode that matters is the rounding direction pm->rd;
490  * pm->df and pm->ndigits are never used.
491  */
492 
493 /* maximum decimal exponent we need to consider */
494 #define	SINGLE_MAXE	  47
495 #define	DOUBLE_MAXE	 326
496 #define	EXTENDED_MAXE	4968
497 #define	QUAD_MAXE	4968
498 
499 void
500 decimal_to_single(single *px, decimal_mode *pm, decimal_record *pd,
501     fp_exception_field_type *ps)
502 {
503 	single_equivalence	*kluge;
504 	unpacked		u;
505 	fp_exception_field_type	ef;
506 	int			i;
507 
508 	/* special values */
509 	kluge = (single_equivalence *)px;
510 	switch (pd->fpclass) {
511 	case fp_zero:
512 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
513 		kluge->f.msw.exponent = 0;
514 		kluge->f.msw.significand = 0;
515 		*ps = 0;
516 		return;
517 
518 	case fp_infinity:
519 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
520 		kluge->f.msw.exponent = 0xff;
521 		kluge->f.msw.significand = 0;
522 		*ps = 0;
523 		return;
524 
525 	case fp_quiet:
526 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
527 		kluge->f.msw.exponent = 0xff;
528 		kluge->f.msw.significand = 0x7fffff;
529 		*ps = 0;
530 		return;
531 
532 	case fp_signaling:
533 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
534 		kluge->f.msw.exponent = 0xff;
535 		kluge->f.msw.significand = 0x3fffff;
536 		*ps = 0;
537 		return;
538 	}
539 
540 	/* numeric values */
541 	ef = 0;
542 	if (pd->exponent + pd->ndigits > SINGLE_MAXE) {
543 		/* result must overflow */
544 		u.sign = (pd->sign != 0);
545 		u.fpclass = fp_normal;
546 		u.exponent = 0x000fffff;
547 		u.significand[0] = 0x80000000;
548 		for (i = 1; i < UNPACKED_SIZE; i++)
549 			u.significand[i] = 0;
550 	} else if (pd->exponent + pd->ndigits < -SINGLE_MAXE) {
551 		/* result must underflow completely */
552 		u.sign = (pd->sign != 0);
553 		u.fpclass = fp_normal;
554 		u.exponent = -0x000fffff;
555 		u.significand[0] = 0x80000000;
556 		for (i = 1; i < UNPACKED_SIZE; i++)
557 			u.significand[i] = 0;
558 	} else {
559 		/* result may be in range */
560 		if (__fast_decimal_to_single(px, pm, pd, &ef) == 1) {
561 			*ps = ef;
562 			if (ef != 0)
563 				__base_conversion_set_exception(ef);
564 			return;
565 		}
566 		__decimal_to_unpacked(&u, pd, 24);
567 	}
568 	__pack_single(&u, px, pm->rd, &ef);
569 	*ps = ef;
570 	if (ef != 0)
571 		__base_conversion_set_exception(ef);
572 }
573 
574 void
575 decimal_to_double(double *px, decimal_mode *pm, decimal_record *pd,
576     fp_exception_field_type *ps)
577 {
578 	double_equivalence	*kluge;
579 	unpacked		u;
580 	fp_exception_field_type	ef;
581 	int			i;
582 
583 	/* special values */
584 	kluge = (double_equivalence *)px;
585 	switch (pd->fpclass) {
586 	case fp_zero:
587 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
588 		kluge->f.msw.exponent = 0;
589 		kluge->f.msw.significand = 0;
590 		kluge->f.significand2 = 0;
591 		*ps = 0;
592 		return;
593 
594 	case fp_infinity:
595 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
596 		kluge->f.msw.exponent = 0x7ff;
597 		kluge->f.msw.significand = 0;
598 		kluge->f.significand2 = 0;
599 		*ps = 0;
600 		return;
601 
602 	case fp_quiet:
603 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
604 		kluge->f.msw.exponent = 0x7ff;
605 		kluge->f.msw.significand = 0xfffff;
606 		kluge->f.significand2 = 0xffffffff;
607 		*ps = 0;
608 		return;
609 
610 	case fp_signaling:
611 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
612 		kluge->f.msw.exponent = 0x7ff;
613 		kluge->f.msw.significand = 0x7ffff;
614 		kluge->f.significand2 = 0xffffffff;
615 		*ps = 0;
616 		return;
617 	}
618 
619 	/* numeric values */
620 	ef = 0;
621 	if (pd->exponent + pd->ndigits > DOUBLE_MAXE) {
622 		/* result must overflow */
623 		u.sign = (pd->sign != 0);
624 		u.fpclass = fp_normal;
625 		u.exponent = 0x000fffff;
626 		u.significand[0] = 0x80000000;
627 		for (i = 1; i < UNPACKED_SIZE; i++)
628 			u.significand[i] = 0;
629 	} else if (pd->exponent + pd->ndigits < -DOUBLE_MAXE) {
630 		/* result must underflow completely */
631 		u.sign = (pd->sign != 0);
632 		u.fpclass = fp_normal;
633 		u.exponent = -0x000fffff;
634 		u.significand[0] = 0x80000000;
635 		for (i = 1; i < UNPACKED_SIZE; i++)
636 			u.significand[i] = 0;
637 	} else {
638 		/* result may be in range */
639 		if (__fast_decimal_to_double(px, pm, pd, &ef) == 1) {
640 			*ps = ef;
641 			if (ef != 0)
642 				__base_conversion_set_exception(ef);
643 			return;
644 		}
645 		__decimal_to_unpacked(&u, pd, 53);
646 	}
647 	__pack_double(&u, px, pm->rd, &ef);
648 	*ps = ef;
649 	if (ef != 0)
650 		__base_conversion_set_exception(ef);
651 }
652 
653 void
654 decimal_to_extended(extended *px, decimal_mode *pm, decimal_record *pd,
655     fp_exception_field_type *ps)
656 {
657 	extended_equivalence	*kluge;
658 	unpacked		u;
659 	double_equivalence	dd;
660 	fp_exception_field_type ef;
661 	int			i;
662 
663 	/* special values */
664 	kluge = (extended_equivalence *)px;
665 	switch (pd->fpclass) {
666 	case fp_zero:
667 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
668 		kluge->f.msw.exponent = 0;
669 		kluge->f.significand = 0;
670 		kluge->f.significand2 = 0;
671 		*ps = 0;
672 		return;
673 
674 	case fp_infinity:
675 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
676 		kluge->f.msw.exponent = 0x7fff;
677 		kluge->f.significand = 0x80000000;
678 		kluge->f.significand2 = 0;
679 		*ps = 0;
680 		return;
681 
682 	case fp_quiet:
683 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
684 		kluge->f.msw.exponent = 0x7fff;
685 		kluge->f.significand = 0xffffffff;
686 		kluge->f.significand2 = 0xffffffff;
687 		*ps = 0;
688 		return;
689 
690 	case fp_signaling:
691 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
692 		kluge->f.msw.exponent = 0x7fff;
693 		kluge->f.significand = 0xbfffffff;
694 		kluge->f.significand2 = 0xffffffff;
695 		*ps = 0;
696 		return;
697 	}
698 
699 	/* numeric values */
700 	ef = 0;
701 	if (pd->exponent + pd->ndigits > EXTENDED_MAXE) {
702 		/* result must overflow */
703 		u.sign = (pd->sign != 0);
704 		u.fpclass = fp_normal;
705 		u.exponent = 0x000fffff;
706 		u.significand[0] = 0x80000000;
707 		for (i = 1; i < UNPACKED_SIZE; i++)
708 			u.significand[i] = 0;
709 	} else if (pd->exponent + pd->ndigits < -EXTENDED_MAXE) {
710 		/* result must underflow completely */
711 		u.sign = (pd->sign != 0);
712 		u.fpclass = fp_normal;
713 		u.exponent = -0x000fffff;
714 		u.significand[0] = 0x80000000;
715 		for (i = 1; i < UNPACKED_SIZE; i++)
716 			u.significand[i] = 0;
717 	} else {
718 		/* result may be in range */
719 		if (__fast_decimal_to_double(&dd.x, pm, pd, &ef) == 1 &&
720 		    ef == 0) {
721 			u.sign = dd.f.msw.sign;
722 			u.fpclass = fp_normal;
723 			u.exponent = dd.f.msw.exponent - DOUBLE_BIAS;
724 			u.significand[0] = ((0x100000 |
725 			    dd.f.msw.significand) << 11) |
726 			    (dd.f.significand2 >> 21);
727 			u.significand[1] = dd.f.significand2 << 11;
728 			for (i = 2; i < UNPACKED_SIZE; i++)
729 				u.significand[i] = 0;
730 		} else {
731 			__decimal_to_unpacked(&u, pd, 64);
732 		}
733 	}
734 	__pack_extended(&u, px, pm->rd, &ef);
735 	*ps = ef;
736 	if (ef != 0)
737 		__base_conversion_set_exception(ef);
738 }
739 
740 void
741 decimal_to_quadruple(quadruple *px, decimal_mode *pm, decimal_record *pd,
742     fp_exception_field_type *ps)
743 {
744 	quadruple_equivalence	*kluge;
745 	unpacked		u;
746 	double_equivalence	dd;
747 	fp_exception_field_type ef;
748 	int			i;
749 
750 	/* special values */
751 	kluge = (quadruple_equivalence *)px;
752 	switch (pd->fpclass) {
753 	case fp_zero:
754 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
755 		kluge->f.msw.exponent = 0;
756 		kluge->f.msw.significand = 0;
757 		kluge->f.significand2 = 0;
758 		kluge->f.significand3 = 0;
759 		kluge->f.significand4 = 0;
760 		*ps = 0;
761 		return;
762 
763 	case fp_infinity:
764 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
765 		kluge->f.msw.exponent = 0x7fff;
766 		kluge->f.msw.significand = 0;
767 		kluge->f.significand2 = 0;
768 		kluge->f.significand3 = 0;
769 		kluge->f.significand4 = 0;
770 		*ps = 0;
771 		return;
772 
773 	case fp_quiet:
774 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
775 		kluge->f.msw.exponent = 0x7fff;
776 		kluge->f.msw.significand = 0xffff;
777 		kluge->f.significand2 = 0xffffffff;
778 		kluge->f.significand3 = 0xffffffff;
779 		kluge->f.significand4 = 0xffffffff;
780 		*ps = 0;
781 		return;
782 
783 	case fp_signaling:
784 		kluge->f.msw.sign = (pd->sign)? 1 : 0;
785 		kluge->f.msw.exponent = 0x7fff;
786 		kluge->f.msw.significand = 0x7fff;
787 		kluge->f.significand2 = 0xffffffff;
788 		kluge->f.significand3 = 0xffffffff;
789 		kluge->f.significand4 = 0xffffffff;
790 		*ps = 0;
791 		return;
792 	}
793 
794 	/* numeric values */
795 	ef = 0;
796 	if (pd->exponent + pd->ndigits > QUAD_MAXE) {
797 		/* result must overflow */
798 		u.sign = (pd->sign != 0);
799 		u.fpclass = fp_normal;
800 		u.exponent = 0x000fffff;
801 		u.significand[0] = 0x80000000;
802 		for (i = 1; i < UNPACKED_SIZE; i++)
803 			u.significand[i] = 0;
804 	} else if (pd->exponent + pd->ndigits < -QUAD_MAXE) {
805 		/* result must underflow completely */
806 		u.sign = (pd->sign != 0);
807 		u.fpclass = fp_normal;
808 		u.exponent = -0x000fffff;
809 		u.significand[0] = 0x80000000;
810 		for (i = 1; i < UNPACKED_SIZE; i++)
811 			u.significand[i] = 0;
812 	} else {
813 		/* result may be in range */
814 		if (__fast_decimal_to_double(&dd.x, pm, pd, &ef) == 1 &&
815 		    ef == 0) {
816 			u.sign = dd.f.msw.sign;
817 			u.fpclass = fp_normal;
818 			u.exponent = dd.f.msw.exponent - DOUBLE_BIAS;
819 			u.significand[0] = ((0x100000 |
820 			    dd.f.msw.significand) << 11) |
821 			    (dd.f.significand2 >> 21);
822 			u.significand[1] = dd.f.significand2 << 11;
823 			for (i = 2; i < UNPACKED_SIZE; i++)
824 				u.significand[i] = 0;
825 		} else {
826 			__decimal_to_unpacked(&u, pd, 113);
827 		}
828 	}
829 	__pack_quadruple(&u, px, pm->rd, &ef);
830 	*ps = ef;
831 	if (ef != 0)
832 		__base_conversion_set_exception(ef);
833 }
834