├── .gitignore ├── LICENSE ├── Makefile.am ├── README ├── arithchk.c ├── autogen.sh ├── configure.ac ├── dmisc.c ├── dtoa.c ├── g_Qfmt.c ├── g__fmt.c ├── g_ddfmt.c ├── g_dfmt.c ├── g_ffmt.c ├── g_xLfmt.c ├── g_xfmt.c ├── gdtoa.c ├── gdtoa.h ├── gdtoaimp.h ├── gethex.c ├── gmisc.c ├── hd_init.c ├── hexnan.c ├── misc.c ├── qnan.c ├── smisc.c ├── strtoIQ.c ├── strtoId.c ├── strtoIdd.c ├── strtoIf.c ├── strtoIg.c ├── strtoIx.c ├── strtoIxL.c ├── strtod.c ├── strtodI.c ├── strtodg.c ├── strtodnrp.c ├── strtof.c ├── strtopQ.c ├── strtopd.c ├── strtopdd.c ├── strtopf.c ├── strtopx.c ├── strtopxL.c ├── strtorQ.c ├── strtord.c ├── strtordd.c ├── strtorf.c ├── strtorx.c ├── strtorxL.c ├── sum.c ├── test ├── Q.ou0 ├── Q.ou1 ├── Qtest.c ├── README ├── d.out ├── dI.out ├── dIsi.out ├── dItest.c ├── dd.out ├── ddsi.out ├── ddtest.c ├── dt.c ├── dtest.c ├── dtst.out ├── f.out ├── ftest.c ├── getround.c ├── makefile ├── rtestnos ├── strtoIdSI.c ├── strtoIddSI.c ├── strtodISI.c ├── strtodt.c ├── strtopddSI.c ├── strtorddSI.c ├── testnos ├── testnos1 ├── testnos3 ├── x.ou0 ├── x.ou1 ├── xL.ou0 ├── xL.ou1 ├── xLtest.c ├── xQtest.c ├── xsum0.out └── xtest.c ├── ulp.c └── xsum0.out /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.l[oa] 3 | *.[oa] 4 | /autom4te.cache 5 | /COPYING 6 | /Makefile.in 7 | /acconf.h.in 8 | /aclocal.m4 9 | /configure 10 | /m4/ 11 | /INSTALL 12 | /.deps/ 13 | /.libs/ 14 | /Makefile 15 | /acconf.h 16 | /arith.h 17 | /config.guess 18 | /config.log 19 | /config.status 20 | /config.sub 21 | /depcomp 22 | /gd_qnan.h 23 | /install-sh 24 | /libtool 25 | /ltmain.sh 26 | /missing 27 | /stamp-h1 28 | /test/Q.out 29 | /test/Qtest 30 | /test/dItest 31 | /test/dItestsi 32 | /test/ddtest 33 | /test/ddtestsi 34 | /test/dt 35 | /test/dtest 36 | /test/ftest 37 | /test/strtodt 38 | /test/strtodtnrp 39 | /test/tests 40 | /test/x.out 41 | /test/xL.out 42 | /test/xLtest 43 | /test/xtest 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The author of this software is David M. Gay. 2 | 3 | Copyright (C) 1998 by Lucent Technologies 4 | All Rights Reserved 5 | 6 | Permission to use, copy, modify, and distribute this software and 7 | its documentation for any purpose and without fee is hereby 8 | granted, provided that the above copyright notice appear in all 9 | copies and that both that the copyright notice and this 10 | permission notice and warranty disclaimer appear in supporting 11 | documentation, and that the name of Lucent or any of its entities 12 | not be used in advertising or publicity pertaining to 13 | distribution of the software without specific, written prior 14 | permission. 15 | 16 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 17 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 18 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 19 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 20 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 21 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 22 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 23 | THIS SOFTWARE. 24 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | lib_LTLIBRARIES = libgdtoa.la 2 | 3 | libgdtoa_la_LDFLAGS = -release 1.0 4 | libgdtoa_la_CPPFLAGS = -I$(top_builddir) 5 | libgdtoa_la_SOURCES = \ 6 | dmisc.c dtoa.c g_Qfmt.c g__fmt.c g_ddfmt.c g_dfmt.c g_ffmt.c \ 7 | g_xLfmt.c g_xfmt.c gdtoa.c gethex.c gmisc.c hd_init.c hexnan.c \ 8 | misc.c smisc.c strtoIQ.c strtoId.c strtoIdd.c strtoIf.c strtoIg.c \ 9 | strtoIx.c strtoIxL.c strtod.c strtodI.c strtodg.c strtof.c strtopQ.c \ 10 | strtopd.c strtopdd.c strtopf.c strtopx.c strtopxL.c strtorQ.c \ 11 | strtord.c strtordd.c strtorf.c strtorx.c strtorxL.c sum.c ulp.c 12 | 13 | EXTRA_libgdtoa_la_SOURCES = arithchk.c qnan.c 14 | 15 | BUILT_SOURCES = arith.h gd_qnan.h 16 | CLEANFILES = arith.h gd_qnan.h arithchk qnan 17 | EXTRA_DIST = LICENSE strtodnrp.c 18 | 19 | arith.h: arithchk.c 20 | $(CC) $(CFLAGS) -o $(top_builddir)/arithchk $< || \ 21 | $(CC) -DNO_LONG_LONG $(CFLAGS) -o $(top_builddir)/arithchk $< 22 | $(top_builddir)/arithchk > $(top_builddir)/$@ 23 | rm -f $(top_builddir)/arithchk 24 | -rm -fr $(top_builddir)/arithchk.dSYM # for Mac OS X 25 | 26 | gd_qnan.h: qnan.c arith.h 27 | $(CC) $(CFLAGS) -o $(top_builddir)/qnan -I$(top_builddir) $< 28 | $(top_builddir)/qnan > $(top_builddir)/$@ 29 | rm -f $(top_builddir)/qnan 30 | -rm -fr $(top_builddir)/qnan.dSYM # for Mac OS X 31 | 32 | pkginclude_HEADERS = gdtoa.h gdtoaimp.h 33 | -------------------------------------------------------------------------------- /arithchk.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | Copyright (C) 1997, 1998 Lucent Technologies 3 | All Rights Reserved 4 | 5 | Permission to use, copy, modify, and distribute this software and 6 | its documentation for any purpose and without fee is hereby 7 | granted, provided that the above copyright notice appear in all 8 | copies and that both that the copyright notice and this 9 | permission notice and warranty disclaimer appear in supporting 10 | documentation, and that the name of Lucent or any of its entities 11 | not be used in advertising or publicity pertaining to 12 | distribution of the software without specific, written prior 13 | permission. 14 | 15 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 17 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 18 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 20 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 21 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 22 | THIS SOFTWARE. 23 | ****************************************************************/ 24 | 25 | /* Try to deduce arith.h from arithmetic properties. */ 26 | 27 | #include 28 | 29 | static int dalign; 30 | typedef struct 31 | Akind { 32 | char *name; 33 | int kind; 34 | } Akind; 35 | 36 | static Akind 37 | IEEE_8087 = { "IEEE_8087", 1 }, 38 | IEEE_MC68k = { "IEEE_MC68k", 2 }, 39 | IBM = { "IBM", 3 }, 40 | VAX = { "VAX", 4 }, 41 | CRAY = { "CRAY", 5}; 42 | 43 | static Akind * 44 | Lcheck() 45 | { 46 | union { 47 | double d; 48 | long L[2]; 49 | } u; 50 | struct { 51 | double d; 52 | long L; 53 | } x[2]; 54 | 55 | if (sizeof(x) > 2*(sizeof(double) + sizeof(long))) 56 | dalign = 1; 57 | u.L[0] = u.L[1] = 0; 58 | u.d = 1e13; 59 | if (u.L[0] == 1117925532 && u.L[1] == -448790528) 60 | return &IEEE_MC68k; 61 | if (u.L[1] == 1117925532 && u.L[0] == -448790528) 62 | return &IEEE_8087; 63 | if (u.L[0] == -2065213935 && u.L[1] == 10752) 64 | return &VAX; 65 | if (u.L[0] == 1267827943 && u.L[1] == 704643072) 66 | return &IBM; 67 | return 0; 68 | } 69 | 70 | static Akind * 71 | icheck() 72 | { 73 | union { 74 | double d; 75 | int L[2]; 76 | } u; 77 | struct { 78 | double d; 79 | int L; 80 | } x[2]; 81 | 82 | if (sizeof(x) > 2*(sizeof(double) + sizeof(int))) 83 | dalign = 1; 84 | u.L[0] = u.L[1] = 0; 85 | u.d = 1e13; 86 | if (u.L[0] == 1117925532 && u.L[1] == -448790528) 87 | return &IEEE_MC68k; 88 | if (u.L[1] == 1117925532 && u.L[0] == -448790528) 89 | return &IEEE_8087; 90 | if (u.L[0] == -2065213935 && u.L[1] == 10752) 91 | return &VAX; 92 | if (u.L[0] == 1267827943 && u.L[1] == 704643072) 93 | return &IBM; 94 | return 0; 95 | } 96 | 97 | char *emptyfmt = ""; /* avoid possible warning message with printf("") */ 98 | 99 | static Akind * 100 | ccheck() 101 | { 102 | union { 103 | double d; 104 | long L; 105 | } u; 106 | long Cray1; 107 | 108 | /* Cray1 = 4617762693716115456 -- without overflow on non-Crays */ 109 | Cray1 = printf(emptyfmt) < 0 ? 0 : 4617762; 110 | if (printf(emptyfmt, Cray1) >= 0) 111 | Cray1 = 1000000*Cray1 + 693716; 112 | if (printf(emptyfmt, Cray1) >= 0) 113 | Cray1 = 1000000*Cray1 + 115456; 114 | u.d = 1e13; 115 | if (u.L == Cray1) 116 | return &CRAY; 117 | return 0; 118 | } 119 | 120 | static int 121 | fzcheck() 122 | { 123 | double a, b; 124 | int i; 125 | 126 | a = 1.; 127 | b = .1; 128 | for(i = 155;; b *= b, i >>= 1) { 129 | if (i & 1) { 130 | a *= b; 131 | if (i == 1) 132 | break; 133 | } 134 | } 135 | b = a * a; 136 | return b == 0.; 137 | } 138 | 139 | int 140 | main() 141 | { 142 | Akind *a = 0; 143 | int Ldef = 0; 144 | FILE *f; 145 | 146 | #ifdef WRITE_ARITH_H /* for Symantec's buggy "make" */ 147 | f = fopen("arith.h", "w"); 148 | if (!f) { 149 | printf("Cannot open arith.h\n"); 150 | return 1; 151 | } 152 | #else 153 | f = stdout; 154 | #endif 155 | 156 | if (sizeof(double) == 2*sizeof(long)) 157 | a = Lcheck(); 158 | else if (sizeof(double) == 2*sizeof(int)) { 159 | Ldef = 1; 160 | a = icheck(); 161 | } 162 | else if (sizeof(double) == sizeof(long)) 163 | a = ccheck(); 164 | if (a) { 165 | fprintf(f, "#define %s\n#define Arith_Kind_ASL %d\n", 166 | a->name, a->kind); 167 | if (Ldef) 168 | fprintf(f, "#define Long int\n#define Intcast (int)(long)\n"); 169 | if (dalign) 170 | fprintf(f, "#define Double_Align\n"); 171 | if (sizeof(char*) == 8) 172 | fprintf(f, "#define X64_bit_pointers\n"); 173 | #ifndef NO_LONG_LONG 174 | if (sizeof(long long) < 8) 175 | #endif 176 | fprintf(f, "#define NO_LONG_LONG\n"); 177 | if (a->kind <= 2 && fzcheck()) 178 | fprintf(f, "#define Sudden_Underflow\n"); 179 | return 0; 180 | } 181 | fprintf(f, "/* Unknown arithmetic */\n"); 182 | return 1; 183 | } 184 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ(2.61) 5 | 6 | AC_INIT([gdtoa],[1.0],[David M. Gay]) 7 | 8 | AC_CONFIG_AUX_DIR([.]) 9 | AM_INIT_AUTOMAKE([dist-bzip2 foreign]) 10 | 11 | AC_CONFIG_SRCDIR([gdtoa.c]) 12 | AC_CONFIG_HEADERS([acconf.h]) 13 | 14 | # Checks for programs. 15 | AC_PROG_MAKE_SET 16 | AC_PROG_LIBTOOL 17 | AM_PROG_LIBTOOL 18 | 19 | # Checks for header files. 20 | AC_HEADER_STDC 21 | AC_CHECK_HEADERS([math.h]) 22 | 23 | # Checks for typedefs, structures, and compiler characteristics. 24 | AC_TYPE_SIZE_T 25 | 26 | # Checks for library functions. 27 | AC_HEADER_STDC 28 | 29 | AC_CONFIG_FILES([Makefile]) 30 | AC_OUTPUT 31 | -------------------------------------------------------------------------------- /dmisc.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #ifndef MULTIPLE_THREADS 35 | char *dtoa_result; 36 | #endif 37 | 38 | char * 39 | #ifdef KR_headers 40 | rv_alloc(i) int i; 41 | #else 42 | rv_alloc(int i) 43 | #endif 44 | { 45 | int j, k, *r; 46 | 47 | j = sizeof(ULong); 48 | for(k = 0; 49 | sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i; 50 | j <<= 1) 51 | k++; 52 | r = (int*)Balloc(k); 53 | *r = k; 54 | return 55 | #ifndef MULTIPLE_THREADS 56 | dtoa_result = 57 | #endif 58 | (char *)(r+1); 59 | } 60 | 61 | char * 62 | #ifdef KR_headers 63 | nrv_alloc(s, rve, n) char *s, **rve; int n; 64 | #else 65 | nrv_alloc(char *s, char **rve, int n) 66 | #endif 67 | { 68 | char *rv, *t; 69 | 70 | t = rv = rv_alloc(n); 71 | while((*t = *s++) !=0) 72 | t++; 73 | if (rve) 74 | *rve = t; 75 | return rv; 76 | } 77 | 78 | /* freedtoa(s) must be used to free values s returned by dtoa 79 | * when MULTIPLE_THREADS is #defined. It should be used in all cases, 80 | * but for consistency with earlier versions of dtoa, it is optional 81 | * when MULTIPLE_THREADS is not defined. 82 | */ 83 | 84 | void 85 | #ifdef KR_headers 86 | freedtoa(s) char *s; 87 | #else 88 | freedtoa(char *s) 89 | #endif 90 | { 91 | Bigint *b = (Bigint *)((int *)s - 1); 92 | b->maxwds = 1 << (b->k = *(int*)b); 93 | Bfree(b); 94 | #ifndef MULTIPLE_THREADS 95 | if (s == dtoa_result) 96 | dtoa_result = 0; 97 | #endif 98 | } 99 | 100 | int 101 | quorem 102 | #ifdef KR_headers 103 | (b, S) Bigint *b, *S; 104 | #else 105 | (Bigint *b, Bigint *S) 106 | #endif 107 | { 108 | int n; 109 | ULong *bx, *bxe, q, *sx, *sxe; 110 | #ifdef ULLong 111 | ULLong borrow, carry, y, ys; 112 | #else 113 | ULong borrow, carry, y, ys; 114 | #ifdef Pack_32 115 | ULong si, z, zs; 116 | #endif 117 | #endif 118 | 119 | n = S->wds; 120 | #ifdef DEBUG 121 | /*debug*/ if (b->wds > n) 122 | /*debug*/ Bug("oversize b in quorem"); 123 | #endif 124 | if (b->wds < n) 125 | return 0; 126 | sx = S->x; 127 | sxe = sx + --n; 128 | bx = b->x; 129 | bxe = bx + n; 130 | q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ 131 | #ifdef DEBUG 132 | /*debug*/ if (q > 9) 133 | /*debug*/ Bug("oversized quotient in quorem"); 134 | #endif 135 | if (q) { 136 | borrow = 0; 137 | carry = 0; 138 | do { 139 | #ifdef ULLong 140 | ys = *sx++ * (ULLong)q + carry; 141 | carry = ys >> 32; 142 | y = *bx - (ys & 0xffffffffUL) - borrow; 143 | borrow = y >> 32 & 1UL; 144 | *bx++ = y & 0xffffffffUL; 145 | #else 146 | #ifdef Pack_32 147 | si = *sx++; 148 | ys = (si & 0xffff) * q + carry; 149 | zs = (si >> 16) * q + (ys >> 16); 150 | carry = zs >> 16; 151 | y = (*bx & 0xffff) - (ys & 0xffff) - borrow; 152 | borrow = (y & 0x10000) >> 16; 153 | z = (*bx >> 16) - (zs & 0xffff) - borrow; 154 | borrow = (z & 0x10000) >> 16; 155 | Storeinc(bx, z, y); 156 | #else 157 | ys = *sx++ * q + carry; 158 | carry = ys >> 16; 159 | y = *bx - (ys & 0xffff) - borrow; 160 | borrow = (y & 0x10000) >> 16; 161 | *bx++ = y & 0xffff; 162 | #endif 163 | #endif 164 | } 165 | while(sx <= sxe); 166 | if (!*bxe) { 167 | bx = b->x; 168 | while(--bxe > bx && !*bxe) 169 | --n; 170 | b->wds = n; 171 | } 172 | } 173 | if (cmp(b, S) >= 0) { 174 | q++; 175 | borrow = 0; 176 | carry = 0; 177 | bx = b->x; 178 | sx = S->x; 179 | do { 180 | #ifdef ULLong 181 | ys = *sx++ + carry; 182 | carry = ys >> 32; 183 | y = *bx - (ys & 0xffffffffUL) - borrow; 184 | borrow = y >> 32 & 1UL; 185 | *bx++ = y & 0xffffffffUL; 186 | #else 187 | #ifdef Pack_32 188 | si = *sx++; 189 | ys = (si & 0xffff) + carry; 190 | zs = (si >> 16) + (ys >> 16); 191 | carry = zs >> 16; 192 | y = (*bx & 0xffff) - (ys & 0xffff) - borrow; 193 | borrow = (y & 0x10000) >> 16; 194 | z = (*bx >> 16) - (zs & 0xffff) - borrow; 195 | borrow = (z & 0x10000) >> 16; 196 | Storeinc(bx, z, y); 197 | #else 198 | ys = *sx++ + carry; 199 | carry = ys >> 16; 200 | y = *bx - (ys & 0xffff) - borrow; 201 | borrow = (y & 0x10000) >> 16; 202 | *bx++ = y & 0xffff; 203 | #endif 204 | #endif 205 | } 206 | while(sx <= sxe); 207 | bx = b->x; 208 | bxe = bx + n; 209 | if (!*bxe) { 210 | while(--bxe > bx && !*bxe) 211 | --n; 212 | b->wds = n; 213 | } 214 | } 215 | return q; 216 | } 217 | -------------------------------------------------------------------------------- /g_Qfmt.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #undef _0 35 | #undef _1 36 | 37 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 38 | 39 | #ifdef IEEE_MC68k 40 | #define _0 0 41 | #define _1 1 42 | #define _2 2 43 | #define _3 3 44 | #endif 45 | #ifdef IEEE_8087 46 | #define _0 3 47 | #define _1 2 48 | #define _2 1 49 | #define _3 0 50 | #endif 51 | 52 | char* 53 | #ifdef KR_headers 54 | g_Qfmt(buf, V, ndig, bufsize) char *buf; char *V; int ndig; unsigned bufsize; 55 | #else 56 | g_Qfmt(char *buf, void *V, int ndig, unsigned bufsize) 57 | #endif 58 | { 59 | static FPI fpi = { 113, 1-16383-113+1, 32766 - 16383 - 113 + 1, 1, 0 }; 60 | char *b, *s, *se; 61 | ULong bits[4], *L, sign; 62 | int decpt, ex, i, mode; 63 | 64 | if (ndig < 0) 65 | ndig = 0; 66 | if (bufsize < ndig + 10) 67 | return 0; 68 | 69 | L = (ULong*)V; 70 | sign = L[_0] & 0x80000000L; 71 | bits[3] = L[_0] & 0xffff; 72 | bits[2] = L[_1]; 73 | bits[1] = L[_2]; 74 | bits[0] = L[_3]; 75 | b = buf; 76 | if ( (ex = (L[_0] & 0x7fff0000L) >> 16) !=0) { 77 | if (ex == 0x7fff) { 78 | /* Infinity or NaN */ 79 | if (bits[0] | bits[1] | bits[2] | bits[3]) 80 | b = strcp(b, "NaN"); 81 | else { 82 | b = buf; 83 | if (sign) 84 | *b++ = '-'; 85 | b = strcp(b, "Infinity"); 86 | } 87 | return b; 88 | } 89 | i = STRTOG_Normal; 90 | bits[3] |= 0x10000; 91 | } 92 | else if (bits[0] | bits[1] | bits[2] | bits[3]) { 93 | i = STRTOG_Denormal; 94 | ex = 1; 95 | } 96 | else { 97 | #ifndef IGNORE_ZERO_SIGN 98 | if (sign) 99 | *b++ = '-'; 100 | #endif 101 | *b++ = '0'; 102 | *b = 0; 103 | return b; 104 | } 105 | ex -= 0x3fff + 112; 106 | mode = 2; 107 | if (ndig <= 0) { 108 | if (bufsize < 48) 109 | return 0; 110 | mode = 0; 111 | } 112 | s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); 113 | return g__fmt(buf, s, se, decpt, sign); 114 | } 115 | -------------------------------------------------------------------------------- /g__fmt.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #ifdef USE_LOCALE 35 | #include "locale.h" 36 | #endif 37 | 38 | char * 39 | #ifdef KR_headers 40 | g__fmt(b, s, se, decpt, sign) char *b; char *s; char *se; int decpt; ULong sign; 41 | #else 42 | g__fmt(char *b, char *s, char *se, int decpt, ULong sign) 43 | #endif 44 | { 45 | int i, j, k; 46 | char *s0 = s; 47 | #ifdef USE_LOCALE 48 | char decimalpoint = *localeconv()->decimal_point; 49 | #else 50 | #define decimalpoint '.' 51 | #endif 52 | if (sign) 53 | *b++ = '-'; 54 | if (decpt <= -4 || decpt > se - s + 5) { 55 | *b++ = *s++; 56 | if (*s) { 57 | *b++ = decimalpoint; 58 | while((*b = *s++) !=0) 59 | b++; 60 | } 61 | *b++ = 'e'; 62 | /* sprintf(b, "%+.2d", decpt - 1); */ 63 | if (--decpt < 0) { 64 | *b++ = '-'; 65 | decpt = -decpt; 66 | } 67 | else 68 | *b++ = '+'; 69 | for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){} 70 | for(;;) { 71 | i = decpt / k; 72 | *b++ = i + '0'; 73 | if (--j <= 0) 74 | break; 75 | decpt -= i*k; 76 | decpt *= 10; 77 | } 78 | *b = 0; 79 | } 80 | else if (decpt <= 0) { 81 | *b++ = decimalpoint; 82 | for(; decpt < 0; decpt++) 83 | *b++ = '0'; 84 | while((*b = *s++) !=0) 85 | b++; 86 | } 87 | else { 88 | while((*b = *s++) !=0) { 89 | b++; 90 | if (--decpt == 0 && *s) 91 | *b++ = decimalpoint; 92 | } 93 | for(; decpt > 0; decpt--) 94 | *b++ = '0'; 95 | *b = 0; 96 | } 97 | freedtoa(s0); 98 | return b; 99 | } 100 | -------------------------------------------------------------------------------- /g_ddfmt.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg@acm.org). */ 30 | 31 | #include "gdtoaimp.h" 32 | #include 33 | 34 | char * 35 | #ifdef KR_headers 36 | g_ddfmt(buf, dd, ndig, bufsize) char *buf; double *dd; int ndig; unsigned bufsize; 37 | #else 38 | g_ddfmt(char *buf, double *dd, int ndig, unsigned bufsize) 39 | #endif 40 | { 41 | FPI fpi; 42 | char *b, *s, *se; 43 | ULong *L, bits0[4], *bits, *zx; 44 | int bx, by, decpt, ex, ey, i, j, mode; 45 | Bigint *x, *y, *z; 46 | double ddx[2]; 47 | 48 | if (bufsize < 10 || bufsize < ndig + 8) 49 | return 0; 50 | 51 | L = (ULong*)dd; 52 | if ((L[_0] & 0x7ff00000L) == 0x7ff00000L) { 53 | /* Infinity or NaN */ 54 | if (L[_0] & 0xfffff || L[_1]) { 55 | nanret: 56 | return strcp(buf, "NaN"); 57 | } 58 | if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) { 59 | if (L[2+_0] & 0xfffff || L[2+_1]) 60 | goto nanret; 61 | if ((L[_0] ^ L[2+_0]) & 0x80000000L) 62 | goto nanret; /* Infinity - Infinity */ 63 | } 64 | infret: 65 | b = buf; 66 | if (L[_0] & 0x80000000L) 67 | *b++ = '-'; 68 | return strcp(b, "Infinity"); 69 | } 70 | if ((L[2+_0] & 0x7ff00000) == 0x7ff00000) { 71 | L += 2; 72 | if (L[_0] & 0xfffff || L[_1]) 73 | goto nanret; 74 | goto infret; 75 | } 76 | if (dd[0] + dd[1] == 0.) { 77 | b = buf; 78 | #ifndef IGNORE_ZERO_SIGN 79 | if (L[_0] & L[2+_0] & 0x80000000L) 80 | *b++ = '-'; 81 | #endif 82 | *b++ = '0'; 83 | *b = 0; 84 | return b; 85 | } 86 | if ((L[_0] & 0x7ff00000L) < (L[2+_0] & 0x7ff00000L)) { 87 | ddx[1] = dd[0]; 88 | ddx[0] = dd[1]; 89 | dd = ddx; 90 | L = (ULong*)dd; 91 | } 92 | z = d2b(dd[0], &ex, &bx); 93 | if (dd[1] == 0.) 94 | goto no_y; 95 | x = z; 96 | y = d2b(dd[1], &ey, &by); 97 | if ( (i = ex - ey) !=0) { 98 | if (i > 0) { 99 | x = lshift(x, i); 100 | ex = ey; 101 | } 102 | else 103 | y = lshift(y, -i); 104 | } 105 | if ((L[_0] ^ L[2+_0]) & 0x80000000L) { 106 | z = diff(x, y); 107 | if (L[_0] & 0x80000000L) 108 | z->sign = 1 - z->sign; 109 | } 110 | else { 111 | z = sum(x, y); 112 | if (L[_0] & 0x80000000L) 113 | z->sign = 1; 114 | } 115 | Bfree(x); 116 | Bfree(y); 117 | no_y: 118 | bits = zx = z->x; 119 | for(i = 0; !*zx; zx++) 120 | i += 32; 121 | i += lo0bits(zx); 122 | if (i) { 123 | rshift(z, i); 124 | ex += i; 125 | } 126 | fpi.nbits = z->wds * 32 - hi0bits(z->x[j = z->wds-1]); 127 | if (fpi.nbits < 106) { 128 | fpi.nbits = 106; 129 | if (j < 3) { 130 | for(i = 0; i <= j; i++) 131 | bits0[i] = bits[i]; 132 | while(i < 4) 133 | bits0[i++] = 0; 134 | bits = bits0; 135 | } 136 | } 137 | mode = 2; 138 | if (ndig <= 0) { 139 | if (bufsize < (int)(fpi.nbits * .301029995664) + 10) { 140 | Bfree(z); 141 | return 0; 142 | } 143 | mode = 0; 144 | } 145 | fpi.emin = 1-1023-53+1; 146 | fpi.emax = 2046-1023-106+1; 147 | fpi.rounding = FPI_Round_near; 148 | fpi.sudden_underflow = 0; 149 | i = STRTOG_Normal; 150 | s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); 151 | b = g__fmt(buf, s, se, decpt, z->sign); 152 | Bfree(z); 153 | return b; 154 | } 155 | -------------------------------------------------------------------------------- /g_dfmt.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | char* 35 | #ifdef KR_headers 36 | g_dfmt(buf, d, ndig, bufsize) char *buf; double *d; int ndig; unsigned bufsize; 37 | #else 38 | g_dfmt(char *buf, double *d, int ndig, unsigned bufsize) 39 | #endif 40 | { 41 | static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, 0 }; 42 | char *b, *s, *se; 43 | ULong bits[2], *L, sign; 44 | int decpt, ex, i, mode; 45 | 46 | if (ndig < 0) 47 | ndig = 0; 48 | if (bufsize < ndig + 10) 49 | return 0; 50 | 51 | L = (ULong*)d; 52 | sign = L[_0] & 0x80000000L; 53 | if ((L[_0] & 0x7ff00000) == 0x7ff00000) { 54 | /* Infinity or NaN */ 55 | if (L[_0] & 0xfffff || L[_1]) { 56 | return strcp(buf, "NaN"); 57 | } 58 | b = buf; 59 | if (sign) 60 | *b++ = '-'; 61 | return strcp(b, "Infinity"); 62 | } 63 | if (L[_1] == 0 && (L[_0] ^ sign) == 0 /*d == 0.*/) { 64 | b = buf; 65 | #ifndef IGNORE_ZERO_SIGN 66 | if (L[_0] & 0x80000000L) 67 | *b++ = '-'; 68 | #endif 69 | *b++ = '0'; 70 | *b = 0; 71 | return b; 72 | } 73 | bits[0] = L[_1]; 74 | bits[1] = L[_0] & 0xfffff; 75 | if ( (ex = (L[_0] >> 20) & 0x7ff) !=0) 76 | bits[1] |= 0x100000; 77 | else 78 | ex = 1; 79 | ex -= 0x3ff + 52; 80 | mode = 2; 81 | if (ndig <= 0) { 82 | if (bufsize < 25) 83 | return 0; 84 | mode = 0; 85 | } 86 | i = STRTOG_Normal; 87 | s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); 88 | return g__fmt(buf, s, se, decpt, sign); 89 | } 90 | -------------------------------------------------------------------------------- /g_ffmt.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | char* 35 | #ifdef KR_headers 36 | g_ffmt(buf, f, ndig, bufsize) char *buf; float *f; int ndig; unsigned bufsize; 37 | #else 38 | g_ffmt(char *buf, float *f, int ndig, unsigned bufsize) 39 | #endif 40 | { 41 | static FPI fpi = { 24, 1-127-24+1, 254-127-24+1, 1, 0 }; 42 | char *b, *s, *se; 43 | ULong bits[1], *L, sign; 44 | int decpt, ex, i, mode; 45 | 46 | if (ndig < 0) 47 | ndig = 0; 48 | if (bufsize < ndig + 10) 49 | return 0; 50 | 51 | L = (ULong*)f; 52 | sign = L[0] & 0x80000000L; 53 | if ((L[0] & 0x7f800000) == 0x7f800000) { 54 | /* Infinity or NaN */ 55 | if (L[0] & 0x7fffff) { 56 | return strcp(buf, "NaN"); 57 | } 58 | b = buf; 59 | if (sign) 60 | *b++ = '-'; 61 | return strcp(b, "Infinity"); 62 | } 63 | if (*f == 0.) { 64 | b = buf; 65 | #ifndef IGNORE_ZERO_SIGN 66 | if (L[0] & 0x80000000L) 67 | *b++ = '-'; 68 | #endif 69 | *b++ = '0'; 70 | *b = 0; 71 | return b; 72 | } 73 | bits[0] = L[0] & 0x7fffff; 74 | if ( (ex = (L[0] >> 23) & 0xff) !=0) 75 | bits[0] |= 0x800000; 76 | else 77 | ex = 1; 78 | ex -= 0x7f + 23; 79 | mode = 2; 80 | if (ndig <= 0) { 81 | if (bufsize < 16) 82 | return 0; 83 | mode = 0; 84 | } 85 | i = STRTOG_Normal; 86 | s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); 87 | return g__fmt(buf, s, se, decpt, sign); 88 | } 89 | -------------------------------------------------------------------------------- /g_xLfmt.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #undef _0 35 | #undef _1 36 | 37 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 38 | 39 | #ifdef IEEE_MC68k 40 | #define _0 0 41 | #define _1 1 42 | #define _2 2 43 | #endif 44 | #ifdef IEEE_8087 45 | #define _0 2 46 | #define _1 1 47 | #define _2 0 48 | #endif 49 | 50 | char* 51 | #ifdef KR_headers 52 | g_xLfmt(buf, V, ndig, bufsize) char *buf; char *V; int ndig; unsigned bufsize; 53 | #else 54 | g_xLfmt(char *buf, void *V, int ndig, unsigned bufsize) 55 | #endif 56 | { 57 | static FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, 0 }; 58 | char *b, *s, *se; 59 | ULong bits[2], *L, sign; 60 | int decpt, ex, i, mode; 61 | 62 | if (ndig < 0) 63 | ndig = 0; 64 | if (bufsize < ndig + 10) 65 | return 0; 66 | 67 | L = (ULong*)V; 68 | sign = L[_0] & 0x80000000L; 69 | bits[1] = L[_1]; 70 | bits[0] = L[_2]; 71 | if ( (ex = (L[_0] >> 16) & 0x7fff) !=0) { 72 | if (ex == 0x7fff) { 73 | /* Infinity or NaN */ 74 | if (bits[0] | bits[1]) 75 | b = strcp(buf, "NaN"); 76 | else { 77 | b = buf; 78 | if (sign) 79 | *b++ = '-'; 80 | b = strcp(b, "Infinity"); 81 | } 82 | return b; 83 | } 84 | i = STRTOG_Normal; 85 | } 86 | else if (bits[0] | bits[1]) { 87 | i = STRTOG_Denormal; 88 | } 89 | else { 90 | b = buf; 91 | #ifndef IGNORE_ZERO_SIGN 92 | if (sign) 93 | *b++ = '-'; 94 | #endif 95 | *b++ = '0'; 96 | *b = 0; 97 | return b; 98 | } 99 | ex -= 0x3fff + 63; 100 | mode = 2; 101 | if (ndig <= 0) { 102 | if (bufsize < 32) 103 | return 0; 104 | mode = 0; 105 | } 106 | s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); 107 | return g__fmt(buf, s, se, decpt, sign); 108 | } 109 | -------------------------------------------------------------------------------- /g_xfmt.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #undef _0 35 | #undef _1 36 | 37 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 38 | 39 | #ifdef IEEE_MC68k 40 | #define _0 0 41 | #define _1 1 42 | #define _2 2 43 | #define _3 3 44 | #define _4 4 45 | #endif 46 | #ifdef IEEE_8087 47 | #define _0 4 48 | #define _1 3 49 | #define _2 2 50 | #define _3 1 51 | #define _4 0 52 | #endif 53 | 54 | char* 55 | #ifdef KR_headers 56 | g_xfmt(buf, V, ndig, bufsize) char *buf; char *V; int ndig; unsigned bufsize; 57 | #else 58 | g_xfmt(char *buf, void *V, int ndig, unsigned bufsize) 59 | #endif 60 | { 61 | static FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, 0 }; 62 | char *b, *s, *se; 63 | ULong bits[2], sign; 64 | UShort *L; 65 | int decpt, ex, i, mode; 66 | 67 | if (ndig < 0) 68 | ndig = 0; 69 | if (bufsize < ndig + 10) 70 | return 0; 71 | 72 | L = (UShort *)V; 73 | sign = L[_0] & 0x8000; 74 | bits[1] = (L[_1] << 16) | L[_2]; 75 | bits[0] = (L[_3] << 16) | L[_4]; 76 | if ( (ex = L[_0] & 0x7fff) !=0) { 77 | if (ex == 0x7fff) { 78 | /* Infinity or NaN */ 79 | if (bits[0] | bits[1]) 80 | b = strcp(buf, "NaN"); 81 | else { 82 | b = buf; 83 | if (sign) 84 | *b++ = '-'; 85 | b = strcp(b, "Infinity"); 86 | } 87 | return b; 88 | } 89 | i = STRTOG_Normal; 90 | } 91 | else if (bits[0] | bits[1]) { 92 | i = STRTOG_Denormal; 93 | ex = 1; 94 | } 95 | else { 96 | b = buf; 97 | #ifndef IGNORE_ZERO_SIGN 98 | if (sign) 99 | *b++ = '-'; 100 | #endif 101 | *b++ = '0'; 102 | *b = 0; 103 | return b; 104 | } 105 | ex -= 0x3fff + 63; 106 | mode = 2; 107 | if (ndig <= 0) { 108 | if (bufsize < 32) 109 | return 0; 110 | mode = 0; 111 | } 112 | s = gdtoa(&fpi, ex, bits, &i, mode, ndig, &decpt, &se); 113 | return g__fmt(buf, s, se, decpt, sign); 114 | } 115 | -------------------------------------------------------------------------------- /gdtoa.h: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #ifndef GDTOA_H_INCLUDED 33 | #define GDTOA_H_INCLUDED 34 | 35 | #include "arith.h" 36 | 37 | #ifndef Long 38 | #define Long long 39 | #endif 40 | #ifndef ULong 41 | typedef unsigned Long ULong; 42 | #endif 43 | #ifndef UShort 44 | typedef unsigned short UShort; 45 | #endif 46 | 47 | #ifndef ANSI 48 | #ifdef KR_headers 49 | #define ANSI(x) () 50 | #define Void /*nothing*/ 51 | #else 52 | #define ANSI(x) x 53 | #define Void void 54 | #endif 55 | #endif /* ANSI */ 56 | 57 | #ifndef CONST 58 | #ifdef KR_headers 59 | #define CONST /* blank */ 60 | #else 61 | #define CONST const 62 | #endif 63 | #endif /* CONST */ 64 | 65 | enum { /* return values from strtodg */ 66 | STRTOG_Zero = 0, 67 | STRTOG_Normal = 1, 68 | STRTOG_Denormal = 2, 69 | STRTOG_Infinite = 3, 70 | STRTOG_NaN = 4, 71 | STRTOG_NaNbits = 5, 72 | STRTOG_NoNumber = 6, 73 | STRTOG_Retmask = 7, 74 | 75 | /* The following may be or-ed into one of the above values. */ 76 | 77 | STRTOG_Neg = 0x08, 78 | STRTOG_Inexlo = 0x10, 79 | STRTOG_Inexhi = 0x20, 80 | STRTOG_Inexact = 0x30, 81 | STRTOG_Underflow= 0x40, 82 | STRTOG_Overflow = 0x80 83 | }; 84 | 85 | typedef struct 86 | FPI { 87 | int nbits; 88 | int emin; 89 | int emax; 90 | int rounding; 91 | int sudden_underflow; 92 | } FPI; 93 | 94 | enum { /* FPI.rounding values: same as FLT_ROUNDS */ 95 | FPI_Round_zero = 0, 96 | FPI_Round_near = 1, 97 | FPI_Round_up = 2, 98 | FPI_Round_down = 3 99 | }; 100 | 101 | #ifdef __cplusplus 102 | extern "C" { 103 | #endif 104 | 105 | extern char* dtoa ANSI((double d, int mode, int ndigits, int *decpt, 106 | int *sign, char **rve)); 107 | extern char* gdtoa ANSI((FPI *fpi, int be, ULong *bits, int *kindp, 108 | int mode, int ndigits, int *decpt, char **rve)); 109 | extern void freedtoa ANSI((char*)); 110 | extern float strtof ANSI((CONST char *, char **)); 111 | extern double strtod ANSI((CONST char *, char **)); 112 | extern int strtodg ANSI((CONST char*, char**, FPI*, Long*, ULong*)); 113 | 114 | extern char* g_ddfmt ANSI((char*, double*, int, unsigned)); 115 | extern char* g_dfmt ANSI((char*, double*, int, unsigned)); 116 | extern char* g_ffmt ANSI((char*, float*, int, unsigned)); 117 | extern char* g_Qfmt ANSI((char*, void*, int, unsigned)); 118 | extern char* g_xfmt ANSI((char*, void*, int, unsigned)); 119 | extern char* g_xLfmt ANSI((char*, void*, int, unsigned)); 120 | 121 | extern int strtoId ANSI((CONST char*, char**, double*, double*)); 122 | extern int strtoIdd ANSI((CONST char*, char**, double*, double*)); 123 | extern int strtoIf ANSI((CONST char*, char**, float*, float*)); 124 | extern int strtoIQ ANSI((CONST char*, char**, void*, void*)); 125 | extern int strtoIx ANSI((CONST char*, char**, void*, void*)); 126 | extern int strtoIxL ANSI((CONST char*, char**, void*, void*)); 127 | extern int strtord ANSI((CONST char*, char**, int, double*)); 128 | extern int strtordd ANSI((CONST char*, char**, int, double*)); 129 | extern int strtorf ANSI((CONST char*, char**, int, float*)); 130 | extern int strtorQ ANSI((CONST char*, char**, int, void*)); 131 | extern int strtorx ANSI((CONST char*, char**, int, void*)); 132 | extern int strtorxL ANSI((CONST char*, char**, int, void*)); 133 | #if 1 134 | extern int strtodI ANSI((CONST char*, char**, double*)); 135 | extern int strtopd ANSI((CONST char*, char**, double*)); 136 | extern int strtopdd ANSI((CONST char*, char**, double*)); 137 | extern int strtopf ANSI((CONST char*, char**, float*)); 138 | extern int strtopQ ANSI((CONST char*, char**, void*)); 139 | extern int strtopx ANSI((CONST char*, char**, void*)); 140 | extern int strtopxL ANSI((CONST char*, char**, void*)); 141 | #else 142 | #define strtopd(s,se,x) strtord(s,se,1,x) 143 | #define strtopdd(s,se,x) strtordd(s,se,1,x) 144 | #define strtopf(s,se,x) strtorf(s,se,1,x) 145 | #define strtopQ(s,se,x) strtorQ(s,se,1,x) 146 | #define strtopx(s,se,x) strtorx(s,se,1,x) 147 | #define strtopxL(s,se,x) strtorxL(s,se,1,x) 148 | #endif 149 | 150 | #ifdef __cplusplus 151 | } 152 | #endif 153 | #endif /* GDTOA_H_INCLUDED */ 154 | -------------------------------------------------------------------------------- /gethex.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #ifdef USE_LOCALE 35 | #include "locale.h" 36 | #endif 37 | 38 | int 39 | #ifdef KR_headers 40 | gethex(sp, fpi, exp, bp, sign) 41 | CONST char **sp; FPI *fpi; Long *exp; Bigint **bp; int sign; 42 | #else 43 | gethex( CONST char **sp, FPI *fpi, Long *exp, Bigint **bp, int sign) 44 | #endif 45 | { 46 | Bigint *b; 47 | CONST unsigned char *decpt, *s0, *s, *s1; 48 | int esign, havedig, irv, k, n, nbits, up, zret; 49 | ULong L, lostbits, *x; 50 | Long e, e1; 51 | #ifdef USE_LOCALE 52 | unsigned char decimalpoint = *localeconv()->decimal_point; 53 | #else 54 | #define decimalpoint '.' 55 | #endif 56 | 57 | if (!hexdig['0']) 58 | hexdig_init_D2A(); 59 | havedig = 0; 60 | s0 = *(CONST unsigned char **)sp + 2; 61 | while(s0[havedig] == '0') 62 | havedig++; 63 | s0 += havedig; 64 | s = s0; 65 | decpt = 0; 66 | zret = 0; 67 | e = 0; 68 | if (!hexdig[*s]) { 69 | zret = 1; 70 | if (*s != decimalpoint) 71 | goto pcheck; 72 | decpt = ++s; 73 | if (!hexdig[*s]) 74 | goto pcheck; 75 | while(*s == '0') 76 | s++; 77 | if (hexdig[*s]) 78 | zret = 0; 79 | havedig = 1; 80 | s0 = s; 81 | } 82 | while(hexdig[*s]) 83 | s++; 84 | if (*s == decimalpoint && !decpt) { 85 | decpt = ++s; 86 | while(hexdig[*s]) 87 | s++; 88 | } 89 | if (decpt) 90 | e = -(((Long)(s-decpt)) << 2); 91 | pcheck: 92 | s1 = s; 93 | switch(*s) { 94 | case 'p': 95 | case 'P': 96 | esign = 0; 97 | switch(*++s) { 98 | case '-': 99 | esign = 1; 100 | /* no break */ 101 | case '+': 102 | s++; 103 | } 104 | if ((n = hexdig[*s]) == 0 || n > 0x19) { 105 | s = s1; 106 | break; 107 | } 108 | e1 = n - 0x10; 109 | while((n = hexdig[*++s]) !=0 && n <= 0x19) 110 | e1 = 10*e1 + n - 0x10; 111 | if (esign) 112 | e1 = -e1; 113 | e += e1; 114 | } 115 | *sp = (char*)s; 116 | if (zret) { 117 | if (!havedig) 118 | *sp = (char*)s0 - 1; 119 | return STRTOG_Zero; 120 | } 121 | n = s1 - s0 - 1; 122 | for(k = 0; n > 7; n >>= 1) 123 | k++; 124 | b = Balloc(k); 125 | x = b->x; 126 | n = 0; 127 | L = 0; 128 | while(s1 > s0) { 129 | if (*--s1 == decimalpoint) 130 | continue; 131 | if (n == 32) { 132 | *x++ = L; 133 | L = 0; 134 | n = 0; 135 | } 136 | L |= (hexdig[*s1] & 0x0f) << n; 137 | n += 4; 138 | } 139 | *x++ = L; 140 | b->wds = n = x - b->x; 141 | n = 32*n - hi0bits(L); 142 | nbits = fpi->nbits; 143 | lostbits = 0; 144 | x = b->x; 145 | if (n > nbits) { 146 | n -= nbits; 147 | if (any_on(b,n)) { 148 | lostbits = 1; 149 | k = n - 1; 150 | if (x[k>>kshift] & 1 << (k & kmask)) { 151 | lostbits = 2; 152 | if (k > 1 && any_on(b,k-1)) 153 | lostbits = 3; 154 | } 155 | } 156 | rshift(b, n); 157 | e += n; 158 | } 159 | else if (n < nbits) { 160 | n = nbits - n; 161 | b = lshift(b, n); 162 | e -= n; 163 | x = b->x; 164 | } 165 | if (e > fpi->emax) { 166 | ovfl: 167 | Bfree(b); 168 | *bp = 0; 169 | return STRTOG_Infinite | STRTOG_Overflow | STRTOG_Inexhi; 170 | } 171 | irv = STRTOG_Normal; 172 | if (e < fpi->emin) { 173 | irv = STRTOG_Denormal; 174 | n = fpi->emin - e; 175 | if (n >= nbits) { 176 | switch (fpi->rounding) { 177 | case FPI_Round_near: 178 | if (n == nbits && (n < 2 || any_on(b,n-1))) 179 | goto one_bit; 180 | break; 181 | case FPI_Round_up: 182 | if (!sign) 183 | goto one_bit; 184 | break; 185 | case FPI_Round_down: 186 | if (sign) { 187 | one_bit: 188 | *exp = fpi->emin; 189 | x[0] = b->wds = 1; 190 | *bp = b; 191 | return STRTOG_Denormal | STRTOG_Inexhi 192 | | STRTOG_Underflow; 193 | } 194 | } 195 | Bfree(b); 196 | *bp = 0; 197 | return STRTOG_Zero | STRTOG_Inexlo | STRTOG_Underflow; 198 | } 199 | k = n - 1; 200 | if (lostbits) 201 | lostbits = 1; 202 | else if (k > 0) 203 | lostbits = any_on(b,k); 204 | if (x[k>>kshift] & 1 << (k & kmask)) 205 | lostbits |= 2; 206 | nbits -= n; 207 | rshift(b,n); 208 | e = fpi->emin; 209 | } 210 | if (lostbits) { 211 | up = 0; 212 | switch(fpi->rounding) { 213 | case FPI_Round_zero: 214 | break; 215 | case FPI_Round_near: 216 | if (lostbits & 2 217 | && (lostbits & 1) | x[0] & 1) 218 | up = 1; 219 | break; 220 | case FPI_Round_up: 221 | up = 1 - sign; 222 | break; 223 | case FPI_Round_down: 224 | up = sign; 225 | } 226 | if (up) { 227 | k = b->wds; 228 | b = increment(b); 229 | x = b->x; 230 | if (irv == STRTOG_Denormal) { 231 | if (nbits == fpi->nbits - 1 232 | && x[nbits >> kshift] & 1 << (nbits & kmask)) 233 | irv = STRTOG_Normal; 234 | } 235 | else if (b->wds > k 236 | || (n = nbits & kmask) !=0 237 | && hi0bits(x[k-1]) < 32-n) { 238 | rshift(b,1); 239 | if (++e > fpi->emax) 240 | goto ovfl; 241 | } 242 | irv |= STRTOG_Inexhi; 243 | } 244 | else 245 | irv |= STRTOG_Inexlo; 246 | } 247 | *bp = b; 248 | *exp = e; 249 | return irv; 250 | } 251 | -------------------------------------------------------------------------------- /gmisc.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | void 35 | #ifdef KR_headers 36 | rshift(b, k) Bigint *b; int k; 37 | #else 38 | rshift(Bigint *b, int k) 39 | #endif 40 | { 41 | ULong *x, *x1, *xe, y; 42 | int n; 43 | 44 | x = x1 = b->x; 45 | n = k >> kshift; 46 | if (n < b->wds) { 47 | xe = x + b->wds; 48 | x += n; 49 | if (k &= kmask) { 50 | n = ULbits - k; 51 | y = *x++ >> k; 52 | while(x < xe) { 53 | *x1++ = (y | (*x << n)) & ALL_ON; 54 | y = *x++ >> k; 55 | } 56 | if ((*x1 = y) !=0) 57 | x1++; 58 | } 59 | else 60 | while(x < xe) 61 | *x1++ = *x++; 62 | } 63 | if ((b->wds = x1 - b->x) == 0) 64 | b->x[0] = 0; 65 | } 66 | 67 | int 68 | #ifdef KR_headers 69 | trailz(b) Bigint *b; 70 | #else 71 | trailz(Bigint *b) 72 | #endif 73 | { 74 | ULong L, *x, *xe; 75 | int n = 0; 76 | 77 | x = b->x; 78 | xe = x + b->wds; 79 | for(n = 0; x < xe && !*x; x++) 80 | n += ULbits; 81 | if (x < xe) { 82 | L = *x; 83 | n += lo0bits(&L); 84 | } 85 | return n; 86 | } 87 | -------------------------------------------------------------------------------- /hd_init.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | unsigned char hexdig[256]; 35 | 36 | static void 37 | #ifdef KR_headers 38 | htinit(h, s, inc) unsigned char *h; unsigned char *s; int inc; 39 | #else 40 | htinit(unsigned char *h, unsigned char *s, int inc) 41 | #endif 42 | { 43 | int i, j; 44 | for(i = 0; (j = s[i]) !=0; i++) 45 | h[j] = i + inc; 46 | } 47 | 48 | void 49 | hexdig_init_D2A(Void) 50 | { 51 | #define USC (unsigned char *) 52 | htinit(hexdig, USC "0123456789", 0x10); 53 | htinit(hexdig, USC "abcdef", 0x10 + 10); 54 | htinit(hexdig, USC "ABCDEF", 0x10 + 10); 55 | } 56 | -------------------------------------------------------------------------------- /hexnan.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | static void 35 | #ifdef KR_headers 36 | L_shift(x, x1, i) ULong *x; ULong *x1; int i; 37 | #else 38 | L_shift(ULong *x, ULong *x1, int i) 39 | #endif 40 | { 41 | int j; 42 | 43 | i = 8 - i; 44 | i <<= 2; 45 | j = ULbits - i; 46 | do { 47 | *x |= x[1] << j; 48 | x[1] >>= i; 49 | } while(++x < x1); 50 | } 51 | 52 | int 53 | #ifdef KR_headers 54 | hexnan(sp, fpi, x0) 55 | CONST char **sp; FPI *fpi; ULong *x0; 56 | #else 57 | hexnan( CONST char **sp, FPI *fpi, ULong *x0) 58 | #endif 59 | { 60 | ULong c, h, *x, *x1, *xe; 61 | CONST char *s; 62 | int havedig, hd0, i, nbits; 63 | 64 | if (!hexdig['0']) 65 | hexdig_init_D2A(); 66 | nbits = fpi->nbits; 67 | x = x0 + (nbits >> kshift); 68 | if (nbits & kmask) 69 | x++; 70 | *--x = 0; 71 | x1 = xe = x; 72 | havedig = hd0 = i = 0; 73 | s = *sp; 74 | /* allow optional initial 0x or 0X */ 75 | while((c = *(CONST unsigned char*)(s+1)) && c <= ' ') 76 | ++s; 77 | if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X') 78 | && *(CONST unsigned char*)(s+3) > ' ') 79 | s += 2; 80 | while(c = *(CONST unsigned char*)++s) { 81 | if (!(h = hexdig[c])) { 82 | if (c <= ' ') { 83 | if (hd0 < havedig) { 84 | if (x < x1 && i < 8) 85 | L_shift(x, x1, i); 86 | if (x <= x0) { 87 | i = 8; 88 | continue; 89 | } 90 | hd0 = havedig; 91 | *--x = 0; 92 | x1 = x; 93 | i = 0; 94 | } 95 | while(*(CONST unsigned char*)(s+1) <= ' ') 96 | ++s; 97 | if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X') 98 | && *(CONST unsigned char*)(s+3) > ' ') 99 | s += 2; 100 | continue; 101 | } 102 | if (/*(*/ c == ')' && havedig) { 103 | *sp = s + 1; 104 | break; 105 | } 106 | #ifndef GDTOA_NON_PEDANTIC_NANCHECK 107 | do { 108 | if (/*(*/ c == ')') { 109 | *sp = s + 1; 110 | break; 111 | } 112 | } while(c = *++s); 113 | #endif 114 | return STRTOG_NaN; 115 | } 116 | havedig++; 117 | if (++i > 8) { 118 | if (x <= x0) 119 | continue; 120 | i = 1; 121 | *--x = 0; 122 | } 123 | *x = (*x << 4) | h & 0xf; 124 | } 125 | if (!havedig) 126 | return STRTOG_NaN; 127 | if (x < x1 && i < 8) 128 | L_shift(x, x1, i); 129 | if (x > x0) { 130 | x1 = x0; 131 | do *x1++ = *x++; 132 | while(x <= xe); 133 | do *x1++ = 0; 134 | while(x1 <= xe); 135 | } 136 | else { 137 | /* truncate high-order word if necessary */ 138 | if ( (i = nbits & (ULbits-1)) !=0) 139 | *xe &= ((ULong)0xffffffff) >> (ULbits - i); 140 | } 141 | for(x1 = xe;; --x1) { 142 | if (*x1 != 0) 143 | break; 144 | if (x1 == x0) { 145 | *x1 = 1; 146 | break; 147 | } 148 | } 149 | return STRTOG_NaNbits; 150 | } 151 | -------------------------------------------------------------------------------- /qnan.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 2005 by David M. Gay 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and its 9 | documentation for any purpose and without fee is hereby granted, 10 | provided that the above copyright notice appear in all copies and that 11 | both that the copyright notice and this permission notice and warranty 12 | disclaimer appear in supporting documentation, and that the name of 13 | the author or any of his current or former employers not be used in 14 | advertising or publicity pertaining to distribution of the software 15 | without specific, written prior permission. 16 | 17 | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN 19 | NO EVENT SHALL THE AUTHOR OR ANY OF HIS CURRENT OR FORMER EMPLOYERS BE 20 | LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 21 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 22 | WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 23 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 24 | SOFTWARE. 25 | 26 | ****************************************************************/ 27 | 28 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 29 | * with " at " changed at "@" and " dot " changed to "."). */ 30 | 31 | /* Program to compute quiet NaNs of various precisions (float, */ 32 | /* double, and perhaps long double) on the current system, */ 33 | /* provided the system uses binary IEEE (P754) arithmetic. */ 34 | /* Note that one system's quiet NaN may be a signaling NaN on */ 35 | /* another system. The IEEE arithmetic standards (P754, P854) */ 36 | /* do not specify how to distinguish signaling NaNs from quiet */ 37 | /* ones, and this detail varies across systems. The computed */ 38 | /* NaN values are encoded in #defines for values for an */ 39 | /* unsigned 32-bit integer type, called Ulong below, and */ 40 | /* (for long double) perhaps as unsigned short values. Once */ 41 | /* upon a time, there were PC compilers for Intel CPUs that */ 42 | /* had sizeof(long double) = 10. Are such compilers still */ 43 | /* distributed? */ 44 | 45 | #include 46 | #include "arith.h" 47 | 48 | #ifndef Long 49 | #define Long long 50 | #endif 51 | 52 | typedef unsigned Long Ulong; 53 | 54 | #undef HAVE_IEEE 55 | #ifdef IEEE_8087 56 | #define _0 1 57 | #define _1 0 58 | #define HAVE_IEEE 59 | #endif 60 | #ifdef IEEE_MC68k 61 | #define _0 0 62 | #define _1 1 63 | #define HAVE_IEEE 64 | #endif 65 | 66 | #define UL (unsigned long) 67 | 68 | int 69 | main(void) 70 | { 71 | #ifdef HAVE_IEEE 72 | typedef union { 73 | float f; 74 | double d; 75 | Ulong L[4]; 76 | #ifndef NO_LONG_LONG 77 | unsigned short u[5]; 78 | long double D; 79 | #endif 80 | } U; 81 | U a, b, c; 82 | int i; 83 | 84 | a.L[0] = b.L[0] = 0x7f800000; 85 | c.f = a.f - b.f; 86 | printf("#define f_QNAN 0x%lx\n", UL c.L[0]); 87 | a.L[_0] = b.L[_0] = 0x7ff00000; 88 | a.L[_1] = b.L[_1] = 0; 89 | c.d = a.d - b.d; /* quiet NaN */ 90 | printf("#define d_QNAN0 0x%lx\n", UL c.L[0]); 91 | printf("#define d_QNAN1 0x%lx\n", UL c.L[1]); 92 | #ifdef NO_LONG_LONG 93 | for(i = 0; i < 4; i++) 94 | printf("#define ld_QNAN%d 0xffffffff\n", i); 95 | for(i = 0; i < 5; i++) 96 | printf("#define ldus_QNAN%d 0xffff\n", i); 97 | #else 98 | b.D = c.D = a.d; 99 | if (printf("") < 0) 100 | c.D = 37; /* never executed; just defeat optimization */ 101 | a.L[2] = a.L[3] = 0; 102 | a.D = b.D - c.D; 103 | for(i = 0; i < 4; i++) 104 | printf("#define ld_QNAN%d 0x%lx\n", i, UL a.L[i]); 105 | for(i = 0; i < 5; i++) 106 | printf("#define ldus_QNAN%d 0x%x\n", i, a.u[i]); 107 | #endif 108 | #endif /* HAVE_IEEE */ 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /smisc.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 1999 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | Bigint * 35 | s2b 36 | #ifdef KR_headers 37 | (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9; 38 | #else 39 | (CONST char *s, int nd0, int nd, ULong y9) 40 | #endif 41 | { 42 | Bigint *b; 43 | int i, k; 44 | Long x, y; 45 | 46 | x = (nd + 8) / 9; 47 | for(k = 0, y = 1; x > y; y <<= 1, k++) ; 48 | #ifdef Pack_32 49 | b = Balloc(k); 50 | b->x[0] = y9; 51 | b->wds = 1; 52 | #else 53 | b = Balloc(k+1); 54 | b->x[0] = y9 & 0xffff; 55 | b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; 56 | #endif 57 | 58 | i = 9; 59 | if (9 < nd0) { 60 | s += 9; 61 | do b = multadd(b, 10, *s++ - '0'); 62 | while(++i < nd0); 63 | s++; 64 | } 65 | else 66 | s += 10; 67 | for(; i < nd; i++) 68 | b = multadd(b, 10, *s++ - '0'); 69 | return b; 70 | } 71 | 72 | double 73 | ratio 74 | #ifdef KR_headers 75 | (a, b) Bigint *a, *b; 76 | #else 77 | (Bigint *a, Bigint *b) 78 | #endif 79 | { 80 | double da, db; 81 | int k, ka, kb; 82 | 83 | dval(da) = b2d(a, &ka); 84 | dval(db) = b2d(b, &kb); 85 | k = ka - kb + ULbits*(a->wds - b->wds); 86 | #ifdef IBM 87 | if (k > 0) { 88 | word0(da) += (k >> 2)*Exp_msk1; 89 | if (k &= 3) 90 | dval(da) *= 1 << k; 91 | } 92 | else { 93 | k = -k; 94 | word0(db) += (k >> 2)*Exp_msk1; 95 | if (k &= 3) 96 | dval(db) *= 1 << k; 97 | } 98 | #else 99 | if (k > 0) 100 | word0(da) += k*Exp_msk1; 101 | else { 102 | k = -k; 103 | word0(db) += k*Exp_msk1; 104 | } 105 | #endif 106 | return dval(da) / dval(db); 107 | } 108 | 109 | #ifdef INFNAN_CHECK 110 | 111 | int 112 | match 113 | #ifdef KR_headers 114 | (sp, t) char **sp, *t; 115 | #else 116 | (CONST char **sp, char *t) 117 | #endif 118 | { 119 | int c, d; 120 | CONST char *s = *sp; 121 | 122 | while( (d = *t++) !=0) { 123 | if ((c = *++s) >= 'A' && c <= 'Z') 124 | c += 'a' - 'A'; 125 | if (c != d) 126 | return 0; 127 | } 128 | *sp = s + 1; 129 | return 1; 130 | } 131 | #endif /* INFNAN_CHECK */ 132 | 133 | void 134 | #ifdef KR_headers 135 | copybits(c, n, b) ULong *c; int n; Bigint *b; 136 | #else 137 | copybits(ULong *c, int n, Bigint *b) 138 | #endif 139 | { 140 | ULong *ce, *x, *xe; 141 | #ifdef Pack_16 142 | int nw, nw1; 143 | #endif 144 | 145 | ce = c + ((n-1) >> kshift) + 1; 146 | x = b->x; 147 | #ifdef Pack_32 148 | xe = x + b->wds; 149 | while(x < xe) 150 | *c++ = *x++; 151 | #else 152 | nw = b->wds; 153 | nw1 = nw & 1; 154 | for(xe = x + (nw - nw1); x < xe; x += 2) 155 | Storeinc(c, x[1], x[0]); 156 | if (nw1) 157 | *c++ = *x; 158 | #endif 159 | while(c < ce) 160 | *c++ = 0; 161 | } 162 | 163 | ULong 164 | #ifdef KR_headers 165 | any_on(b, k) Bigint *b; int k; 166 | #else 167 | any_on(Bigint *b, int k) 168 | #endif 169 | { 170 | int n, nwds; 171 | ULong *x, *x0, x1, x2; 172 | 173 | x = b->x; 174 | nwds = b->wds; 175 | n = k >> kshift; 176 | if (n > nwds) 177 | n = nwds; 178 | else if (n < nwds && (k &= kmask)) { 179 | x1 = x2 = x[n]; 180 | x1 >>= k; 181 | x1 <<= k; 182 | if (x1 != x2) 183 | return 1; 184 | } 185 | x0 = x; 186 | x += n; 187 | while(x > x0) 188 | if (*--x) 189 | return 1; 190 | return 0; 191 | } 192 | -------------------------------------------------------------------------------- /strtoIQ.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | int 35 | #ifdef KR_headers 36 | strtoIQ(s, sp, a, b) CONST char *s; char **sp; void *a; void *b; 37 | #else 38 | strtoIQ(CONST char *s, char **sp, void *a, void *b) 39 | #endif 40 | { 41 | static FPI fpi = { 113, 1-16383-113+1, 32766-16383-113+1, 1, SI }; 42 | Long exp[2]; 43 | Bigint *B[2]; 44 | int k, rv[2]; 45 | ULong *L = (ULong *)a, *M = (ULong *)b; 46 | 47 | B[0] = Balloc(2); 48 | B[0]->wds = 4; 49 | k = strtoIg(s, sp, &fpi, exp, B, rv); 50 | ULtoQ(L, B[0]->x, exp[0], rv[0]); 51 | Bfree(B[0]); 52 | if (B[1]) { 53 | ULtoQ(M, B[1]->x, exp[1], rv[1]); 54 | Bfree(B[1]); 55 | } 56 | else { 57 | M[0] = L[0]; 58 | M[1] = L[1]; 59 | M[2] = L[2]; 60 | M[3] = L[3]; 61 | } 62 | return k; 63 | } 64 | -------------------------------------------------------------------------------- /strtoId.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | int 35 | #ifdef KR_headers 36 | strtoId(s, sp, f0, f1) CONST char *s; char **sp; double *f0, *f1; 37 | #else 38 | strtoId(CONST char *s, char **sp, double *f0, double *f1) 39 | #endif 40 | { 41 | static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; 42 | Long exp[2]; 43 | Bigint *B[2]; 44 | int k, rv[2]; 45 | 46 | B[0] = Balloc(1); 47 | B[0]->wds = 2; 48 | k = strtoIg(s, sp, &fpi, exp, B, rv); 49 | ULtod((ULong*)f0, B[0]->x, exp[0], rv[0]); 50 | Bfree(B[0]); 51 | if (B[1]) { 52 | ULtod((ULong*)f1, B[1]->x, exp[1], rv[1]); 53 | Bfree(B[1]); 54 | } 55 | else { 56 | ((ULong*)f1)[0] = ((ULong*)f0)[0]; 57 | ((ULong*)f1)[1] = ((ULong*)f0)[1]; 58 | } 59 | return k; 60 | } 61 | -------------------------------------------------------------------------------- /strtoIdd.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | int 35 | #ifdef KR_headers 36 | strtoIdd(s, sp, f0, f1) CONST char *s; char **sp; double *f0, *f1; 37 | #else 38 | strtoIdd(CONST char *s, char **sp, double *f0, double *f1) 39 | #endif 40 | { 41 | #ifdef Sudden_Underflow 42 | static FPI fpi = { 106, 1-1023, 2046-1023-106+1, 1, 1 }; 43 | #else 44 | static FPI fpi = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 }; 45 | #endif 46 | Long exp[2]; 47 | Bigint *B[2]; 48 | int k, rv[2]; 49 | 50 | B[0] = Balloc(2); 51 | B[0]->wds = 4; 52 | k = strtoIg(s, sp, &fpi, exp, B, rv); 53 | ULtodd((ULong*)f0, B[0]->x, exp[0], rv[0]); 54 | Bfree(B[0]); 55 | if (B[1]) { 56 | ULtodd((ULong*)f1, B[1]->x, exp[1], rv[1]); 57 | Bfree(B[1]); 58 | } 59 | else { 60 | ((ULong*)f1)[0] = ((ULong*)f0)[0]; 61 | ((ULong*)f1)[1] = ((ULong*)f0)[1]; 62 | ((ULong*)f1)[2] = ((ULong*)f0)[2]; 63 | ((ULong*)f1)[3] = ((ULong*)f0)[3]; 64 | } 65 | return k; 66 | } 67 | -------------------------------------------------------------------------------- /strtoIf.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | int 35 | #ifdef KR_headers 36 | strtoIf(s, sp, f0, f1) CONST char *s; char **sp; float *f0, *f1; 37 | #else 38 | strtoIf(CONST char *s, char **sp, float *f0, float *f1) 39 | #endif 40 | { 41 | static FPI fpi = { 24, 1-127-24+1, 254-127-24+1, 1, SI }; 42 | Long exp[2]; 43 | Bigint *B[2]; 44 | int k, rv[2]; 45 | 46 | B[0] = Balloc(0); 47 | B[0]->wds = 1; 48 | k = strtoIg(s, sp, &fpi, exp, B, rv); 49 | ULtof((ULong*)f0, B[0]->x, exp[0], rv[0]); 50 | Bfree(B[0]); 51 | if (B[1]) { 52 | ULtof((ULong*)f1, B[1]->x, exp[1], rv[1]); 53 | Bfree(B[1]); 54 | } 55 | else 56 | *(ULong*)f1 = *(ULong*)f0; 57 | return k; 58 | } 59 | -------------------------------------------------------------------------------- /strtoIg.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | int 35 | #ifdef KR_headers 36 | strtoIg(s00, se, fpi, exp, B, rvp) CONST char *s00; char **se; FPI *fpi; Long *exp; Bigint **B; int *rvp; 37 | #else 38 | strtoIg(CONST char *s00, char **se, FPI *fpi, Long *exp, Bigint **B, int *rvp) 39 | #endif 40 | { 41 | Bigint *b, *b1; 42 | int i, nb, nw, nw1, rv, rv1, swap; 43 | unsigned int nb1, nb11; 44 | Long e1; 45 | 46 | b = *B; 47 | rv = strtodg(s00, se, fpi, exp, b->x); 48 | if (!(rv & STRTOG_Inexact)) { 49 | B[1] = 0; 50 | return *rvp = rv; 51 | } 52 | e1 = exp[0]; 53 | rv1 = rv ^ STRTOG_Inexact; 54 | b1 = Balloc(b->k); 55 | Bcopy(b1, b); 56 | nb = fpi->nbits; 57 | nb1 = nb & 31; 58 | nb11 = (nb1 - 1) & 31; 59 | nw = b->wds; 60 | nw1 = nw - 1; 61 | if (rv & STRTOG_Inexlo) { 62 | swap = 0; 63 | b1 = increment(b1); 64 | if (fpi->sudden_underflow 65 | && (rv & STRTOG_Retmask) == STRTOG_Zero) { 66 | b1->x[0] = 0; 67 | b1->x[nw1] = 1L << nb11; 68 | rv1 += STRTOG_Normal - STRTOG_Zero; 69 | rv1 &= ~STRTOG_Underflow; 70 | goto swapcheck; 71 | } 72 | if (b1->wds > nw 73 | || nb1 && b1->x[nw1] & 1L << nb1) { 74 | if (++e1 > fpi->emax) 75 | rv1 = STRTOG_Infinite | STRTOG_Inexhi; 76 | rshift(b1, 1); 77 | } 78 | else if ((rv & STRTOG_Retmask) == STRTOG_Denormal) { 79 | if (b1->x[nw1] & 1L << nb11) { 80 | rv1 += STRTOG_Normal - STRTOG_Denormal; 81 | rv1 &= ~STRTOG_Underflow; 82 | } 83 | } 84 | } 85 | else { 86 | swap = STRTOG_Neg; 87 | if ((rv & STRTOG_Retmask) == STRTOG_Infinite) { 88 | b1 = set_ones(b1, nb); 89 | e1 = fpi->emax; 90 | rv1 = STRTOG_Normal | STRTOG_Inexlo; 91 | goto swapcheck; 92 | } 93 | decrement(b1); 94 | if ((rv & STRTOG_Retmask) == STRTOG_Denormal) { 95 | for(i = nw1; !b1->x[i]; --i) 96 | if (!i) { 97 | rv1 = STRTOG_Zero | STRTOG_Inexlo; 98 | break; 99 | } 100 | goto swapcheck; 101 | } 102 | if (!(b1->x[nw1] & 1L << nb11)) { 103 | if (e1 == fpi->emin) { 104 | if (fpi->sudden_underflow) 105 | rv1 += STRTOG_Zero - STRTOG_Normal; 106 | else 107 | rv1 += STRTOG_Denormal - STRTOG_Normal; 108 | rv1 |= STRTOG_Underflow; 109 | } 110 | else { 111 | b1 = lshift(b1, 1); 112 | b1->x[0] |= 1; 113 | --e1; 114 | } 115 | } 116 | } 117 | swapcheck: 118 | if (swap ^ (rv & STRTOG_Neg)) { 119 | rvp[0] = rv1; 120 | rvp[1] = rv; 121 | B[0] = b1; 122 | B[1] = b; 123 | exp[1] = exp[0]; 124 | exp[0] = e1; 125 | } 126 | else { 127 | rvp[0] = rv; 128 | rvp[1] = rv1; 129 | B[1] = b1; 130 | exp[1] = e1; 131 | } 132 | return rv; 133 | } 134 | -------------------------------------------------------------------------------- /strtoIx.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | int 35 | #ifdef KR_headers 36 | strtoIx(s, sp, a, b) CONST char *s; char **sp; void *a; void *b; 37 | #else 38 | strtoIx(CONST char *s, char **sp, void *a, void *b) 39 | #endif 40 | { 41 | static FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; 42 | Long exp[2]; 43 | Bigint *B[2]; 44 | int k, rv[2]; 45 | UShort *L = (UShort *)a, *M = (UShort *)b; 46 | 47 | B[0] = Balloc(1); 48 | B[0]->wds = 2; 49 | k = strtoIg(s, sp, &fpi, exp, B, rv); 50 | ULtox(L, B[0]->x, exp[0], rv[0]); 51 | Bfree(B[0]); 52 | if (B[1]) { 53 | ULtox(M, B[1]->x, exp[1], rv[1]); 54 | Bfree(B[1]); 55 | } 56 | else { 57 | M[0] = L[0]; 58 | M[1] = L[1]; 59 | M[2] = L[2]; 60 | M[3] = L[3]; 61 | M[4] = L[4]; 62 | } 63 | return k; 64 | } 65 | -------------------------------------------------------------------------------- /strtoIxL.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | int 35 | #ifdef KR_headers 36 | strtoIxL(s, sp, a, b) CONST char *s; char **sp; void *a; void *b; 37 | #else 38 | strtoIxL(CONST char *s, char **sp, void *a, void *b) 39 | #endif 40 | { 41 | static FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; 42 | Long exp[2]; 43 | Bigint *B[2]; 44 | int k, rv[2]; 45 | ULong *L = (ULong *)a, *M = (ULong *)b; 46 | 47 | B[0] = Balloc(1); 48 | B[0]->wds = 2; 49 | k = strtoIg(s, sp, &fpi, exp, B, rv); 50 | ULtoxL(L, B[0]->x, exp[0], rv[0]); 51 | Bfree(B[0]); 52 | if (B[1]) { 53 | ULtoxL(M, B[1]->x, exp[1], rv[1]); 54 | Bfree(B[1]); 55 | } 56 | else { 57 | M[0] = L[0]; 58 | M[1] = L[1]; 59 | M[2] = L[2]; 60 | } 61 | return k; 62 | } 63 | -------------------------------------------------------------------------------- /strtodI.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | static double 35 | #ifdef KR_headers 36 | ulpdown(d) double *d; 37 | #else 38 | ulpdown(double *d) 39 | #endif 40 | { 41 | double u; 42 | ULong *L = (ULong*)d; 43 | 44 | u = ulp(*d); 45 | if (!(L[_1] | L[_0] & 0xfffff) 46 | && (L[_0] & 0x7ff00000) > 0x00100000) 47 | u *= 0.5; 48 | return u; 49 | } 50 | 51 | int 52 | #ifdef KR_headers 53 | strtodI(s, sp, dd) CONST char *s; char **sp; double *dd; 54 | #else 55 | strtodI(CONST char *s, char **sp, double *dd) 56 | #endif 57 | { 58 | static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; 59 | ULong bits[2], sign; 60 | Long exp; 61 | int j, k; 62 | typedef union { 63 | double d[2]; 64 | ULong L[4]; 65 | } U; 66 | U *u; 67 | 68 | k = strtodg(s, sp, &fpi, &exp, bits); 69 | u = (U*)dd; 70 | sign = k & STRTOG_Neg ? 0x80000000L : 0; 71 | switch(k & STRTOG_Retmask) { 72 | case STRTOG_NoNumber: 73 | u->d[0] = u->d[1] = 0.; 74 | break; 75 | 76 | case STRTOG_Zero: 77 | u->d[0] = u->d[1] = 0.; 78 | #ifdef Sudden_Underflow 79 | if (k & STRTOG_Inexact) { 80 | if (sign) 81 | u->L[_0] = 0x80100000L; 82 | else 83 | u->L[2+_0] = 0x100000L; 84 | } 85 | break; 86 | #else 87 | goto contain; 88 | #endif 89 | 90 | case STRTOG_Denormal: 91 | u->L[_1] = bits[0]; 92 | u->L[_0] = bits[1]; 93 | goto contain; 94 | 95 | case STRTOG_Normal: 96 | u->L[_1] = bits[0]; 97 | u->L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); 98 | contain: 99 | j = k & STRTOG_Inexact; 100 | if (sign) { 101 | u->L[_0] |= sign; 102 | j = STRTOG_Inexact - j; 103 | } 104 | switch(j) { 105 | case STRTOG_Inexlo: 106 | #ifdef Sudden_Underflow 107 | if ((u->L[_0] & 0x7ff00000) < 0x3500000) { 108 | u->L[2+_0] = u->L[_0] + 0x3500000; 109 | u->L[2+_1] = u->L[_1]; 110 | u->d[1] += ulp(u->d[1]); 111 | u->L[2+_0] -= 0x3500000; 112 | if (!(u->L[2+_0] & 0x7ff00000)) { 113 | u->L[2+_0] = sign; 114 | u->L[2+_1] = 0; 115 | } 116 | } 117 | else 118 | #endif 119 | u->d[1] = u->d[0] + ulp(u->d[0]); 120 | break; 121 | case STRTOG_Inexhi: 122 | u->d[1] = u->d[0]; 123 | #ifdef Sudden_Underflow 124 | if ((u->L[_0] & 0x7ff00000) < 0x3500000) { 125 | u->L[_0] += 0x3500000; 126 | u->d[0] -= ulpdown(u->d); 127 | u->L[_0] -= 0x3500000; 128 | if (!(u->L[_0] & 0x7ff00000)) { 129 | u->L[_0] = sign; 130 | u->L[_1] = 0; 131 | } 132 | } 133 | else 134 | #endif 135 | u->d[0] -= ulpdown(u->d); 136 | break; 137 | default: 138 | u->d[1] = u->d[0]; 139 | } 140 | break; 141 | 142 | case STRTOG_Infinite: 143 | u->L[_0] = u->L[2+_0] = sign | 0x7ff00000; 144 | u->L[_1] = u->L[2+_1] = 0; 145 | if (k & STRTOG_Inexact) { 146 | if (sign) { 147 | u->L[2+_0] = 0xffefffffL; 148 | u->L[2+_1] = 0xffffffffL; 149 | } 150 | else { 151 | u->L[_0] = 0x7fefffffL; 152 | u->L[_1] = 0xffffffffL; 153 | } 154 | } 155 | break; 156 | 157 | case STRTOG_NaN: 158 | u->L[0] = u->L[2] = d_QNAN0; 159 | u->L[1] = u->L[3] = d_QNAN1; 160 | break; 161 | 162 | case STRTOG_NaNbits: 163 | u->L[_0] = u->L[2+_0] = 0x7ff00000 | sign | bits[1]; 164 | u->L[_1] = u->L[2+_1] = bits[0]; 165 | } 166 | return k; 167 | } 168 | -------------------------------------------------------------------------------- /strtodnrp.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 2004 by David M. Gay. 6 | All Rights Reserved 7 | Based on material in the rest of /netlib/fp/gdota.tar.gz, 8 | which is copyright (C) 1998, 2000 by Lucent Technologies. 9 | 10 | Permission to use, copy, modify, and distribute this software and 11 | its documentation for any purpose and without fee is hereby 12 | granted, provided that the above copyright notice appear in all 13 | copies and that both that the copyright notice and this 14 | permission notice and warranty disclaimer appear in supporting 15 | documentation, and that the name of Lucent or any of its entities 16 | not be used in advertising or publicity pertaining to 17 | distribution of the software without specific, written prior 18 | permission. 19 | 20 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 21 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 22 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 23 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 24 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 25 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 26 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 27 | THIS SOFTWARE. 28 | 29 | ****************************************************************/ 30 | 31 | /* This is a variant of strtod that works on Intel ia32 systems */ 32 | /* with the default extended-precision arithmetic -- it does not */ 33 | /* require setting the precision control to 53 bits. */ 34 | 35 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 36 | * with " at " changed at "@" and " dot " changed to "."). */ 37 | 38 | #include "gdtoaimp.h" 39 | 40 | double 41 | #ifdef KR_headers 42 | strtod(s, sp) CONST char *s; char **sp; 43 | #else 44 | strtod(CONST char *s, char **sp) 45 | #endif 46 | { 47 | static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; 48 | ULong bits[2]; 49 | Long exp; 50 | int k; 51 | union { ULong L[2]; double d; } u; 52 | 53 | k = strtodg(s, sp, &fpi, &exp, bits); 54 | switch(k & STRTOG_Retmask) { 55 | case STRTOG_NoNumber: 56 | case STRTOG_Zero: 57 | u.L[0] = u.L[1] = 0; 58 | break; 59 | 60 | case STRTOG_Normal: 61 | u.L[_1] = bits[0]; 62 | u.L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); 63 | break; 64 | 65 | case STRTOG_Denormal: 66 | u.L[_1] = bits[0]; 67 | u.L[_0] = bits[1]; 68 | break; 69 | 70 | case STRTOG_Infinite: 71 | u.L[_0] = 0x7ff00000; 72 | u.L[_1] = 0; 73 | break; 74 | 75 | case STRTOG_NaN: 76 | u.L[0] = d_QNAN0; 77 | u.L[1] = d_QNAN1; 78 | break; 79 | 80 | case STRTOG_NaNbits: 81 | u.L[_0] = 0x7ff00000 | bits[1]; 82 | u.L[_1] = bits[0]; 83 | } 84 | if (k & STRTOG_Neg) 85 | u.L[_0] |= 0x80000000L; 86 | return u.d; 87 | } 88 | -------------------------------------------------------------------------------- /strtof.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | float 35 | #ifdef KR_headers 36 | strtof(s, sp) CONST char *s; char **sp; 37 | #else 38 | strtof(CONST char *s, char **sp) 39 | #endif 40 | { 41 | static FPI fpi = { 24, 1-127-24+1, 254-127-24+1, 1, SI }; 42 | ULong bits[1]; 43 | Long exp; 44 | int k; 45 | union { ULong L[1]; float f; } u; 46 | 47 | k = strtodg(s, sp, &fpi, &exp, bits); 48 | switch(k & STRTOG_Retmask) { 49 | case STRTOG_NoNumber: 50 | case STRTOG_Zero: 51 | u.L[0] = 0; 52 | break; 53 | 54 | case STRTOG_Normal: 55 | case STRTOG_NaNbits: 56 | u.L[0] = bits[0] & 0x7fffff | exp + 0x7f + 23 << 23; 57 | break; 58 | 59 | case STRTOG_Denormal: 60 | u.L[0] = bits[0]; 61 | break; 62 | 63 | case STRTOG_Infinite: 64 | u.L[0] = 0x7f800000; 65 | break; 66 | 67 | case STRTOG_NaN: 68 | u.L[0] = f_QNAN; 69 | } 70 | if (k & STRTOG_Neg) 71 | u.L[0] |= 0x80000000L; 72 | return u.f; 73 | } 74 | -------------------------------------------------------------------------------- /strtopQ.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #undef _0 35 | #undef _1 36 | 37 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 38 | 39 | #ifdef IEEE_MC68k 40 | #define _0 0 41 | #define _1 1 42 | #define _2 2 43 | #define _3 3 44 | #endif 45 | #ifdef IEEE_8087 46 | #define _0 3 47 | #define _1 2 48 | #define _2 1 49 | #define _3 0 50 | #endif 51 | 52 | int 53 | #ifdef KR_headers 54 | strtopQ(s, sp, V) CONST char *s; char **sp; void *V; 55 | #else 56 | strtopQ(CONST char *s, char **sp, void *V) 57 | #endif 58 | { 59 | static FPI fpi = { 113, 1-16383-113+1, 32766 - 16383 - 113 + 1, 1, SI }; 60 | ULong bits[4]; 61 | Long exp; 62 | int k; 63 | ULong *L = (ULong*)V; 64 | 65 | k = strtodg(s, sp, &fpi, &exp, bits); 66 | switch(k & STRTOG_Retmask) { 67 | case STRTOG_NoNumber: 68 | case STRTOG_Zero: 69 | L[0] = L[1] = L[2] = L[3] = 0; 70 | break; 71 | 72 | case STRTOG_Normal: 73 | case STRTOG_NaNbits: 74 | L[_3] = bits[0]; 75 | L[_2] = bits[1]; 76 | L[_1] = bits[2]; 77 | L[_0] = (bits[3] & ~0x10000) | ((exp + 0x3fff + 112) << 16); 78 | break; 79 | 80 | case STRTOG_Denormal: 81 | L[_3] = bits[0]; 82 | L[_2] = bits[1]; 83 | L[_1] = bits[2]; 84 | L[_0] = bits[3]; 85 | break; 86 | 87 | case STRTOG_Infinite: 88 | L[_0] = 0x7fff0000; 89 | L[_1] = L[_2] = L[_3] = 0; 90 | break; 91 | 92 | case STRTOG_NaN: 93 | L[0] = ld_QNAN0; 94 | L[1] = ld_QNAN1; 95 | L[2] = ld_QNAN2; 96 | L[3] = ld_QNAN3; 97 | } 98 | if (k & STRTOG_Neg) 99 | L[_0] |= 0x80000000L; 100 | return k; 101 | } 102 | -------------------------------------------------------------------------------- /strtopd.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | int 35 | #ifdef KR_headers 36 | strtopd(s, sp, d) char *s; char **sp; double *d; 37 | #else 38 | strtopd(CONST char *s, char **sp, double *d) 39 | #endif 40 | { 41 | static FPI fpi0 = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; 42 | ULong bits[2]; 43 | Long exp; 44 | int k; 45 | 46 | k = strtodg(s, sp, &fpi0, &exp, bits); 47 | ULtod((ULong*)d, bits, exp, k); 48 | return k; 49 | } 50 | -------------------------------------------------------------------------------- /strtopdd.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | int 35 | #ifdef KR_headers 36 | strtopdd(s, sp, dd) CONST char *s; char **sp; double *dd; 37 | #else 38 | strtopdd(CONST char *s, char **sp, double *dd) 39 | #endif 40 | { 41 | #ifdef Sudden_Underflow 42 | static FPI fpi = { 106, 1-1023, 2046-1023-106+1, 1, 1 }; 43 | #else 44 | static FPI fpi = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 }; 45 | #endif 46 | ULong bits[4]; 47 | Long exp; 48 | int i, j, rv; 49 | typedef union { 50 | double d[2]; 51 | ULong L[4]; 52 | } U; 53 | U *u; 54 | 55 | rv = strtodg(s, sp, &fpi, &exp, bits); 56 | u = (U*)dd; 57 | switch(rv & STRTOG_Retmask) { 58 | case STRTOG_NoNumber: 59 | case STRTOG_Zero: 60 | u->d[0] = u->d[1] = 0.; 61 | break; 62 | 63 | case STRTOG_Normal: 64 | u->L[_1] = (bits[1] >> 21 | bits[2] << 11) & 0xffffffffL; 65 | u->L[_0] = bits[2] >> 21 | bits[3] << 11 & 0xfffff 66 | | exp + 0x3ff + 105 << 20; 67 | exp += 0x3ff + 52; 68 | if (bits[1] &= 0x1fffff) { 69 | i = hi0bits(bits[1]) - 11; 70 | if (i >= exp) { 71 | i = exp - 1; 72 | exp = 0; 73 | } 74 | else 75 | exp -= i; 76 | if (i > 0) { 77 | bits[1] = bits[1] << i | bits[0] >> 32-i; 78 | bits[0] = bits[0] << i & 0xffffffffL; 79 | } 80 | } 81 | else if (bits[0]) { 82 | i = hi0bits(bits[0]) + 21; 83 | if (i >= exp) { 84 | i = exp - 1; 85 | exp = 0; 86 | } 87 | else 88 | exp -= i; 89 | if (i < 32) { 90 | bits[1] = bits[0] >> 32 - i; 91 | bits[0] = bits[0] << i & 0xffffffffL; 92 | } 93 | else { 94 | bits[1] = bits[0] << i - 32; 95 | bits[0] = 0; 96 | } 97 | } 98 | else { 99 | u->L[2] = u->L[3] = 0; 100 | break; 101 | } 102 | u->L[2+_1] = bits[0]; 103 | u->L[2+_0] = bits[1] & 0xfffff | exp << 20; 104 | break; 105 | 106 | case STRTOG_Denormal: 107 | if (bits[3]) 108 | goto nearly_normal; 109 | if (bits[2]) 110 | goto partly_normal; 111 | if (bits[1] & 0xffe00000) 112 | goto hardly_normal; 113 | /* completely denormal */ 114 | u->L[2] = u->L[3] = 0; 115 | u->L[_1] = bits[0]; 116 | u->L[_0] = bits[1]; 117 | break; 118 | 119 | nearly_normal: 120 | i = hi0bits(bits[3]) - 11; /* i >= 12 */ 121 | j = 32 - i; 122 | u->L[_0] = (bits[3] << i | bits[2] >> j) & 0xfffff 123 | | 65 - i << 20; 124 | u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; 125 | u->L[2+_0] = bits[1] & (1L << j) - 1; 126 | u->L[2+_1] = bits[0]; 127 | break; 128 | 129 | partly_normal: 130 | i = hi0bits(bits[2]) - 11; 131 | if (i < 0) { 132 | j = -i; 133 | i += 32; 134 | u->L[_0] = bits[2] >> j & 0xfffff | (33 + j) << 20; 135 | u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; 136 | u->L[2+_0] = bits[1] & (1L << j) - 1; 137 | u->L[2+_1] = bits[0]; 138 | break; 139 | } 140 | if (i == 0) { 141 | u->L[_0] = bits[2] & 0xfffff | 33 << 20; 142 | u->L[_1] = bits[1]; 143 | u->L[2+_0] = 0; 144 | u->L[2+_1] = bits[0]; 145 | break; 146 | } 147 | j = 32 - i; 148 | u->L[_0] = (bits[2] << i | bits[1] >> j) & 0xfffff 149 | | j + 1 << 20; 150 | u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; 151 | u->L[2+_0] = 0; 152 | u->L[2+_1] = bits[0] & (1L << j) - 1; 153 | break; 154 | 155 | hardly_normal: 156 | j = 11 - hi0bits(bits[1]); 157 | i = 32 - j; 158 | u->L[_0] = bits[1] >> j & 0xfffff | j + 1 << 20; 159 | u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; 160 | u->L[2+_0] = 0; 161 | u->L[2+_1] = bits[0] & (1L << j) - 1; 162 | break; 163 | 164 | case STRTOG_Infinite: 165 | u->L[_0] = u->L[2+_0] = 0x7ff00000; 166 | u->L[_1] = u->L[2+_1] = 0; 167 | break; 168 | 169 | case STRTOG_NaN: 170 | u->L[0] = u->L[2] = d_QNAN0; 171 | u->L[1] = u->L[3] = d_QNAN1; 172 | } 173 | if (rv & STRTOG_Neg) { 174 | u->L[ _0] |= 0x80000000L; 175 | u->L[2+_0] |= 0x80000000L; 176 | } 177 | return rv; 178 | } 179 | -------------------------------------------------------------------------------- /strtopf.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | int 35 | #ifdef KR_headers 36 | strtopf(s, sp, f) CONST char *s; char **sp; float *f; 37 | #else 38 | strtopf(CONST char *s, char **sp, float *f) 39 | #endif 40 | { 41 | static FPI fpi = { 24, 1-127-24+1, 254-127-24+1, 1, SI }; 42 | ULong bits[1], *L; 43 | Long exp; 44 | int k; 45 | 46 | k = strtodg(s, sp, &fpi, &exp, bits); 47 | L = (ULong*)f; 48 | switch(k & STRTOG_Retmask) { 49 | case STRTOG_NoNumber: 50 | case STRTOG_Zero: 51 | L[0] = 0; 52 | break; 53 | 54 | case STRTOG_Normal: 55 | case STRTOG_NaNbits: 56 | L[0] = bits[0] & 0x7fffff | exp + 0x7f + 23 << 23; 57 | break; 58 | 59 | case STRTOG_Denormal: 60 | L[0] = bits[0]; 61 | break; 62 | 63 | case STRTOG_Infinite: 64 | L[0] = 0x7f800000; 65 | break; 66 | 67 | case STRTOG_NaN: 68 | L[0] = f_QNAN; 69 | } 70 | if (k & STRTOG_Neg) 71 | L[0] |= 0x80000000L; 72 | return k; 73 | } 74 | -------------------------------------------------------------------------------- /strtopx.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #undef _0 35 | #undef _1 36 | 37 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 38 | 39 | #ifdef IEEE_MC68k 40 | #define _0 0 41 | #define _1 1 42 | #define _2 2 43 | #define _3 3 44 | #define _4 4 45 | #endif 46 | #ifdef IEEE_8087 47 | #define _0 4 48 | #define _1 3 49 | #define _2 2 50 | #define _3 1 51 | #define _4 0 52 | #endif 53 | 54 | int 55 | #ifdef KR_headers 56 | strtopx(s, sp, V) CONST char *s; char **sp; void *V; 57 | #else 58 | strtopx(CONST char *s, char **sp, void *V) 59 | #endif 60 | { 61 | static FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; 62 | ULong bits[2]; 63 | Long exp; 64 | int k; 65 | UShort *L = (UShort*)V; 66 | 67 | k = strtodg(s, sp, &fpi, &exp, bits); 68 | switch(k & STRTOG_Retmask) { 69 | case STRTOG_NoNumber: 70 | case STRTOG_Zero: 71 | L[0] = L[1] = L[2] = L[3] = L[4] = 0; 72 | break; 73 | 74 | case STRTOG_Denormal: 75 | L[_0] = 0; 76 | goto normal_bits; 77 | 78 | case STRTOG_Normal: 79 | case STRTOG_NaNbits: 80 | L[_0] = exp + 0x3fff + 63; 81 | normal_bits: 82 | L[_4] = (UShort)bits[0]; 83 | L[_3] = (UShort)(bits[0] >> 16); 84 | L[_2] = (UShort)bits[1]; 85 | L[_1] = (UShort)(bits[1] >> 16); 86 | break; 87 | 88 | case STRTOG_Infinite: 89 | L[_0] = 0x7fff; 90 | L[_1] = L[_2] = L[_3] = L[_4] = 0; 91 | break; 92 | 93 | case STRTOG_NaN: 94 | L[0] = ldus_QNAN0; 95 | L[1] = ldus_QNAN1; 96 | L[2] = ldus_QNAN2; 97 | L[3] = ldus_QNAN3; 98 | L[4] = ldus_QNAN4; 99 | } 100 | if (k & STRTOG_Neg) 101 | L[_0] |= 0x8000; 102 | return k; 103 | } 104 | -------------------------------------------------------------------------------- /strtopxL.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #undef _0 35 | #undef _1 36 | 37 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 38 | 39 | #ifdef IEEE_MC68k 40 | #define _0 0 41 | #define _1 1 42 | #define _2 2 43 | #endif 44 | #ifdef IEEE_8087 45 | #define _0 2 46 | #define _1 1 47 | #define _2 0 48 | #endif 49 | 50 | int 51 | #ifdef KR_headers 52 | strtopxL(s, sp, V) CONST char *s; char **sp; void *V; 53 | #else 54 | strtopxL(CONST char *s, char **sp, void *V) 55 | #endif 56 | { 57 | static FPI fpi = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; 58 | ULong bits[2]; 59 | Long exp; 60 | int k; 61 | ULong *L = (ULong*)V; 62 | 63 | k = strtodg(s, sp, &fpi, &exp, bits); 64 | switch(k & STRTOG_Retmask) { 65 | case STRTOG_NoNumber: 66 | case STRTOG_Zero: 67 | L[0] = L[1] = L[2] = 0; 68 | break; 69 | 70 | case STRTOG_Normal: 71 | case STRTOG_Denormal: 72 | case STRTOG_NaNbits: 73 | L[_2] = bits[0]; 74 | L[_1] = bits[1]; 75 | L[_0] = (exp + 0x3fff + 63) << 16; 76 | break; 77 | 78 | case STRTOG_Infinite: 79 | L[_0] = 0x7fff << 16; 80 | L[_1] = L[_2] = 0; 81 | break; 82 | 83 | case STRTOG_NaN: 84 | L[0] = ld_QNAN0; 85 | L[1] = ld_QNAN1; 86 | L[2] = ld_QNAN2; 87 | } 88 | if (k & STRTOG_Neg) 89 | L[_0] |= 0x80000000L; 90 | return k; 91 | } 92 | -------------------------------------------------------------------------------- /strtorQ.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #undef _0 35 | #undef _1 36 | 37 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 38 | 39 | #ifdef IEEE_MC68k 40 | #define _0 0 41 | #define _1 1 42 | #define _2 2 43 | #define _3 3 44 | #endif 45 | #ifdef IEEE_8087 46 | #define _0 3 47 | #define _1 2 48 | #define _2 1 49 | #define _3 0 50 | #endif 51 | 52 | void 53 | #ifdef KR_headers 54 | ULtoQ(L, bits, exp, k) ULong *L; ULong *bits; Long exp; int k; 55 | #else 56 | ULtoQ(ULong *L, ULong *bits, Long exp, int k) 57 | #endif 58 | { 59 | switch(k & STRTOG_Retmask) { 60 | case STRTOG_NoNumber: 61 | case STRTOG_Zero: 62 | L[0] = L[1] = L[2] = L[3] = 0; 63 | break; 64 | 65 | case STRTOG_Normal: 66 | case STRTOG_NaNbits: 67 | L[_3] = bits[0]; 68 | L[_2] = bits[1]; 69 | L[_1] = bits[2]; 70 | L[_0] = (bits[3] & ~0x10000) | ((exp + 0x3fff + 112) << 16); 71 | break; 72 | 73 | case STRTOG_Denormal: 74 | L[_3] = bits[0]; 75 | L[_2] = bits[1]; 76 | L[_1] = bits[2]; 77 | L[_0] = bits[3]; 78 | break; 79 | 80 | case STRTOG_Infinite: 81 | L[_0] = 0x7fff0000; 82 | L[_1] = L[_2] = L[_3] = 0; 83 | break; 84 | 85 | case STRTOG_NaN: 86 | L[0] = ld_QNAN0; 87 | L[1] = ld_QNAN1; 88 | L[2] = ld_QNAN2; 89 | L[3] = ld_QNAN3; 90 | } 91 | if (k & STRTOG_Neg) 92 | L[_0] |= 0x80000000L; 93 | } 94 | 95 | int 96 | #ifdef KR_headers 97 | strtorQ(s, sp, rounding, L) CONST char *s; char **sp; int rounding; void *L; 98 | #else 99 | strtorQ(CONST char *s, char **sp, int rounding, void *L) 100 | #endif 101 | { 102 | static FPI fpi0 = { 113, 1-16383-113+1, 32766-16383-113+1, 1, SI }; 103 | FPI *fpi, fpi1; 104 | ULong bits[4]; 105 | Long exp; 106 | int k; 107 | 108 | fpi = &fpi0; 109 | if (rounding != FPI_Round_near) { 110 | fpi1 = fpi0; 111 | fpi1.rounding = rounding; 112 | fpi = &fpi1; 113 | } 114 | k = strtodg(s, sp, fpi, &exp, bits); 115 | ULtoQ((ULong*)L, bits, exp, k); 116 | return k; 117 | } 118 | -------------------------------------------------------------------------------- /strtord.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | void 35 | #ifdef KR_headers 36 | ULtod(L, bits, exp, k) ULong *L; ULong *bits; Long exp; int k; 37 | #else 38 | ULtod(ULong *L, ULong *bits, Long exp, int k) 39 | #endif 40 | { 41 | switch(k & STRTOG_Retmask) { 42 | case STRTOG_NoNumber: 43 | case STRTOG_Zero: 44 | L[0] = L[1] = 0; 45 | break; 46 | 47 | case STRTOG_Denormal: 48 | L[_1] = bits[0]; 49 | L[_0] = bits[1]; 50 | break; 51 | 52 | case STRTOG_Normal: 53 | case STRTOG_NaNbits: 54 | L[_1] = bits[0]; 55 | L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); 56 | break; 57 | 58 | case STRTOG_Infinite: 59 | L[_0] = 0x7ff00000; 60 | L[_1] = 0; 61 | break; 62 | 63 | case STRTOG_NaN: 64 | L[0] = d_QNAN0; 65 | L[1] = d_QNAN1; 66 | } 67 | if (k & STRTOG_Neg) 68 | L[_0] |= 0x80000000L; 69 | } 70 | 71 | int 72 | #ifdef KR_headers 73 | strtord(s, sp, rounding, d) CONST char *s; char **sp; int rounding; double *d; 74 | #else 75 | strtord(CONST char *s, char **sp, int rounding, double *d) 76 | #endif 77 | { 78 | static FPI fpi0 = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; 79 | FPI *fpi, fpi1; 80 | ULong bits[2]; 81 | Long exp; 82 | int k; 83 | 84 | fpi = &fpi0; 85 | if (rounding != FPI_Round_near) { 86 | fpi1 = fpi0; 87 | fpi1.rounding = rounding; 88 | fpi = &fpi1; 89 | } 90 | k = strtodg(s, sp, fpi, &exp, bits); 91 | ULtod((ULong*)d, bits, exp, k); 92 | return k; 93 | } 94 | -------------------------------------------------------------------------------- /strtordd.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | void 35 | #ifdef KR_headers 36 | ULtodd(L, bits, exp, k) ULong *L; ULong *bits; Long exp; int k; 37 | #else 38 | ULtodd(ULong *L, ULong *bits, Long exp, int k) 39 | #endif 40 | { 41 | int i, j; 42 | 43 | switch(k & STRTOG_Retmask) { 44 | case STRTOG_NoNumber: 45 | case STRTOG_Zero: 46 | L[0] = L[1] = L[2] = L[3] = 0; 47 | break; 48 | 49 | case STRTOG_Normal: 50 | L[_1] = (bits[1] >> 21 | bits[2] << 11) & (ULong)0xffffffffL; 51 | L[_0] = bits[2] >> 21 | bits[3] << 11 & 0xfffff 52 | | exp + 0x3ff + 105 << 20; 53 | exp += 0x3ff + 52; 54 | if (bits[1] &= 0x1fffff) { 55 | i = hi0bits(bits[1]) - 11; 56 | if (i >= exp) { 57 | i = exp - 1; 58 | exp = 0; 59 | } 60 | else 61 | exp -= i; 62 | if (i > 0) { 63 | bits[1] = bits[1] << i | bits[0] >> 32-i; 64 | bits[0] = bits[0] << i & (ULong)0xffffffffL; 65 | } 66 | } 67 | else if (bits[0]) { 68 | i = hi0bits(bits[0]) + 21; 69 | if (i >= exp) { 70 | i = exp - 1; 71 | exp = 0; 72 | } 73 | else 74 | exp -= i; 75 | if (i < 32) { 76 | bits[1] = bits[0] >> 32 - i; 77 | bits[0] = bits[0] << i & (ULong)0xffffffffL; 78 | } 79 | else { 80 | bits[1] = bits[0] << i - 32; 81 | bits[0] = 0; 82 | } 83 | } 84 | else { 85 | L[2] = L[3] = 0; 86 | break; 87 | } 88 | L[2+_1] = bits[0]; 89 | L[2+_0] = bits[1] & 0xfffff | exp << 20; 90 | break; 91 | 92 | case STRTOG_Denormal: 93 | if (bits[3]) 94 | goto nearly_normal; 95 | if (bits[2]) 96 | goto partly_normal; 97 | if (bits[1] & 0xffe00000) 98 | goto hardly_normal; 99 | /* completely denormal */ 100 | L[2] = L[3] = 0; 101 | L[_1] = bits[0]; 102 | L[_0] = bits[1]; 103 | break; 104 | 105 | nearly_normal: 106 | i = hi0bits(bits[3]) - 11; /* i >= 12 */ 107 | j = 32 - i; 108 | L[_0] = (bits[3] << i | bits[2] >> j) & 0xfffff 109 | | 65 - i << 20; 110 | L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; 111 | L[2+_0] = bits[1] & ((ULong)1L << j) - 1; 112 | L[2+_1] = bits[0]; 113 | break; 114 | 115 | partly_normal: 116 | i = hi0bits(bits[2]) - 11; 117 | if (i < 0) { 118 | j = -i; 119 | i += 32; 120 | L[_0] = bits[2] >> j & 0xfffff | (33 + j) << 20; 121 | L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL; 122 | L[2+_0] = bits[1] & ((ULong)1L << j) - 1; 123 | L[2+_1] = bits[0]; 124 | break; 125 | } 126 | if (i == 0) { 127 | L[_0] = bits[2] & 0xfffff | 33 << 20; 128 | L[_1] = bits[1]; 129 | L[2+_0] = 0; 130 | L[2+_1] = bits[0]; 131 | break; 132 | } 133 | j = 32 - i; 134 | L[_0] = (bits[2] << i | bits[1] >> j) & 0xfffff 135 | | j + 1 << 20; 136 | L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; 137 | L[2+_0] = 0; 138 | L[2+_1] = bits[0] & (1L << j) - 1; 139 | break; 140 | 141 | hardly_normal: 142 | j = 11 - hi0bits(bits[1]); 143 | i = 32 - j; 144 | L[_0] = bits[1] >> j & 0xfffff | j + 1 << 20; 145 | L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL; 146 | L[2+_0] = 0; 147 | L[2+_1] = bits[0] & ((ULong)1L << j) - 1; 148 | break; 149 | 150 | case STRTOG_Infinite: 151 | L[_0] = L[2+_0] = 0x7ff00000; 152 | L[_1] = L[2+_1] = 0; 153 | break; 154 | 155 | case STRTOG_NaN: 156 | L[0] = L[2] = d_QNAN0; 157 | L[1] = L[3] = d_QNAN1; 158 | break; 159 | 160 | case STRTOG_NaNbits: 161 | L[_1] = (bits[1] >> 21 | bits[2] << 11) & (ULong)0xffffffffL; 162 | L[_0] = bits[2] >> 21 | bits[3] << 11 163 | | (ULong)0x7ff00000L; 164 | L[2+_1] = bits[0]; 165 | L[2+_0] = bits[1] | (ULong)0x7ff00000L; 166 | } 167 | if (k & STRTOG_Neg) { 168 | L[_0] |= 0x80000000L; 169 | L[2+_0] |= 0x80000000L; 170 | } 171 | } 172 | 173 | int 174 | #ifdef KR_headers 175 | strtordd(s, sp, rounding, dd) CONST char *s; char **sp; int rounding; double *dd; 176 | #else 177 | strtordd(CONST char *s, char **sp, int rounding, double *dd) 178 | #endif 179 | { 180 | #ifdef Sudden_Underflow 181 | static FPI fpi0 = { 106, 1-1023, 2046-1023-106+1, 1, 1 }; 182 | #else 183 | static FPI fpi0 = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 }; 184 | #endif 185 | FPI *fpi, fpi1; 186 | ULong bits[4]; 187 | Long exp; 188 | int k; 189 | 190 | fpi = &fpi0; 191 | if (rounding != FPI_Round_near) { 192 | fpi1 = fpi0; 193 | fpi1.rounding = rounding; 194 | fpi = &fpi1; 195 | } 196 | k = strtodg(s, sp, fpi, &exp, bits); 197 | ULtodd((ULong*)dd, bits, exp, k); 198 | return k; 199 | } 200 | -------------------------------------------------------------------------------- /strtorf.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | void 35 | #ifdef KR_headers 36 | ULtof(L, bits, exp, k) ULong *L; ULong *bits; Long exp; int k; 37 | #else 38 | ULtof(ULong *L, ULong *bits, Long exp, int k) 39 | #endif 40 | { 41 | switch(k & STRTOG_Retmask) { 42 | case STRTOG_NoNumber: 43 | case STRTOG_Zero: 44 | *L = 0; 45 | break; 46 | 47 | case STRTOG_Normal: 48 | case STRTOG_NaNbits: 49 | L[0] = bits[0] & 0x7fffff | exp + 0x7f + 23 << 23; 50 | break; 51 | 52 | case STRTOG_Denormal: 53 | L[0] = bits[0]; 54 | break; 55 | 56 | case STRTOG_Infinite: 57 | L[0] = 0x7f800000; 58 | break; 59 | 60 | case STRTOG_NaN: 61 | L[0] = f_QNAN; 62 | } 63 | if (k & STRTOG_Neg) 64 | L[0] |= 0x80000000L; 65 | } 66 | 67 | int 68 | #ifdef KR_headers 69 | strtorf(s, sp, rounding, f) CONST char *s; char **sp; int rounding; float *f; 70 | #else 71 | strtorf(CONST char *s, char **sp, int rounding, float *f) 72 | #endif 73 | { 74 | static FPI fpi0 = { 24, 1-127-24+1, 254-127-24+1, 1, SI }; 75 | FPI *fpi, fpi1; 76 | ULong bits[1]; 77 | Long exp; 78 | int k; 79 | 80 | fpi = &fpi0; 81 | if (rounding != FPI_Round_near) { 82 | fpi1 = fpi0; 83 | fpi1.rounding = rounding; 84 | fpi = &fpi1; 85 | } 86 | k = strtodg(s, sp, fpi, &exp, bits); 87 | ULtof((ULong*)f, bits, exp, k); 88 | return k; 89 | } 90 | -------------------------------------------------------------------------------- /strtorx.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #undef _0 35 | #undef _1 36 | 37 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 38 | 39 | #ifdef IEEE_MC68k 40 | #define _0 0 41 | #define _1 1 42 | #define _2 2 43 | #define _3 3 44 | #define _4 4 45 | #endif 46 | #ifdef IEEE_8087 47 | #define _0 4 48 | #define _1 3 49 | #define _2 2 50 | #define _3 1 51 | #define _4 0 52 | #endif 53 | 54 | void 55 | #ifdef KR_headers 56 | ULtox(L, bits, exp, k) UShort *L; ULong *bits; Long exp; int k; 57 | #else 58 | ULtox(UShort *L, ULong *bits, Long exp, int k) 59 | #endif 60 | { 61 | switch(k & STRTOG_Retmask) { 62 | case STRTOG_NoNumber: 63 | case STRTOG_Zero: 64 | L[0] = L[1] = L[2] = L[3] = L[4] = 0; 65 | break; 66 | 67 | case STRTOG_Denormal: 68 | L[_0] = 0; 69 | goto normal_bits; 70 | 71 | case STRTOG_Normal: 72 | case STRTOG_NaNbits: 73 | L[_0] = exp + 0x3fff + 63; 74 | normal_bits: 75 | L[_4] = (UShort)bits[0]; 76 | L[_3] = (UShort)(bits[0] >> 16); 77 | L[_2] = (UShort)bits[1]; 78 | L[_1] = (UShort)(bits[1] >> 16); 79 | break; 80 | 81 | case STRTOG_Infinite: 82 | L[_0] = 0x7fff; 83 | L[_1] = L[_2] = L[_3] = L[_4] = 0; 84 | break; 85 | 86 | case STRTOG_NaN: 87 | L[0] = ldus_QNAN0; 88 | L[1] = ldus_QNAN1; 89 | L[2] = ldus_QNAN2; 90 | L[3] = ldus_QNAN3; 91 | L[4] = ldus_QNAN4; 92 | } 93 | if (k & STRTOG_Neg) 94 | L[_0] |= 0x8000; 95 | } 96 | 97 | int 98 | #ifdef KR_headers 99 | strtorx(s, sp, rounding, L) CONST char *s; char **sp; int rounding; void *L; 100 | #else 101 | strtorx(CONST char *s, char **sp, int rounding, void *L) 102 | #endif 103 | { 104 | static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; 105 | FPI *fpi, fpi1; 106 | ULong bits[2]; 107 | Long exp; 108 | int k; 109 | 110 | fpi = &fpi0; 111 | if (rounding != FPI_Round_near) { 112 | fpi1 = fpi0; 113 | fpi1.rounding = rounding; 114 | fpi = &fpi1; 115 | } 116 | k = strtodg(s, sp, fpi, &exp, bits); 117 | ULtox((UShort*)L, bits, exp, k); 118 | return k; 119 | } 120 | -------------------------------------------------------------------------------- /strtorxL.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2000 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | #undef _0 35 | #undef _1 36 | 37 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 38 | 39 | #ifdef IEEE_MC68k 40 | #define _0 0 41 | #define _1 1 42 | #define _2 2 43 | #endif 44 | #ifdef IEEE_8087 45 | #define _0 2 46 | #define _1 1 47 | #define _2 0 48 | #endif 49 | 50 | void 51 | #ifdef KR_headers 52 | ULtoxL(L, bits, exp, k) ULong *L; ULong *bits; Long exp; int k; 53 | #else 54 | ULtoxL(ULong *L, ULong *bits, Long exp, int k) 55 | #endif 56 | { 57 | switch(k & STRTOG_Retmask) { 58 | case STRTOG_NoNumber: 59 | case STRTOG_Zero: 60 | L[0] = L[1] = L[2] = 0; 61 | break; 62 | 63 | case STRTOG_Normal: 64 | case STRTOG_Denormal: 65 | case STRTOG_NaNbits: 66 | L[_0] = (exp + 0x3fff + 63) << 16; 67 | L[_1] = bits[1]; 68 | L[_2] = bits[0]; 69 | break; 70 | 71 | case STRTOG_Infinite: 72 | L[_0] = 0x7fff << 16; 73 | L[_1] = L[_2] = 0; 74 | break; 75 | 76 | case STRTOG_NaN: 77 | L[0] = ld_QNAN0; 78 | L[1] = ld_QNAN1; 79 | L[2] = ld_QNAN2; 80 | } 81 | if (k & STRTOG_Neg) 82 | L[_0] |= 0x80000000L; 83 | } 84 | 85 | int 86 | #ifdef KR_headers 87 | strtorxL(s, sp, rounding, L) CONST char *s; char **sp; int rounding; void *L; 88 | #else 89 | strtorxL(CONST char *s, char **sp, int rounding, void *L) 90 | #endif 91 | { 92 | static FPI fpi0 = { 64, 1-16383-64+1, 32766 - 16383 - 64 + 1, 1, SI }; 93 | FPI *fpi, fpi1; 94 | ULong bits[2]; 95 | Long exp; 96 | int k; 97 | 98 | fpi = &fpi0; 99 | if (rounding != FPI_Round_near) { 100 | fpi1 = fpi0; 101 | fpi1.rounding = rounding; 102 | fpi = &fpi1; 103 | } 104 | k = strtodg(s, sp, fpi, &exp, bits); 105 | ULtoxL((ULong*)L, bits, exp, k); 106 | return k; 107 | } 108 | -------------------------------------------------------------------------------- /sum.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | Bigint * 35 | #ifdef KR_headers 36 | sum(a, b) Bigint *a; Bigint *b; 37 | #else 38 | sum(Bigint *a, Bigint *b) 39 | #endif 40 | { 41 | Bigint *c; 42 | ULong carry, *xc, *xa, *xb, *xe, y; 43 | #ifdef Pack_32 44 | ULong z; 45 | #endif 46 | 47 | if (a->wds < b->wds) { 48 | c = b; b = a; a = c; 49 | } 50 | c = Balloc(a->k); 51 | c->wds = a->wds; 52 | carry = 0; 53 | xa = a->x; 54 | xb = b->x; 55 | xc = c->x; 56 | xe = xc + b->wds; 57 | #ifdef Pack_32 58 | do { 59 | y = (*xa & 0xffff) + (*xb & 0xffff) + carry; 60 | carry = (y & 0x10000) >> 16; 61 | z = (*xa++ >> 16) + (*xb++ >> 16) + carry; 62 | carry = (z & 0x10000) >> 16; 63 | Storeinc(xc, z, y); 64 | } 65 | while(xc < xe); 66 | xe += a->wds - b->wds; 67 | while(xc < xe) { 68 | y = (*xa & 0xffff) + carry; 69 | carry = (y & 0x10000) >> 16; 70 | z = (*xa++ >> 16) + carry; 71 | carry = (z & 0x10000) >> 16; 72 | Storeinc(xc, z, y); 73 | } 74 | #else 75 | do { 76 | y = *xa++ + *xb++ + carry; 77 | carry = (y & 0x10000) >> 16; 78 | *xc++ = y & 0xffff; 79 | } 80 | while(xc < xe); 81 | xe += a->wds - b->wds; 82 | while(xc < xe) { 83 | y = *xa++ + carry; 84 | carry = (y & 0x10000) >> 16; 85 | *xc++ = y & 0xffff; 86 | } 87 | #endif 88 | if (carry) { 89 | if (c->wds == c->maxwds) { 90 | b = Balloc(c->k + 1); 91 | Bcopy(b, c); 92 | Bfree(c); 93 | c = b; 94 | } 95 | c->x[c->wds++] = 1; 96 | } 97 | return c; 98 | } 99 | -------------------------------------------------------------------------------- /test/Qtest.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998-2001 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | /* Test program for g_Qfmt, strtoIQ, strtopQ, and strtorQ. 33 | * 34 | * Inputs (on stdin): 35 | * r rounding_mode 36 | * n ndig 37 | * number 38 | * #hex0 hex1 hex2 hex3 39 | * 40 | * rounding_mode values: 41 | * 0 = toward zero 42 | * 1 = nearest 43 | * 2 = toward +Infinity 44 | * 3 = toward -Infinity 45 | * 46 | * where number is a decimal floating-point number, 47 | * hex0 is a string of <= 8 Hex digits for the most significant 48 | * word of the number, hex1 is a similar string for the next 49 | * word, etc., and ndig is a parameters to g_Qfmt. 50 | */ 51 | 52 | #include "gdtoa.h" 53 | #include 54 | #include 55 | #include 56 | 57 | extern int getround ANSI((int,char*)); 58 | 59 | static char ibuf[2048], obuf[2048]; 60 | #undef _0 61 | #undef _1 62 | 63 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 64 | 65 | #ifdef IEEE_MC68k 66 | #define _0 0 67 | #define _1 1 68 | #define _2 2 69 | #define _3 3 70 | #endif 71 | #ifdef IEEE_8087 72 | #define _0 3 73 | #define _1 2 74 | #define _2 1 75 | #define _3 0 76 | #endif 77 | 78 | #define U (unsigned long) 79 | 80 | int 81 | main(Void) 82 | { 83 | char *s, *s1, *se, *se1; 84 | int i, dItry, ndig = 0, r = 1; 85 | union { long double d; ULong bits[4]; } u, v[2]; 86 | 87 | while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) { 88 | while(*s <= ' ') 89 | if (!*s++) 90 | continue; 91 | dItry = 0; 92 | switch(*s) { 93 | case 'r': 94 | r = getround(r, s); 95 | continue; 96 | case 'n': 97 | i = s[1]; 98 | if (i <= ' ' || i >= '0' && i <= '9') { 99 | ndig = atoi(s+1); 100 | continue; 101 | } 102 | break; /* nan? */ 103 | case '#': 104 | /* sscanf(s+1, "%lx %lx %lx %lx", &u.bits[_0], */ 105 | /* &u.bits[_1], &u.bits[_2], &u.bits[_3]); */ 106 | u.bits[_0] = (ULong)strtoul(s1 = s+1, &se, 16); 107 | if (se > s1) { 108 | u.bits[_1] = (ULong)strtoul(s1 = se, &se, 16); 109 | if (se > s1) { 110 | u.bits[_2] = (ULong)strtoul(s1 = se, &se, 16); 111 | if (se > s1) 112 | u.bits[_3] = (ULong)strtoul(s1 = se, &se, 16); 113 | } 114 | } 115 | printf("\nInput: %s", ibuf); 116 | printf(" --> f = #%lx %lx %lx %lx\n", u.bits[_0], 117 | u.bits[_1], u.bits[_2], u.bits[_3]); 118 | goto fmt_test; 119 | } 120 | dItry = 1; 121 | printf("\nInput: %s", ibuf); 122 | i = strtorQ(ibuf, &se, r, u.bits); 123 | if (r == 1 && (strtopQ(ibuf,&se1,v[0].bits) != i 124 | || se != se1 || memcmp(u.bits, v[0].bits, 16))) 125 | printf("***strtoQ and strtorQ disagree!!\n:"); 126 | printf("\nstrtoQ consumes %d bytes and returns %d\n", 127 | (int)(se-ibuf), i); 128 | printf("with bits = #%lx %lx %lx %lx\n", 129 | U u.bits[_0], U u.bits[_1], U u.bits[_2], U u.bits[_3]); 130 | fmt_test: 131 | if (sizeof(long double) == 16) 132 | printf("printf(\"%%.35Lg\") gives %.35Lg\n", u.d); 133 | se = g_Qfmt(obuf, u.bits, ndig, sizeof(obuf)); 134 | printf("g_Qfmt(%d) gives %d bytes: \"%s\"\n\n", ndig, 135 | (int)(se-obuf), se ? obuf : ""); 136 | if (!dItry) 137 | continue; 138 | printf("strtoIQ returns %d,", 139 | strtoIQ(ibuf, &se, v[0].bits, v[1].bits)); 140 | printf(" consuming %d bytes.\n", (int)(se-ibuf)); 141 | if (!memcmp(v[0].bits, v[1].bits, 16)) { 142 | if (!memcpy(u.bits, v[0].bits, 16)) 143 | printf("fI[0] == fI[1] == strtoQ\n"); 144 | else { 145 | printf("fI[0] == fI[1] = #%lx %lx %lx %lx\n", 146 | U v[0].bits[_0], U v[0].bits[_1], 147 | U v[0].bits[_2], U v[0].bits[_3]); 148 | if (sizeof(long double) == 16) 149 | printf("= %.35Lg\n", v[0].d); 150 | } 151 | } 152 | else { 153 | printf("fI[0] = #%lx %lx %lx %lx\n", 154 | U v[0].bits[_0], U v[0].bits[_1], 155 | U v[0].bits[_2], U v[0].bits[_3]); 156 | if (sizeof(long double) == 16) 157 | printf("= %.35Lg\n", v[0].d); 158 | printf("fI[1] = #%lx %lx %lx %lx\n", 159 | U v[1].bits[_0], U v[1].bits[_1], 160 | U v[1].bits[_2], U v[1].bits[_3]); 161 | if (sizeof(long double) == 16) 162 | printf("= %.35Lg\n", v[1].d); 163 | if (!memcmp(v[0].bits, u.bits, 16)) 164 | printf("fI[0] == strtod\n"); 165 | else if (!memcmp(v[1].bits, u.bits, 16)) 166 | printf("fI[1] == strtod\n"); 167 | else 168 | printf("**** Both differ from strtod ****\n"); 169 | } 170 | printf("\n"); 171 | } 172 | return 0; 173 | } 174 | -------------------------------------------------------------------------------- /test/README: -------------------------------------------------------------------------------- 1 | This directory contains source for several test programs: 2 | 3 | dt is for conversion to/from double; it permits input of pairs of 4 | 32-bit hex integers as #hhhhhhhh hhhhhhhh (i.e., the initial '#' 5 | indicates hex input). No initial # ==> decimal input. 6 | After the input number is an optional : mode ndigits 7 | (colon, and decimal integers for parameters "mode" and "ndigits" 8 | to gdtoa). 9 | 10 | Qtest, ddtest, dtest, ftest, xLtest and xtest are for conversion to/from 11 | 12 | f IEEE single precision 13 | d IEEE double precision 14 | xL IEEE extended precision, as on Motorola 680x0 chips 15 | x IEEE extended precision, as on Intel 80x87 chips or 16 | software emulation of Motorola 680x0 chips 17 | Q quad precision, as on Sun Sparc chips 18 | dd double double, pairs of IEEE double numbers 19 | whose sum is the desired value 20 | 21 | They're all similar, except for the precision. They test both 22 | directed roundings and interval input (the strtoI* routines). 23 | Lines that begin with "r" specify or interrogate the desired rounding 24 | direction: 25 | 26 | 0 = toward 0 27 | 1 = nearest (default) 28 | 2 = toward +Infinity 29 | 3 = toward -Infinity 30 | 31 | These are the FPI_Round_* values in gdota.h. The "r" value is sticky: 32 | it stays in effect til changed. To change the value, give a line that 33 | starts with r followed by 0, 1, 2, or 3. To check the value, give "r" 34 | by itself. 35 | 36 | Lines that begin with n followed by a number specify the ndig 37 | argument for subsequent calls to the relevant g_*fmt routine. 38 | 39 | Lines that start with # followed by the appropriate number of 40 | hexadecimal strings (see the comments) give the big-endian 41 | internal representation of the desired number. 42 | 43 | When routines Qtest, xLtest, and xtest are used on machines whose 44 | long double is of type "quad" (for Qtest) or "extended" (for x*test), 45 | they try to print with %Lg as another way to show binary values. 46 | 47 | Program ddtest also accepts (white-space separated) pairs of decimal 48 | input numbers; it converts both with strtod and feeds the result 49 | to g_ddfmt. 50 | 51 | Program dItest exercises strtodI and strtoId. 52 | 53 | Programs dItestsi and ddtestsi are for testing the sudden-underflow 54 | logic (on double and double-double conversions). 55 | 56 | Program strtodt tests strtod on some hard cases (in file testnos3) 57 | posted by Fred Tydeman to comp.arch.arithmetic on 26 Feb. 1996. 58 | To get correct results on Intel (x86) systems, the rounding precision 59 | must be set to 53 bits. This can be done, e.g., by invoking 60 | fpinit_ASL(), whose source appears in 61 | http://www.netlib.org/ampl/solvers/fpinit.c . 62 | 63 | These are simple test programs, not meant for exhaustive testing, 64 | but for manually testing "interesting" cases. Paxson's testbase 65 | is good for more exhaustive testing, in part with random inputs. 66 | See ftp://ftp.ee.lbl.gov/testbase-report.ps.Z . 67 | -------------------------------------------------------------------------------- /test/dI.out: -------------------------------------------------------------------------------- 1 | 2 | Input: 1.23 3 | strtodI consumes 4 bytes and returns 17 4 | dd[0] = #3ff3ae14 7ae147ae = 1.23 5 | dd[1] = #3ff3ae14 7ae147af = 1.2300000000000002 6 | 7 | 8 | Input: 1.23e+20 9 | strtodI consumes 8 bytes and returns 1 10 | dd[0] = #441aabdf 2145b430 = 1.23e+20 11 | dd[1] = #441aabdf 2145b430 = 1.23e+20 12 | 13 | 14 | Input: 1.23e-20 15 | strtodI consumes 8 bytes and returns 33 16 | dd[0] = #3bcd0ae4 cf767530 = 1.2299999999999999e-20 17 | dd[1] = #3bcd0ae4 cf767531 = 1.23e-20 18 | 19 | 20 | Input: 1.23456789 21 | strtodI consumes 10 bytes and returns 17 22 | dd[0] = #3ff3c0ca 4283de1b = 1.23456789 23 | dd[1] = #3ff3c0ca 4283de1c = 1.2345678900000001 24 | 25 | 26 | Input: 1.23456589e+20 27 | strtodI consumes 14 bytes and returns 17 28 | dd[0] = #441ac537 a660b997 = 1.23456589e+20 29 | dd[1] = #441ac537 a660b998 = 123456589000000010000 30 | 31 | 32 | Input: 1.23e+30 33 | strtodI consumes 8 bytes and returns 17 34 | dd[0] = #462f0cb0 4e8fb790 = 1.23e+30 35 | dd[1] = #462f0cb0 4e8fb791 = 1.2300000000000001e+30 36 | 37 | 38 | Input: 1.23e-30 39 | strtodI consumes 8 bytes and returns 33 40 | dd[0] = #39b8f286 6f5010aa = 1.2299999999999999e-30 41 | dd[1] = #39b8f286 6f5010ab = 1.23e-30 42 | 43 | 44 | Input: 1.23456789e-20 45 | strtodI consumes 14 bytes and returns 17 46 | dd[0] = #3bcd2681 471e7ada = 1.23456789e-20 47 | dd[1] = #3bcd2681 471e7adb = 1.2345678900000001e-20 48 | 49 | 50 | Input: 1.23456789e-30 51 | strtodI consumes 14 bytes and returns 17 52 | dd[0] = #39b90a3e 33bbd995 = 1.23456789e-30 53 | dd[1] = #39b90a3e 33bbd996 = 1.2345678900000002e-30 54 | 55 | 56 | Input: 1.234567890123456789 57 | strtodI consumes 20 bytes and returns 17 58 | dd[0] = #3ff3c0ca 428c59fb = 1.2345678901234567 59 | dd[1] = #3ff3c0ca 428c59fc = 1.234567890123457 60 | 61 | 62 | Input: 1.23456789012345678901234567890123456789 63 | strtodI consumes 40 bytes and returns 17 64 | dd[0] = #3ff3c0ca 428c59fb = 1.2345678901234567 65 | dd[1] = #3ff3c0ca 428c59fc = 1.234567890123457 66 | 67 | 68 | Input: 1.23e306 69 | strtodI consumes 8 bytes and returns 33 70 | dd[0] = #7f7c0676 cd1c61f4 = 1.2299999999999999e+306 71 | dd[1] = #7f7c0676 cd1c61f5 = 1.23e+306 72 | 73 | 74 | Input: 1.23e-306 75 | strtodI consumes 9 bytes and returns 33 76 | dd[0] = #6ba3b8 5da396e7 = 1.2299999999999999e-306 77 | dd[1] = #6ba3b8 5da396e8 = 1.23e-306 78 | 79 | 80 | Input: 1.23e-320 81 | strtodI consumes 9 bytes and returns 98 82 | dd[0] = #0 9b9 = 1.2297e-320 83 | dd[1] = #0 9ba = 1.23e-320 84 | 85 | 86 | Input: 1.23e-20 87 | strtodI consumes 8 bytes and returns 33 88 | dd[0] = #3bcd0ae4 cf767530 = 1.2299999999999999e-20 89 | dd[1] = #3bcd0ae4 cf767531 = 1.23e-20 90 | 91 | 92 | Input: 1.23456789e307 93 | strtodI consumes 14 bytes and returns 33 94 | dd[0] = #7fb194b1 4bdaecdb = 1.2345678899999998e+307 95 | dd[1] = #7fb194b1 4bdaecdc = 1.23456789e+307 96 | 97 | 98 | Input: 1.23456589e-307 99 | strtodI consumes 15 bytes and returns 17 100 | dd[0] = #363196 bb9845fa = 1.23456589e-307 101 | dd[1] = #363196 bb9845fb = 1.2345658900000001e-307 102 | 103 | 104 | Input: 1.234567890123456789 105 | strtodI consumes 20 bytes and returns 17 106 | dd[0] = #3ff3c0ca 428c59fb = 1.2345678901234567 107 | dd[1] = #3ff3c0ca 428c59fc = 1.234567890123457 108 | 109 | 110 | Input: 1.234567890123456789e301 111 | strtodI consumes 24 bytes and returns 17 112 | dd[0] = #7e726f51 75f56413 = 1.2345678901234568e+301 113 | dd[1] = #7e726f51 75f56414 = 1.234567890123457e+301 114 | 115 | 116 | Input: 1.234567890123456789e-301 117 | strtodI consumes 25 bytes and returns 17 118 | dd[0] = #1752a64 e34ba0d3 = 1.2345678901234567e-301 119 | dd[1] = #1752a64 e34ba0d4 = 1.234567890123457e-301 120 | 121 | 122 | Input: 1.234567890123456789e-321 123 | strtodI consumes 25 bytes and returns 98 124 | dd[0] = #0 f9 = 1.23e-321 125 | dd[1] = #0 fa = 1.235e-321 126 | 127 | 128 | Input: 1e23 129 | strtodI consumes 4 bytes and returns 17 130 | dd[0] = #44b52d02 c7e14af6 = 1e+23 131 | dd[1] = #44b52d02 c7e14af7 = 1.0000000000000001e+23 132 | 133 | 134 | Input: 1e310 135 | strtodI consumes 5 bytes and returns 163 136 | dd[0] = #7fefffff ffffffff = 1.7976931348623157e+308 137 | dd[1] = #7ff00000 0 = Infinity 138 | 139 | 140 | Input: 9.0259718793241475e-277 141 | strtodI consumes 23 bytes and returns 33 142 | dd[0] = #69fffff ffffffff = 9.025971879324147e-277 143 | dd[1] = #6a00000 0 = 9.025971879324148e-277 144 | 145 | 146 | Input: 9.025971879324147880346310405869e-277 147 | strtodI consumes 37 bytes and returns 17 148 | dd[0] = #6a00000 0 = 9.025971879324148e-277 149 | dd[1] = #6a00000 1 = 9.02597187932415e-277 150 | 151 | 152 | Input: 9.025971879324147880346310405868e-277 153 | strtodI consumes 37 bytes and returns 33 154 | dd[0] = #69fffff ffffffff = 9.025971879324147e-277 155 | dd[1] = #6a00000 0 = 9.025971879324148e-277 156 | 157 | 158 | Input: 2.2250738585072014e-308 159 | strtodI consumes 23 bytes and returns 17 160 | dd[0] = #100000 0 = 2.2250738585072014e-308 161 | dd[1] = #100000 1 = 2.225073858507202e-308 162 | 163 | 164 | Input: 2.2250738585072013e-308 165 | strtodI consumes 23 bytes and returns 33 166 | dd[0] = #fffff ffffffff = 2.225073858507201e-308 167 | dd[1] = #100000 0 = 2.2250738585072014e-308 168 | 169 | -------------------------------------------------------------------------------- /test/dIsi.out: -------------------------------------------------------------------------------- 1 | 2 | Input: 1.23 3 | strtodI consumes 4 bytes and returns 17 4 | dd[0] = #3ff3ae14 7ae147ae = 1.23 5 | dd[1] = #3ff3ae14 7ae147af = 1.2300000000000002 6 | 7 | 8 | Input: 1.23e+20 9 | strtodI consumes 8 bytes and returns 1 10 | dd[0] = #441aabdf 2145b430 = 1.23e+20 11 | dd[1] = #441aabdf 2145b430 = 1.23e+20 12 | 13 | 14 | Input: 1.23e-20 15 | strtodI consumes 8 bytes and returns 33 16 | dd[0] = #3bcd0ae4 cf767530 = 1.2299999999999999e-20 17 | dd[1] = #3bcd0ae4 cf767531 = 1.23e-20 18 | 19 | 20 | Input: 1.23456789 21 | strtodI consumes 10 bytes and returns 17 22 | dd[0] = #3ff3c0ca 4283de1b = 1.23456789 23 | dd[1] = #3ff3c0ca 4283de1c = 1.2345678900000001 24 | 25 | 26 | Input: 1.23456589e+20 27 | strtodI consumes 14 bytes and returns 17 28 | dd[0] = #441ac537 a660b997 = 1.23456589e+20 29 | dd[1] = #441ac537 a660b998 = 123456589000000010000 30 | 31 | 32 | Input: 1.23e+30 33 | strtodI consumes 8 bytes and returns 17 34 | dd[0] = #462f0cb0 4e8fb790 = 1.23e+30 35 | dd[1] = #462f0cb0 4e8fb791 = 1.2300000000000001e+30 36 | 37 | 38 | Input: 1.23e-30 39 | strtodI consumes 8 bytes and returns 33 40 | dd[0] = #39b8f286 6f5010aa = 1.2299999999999999e-30 41 | dd[1] = #39b8f286 6f5010ab = 1.23e-30 42 | 43 | 44 | Input: 1.23456789e-20 45 | strtodI consumes 14 bytes and returns 17 46 | dd[0] = #3bcd2681 471e7ada = 1.23456789e-20 47 | dd[1] = #3bcd2681 471e7adb = 1.2345678900000001e-20 48 | 49 | 50 | Input: 1.23456789e-30 51 | strtodI consumes 14 bytes and returns 17 52 | dd[0] = #39b90a3e 33bbd995 = 1.23456789e-30 53 | dd[1] = #39b90a3e 33bbd996 = 1.2345678900000002e-30 54 | 55 | 56 | Input: 1.234567890123456789 57 | strtodI consumes 20 bytes and returns 17 58 | dd[0] = #3ff3c0ca 428c59fb = 1.2345678901234567 59 | dd[1] = #3ff3c0ca 428c59fc = 1.234567890123457 60 | 61 | 62 | Input: 1.23456789012345678901234567890123456789 63 | strtodI consumes 40 bytes and returns 17 64 | dd[0] = #3ff3c0ca 428c59fb = 1.2345678901234567 65 | dd[1] = #3ff3c0ca 428c59fc = 1.234567890123457 66 | 67 | 68 | Input: 1.23e306 69 | strtodI consumes 8 bytes and returns 33 70 | dd[0] = #7f7c0676 cd1c61f4 = 1.2299999999999999e+306 71 | dd[1] = #7f7c0676 cd1c61f5 = 1.23e+306 72 | 73 | 74 | Input: 1.23e-306 75 | strtodI consumes 9 bytes and returns 33 76 | dd[0] = #6ba3b8 5da396e7 = 1.2299999999999999e-306 77 | dd[1] = #6ba3b8 5da396e8 = 1.23e-306 78 | 79 | 80 | Input: 1.23e-320 81 | strtodI consumes 9 bytes and returns 80 82 | dd[0] = #0 0 = 0 83 | dd[1] = #100000 0 = 2.2250738585072014e-308 84 | 85 | 86 | Input: 1.23e-20 87 | strtodI consumes 8 bytes and returns 33 88 | dd[0] = #3bcd0ae4 cf767530 = 1.2299999999999999e-20 89 | dd[1] = #3bcd0ae4 cf767531 = 1.23e-20 90 | 91 | 92 | Input: 1.23456789e307 93 | strtodI consumes 14 bytes and returns 33 94 | dd[0] = #7fb194b1 4bdaecdb = 1.2345678899999998e+307 95 | dd[1] = #7fb194b1 4bdaecdc = 1.23456789e+307 96 | 97 | 98 | Input: 1.23456589e-307 99 | strtodI consumes 15 bytes and returns 17 100 | dd[0] = #363196 bb9845fa = 1.23456589e-307 101 | dd[1] = #363196 bb9845fb = 1.2345658900000001e-307 102 | 103 | 104 | Input: 1.234567890123456789 105 | strtodI consumes 20 bytes and returns 17 106 | dd[0] = #3ff3c0ca 428c59fb = 1.2345678901234567 107 | dd[1] = #3ff3c0ca 428c59fc = 1.234567890123457 108 | 109 | 110 | Input: 1.234567890123456789e301 111 | strtodI consumes 24 bytes and returns 17 112 | dd[0] = #7e726f51 75f56413 = 1.2345678901234568e+301 113 | dd[1] = #7e726f51 75f56414 = 1.234567890123457e+301 114 | 115 | 116 | Input: 1.234567890123456789e-301 117 | strtodI consumes 25 bytes and returns 17 118 | dd[0] = #1752a64 e34ba0d3 = 1.2345678901234567e-301 119 | dd[1] = #1752a64 e34ba0d4 = 1.234567890123457e-301 120 | 121 | 122 | Input: 1.234567890123456789e-321 123 | strtodI consumes 25 bytes and returns 80 124 | dd[0] = #0 0 = 0 125 | dd[1] = #100000 0 = 2.2250738585072014e-308 126 | 127 | 128 | Input: 1e23 129 | strtodI consumes 4 bytes and returns 17 130 | dd[0] = #44b52d02 c7e14af6 = 1e+23 131 | dd[1] = #44b52d02 c7e14af7 = 1.0000000000000001e+23 132 | 133 | 134 | Input: 1e310 135 | strtodI consumes 5 bytes and returns 163 136 | dd[0] = #7fefffff ffffffff = 1.7976931348623157e+308 137 | dd[1] = #7ff00000 0 = Infinity 138 | 139 | 140 | Input: 9.0259718793241475e-277 141 | strtodI consumes 23 bytes and returns 33 142 | dd[0] = #69fffff ffffffff = 9.025971879324147e-277 143 | dd[1] = #6a00000 0 = 9.025971879324148e-277 144 | 145 | 146 | Input: 9.025971879324147880346310405869e-277 147 | strtodI consumes 37 bytes and returns 17 148 | dd[0] = #6a00000 0 = 9.025971879324148e-277 149 | dd[1] = #6a00000 1 = 9.02597187932415e-277 150 | 151 | 152 | Input: 9.025971879324147880346310405868e-277 153 | strtodI consumes 37 bytes and returns 33 154 | dd[0] = #69fffff ffffffff = 9.025971879324147e-277 155 | dd[1] = #6a00000 0 = 9.025971879324148e-277 156 | 157 | 158 | Input: 2.2250738585072014e-308 159 | strtodI consumes 23 bytes and returns 17 160 | dd[0] = #100000 0 = 2.2250738585072014e-308 161 | dd[1] = #100000 1 = 2.225073858507202e-308 162 | 163 | 164 | Input: 2.2250738585072013e-308 165 | strtodI consumes 23 bytes and returns 33 166 | dd[0] = #0 0 = 0 167 | dd[1] = #100000 0 = 2.2250738585072014e-308 168 | 169 | -------------------------------------------------------------------------------- /test/dItest.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 2001 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | #include 34 | #include 35 | 36 | static char ibuf[2048]; 37 | 38 | #define U (unsigned long) 39 | 40 | static void 41 | #ifdef KR_headers 42 | dshow(what, d) char *what; double d; 43 | #else 44 | dshow(char *what, double d) 45 | #endif 46 | { 47 | char buf[32]; 48 | g_dfmt(buf, &d, 0, sizeof(buf)); 49 | printf("%s = #%lx %lx = %s\n", what, 50 | U ((ULong*)&d)[_0], U ((ULong*)&d)[_1], buf); 51 | } 52 | 53 | int 54 | main(Void) 55 | { 56 | /* Input: one number per line */ 57 | 58 | char *s, *se, *se1; 59 | int i, j; 60 | double dd[2], dd1, dd2; 61 | static char cfmt[] = "%s consumes %d bytes and returns %d\n"; 62 | 63 | while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) { 64 | while(*s <= ' ') 65 | if (!*s++) 66 | continue; 67 | printf("\nInput: %s", ibuf); 68 | i = strtodI(ibuf, &se, dd); 69 | printf(cfmt, "strtodI", (int)(se-ibuf), i); 70 | dshow("dd[0]", dd[0]); 71 | dshow("dd[1]", dd[1]); 72 | printf("\n"); 73 | j = strtoId(ibuf, &se1, &dd1, &dd2); 74 | if (j != i || se != se1 75 | || dd[0] != dd1 || dd[1] != dd2) { 76 | printf(cfmt, "**** strtoId", (int)(se-ibuf), j); 77 | dshow("dd1", dd1); 78 | dshow("dd2", dd2); 79 | } 80 | } 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /test/ddsi.out: -------------------------------------------------------------------------------- 1 | 2 | Input: 1.23 3 | strtopdd consumes 4 bytes and returns 17 4 | dd[0] = 1.23 = #3ff3ae14 7ae147ae 5 | dd[1] = 1.7763568394002496e-17 = #3c747ae1 47ae1478 6 | g_ddfmt(0) gives 4 bytes: "1.23" 7 | 8 | strtoIdd returns 17, consuming 4 bytes. 9 | ddI[0] = #3ff3ae14 7ae147ae + 3c747ae1 47ae1478 10 | = 1.23 + 1.7763568394002496e-17 11 | ddI[1] = #3ff3ae14 7ae147ae + 3c747ae1 47ae1480 12 | = 1.23 + 1.776356839400252e-17 13 | ddI[0] == strtod 14 | 15 | 16 | Input: 1.23e+20 17 | strtopdd consumes 8 bytes and returns 1 18 | dd[0] = 1.23e+20 = #441aabdf 2145b430 19 | dd[1] = 0 = #0 0 20 | g_ddfmt(0) gives 8 bytes: "1.23e+20" 21 | 22 | strtoIdd returns 1, consuming 8 bytes. 23 | ddI[0] == ddI[1] == strtopdd 24 | 25 | 26 | Input: 1.23e-20 27 | strtopdd consumes 8 bytes and returns 33 28 | dd[0] = 1.2299999999999999e-20 = #3bcd0ae4 cf767530 29 | dd[1] = 9.304023318521521e-37 = #3873c997 955b2691 30 | g_ddfmt(0) gives 8 bytes: "1.23e-20" 31 | 32 | strtoIdd returns 33, consuming 8 bytes. 33 | ddI[0] = #3bcd0ae4 cf767530 + 3873c997 955b2690 34 | = 1.2299999999999999e-20 + 9.3040233185215194e-37 35 | ddI[1] = #3bcd0ae4 cf767530 + 3873c997 955b2691 36 | = 1.2299999999999999e-20 + 9.3040233185215211e-37 37 | ddI[1] == strtod 38 | 39 | 40 | Input: 1.23456789 41 | strtopdd consumes 10 bytes and returns 17 42 | dd[0] = 1.23456789 = #3ff3c0ca 4283de1b 43 | dd[1] = 1.0990618193318369e-16 = #3c9fada5 144c1252 44 | g_ddfmt(0) gives 10 bytes: "1.23456789" 45 | 46 | strtoIdd returns 17, consuming 10 bytes. 47 | ddI[0] = #3ff3c0ca 4283de1b + 3c9fada5 144c1252 48 | = 1.2345678899999999 + 1.0990618193318369e-16 49 | ddI[1] = #3ff3c0ca 4283de1b + 3c9fada5 144c1254 50 | = 1.2345678899999999 + 1.0990618193318371e-16 51 | ddI[0] == strtod 52 | 53 | 54 | Input: 1.23456589e+20 55 | strtopdd consumes 14 bytes and returns 1 56 | dd[0] = 1.23456589e+20 = #441ac537 a660b997 57 | dd[1] = 4096 = #40b00000 0 58 | g_ddfmt(0) gives 14 bytes: "1.23456589e+20" 59 | 60 | strtoIdd returns 1, consuming 14 bytes. 61 | ddI[0] == ddI[1] == strtopdd 62 | 63 | 64 | Input: 1.23e+30 65 | strtopdd consumes 8 bytes and returns 1 66 | dd[0] = 1.23e+30 = #462f0cb0 4e8fb790 67 | dd[1] = 40281156091904 = #42c25158 0 68 | g_ddfmt(0) gives 8 bytes: "1.23e+30" 69 | 70 | strtoIdd returns 1, consuming 8 bytes. 71 | ddI[0] == ddI[1] == strtopdd 72 | 73 | 74 | Input: 1.23e-30 75 | strtopdd consumes 8 bytes and returns 17 76 | dd[0] = 1.2299999999999999e-30 = #39b8f286 6f5010aa 77 | dd[1] = 1.076909723013918e-46 = #3663ac7f 3dafd174 78 | g_ddfmt(0) gives 8 bytes: "1.23e-30" 79 | 80 | strtoIdd returns 17, consuming 8 bytes. 81 | ddI[0] = #39b8f286 6f5010aa + 3663ac7f 3dafd174 82 | = 1.2299999999999999e-30 + 1.076909723013918e-46 83 | ddI[1] = #39b8f286 6f5010aa + 3663ac7f 3dafd175 84 | = 1.2299999999999999e-30 + 1.0769097230139181e-46 85 | ddI[0] == strtod 86 | 87 | 88 | Input: 1.23456789e-20 89 | strtopdd consumes 14 bytes and returns 17 90 | dd[0] = 1.23456789e-20 = #3bcd2681 471e7ada 91 | dd[1] = 6.247111971663133e-37 = #386a9280 a761b07e 92 | g_ddfmt(0) gives 14 bytes: "1.23456789e-20" 93 | 94 | strtoIdd returns 17, consuming 14 bytes. 95 | ddI[0] = #3bcd2681 471e7ada + 386a9280 a761b07e 96 | = 1.2345678899999999e-20 + 6.2471119716631328e-37 97 | ddI[1] = #3bcd2681 471e7ada + 386a9280 a761b080 98 | = 1.2345678899999999e-20 + 6.2471119716631345e-37 99 | ddI[0] == strtod 100 | 101 | 102 | Input: 1.23456789e-30 103 | strtopdd consumes 14 bytes and returns 33 104 | dd[0] = 1.23456789e-30 = #39b90a3e 33bbd995 105 | dd[1] = 2.1567930523648577e-47 = #363f8585 55a6b1a0 106 | g_ddfmt(0) gives 14 bytes: "1.23456789e-30" 107 | 108 | strtoIdd returns 33, consuming 14 bytes. 109 | ddI[0] = #39b90a3e 33bbd995 + 363f8585 55a6b198 110 | = 1.23456789e-30 + 2.1567930523648558e-47 111 | ddI[1] = #39b90a3e 33bbd995 + 363f8585 55a6b1a0 112 | = 1.23456789e-30 + 2.1567930523648577e-47 113 | ddI[1] == strtod 114 | 115 | 116 | Input: 1.234567890123456789 117 | strtopdd consumes 20 bytes and returns 33 118 | dd[0] = 1.2345678901234567 = #3ff3c0ca 428c59fb 119 | dd[1] = 9.856786452588859e-17 = #3c9c6906 51a3745e 120 | g_ddfmt(0) gives 20 bytes: "1.234567890123456789" 121 | 122 | strtoIdd returns 33, consuming 20 bytes. 123 | ddI[0] = #3ff3c0ca 428c59fb + 3c9c6906 51a3745c 124 | = 1.2345678901234567 + 9.8567864525888563e-17 125 | ddI[1] = #3ff3c0ca 428c59fb + 3c9c6906 51a3745e 126 | = 1.2345678901234567 + 9.8567864525888588e-17 127 | ddI[1] == strtod 128 | 129 | 130 | Input: 1.23456789012345678901234567890123456789 131 | strtopdd consumes 40 bytes and returns 33 132 | dd[0] = 1.2345678901234567 = #3ff3c0ca 428c59fb 133 | dd[1] = 9.858021020478982e-17 = #3c9c69ef 85adadb6 134 | g_ddfmt(0) gives 34 bytes: "1.23456789012345678901234567890124" 135 | 136 | strtoIdd returns 33, consuming 40 bytes. 137 | ddI[0] = #3ff3c0ca 428c59fb + 3c9c69ef 85adadb4 138 | = 1.2345678901234567 + 9.8580210204789798e-17 139 | ddI[1] = #3ff3c0ca 428c59fb + 3c9c69ef 85adadb6 140 | = 1.2345678901234567 + 9.8580210204789823e-17 141 | ddI[1] == strtod 142 | 143 | 144 | Input: 1.23e306 145 | strtopdd consumes 8 bytes and returns 33 146 | dd[0] = 1.2299999999999999e+306 = #7f7c0676 cd1c61f4 147 | dd[1] = 1.3319001448659015e+290 = #7c2b558b e3d3f477 148 | g_ddfmt(0) gives 9 bytes: "1.23e+306" 149 | 150 | strtoIdd returns 33, consuming 8 bytes. 151 | ddI[0] = #7f7c0676 cd1c61f4 + 7c2b558b e3d3f476 152 | = 1.2299999999999999e+306 + 1.3319001448659013e+290 153 | ddI[1] = #7f7c0676 cd1c61f4 + 7c2b558b e3d3f477 154 | = 1.2299999999999999e+306 + 1.3319001448659015e+290 155 | ddI[1] == strtod 156 | 157 | 158 | Input: 1.23e-306 159 | strtopdd consumes 9 bytes and returns 80 160 | dd[0] = 0 = #0 0 161 | dd[1] = 0 = #0 0 162 | g_ddfmt(0) gives 1 bytes: "0" 163 | 164 | strtoIdd returns 80, consuming 9 bytes. 165 | ddI[0] = #0 0 + 0 0 166 | = 0 + 0 167 | ddI[1] = #6a00000 0 + 0 0 168 | = 9.0259718793241479e-277 + 0 169 | ddI[0] == strtod 170 | 171 | 172 | Input: 1.23e-320 173 | strtopdd consumes 9 bytes and returns 80 174 | dd[0] = 0 = #0 0 175 | dd[1] = 0 = #0 0 176 | g_ddfmt(0) gives 1 bytes: "0" 177 | 178 | strtoIdd returns 80, consuming 9 bytes. 179 | ddI[0] = #0 0 + 0 0 180 | = 0 + 0 181 | ddI[1] = #6a00000 0 + 0 0 182 | = 9.0259718793241479e-277 + 0 183 | ddI[0] == strtod 184 | 185 | 186 | Input: 1.23e-20 187 | strtopdd consumes 8 bytes and returns 33 188 | dd[0] = 1.2299999999999999e-20 = #3bcd0ae4 cf767530 189 | dd[1] = 9.304023318521521e-37 = #3873c997 955b2691 190 | g_ddfmt(0) gives 8 bytes: "1.23e-20" 191 | 192 | strtoIdd returns 33, consuming 8 bytes. 193 | ddI[0] = #3bcd0ae4 cf767530 + 3873c997 955b2690 194 | = 1.2299999999999999e-20 + 9.3040233185215194e-37 195 | ddI[1] = #3bcd0ae4 cf767530 + 3873c997 955b2691 196 | = 1.2299999999999999e-20 + 9.3040233185215211e-37 197 | ddI[1] == strtod 198 | 199 | 200 | Input: 1.23456789e307 201 | strtopdd consumes 14 bytes and returns 33 202 | dd[0] = 1.2345678899999998e+307 = #7fb194b1 4bdaecdb 203 | dd[1] = 2.0137933598720243e+291 = #7c69d48d 192048ca 204 | g_ddfmt(0) gives 15 bytes: "1.23456789e+307" 205 | 206 | strtoIdd returns 33, consuming 14 bytes. 207 | ddI[0] = #7fb194b1 4bdaecdb + 7c69d48d 192048c9 208 | = 1.2345678899999998e+307 + 2.013793359872024e+291 209 | ddI[1] = #7fb194b1 4bdaecdb + 7c69d48d 192048ca 210 | = 1.2345678899999998e+307 + 2.0137933598720243e+291 211 | ddI[1] == strtod 212 | 213 | 214 | Input: 1.23456589e-307 215 | strtopdd consumes 15 bytes and returns 80 216 | dd[0] = 0 = #0 0 217 | dd[1] = 0 = #0 0 218 | g_ddfmt(0) gives 1 bytes: "0" 219 | 220 | strtoIdd returns 80, consuming 15 bytes. 221 | ddI[0] = #0 0 + 0 0 222 | = 0 + 0 223 | ddI[1] = #6a00000 0 + 0 0 224 | = 9.0259718793241479e-277 + 0 225 | ddI[0] == strtod 226 | 227 | 228 | Input: 1.234567890123456789 229 | strtopdd consumes 20 bytes and returns 33 230 | dd[0] = 1.2345678901234567 = #3ff3c0ca 428c59fb 231 | dd[1] = 9.856786452588859e-17 = #3c9c6906 51a3745e 232 | g_ddfmt(0) gives 20 bytes: "1.234567890123456789" 233 | 234 | strtoIdd returns 33, consuming 20 bytes. 235 | ddI[0] = #3ff3c0ca 428c59fb + 3c9c6906 51a3745c 236 | = 1.2345678901234567 + 9.8567864525888563e-17 237 | ddI[1] = #3ff3c0ca 428c59fb + 3c9c6906 51a3745e 238 | = 1.2345678901234567 + 9.8567864525888588e-17 239 | ddI[1] == strtod 240 | 241 | 242 | Input: 1.234567890123456789e301 243 | strtopdd consumes 24 bytes and returns 33 244 | dd[0] = 1.2345678901234568e+301 = #7e726f51 75f56413 245 | dd[1] = 1.3892003943918827e+283 = #7ab7ea80 76399100 246 | g_ddfmt(0) gives 25 bytes: "1.234567890123456789e+301" 247 | 248 | strtoIdd returns 33, consuming 24 bytes. 249 | ddI[0] = #7e726f51 75f56413 + 7ab7ea80 76399080 250 | = 1.2345678901234568e+301 + 1.3892003943918563e+283 251 | ddI[1] = #7e726f51 75f56413 + 7ab7ea80 76399100 252 | = 1.2345678901234568e+301 + 1.3892003943918827e+283 253 | ddI[1] == strtod 254 | 255 | 256 | Input: 1.234567890123456789e-301 257 | strtopdd consumes 25 bytes and returns 80 258 | dd[0] = 0 = #0 0 259 | dd[1] = 0 = #0 0 260 | g_ddfmt(0) gives 1 bytes: "0" 261 | 262 | strtoIdd returns 80, consuming 25 bytes. 263 | ddI[0] = #0 0 + 0 0 264 | = 0 + 0 265 | ddI[1] = #6a00000 0 + 0 0 266 | = 9.0259718793241479e-277 + 0 267 | ddI[0] == strtod 268 | 269 | 270 | Input: 1.234567890123456789e-321 271 | strtopdd consumes 25 bytes and returns 80 272 | dd[0] = 0 = #0 0 273 | dd[1] = 0 = #0 0 274 | g_ddfmt(0) gives 1 bytes: "0" 275 | 276 | strtoIdd returns 80, consuming 25 bytes. 277 | ddI[0] = #0 0 + 0 0 278 | = 0 + 0 279 | ddI[1] = #6a00000 0 + 0 0 280 | = 9.0259718793241479e-277 + 0 281 | ddI[0] == strtod 282 | 283 | 284 | Input: 1e23 285 | strtopdd consumes 4 bytes and returns 1 286 | dd[0] = 1e+23 = #44b52d02 c7e14af6 287 | dd[1] = 8388608 = #41600000 0 288 | g_ddfmt(0) gives 5 bytes: "1e+23" 289 | 290 | strtoIdd returns 1, consuming 4 bytes. 291 | ddI[0] == ddI[1] == strtopdd 292 | 293 | 294 | Input: 1e310 295 | strtopdd consumes 5 bytes and returns 163 296 | dd[0] = Infinity = #7ff00000 0 297 | dd[1] = Infinity = #7ff00000 0 298 | g_ddfmt(0) gives 8 bytes: "Infinity" 299 | 300 | strtoIdd returns 163, consuming 5 bytes. 301 | ddI[0] = #7fefffff ffffffff + 7c9fffff ffffffff 302 | = 1.7976931348623157e+308 + 1.9958403095347196e+292 303 | ddI[1] = #7ff00000 0 + 7ff00000 0 304 | = Infinity + Infinity 305 | ddI[1] == strtod 306 | 307 | 308 | Input: 9.0259718793241475e-277 309 | strtopdd consumes 23 bytes and returns 80 310 | dd[0] = 0 = #0 0 311 | dd[1] = 0 = #0 0 312 | g_ddfmt(0) gives 1 bytes: "0" 313 | 314 | strtoIdd returns 80, consuming 23 bytes. 315 | ddI[0] = #0 0 + 0 0 316 | = 0 + 0 317 | ddI[1] = #6a00000 0 + 0 0 318 | = 9.0259718793241479e-277 + 0 319 | ddI[0] == strtod 320 | 321 | 322 | Input: 9.025971879324147880346310405869e-277 323 | strtopdd consumes 37 bytes and returns 17 324 | dd[0] = 9.025971879324148e-277 = #6a00000 0 325 | dd[1] = 2.2250738585072014e-308 = #100000 0 326 | g_ddfmt(0) gives 37 bytes: "9.025971879324147880346310405869e-277" 327 | 328 | strtoIdd returns 17, consuming 37 bytes. 329 | ddI[0] = #6a00000 0 + 100000 0 330 | = 9.0259718793241479e-277 + 2.2250738585072014e-308 331 | ddI[1] = #6a00000 0 + 200000 0 332 | = 9.0259718793241479e-277 + 4.4501477170144028e-308 333 | ddI[0] == strtod 334 | 335 | 336 | Input: 9.025971879324147880346310405868e-277 337 | strtopdd consumes 37 bytes and returns 80 338 | dd[0] = 0 = #0 0 339 | dd[1] = 0 = #0 0 340 | g_ddfmt(0) gives 1 bytes: "0" 341 | 342 | strtoIdd returns 80, consuming 37 bytes. 343 | ddI[0] = #0 0 + 0 0 344 | = 0 + 0 345 | ddI[1] = #6a00000 0 + 0 0 346 | = 9.0259718793241479e-277 + 0 347 | ddI[0] == strtod 348 | 349 | 350 | Input: 2.2250738585072014e-308 351 | strtopdd consumes 23 bytes and returns 80 352 | dd[0] = 0 = #0 0 353 | dd[1] = 0 = #0 0 354 | g_ddfmt(0) gives 1 bytes: "0" 355 | 356 | strtoIdd returns 80, consuming 23 bytes. 357 | ddI[0] = #0 0 + 0 0 358 | = 0 + 0 359 | ddI[1] = #6a00000 0 + 0 0 360 | = 9.0259718793241479e-277 + 0 361 | ddI[0] == strtod 362 | 363 | 364 | Input: 2.2250738585072013e-308 365 | strtopdd consumes 23 bytes and returns 80 366 | dd[0] = 0 = #0 0 367 | dd[1] = 0 = #0 0 368 | g_ddfmt(0) gives 1 bytes: "0" 369 | 370 | strtoIdd returns 80, consuming 23 bytes. 371 | ddI[0] = #0 0 + 0 0 372 | = 0 + 0 373 | ddI[1] = #6a00000 0 + 0 0 374 | = 9.0259718793241479e-277 + 0 375 | ddI[0] == strtod 376 | 377 | -------------------------------------------------------------------------------- /test/ddtest.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998-2001 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | /* Test program for g_ddfmt, strtoIdd, strtopdd, and strtordd. 33 | * 34 | * Inputs (on stdin): 35 | * r rounding_mode 36 | * n ndig 37 | * number 38 | * #hex0 hex1 hex2 hex3 39 | * 40 | * rounding_mode values: 41 | * 0 = toward zero 42 | * 1 = nearest 43 | * 2 = toward +Infinity 44 | * 3 = toward -Infinity 45 | * 46 | * where number is a decimal floating-point number, 47 | * hex0 is a string of <= 8 Hex digits for the most significant 48 | * word of the number, hex1 is a similar string for the next 49 | * word, etc., and ndig is a parameters to g_ddfmt. 50 | */ 51 | 52 | #include "gdtoaimp.h" 53 | #include 54 | #include 55 | 56 | extern int getround ANSI((int,char*)); 57 | 58 | static char ibuf[2048], obuf[1024]; 59 | 60 | #define U (unsigned long) 61 | 62 | static void 63 | #ifdef KR_headers 64 | dprint(what, d) char *what; double d; 65 | #else 66 | dprint(char *what, double d) 67 | #endif 68 | { 69 | char buf[32]; 70 | union { double d; ULong L[2]; } u; 71 | 72 | u.d = d; 73 | g_dfmt(buf,&d,0,sizeof(buf)); 74 | printf("%s = %s = #%lx %lx\n", what, buf, U u.L[_0], U u.L[_1]); 75 | } 76 | 77 | int 78 | main(Void) 79 | { 80 | char *s, *s1, *se, *se1; 81 | int dItry, i, j, r = 1, ndig = 0; 82 | double ddI[4]; 83 | long LL[4]; 84 | union { double dd[2]; ULong L[4]; } u; 85 | 86 | while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) { 87 | while(*s <= ' ') 88 | if (!*s++) 89 | continue; 90 | dItry = 0; 91 | switch(*s) { 92 | case 'r': 93 | r = getround(r, s); 94 | continue; 95 | case 'n': 96 | i = s[1]; 97 | if (i <= ' ' || i >= '0' && i <= '9') { 98 | ndig = atoi(s+1); 99 | continue; 100 | } 101 | break; /* nan? */ 102 | case '#': 103 | LL[0] = u.L[_0]; 104 | LL[1] = u.L[_1]; 105 | LL[2] = u.L[2+_0]; 106 | LL[3] = u.L[2+_1]; 107 | sscanf(s+1, "%lx %lx %lx %lx", &LL[0], &LL[1], 108 | &LL[2], &LL[3]); 109 | u.L[_0] = LL[0]; 110 | u.L[_1] = LL[1]; 111 | u.L[2+_0] = LL[2]; 112 | u.L[2+_1] = LL[3]; 113 | printf("\nInput: %s", ibuf); 114 | printf(" --> f = #%lx %lx %lx %lx\n", 115 | LL[0],LL[1],LL[2],LL[3]); 116 | goto fmt_test; 117 | } 118 | printf("\nInput: %s", ibuf); 119 | for(s1 = s; *s1 > ' '; s1++){}; 120 | while(*s1 <= ' ' && *s1) s1++; 121 | if (!*s1) { 122 | dItry = 1; 123 | i = strtordd(ibuf, &se, r, u.dd); 124 | if (r == 1) { 125 | j = strtopdd(ibuf, &se1, ddI); 126 | if (i != j || u.dd[0] != ddI[0] 127 | || u.dd[1] != ddI[1] || se != se1) 128 | printf("***strtopdd and strtordd disagree!!\n:"); 129 | } 130 | printf("strtopdd consumes %d bytes and returns %d\n", 131 | (int)(se-ibuf), i); 132 | } 133 | else { 134 | u.dd[0] = strtod(s, &se); 135 | u.dd[1] = strtod(se, &se); 136 | } 137 | fmt_test: 138 | dprint("dd[0]", u.dd[0]); 139 | dprint("dd[1]", u.dd[1]); 140 | se = g_ddfmt(obuf, u.dd, ndig, sizeof(obuf)); 141 | printf("g_ddfmt(%d) gives %d bytes: \"%s\"\n\n", 142 | ndig, (int)(se-obuf), se ? obuf : ""); 143 | if (!dItry) 144 | continue; 145 | printf("strtoIdd returns %d,", strtoIdd(ibuf, &se, ddI,&ddI[2])); 146 | printf(" consuming %d bytes.\n", (int)(se-ibuf)); 147 | if (ddI[0] == ddI[2] && ddI[1] == ddI[3]) { 148 | if (ddI[0] == u.dd[0] && ddI[1] == u.dd[1]) 149 | printf("ddI[0] == ddI[1] == strtopdd\n"); 150 | else 151 | printf("ddI[0] == ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %17.g\n", 152 | U ((ULong*)ddI)[_0], 153 | U ((ULong*)ddI)[_1], 154 | U ((ULong*)ddI)[2+_0], 155 | U ((ULong*)ddI)[2+_1], 156 | ddI[0], ddI[1]); 157 | } 158 | else { 159 | printf("ddI[0] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n", 160 | U ((ULong*)ddI)[_0], U ((ULong*)ddI)[_1], 161 | U ((ULong*)ddI)[2+_0], U ((ULong*)ddI)[2+_1], 162 | ddI[0], ddI[1]); 163 | printf("ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n", 164 | U ((ULong*)ddI)[4+_0], U ((ULong*)ddI)[4+_1], 165 | U ((ULong*)ddI)[6+_0], U ((ULong*)ddI)[6+_1], 166 | ddI[2], ddI[3]); 167 | if (ddI[0] == u.dd[0] && ddI[1] == u.dd[1]) 168 | printf("ddI[0] == strtod\n"); 169 | else if (ddI[2] == u.dd[0] && ddI[3] == u.dd[1]) 170 | printf("ddI[1] == strtod\n"); 171 | else 172 | printf("**** Both differ from strtopdd ****\n"); 173 | } 174 | printf("\n"); 175 | } 176 | return 0; 177 | } 178 | -------------------------------------------------------------------------------- /test/dt.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | /* Test program for strtod and dtoa. 33 | * 34 | * Inputs (on stdin): 35 | * number[: mode [ndigits]] 36 | * or 37 | * #hex0 hex1[: mode [ndigits]] 38 | * where number is a decimal floating-point number, 39 | * hex0 is a string of Hex digits for the most significant 40 | * word of the number, hex1 is a similar string for the other 41 | * (least significant) word, and mode and ndigits are 42 | * parameters to dtoa. 43 | */ 44 | 45 | #include 46 | #include "gdtoa.h" 47 | #ifdef KR_headers 48 | #define Void /*void*/ 49 | #else 50 | #define Void void 51 | #endif 52 | 53 | #ifdef __STDC__ 54 | #include 55 | #else 56 | #ifdef __cplusplus 57 | extern "C" double atof(const char*); 58 | #else 59 | extern double atof ANSI((char*)); 60 | #endif 61 | #endif 62 | #ifdef IEEE_8087 63 | #define word0(x) ((ULong *)&x)[1] 64 | #define word1(x) ((ULong *)&x)[0] 65 | #else 66 | #define word0(x) ((ULong *)&x)[0] 67 | #define word1(x) ((ULong *)&x)[1] 68 | #endif 69 | #include "errno.h" 70 | 71 | #ifdef __cplusplus 72 | extern "C" char *dtoa(double, int, int, int*, int*, char **); 73 | #else 74 | extern char *dtoa ANSI((double, int, int, int*, int*, char **)); 75 | #endif 76 | 77 | static void 78 | #ifdef KR_headers 79 | g_fmt(b, x) char *b; double x; 80 | #else 81 | g_fmt(char *b, double x) 82 | #endif 83 | { 84 | char *s, *se; 85 | int decpt, i, j, k, sign; 86 | 87 | if (!x) { 88 | *b++ = '0'; 89 | *b = 0; 90 | return; 91 | } 92 | s = dtoa(x, 0, 0, &decpt, &sign, &se); 93 | if (sign) 94 | *b++ = '-'; 95 | if (decpt == 9999) /* Infinity or Nan */ { 96 | while(*b++ = *s++); 97 | return; 98 | } 99 | if (decpt <= -4 || decpt > se - s + 5) { 100 | *b++ = *s++; 101 | if (*s) { 102 | *b++ = '.'; 103 | while(*b = *s++) 104 | b++; 105 | } 106 | *b++ = 'e'; 107 | /* sprintf(b, "%+.2d", decpt - 1); */ 108 | if (--decpt < 0) { 109 | *b++ = '-'; 110 | decpt = -decpt; 111 | } 112 | else 113 | *b++ = '+'; 114 | for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}; 115 | for(;;) { 116 | i = decpt / k; 117 | *b++ = i + '0'; 118 | if (--j <= 0) 119 | break; 120 | decpt -= i*k; 121 | decpt *= 10; 122 | } 123 | *b = 0; 124 | } 125 | else if (decpt <= 0) { 126 | *b++ = '.'; 127 | for(; decpt < 0; decpt++) 128 | *b++ = '0'; 129 | while(*b++ = *s++); 130 | } 131 | else { 132 | while(*b = *s++) { 133 | b++; 134 | if (--decpt == 0 && *s) 135 | *b++ = '.'; 136 | } 137 | for(; decpt > 0; decpt--) 138 | *b++ = '0'; 139 | *b = 0; 140 | } 141 | } 142 | 143 | static void 144 | baderrno(Void) 145 | { 146 | fflush(stdout); 147 | perror("\nerrno strtod"); 148 | fflush(stderr); 149 | } 150 | 151 | #define U (unsigned long) 152 | 153 | static void 154 | #ifdef KR_headers 155 | check(d) double d; 156 | #else 157 | check(double d) 158 | #endif 159 | { 160 | char buf[64]; 161 | int decpt, sign; 162 | char *s, *se; 163 | double d1; 164 | 165 | s = dtoa(d, 0, 0, &decpt, &sign, &se); 166 | sprintf(buf, "%s%s%se%d", sign ? "-" : "", 167 | decpt == 9999 ? "" : ".", s, decpt); 168 | errno = 0; 169 | d1 = strtod(buf, (char **)0); 170 | if (errno) 171 | baderrno(); 172 | if (d != d1) { 173 | printf("sent d = %.17g = 0x%lx %lx, buf = %s\n", 174 | d, U word0(d), U word1(d), buf); 175 | printf("got d1 = %.17g = 0x%lx %lx\n", 176 | d1, U word0(d1), U word1(d1)); 177 | } 178 | } 179 | 180 | int 181 | main(Void){ 182 | char buf[2048], buf1[32]; 183 | char *fmt, *s, *s1, *se; 184 | double d, d1; 185 | int decpt, sign; 186 | int mode = 0, ndigits = 17; 187 | ULong x, y; 188 | #ifdef VAX 189 | ULong z; 190 | #endif 191 | 192 | while(fgets(buf, sizeof(buf), stdin)) { 193 | if (*buf == '*') { 194 | printf("%s", buf); 195 | continue; 196 | } 197 | printf("Input: %s", buf); 198 | if (*buf == '#') { 199 | x = word0(d); 200 | y = word1(d); 201 | /* sscanf(buf+1, "%lx %lx:%d %d", &x, &y, &mode, &ndigits); */ 202 | x = (ULong)strtoul(s1 = buf+1, &se, 16); 203 | if (se > s1) { 204 | y = (ULong)strtoul(s1 = se, &se, 16); 205 | if (se > s1) 206 | sscanf(se, ":%d %d", &mode, &ndigits); 207 | } 208 | word0(d) = x; 209 | word1(d) = y; 210 | fmt = "Output: d =\n%.17g = 0x%lx %lx\n"; 211 | } 212 | else { 213 | errno = 0; 214 | d = strtod(buf,&se); 215 | if (*se == ':') 216 | sscanf(se+1,"%d %d", &mode, &ndigits); 217 | d1 = atof(buf); 218 | fmt = "Output: d =\n%.17g = 0x%lx %lx, se = %s"; 219 | if (errno) 220 | baderrno(); 221 | } 222 | printf(fmt, d, U word0(d), U word1(d), se); 223 | g_fmt(buf1, d); 224 | printf("\tg_fmt gives \"%s\"\n", buf1); 225 | if (*buf != '#' && d != d1) 226 | printf("atof gives\n\ 227 | d1 = %.17g = 0x%lx %lx\nversus\n\ 228 | d = %.17g = 0x%lx %lx\n", d1, U word0(d1), U word1(d1), 229 | d, U word0(d), U word1(d)); 230 | check(d); 231 | s = dtoa(d, mode, ndigits, &decpt, &sign, &se); 232 | printf("\tdtoa(mode = %d, ndigits = %d):\n", mode, ndigits); 233 | printf("\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n", 234 | sign, decpt, se-s, s); 235 | x = word1(d); 236 | if (x != 0xffffffff 237 | && (word0(d) & 0x7ff00000) != 0x7ff00000) { 238 | #ifdef VAX 239 | z = x << 16 | x >> 16; 240 | z++; 241 | z = z << 16 | z >> 16; 242 | word1(d) = z; 243 | #else 244 | word1(d) = x + 1; 245 | #endif 246 | printf("\tnextafter(d,+Inf) = %.17g = 0x%lx %lx:\n", 247 | d, U word0(d), U word1(d)); 248 | g_fmt(buf1, d); 249 | printf("\tg_fmt gives \"%s\"\n", buf1); 250 | s = dtoa(d, mode, ndigits, &decpt, &sign, &se); 251 | printf( 252 | "\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n", 253 | sign, decpt, se-s, s); 254 | check(d); 255 | } 256 | if (x) { 257 | #ifdef VAX 258 | z = x << 16 | x >> 16; 259 | z--; 260 | z = z << 16 | z >> 16; 261 | word1(d) = z; 262 | #else 263 | word1(d) = x - 1; 264 | #endif 265 | printf("\tnextafter(d,-Inf) = %.17g = 0x%lx %lx:\n", 266 | d, U word0(d), U word1(d)); 267 | g_fmt(buf1, d); 268 | printf("\tg_fmt gives \"%s\"\n", buf1); 269 | s = dtoa(d, mode, ndigits, &decpt, &sign, &se); 270 | printf( 271 | "\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n", 272 | sign, decpt, se-s, s); 273 | check(d); 274 | } 275 | } 276 | return 0; 277 | } 278 | -------------------------------------------------------------------------------- /test/dtest.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998-2001 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | /* Test program for g_dfmt, strtoId, strtod, strtopd, and strtord. 33 | * 34 | * Inputs (on stdin): 35 | * r rounding_mode 36 | * n ndig 37 | * number 38 | * #hex0 hex1 39 | * 40 | * rounding_mode values: 41 | * 0 = toward zero 42 | * 1 = nearest 43 | * 2 = toward +Infinity 44 | * 3 = toward -Infinity 45 | * 46 | * where number is a decimal floating-point number, 47 | * hex0 is a string of Hex <= 8 digits for the most significant 48 | * word of the number, hex1 is a similar string for the other 49 | * (least significant) word, and ndig is a parameters to g_dfmt. 50 | */ 51 | 52 | #include "gdtoaimp.h" 53 | #include 54 | #include 55 | 56 | extern int getround ANSI((int,char*)); 57 | 58 | static char ibuf[2048], obuf[1024]; 59 | 60 | #define U (unsigned long) 61 | 62 | int 63 | main(Void) 64 | { 65 | char *s, *se, *se1; 66 | double f1, fI[2]; 67 | int i, i1, ndig = 0, r = 1; 68 | long LL[2]; 69 | union { double f; ULong L[2]; } u; 70 | 71 | while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) { 72 | while(*s <= ' ') 73 | if (!*s++) 74 | continue; 75 | switch(*s) { 76 | case 'r': 77 | r = getround(r, s); 78 | continue; 79 | case 'n': 80 | i = s[1]; 81 | if (i <= ' ' || i >= '0' && i <= '9') { 82 | ndig = atoi(s+1); 83 | continue; 84 | } 85 | break; /* nan? */ 86 | case '#': 87 | LL[0] = u.L[_0]; 88 | LL[1] = u.L[_1]; 89 | sscanf(s+1, "%lx %lx", &LL[0], &LL[1]); 90 | u.L[_0] = LL[0]; 91 | u.L[_1] = LL[1]; 92 | printf("\nInput: %s", ibuf); 93 | printf("--> f = #%lx %lx\n", (long)u.L[_0], (long)u.L[_1]); 94 | goto fmt_test; 95 | } 96 | printf("\nInput: %s", ibuf); 97 | i = strtord(ibuf, &se, r, &u.f); 98 | if (r == 1) { 99 | if ((u.f != strtod(ibuf, &se1) || se1 != se)) 100 | printf("***strtod and strtord disagree!!\n"); 101 | i1 = strtopd(ibuf, &se, &f1); 102 | if (i != i1 || u.f != f1 || se != se1) 103 | printf("***strtord and strtopd disagree!!\n"); 104 | } 105 | printf("strtod consumes %d bytes and returns %d with f = %.17g = #%lx %lx\n", 106 | (int)(se-ibuf), i, u.f, U u.L[_0], U u.L[_1]); 107 | fmt_test: 108 | se = g_dfmt(obuf, &u.f, ndig, sizeof(obuf)); 109 | printf("g_dfmt(%d) gives %d bytes: \"%s\"\n\n", 110 | ndig, (int)(se-obuf), se ? obuf : ""); 111 | if (*s == '#') 112 | continue; 113 | printf("strtoId returns %d,", strtoId(ibuf, &se, fI, &fI[1])); 114 | printf(" consuming %d bytes.\n", (int)(se-ibuf)); 115 | if (fI[0] == fI[1]) { 116 | if (fI[0] == u.f) 117 | printf("fI[0] == fI[1] == strtod\n"); 118 | else 119 | printf("fI[0] == fI[1] = #%lx %lx = %.17g\n", 120 | U ((ULong*)fI)[_0], U ((ULong*)fI)[_1], 121 | fI[0]); 122 | } 123 | else { 124 | printf("fI[0] = #%lx %lx = %.17g\n", 125 | U ((ULong*)fI)[_0], U ((ULong*)fI)[_1], fI[0]); 126 | printf("fI[1] = #%lx %lx = %.17g\n", 127 | U ((ULong*)&fI[1])[_0], U ((ULong*)&fI[1])[_1], 128 | fI[1]); 129 | if (fI[0] == u.f) 130 | printf("fI[0] == strtod\n"); 131 | else if (fI[1] == u.f) 132 | printf("fI[1] == strtod\n"); 133 | else 134 | printf("**** Both differ from strtod ****\n"); 135 | } 136 | printf("\n"); 137 | } 138 | return 0; 139 | } 140 | -------------------------------------------------------------------------------- /test/ftest.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998-2001 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | /* Test program for g_ffmt, strtof, strtoIf, strtopf, and strtorf. 33 | * 34 | * Inputs (on stdin): 35 | * r rounding_mode 36 | * n ndig 37 | * number 38 | * #hex 39 | * 40 | * rounding_mode values: 41 | * 0 = toward zero 42 | * 1 = nearest 43 | * 2 = toward +Infinity 44 | * 3 = toward -Infinity 45 | * 46 | * where number is a decimal floating-point number, 47 | * hex is a string of <= 8 Hex digits for the internal representation 48 | * of the number, and ndig is a parameters to g_ffmt. 49 | */ 50 | 51 | #include "gdtoa.h" 52 | #include 53 | #include 54 | 55 | extern int getround ANSI((int,char*)); 56 | 57 | static char ibuf[2048], obuf[1024]; 58 | 59 | #define U (unsigned long) 60 | 61 | int 62 | main(Void) 63 | { 64 | char *s, *se, *se1; 65 | int dItry, i, i1, ndig = 0, r = 1; 66 | float f1, fI[2]; 67 | union { float f; ULong L[1]; } u; 68 | 69 | while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) { 70 | while(*s <= ' ') 71 | if (!*s++) 72 | continue; 73 | dItry = 0; 74 | switch(*s) { 75 | case 'r': 76 | r = getround(r, s); 77 | continue; 78 | case 'n': 79 | i = s[1]; 80 | if (i <= ' ' || i >= '0' && i <= '9') { 81 | ndig = atoi(s+1); 82 | continue; 83 | } 84 | break; /* nan? */ 85 | case '#': 86 | /* sscanf(s+1, "%lx", &u.L[0]); */ 87 | u.L[0] = (ULong)strtoul(s+1, &se, 16); 88 | printf("\nInput: %s", ibuf); 89 | printf(" --> f = #%lx\n", U u.L[0]); 90 | goto fmt_test; 91 | } 92 | dItry = 1; 93 | printf("\nInput: %s", ibuf); 94 | i = strtorf(ibuf, &se, r, &u.f); 95 | if (r == 1) { 96 | if (u.f != (i1 = strtopf(ibuf, &se1, &f1), f1) 97 | || se != se1 || i != i1) { 98 | printf("***strtopf and strtorf disagree!!\n"); 99 | if (u.f != f1) 100 | printf("\tf1 = %g\n", (double)f1); 101 | if (i != i1) 102 | printf("\ti = %d but i1 = %d\n", i, i1); 103 | if (se != se1) 104 | printf("se - se1 = %d\n", (int)(se-se1)); 105 | } 106 | if (u.f != strtof(ibuf, &se1) || se != se1) 107 | printf("***strtof and strtorf disagree!\n"); 108 | } 109 | printf("strtof consumes %d bytes and returns %.8g = #%lx\n", 110 | (int)(se-ibuf), u.f, U u.L[0]); 111 | fmt_test: 112 | se = g_ffmt(obuf, &u.f, ndig, sizeof(obuf)); 113 | printf("g_ffmt(%d) gives %d bytes: \"%s\"\n\n", 114 | ndig, (int)(se-obuf), se ? obuf : ""); 115 | if (!dItry) 116 | continue; 117 | printf("strtoIf returns %d,", strtoIf(ibuf, &se, fI, &fI[1])); 118 | printf(" consuming %d bytes.\n", (int)(se-ibuf)); 119 | if (fI[0] == fI[1]) { 120 | if (fI[0] == u.f) 121 | printf("fI[0] == fI[1] == strtof\n"); 122 | else 123 | printf("fI[0] == fI[1] = #%lx = %.8g\n", 124 | U *(ULong*)fI, fI[0]); 125 | } 126 | else { 127 | printf("fI[0] = #%lx = %.8g\nfI[1] = #%lx = %.8g\n", 128 | U *(ULong*)fI, fI[0], 129 | U *(ULong*)&fI[1], fI[1]); 130 | if (fI[0] == u.f) 131 | printf("fI[0] == strtof\n"); 132 | else if (fI[1] == u.f) 133 | printf("fI[1] == strtof\n"); 134 | else 135 | printf("**** Both differ from strtof ****\n"); 136 | } 137 | printf("\n"); 138 | } 139 | return 0; 140 | } 141 | -------------------------------------------------------------------------------- /test/getround.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include 33 | #include 34 | 35 | static char *dir[4] = { "toward zero", "nearest", "toward +Infinity", 36 | "toward -Infinity" }; 37 | 38 | int 39 | #ifdef KR_headers 40 | getround(r, s) int r; char *s; 41 | #else 42 | getround(int r, char *s) 43 | #endif 44 | { 45 | int i; 46 | 47 | i = atoi(s+1); 48 | if (i >= 0 && i < 4) { 49 | printf("Rounding mode for strtor... "); 50 | if (i == r) 51 | printf("was and is %d (%s)\n", i, dir[i]); 52 | else 53 | printf("changed from %d (%s) to %d (%s)\n", 54 | r, dir[r], i, dir[i]); 55 | return i; 56 | } 57 | printf("Bad rounding direction %d: choose among\n", i); 58 | for(i = 0; i < 4; i++) 59 | printf("\t%d (%s)\n", i, dir[i]); 60 | printf("Leaving rounding mode for strtor... at %d (%s)\n", r, dir[r]); 61 | return r; 62 | } 63 | -------------------------------------------------------------------------------- /test/makefile: -------------------------------------------------------------------------------- 1 | # /**************************************************************** 2 | # Copyright (C) 1998, 2000 by Lucent Technologies 3 | # All Rights Reserved 4 | # 5 | # Permission to use, copy, modify, and distribute this software and 6 | # its documentation for any purpose and without fee is hereby 7 | # granted, provided that the above copyright notice appear in all 8 | # copies and that both that the copyright notice and this 9 | # permission notice and warranty disclaimer appear in supporting 10 | # documentation, and that the name of Lucent or any of its entities 11 | # not be used in advertising or publicity pertaining to 12 | # distribution of the software without specific, written prior 13 | # permission. 14 | # 15 | # LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 | # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 17 | # IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 18 | # SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 20 | # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 21 | # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 22 | # THIS SOFTWARE. 23 | # 24 | # ****************************************************************/ 25 | 26 | .SUFFIXES: .c .o 27 | CC = cc 28 | CFLAGS = -g -I.. 29 | L = -lm -L../.libs -lgdtoa 30 | INFFIX = | sed 's/[Ii][Nn][Ff][intyINTY]*/Infinity/g' 31 | 32 | .c.o: 33 | $(CC) -c $(CFLAGS) $*.c 34 | 35 | all: dt dItest ddtest dtest ftest Qtest xLtest xtest ddtestsi dItestsi tests 36 | 37 | dt = dt.o 38 | dt: $(dt) 39 | $(CC) -o dt $(dt) $L 40 | 41 | dItest = dItest.o getround.o 42 | dItest: $(dItest) 43 | $(CC) -o dItest $(dItest) $L 44 | 45 | ddtest = ddtest.o getround.o 46 | ddtest: $(ddtest) 47 | $(CC) -o ddtest $(ddtest) $L 48 | 49 | dtest = dtest.o getround.o 50 | dtest: $(dtest) 51 | $(CC) -o dtest $(dtest) $L 52 | 53 | ftest = ftest.o getround.o 54 | ftest: $(ftest) 55 | $(CC) -o ftest $(ftest) $L 56 | 57 | Qtest = Qtest.o getround.o 58 | Qtest: $(Qtest) 59 | $(CC) -o Qtest $(Qtest) $L 60 | 61 | xtest = xtest.o getround.o 62 | xtest: $(xtest) 63 | $(CC) -o xtest $(xtest) $L 64 | 65 | xLtest = xLtest.o getround.o 66 | xLtest: $(xLtest) 67 | $(CC) -o xLtest $(xLtest) $L 68 | 69 | strtopddSI.o: strtopddSI.c ../strtopdd.c 70 | 71 | strtorddSI.o: strtorddSI.c ../strtordd.c 72 | 73 | strtodISI.o: strtodISI.c ../strtodI.c 74 | 75 | strtoIddSI.o: strtoIddSI.c ../strtoIdd.c 76 | 77 | strtoIdSI.o: strtoIdSI.c ../strtoId.c 78 | 79 | ddtestsi = ddtest.o strtopddSI.o strtorddSI.o strtoIddSI.o getround.o 80 | ddtestsi: $(ddtestsi) 81 | $(CC) -o ddtestsi $(ddtestsi) $L 82 | 83 | dItestsi = dItest.o strtodISI.o strtoIdSI.o getround.o 84 | dItestsi: $(dItestsi) 85 | $(CC) -o dItestsi $(dItestsi) $L 86 | 87 | strtodt = strtodt.o 88 | strtodt: $(strtodt) 89 | $(CC) -o strtodt $(strtodt) $L 90 | 91 | ## On Intel (and Intel-like) systems using extended-precision registers 92 | ## for double-precision (C type double) computations that sometimes suffer 93 | ## double rounding errors, the test below involving strtodt generally shows 94 | ## five lines of unexpected results. Variant strtodtnrp uses ../strtodrnp.c 95 | ## (which does all computations in integer arithmetic) and should show no 96 | ## unexpected results. 97 | 98 | strtodtnrp = strtodt.o ../strtodnrp.c 99 | strtodtnrp: $(strtodtnrp) 100 | $(CC) -o strtodtnrp $(strtodtnrp) $L 101 | 102 | # xQtest generates cp commands that depend on sizeof(long double). 103 | # See the source for details. If you know better, create Q.out, 104 | # x.out and xL.out by copying the relevant *.ou0 or *.ou1 files 105 | # to the corresponding .out files. In short, the *.ou0 files are 106 | # for long double == double; x.ou1 and xL.ou1 are for 107 | # long double == extended (a la 80x87 and MC680x0), and Q.ou1 is 108 | # for long double == quad. 109 | 110 | Q.out x.out xL.out: 111 | $(CC) -o xQtest xQtest.c 112 | ./xQtest | sh 113 | rm -f xQtest xQtest.o 114 | 115 | ## The rmdir below will fail if any test results differ. 116 | 117 | tests: Q.out x.out xL.out dt dItest ddtest dtest ftest Qtest xLtest xtest ddtestsi dItestsi strtodt strtodtnrp 118 | mkdir -p bad 119 | cat testnos testnos1 | ./dt $(INFFIX) >zap 2>&1 120 | cmp dtst.out zap || mv zap bad/dtst.out 121 | ./dItest zap 2>&1 122 | cmp dI.out zap || mv zap bad/dI.out 123 | ./dItestsi zap 2>&1 124 | cmp dIsi.out zap || mv zap bad/dIsi.out 125 | ./ddtestsi zap 2>&1 126 | cmp ddsi.out zap || mv zap bad/ddsi.out 127 | for i in dd d f x xL Q; do cat testnos rtestnos | \ 128 | ./"$$i"test $(INFFIX) >zap 2>&1;\ 129 | cmp $$i.out zap || mv zap bad/$$i.out; done 130 | ./strtodt testnos3 >bad/strtodt.out && rm bad/strtodt.out || \ 131 | cat bad/strtodt.out 132 | ./strtodtnrp testnos3 >bad/strtodtnrp.out && rm bad/strtodtnrp.out || \ 133 | cat bad/strtodtnrp.out 134 | rm -fr bad 135 | touch tests 136 | 137 | xs0 = README Qtest.c dItest.c ddtest.c dtest.c dt.c ftest.c getround.c \ 138 | strtoIdSI.c strtoIddSI.c strtodISI.c strtodt.c strtopddSI.c \ 139 | strtorddSI.c xLtest.c xQtest.c xtest.c rtestnos testnos testnos1 \ 140 | testnos3 dI.out dIsi.out ddsi.out dd.out dtst.out d.out f.out \ 141 | x.ou0 xL.ou0 x.ou1 xL.ou1 Q.ou0 Q.ou1 makefile 142 | 143 | xsum.out: xsum0.out $(xs0) 144 | xsum $(xs0) >xsum1.out 145 | cmp xsum0.out xsum1.out && mv xsum1.out xsum.out || diff xsum[01].out 146 | 147 | clean: 148 | rm -f *.[ao] dt *test *testsi strtodt strtodtnrp xsum.out xsum1.out tests zap x.out xL.out Q.out 149 | rm -rf bad 150 | -------------------------------------------------------------------------------- /test/rtestnos: -------------------------------------------------------------------------------- 1 | r0 2 | 1.1 3 | -1.1 4 | 1.2 5 | -1.2 6 | 1.3 7 | -1.3 8 | 1.4 9 | -1.4 10 | 1.5 11 | -1.5 12 | 1.6 13 | -1.6 14 | 1.7 15 | -1.7 16 | 1.8 17 | -1.8 18 | 1.9 19 | -1.9 20 | r1 21 | 1.1 22 | -1.1 23 | 1.2 24 | -1.2 25 | 1.3 26 | -1.3 27 | 1.4 28 | -1.4 29 | 1.5 30 | -1.5 31 | 1.6 32 | -1.6 33 | 1.7 34 | -1.7 35 | 1.8 36 | -1.8 37 | 1.9 38 | -1.9 39 | r2 40 | 1.1 41 | -1.1 42 | 1.2 43 | -1.2 44 | 1.3 45 | -1.3 46 | 1.4 47 | -1.4 48 | 1.5 49 | -1.5 50 | 1.6 51 | -1.6 52 | 1.7 53 | -1.7 54 | 1.8 55 | -1.8 56 | 1.9 57 | -1.9 58 | r3 59 | 1.1 60 | -1.1 61 | 1.2 62 | -1.2 63 | 1.3 64 | -1.3 65 | 1.4 66 | -1.4 67 | 1.5 68 | -1.5 69 | 1.6 70 | -1.6 71 | 1.7 72 | -1.7 73 | 1.8 74 | -1.8 75 | 1.9 76 | -1.9 77 | -------------------------------------------------------------------------------- /test/strtoIdSI.c: -------------------------------------------------------------------------------- 1 | #define Sudden_Underflow 2 | #include "../strtoId.c" 3 | -------------------------------------------------------------------------------- /test/strtoIddSI.c: -------------------------------------------------------------------------------- 1 | #define Sudden_Underflow 2 | #include "../strtoIdd.c" 3 | -------------------------------------------------------------------------------- /test/strtodISI.c: -------------------------------------------------------------------------------- 1 | #define Sudden_Underflow 2 | #include "../strtodI.c" 3 | -------------------------------------------------------------------------------- /test/strtodt.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 2001 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | /* Test strtod. */ 33 | 34 | /* On stdin, read triples: d x y: 35 | * d = decimal string 36 | * x = high-order Hex value expected from strtod 37 | * y = low-order Hex value 38 | * Complain about errors. 39 | */ 40 | 41 | #include "gdtoa.h" /* for ULong */ 42 | #include 43 | #include 44 | #include 45 | 46 | static int W0, W1; 47 | typedef union { 48 | double d; 49 | ULong L[2]; 50 | } U; 51 | 52 | static int 53 | process(char *fname, FILE *f) 54 | { 55 | U a, b; 56 | char buf[2048]; 57 | char *s, *s1, *se; 58 | int line, n; 59 | 60 | line = n = 0; 61 | 62 | top: 63 | while(fgets(s = buf, sizeof(buf), f)) { 64 | line++; 65 | while(*s <= ' ') 66 | if (!*s++) 67 | goto top; /* break 2 */ 68 | if (*s == '#') 69 | continue; 70 | while(*s > ' ') 71 | s++; 72 | /* if (sscanf(s,"\t%lx\t%lx", &a.L[0], &a.L[1]) != 2) */ 73 | if ((a.L[0] = (ULong)strtoul(s, &s1,16), s1 <= s) 74 | || (a.L[1] = (ULong)strtoul(s1,&se,16), se <= s1)) { 75 | printf("Badly formatted line %d of %s\n", 76 | line, fname); 77 | n++; 78 | continue; 79 | } 80 | b.d = strtod(buf,0); 81 | if (b.L[W0] != a.L[0] || b.L[W1] != a.L[1]) { 82 | n++; 83 | printf("Line %d of %s: got %lx %lx; expected %lx %lx\n", 84 | line, fname, b.L[W0], b.L[W1], a.L[0], a.L[1]); 85 | } 86 | } 87 | return n; 88 | } 89 | 90 | int 91 | main(int argc, char **argv) 92 | { 93 | FILE *f; 94 | char *prog, *s; 95 | int n, rc; 96 | U u; 97 | 98 | prog = argv[0]; 99 | if (argc == 2 && !strcmp(argv[1],"-?")) { 100 | fprintf(stderr, "Usage: %s [file [file...]]\n" 101 | "\tto read data file(s) of tab-separated triples d x y with\n" 102 | "\t\td decimal string\n" 103 | "\t\tx = high-order Hex value expected from strtod\n" 104 | "\t\ty = low-order Hex value\n" 105 | "\tComplain about errors by strtod.\n" 106 | "\tIf no files, read triples from stdin.\n", 107 | prog); 108 | return 0; 109 | } 110 | 111 | /* determine endian-ness */ 112 | 113 | u.d = 1.; 114 | W0 = u.L[0] == 0; 115 | W1 = 1 - W0; 116 | 117 | /* test */ 118 | 119 | n = rc = 0; 120 | if (argc <= 1) 121 | n = process("", stdin); 122 | else 123 | while(s = *++argv) 124 | if (f = fopen(s,"r")) { 125 | n += process(s, f); 126 | fclose(f); 127 | } 128 | else { 129 | rc = 2; 130 | fprintf(stderr, "Cannot open %s\n", s); 131 | } 132 | printf("%d bad conversions\n", n); 133 | if (n) 134 | rc |= 1; 135 | return rc; 136 | } 137 | -------------------------------------------------------------------------------- /test/strtopddSI.c: -------------------------------------------------------------------------------- 1 | #define Sudden_Underflow 2 | #include "../strtopdd.c" 3 | -------------------------------------------------------------------------------- /test/strtorddSI.c: -------------------------------------------------------------------------------- 1 | #define Sudden_Underflow 2 | #include "../strtordd.c" 3 | -------------------------------------------------------------------------------- /test/testnos: -------------------------------------------------------------------------------- 1 | 1.23 2 | 1.23e+20 3 | 1.23e-20 4 | 1.23456789 5 | 1.23456589e+20 6 | 1.23e+30 7 | 1.23e-30 8 | 1.23456789e-20 9 | 1.23456789e-30 10 | 1.234567890123456789 11 | 1.23456789012345678901234567890123456789 12 | 1.23e306 13 | 1.23e-306 14 | 1.23e-320 15 | 1.23e-20 16 | 1.23456789e307 17 | 1.23456589e-307 18 | 1.234567890123456789 19 | 1.234567890123456789e301 20 | 1.234567890123456789e-301 21 | 1.234567890123456789e-321 22 | 1e23 23 | 1e310 24 | 9.0259718793241475e-277 25 | 9.025971879324147880346310405869e-277 26 | 9.025971879324147880346310405868e-277 27 | 2.2250738585072014e-308 28 | 2.2250738585072013e-308 29 | -------------------------------------------------------------------------------- /test/testnos1: -------------------------------------------------------------------------------- 1 | 1.23:2 6 2 | 1.23:4 6 3 | 1.23e+20:2 6 4 | 1.23e+20:4 6 5 | 1.23e-20:2 6 6 | 1.23e-20:4 6 7 | 1.23456789:2 6 8 | 1.23456789:4 6 9 | 1.23456589e+20:2 6 10 | 1.23456589e+20:4 6 11 | 1.23456789e-20:2 6 12 | 1.23456789e-20:4 6 13 | 1234565:2 6 14 | 1234565:4 6 15 | 1.234565:2 6 16 | 1.234565:4 6 17 | 1.234565e+20:2 6 18 | 1.234565e+20:4 6 19 | 1.234565e-20:2 6 20 | 1.234565e-20:4 6 21 | -------------------------------------------------------------------------------- /test/xLtest.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998-2001 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | /* Test program for g_xLfmt, strtoIxL, strtopxL, and strtorxL. 33 | * 34 | * Inputs (on stdin): 35 | * r rounding_mode 36 | * n ndig 37 | * number 38 | * #hex0 hex1 hex2 39 | * 40 | * rounding_mode values: 41 | * 0 = toward zero 42 | * 1 = nearest 43 | * 2 = toward +Infinity 44 | * 3 = toward -Infinity 45 | * 46 | * where number is a decimal floating-point number, 47 | * hex0 is a string of <= 8 Hex digits for the most significant 48 | * word of the number, hex1 is a similar string for the next 49 | * word, etc., and ndig is a parameters to g_xLfmt. 50 | */ 51 | 52 | #include "gdtoa.h" 53 | #include 54 | #include 55 | #include 56 | 57 | extern int getround ANSI((int,char*)); 58 | 59 | static char ibuf[2048], obuf[2048]; 60 | 61 | #define U (unsigned long) 62 | 63 | #undef _0 64 | #undef _1 65 | 66 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 67 | 68 | #ifdef IEEE_MC68k 69 | #define _0 0 70 | #define _1 1 71 | #define _2 2 72 | #endif 73 | #ifdef IEEE_8087 74 | #define _0 2 75 | #define _1 1 76 | #define _2 0 77 | #endif 78 | 79 | int 80 | main(Void) 81 | { 82 | char *s, *s1, *se, *se1; 83 | int dItry, i, ndig = 0, r = 1; 84 | union { long double d; ULong bits[3]; } u, v[2]; 85 | 86 | while(s = fgets(ibuf, sizeof(ibuf), stdin)) { 87 | while(*s <= ' ') 88 | if (!*s++) 89 | continue; 90 | dItry = 0; 91 | switch(*s) { 92 | case 'r': 93 | r = getround(r, s); 94 | continue; 95 | case 'n': 96 | i = s[1]; 97 | if (i <= ' ' || i >= '0' && i <= '9') { 98 | ndig = atoi(s+1); 99 | continue; 100 | } 101 | break; /* nan? */ 102 | case '#': 103 | /* sscanf(s+1, "%lx %lx %lx", &u.bits[_0], */ 104 | /* &u.bits[_1], &u.bits[_2]); */ 105 | u.bits[_0] = (ULong)strtoul(s1 = s+1, &se, 16); 106 | if (se > s1) { 107 | u.bits[_1] = (ULong)strtoul(s1=se, &se, 16); 108 | if (se > s1) 109 | u.bits[_2] = (ULong)strtoul(s1=se, &se, 16); 110 | } 111 | printf("\nInput: %s", ibuf); 112 | printf(" --> f = #%lx %lx %lx\n", u.bits[_0], 113 | u.bits[_1], u.bits[_2]); 114 | goto fmt_test; 115 | } 116 | dItry = 1; 117 | printf("\nInput: %s", ibuf); 118 | i = strtorxL(ibuf, &se, r, u.bits); 119 | if (r == 1 && (i != strtopxL(ibuf, &se1, v[0].bits) || se1 != se 120 | || memcmp(u.bits, v[0].bits, 12))) 121 | printf("***strtoxL and strtorxL disagree!!\n:"); 122 | printf("\nstrtoxL consumes %d bytes and returns %d\n", 123 | (int)(se-ibuf), i); 124 | printf("with bits = #%lx %lx %lx\n", 125 | U u.bits[_0], U u.bits[_1], U u.bits[_2]); 126 | if (sizeof(long double) == 12) 127 | printf("printf(\"%%.21Lg\") gives %.21Lg\n", u.d); 128 | fmt_test: 129 | se = g_xLfmt(obuf, u.bits, ndig, sizeof(obuf)); 130 | printf("g_xLfmt(%d) gives %d bytes: \"%s\"\n\n", 131 | ndig, (int)(se-obuf), se ? obuf : ""); 132 | if (!dItry) 133 | continue; 134 | printf("strtoIxL returns %d,", 135 | strtoIxL(ibuf, &se, v[0].bits, v[1].bits)); 136 | printf(" consuming %d bytes.\n", (int)(se-ibuf)); 137 | if (!memcmp(v[0].bits, v[1].bits, 12)) { 138 | if (!memcmp(u.bits, v[0].bits, 12)) 139 | printf("fI[0] == fI[1] == strtoxL\n"); 140 | else { 141 | printf("fI[0] == fI[1] = #%lx %lx %lx\n", 142 | U v[0].bits[_0], U v[0].bits[_1], 143 | U v[0].bits[_2]); 144 | if (sizeof(long double) == 12) 145 | printf("= %.21Lg\n", v[0].d); 146 | } 147 | } 148 | else { 149 | printf("fI[0] = #%lx %lx %lx\n", 150 | U v[0].bits[_0], U v[0].bits[_1], 151 | U v[0].bits[_2]); 152 | if (sizeof(long double) == 12) 153 | printf("= %.21Lg\n", v[0].d); 154 | printf("fI[1] = #%lx %lx %lx\n", 155 | U v[1].bits[_0], U v[1].bits[_1], 156 | U v[1].bits[_2]); 157 | if (sizeof(long double) == 12) 158 | printf("= %.21Lg\n", v[1].d); 159 | if (!memcmp(v[0].bits, u.bits, 12)) 160 | printf("fI[0] == strtoxL\n"); 161 | else if (!memcmp(v[1].bits, u.bits, 12)) 162 | printf("fI[1] == strtoxL\n"); 163 | else 164 | printf("**** Both differ from strtod ****\n"); 165 | } 166 | printf("\n"); 167 | } 168 | return 0; 169 | } 170 | -------------------------------------------------------------------------------- /test/xQtest.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 2002 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | #include 30 | 31 | int 32 | main(void) 33 | { 34 | switch(sizeof(long double)) { 35 | case 10: 36 | case 12: 37 | printf("cp x.ou1 x.out; cp xL.ou1 xL.out; cp Q.ou0 Q.out\n"); 38 | break; 39 | case 16: 40 | printf("cp x.ou0 x.out; cp xL.ou0 xL.out; cp Q.ou1 Q.out\n"); 41 | break; 42 | default: 43 | printf("cp x.ou0 x.out; cp xL.ou0 xL.out; cp Q.ou0 Q.out\n"); 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /test/xsum0.out: -------------------------------------------------------------------------------- 1 | README e6ebdc91 2429 2 | Qtest.c e8353ffc 5046 3 | dItest.c e33800ce 2371 4 | ddtest.c f9d06e7b 4984 5 | dtest.c ee533ac3 4078 6 | dt.c 7eeda57 6384 7 | ftest.c ec8a6654 3999 8 | getround.c f810968 2011 9 | strtoIdSI.c 7bfb88b 49 10 | strtoIddSI.c 72e8852 50 11 | strtodISI.c ed08b740 49 12 | strtodt.c aaf94bc 3330 13 | strtopddSI.c 13e7138d 50 14 | strtorddSI.c f7e4b1d5 50 15 | xLtest.c f3f96ad1 4833 16 | xQtest.c efdea3a2 1549 17 | xtest.c ee81e661 4830 18 | rtestnos f94bcdf6 336 19 | testnos e89999d6 485 20 | testnos1 7e16229 294 21 | testnos3 fa5c8aca 11998 22 | dI.out d522eef 4369 23 | dIsi.out 1dd6d02f 4350 24 | ddsi.out 1f94bbe2 10251 25 | dd.out e262456e 40923 26 | dtst.out e284ac98 23711 27 | d.out f271efc9 28131 28 | f.out 4b0bd51 21207 29 | x.ou0 1402f834 25372 30 | xL.ou0 faa3a741 26363 31 | x.ou1 f1af5a00 34581 32 | xL.ou1 e349e5c 37165 33 | Q.ou0 e4592b85 28742 34 | Q.ou1 ea0b344d 39572 35 | makefile b77232c 4939 36 | -------------------------------------------------------------------------------- /test/xtest.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998-2001 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | /* Test program for g_xfmt, strtoIx, strtopx, and strtorx. 33 | * 34 | * Inputs (on stdin): 35 | * r rounding_mode 36 | * n ndig 37 | * number 38 | * #hex0 hex1 hex2 hex3 hex4 39 | * 40 | * rounding_mode values: 41 | * 0 = toward zero 42 | * 1 = nearest 43 | * 2 = toward +Infinity 44 | * 3 = toward -Infinity 45 | * 46 | * where number is a decimal floating-point number, 47 | * hex0 is a string of <= 4 Hex digits for the most significant 48 | * half-word of the number, hex1 is a similar string for the next 49 | * half-word, etc., and ndig is a parameters to g_xfmt. 50 | */ 51 | 52 | #include "gdtoa.h" 53 | #include 54 | #include 55 | #include 56 | 57 | extern int getround ANSI((int,char*)); 58 | 59 | static char ibuf[2048], obuf[2048]; 60 | 61 | #undef _0 62 | #undef _1 63 | 64 | /* one or the other of IEEE_MC68k or IEEE_8087 should be #defined */ 65 | 66 | #ifdef IEEE_MC68k 67 | #define _0 0 68 | #define _1 1 69 | #define _2 2 70 | #define _3 3 71 | #define _4 4 72 | #endif 73 | #ifdef IEEE_8087 74 | #define _0 4 75 | #define _1 3 76 | #define _2 2 77 | #define _3 1 78 | #define _4 0 79 | #endif 80 | 81 | int 82 | main(Void) 83 | { 84 | char *s, *se, *se1; 85 | int i, dItry, ndig = 0, r = 1; 86 | union { long double d; UShort bits[5]; } u, v[2]; 87 | 88 | while(s = fgets(ibuf, sizeof(ibuf), stdin)) { 89 | while(*s <= ' ') 90 | if (!*s++) 91 | continue; 92 | dItry = 0; 93 | switch(*s) { 94 | case 'r': 95 | r = getround(r, s); 96 | continue; 97 | case 'n': 98 | i = s[1]; 99 | if (i <= ' ' || i >= '0' && i <= '9') { 100 | ndig = atoi(s+1); 101 | continue; 102 | } 103 | break; /* nan? */ 104 | case '#': 105 | sscanf(s+1, "%hx %hx %hx %hx %hx", &u.bits[_0], 106 | &u.bits[_1], &u.bits[_2], &u.bits[_3], 107 | &u.bits[_4]); 108 | printf("\nInput: %s", ibuf); 109 | printf(" --> f = #%x %x %x %x %x\n", u.bits[_0], 110 | u.bits[_1], u.bits[_2], u.bits[_3], u.bits[_4]); 111 | goto fmt_test; 112 | } 113 | dItry = 1; 114 | printf("\nInput: %s", ibuf); 115 | i = strtorx(ibuf, &se, r, u.bits); 116 | if (r == 1 && (i != strtopx(ibuf, &se1, v[0].bits) || se1 != se 117 | || memcmp(u.bits, v[0].bits, 10))) 118 | printf("***strtox and strtorx disagree!!\n:"); 119 | printf("\nstrtox consumes %d bytes and returns %d\n", 120 | (int)(se-ibuf), i); 121 | printf("with bits = #%x %x %x %x %x\n", 122 | u.bits[_0], u.bits[_1], u.bits[_2], 123 | u.bits[_3], u.bits[_4]); 124 | if (sizeof(long double) == 12) 125 | printf("printf(\"%%.21Lg\") gives %.21Lg\n", u.d); 126 | fmt_test: 127 | se = g_xfmt(obuf, u.bits, ndig, sizeof(obuf)); 128 | printf("g_xfmt(%d) gives %d bytes: \"%s\"\n\n", 129 | ndig, (int)(se-obuf), se ? obuf : ""); 130 | if (!dItry) 131 | continue; 132 | printf("strtoIx returns %d,", 133 | strtoIx(ibuf, &se, v[0].bits, v[1].bits)); 134 | printf(" consuming %d bytes.\n", (int)(se-ibuf)); 135 | if (!memcmp(v[0].bits, v[1].bits, 10)) { 136 | if (!memcmp(u.bits, v[0].bits, 10)) 137 | printf("fI[0] == fI[1] == strtox\n"); 138 | else { 139 | printf("fI[0] == fI[1] = #%x %x %x %x %x\n", 140 | v[0].bits[_0], v[0].bits[_1], 141 | v[0].bits[_2], v[0].bits[_3], 142 | v[0].bits[_4]); 143 | if (sizeof(long double) == 12) 144 | printf("= %.21Lg\n", v[0].d); 145 | } 146 | } 147 | else { 148 | printf("fI[0] = #%x %x %x %x %x\n", 149 | v[0].bits[_0], v[0].bits[_1], 150 | v[0].bits[_2], v[0].bits[_3], 151 | v[0].bits[_4]); 152 | if (sizeof(long double) == 12) 153 | printf("= %.21Lg\n", v[0].d); 154 | printf("fI[1] = #%x %x %x %x %x\n", 155 | v[1].bits[_0], v[1].bits[_1], 156 | v[1].bits[_2], v[1].bits[_3], 157 | v[1].bits[_4]); 158 | if (sizeof(long double) == 12) 159 | printf("= %.21Lg\n", v[1].d); 160 | if (!memcmp(v[0].bits, u.bits, 10)) 161 | printf("fI[0] == strtox\n"); 162 | else if (!memcmp(v[1].bits, u.bits, 10)) 163 | printf("fI[1] == strtox\n"); 164 | else 165 | printf("**** Both differ from strtod ****\n"); 166 | } 167 | printf("\n"); 168 | } 169 | return 0; 170 | } 171 | -------------------------------------------------------------------------------- /ulp.c: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | The author of this software is David M. Gay. 4 | 5 | Copyright (C) 1998, 1999 by Lucent Technologies 6 | All Rights Reserved 7 | 8 | Permission to use, copy, modify, and distribute this software and 9 | its documentation for any purpose and without fee is hereby 10 | granted, provided that the above copyright notice appear in all 11 | copies and that both that the copyright notice and this 12 | permission notice and warranty disclaimer appear in supporting 13 | documentation, and that the name of Lucent or any of its entities 14 | not be used in advertising or publicity pertaining to 15 | distribution of the software without specific, written prior 16 | permission. 17 | 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 | THIS SOFTWARE. 26 | 27 | ****************************************************************/ 28 | 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 | * with " at " changed at "@" and " dot " changed to "."). */ 31 | 32 | #include "gdtoaimp.h" 33 | 34 | double 35 | ulp 36 | #ifdef KR_headers 37 | (x) double x; 38 | #else 39 | (double x) 40 | #endif 41 | { 42 | Long L; 43 | double a; 44 | 45 | L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; 46 | #ifndef Sudden_Underflow 47 | if (L > 0) { 48 | #endif 49 | #ifdef IBM 50 | L |= Exp_msk1 >> 4; 51 | #endif 52 | word0(a) = L; 53 | word1(a) = 0; 54 | #ifndef Sudden_Underflow 55 | } 56 | else { 57 | L = -L >> Exp_shift; 58 | if (L < Exp_shift) { 59 | word0(a) = 0x80000 >> L; 60 | word1(a) = 0; 61 | } 62 | else { 63 | word0(a) = 0; 64 | L -= Exp_shift; 65 | word1(a) = L >= 31 ? 1 : 1 << 31 - L; 66 | } 67 | } 68 | #endif 69 | return a; 70 | } 71 | -------------------------------------------------------------------------------- /xsum0.out: -------------------------------------------------------------------------------- 1 | README f2477cff 14190 2 | arithchk.c ebbe5bc7 4075 3 | dmisc.c c8daa18 4682 4 | dtoa.c 6a7b6fe 16876 5 | g_Qfmt.c f791d807 2839 6 | g__fmt.c 14dca85 2504 7 | g_ddfmt.c 10eae12a 3695 8 | g_dfmt.c f36c1014 2503 9 | g_ffmt.c fb83cfb5 2429 10 | g_xLfmt.c f216a096 2686 11 | g_xfmt.c ed824bf3 2775 12 | gdtoa.c e29409a6 16988 13 | gdtoa.h f208c204 4780 14 | gdtoaimp.h e3c2a970 19441 15 | gethex.c dba1616 5201 16 | gmisc.c 1859d016 2084 17 | hd_init.c efdbe921 1797 18 | hexnan.c f7ea38f9 2958 19 | makefile f890b12 2932 20 | misc.c 1757f7fc 14252 21 | qnan.c efd33d64 3417 22 | smisc.c e282e715 3655 23 | strtoIQ.c 1809dfcf 1939 24 | strtoId.c f41ddac2 1931 25 | strtoIdd.c f13e3bc3 2105 26 | strtoIf.c f12c6af4 1875 27 | strtoIg.c ef30d392 3454 28 | strtoIx.c e50f716d 1960 29 | strtoIxL.c ea0b821b 1931 30 | strtod.c eec1df60 20532 31 | strtodI.c 1c2440ce 3915 32 | strtodg.c f6c3dd52 19911 33 | strtodnrp.c af895e9 2538 34 | strtof.c 1c5192d3 2073 35 | strtopQ.c f116d4f0 2563 36 | strtopd.c f7681c7a 1671 37 | strtopdd.c 9864fba 4497 38 | strtopf.c eb15b627 2067 39 | strtopx.c 1cafe482 2618 40 | strtopxL.c 1e4b77e9 2373 41 | strtorQ.c 9360a0b 2885 42 | strtord.c af5c50e 2491 43 | strtordd.c 1b266865 4936 44 | strtorf.c f0d86e2b 2396 45 | strtorx.c f19a56af 2947 46 | strtorxL.c 167fe87c 2704 47 | sum.c f525bad9 2494 48 | ulp.c 1e2e148f 1864 49 | --------------------------------------------------------------------------------