1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <mdb/mdb_stdlib.h>
28 #include <mdb/mdb_string.h>
29 #include <mdb/mdb_modapi.h>
30 #include <mdb/mdb_debug.h>
31 
32 #include <sys/types.h>
33 #include <floatingpoint.h>
34 #include <poll.h>
35 
36 /*
37  * Post-processing routine for econvert and qeconvert.  This function is
38  * called by both doubletos() and longdoubletos() below.
39  */
40 static const char *
fptos(const char * p,char * buf,size_t buflen,int decpt,int sign,char expchr)41 fptos(const char *p, char *buf, size_t buflen, int decpt, int sign, char expchr)
42 {
43 	char *q = buf;
44 
45 	*q++ = sign ? '-' : '+';
46 
47 	/*
48 	 * If the initial character is not a digit, the result is a special
49 	 * identifier such as "NaN" or "Inf"; just copy it verbatim.
50 	 */
51 	if (*p < '0' || *p > '9') {
52 		(void) strncpy(q, p, buflen);
53 		buf[buflen - 1] = '\0';
54 		return (buf);
55 	}
56 
57 	*q++ = *p++;
58 	*q++ = '.';
59 
60 	(void) strcpy(q, p);
61 	q += strlen(q);
62 	*q++ = expchr;
63 
64 	if (--decpt < 0) {
65 		decpt = -decpt;
66 		*q++ = '-';
67 	} else
68 		*q++ = '+';
69 
70 	if (decpt < 10)
71 		*q++ = '0';
72 
73 	(void) strcpy(q, numtostr((uint_t)decpt, 10, 0));
74 	return (buf);
75 }
76 
77 /*
78  * Convert the specified double to a string, and return a pointer to a static
79  * buffer containing the string value.  The double is converted using the
80  * same formatting conventions as sprintf(buf, "%+.*e", precision, d).  The
81  * expchr parameter specifies the character used to denote the exponent,
82  * and is usually 'e' or 'E'.
83  */
84 const char *
doubletos(double d,int precision,char expchr)85 doubletos(double d, int precision, char expchr)
86 {
87 	static char buf[DECIMAL_STRING_LENGTH];
88 	char digits[DECIMAL_STRING_LENGTH];
89 	int decpt, sign;
90 	char *p;
91 
92 	p = econvert(d, precision + 1, &decpt, &sign, digits);
93 	return (fptos(p, buf, sizeof (buf), decpt, sign, expchr));
94 }
95 
96 /*
97  * Same as doubletos(), but for long doubles (quad precision floating point).
98  */
99 const char *
longdoubletos(long double * ldp,int precision,char expchr)100 longdoubletos(long double *ldp, int precision, char expchr)
101 {
102 	static char buf[DECIMAL_STRING_LENGTH];
103 	char digits[DECIMAL_STRING_LENGTH];
104 	int decpt, sign;
105 	char *p;
106 
107 	p = qeconvert(ldp, precision + 1, &decpt, &sign, digits);
108 	return (fptos(p, buf, sizeof (buf), decpt, sign, expchr));
109 }
110