Use MSGT_DECVIDEO in a video decoder.
[mplayer/glamo.git] / libass / ass_strtod.c
blob7b73630bc38fb22ba513fa9a72c32a63ab2cc46a
1 /*
2 * Copyright (c) 1988-1993 The Regents of the University of California.
3 * Copyright (c) 1994 Sun Microsystems, Inc.
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies. The University of California
9 * makes no representations about the suitability of this
10 * software for any purpose. It is provided "as is" without
11 * express or implied warranty.
15 #include <stdlib.h>
16 #include <ctype.h>
17 #include <errno.h>
19 static int maxExponent = 511; /* Largest possible base 10 exponent. Any
20 * exponent larger than this will already
21 * produce underflow or overflow, so there's
22 * no need to worry about additional digits.
25 static double powersOf10[] = { /* Table giving binary powers of 10. Entry */
26 10., /* is 10^2^i. Used to convert decimal */
27 100., /* exponents into floating-point numbers. */
28 1.0e4,
29 1.0e8,
30 1.0e16,
31 1.0e32,
32 1.0e64,
33 1.0e128,
34 1.0e256
38 *----------------------------------------------------------------------
40 * strtod --
42 * This procedure converts a floating-point number from an ASCII
43 * decimal representation to internal double-precision format.
45 * Results:
46 * The return value is the double-precision floating-point
47 * representation of the characters in string. If endPtr isn't
48 * NULL, then *endPtr is filled in with the address of the
49 * next character after the last one that was part of the
50 * floating-point number.
52 * Side effects:
53 * None.
55 *----------------------------------------------------------------------
58 double
59 ass_strtod(string, endPtr)
60 const char *string; /* A decimal ASCII floating-point number,
61 * optionally preceded by white space.
62 * Must have form "-I.FE-X", where I is the
63 * integer part of the mantissa, F is the
64 * fractional part of the mantissa, and X
65 * is the exponent. Either of the signs
66 * may be "+", "-", or omitted. Either I
67 * or F may be omitted, or both. The decimal
68 * point isn't necessary unless F is present.
69 * The "E" may actually be an "e". E and X
70 * may both be omitted (but not just one).
72 char **endPtr; /* If non-NULL, store terminating character's
73 * address here. */
75 int sign, expSign = 0;
76 double fraction, dblExp, *d;
77 register const char *p;
78 register int c;
79 int exp = 0; /* Exponent read from "EX" field. */
80 int fracExp = 0; /* Exponent that derives from the fractional
81 * part. Under normal circumstatnces, it is
82 * the negative of the number of digits in F.
83 * However, if I is very long, the last digits
84 * of I get dropped (otherwise a long I with a
85 * large negative exponent could cause an
86 * unnecessary overflow on I alone). In this
87 * case, fracExp is incremented one for each
88 * dropped digit. */
89 int mantSize; /* Number of digits in mantissa. */
90 int decPt; /* Number of mantissa digits BEFORE decimal
91 * point. */
92 const char *pExp; /* Temporarily holds location of exponent
93 * in string. */
96 * Strip off leading blanks and check for a sign.
99 p = string;
100 while (isspace(*p)) {
101 p += 1;
103 if (*p == '-') {
104 sign = 1;
105 p += 1;
106 } else {
107 if (*p == '+') {
108 p += 1;
110 sign = 0;
114 * Count the number of digits in the mantissa (including the decimal
115 * point), and also locate the decimal point.
118 decPt = -1;
119 for (mantSize = 0; ; mantSize += 1)
121 c = *p;
122 if (!isdigit(c)) {
123 if ((c != '.') || (decPt >= 0)) {
124 break;
126 decPt = mantSize;
128 p += 1;
132 * Now suck up the digits in the mantissa. Use two integers to
133 * collect 9 digits each (this is faster than using floating-point).
134 * If the mantissa has more than 18 digits, ignore the extras, since
135 * they can't affect the value anyway.
138 pExp = p;
139 p -= mantSize;
140 if (decPt < 0) {
141 decPt = mantSize;
142 } else {
143 mantSize -= 1; /* One of the digits was the point. */
145 if (mantSize > 18) {
146 fracExp = decPt - 18;
147 mantSize = 18;
148 } else {
149 fracExp = decPt - mantSize;
151 if (mantSize == 0) {
152 fraction = 0.0;
153 p = string;
154 goto done;
155 } else {
156 int frac1, frac2;
157 frac1 = 0;
158 for ( ; mantSize > 9; mantSize -= 1)
160 c = *p;
161 p += 1;
162 if (c == '.') {
163 c = *p;
164 p += 1;
166 frac1 = 10*frac1 + (c - '0');
168 frac2 = 0;
169 for (; mantSize > 0; mantSize -= 1)
171 c = *p;
172 p += 1;
173 if (c == '.') {
174 c = *p;
175 p += 1;
177 frac2 = 10*frac2 + (c - '0');
179 fraction = (1.0e9 * frac1) + frac2;
183 * Skim off the exponent.
186 p = pExp;
187 if ((*p == 'E') || (*p == 'e')) {
188 p += 1;
189 if (*p == '-') {
190 expSign = 1;
191 p += 1;
192 } else {
193 if (*p == '+') {
194 p += 1;
196 expSign = 0;
198 while (isdigit(*p)) {
199 exp = exp * 10 + (*p - '0');
200 p += 1;
203 if (expSign) {
204 exp = fracExp - exp;
205 } else {
206 exp = fracExp + exp;
210 * Generate a floating-point number that represents the exponent.
211 * Do this by processing the exponent one bit at a time to combine
212 * many powers of 2 of 10. Then combine the exponent with the
213 * fraction.
216 if (exp < 0) {
217 expSign = 1;
218 exp = -exp;
219 } else {
220 expSign = 0;
222 if (exp > maxExponent) {
223 exp = maxExponent;
224 errno = ERANGE;
226 dblExp = 1.0;
227 for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
228 if (exp & 01) {
229 dblExp *= *d;
232 if (expSign) {
233 fraction /= dblExp;
234 } else {
235 fraction *= dblExp;
238 done:
239 if (endPtr != NULL) {
240 *endPtr = (char *) p;
243 if (sign) {
244 return -fraction;
246 return fraction;