17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*7257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
29*7257d1b4Sraf #include "lint.h"
307c478bd9Sstevel@tonic-gate #include "base_conversion.h"
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include "libc.h"
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate static void
__k_gconvert(int ndigits,decimal_record * pd,int trailing,char * buf)367c478bd9Sstevel@tonic-gate __k_gconvert(int ndigits, decimal_record *pd, int trailing, char *buf)
377c478bd9Sstevel@tonic-gate {
387c478bd9Sstevel@tonic-gate char *p;
397c478bd9Sstevel@tonic-gate int i;
407c478bd9Sstevel@tonic-gate char decpt = *(localeconv()->decimal_point);
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate p = buf;
437c478bd9Sstevel@tonic-gate if (pd->sign)
447c478bd9Sstevel@tonic-gate *(p++) = '-';
457c478bd9Sstevel@tonic-gate switch (pd->fpclass) {
467c478bd9Sstevel@tonic-gate case fp_zero:
477c478bd9Sstevel@tonic-gate *(p++) = '0';
487c478bd9Sstevel@tonic-gate if (trailing != 0) {
497c478bd9Sstevel@tonic-gate *(p++) = decpt;
507c478bd9Sstevel@tonic-gate for (i = 0; i < ndigits - 1; i++)
517c478bd9Sstevel@tonic-gate *(p++) = '0';
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate *p++ = 0;
547c478bd9Sstevel@tonic-gate break;
557c478bd9Sstevel@tonic-gate case fp_subnormal:
567c478bd9Sstevel@tonic-gate case fp_normal:
577c478bd9Sstevel@tonic-gate if ((pd->exponent > 0) || (pd->exponent < -(ndigits + 3))) {
587c478bd9Sstevel@tonic-gate /* E format. */
597c478bd9Sstevel@tonic-gate char estring[4];
607c478bd9Sstevel@tonic-gate int n;
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate *(p++) = pd->ds[0];
637c478bd9Sstevel@tonic-gate *(p++) = decpt;
647c478bd9Sstevel@tonic-gate for (i = 1; pd->ds[i] != 0; )
657c478bd9Sstevel@tonic-gate *(p++) = pd->ds[i++];
667c478bd9Sstevel@tonic-gate if (trailing == 0) {
677c478bd9Sstevel@tonic-gate /* Remove trailing zeros and . */
687c478bd9Sstevel@tonic-gate p--;
697c478bd9Sstevel@tonic-gate while (*p == '0')
707c478bd9Sstevel@tonic-gate p--;
717c478bd9Sstevel@tonic-gate if (*p != decpt)
727c478bd9Sstevel@tonic-gate p++;
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate *(p++) = 'e';
757c478bd9Sstevel@tonic-gate n = pd->exponent + i - 1;
767c478bd9Sstevel@tonic-gate if (n >= 0)
777c478bd9Sstevel@tonic-gate *(p++) = '+';
787c478bd9Sstevel@tonic-gate else {
797c478bd9Sstevel@tonic-gate *(p++) = '-';
807c478bd9Sstevel@tonic-gate n = -n;
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate __four_digits_quick((unsigned short) n, estring);
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /* Find end of zeros. */
857c478bd9Sstevel@tonic-gate for (i = 0; estring[i] == '0'; i++)
867c478bd9Sstevel@tonic-gate ;
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate if (i > 2)
897c478bd9Sstevel@tonic-gate i = 2; /* Guarantee two zeros. */
907c478bd9Sstevel@tonic-gate for (; i <= 3; )
917c478bd9Sstevel@tonic-gate *(p++) = estring[i++]; /* Copy exp digits. */
927c478bd9Sstevel@tonic-gate } else { /* F format. */
937c478bd9Sstevel@tonic-gate if (pd->exponent >= (1 - ndigits)) { /* x.xxx */
947c478bd9Sstevel@tonic-gate for (i = 0; i < (ndigits + pd->exponent); )
957c478bd9Sstevel@tonic-gate *(p++) = pd->ds[i++];
967c478bd9Sstevel@tonic-gate *(p++) = decpt;
977c478bd9Sstevel@tonic-gate if (pd->ds[i] != 0) {
987c478bd9Sstevel@tonic-gate /* More follows point. */
997c478bd9Sstevel@tonic-gate for (; i < ndigits; )
1007c478bd9Sstevel@tonic-gate *(p++) = pd->ds[i++];
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate } else { /* 0.00xxxx */
1037c478bd9Sstevel@tonic-gate *(p++) = '0';
1047c478bd9Sstevel@tonic-gate *(p++) = decpt;
1057c478bd9Sstevel@tonic-gate for (i = 0; i < -(pd->exponent + ndigits); i++)
1067c478bd9Sstevel@tonic-gate *(p++) = '0';
1077c478bd9Sstevel@tonic-gate for (i = 0; pd->ds[i] != 0; )
1087c478bd9Sstevel@tonic-gate *(p++) = pd->ds[i++];
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate if (trailing == 0) {
1117c478bd9Sstevel@tonic-gate /* Remove trailing zeros and point. */
1127c478bd9Sstevel@tonic-gate p--;
1137c478bd9Sstevel@tonic-gate while (*p == '0')
1147c478bd9Sstevel@tonic-gate p--;
1157c478bd9Sstevel@tonic-gate if (*p != decpt)
1167c478bd9Sstevel@tonic-gate p++;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate *(p++) = 0;
1207c478bd9Sstevel@tonic-gate break;
1217c478bd9Sstevel@tonic-gate default:
1227c478bd9Sstevel@tonic-gate __infnanstring(pd->fpclass, ndigits, p);
1237c478bd9Sstevel@tonic-gate break;
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate char *
gconvert(double number,int ndigits,int trailing,char * buf)1287c478bd9Sstevel@tonic-gate gconvert(double number, int ndigits, int trailing, char *buf)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate decimal_mode dm;
1317c478bd9Sstevel@tonic-gate decimal_record dr;
1327c478bd9Sstevel@tonic-gate fp_exception_field_type fef;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate #if defined(__sparc)
1357c478bd9Sstevel@tonic-gate dm.rd = _QgetRD();
1367c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
1377c478bd9Sstevel@tonic-gate dm.rd = __xgetRD();
1387c478bd9Sstevel@tonic-gate #else
1397c478bd9Sstevel@tonic-gate #error Unknown architecture
1407c478bd9Sstevel@tonic-gate #endif
1417c478bd9Sstevel@tonic-gate dm.df = floating_form;
1427c478bd9Sstevel@tonic-gate if (ndigits < 0)
1437c478bd9Sstevel@tonic-gate ndigits = 6;
1447c478bd9Sstevel@tonic-gate else if (ndigits == 0)
1457c478bd9Sstevel@tonic-gate ndigits = 1;
1467c478bd9Sstevel@tonic-gate else if (ndigits >= DECIMAL_STRING_LENGTH)
1477c478bd9Sstevel@tonic-gate ndigits = DECIMAL_STRING_LENGTH - 1;
1487c478bd9Sstevel@tonic-gate dm.ndigits = ndigits;
1497c478bd9Sstevel@tonic-gate double_to_decimal(&number, &dm, &dr, &fef);
1507c478bd9Sstevel@tonic-gate __k_gconvert(ndigits, &dr, trailing, buf);
1517c478bd9Sstevel@tonic-gate return (buf);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate char *
sgconvert(single * number,int ndigits,int trailing,char * buf)1557c478bd9Sstevel@tonic-gate sgconvert(single *number, int ndigits, int trailing, char *buf)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate decimal_mode dm;
1587c478bd9Sstevel@tonic-gate decimal_record dr;
1597c478bd9Sstevel@tonic-gate fp_exception_field_type fef;
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate #if defined(__sparc)
1627c478bd9Sstevel@tonic-gate dm.rd = _QgetRD();
1637c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
1647c478bd9Sstevel@tonic-gate dm.rd = __xgetRD();
1657c478bd9Sstevel@tonic-gate #else
1667c478bd9Sstevel@tonic-gate #error Unknown architecture
1677c478bd9Sstevel@tonic-gate #endif
1687c478bd9Sstevel@tonic-gate dm.df = floating_form;
1697c478bd9Sstevel@tonic-gate if (ndigits < 0)
1707c478bd9Sstevel@tonic-gate ndigits = 6;
1717c478bd9Sstevel@tonic-gate else if (ndigits == 0)
1727c478bd9Sstevel@tonic-gate ndigits = 1;
1737c478bd9Sstevel@tonic-gate else if (ndigits >= DECIMAL_STRING_LENGTH)
1747c478bd9Sstevel@tonic-gate ndigits = DECIMAL_STRING_LENGTH - 1;
1757c478bd9Sstevel@tonic-gate dm.ndigits = ndigits;
1767c478bd9Sstevel@tonic-gate single_to_decimal(number, &dm, &dr, &fef);
1777c478bd9Sstevel@tonic-gate __k_gconvert(ndigits, &dr, trailing, buf);
1787c478bd9Sstevel@tonic-gate return (buf);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate char *
qgconvert(quadruple * number,int ndigits,int trailing,char * buf)1827c478bd9Sstevel@tonic-gate qgconvert(quadruple *number, int ndigits, int trailing, char *buf)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate decimal_mode dm;
1857c478bd9Sstevel@tonic-gate decimal_record dr;
1867c478bd9Sstevel@tonic-gate fp_exception_field_type fef;
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate #if defined(__sparc)
1897c478bd9Sstevel@tonic-gate dm.rd = _QgetRD();
1907c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
1917c478bd9Sstevel@tonic-gate dm.rd = __xgetRD();
1927c478bd9Sstevel@tonic-gate #else
1937c478bd9Sstevel@tonic-gate #error Unknown architecture
1947c478bd9Sstevel@tonic-gate #endif
1957c478bd9Sstevel@tonic-gate dm.df = floating_form;
1967c478bd9Sstevel@tonic-gate if (ndigits < 0)
1977c478bd9Sstevel@tonic-gate ndigits = 6;
1987c478bd9Sstevel@tonic-gate else if (ndigits == 0)
1997c478bd9Sstevel@tonic-gate ndigits = 1;
2007c478bd9Sstevel@tonic-gate else if (ndigits >= DECIMAL_STRING_LENGTH)
2017c478bd9Sstevel@tonic-gate ndigits = DECIMAL_STRING_LENGTH - 1;
2027c478bd9Sstevel@tonic-gate dm.ndigits = ndigits;
2037c478bd9Sstevel@tonic-gate #if defined(__sparc)
2047c478bd9Sstevel@tonic-gate quadruple_to_decimal(number, &dm, &dr, &fef);
2057c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
2067c478bd9Sstevel@tonic-gate extended_to_decimal((extended *)number, &dm, &dr, &fef);
2077c478bd9Sstevel@tonic-gate #else
2087c478bd9Sstevel@tonic-gate #error Unknown architecture
2097c478bd9Sstevel@tonic-gate #endif
2107c478bd9Sstevel@tonic-gate __k_gconvert(ndigits, &dr, trailing, buf);
2117c478bd9Sstevel@tonic-gate return (buf);
2127c478bd9Sstevel@tonic-gate }
213