4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include "base_conversion.h"
31 #include <sys/types.h>
36 __k_gconvert(int ndigits
, decimal_record
*pd
, int trailing
, char *buf
)
40 char decpt
= *(localeconv()->decimal_point
);
45 switch (pd
->fpclass
) {
50 for (i
= 0; i
< ndigits
- 1; i
++)
57 if ((pd
->exponent
> 0) || (pd
->exponent
< -(ndigits
+ 3))) {
64 for (i
= 1; pd
->ds
[i
] != 0; )
67 /* Remove trailing zeros and . */
75 n
= pd
->exponent
+ i
- 1;
82 __four_digits_quick((unsigned short) n
, estring
);
84 /* Find end of zeros. */
85 for (i
= 0; estring
[i
] == '0'; i
++)
89 i
= 2; /* Guarantee two zeros. */
91 *(p
++) = estring
[i
++]; /* Copy exp digits. */
92 } else { /* F format. */
93 if (pd
->exponent
>= (1 - ndigits
)) { /* x.xxx */
94 for (i
= 0; i
< (ndigits
+ pd
->exponent
); )
98 /* More follows point. */
100 *(p
++) = pd
->ds
[i
++];
102 } else { /* 0.00xxxx */
105 for (i
= 0; i
< -(pd
->exponent
+ ndigits
); i
++)
107 for (i
= 0; pd
->ds
[i
] != 0; )
108 *(p
++) = pd
->ds
[i
++];
111 /* Remove trailing zeros and point. */
122 __infnanstring(pd
->fpclass
, ndigits
, p
);
128 gconvert(double number
, int ndigits
, int trailing
, char *buf
)
132 fp_exception_field_type fef
;
136 #elif defined(__i386) || defined(__amd64)
139 #error Unknown architecture
141 dm
.df
= floating_form
;
144 else if (ndigits
== 0)
146 else if (ndigits
>= DECIMAL_STRING_LENGTH
)
147 ndigits
= DECIMAL_STRING_LENGTH
- 1;
148 dm
.ndigits
= ndigits
;
149 double_to_decimal(&number
, &dm
, &dr
, &fef
);
150 __k_gconvert(ndigits
, &dr
, trailing
, buf
);
155 sgconvert(single
*number
, int ndigits
, int trailing
, char *buf
)
159 fp_exception_field_type fef
;
163 #elif defined(__i386) || defined(__amd64)
166 #error Unknown architecture
168 dm
.df
= floating_form
;
171 else if (ndigits
== 0)
173 else if (ndigits
>= DECIMAL_STRING_LENGTH
)
174 ndigits
= DECIMAL_STRING_LENGTH
- 1;
175 dm
.ndigits
= ndigits
;
176 single_to_decimal(number
, &dm
, &dr
, &fef
);
177 __k_gconvert(ndigits
, &dr
, trailing
, buf
);
182 qgconvert(quadruple
*number
, int ndigits
, int trailing
, char *buf
)
186 fp_exception_field_type fef
;
190 #elif defined(__i386) || defined(__amd64)
193 #error Unknown architecture
195 dm
.df
= floating_form
;
198 else if (ndigits
== 0)
200 else if (ndigits
>= DECIMAL_STRING_LENGTH
)
201 ndigits
= DECIMAL_STRING_LENGTH
- 1;
202 dm
.ndigits
= ndigits
;
204 quadruple_to_decimal(number
, &dm
, &dr
, &fef
);
205 #elif defined(__i386) || defined(__amd64)
206 extended_to_decimal((extended
*)number
, &dm
, &dr
, &fef
);
208 #error Unknown architecture
210 __k_gconvert(ndigits
, &dr
, trailing
, buf
);