xref: /titanic_51/usr/src/lib/libbc/libc/gen/common/seconvert.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 char           *
31*7c478bd9Sstevel@tonic-gate seconvert(arg, ndigits, decpt, sign, buf)
32*7c478bd9Sstevel@tonic-gate 	single         *arg;
33*7c478bd9Sstevel@tonic-gate 	int             ndigits, *decpt, *sign;
34*7c478bd9Sstevel@tonic-gate 	char           *buf;
35*7c478bd9Sstevel@tonic-gate {
36*7c478bd9Sstevel@tonic-gate 	decimal_mode    dm;
37*7c478bd9Sstevel@tonic-gate 	decimal_record  dr;
38*7c478bd9Sstevel@tonic-gate 	fp_exception_field_type ef;
39*7c478bd9Sstevel@tonic-gate 	int             i;
40*7c478bd9Sstevel@tonic-gate 	static char     *nanstring = "NaN";
41*7c478bd9Sstevel@tonic-gate 	static char     *infstring = "Infinity";
42*7c478bd9Sstevel@tonic-gate 	char           *pc;
43*7c478bd9Sstevel@tonic-gate 	int             nc;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate 	dm.rd = fp_direction;	/* Rounding direction. */
46*7c478bd9Sstevel@tonic-gate 	dm.df = floating_form;	/* E format. */
47*7c478bd9Sstevel@tonic-gate 	dm.ndigits = ndigits;	/* Number of significant digits. */
48*7c478bd9Sstevel@tonic-gate 	single_to_decimal(arg, &dm, &dr, &ef);
49*7c478bd9Sstevel@tonic-gate 	*sign = dr.sign;
50*7c478bd9Sstevel@tonic-gate 	switch (dr.fpclass) {
51*7c478bd9Sstevel@tonic-gate 	case fp_normal:
52*7c478bd9Sstevel@tonic-gate 	case fp_subnormal:
53*7c478bd9Sstevel@tonic-gate 		*decpt = dr.exponent + ndigits;
54*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ndigits; i++)
55*7c478bd9Sstevel@tonic-gate 			buf[i] = dr.ds[i];
56*7c478bd9Sstevel@tonic-gate 		buf[ndigits] = 0;
57*7c478bd9Sstevel@tonic-gate 		break;
58*7c478bd9Sstevel@tonic-gate 	case fp_zero:
59*7c478bd9Sstevel@tonic-gate 		*decpt = 1;
60*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ndigits; i++)
61*7c478bd9Sstevel@tonic-gate 			buf[i] = '0';
62*7c478bd9Sstevel@tonic-gate 		buf[ndigits] = 0;
63*7c478bd9Sstevel@tonic-gate 		break;
64*7c478bd9Sstevel@tonic-gate 	case fp_infinity:
65*7c478bd9Sstevel@tonic-gate 		*decpt = 0;
66*7c478bd9Sstevel@tonic-gate 		pc = infstring;
67*7c478bd9Sstevel@tonic-gate 		if (ndigits < 8)
68*7c478bd9Sstevel@tonic-gate 			nc = 3;
69*7c478bd9Sstevel@tonic-gate 		else
70*7c478bd9Sstevel@tonic-gate 			nc = 8;
71*7c478bd9Sstevel@tonic-gate 		goto movestring;
72*7c478bd9Sstevel@tonic-gate 	case fp_quiet:
73*7c478bd9Sstevel@tonic-gate 	case fp_signaling:
74*7c478bd9Sstevel@tonic-gate 		*decpt = 0;
75*7c478bd9Sstevel@tonic-gate 		pc = nanstring;
76*7c478bd9Sstevel@tonic-gate 		nc = 3;
77*7c478bd9Sstevel@tonic-gate movestring:
78*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nc; i++)
79*7c478bd9Sstevel@tonic-gate 			buf[i] = pc[i];
80*7c478bd9Sstevel@tonic-gate 		buf[nc] = 0;
81*7c478bd9Sstevel@tonic-gate 		break;
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 	return buf;		/* For compatibility with ecvt. */
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate char           *
87*7c478bd9Sstevel@tonic-gate sfconvert(arg, ndigits, decpt, sign, buf)
88*7c478bd9Sstevel@tonic-gate 	single         *arg;
89*7c478bd9Sstevel@tonic-gate 	int             ndigits, *decpt, *sign;
90*7c478bd9Sstevel@tonic-gate 	char           *buf;
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	decimal_mode    dm;
93*7c478bd9Sstevel@tonic-gate 	decimal_record  dr;
94*7c478bd9Sstevel@tonic-gate 	fp_exception_field_type ef;
95*7c478bd9Sstevel@tonic-gate 	int             i;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	dm.rd = fp_direction;	/* Rounding direction. */
98*7c478bd9Sstevel@tonic-gate 	dm.df = fixed_form;	/* F format. */
99*7c478bd9Sstevel@tonic-gate 	dm.ndigits = ndigits;	/* Number of digits after point. */
100*7c478bd9Sstevel@tonic-gate 	single_to_decimal(arg, &dm, &dr, &ef);
101*7c478bd9Sstevel@tonic-gate 	*sign = dr.sign;
102*7c478bd9Sstevel@tonic-gate 	switch (dr.fpclass) {
103*7c478bd9Sstevel@tonic-gate 	case fp_normal:
104*7c478bd9Sstevel@tonic-gate 	case fp_subnormal:
105*7c478bd9Sstevel@tonic-gate 		if (ndigits >= 0)
106*7c478bd9Sstevel@tonic-gate 			*decpt = dr.ndigits - ndigits;
107*7c478bd9Sstevel@tonic-gate 		else
108*7c478bd9Sstevel@tonic-gate 			*decpt = dr.ndigits;
109*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < dr.ndigits; i++)
110*7c478bd9Sstevel@tonic-gate 			buf[i] = dr.ds[i];
111*7c478bd9Sstevel@tonic-gate 		buf[dr.ndigits] = 0;
112*7c478bd9Sstevel@tonic-gate 		break;
113*7c478bd9Sstevel@tonic-gate 	case fp_zero:
114*7c478bd9Sstevel@tonic-gate 		*decpt = 0;
115*7c478bd9Sstevel@tonic-gate 		buf[0] = '0';
116*7c478bd9Sstevel@tonic-gate 		for (i = 1; i < ndigits; i++)
117*7c478bd9Sstevel@tonic-gate 			buf[i] = '0';
118*7c478bd9Sstevel@tonic-gate 		buf[i] = 0;
119*7c478bd9Sstevel@tonic-gate 		break;
120*7c478bd9Sstevel@tonic-gate 	case fp_infinity:
121*7c478bd9Sstevel@tonic-gate 		*decpt = 0;
122*7c478bd9Sstevel@tonic-gate 		if (ndigits < 8)
123*7c478bd9Sstevel@tonic-gate 			buf = "Inf";
124*7c478bd9Sstevel@tonic-gate 		else
125*7c478bd9Sstevel@tonic-gate 			buf = "Infinity";
126*7c478bd9Sstevel@tonic-gate 		break;
127*7c478bd9Sstevel@tonic-gate 	case fp_quiet:
128*7c478bd9Sstevel@tonic-gate 	case fp_signaling:
129*7c478bd9Sstevel@tonic-gate 		*decpt = 0;
130*7c478bd9Sstevel@tonic-gate 		buf = "NaN";
131*7c478bd9Sstevel@tonic-gate 		break;
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 	return buf;		/* For compatibility with fcvt. */
134*7c478bd9Sstevel@tonic-gate }
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate extern void    _gcvt();
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate char           *
139*7c478bd9Sstevel@tonic-gate sgconvert(number, ndigit, trailing, buf)
140*7c478bd9Sstevel@tonic-gate 	single         *number;
141*7c478bd9Sstevel@tonic-gate 	int             ndigit, trailing;
142*7c478bd9Sstevel@tonic-gate 	char           *buf;
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	decimal_mode    dm;
145*7c478bd9Sstevel@tonic-gate 	decimal_record  dr;
146*7c478bd9Sstevel@tonic-gate 	fp_exception_field_type fef;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	dm.rd = fp_direction;
149*7c478bd9Sstevel@tonic-gate 	dm.df = floating_form;
150*7c478bd9Sstevel@tonic-gate 	dm.ndigits = ndigit;
151*7c478bd9Sstevel@tonic-gate 	single_to_decimal(number, &dm, &dr, &fef);
152*7c478bd9Sstevel@tonic-gate 	_gcvt(ndigit, &dr, trailing, buf);
153*7c478bd9Sstevel@tonic-gate 	return (buf);
154*7c478bd9Sstevel@tonic-gate }
155