diff options
-rw-r--r-- | include/mupdf/fitz/string.h | 1 | ||||
-rw-r--r-- | platform/win32/libmupdf.vcproj | 12 | ||||
-rw-r--r-- | platform/windows/libmupdf_winRT.vcxproj.filters | 5 | ||||
-rw-r--r-- | scripts/cmapdump.c | 3 | ||||
-rw-r--r-- | source/fitz/ftoa.c | 327 | ||||
-rw-r--r-- | source/fitz/printf.c | 2 | ||||
-rw-r--r-- | source/fitz/strtod.c (renamed from source/fitz/dtoa.c) | 310 |
7 files changed, 343 insertions, 317 deletions
diff --git a/include/mupdf/fitz/string.h b/include/mupdf/fitz/string.h index ee5f85a7..b726cf71 100644 --- a/include/mupdf/fitz/string.h +++ b/include/mupdf/fitz/string.h @@ -96,5 +96,6 @@ double fz_strtod(const char *s, char **es); Assumes special cases (NaN, +Inf, -Inf) have been handled. */ void fz_dtoa(double f, char *s, int *exp, int *neg, int *ns); +void fz_ftoa(float f, char *s, int *exp, int *neg, int *ns); #endif diff --git a/platform/win32/libmupdf.vcproj b/platform/win32/libmupdf.vcproj index 89e0ac46..c96042f8 100644 --- a/platform/win32/libmupdf.vcproj +++ b/platform/win32/libmupdf.vcproj @@ -486,10 +486,6 @@ > </File> <File - RelativePath="..\..\source\fitz\dtoa.c" - > - </File> - <File RelativePath="..\..\source\fitz\error.c" > </File> @@ -530,6 +526,10 @@ > </File> <File + RelativePath="..\..\source\fitz\ftoa.c" + > + </File> + <File RelativePath="..\..\source\fitz\function.c" > </File> @@ -670,6 +670,10 @@ > </File> <File + RelativePath="..\..\source\fitz\strtod.c" + > + </File> + <File RelativePath="..\..\source\fitz\svg-device.c" > </File> diff --git a/platform/windows/libmupdf_winRT.vcxproj.filters b/platform/windows/libmupdf_winRT.vcxproj.filters index b7793b33..b65301f4 100644 --- a/platform/windows/libmupdf_winRT.vcxproj.filters +++ b/platform/windows/libmupdf_winRT.vcxproj.filters @@ -429,7 +429,10 @@ <ClCompile Include="..\..\source\xps\xps-zip.c"> <Filter>xps</Filter> </ClCompile> - <ClCompile Include="..\..\source\fitz\dtoa.c"> + <ClCompile Include="..\..\source\fitz\strtod.c"> + <Filter>fitz</Filter> + </ClCompile> + <ClCompile Include="..\..\source\fitz\ftoa.c"> <Filter>fitz</Filter> </ClCompile> </ItemGroup> diff --git a/scripts/cmapdump.c b/scripts/cmapdump.c index 529264e9..3251c701 100644 --- a/scripts/cmapdump.c +++ b/scripts/cmapdump.c @@ -15,7 +15,8 @@ #include "../source/fitz/buffer.c" #include "../source/fitz/stream-open.c" #include "../source/fitz/stream-read.c" -#include "../source/fitz/dtoa.c" +#include "../source/fitz/strtod.c" +#include "../source/fitz/ftoa.c" #include "../source/fitz/printf.c" #include "../source/pdf/pdf-lex.c" diff --git a/source/fitz/ftoa.c b/source/fitz/ftoa.c new file mode 100644 index 00000000..dc3ce73b --- /dev/null +++ b/source/fitz/ftoa.c @@ -0,0 +1,327 @@ +/* The authors of this software are Rob Pike and Ken Thompson. + * Copyright (c) 2002 by Lucent Technologies. + * Permission to use, copy, modify, and distribute this software for any + * purpose without fee is hereby granted, provided that this entire notice + * is included in all copies of any software which is or includes a copy + * or modification of this software and in all copies of the supporting + * documentation for such software. + * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED + * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY + * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY + * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. + */ + +#include "mupdf/fitz.h" + +#include <stdio.h> +#include <math.h> +#include <float.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> + +#ifndef INFINITY +#define INFINITY (DBL_MAX+DBL_MAX) +#endif +#ifndef NAN +#define NAN (INFINITY-INFINITY) +#endif + +typedef unsigned long ulong; + +enum { NSIGNIF = 9 }; + +/* + * first few powers of 10 + */ +static float pows10[] = +{ + 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, + 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, + 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29, + 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39, +}; +#define npows10 ((int)(sizeof(pows10)/sizeof(pows10[0]))) +#define pow10(x) fmtpow10(x) + +static float +pow10(int n) +{ + float d; + int neg; + + neg = 0; + if(n < 0){ + neg = 1; + n = -n; + } + + if(n < npows10) + d = pows10[n]; + else{ + d = pows10[npows10-1]; + for(;;){ + n -= npows10 - 1; + if(n < npows10){ + d *= pows10[n]; + break; + } + d *= pows10[npows10 - 1]; + } + } + if(neg) + return 1./d; + return d; +} + +/* + * add 1 to the decimal integer string a of length n. + * if 99999 overflows into 10000, return 1 to tell caller + * to move the virtual decimal point. + */ +static int +xadd1(char *a, int n) +{ + char *b; + int c; + + if(n < 0 || n > NSIGNIF) + return 0; + for(b = a+n-1; b >= a; b--) { + c = *b + 1; + if(c <= '9') { + *b = c; + return 0; + } + *b = '0'; + } + /* + * need to overflow adding digit. + * shift number down and insert 1 at beginning. + * decimal is known to be 0s or we wouldn't + * have gotten this far. (e.g., 99999+1 => 00000) + */ + a[0] = '1'; + return 1; +} + +/* + * subtract 1 from the decimal integer string a. + * if 10000 underflows into 09999, make it 99999 + * and return 1 to tell caller to move the virtual + * decimal point. this way, xsub1 is inverse of xadd1. + */ +static int +xsub1(char *a, int n) +{ + char *b; + int c; + + if(n < 0 || n > NSIGNIF) + return 0; + for(b = a+n-1; b >= a; b--) { + c = *b - 1; + if(c >= '0') { + if(c == '0' && b == a) { + /* + * just zeroed the top digit; shift everyone up. + * decimal is known to be 9s or we wouldn't + * have gotten this far. (e.g., 10000-1 => 09999) + */ + *b = '9'; + return 1; + } + *b = c; + return 0; + } + *b = '9'; + } + /* + * can't get here. the number a is always normalized + * so that it has a nonzero first digit. + */ + return 0; +} + +/* + * format exponent like sprintf(p, "e%+d", e) + */ +static void +fmtexp(char *p, int e) +{ + char se[9]; + int i; + + *p++ = 'e'; + if(e < 0) { + *p++ = '-'; + e = -e; + } else + *p++ = '+'; + i = 0; + while(e) { + se[i++] = e % 10 + '0'; + e /= 10; + } + while(i < 1) + se[i++] = '0'; + while(i > 0) + *p++ = se[--i]; + *p++ = '\0'; +} + +/* + * compute decimal integer m, exp such that: + * f = m*10^exp + * m is as short as possible with losing exactness + * assumes special cases (NaN, +Inf, -Inf) have been handled. + */ +void +fz_ftoa(float f, char *s, int *exp, int *neg, int *ns) +{ + int c, d, e2, e, ee, i, ndigit, oerrno; + char tmp[NSIGNIF+10]; + float g; + + oerrno = errno; /* in case strtod smashes errno */ + + /* + * make f non-negative. + */ + *neg = 0; + if(f < 0) { + f = -f; + *neg = 1; + } + + /* + * must handle zero specially. + */ + if(f == 0){ + *exp = 0; + s[0] = '0'; + s[1] = '\0'; + *ns = 1; + return; + } + + /* + * find g,e such that f = g*10^e. + * guess 10-exponent using 2-exponent, then fine tune. + */ + frexpf(f, &e2); + e = (int)(e2 * .301029995664f); + g = f * pow10(-e); + while(g < 1) { + e--; + g = f * pow10(-e); + } + while(g >= 10) { + e++; + g = f * pow10(-e); + } + + /* + * convert NSIGNIF digits as a first approximation. + */ + for(i=0; i<NSIGNIF; i++) { + d = (int)g; + s[i] = d+'0'; + g = (g-d) * 10; + } + s[i] = 0; + + /* + * adjust e because s is 314159... not 3.14159... + */ + e -= NSIGNIF-1; + fmtexp(s+NSIGNIF, e); + + /* + * adjust conversion until strtod(s) == f exactly. + */ + for(i=0; i<10; i++) { + g = fz_strtod(s, NULL); + if(f > g) { + if(xadd1(s, NSIGNIF)) { + /* gained a digit */ + e--; + fmtexp(s+NSIGNIF, e); + } + continue; + } + if(f < g) { + if(xsub1(s, NSIGNIF)) { + /* lost a digit */ + e++; + fmtexp(s+NSIGNIF, e); + } + continue; + } + break; + } + + /* + * play with the decimal to try to simplify. + */ + + /* + * bump last few digits up to 9 if we can + */ + for(i=NSIGNIF-1; i>=NSIGNIF-3; i--) { + c = s[i]; + if(c != '9') { + s[i] = '9'; + g = fz_strtod(s, NULL); + if(g != f) { + s[i] = c; + break; + } + } + } + + /* + * add 1 in hopes of turning 9s to 0s + */ + if(s[NSIGNIF-1] == '9') { + strcpy(tmp, s); + ee = e; + if(xadd1(tmp, NSIGNIF)) { + ee--; + fmtexp(tmp+NSIGNIF, ee); + } + g = fz_strtod(tmp, NULL); + if(g == f) { + strcpy(s, tmp); + e = ee; + } + } + + /* + * bump last few digits down to 0 as we can. + */ + for(i=NSIGNIF-1; i>=NSIGNIF-3; i--) { + c = s[i]; + if(c != '0') { + s[i] = '0'; + g = fz_strtod(s, NULL); + if(g != f) { + s[i] = c; + break; + } + } + } + + /* + * remove trailing zeros. + */ + ndigit = NSIGNIF; + while(ndigit > 1 && s[ndigit-1] == '0'){ + e++; + --ndigit; + } + s[ndigit] = 0; + *exp = e; + *ns = ndigit; + errno = oerrno; +} diff --git a/source/fitz/printf.c b/source/fitz/printf.c index 8a429db6..8681ac1c 100644 --- a/source/fitz/printf.c +++ b/source/fitz/printf.c @@ -26,7 +26,7 @@ static void fmtfloat(struct fmtbuf *out, float f) if (isnan(f)) f = 0; if (isinf(f)) f = f < 0 ? -FLT_MAX : FLT_MAX; - fz_dtoa(f, digits, &exp, &neg, &ndigits); + fz_ftoa(f, digits, &exp, &neg, &ndigits); point = exp + ndigits; if (neg) diff --git a/source/fitz/dtoa.c b/source/fitz/strtod.c index cb7f081b..f548b6f3 100644 --- a/source/fitz/dtoa.c +++ b/source/fitz/strtod.c @@ -29,316 +29,6 @@ typedef unsigned long ulong; -enum { NSIGNIF = 17 }; - -/* - * first few powers of 10, enough for about 1/2 of the - * total space for doubles. - */ -static double pows10[] = -{ - 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, - 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, - 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29, - 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39, - 1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49, - 1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59, - 1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69, - 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79, - 1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88, 1e89, - 1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97, 1e98, 1e99, - 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109, - 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119, - 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129, - 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139, - 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149, - 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159, -}; -#define npows10 ((int)(sizeof(pows10)/sizeof(pows10[0]))) -#define pow10(x) fmtpow10(x) - -static double -pow10(int n) -{ - double d; - int neg; - - neg = 0; - if(n < 0){ - neg = 1; - n = -n; - } - - if(n < npows10) - d = pows10[n]; - else{ - d = pows10[npows10-1]; - for(;;){ - n -= npows10 - 1; - if(n < npows10){ - d *= pows10[n]; - break; - } - d *= pows10[npows10 - 1]; - } - } - if(neg) - return 1./d; - return d; -} - -/* - * add 1 to the decimal integer string a of length n. - * if 99999 overflows into 10000, return 1 to tell caller - * to move the virtual decimal point. - */ -static int -xadd1(char *a, int n) -{ - char *b; - int c; - - if(n < 0 || n > NSIGNIF) - return 0; - for(b = a+n-1; b >= a; b--) { - c = *b + 1; - if(c <= '9') { - *b = c; - return 0; - } - *b = '0'; - } - /* - * need to overflow adding digit. - * shift number down and insert 1 at beginning. - * decimal is known to be 0s or we wouldn't - * have gotten this far. (e.g., 99999+1 => 00000) - */ - a[0] = '1'; - return 1; -} - -/* - * subtract 1 from the decimal integer string a. - * if 10000 underflows into 09999, make it 99999 - * and return 1 to tell caller to move the virtual - * decimal point. this way, xsub1 is inverse of xadd1. - */ -static int -xsub1(char *a, int n) -{ - char *b; - int c; - - if(n < 0 || n > NSIGNIF) - return 0; - for(b = a+n-1; b >= a; b--) { - c = *b - 1; - if(c >= '0') { - if(c == '0' && b == a) { - /* - * just zeroed the top digit; shift everyone up. - * decimal is known to be 9s or we wouldn't - * have gotten this far. (e.g., 10000-1 => 09999) - */ - *b = '9'; - return 1; - } - *b = c; - return 0; - } - *b = '9'; - } - /* - * can't get here. the number a is always normalized - * so that it has a nonzero first digit. - */ - return 0; -} - -/* - * format exponent like sprintf(p, "e%+d", e) - */ -static void -fmtexp(char *p, int e) -{ - char se[9]; - int i; - - *p++ = 'e'; - if(e < 0) { - *p++ = '-'; - e = -e; - } else - *p++ = '+'; - i = 0; - while(e) { - se[i++] = e % 10 + '0'; - e /= 10; - } - while(i < 1) - se[i++] = '0'; - while(i > 0) - *p++ = se[--i]; - *p++ = '\0'; -} - -/* - * compute decimal integer m, exp such that: - * f = m*10^exp - * m is as short as possible with losing exactness - * assumes special cases (NaN, +Inf, -Inf) have been handled. - */ -void -fz_dtoa(double f, char *s, int *exp, int *neg, int *ns) -{ - int c, d, e2, e, ee, i, ndigit, oerrno; - char tmp[NSIGNIF+10]; - double g; - - oerrno = errno; /* in case strtod smashes errno */ - - /* - * make f non-negative. - */ - *neg = 0; - if(f < 0) { - f = -f; - *neg = 1; - } - - /* - * must handle zero specially. - */ - if(f == 0){ - *exp = 0; - s[0] = '0'; - s[1] = '\0'; - *ns = 1; - return; - } - - /* - * find g,e such that f = g*10^e. - * guess 10-exponent using 2-exponent, then fine tune. - */ - frexp(f, &e2); - e = (int)(e2 * .301029995664); - g = f * pow10(-e); - while(g < 1) { - e--; - g = f * pow10(-e); - } - while(g >= 10) { - e++; - g = f * pow10(-e); - } - - /* - * convert NSIGNIF digits as a first approximation. - */ - for(i=0; i<NSIGNIF; i++) { - d = (int)g; - s[i] = d+'0'; - g = (g-d) * 10; - } - s[i] = 0; - - /* - * adjust e because s is 314159... not 3.14159... - */ - e -= NSIGNIF-1; - fmtexp(s+NSIGNIF, e); - - /* - * adjust conversion until strtod(s) == f exactly. - */ - for(i=0; i<10; i++) { - g = fz_strtod(s, NULL); - if(f > g) { - if(xadd1(s, NSIGNIF)) { - /* gained a digit */ - e--; - fmtexp(s+NSIGNIF, e); - } - continue; - } - if(f < g) { - if(xsub1(s, NSIGNIF)) { - /* lost a digit */ - e++; - fmtexp(s+NSIGNIF, e); - } - continue; - } - break; - } - - /* - * play with the decimal to try to simplify. - */ - - /* - * bump last few digits up to 9 if we can - */ - for(i=NSIGNIF-1; i>=NSIGNIF-3; i--) { - c = s[i]; - if(c != '9') { - s[i] = '9'; - g = fz_strtod(s, NULL); - if(g != f) { - s[i] = c; - break; - } - } - } - - /* - * add 1 in hopes of turning 9s to 0s - */ - if(s[NSIGNIF-1] == '9') { - strcpy(tmp, s); - ee = e; - if(xadd1(tmp, NSIGNIF)) { - ee--; - fmtexp(tmp+NSIGNIF, ee); - } - g = fz_strtod(tmp, NULL); - if(g == f) { - strcpy(s, tmp); - e = ee; - } - } - - /* - * bump last few digits down to 0 as we can. - */ - for(i=NSIGNIF-1; i>=NSIGNIF-3; i--) { - c = s[i]; - if(c != '0') { - s[i] = '0'; - g = fz_strtod(s, NULL); - if(g != f) { - s[i] = c; - break; - } - } - } - - /* - * remove trailing zeros. - */ - ndigit = NSIGNIF; - while(ndigit > 1 && s[ndigit-1] == '0'){ - e++; - --ndigit; - } - s[ndigit] = 0; - *exp = e; - *ns = ndigit; - errno = oerrno; -} - static ulong umuldiv(ulong a, ulong b, ulong c) { |