llvm.org GIT mirror llvm / 1870f29
Separate out parsing of decimal number. Use this to only allocate memory for the significand once up-front. Also ignore insignificant trailing zeroes; this saves unnecessary multiplications later. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42964 91177308-0d34-0410-b5e6-96231b3b80d8 Neil Booth 13 years ago
1 changed file(s) with 154 addition(s) and 101 deletion(s). Raw diff Collapse all Expand all
7979 return ((bits) + integerPartWidth - 1) / integerPartWidth;
8080 }
8181
82 unsigned int
83 digitValue(unsigned int c)
82 /* Returns 0U-9U. Return values >= 10U are not digits. */
83 inline unsigned int
84 decDigitValue(unsigned int c)
8485 {
85 unsigned int r;
86
87 r = c - '0';
88 if(r <= 9)
89 return r;
90
91 return -1U;
86 return c - '0';
9287 }
9388
9489 unsigned int
109104 return r + 10;
110105
111106 return -1U;
107 }
108
109 /* Return the value of a decimal exponent of the form
110 [+-]ddddddd.
111
112 If the exponent overflows, returns a large exponent with the
113 appropriate sign. */
114 static int
115 readExponent(const char *p)
116 {
117 bool isNegative;
118 unsigned int absExponent;
119 const unsigned int overlargeExponent = 24000; /* FIXME. */
120
121 isNegative = (*p == '-');
122 if (*p == '-' || *p == '+')
123 p++;
124
125 absExponent = decDigitValue(*p++);
126 assert (absExponent < 10U);
127
128 for (;;) {
129 unsigned int value;
130
131 value = decDigitValue(*p);
132 if (value >= 10U)
133 break;
134
135 p++;
136 value += absExponent * 10;
137 if (absExponent >= overlargeExponent) {
138 absExponent = overlargeExponent;
139 break;
140 }
141 absExponent = value;
142 }
143
144 if (isNegative)
145 return -(int) absExponent;
146 else
147 return (int) absExponent;
112148 }
113149
114150 /* This is ugly and needs cleaning up, but I don't immediately see
131167 for(;;) {
132168 unsigned int value;
133169
134 value = digitValue(*p);
135 if(value == -1U)
170 value = decDigitValue(*p);
171 if(value >= 10U)
136172 break;
137173
138174 p++;
173209 }
174210
175211 return p;
212 }
213
214 /* Given a normal decimal floating point number of the form
215
216 dddd.dddd[eE][+-]ddd
217
218 where the decimal point and exponent are optional, fill out the
219 structure D. If the value is zero, V->firstSigDigit
220 points to a zero, and the return exponent is zero. */
221 struct decimalInfo {
222 const char *firstSigDigit;
223 const char *lastSigDigit;
224 int exponent;
225 };
226
227 void
228 interpretDecimal(const char *p, decimalInfo *D)
229 {
230 const char *dot;
231
232 p = skipLeadingZeroesAndAnyDot (p, &dot);
233
234 D->firstSigDigit = p;
235 D->exponent = 0;
236
237 for (;;) {
238 if (*p == '.') {
239 assert(dot == 0);
240 dot = p++;
241 }
242 if (decDigitValue(*p) >= 10U)
243 break;
244 p++;
245 }
246
247 /* If number is all zerooes accept any exponent. */
248 if (p != D->firstSigDigit) {
249 if (*p == 'e' || *p == 'E')
250 D->exponent = readExponent(p + 1);
251
252 /* Implied decimal point? */
253 if (!dot)
254 dot = p;
255
256 /* Drop insignificant trailing zeroes. */
257 do
258 do
259 p--;
260 while (*p == '0');
261 while (*p == '.');
262
263 /* Adjust the specified exponent for any decimal point. */
264 D->exponent += (dot - p) - (dot > p);
265 }
266
267 D->lastSigDigit = p;
176268 }
177269
178270 /* Return the trailing fraction of a hexadecimal number.
19802072 APFloat::opStatus
19812073 APFloat::convertFromDecimalString(const char *p, roundingMode rounding_mode)
19822074 {
1983 const char *dot, *firstSignificantDigit;
1984 integerPart val, maxVal, decValue;
2075 decimalInfo D;
19852076 opStatus fs;
19862077
1987 /* Skip leading zeroes and any decimal point. */
1988 p = skipLeadingZeroesAndAnyDot(p, &dot);
1989 firstSignificantDigit = p;
1990
1991 /* The maximum number that can be multiplied by ten with any digit
1992 added without overflowing an integerPart. */
1993 maxVal = (~ (integerPart) 0 - 9) / 10;
1994
1995 val = 0;
1996 while (val <= maxVal) {
1997 if (*p == '.') {
1998 assert(dot == 0);
1999 dot = p++;
2000 }
2001
2002 decValue = digitValue(*p);
2003 if (decValue == -1U)
2004 break;
2005 p++;
2006 val = val * 10 + decValue;
2007 }
2008
2009 integerPart *decSignificand;
2010 unsigned int partCount, maxPartCount;
2011
2012 partCount = 0;
2013 maxPartCount = 4;
2014 decSignificand = new integerPart[maxPartCount];
2015 decSignificand[partCount++] = val;
2016
2017 /* Now continue to do single-part arithmetic for as long as we can.
2018 Then do a part multiplication, and repeat. */
2019 while (decValue != -1U) {
2020 integerPart multiplier;
2021
2022 val = 0;
2023 multiplier = 1;
2024
2025 while (multiplier <= maxVal) {
2026 if (*p == '.') {
2027 assert(dot == 0);
2028 dot = p++;
2029 }
2030
2031 decValue = digitValue(*p);
2032 if (decValue == -1U)
2033 break;
2034 p++;
2035 multiplier *= 10;
2036 val = val * 10 + decValue;
2037 }
2038
2039 if (partCount == maxPartCount) {
2040 integerPart *newDecSignificand;
2041 newDecSignificand = new integerPart[maxPartCount = partCount * 2];
2042 APInt::tcAssign(newDecSignificand, decSignificand, partCount);
2043 delete [] decSignificand;
2044 decSignificand = newDecSignificand;
2045 }
2046
2047 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2048 partCount, partCount + 1, false);
2049
2050 /* If we used another part (likely), increase the count. */
2051 if (decSignificand[partCount] != 0)
2052 partCount++;
2053 }
2054
2055 /* Now decSignificand contains the supplied significand ignoring the
2056 decimal point. Figure out our effective exponent, which is the
2057 specified exponent adjusted for any decimal point. */
2058
2059 if (p == firstSignificantDigit) {
2060 /* Ignore the exponent if we are zero - we cannot overflow. */
2078 /* Scan the text. */
2079 interpretDecimal(p, &D);
2080
2081 if (*D.firstSigDigit == '0') {
20612082 category = fcZero;
20622083 fs = opOK;
20632084 } else {
2064 int decimalExponent;
2065
2066 if (dot)
2067 decimalExponent = dot + 1 - p;
2068 else
2069 decimalExponent = 0;
2070
2071 /* Add the given exponent. */
2072 if (*p == 'e' || *p == 'E')
2073 decimalExponent = totalExponent(p, decimalExponent);
2085 integerPart *decSignificand;
2086 unsigned int partCount;
2087
2088 /* A tight upper bound on number of bits required to hold an
2089 N-digit decimal integer is N * 256 / 77. Allocate enough space
2090 to hold the full significand, and an extra part required by
2091 tcMultiplyPart. */
2092 partCount = (D.lastSigDigit - D.firstSigDigit) + 1;
2093 partCount = partCountForBits(1 + 256 * partCount / 77);
2094 decSignificand = new integerPart[partCount + 1];
2095 partCount = 0;
2096
2097 /* Convert to binary efficiently - we do almost all multiplication
2098 in an integerPart. When this would overflow do we do a single
2099 bignum multiplication, and then revert again to multiplication
2100 in an integerPart. */
2101 do {
2102 integerPart decValue, val, multiplier;
2103
2104 val = 0;
2105 multiplier = 1;
2106
2107 do {
2108 if (*p == '.')
2109 p++;
2110
2111 decValue = decDigitValue(*p++);
2112 multiplier *= 10;
2113 val = val * 10 + decValue;
2114 /* The maximum number that can be multiplied by ten with any
2115 digit added without overflowing an integerPart. */
2116 } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10);
2117
2118 /* Multiply out the current part. */
2119 APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val,
2120 partCount, partCount + 1, false);
2121
2122 /* If we used another part (likely but not guaranteed), increase
2123 the count. */
2124 if (decSignificand[partCount])
2125 partCount++;
2126 } while (p <= D.lastSigDigit);
20742127
20752128 category = fcNormal;
20762129 fs = roundSignificandWithExponent(decSignificand, partCount,
2077 decimalExponent, rounding_mode);
2078 }
2079
2080 delete [] decSignificand;
2130 D.exponent, rounding_mode);
2131
2132 delete [] decSignificand;
2133 }
20812134
20822135 return fs;
20832136 }