1 /* Copyright (c) 2005, Dmitry Xmelkov
2    All rights reserved.
3 
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions are met:
6 
7    * Redistributions of source code must retain the above copyright
8      notice, this list of conditions and the following disclaimer.
9    * Redistributions in binary form must reproduce the above copyright
10      notice, this list of conditions and the following disclaimer in
11      the documentation and/or other materials provided with the
12      distribution.
13    * Neither the name of the copyright holders nor the names of
14      contributors may be used to endorse or promote products derived
15      from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27   POSSIBILITY OF SUCH DAMAGE. */
28 
29 /* $Id: dtoa_prf.c 1944 2009-04-01 23:12:20Z arcanum $ */
30 
31 #include "ftoa_engine.h"
32 #include "dtoa_conv.h"
33 #include "sectionname.h"
34 
35 ATTRIBUTE_CLIB_SECTION
36 int
dtoa_prf(double val,char * s,unsigned char width,unsigned char prec,unsigned char flags)37 dtoa_prf (double val, char *s, unsigned char width, unsigned char prec,
38           unsigned char flags)
39 {
40     int exp;
41     int n;
42     unsigned char vtype;
43     unsigned char sign;
44     unsigned char ndigs;
45     unsigned char buf[9];
46 
47     ndigs = prec < 60 ? prec + 1 : 60;
48     exp = __ftoa_engine (val, (char *)buf, 7, ndigs);
49     vtype = buf[0];
50 
51     sign = 0;
52     if ((vtype & (FTOA_MINUS | FTOA_NAN)) == FTOA_MINUS)
53 	sign = '-';
54     else if (flags & DTOA_PLUS)
55 	sign = '+';
56     else if (flags & DTOA_SPACE)
57 	sign = ' ';
58 
59     if (vtype & FTOA_NAN) {
60 	ndigs = sign ? 4 : 3;
61 	width = (width > ndigs) ? width - ndigs : 0;
62 	if (!(flags & DTOA_LEFT)) {
63 	    while (width) {
64 		*s++ = ' ';
65 		width--;
66 	    }
67 	}
68 	if (sign) *s++ = sign;
69 	if (flags & DTOA_UPPER) {
70 	    *s++ = 'N';  *s++ = 'A';  *s++ = 'N';
71 	} else {
72 	    *s++ = 'n';  *s++ = 'a';  *s++ = 'n';
73 	}
74 	while (width) {
75 	    *s++ = ' ';
76 	    width--;
77 	}
78 	*s = 0;
79 	return DTOA_NONFINITE;
80     }
81 
82     if (vtype & FTOA_INF) {
83 	ndigs = sign ? 4 : 3;
84 	width = (width > ndigs) ? width - ndigs : 0;
85 	if (!(flags & DTOA_LEFT)) {
86 	    while (width) {
87 		*s++ = ' ';
88 		width--;
89 	    }
90 	}
91 	if (sign) *s++ = sign;
92 	if (flags & DTOA_UPPER) {
93 	    *s++ = 'I';  *s++ = 'N';  *s++ = 'F';
94 	} else {
95 	    *s++ = 'i';  *s++ = 'n';  *s++ = 'f';
96 	}
97 	while (width) {
98 	    *s++ = ' ';
99 	    width--;
100 	}
101 	*s = 0;
102 	return DTOA_NONFINITE;
103     }
104 
105     n = (sign ? 1 : 0) + (exp>0 ? exp+1 : 1) + (prec ? prec+1 : 0);
106     width = width > n ? width - n : 0;
107 
108     if (!(flags & DTOA_LEFT) && !(flags & DTOA_ZFILL)) {
109 	while (width) {
110 	    *s++ = ' ';
111 	    width--;
112 	}
113     }
114     if (sign) *s++ = sign;
115     if (!(flags & DTOA_LEFT)) {
116         while (width) {
117 	    *s++ = '0';
118 	    width--;
119 	}
120     }
121 
122     ndigs += exp;		/* exp is resticted approx. -40 .. +40	*/
123     sign = buf[1];
124     if ((vtype & FTOA_CARRY) && sign == '1')
125 	ndigs -= 1;
126     if ((signed char)ndigs < 1)
127 	ndigs = 1;
128     else if (ndigs > 8)
129 	ndigs = 8;
130 
131     n = exp > 0 ? exp : 0;
132     do {
133 	if (n == -1)
134 	    *s++ = '.';
135 	flags = (n <= exp && n > exp - ndigs) ? buf[exp - n + 1] : '0';
136 	if (--n < -prec)
137 	    break;
138 	*s++ = flags;
139     } while (1);
140     if ( n == exp && (sign > '5' || (sign == '5' && !(vtype & FTOA_CARRY))) )
141 	flags = '1';
142     *s++ = flags;
143 
144     while (width) {
145 	*s++ = ' ';
146 	width--;
147     }
148     *s++ = 0;
149 
150     return 0;
151 }
152 			/*** end of file ***/
153