xref: /titanic_52/usr/src/lib/libbc/libc/gen/common/econvert.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate /*
25*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1988 by Sun Microsystems, Inc.
26*7c478bd9Sstevel@tonic-gate  */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include "base_conversion.h"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate static char     *nanstring = "NaN";
31*7c478bd9Sstevel@tonic-gate static char     *infstring = "Infinity";
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate char           *
34*7c478bd9Sstevel@tonic-gate econvert(arg, ndigits, decpt, sign, buf)
35*7c478bd9Sstevel@tonic-gate 	double          arg;
36*7c478bd9Sstevel@tonic-gate 	int             ndigits, *decpt, *sign;
37*7c478bd9Sstevel@tonic-gate 	char           *buf;
38*7c478bd9Sstevel@tonic-gate {
39*7c478bd9Sstevel@tonic-gate 	decimal_mode    dm;
40*7c478bd9Sstevel@tonic-gate 	decimal_record  dr;
41*7c478bd9Sstevel@tonic-gate 	fp_exception_field_type ef;
42*7c478bd9Sstevel@tonic-gate 	int             i;
43*7c478bd9Sstevel@tonic-gate 	char           *pc;
44*7c478bd9Sstevel@tonic-gate 	int             nc;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	dm.rd = fp_direction;	/* Rounding direction. */
47*7c478bd9Sstevel@tonic-gate 	dm.df = floating_form;	/* E format. */
48*7c478bd9Sstevel@tonic-gate 	dm.ndigits = ndigits;	/* Number of significant digits. */
49*7c478bd9Sstevel@tonic-gate 	double_to_decimal(&arg, &dm, &dr, &ef);
50*7c478bd9Sstevel@tonic-gate 	*sign = dr.sign;
51*7c478bd9Sstevel@tonic-gate 	switch (dr.fpclass) {
52*7c478bd9Sstevel@tonic-gate 	case fp_normal:
53*7c478bd9Sstevel@tonic-gate 	case fp_subnormal:
54*7c478bd9Sstevel@tonic-gate 		*decpt = dr.exponent + ndigits;
55*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ndigits; i++)
56*7c478bd9Sstevel@tonic-gate 			buf[i] = dr.ds[i];
57*7c478bd9Sstevel@tonic-gate 		buf[ndigits] = 0;
58*7c478bd9Sstevel@tonic-gate 		break;
59*7c478bd9Sstevel@tonic-gate 	case fp_zero:
60*7c478bd9Sstevel@tonic-gate 		*decpt = 1;
61*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ndigits; i++)
62*7c478bd9Sstevel@tonic-gate 			buf[i] = '0';
63*7c478bd9Sstevel@tonic-gate 		buf[ndigits] = 0;
64*7c478bd9Sstevel@tonic-gate 		break;
65*7c478bd9Sstevel@tonic-gate 	case fp_infinity:
66*7c478bd9Sstevel@tonic-gate 		*decpt = 0;
67*7c478bd9Sstevel@tonic-gate 		pc = infstring;
68*7c478bd9Sstevel@tonic-gate 		if (ndigits < 8)
69*7c478bd9Sstevel@tonic-gate 			nc = 3;
70*7c478bd9Sstevel@tonic-gate 		else
71*7c478bd9Sstevel@tonic-gate 			nc = 8;
72*7c478bd9Sstevel@tonic-gate 		goto movestring;
73*7c478bd9Sstevel@tonic-gate 	case fp_quiet:
74*7c478bd9Sstevel@tonic-gate 	case fp_signaling:
75*7c478bd9Sstevel@tonic-gate 		*decpt = 0;
76*7c478bd9Sstevel@tonic-gate 		pc = nanstring;
77*7c478bd9Sstevel@tonic-gate 		nc = 3;
78*7c478bd9Sstevel@tonic-gate movestring:
79*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nc; i++)
80*7c478bd9Sstevel@tonic-gate 			buf[i] = pc[i];
81*7c478bd9Sstevel@tonic-gate 		buf[nc] = 0;
82*7c478bd9Sstevel@tonic-gate 		break;
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 	return buf;		/* For compatibility with ecvt. */
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate char           *
88*7c478bd9Sstevel@tonic-gate fconvert(arg, ndigits, decpt, sign, buf)
89*7c478bd9Sstevel@tonic-gate 	double          arg;
90*7c478bd9Sstevel@tonic-gate 	int             ndigits, *decpt, *sign;
91*7c478bd9Sstevel@tonic-gate 	char           *buf;
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate 	decimal_mode    dm;
94*7c478bd9Sstevel@tonic-gate 	decimal_record  dr;
95*7c478bd9Sstevel@tonic-gate 	fp_exception_field_type ef;
96*7c478bd9Sstevel@tonic-gate 	int             i;
97*7c478bd9Sstevel@tonic-gate 	char           *pc;
98*7c478bd9Sstevel@tonic-gate 	int             nc;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	dm.rd = fp_direction;	/* Rounding direction. */
101*7c478bd9Sstevel@tonic-gate 	dm.df = fixed_form;	/* F format. */
102*7c478bd9Sstevel@tonic-gate 	dm.ndigits = ndigits;	/* Number of digits after point. */
103*7c478bd9Sstevel@tonic-gate 	double_to_decimal(&arg, &dm, &dr, &ef);
104*7c478bd9Sstevel@tonic-gate 	*sign = dr.sign;
105*7c478bd9Sstevel@tonic-gate 	switch (dr.fpclass) {
106*7c478bd9Sstevel@tonic-gate 	case fp_normal:
107*7c478bd9Sstevel@tonic-gate 	case fp_subnormal:
108*7c478bd9Sstevel@tonic-gate 		if (ndigits >= 0)
109*7c478bd9Sstevel@tonic-gate 			*decpt = dr.ndigits - ndigits;
110*7c478bd9Sstevel@tonic-gate 		else
111*7c478bd9Sstevel@tonic-gate 			*decpt = dr.ndigits;
112*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < dr.ndigits; i++)
113*7c478bd9Sstevel@tonic-gate 			buf[i] = dr.ds[i];
114*7c478bd9Sstevel@tonic-gate 		buf[dr.ndigits] = 0;
115*7c478bd9Sstevel@tonic-gate 		break;
116*7c478bd9Sstevel@tonic-gate 	case fp_zero:
117*7c478bd9Sstevel@tonic-gate 		*decpt = 0;
118*7c478bd9Sstevel@tonic-gate 		buf[0] = '0';
119*7c478bd9Sstevel@tonic-gate 		for (i = 1; i < ndigits; i++)
120*7c478bd9Sstevel@tonic-gate 			buf[i] = '0';
121*7c478bd9Sstevel@tonic-gate 		buf[i] = 0;
122*7c478bd9Sstevel@tonic-gate 		break;
123*7c478bd9Sstevel@tonic-gate 	case fp_infinity:
124*7c478bd9Sstevel@tonic-gate 		*decpt = 0;
125*7c478bd9Sstevel@tonic-gate 		pc = infstring;
126*7c478bd9Sstevel@tonic-gate 		if (ndigits < 8)
127*7c478bd9Sstevel@tonic-gate 			nc = 3;
128*7c478bd9Sstevel@tonic-gate 		else
129*7c478bd9Sstevel@tonic-gate 			nc = 8;
130*7c478bd9Sstevel@tonic-gate 		goto movestring;
131*7c478bd9Sstevel@tonic-gate 	case fp_quiet:
132*7c478bd9Sstevel@tonic-gate 	case fp_signaling:
133*7c478bd9Sstevel@tonic-gate 		*decpt = 0;
134*7c478bd9Sstevel@tonic-gate 		pc = nanstring;
135*7c478bd9Sstevel@tonic-gate 		nc = 3;
136*7c478bd9Sstevel@tonic-gate movestring:
137*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nc; i++)
138*7c478bd9Sstevel@tonic-gate 			buf[i] = pc[i];
139*7c478bd9Sstevel@tonic-gate 		buf[nc] = 0;
140*7c478bd9Sstevel@tonic-gate 		break;
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 	return buf;		/* For compatibility with fcvt. */
143*7c478bd9Sstevel@tonic-gate }
144