xref: /titanic_51/usr/src/lib/libc/port/gen/ecvt.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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 /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *	ecvt converts to decimal
347c478bd9Sstevel@tonic-gate  *	the number of digits is specified by ndigit
357c478bd9Sstevel@tonic-gate  *	decpt is set to the position of the decimal point
367c478bd9Sstevel@tonic-gate  *	sign is set to 0 for positive, 1 for negative
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
40*7257d1b4Sraf #pragma weak _ecvt = ecvt
41*7257d1b4Sraf #pragma weak _fcvt = fcvt
42*7257d1b4Sraf 
43*7257d1b4Sraf #include "lint.h"
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <values.h>
467c478bd9Sstevel@tonic-gate #include <nan.h>
477c478bd9Sstevel@tonic-gate #include <string.h>
487c478bd9Sstevel@tonic-gate #include "tsd.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	NMAX	((DSIGNIF * 3 + 19)/10) /* restrict max precision */
517c478bd9Sstevel@tonic-gate #define	NDIG	80
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static char *cvt(double, int, int *, int *, int);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate char *
567c478bd9Sstevel@tonic-gate ecvt(double value, int ndigit, int *decpt, int *sign)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	return (cvt(value, ndigit, decpt, sign, 0));
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate char *
627c478bd9Sstevel@tonic-gate fcvt(double value, int ndigit, int *decpt, int *sign)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	return (cvt(value, ndigit, decpt, sign, 1));
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static char *
687c478bd9Sstevel@tonic-gate cvt(double value, int ndigit, int *decpt, int *sign, int f_flag)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	char *buf = tsdalloc(_T_ECVT, NDIG, NULL);
717c478bd9Sstevel@tonic-gate 	char *p = &buf[0], *p_last = &buf[ndigit];
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	buf[0] = '\0';
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (IsNANorINF(value)) {
767c478bd9Sstevel@tonic-gate 		if (IsINF(value))  /* value is an INF, return "inf" */
777c478bd9Sstevel@tonic-gate 			(void) strncpy(buf, "inf", NDIG);
787c478bd9Sstevel@tonic-gate 		else /* value is a NaN, return "NaN" */
797c478bd9Sstevel@tonic-gate 			(void) strncpy(buf, "nan", NDIG);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 		return (buf);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if ((*sign = (value < 0.0)) != 0)
857c478bd9Sstevel@tonic-gate 		value = -value;
867c478bd9Sstevel@tonic-gate 	*decpt = 0;
877c478bd9Sstevel@tonic-gate 	if (value != 0.0) {
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * rescale to range [1.0, 10.0)
907c478bd9Sstevel@tonic-gate  * in binary for speed and to minimize error build-up
917c478bd9Sstevel@tonic-gate  * even for the IEEE standard with its high exponents,
927c478bd9Sstevel@tonic-gate  *  it's probably better for speed to just loop on them
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 		static const struct s { double p10; int n; } s[] = {
957c478bd9Sstevel@tonic-gate 			1e32,	32,
967c478bd9Sstevel@tonic-gate 			1e16,	16,
977c478bd9Sstevel@tonic-gate 			1e8,	8,
987c478bd9Sstevel@tonic-gate 			1e4,	4,
997c478bd9Sstevel@tonic-gate 			1e2,	2,
1007c478bd9Sstevel@tonic-gate 			1e1,	1,
1017c478bd9Sstevel@tonic-gate 		};
1027c478bd9Sstevel@tonic-gate 		const struct s *sp = s;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		++*decpt;
1057c478bd9Sstevel@tonic-gate 		if (value >= 2.0 * MAXPOWTWO) /* can't be precisely integral */
1067c478bd9Sstevel@tonic-gate 			do {
1077c478bd9Sstevel@tonic-gate 				for (; value >= sp->p10; *decpt += sp->n)
1087c478bd9Sstevel@tonic-gate 					value /= sp->p10;
1097c478bd9Sstevel@tonic-gate 			} while (sp++->n > 1);
1107c478bd9Sstevel@tonic-gate 		else if (value >= 10.0) { /* convert integer part separately */
1117c478bd9Sstevel@tonic-gate 			double pow10 = 10.0, powtemp;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 			while ((powtemp = 10.0 * pow10) <= value)
1147c478bd9Sstevel@tonic-gate 				pow10 = powtemp;
1157c478bd9Sstevel@tonic-gate 			for (; ; pow10 /= 10.0) {
1167c478bd9Sstevel@tonic-gate 				int digit = value/pow10;
1177c478bd9Sstevel@tonic-gate 				*p++ = digit + '0';
1187c478bd9Sstevel@tonic-gate 				value -= digit * pow10;
1197c478bd9Sstevel@tonic-gate 				++*decpt;
1207c478bd9Sstevel@tonic-gate 				if (pow10 <= 10.0)
1217c478bd9Sstevel@tonic-gate 					break;
1227c478bd9Sstevel@tonic-gate 			}
1237c478bd9Sstevel@tonic-gate 		} else if (value < 1.0)
1247c478bd9Sstevel@tonic-gate 			do {
1257c478bd9Sstevel@tonic-gate 				for (; value * sp->p10 < 10.0; *decpt -= sp->n)
1267c478bd9Sstevel@tonic-gate 					value *= sp->p10;
1277c478bd9Sstevel@tonic-gate 			} while (sp++->n > 1);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	if (f_flag)
1307c478bd9Sstevel@tonic-gate 		p_last += *decpt;
1317c478bd9Sstevel@tonic-gate 	if (p_last >= buf) {
1327c478bd9Sstevel@tonic-gate 		if (p_last > &buf[NDIG - 2])
1337c478bd9Sstevel@tonic-gate 			p_last = &buf[NDIG - 2];
1347c478bd9Sstevel@tonic-gate 		for (; ; ++p) {
1357c478bd9Sstevel@tonic-gate 			if (value == 0 || p >= &buf[NMAX])
1367c478bd9Sstevel@tonic-gate 				*p = '0';
1377c478bd9Sstevel@tonic-gate 			else {
1387c478bd9Sstevel@tonic-gate 				int intx; /* intx in [0, 9] */
1397c478bd9Sstevel@tonic-gate 				*p = (intx = (int)value) + '0';
1407c478bd9Sstevel@tonic-gate 				value = 10.0 * (value - (double)intx);
1417c478bd9Sstevel@tonic-gate 			}
1427c478bd9Sstevel@tonic-gate 			if (p >= p_last) {
1437c478bd9Sstevel@tonic-gate 				p = p_last;
1447c478bd9Sstevel@tonic-gate 				break;
1457c478bd9Sstevel@tonic-gate 			}
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		if (*p >= '5') /* check rounding in last place + 1 */
1487c478bd9Sstevel@tonic-gate 			do {
1497c478bd9Sstevel@tonic-gate 				if (p == buf) { /* rollover from 99999... */
1507c478bd9Sstevel@tonic-gate 					buf[0] = '1'; /* later digits are 0 */
1517c478bd9Sstevel@tonic-gate 					++*decpt;
1527c478bd9Sstevel@tonic-gate 					if (f_flag)
1537c478bd9Sstevel@tonic-gate 						++p_last;
1547c478bd9Sstevel@tonic-gate 					break;
1557c478bd9Sstevel@tonic-gate 				}
1567c478bd9Sstevel@tonic-gate 				*p = '0';
1577c478bd9Sstevel@tonic-gate 			} while (++*--p > '9'); /* propagate carries left */
1587c478bd9Sstevel@tonic-gate 		*p_last = '\0';
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	return (buf);
1617c478bd9Sstevel@tonic-gate }
162