xref: /onnv-gate/usr/src/lib/libc/port/gen/ecvt.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  *	ecvt converts to decimal
340Sstevel@tonic-gate  *	the number of digits is specified by ndigit
350Sstevel@tonic-gate  *	decpt is set to the position of the decimal point
360Sstevel@tonic-gate  *	sign is set to 0 for positive, 1 for negative
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
40*6812Sraf #pragma weak _ecvt = ecvt
41*6812Sraf #pragma weak _fcvt = fcvt
42*6812Sraf 
43*6812Sraf #include "lint.h"
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <values.h>
460Sstevel@tonic-gate #include <nan.h>
470Sstevel@tonic-gate #include <string.h>
480Sstevel@tonic-gate #include "tsd.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	NMAX	((DSIGNIF * 3 + 19)/10) /* restrict max precision */
510Sstevel@tonic-gate #define	NDIG	80
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static char *cvt(double, int, int *, int *, int);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate char *
ecvt(double value,int ndigit,int * decpt,int * sign)560Sstevel@tonic-gate ecvt(double value, int ndigit, int *decpt, int *sign)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	return (cvt(value, ndigit, decpt, sign, 0));
590Sstevel@tonic-gate }
600Sstevel@tonic-gate 
610Sstevel@tonic-gate char *
fcvt(double value,int ndigit,int * decpt,int * sign)620Sstevel@tonic-gate fcvt(double value, int ndigit, int *decpt, int *sign)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	return (cvt(value, ndigit, decpt, sign, 1));
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static char *
cvt(double value,int ndigit,int * decpt,int * sign,int f_flag)680Sstevel@tonic-gate cvt(double value, int ndigit, int *decpt, int *sign, int f_flag)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	char *buf = tsdalloc(_T_ECVT, NDIG, NULL);
710Sstevel@tonic-gate 	char *p = &buf[0], *p_last = &buf[ndigit];
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	buf[0] = '\0';
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (IsNANorINF(value)) {
760Sstevel@tonic-gate 		if (IsINF(value))  /* value is an INF, return "inf" */
770Sstevel@tonic-gate 			(void) strncpy(buf, "inf", NDIG);
780Sstevel@tonic-gate 		else /* value is a NaN, return "NaN" */
790Sstevel@tonic-gate 			(void) strncpy(buf, "nan", NDIG);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 		return (buf);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if ((*sign = (value < 0.0)) != 0)
850Sstevel@tonic-gate 		value = -value;
860Sstevel@tonic-gate 	*decpt = 0;
870Sstevel@tonic-gate 	if (value != 0.0) {
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * rescale to range [1.0, 10.0)
900Sstevel@tonic-gate  * in binary for speed and to minimize error build-up
910Sstevel@tonic-gate  * even for the IEEE standard with its high exponents,
920Sstevel@tonic-gate  *  it's probably better for speed to just loop on them
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate 		static const struct s { double p10; int n; } s[] = {
950Sstevel@tonic-gate 			1e32,	32,
960Sstevel@tonic-gate 			1e16,	16,
970Sstevel@tonic-gate 			1e8,	8,
980Sstevel@tonic-gate 			1e4,	4,
990Sstevel@tonic-gate 			1e2,	2,
1000Sstevel@tonic-gate 			1e1,	1,
1010Sstevel@tonic-gate 		};
1020Sstevel@tonic-gate 		const struct s *sp = s;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 		++*decpt;
1050Sstevel@tonic-gate 		if (value >= 2.0 * MAXPOWTWO) /* can't be precisely integral */
1060Sstevel@tonic-gate 			do {
1070Sstevel@tonic-gate 				for (; value >= sp->p10; *decpt += sp->n)
1080Sstevel@tonic-gate 					value /= sp->p10;
1090Sstevel@tonic-gate 			} while (sp++->n > 1);
1100Sstevel@tonic-gate 		else if (value >= 10.0) { /* convert integer part separately */
1110Sstevel@tonic-gate 			double pow10 = 10.0, powtemp;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 			while ((powtemp = 10.0 * pow10) <= value)
1140Sstevel@tonic-gate 				pow10 = powtemp;
1150Sstevel@tonic-gate 			for (; ; pow10 /= 10.0) {
1160Sstevel@tonic-gate 				int digit = value/pow10;
1170Sstevel@tonic-gate 				*p++ = digit + '0';
1180Sstevel@tonic-gate 				value -= digit * pow10;
1190Sstevel@tonic-gate 				++*decpt;
1200Sstevel@tonic-gate 				if (pow10 <= 10.0)
1210Sstevel@tonic-gate 					break;
1220Sstevel@tonic-gate 			}
1230Sstevel@tonic-gate 		} else if (value < 1.0)
1240Sstevel@tonic-gate 			do {
1250Sstevel@tonic-gate 				for (; value * sp->p10 < 10.0; *decpt -= sp->n)
1260Sstevel@tonic-gate 					value *= sp->p10;
1270Sstevel@tonic-gate 			} while (sp++->n > 1);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	if (f_flag)
1300Sstevel@tonic-gate 		p_last += *decpt;
1310Sstevel@tonic-gate 	if (p_last >= buf) {
1320Sstevel@tonic-gate 		if (p_last > &buf[NDIG - 2])
1330Sstevel@tonic-gate 			p_last = &buf[NDIG - 2];
1340Sstevel@tonic-gate 		for (; ; ++p) {
1350Sstevel@tonic-gate 			if (value == 0 || p >= &buf[NMAX])
1360Sstevel@tonic-gate 				*p = '0';
1370Sstevel@tonic-gate 			else {
1380Sstevel@tonic-gate 				int intx; /* intx in [0, 9] */
1390Sstevel@tonic-gate 				*p = (intx = (int)value) + '0';
1400Sstevel@tonic-gate 				value = 10.0 * (value - (double)intx);
1410Sstevel@tonic-gate 			}
1420Sstevel@tonic-gate 			if (p >= p_last) {
1430Sstevel@tonic-gate 				p = p_last;
1440Sstevel@tonic-gate 				break;
1450Sstevel@tonic-gate 			}
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 		if (*p >= '5') /* check rounding in last place + 1 */
1480Sstevel@tonic-gate 			do {
1490Sstevel@tonic-gate 				if (p == buf) { /* rollover from 99999... */
1500Sstevel@tonic-gate 					buf[0] = '1'; /* later digits are 0 */
1510Sstevel@tonic-gate 					++*decpt;
1520Sstevel@tonic-gate 					if (f_flag)
1530Sstevel@tonic-gate 						++p_last;
1540Sstevel@tonic-gate 					break;
1550Sstevel@tonic-gate 				}
1560Sstevel@tonic-gate 				*p = '0';
1570Sstevel@tonic-gate 			} while (++*--p > '9'); /* propagate carries left */
1580Sstevel@tonic-gate 		*p_last = '\0';
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	return (buf);
1610Sstevel@tonic-gate }
162