summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormartynas <>2008-09-07 20:36:10 +0000
committermartynas <>2008-09-07 20:36:10 +0000
commitfe4b8d40f2a6b20c26c7ef61d14dd8cd5582a6ed (patch)
tree8b94bbd0fbccd3b996e0c26a613b6a38022f03a7 /src
parentf7077f2415e37a53ccd37311ae6190e7238fe3e7 (diff)
downloadopenbsd-fe4b8d40f2a6b20c26c7ef61d14dd8cd5582a6ed.tar.gz
openbsd-fe4b8d40f2a6b20c26c7ef61d14dd8cd5582a6ed.tar.bz2
openbsd-fe4b8d40f2a6b20c26c7ef61d14dd8cd5582a6ed.zip
- replace dtoa w/ David's gdtoa, version 2008-03-15
- provide proper dtoa locks - use the real strtof implementation - add strtold, __hdtoa, __hldtoa - add %a/%A support - don't lose precision in printf, don't round to double anymore - implement extended-precision versions of libc functions: fpclassify, isnan, isinf, signbit, isnormal, isfinite, now that the ieee.h is fixed - separate vax versions of strtof, and __hdtoa - add complex math support. added functions: cacos, casin, catan, ccos, csin, ctan, cacosh, casinh, catanh, ccosh, csinh, ctanh, cexp, clog, cabs, cpow, csqrt, carg, cimag, conj, cproj, creal, cacosf, casinf, catanf, ccosf, csinf, ctanf, cacoshf, casinhf, catanhf, ccoshf, csinhf, ctanhf, cexpf, clogf, cabsf, cpowf, csqrtf, cargf, cimagf, conjf, cprojf, crealf - add fdim, fmax, fmin - add log2. (adapted implementation e_log.c. could be more acruate & faster, but it's good enough for now) - remove wrappers & cruft in libm, supposed to work-around mistakes in SVID, etc.; use ieee versions. fixes issues in python 2.6 for djm@ - make _digittoint static - proper definitions for i386, and amd64 in ieee.h - sh, powerpc don't really have extended-precision - add missing definitions for mips64 (quad), m{6,8}k (96-bit) float.h for LDBL_* - merge lead to frac for m{6,8}k, for gdtoa to work properly - add FRAC*BITS & EXT_TO_ARRAY32 definitions in ieee.h, for hdtoa&ldtoa to use - add EXT_IMPLICIT_NBIT definition, which indicates implicit normalization bit - add regression tests for libc: fpclassify and printf - arith.h & gd_qnan.h definitions - update ieee.h: hppa doesn't have quad-precision, hppa64 does - add missing prototypes to gdtoaimp - on 64-bit platforms make sure gdtoa doesn't use a long when it really wants an int - etc., what i may have forgotten... - bump libm major, due to removed&changed symbols - no libc bump, since this is riding on djm's libc major crank from a day ago discussed with / requested by / testing theo, sthen@, djm@, jsg@, merdely@, jsing@, tedu@, brad@, jakemsr@, and others. looks good to millert@ parts of the diff ok kettenis@ this commit does not include: - man page changes
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/Makefile.inc4
-rw-r--r--src/lib/libc/stdlib/strtod.c2459
-rw-r--r--src/lib/libc/stdlib/strtof.c39
-rw-r--r--src/regress/lib/libc/Makefile8
-rw-r--r--src/regress/lib/libc/fpclassify/Makefile5
-rw-r--r--src/regress/lib/libc/fpclassify/fpclassify.c76
-rw-r--r--src/regress/lib/libc/printf/Makefile5
-rw-r--r--src/regress/lib/libc/printf/fp.c217
8 files changed, 309 insertions, 2504 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc
index 09db4ce793..ab8bd39f07 100644
--- a/src/lib/libc/stdlib/Makefile.inc
+++ b/src/lib/libc/stdlib/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.39 2008/08/22 17:14:56 otto Exp $ 1# $OpenBSD: Makefile.inc,v 1.40 2008/09/07 20:36:08 martynas Exp $
2 2
3# stdlib sources 3# stdlib sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib 4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib
@@ -7,7 +7,7 @@ SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \
7 cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ 7 cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \
8 getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \ 8 getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \
9 lldiv.c lsearch.c malloc.c merge.c putenv.c qsort.c radixsort.c rand.c \ 9 lldiv.c lsearch.c malloc.c merge.c putenv.c qsort.c radixsort.c rand.c \
10 random.c realpath.c setenv.c strtoimax.c strtod.c strtof.c strtol.c \ 10 random.c realpath.c setenv.c strtoimax.c strtol.c \
11 strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c system.c \ 11 strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c system.c \
12 tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \ 12 tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \
13 lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c 13 lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c
diff --git a/src/lib/libc/stdlib/strtod.c b/src/lib/libc/stdlib/strtod.c
deleted file mode 100644
index 4dc3d65a26..0000000000
--- a/src/lib/libc/stdlib/strtod.c
+++ /dev/null
@@ -1,2459 +0,0 @@
1/* $OpenBSD: strtod.c,v 1.30 2006/10/30 18:30:52 deraadt Exp $ */
2/****************************************************************
3 *
4 * The author of this software is David M. Gay.
5 *
6 * Copyright (c) 1991 by AT&T.
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose without fee is hereby granted, provided that this entire notice
10 * is included in all copies of any software which is or includes a copy
11 * or modification of this software and in all copies of the supporting
12 * documentation for such software.
13 *
14 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
15 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
16 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
17 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
18 *
19 ***************************************************************/
20
21/* Please send bug reports to
22 David M. Gay
23 AT&T Bell Laboratories, Room 2C-463
24 600 Mountain Avenue
25 Murray Hill, NJ 07974-2070
26 U.S.A.
27 dmg@research.att.com or research!dmg
28 */
29
30/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
31 *
32 * This strtod returns a nearest machine number to the input decimal
33 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
34 * broken by the IEEE round-even rule. Otherwise ties are broken by
35 * biased rounding (add half and chop).
36 *
37 * Inspired loosely by William D. Clinger's paper "How to Read Floating
38 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
39 *
40 * Modifications:
41 *
42 * 1. We only require IEEE, IBM, or VAX double-precision
43 * arithmetic (not IEEE double-extended).
44 * 2. We get by with floating-point arithmetic in a case that
45 * Clinger missed -- when we're computing d * 10^n
46 * for a small integer d and the integer n is not too
47 * much larger than 22 (the maximum integer k for which
48 * we can represent 10^k exactly), we may be able to
49 * compute (d*10^k) * 10^(e-k) with just one roundoff.
50 * 3. Rather than a bit-at-a-time adjustment of the binary
51 * result in the hard case, we use floating-point
52 * arithmetic to determine the adjustment to within
53 * one bit; only in really hard cases do we need to
54 * compute a second residual.
55 * 4. Because of 3., we don't need a large table of powers of 10
56 * for ten-to-e (just some small tables, e.g. of 10^k
57 * for 0 <= k <= 22).
58 */
59
60/*
61 * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
62 * significant byte has the lowest address.
63 * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
64 * significant byte has the lowest address.
65 * #define Long int on machines with 32-bit ints and 64-bit longs.
66 * #define Sudden_Underflow for IEEE-format machines without gradual
67 * underflow (i.e., that flush to zero on underflow).
68 * #define IBM for IBM mainframe-style floating-point arithmetic.
69 * #define VAX for VAX-style floating-point arithmetic.
70 * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
71 * #define No_leftright to omit left-right logic in fast floating-point
72 * computation of dtoa.
73 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
74 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
75 * that use extended-precision instructions to compute rounded
76 * products and quotients) with IBM.
77 * #define ROUND_BIASED for IEEE-format with biased rounding.
78 * #define Inaccurate_Divide for IEEE-format with correctly rounded
79 * products but inaccurate quotients, e.g., for Intel i860.
80 * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
81 * integer arithmetic. Whether this speeds things up or slows things
82 * down depends on the machine and the number being converted.
83 * #define Bad_float_h if your system lacks a float.h or if it does not
84 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
85 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
86 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
87 * if memory is available and otherwise does something you deem
88 * appropriate. If MALLOC is undefined, malloc will be invoked
89 * directly -- and assumed always to succeed.
90 */
91
92#include <sys/types.h>
93#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
94 defined(__mips__) || defined(__mips64__) || defined(__ns32k__) || \
95 defined(__alpha__) || defined(__powerpc__) || defined(__m88k__) || \
96 defined(__hppa__) || defined(__amd64__) || defined(__sh__) || \
97 defined(__sparc64__) || \
98 (defined(__arm__) && defined(__VFP_FP__))
99
100#if BYTE_ORDER == BIG_ENDIAN
101#define IEEE_BIG_ENDIAN
102#else
103#define IEEE_LITTLE_ENDIAN
104#endif
105#endif
106
107#if defined(__arm__) && !defined(__VFP_FP__)
108/*
109 * Although the CPU is little endian the FP has different
110 * byte and word endianness. The byte order is still little endian
111 * but the word order is big endian.
112 */
113#define IEEE_BIG_ENDIAN
114#endif
115
116#ifdef __vax__
117#define VAX
118#endif
119
120#define Long int32_t
121#define ULong u_int32_t
122
123#ifdef DEBUG
124#include "stdio.h"
125#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
126#endif
127
128#include "thread_private.h"
129
130_THREAD_PRIVATE_KEY(dtoa);
131_THREAD_PRIVATE_KEY(pow5mult);
132
133#ifdef __cplusplus
134#include "malloc.h"
135#include "memory.h"
136#else
137#include "stdlib.h"
138#include "string.h"
139#include "locale.h"
140#endif
141
142#ifdef MALLOC
143extern void *MALLOC(size_t);
144#else
145#define MALLOC malloc
146#endif
147
148#include "ctype.h"
149#include "errno.h"
150
151#ifdef Bad_float_h
152#ifdef IEEE_BIG_ENDIAN
153#define IEEE_ARITHMETIC
154#endif
155#ifdef IEEE_LITTLE_ENDIAN
156#define IEEE_ARITHMETIC
157#endif
158
159#ifdef IEEE_ARITHMETIC
160#define DBL_DIG 15
161#define DBL_MAX_10_EXP 308
162#define DBL_MAX_EXP 1024
163#define FLT_RADIX 2
164#define FLT_ROUNDS 1
165#define DBL_MAX 1.7976931348623157e+308
166#endif
167
168#ifdef IBM
169#define DBL_DIG 16
170#define DBL_MAX_10_EXP 75
171#define DBL_MAX_EXP 63
172#define FLT_RADIX 16
173#define FLT_ROUNDS 0
174#define DBL_MAX 7.2370055773322621e+75
175#endif
176
177#ifdef VAX
178#define DBL_DIG 16
179#define DBL_MAX_10_EXP 38
180#define DBL_MAX_EXP 127
181#define FLT_RADIX 2
182#define FLT_ROUNDS 1
183#define DBL_MAX 1.7014118346046923e+38
184#endif
185
186#ifndef LONG_MAX
187#define LONG_MAX 2147483647
188#endif
189#else
190#include "float.h"
191#endif
192#ifndef __MATH_H__
193#include "math.h"
194#endif
195
196#ifdef __cplusplus
197extern "C" {
198#endif
199
200#ifndef CONST
201#define CONST const
202#endif
203
204#ifdef Unsigned_Shifts
205#define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
206#else
207#define Sign_Extend(a,b) /*no-op*/
208#endif
209
210#if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
211 defined(IBM) != 1
212#error Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or IBM should be defined.
213#endif
214
215typedef union {
216 double d;
217 ULong ul[2];
218} _double;
219#define value(x) ((x).d)
220#ifdef IEEE_LITTLE_ENDIAN
221#define word0(x) ((x).ul[1])
222#define word1(x) ((x).ul[0])
223#else
224#define word0(x) ((x).ul[0])
225#define word1(x) ((x).ul[1])
226#endif
227
228/* The following definition of Storeinc is appropriate for MIPS processors.
229 * An alternative that might be better on some machines is
230 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
231 */
232#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
233#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
234((unsigned short *)a)[0] = (unsigned short)c, a++)
235#else
236#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
237((unsigned short *)a)[1] = (unsigned short)c, a++)
238#endif
239
240/* #define P DBL_MANT_DIG */
241/* Ten_pmax = floor(P*log(2)/log(5)) */
242/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
243/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
244/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
245
246#if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
247#define Exp_shift 20
248#define Exp_shift1 20
249#define Exp_msk1 0x100000
250#define Exp_msk11 0x100000
251#define Exp_mask 0x7ff00000
252#define P 53
253#define Bias 1023
254#define IEEE_Arith
255#define Emin (-1022)
256#define Exp_1 0x3ff00000
257#define Exp_11 0x3ff00000
258#define Ebits 11
259#define Frac_mask 0xfffff
260#define Frac_mask1 0xfffff
261#define Ten_pmax 22
262#define Bletch 0x10
263#define Bndry_mask 0xfffff
264#define Bndry_mask1 0xfffff
265#define LSB 1
266#define Sign_bit 0x80000000
267#define Log2P 1
268#define Tiny0 0
269#define Tiny1 1
270#define Quick_max 14
271#define Int_max 14
272#define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
273#else
274#undef Sudden_Underflow
275#define Sudden_Underflow
276#ifdef IBM
277#define Exp_shift 24
278#define Exp_shift1 24
279#define Exp_msk1 0x1000000
280#define Exp_msk11 0x1000000
281#define Exp_mask 0x7f000000
282#define P 14
283#define Bias 65
284#define Exp_1 0x41000000
285#define Exp_11 0x41000000
286#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
287#define Frac_mask 0xffffff
288#define Frac_mask1 0xffffff
289#define Bletch 4
290#define Ten_pmax 22
291#define Bndry_mask 0xefffff
292#define Bndry_mask1 0xffffff
293#define LSB 1
294#define Sign_bit 0x80000000
295#define Log2P 4
296#define Tiny0 0x100000
297#define Tiny1 0
298#define Quick_max 14
299#define Int_max 15
300#else /* VAX */
301#define Exp_shift 23
302#define Exp_shift1 7
303#define Exp_msk1 0x80
304#define Exp_msk11 0x800000
305#define Exp_mask 0x7f80
306#define P 56
307#define Bias 129
308#define Exp_1 0x40800000
309#define Exp_11 0x4080
310#define Ebits 8
311#define Frac_mask 0x7fffff
312#define Frac_mask1 0xffff007f
313#define Ten_pmax 24
314#define Bletch 2
315#define Bndry_mask 0xffff007f
316#define Bndry_mask1 0xffff007f
317#define LSB 0x10000
318#define Sign_bit 0x8000
319#define Log2P 1
320#define Tiny0 0x80
321#define Tiny1 0
322#define Quick_max 15
323#define Int_max 15
324#endif
325#endif
326
327#ifndef IEEE_Arith
328#define ROUND_BIASED
329#endif
330
331#ifdef RND_PRODQUOT
332#define rounded_product(a,b) a = rnd_prod(a, b)
333#define rounded_quotient(a,b) a = rnd_quot(a, b)
334extern double rnd_prod(double, double), rnd_quot(double, double);
335#else
336#define rounded_product(a,b) a *= b
337#define rounded_quotient(a,b) a /= b
338#endif
339
340#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
341#define Big1 0xffffffff
342
343#ifndef Just_16
344/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
345 * This makes some inner loops simpler and sometimes saves work
346 * during multiplications, but it often seems to make things slightly
347 * slower. Hence the default is now to store 32 bits per Long.
348 */
349#ifndef Pack_32
350#define Pack_32
351#endif
352#endif
353
354#define Kmax 15
355
356#ifdef __cplusplus
357extern "C" double strtod(const char *s00, char **se);
358extern "C" char *__dtoa(double d, int mode, int ndigits,
359 int *decpt, int *sign, char **rve);
360#endif
361
362 struct
363Bigint {
364 struct Bigint *next;
365 int k, maxwds, sign, wds;
366 ULong x[1];
367 };
368
369 typedef struct Bigint Bigint;
370
371 static Bigint *freelist[Kmax+1];
372
373#define PRIVATE_MEM 2304
374#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
375 static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
376
377 static Bigint *
378Balloc(int k)
379{
380 int x;
381 unsigned int len;
382 Bigint *rv;
383
384 _THREAD_PRIVATE_MUTEX_LOCK(dtoa);
385 if ((rv = freelist[k])) {
386 freelist[k] = rv->next;
387 }
388 else {
389 x = 1 << k;
390 len = (sizeof(Bigint) + (x-1)*sizeof(Long) + sizeof(double) - 1)
391 /sizeof(double);
392 if (pmem_next - private_mem + len <= PRIVATE_mem) {
393 rv = (Bigint *)pmem_next;
394 pmem_next += len;
395 }
396 else
397 rv = (Bigint *)MALLOC(len *sizeof(double));
398 rv->k = k;
399 rv->maxwds = x;
400 }
401 _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa);
402 rv->sign = rv->wds = 0;
403 return rv;
404 }
405
406 static void
407Bfree(Bigint *v)
408{
409 if (v) {
410 _THREAD_PRIVATE_MUTEX_LOCK(dtoa);
411 v->next = freelist[v->k];
412 freelist[v->k] = v;
413 _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa);
414 }
415 }
416
417#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
418y->wds*sizeof(Long) + 2*sizeof(int))
419
420/* return value is only used as a simple string, so mis-aligned parts
421 * inside the Bigint are not at risk on strict align architectures
422 */
423 static char *
424rv_alloc(int i)
425{
426 int j, k, *r;
427
428 j = sizeof(ULong);
429 for(k = 0;
430 sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
431 j <<= 1)
432 k++;
433 r = (int*)Balloc(k);
434 *r = k;
435 return (char *)(r+1);
436 }
437
438 static char *
439nrv_alloc(char *s, char **rve, int n)
440{
441 char *rv, *t;
442
443 t = rv = rv_alloc(n);
444 while((*t = *s++) !=0)
445 t++;
446 if (rve)
447 *rve = t;
448 return rv;
449 }
450
451 void
452__freedtoa(char *s)
453{
454 Bigint *b = (Bigint *)((int *)s - 1);
455 b->maxwds = 1 << (b->k = *(int*)b);
456 Bfree(b);
457 }
458
459 static Bigint *
460multadd(Bigint *b, int m, int a) /* multiply by m and add a */
461{
462 int i, wds;
463 ULong *x, y;
464#ifdef Pack_32
465 ULong xi, z;
466#endif
467 Bigint *b1;
468
469 wds = b->wds;
470 x = b->x;
471 i = 0;
472 do {
473#ifdef Pack_32
474 xi = *x;
475 y = (xi & 0xffff) * m + a;
476 z = (xi >> 16) * m + (y >> 16);
477 a = (int)(z >> 16);
478 *x++ = (z << 16) + (y & 0xffff);
479#else
480 y = *x * m + a;
481 a = (int)(y >> 16);
482 *x++ = y & 0xffff;
483#endif
484 }
485 while(++i < wds);
486 if (a) {
487 if (wds >= b->maxwds) {
488 b1 = Balloc(b->k+1);
489 Bcopy(b1, b);
490 Bfree(b);
491 b = b1;
492 }
493 b->x[wds++] = a;
494 b->wds = wds;
495 }
496 return b;
497 }
498
499 static Bigint *
500s2b(CONST char *s, int nd0, int nd, ULong y9)
501{
502 Bigint *b;
503 int i, k;
504 Long x, y;
505
506 x = (nd + 8) / 9;
507 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
508#ifdef Pack_32
509 b = Balloc(k);
510 b->x[0] = y9;
511 b->wds = 1;
512#else
513 b = Balloc(k+1);
514 b->x[0] = y9 & 0xffff;
515 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
516#endif
517
518 i = 9;
519 if (9 < nd0) {
520 s += 9;
521 do b = multadd(b, 10, *s++ - '0');
522 while(++i < nd0);
523 s++;
524 }
525 else
526 s += 10;
527 for(; i < nd; i++)
528 b = multadd(b, 10, *s++ - '0');
529 return b;
530 }
531
532 static int
533hi0bits(ULong x)
534{
535 int k = 0;
536
537 if (!(x & 0xffff0000)) {
538 k = 16;
539 x <<= 16;
540 }
541 if (!(x & 0xff000000)) {
542 k += 8;
543 x <<= 8;
544 }
545 if (!(x & 0xf0000000)) {
546 k += 4;
547 x <<= 4;
548 }
549 if (!(x & 0xc0000000)) {
550 k += 2;
551 x <<= 2;
552 }
553 if (!(x & 0x80000000)) {
554 k++;
555 if (!(x & 0x40000000))
556 return 32;
557 }
558 return k;
559 }
560
561 static int
562lo0bits(ULong *y)
563{
564 int k;
565 ULong x = *y;
566
567 if (x & 7) {
568 if (x & 1)
569 return 0;
570 if (x & 2) {
571 *y = x >> 1;
572 return 1;
573 }
574 *y = x >> 2;
575 return 2;
576 }
577 k = 0;
578 if (!(x & 0xffff)) {
579 k = 16;
580 x >>= 16;
581 }
582 if (!(x & 0xff)) {
583 k += 8;
584 x >>= 8;
585 }
586 if (!(x & 0xf)) {
587 k += 4;
588 x >>= 4;
589 }
590 if (!(x & 0x3)) {
591 k += 2;
592 x >>= 2;
593 }
594 if (!(x & 1)) {
595 k++;
596 x >>= 1;
597 if (!(x & 1))
598 return 32;
599 }
600 *y = x;
601 return k;
602 }
603
604 static Bigint *
605i2b(int i)
606{
607 Bigint *b;
608
609 b = Balloc(1);
610 b->x[0] = i;
611 b->wds = 1;
612 return b;
613 }
614
615 static Bigint *
616mult(Bigint *a, Bigint *b)
617{
618 Bigint *c;
619 int k, wa, wb, wc;
620 ULong carry, y, z;
621 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
622#ifdef Pack_32
623 ULong z2;
624#endif
625
626 if (a->wds < b->wds) {
627 c = a;
628 a = b;
629 b = c;
630 }
631 k = a->k;
632 wa = a->wds;
633 wb = b->wds;
634 wc = wa + wb;
635 if (wc > a->maxwds)
636 k++;
637 c = Balloc(k);
638 for(x = c->x, xa = x + wc; x < xa; x++)
639 *x = 0;
640 xa = a->x;
641 xae = xa + wa;
642 xb = b->x;
643 xbe = xb + wb;
644 xc0 = c->x;
645#ifdef Pack_32
646 for(; xb < xbe; xb++, xc0++) {
647 if ((y = *xb & 0xffff)) {
648 x = xa;
649 xc = xc0;
650 carry = 0;
651 do {
652 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
653 carry = z >> 16;
654 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
655 carry = z2 >> 16;
656 Storeinc(xc, z2, z);
657 }
658 while(x < xae);
659 *xc = carry;
660 }
661 if ((y = *xb >> 16)) {
662 x = xa;
663 xc = xc0;
664 carry = 0;
665 z2 = *xc;
666 do {
667 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
668 carry = z >> 16;
669 Storeinc(xc, z, z2);
670 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
671 carry = z2 >> 16;
672 }
673 while(x < xae);
674 *xc = z2;
675 }
676 }
677#else
678 for(; xb < xbe; xc0++) {
679 if (y = *xb++) {
680 x = xa;
681 xc = xc0;
682 carry = 0;
683 do {
684 z = *x++ * y + *xc + carry;
685 carry = z >> 16;
686 *xc++ = z & 0xffff;
687 }
688 while(x < xae);
689 *xc = carry;
690 }
691 }
692#endif
693 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
694 c->wds = wc;
695 return c;
696 }
697
698 static Bigint *p5s;
699
700 static Bigint *
701pow5mult(Bigint *b, int k)
702{
703 Bigint *b1, *p5, *p51;
704 int i;
705 static int p05[3] = { 5, 25, 125 };
706
707 if ((i = k & 3))
708 b = multadd(b, p05[i-1], 0);
709
710 if (!(k >>= 2))
711 return b;
712 if (!(p5 = p5s)) {
713 /* first time */
714 _THREAD_PRIVATE_MUTEX_LOCK(pow5mult);
715 p5 = p5s = i2b(625);
716 p5->next = 0;
717 _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult);
718 }
719 for(;;) {
720 if (k & 1) {
721 b1 = mult(b, p5);
722 Bfree(b);
723 b = b1;
724 }
725 if (!(k >>= 1))
726 break;
727 if (!(p51 = p5->next)) {
728 _THREAD_PRIVATE_MUTEX_LOCK(pow5mult);
729 if (!(p51 = p5->next)) {
730 p51 = p5->next = mult(p5,p5);
731 p51->next = 0;
732 }
733 _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult);
734 }
735 p5 = p51;
736 }
737 return b;
738 }
739
740 static Bigint *
741lshift(Bigint *b, int k)
742{
743 int i, k1, n, n1;
744 Bigint *b1;
745 ULong *x, *x1, *xe, z;
746
747#ifdef Pack_32
748 n = k >> 5;
749#else
750 n = k >> 4;
751#endif
752 k1 = b->k;
753 n1 = n + b->wds + 1;
754 for(i = b->maxwds; n1 > i; i <<= 1)
755 k1++;
756 b1 = Balloc(k1);
757 x1 = b1->x;
758 for(i = 0; i < n; i++)
759 *x1++ = 0;
760 x = b->x;
761 xe = x + b->wds;
762#ifdef Pack_32
763 if (k &= 0x1f) {
764 k1 = 32 - k;
765 z = 0;
766 do {
767 *x1++ = *x << k | z;
768 z = *x++ >> k1;
769 }
770 while(x < xe);
771 if ((*x1 = z))
772 ++n1;
773 }
774#else
775 if (k &= 0xf) {
776 k1 = 16 - k;
777 z = 0;
778 do {
779 *x1++ = *x << k & 0xffff | z;
780 z = *x++ >> k1;
781 }
782 while(x < xe);
783 if (*x1 = z)
784 ++n1;
785 }
786#endif
787 else do
788 *x1++ = *x++;
789 while(x < xe);
790 b1->wds = n1 - 1;
791 Bfree(b);
792 return b1;
793 }
794
795 static int
796cmp(Bigint *a, Bigint *b)
797{
798 ULong *xa, *xa0, *xb, *xb0;
799 int i, j;
800
801 i = a->wds;
802 j = b->wds;
803#ifdef DEBUG
804 if (i > 1 && !a->x[i-1])
805 Bug("cmp called with a->x[a->wds-1] == 0");
806 if (j > 1 && !b->x[j-1])
807 Bug("cmp called with b->x[b->wds-1] == 0");
808#endif
809 if (i -= j)
810 return i;
811 xa0 = a->x;
812 xa = xa0 + j;
813 xb0 = b->x;
814 xb = xb0 + j;
815 for(;;) {
816 if (*--xa != *--xb)
817 return *xa < *xb ? -1 : 1;
818 if (xa <= xa0)
819 break;
820 }
821 return 0;
822 }
823
824 static Bigint *
825diff(Bigint *a, Bigint *b)
826{
827 Bigint *c;
828 int i, wa, wb;
829 Long borrow, y; /* We need signed shifts here. */
830 ULong *xa, *xae, *xb, *xbe, *xc;
831#ifdef Pack_32
832 Long z;
833#endif
834
835 i = cmp(a,b);
836 if (!i) {
837 c = Balloc(0);
838 c->wds = 1;
839 c->x[0] = 0;
840 return c;
841 }
842 if (i < 0) {
843 c = a;
844 a = b;
845 b = c;
846 i = 1;
847 }
848 else
849 i = 0;
850 c = Balloc(a->k);
851 c->sign = i;
852 wa = a->wds;
853 xa = a->x;
854 xae = xa + wa;
855 wb = b->wds;
856 xb = b->x;
857 xbe = xb + wb;
858 xc = c->x;
859 borrow = 0;
860#ifdef Pack_32
861 do {
862 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
863 borrow = y >> 16;
864 Sign_Extend(borrow, y);
865 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
866 borrow = z >> 16;
867 Sign_Extend(borrow, z);
868 Storeinc(xc, z, y);
869 }
870 while(xb < xbe);
871 while(xa < xae) {
872 y = (*xa & 0xffff) + borrow;
873 borrow = y >> 16;
874 Sign_Extend(borrow, y);
875 z = (*xa++ >> 16) + borrow;
876 borrow = z >> 16;
877 Sign_Extend(borrow, z);
878 Storeinc(xc, z, y);
879 }
880#else
881 do {
882 y = *xa++ - *xb++ + borrow;
883 borrow = y >> 16;
884 Sign_Extend(borrow, y);
885 *xc++ = y & 0xffff;
886 }
887 while(xb < xbe);
888 while(xa < xae) {
889 y = *xa++ + borrow;
890 borrow = y >> 16;
891 Sign_Extend(borrow, y);
892 *xc++ = y & 0xffff;
893 }
894#endif
895 while(!*--xc)
896 wa--;
897 c->wds = wa;
898 return c;
899 }
900
901 static double
902ulp(double _x)
903{
904 _double x;
905 Long L;
906 _double a;
907
908 value(x) = _x;
909 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
910#ifndef Sudden_Underflow
911 if (L > 0) {
912#endif
913#ifdef IBM
914 L |= Exp_msk1 >> 4;
915#endif
916 word0(a) = L;
917 word1(a) = 0;
918#ifndef Sudden_Underflow
919 }
920 else {
921 L = -L >> Exp_shift;
922 if (L < Exp_shift) {
923 word0(a) = 0x80000 >> L;
924 word1(a) = 0;
925 }
926 else {
927 word0(a) = 0;
928 L -= Exp_shift;
929 word1(a) = L >= 31 ? 1 : 1 << 31 - L;
930 }
931 }
932#endif
933 return value(a);
934 }
935
936 static double
937b2d(Bigint *a, int *e)
938{
939 ULong *xa, *xa0, w, y, z;
940 int k;
941 _double d;
942#ifdef VAX
943 ULong d0, d1;
944#else
945#define d0 word0(d)
946#define d1 word1(d)
947#endif
948
949 xa0 = a->x;
950 xa = xa0 + a->wds;
951 y = *--xa;
952#ifdef DEBUG
953 if (!y) Bug("zero y in b2d");
954#endif
955 k = hi0bits(y);
956 *e = 32 - k;
957#ifdef Pack_32
958 if (k < Ebits) {
959 d0 = Exp_1 | y >> Ebits - k;
960 w = xa > xa0 ? *--xa : 0;
961 d1 = y << (32-Ebits) + k | w >> Ebits - k;
962 goto ret_d;
963 }
964 z = xa > xa0 ? *--xa : 0;
965 if (k -= Ebits) {
966 d0 = Exp_1 | y << k | z >> 32 - k;
967 y = xa > xa0 ? *--xa : 0;
968 d1 = z << k | y >> 32 - k;
969 }
970 else {
971 d0 = Exp_1 | y;
972 d1 = z;
973 }
974#else
975 if (k < Ebits + 16) {
976 z = xa > xa0 ? *--xa : 0;
977 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
978 w = xa > xa0 ? *--xa : 0;
979 y = xa > xa0 ? *--xa : 0;
980 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
981 goto ret_d;
982 }
983 z = xa > xa0 ? *--xa : 0;
984 w = xa > xa0 ? *--xa : 0;
985 k -= Ebits + 16;
986 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
987 y = xa > xa0 ? *--xa : 0;
988 d1 = w << k + 16 | y << k;
989#endif
990 ret_d:
991#ifdef VAX
992 word0(d) = d0 >> 16 | d0 << 16;
993 word1(d) = d1 >> 16 | d1 << 16;
994#else
995#undef d0
996#undef d1
997#endif
998 return value(d);
999 }
1000
1001 static Bigint *
1002d2b(double _d, int *e, int *bits)
1003{
1004 Bigint *b;
1005 int de, i, k;
1006 ULong *x, y, z;
1007 _double d;
1008#ifdef VAX
1009 ULong d0, d1;
1010#endif
1011
1012 value(d) = _d;
1013#ifdef VAX
1014 d0 = word0(d) >> 16 | word0(d) << 16;
1015 d1 = word1(d) >> 16 | word1(d) << 16;
1016#else
1017#define d0 word0(d)
1018#define d1 word1(d)
1019#endif
1020
1021#ifdef Pack_32
1022 b = Balloc(1);
1023#else
1024 b = Balloc(2);
1025#endif
1026 x = b->x;
1027
1028 z = d0 & Frac_mask;
1029 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1030#ifdef Sudden_Underflow
1031 de = (int)(d0 >> Exp_shift);
1032#ifndef IBM
1033 z |= Exp_msk11;
1034#endif
1035#else
1036 if (de = (int)(d0 >> Exp_shift))
1037 z |= Exp_msk1;
1038#endif
1039#ifdef Pack_32
1040 if (y = d1) {
1041 if (k = lo0bits(&y)) {
1042 x[0] = y | z << 32 - k;
1043 z >>= k;
1044 }
1045 else
1046 x[0] = y;
1047 i = b->wds = (x[1] = z) ? 2 : 1;
1048 }
1049 else {
1050#ifdef DEBUG
1051 if (!z)
1052 Bug("Zero passed to d2b");
1053#endif
1054 k = lo0bits(&z);
1055 x[0] = z;
1056 i = b->wds = 1;
1057 k += 32;
1058 }
1059#else
1060 if (y = d1) {
1061 if (k = lo0bits(&y))
1062 if (k >= 16) {
1063 x[0] = y | z << 32 - k & 0xffff;
1064 x[1] = z >> k - 16 & 0xffff;
1065 x[2] = z >> k;
1066 i = 2;
1067 }
1068 else {
1069 x[0] = y & 0xffff;
1070 x[1] = y >> 16 | z << 16 - k & 0xffff;
1071 x[2] = z >> k & 0xffff;
1072 x[3] = z >> k+16;
1073 i = 3;
1074 }
1075 else {
1076 x[0] = y & 0xffff;
1077 x[1] = y >> 16;
1078 x[2] = z & 0xffff;
1079 x[3] = z >> 16;
1080 i = 3;
1081 }
1082 }
1083 else {
1084#ifdef DEBUG
1085 if (!z)
1086 Bug("Zero passed to d2b");
1087#endif
1088 k = lo0bits(&z);
1089 if (k >= 16) {
1090 x[0] = z;
1091 i = 0;
1092 }
1093 else {
1094 x[0] = z & 0xffff;
1095 x[1] = z >> 16;
1096 i = 1;
1097 }
1098 k += 32;
1099 }
1100 while(!x[i])
1101 --i;
1102 b->wds = i + 1;
1103#endif
1104#ifndef Sudden_Underflow
1105 if (de) {
1106#endif
1107#ifdef IBM
1108 *e = (de - Bias - (P-1) << 2) + k;
1109 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1110#else
1111 *e = de - Bias - (P-1) + k;
1112 *bits = P - k;
1113#endif
1114#ifndef Sudden_Underflow
1115 }
1116 else {
1117 *e = de - Bias - (P-1) + 1 + k;
1118#ifdef Pack_32
1119 *bits = 32*i - hi0bits(x[i-1]);
1120#else
1121 *bits = (i+2)*16 - hi0bits(x[i]);
1122#endif
1123 }
1124#endif
1125 return b;
1126 }
1127#undef d0
1128#undef d1
1129
1130 static double
1131ratio(Bigint *a, Bigint *b)
1132{
1133 _double da, db;
1134 int k, ka, kb;
1135
1136 value(da) = b2d(a, &ka);
1137 value(db) = b2d(b, &kb);
1138#ifdef Pack_32
1139 k = ka - kb + 32*(a->wds - b->wds);
1140#else
1141 k = ka - kb + 16*(a->wds - b->wds);
1142#endif
1143#ifdef IBM
1144 if (k > 0) {
1145 word0(da) += (k >> 2)*Exp_msk1;
1146 if (k &= 3)
1147 da *= 1 << k;
1148 }
1149 else {
1150 k = -k;
1151 word0(db) += (k >> 2)*Exp_msk1;
1152 if (k &= 3)
1153 db *= 1 << k;
1154 }
1155#else
1156 if (k > 0)
1157 word0(da) += k*Exp_msk1;
1158 else {
1159 k = -k;
1160 word0(db) += k*Exp_msk1;
1161 }
1162#endif
1163 return value(da) / value(db);
1164 }
1165
1166static CONST double
1167tens[] = {
1168 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1169 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1170 1e20, 1e21, 1e22
1171#ifdef VAX
1172 , 1e23, 1e24
1173#endif
1174 };
1175
1176#ifdef IEEE_Arith
1177static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1178static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1179#define n_bigtens 5
1180#else
1181#ifdef IBM
1182static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
1183static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1184#define n_bigtens 3
1185#else
1186static CONST double bigtens[] = { 1e16, 1e32 };
1187static CONST double tinytens[] = { 1e-16, 1e-32 };
1188#define n_bigtens 2
1189#endif
1190#endif
1191
1192 double
1193strtod(CONST char *s00, char **se)
1194{
1195 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1196 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1197 CONST char *s, *s0, *s1;
1198 double aadj, aadj1, adj;
1199 /*
1200 * volatile forces mem update for FPUs where reg size != sizeof double,
1201 * which should trigger ERANGE in the case of underflow.
1202 */
1203 volatile _double rv;
1204 _double rv0;
1205 Long L;
1206 ULong y, z;
1207 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1208
1209 CONST char decimal_point = localeconv()->decimal_point[0];
1210
1211 sign = nz0 = nz = 0;
1212 value(rv) = 0.;
1213
1214
1215 for(s = s00; isspace((unsigned char) *s); s++)
1216 ;
1217
1218 if (*s == '-') {
1219 sign = 1;
1220 s++;
1221 } else if (*s == '+') {
1222 s++;
1223 }
1224
1225 if (*s == '\0') {
1226 s = s00;
1227 goto ret;
1228 }
1229
1230 if (*s == '0') {
1231 nz0 = 1;
1232 while(*++s == '0') ;
1233 if (!*s)
1234 goto ret;
1235 }
1236 s0 = s;
1237 y = z = 0;
1238 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1239 if (nd < 9)
1240 y = 10*y + c - '0';
1241 else if (nd < 16)
1242 z = 10*z + c - '0';
1243 nd0 = nd;
1244 if (c == decimal_point) {
1245 c = *++s;
1246 if (!nd) {
1247 for(; c == '0'; c = *++s)
1248 nz++;
1249 if (c > '0' && c <= '9') {
1250 s0 = s;
1251 nf += nz;
1252 nz = 0;
1253 goto have_dig;
1254 }
1255 goto dig_done;
1256 }
1257 for(; c >= '0' && c <= '9'; c = *++s) {
1258 have_dig:
1259 nz++;
1260 if (c -= '0') {
1261 nf += nz;
1262 for(i = 1; i < nz; i++)
1263 if (nd++ < 9)
1264 y *= 10;
1265 else if (nd <= DBL_DIG + 1)
1266 z *= 10;
1267 if (nd++ < 9)
1268 y = 10*y + c;
1269 else if (nd <= DBL_DIG + 1)
1270 z = 10*z + c;
1271 nz = 0;
1272 }
1273 }
1274 }
1275 dig_done:
1276 e = 0;
1277 if (c == 'e' || c == 'E') {
1278 if (!nd && !nz && !nz0) {
1279 s = s00;
1280 goto ret;
1281 }
1282 s00 = s;
1283 esign = 0;
1284 switch(c = *++s) {
1285 case '-':
1286 esign = 1;
1287 case '+':
1288 c = *++s;
1289 }
1290 if (c >= '0' && c <= '9') {
1291 while(c == '0')
1292 c = *++s;
1293 if (c > '0' && c <= '9') {
1294 L = c - '0';
1295 s1 = s;
1296 while((c = *++s) >= '0' && c <= '9')
1297 L = 10*L + c - '0';
1298 if (s - s1 > 8 || L > 19999)
1299 /* Avoid confusion from exponents
1300 * so large that e might overflow.
1301 */
1302 e = 19999; /* safe for 16 bit ints */
1303 else
1304 e = (int)L;
1305 if (esign)
1306 e = -e;
1307 }
1308 else
1309 e = 0;
1310 }
1311 else
1312 s = s00;
1313 }
1314 if (!nd) {
1315 if (!nz && !nz0)
1316 s = s00;
1317 goto ret;
1318 }
1319 e1 = e -= nf;
1320
1321 /* Now we have nd0 digits, starting at s0, followed by a
1322 * decimal point, followed by nd-nd0 digits. The number we're
1323 * after is the integer represented by those digits times
1324 * 10**e */
1325
1326 if (!nd0)
1327 nd0 = nd;
1328 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1329 value(rv) = y;
1330 if (k > 9)
1331 value(rv) = tens[k - 9] * value(rv) + z;
1332 bd0 = 0;
1333 if (nd <= DBL_DIG
1334#ifndef RND_PRODQUOT
1335 && FLT_ROUNDS == 1
1336#endif
1337 ) {
1338 if (!e)
1339 goto ret;
1340 if (e > 0) {
1341 if (e <= Ten_pmax) {
1342#ifdef VAX
1343 goto vax_ovfl_check;
1344#else
1345 /* value(rv) = */ rounded_product(value(rv),
1346 tens[e]);
1347 goto ret;
1348#endif
1349 }
1350 i = DBL_DIG - nd;
1351 if (e <= Ten_pmax + i) {
1352 /* A fancier test would sometimes let us do
1353 * this for larger i values.
1354 */
1355 e -= i;
1356 value(rv) *= tens[i];
1357#ifdef VAX
1358 /* VAX exponent range is so narrow we must
1359 * worry about overflow here...
1360 */
1361 vax_ovfl_check:
1362 word0(rv) -= P*Exp_msk1;
1363 /* value(rv) = */ rounded_product(value(rv),
1364 tens[e]);
1365 if ((word0(rv) & Exp_mask)
1366 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1367 goto ovfl;
1368 word0(rv) += P*Exp_msk1;
1369#else
1370 /* value(rv) = */ rounded_product(value(rv),
1371 tens[e]);
1372#endif
1373 goto ret;
1374 }
1375 }
1376#ifndef Inaccurate_Divide
1377 else if (e >= -Ten_pmax) {
1378 /* value(rv) = */ rounded_quotient(value(rv),
1379 tens[-e]);
1380 goto ret;
1381 }
1382#endif
1383 }
1384 e1 += nd - k;
1385
1386 /* Get starting approximation = rv * 10**e1 */
1387
1388 if (e1 > 0) {
1389 if (i = e1 & 15)
1390 value(rv) *= tens[i];
1391 if (e1 &= ~15) {
1392 if (e1 > DBL_MAX_10_EXP) {
1393 ovfl:
1394 errno = ERANGE;
1395#ifndef Bad_float_h
1396 value(rv) = HUGE_VAL;
1397#else
1398 /* Can't trust HUGE_VAL */
1399#ifdef IEEE_Arith
1400 word0(rv) = Exp_mask;
1401 word1(rv) = 0;
1402#else
1403 word0(rv) = Big0;
1404 word1(rv) = Big1;
1405#endif
1406#endif
1407 if (bd0)
1408 goto retfree;
1409 goto ret;
1410 }
1411 if (e1 >>= 4) {
1412 for(j = 0; e1 > 1; j++, e1 >>= 1)
1413 if (e1 & 1)
1414 value(rv) *= bigtens[j];
1415 /* The last multiplication could overflow. */
1416 word0(rv) -= P*Exp_msk1;
1417 value(rv) *= bigtens[j];
1418 if ((z = word0(rv) & Exp_mask)
1419 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1420 goto ovfl;
1421 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1422 /* set to largest number */
1423 /* (Can't trust DBL_MAX) */
1424 word0(rv) = Big0;
1425 word1(rv) = Big1;
1426 }
1427 else
1428 word0(rv) += P*Exp_msk1;
1429 }
1430
1431 }
1432 }
1433 else if (e1 < 0) {
1434 e1 = -e1;
1435 if (i = e1 & 15)
1436 value(rv) /= tens[i];
1437 if (e1 &= ~15) {
1438 e1 >>= 4;
1439 if (e1 >= 1 << n_bigtens)
1440 goto undfl;
1441 for(j = 0; e1 > 1; j++, e1 >>= 1)
1442 if (e1 & 1)
1443 value(rv) *= tinytens[j];
1444 /* The last multiplication could underflow. */
1445 value(rv0) = value(rv);
1446 value(rv) *= tinytens[j];
1447 if (!value(rv)) {
1448 value(rv) = 2.*value(rv0);
1449 value(rv) *= tinytens[j];
1450 if (!value(rv)) {
1451 undfl:
1452 value(rv) = 0.;
1453 errno = ERANGE;
1454 if (bd0)
1455 goto retfree;
1456 goto ret;
1457 }
1458 word0(rv) = Tiny0;
1459 word1(rv) = Tiny1;
1460 /* The refinement below will clean
1461 * this approximation up.
1462 */
1463 }
1464 }
1465 }
1466
1467 /* Now the hard part -- adjusting rv to the correct value.*/
1468
1469 /* Put digits into bd: true value = bd * 10^e */
1470
1471 bd0 = s2b(s0, nd0, nd, y);
1472
1473 for(;;) {
1474 bd = Balloc(bd0->k);
1475 Bcopy(bd, bd0);
1476 bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
1477 bs = i2b(1);
1478
1479 if (e >= 0) {
1480 bb2 = bb5 = 0;
1481 bd2 = bd5 = e;
1482 }
1483 else {
1484 bb2 = bb5 = -e;
1485 bd2 = bd5 = 0;
1486 }
1487 if (bbe >= 0)
1488 bb2 += bbe;
1489 else
1490 bd2 -= bbe;
1491 bs2 = bb2;
1492#ifdef Sudden_Underflow
1493#ifdef IBM
1494 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1495#else
1496 j = P + 1 - bbbits;
1497#endif
1498#else
1499 i = bbe + bbbits - 1; /* logb(rv) */
1500 if (i < Emin) /* denormal */
1501 j = bbe + (P-Emin);
1502 else
1503 j = P + 1 - bbbits;
1504#endif
1505 bb2 += j;
1506 bd2 += j;
1507 i = bb2 < bd2 ? bb2 : bd2;
1508 if (i > bs2)
1509 i = bs2;
1510 if (i > 0) {
1511 bb2 -= i;
1512 bd2 -= i;
1513 bs2 -= i;
1514 }
1515 if (bb5 > 0) {
1516 bs = pow5mult(bs, bb5);
1517 bb1 = mult(bs, bb);
1518 Bfree(bb);
1519 bb = bb1;
1520 }
1521 if (bb2 > 0)
1522 bb = lshift(bb, bb2);
1523 if (bd5 > 0)
1524 bd = pow5mult(bd, bd5);
1525 if (bd2 > 0)
1526 bd = lshift(bd, bd2);
1527 if (bs2 > 0)
1528 bs = lshift(bs, bs2);
1529 delta = diff(bb, bd);
1530 dsign = delta->sign;
1531 delta->sign = 0;
1532 i = cmp(delta, bs);
1533 if (i < 0) {
1534 /* Error is less than half an ulp -- check for
1535 * special case of mantissa a power of two.
1536 */
1537 if (dsign || word1(rv) || word0(rv) & Bndry_mask)
1538 break;
1539 delta = lshift(delta,Log2P);
1540 if (cmp(delta, bs) > 0)
1541 goto drop_down;
1542 break;
1543 }
1544 if (i == 0) {
1545 /* exactly half-way between */
1546 if (dsign) {
1547 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
1548 && word1(rv) == 0xffffffff) {
1549 /*boundary case -- increment exponent*/
1550 word0(rv) = (word0(rv) & Exp_mask)
1551 + Exp_msk1
1552#ifdef IBM
1553 | Exp_msk1 >> 4
1554#endif
1555 ;
1556 word1(rv) = 0;
1557 break;
1558 }
1559 }
1560 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
1561 drop_down:
1562 /* boundary case -- decrement exponent */
1563#ifdef Sudden_Underflow
1564 L = word0(rv) & Exp_mask;
1565#ifdef IBM
1566 if (L < Exp_msk1)
1567#else
1568 if (L <= Exp_msk1)
1569#endif
1570 goto undfl;
1571 L -= Exp_msk1;
1572#else
1573 L = (word0(rv) & Exp_mask) - Exp_msk1;
1574#endif
1575 word0(rv) = L | Bndry_mask1;
1576 word1(rv) = 0xffffffff;
1577#ifdef IBM
1578 goto cont;
1579#else
1580 break;
1581#endif
1582 }
1583#ifndef ROUND_BIASED
1584 if (!(word1(rv) & LSB))
1585 break;
1586#endif
1587 if (dsign)
1588 value(rv) += ulp(value(rv));
1589#ifndef ROUND_BIASED
1590 else {
1591 value(rv) -= ulp(value(rv));
1592#ifndef Sudden_Underflow
1593 if (!value(rv))
1594 goto undfl;
1595#endif
1596 }
1597#endif
1598 break;
1599 }
1600 if ((aadj = ratio(delta, bs)) <= 2.) {
1601 if (dsign)
1602 aadj = aadj1 = 1.;
1603 else if (word1(rv) || word0(rv) & Bndry_mask) {
1604#ifndef Sudden_Underflow
1605 if (word1(rv) == Tiny1 && !word0(rv))
1606 goto undfl;
1607#endif
1608 aadj = 1.;
1609 aadj1 = -1.;
1610 }
1611 else {
1612 /* special case -- power of FLT_RADIX to be */
1613 /* rounded down... */
1614
1615 if (aadj < 2./FLT_RADIX)
1616 aadj = 1./FLT_RADIX;
1617 else
1618 aadj *= 0.5;
1619 aadj1 = -aadj;
1620 }
1621 }
1622 else {
1623 aadj *= 0.5;
1624 aadj1 = dsign ? aadj : -aadj;
1625#ifdef Check_FLT_ROUNDS
1626 switch(FLT_ROUNDS) {
1627 case 2: /* towards +infinity */
1628 aadj1 -= 0.5;
1629 break;
1630 case 0: /* towards 0 */
1631 case 3: /* towards -infinity */
1632 aadj1 += 0.5;
1633 }
1634#else
1635 if (FLT_ROUNDS == 0)
1636 aadj1 += 0.5;
1637#endif
1638 }
1639 y = word0(rv) & Exp_mask;
1640
1641 /* Check for overflow */
1642
1643 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1644 value(rv0) = value(rv);
1645 word0(rv) -= P*Exp_msk1;
1646 adj = aadj1 * ulp(value(rv));
1647 value(rv) += adj;
1648 if ((word0(rv) & Exp_mask) >=
1649 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1650 if (word0(rv0) == Big0 && word1(rv0) == Big1)
1651 goto ovfl;
1652 word0(rv) = Big0;
1653 word1(rv) = Big1;
1654 goto cont;
1655 }
1656 else
1657 word0(rv) += P*Exp_msk1;
1658 }
1659 else {
1660#ifdef Sudden_Underflow
1661 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
1662 value(rv0) = value(rv);
1663 word0(rv) += P*Exp_msk1;
1664 adj = aadj1 * ulp(value(rv));
1665 value(rv) += adj;
1666#ifdef IBM
1667 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
1668#else
1669 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
1670#endif
1671 {
1672 if (word0(rv0) == Tiny0
1673 && word1(rv0) == Tiny1)
1674 goto undfl;
1675 word0(rv) = Tiny0;
1676 word1(rv) = Tiny1;
1677 goto cont;
1678 }
1679 else
1680 word0(rv) -= P*Exp_msk1;
1681 }
1682 else {
1683 adj = aadj1 * ulp(value(rv));
1684 value(rv) += adj;
1685 }
1686#else
1687 /* Compute adj so that the IEEE rounding rules will
1688 * correctly round rv + adj in some half-way cases.
1689 * If rv * ulp(rv) is denormalized (i.e.,
1690 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
1691 * trouble from bits lost to denormalization;
1692 * example: 1.2e-307 .
1693 */
1694 if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
1695 aadj1 = (double)(int)(aadj + 0.5);
1696 if (!dsign)
1697 aadj1 = -aadj1;
1698 }
1699 adj = aadj1 * ulp(value(rv));
1700 value(rv) += adj;
1701#endif
1702 }
1703 z = word0(rv) & Exp_mask;
1704 if (y == z) {
1705 /* Can we stop now? */
1706 L = aadj;
1707 aadj -= L;
1708 /* The tolerances below are conservative. */
1709 if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
1710 if (aadj < .4999999 || aadj > .5000001)
1711 break;
1712 }
1713 else if (aadj < .4999999/FLT_RADIX)
1714 break;
1715 }
1716 cont:
1717 Bfree(bb);
1718 Bfree(bd);
1719 Bfree(bs);
1720 Bfree(delta);
1721 }
1722 retfree:
1723 Bfree(bb);
1724 Bfree(bd);
1725 Bfree(bs);
1726 Bfree(bd0);
1727 Bfree(delta);
1728 ret:
1729 if (se)
1730 *se = (char *)s;
1731 return sign ? -value(rv) : value(rv);
1732 }
1733
1734 static int
1735quorem(Bigint *b, Bigint *S)
1736{
1737 int n;
1738 Long borrow, y;
1739 ULong carry, q, ys;
1740 ULong *bx, *bxe, *sx, *sxe;
1741#ifdef Pack_32
1742 Long z;
1743 ULong si, zs;
1744#endif
1745
1746 n = S->wds;
1747#ifdef DEBUG
1748 /*debug*/ if (b->wds > n)
1749 /*debug*/ Bug("oversize b in quorem");
1750#endif
1751 if (b->wds < n)
1752 return 0;
1753 sx = S->x;
1754 sxe = sx + --n;
1755 bx = b->x;
1756 bxe = bx + n;
1757 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1758#ifdef DEBUG
1759 /*debug*/ if (q > 9)
1760 /*debug*/ Bug("oversized quotient in quorem");
1761#endif
1762 if (q) {
1763 borrow = 0;
1764 carry = 0;
1765 do {
1766#ifdef Pack_32
1767 si = *sx++;
1768 ys = (si & 0xffff) * q + carry;
1769 zs = (si >> 16) * q + (ys >> 16);
1770 carry = zs >> 16;
1771 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1772 borrow = y >> 16;
1773 Sign_Extend(borrow, y);
1774 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1775 borrow = z >> 16;
1776 Sign_Extend(borrow, z);
1777 Storeinc(bx, z, y);
1778#else
1779 ys = *sx++ * q + carry;
1780 carry = ys >> 16;
1781 y = *bx - (ys & 0xffff) + borrow;
1782 borrow = y >> 16;
1783 Sign_Extend(borrow, y);
1784 *bx++ = y & 0xffff;
1785#endif
1786 }
1787 while(sx <= sxe);
1788 if (!*bxe) {
1789 bx = b->x;
1790 while(--bxe > bx && !*bxe)
1791 --n;
1792 b->wds = n;
1793 }
1794 }
1795 if (cmp(b, S) >= 0) {
1796 q++;
1797 borrow = 0;
1798 carry = 0;
1799 bx = b->x;
1800 sx = S->x;
1801 do {
1802#ifdef Pack_32
1803 si = *sx++;
1804 ys = (si & 0xffff) + carry;
1805 zs = (si >> 16) + (ys >> 16);
1806 carry = zs >> 16;
1807 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
1808 borrow = y >> 16;
1809 Sign_Extend(borrow, y);
1810 z = (*bx >> 16) - (zs & 0xffff) + borrow;
1811 borrow = z >> 16;
1812 Sign_Extend(borrow, z);
1813 Storeinc(bx, z, y);
1814#else
1815 ys = *sx++ + carry;
1816 carry = ys >> 16;
1817 y = *bx - (ys & 0xffff) + borrow;
1818 borrow = y >> 16;
1819 Sign_Extend(borrow, y);
1820 *bx++ = y & 0xffff;
1821#endif
1822 }
1823 while(sx <= sxe);
1824 bx = b->x;
1825 bxe = bx + n;
1826 if (!*bxe) {
1827 while(--bxe > bx && !*bxe)
1828 --n;
1829 b->wds = n;
1830 }
1831 }
1832 return q;
1833 }
1834
1835/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1836 *
1837 * Inspired by "How to Print Floating-Point Numbers Accurately" by
1838 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
1839 *
1840 * Modifications:
1841 * 1. Rather than iterating, we use a simple numeric overestimate
1842 * to determine k = floor(log10(d)). We scale relevant
1843 * quantities using O(log2(k)) rather than O(k) multiplications.
1844 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1845 * try to generate digits strictly left to right. Instead, we
1846 * compute with fewer bits and propagate the carry if necessary
1847 * when rounding the final digit up. This is often faster.
1848 * 3. Under the assumption that input will be rounded nearest,
1849 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1850 * That is, we allow equality in stopping tests when the
1851 * round-nearest rule will give the same floating-point value
1852 * as would satisfaction of the stopping test with strict
1853 * inequality.
1854 * 4. We remove common factors of powers of 2 from relevant
1855 * quantities.
1856 * 5. When converting floating-point integers less than 1e16,
1857 * we use floating-point arithmetic rather than resorting
1858 * to multiple-precision integers.
1859 * 6. When asked to produce fewer than 15 digits, we first try
1860 * to get by with floating-point arithmetic; we resort to
1861 * multiple-precision integer arithmetic only if we cannot
1862 * guarantee that the floating-point calculation has given
1863 * the correctly rounded result. For k requested digits and
1864 * "uniformly" distributed input, the probability is
1865 * something like 10^(k-15) that we must resort to the Long
1866 * calculation.
1867 */
1868
1869 char *
1870__dtoa(double _d, int mode, int ndigits, int *decpt, int *sign, char **rve)
1871{
1872 /* Arguments ndigits, decpt, sign are similar to those
1873 of ecvt and fcvt; trailing zeros are suppressed from
1874 the returned string. If not null, *rve is set to point
1875 to the end of the return value. If d is +-Infinity or NaN,
1876 then *decpt is set to 9999.
1877
1878 mode:
1879 0 ==> shortest string that yields d when read in
1880 and rounded to nearest.
1881 1 ==> like 0, but with Steele & White stopping rule;
1882 e.g. with IEEE P754 arithmetic , mode 0 gives
1883 1e23 whereas mode 1 gives 9.999999999999999e22.
1884 2 ==> max(1,ndigits) significant digits. This gives a
1885 return value similar to that of ecvt, except
1886 that trailing zeros are suppressed.
1887 3 ==> through ndigits past the decimal point. This
1888 gives a return value similar to that from fcvt,
1889 except that trailing zeros are suppressed, and
1890 ndigits can be negative.
1891 4-9 should give the same return values as 2-3, i.e.,
1892 4 <= mode <= 9 ==> same return as mode
1893 2 + (mode & 1). These modes are mainly for
1894 debugging; often they run slower but sometimes
1895 faster than modes 2-3.
1896 4,5,8,9 ==> left-to-right digit generation.
1897 6-9 ==> don't try fast floating-point estimate
1898 (if applicable).
1899
1900 Values of mode other than 0-9 are treated as mode 0.
1901
1902 Sufficient space is allocated to the return value
1903 to hold the suppressed trailing zeros.
1904 */
1905
1906 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
1907 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
1908 spec_case, try_quick;
1909 Long L;
1910#ifndef Sudden_Underflow
1911 int denorm;
1912 ULong x;
1913#endif
1914 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
1915 double ds;
1916 char *s, *s0;
1917 _double d, d2, eps;
1918
1919 value(d) = _d;
1920
1921 if (word0(d) & Sign_bit) {
1922 /* set sign for everything, including 0's and NaNs */
1923 *sign = 1;
1924 word0(d) &= ~Sign_bit; /* clear sign bit */
1925 }
1926 else
1927 *sign = 0;
1928
1929#if defined(IEEE_Arith) + defined(VAX)
1930#ifdef IEEE_Arith
1931 if ((word0(d) & Exp_mask) == Exp_mask)
1932#else
1933 if (word0(d) == 0x8000)
1934#endif
1935 {
1936 /* Infinity or NaN */
1937 *decpt = 9999;
1938#ifdef IEEE_Arith
1939 if (!word1(d) && !(word0(d) & 0xfffff))
1940 return nrv_alloc("Infinity", rve, 8);
1941#endif
1942 return nrv_alloc("NaN", rve, 3);
1943 }
1944#endif
1945#ifdef IBM
1946 value(d) += 0; /* normalize */
1947#endif
1948 if (!value(d)) {
1949 *decpt = 1;
1950 return nrv_alloc("0", rve, 1);
1951 }
1952
1953 b = d2b(value(d), &be, &bbits);
1954#ifdef Sudden_Underflow
1955 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
1956#else
1957 if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) {
1958#endif
1959 value(d2) = value(d);
1960 word0(d2) &= Frac_mask1;
1961 word0(d2) |= Exp_11;
1962#ifdef IBM
1963 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
1964 value(d2) /= 1 << j;
1965#endif
1966
1967 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
1968 * log10(x) = log(x) / log(10)
1969 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
1970 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
1971 *
1972 * This suggests computing an approximation k to log10(d) by
1973 *
1974 * k = (i - Bias)*0.301029995663981
1975 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
1976 *
1977 * We want k to be too large rather than too small.
1978 * The error in the first-order Taylor series approximation
1979 * is in our favor, so we just round up the constant enough
1980 * to compensate for any error in the multiplication of
1981 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
1982 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
1983 * adding 1e-13 to the constant term more than suffices.
1984 * Hence we adjust the constant term to 0.1760912590558.
1985 * (We could get a more accurate k by invoking log10,
1986 * but this is probably not worthwhile.)
1987 */
1988
1989 i -= Bias;
1990#ifdef IBM
1991 i <<= 2;
1992 i += j;
1993#endif
1994#ifndef Sudden_Underflow
1995 denorm = 0;
1996 }
1997 else {
1998 /* d is denormalized */
1999
2000 i = bbits + be + (Bias + (P-1) - 1);
2001 x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32
2002 : word1(d) << 32 - i;
2003 value(d2) = x;
2004 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2005 i -= (Bias + (P-1) - 1) + 1;
2006 denorm = 1;
2007 }
2008#endif
2009 ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2010 i*0.301029995663981;
2011 k = (int)ds;
2012 if (ds < 0. && ds != k)
2013 k--; /* want k = floor(ds) */
2014 k_check = 1;
2015 if (k >= 0 && k <= Ten_pmax) {
2016 if (value(d) < tens[k])
2017 k--;
2018 k_check = 0;
2019 }
2020 j = bbits - i - 1;
2021 if (j >= 0) {
2022 b2 = 0;
2023 s2 = j;
2024 }
2025 else {
2026 b2 = -j;
2027 s2 = 0;
2028 }
2029 if (k >= 0) {
2030 b5 = 0;
2031 s5 = k;
2032 s2 += k;
2033 }
2034 else {
2035 b2 -= k;
2036 b5 = -k;
2037 s5 = 0;
2038 }
2039 if (mode < 0 || mode > 9)
2040 mode = 0;
2041 try_quick = 1;
2042 if (mode > 5) {
2043 mode -= 4;
2044 try_quick = 0;
2045 }
2046 leftright = 1;
2047 switch(mode) {
2048 case 0:
2049 case 1:
2050 ilim = ilim1 = -1;
2051 i = 18;
2052 ndigits = 0;
2053 break;
2054 case 2:
2055 leftright = 0;
2056 /* no break */
2057 case 4:
2058 if (ndigits <= 0)
2059 ndigits = 1;
2060 ilim = ilim1 = i = ndigits;
2061 break;
2062 case 3:
2063 leftright = 0;
2064 /* no break */
2065 case 5:
2066 i = ndigits + k + 1;
2067 ilim = i;
2068 ilim1 = i - 1;
2069 if (i <= 0)
2070 i = 1;
2071 }
2072 s = s0 = rv_alloc(i);
2073
2074 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2075
2076 /* Try to get by with floating-point arithmetic. */
2077
2078 i = 0;
2079 value(d2) = value(d);
2080 k0 = k;
2081 ilim0 = ilim;
2082 ieps = 2; /* conservative */
2083 if (k > 0) {
2084 ds = tens[k&0xf];
2085 j = k >> 4;
2086 if (j & Bletch) {
2087 /* prevent overflows */
2088 j &= Bletch - 1;
2089 value(d) /= bigtens[n_bigtens-1];
2090 ieps++;
2091 }
2092 for(; j; j >>= 1, i++)
2093 if (j & 1) {
2094 ieps++;
2095 ds *= bigtens[i];
2096 }
2097 value(d) /= ds;
2098 }
2099 else if (j1 = -k) {
2100 value(d) *= tens[j1 & 0xf];
2101 for(j = j1 >> 4; j; j >>= 1, i++)
2102 if (j & 1) {
2103 ieps++;
2104 value(d) *= bigtens[i];
2105 }
2106 }
2107 if (k_check && value(d) < 1. && ilim > 0) {
2108 if (ilim1 <= 0)
2109 goto fast_failed;
2110 ilim = ilim1;
2111 k--;
2112 value(d) *= 10.;
2113 ieps++;
2114 }
2115 value(eps) = ieps*value(d) + 7.;
2116 word0(eps) -= (P-1)*Exp_msk1;
2117 if (ilim == 0) {
2118 S = mhi = 0;
2119 value(d) -= 5.;
2120 if (value(d) > value(eps))
2121 goto one_digit;
2122 if (value(d) < -value(eps))
2123 goto no_digits;
2124 goto fast_failed;
2125 }
2126#ifndef No_leftright
2127 if (leftright) {
2128 /* Use Steele & White method of only
2129 * generating digits needed.
2130 */
2131 value(eps) = 0.5/tens[ilim-1] - value(eps);
2132 for(i = 0;;) {
2133 L = value(d);
2134 value(d) -= L;
2135 *s++ = '0' + (int)L;
2136 if (value(d) < value(eps))
2137 goto ret1;
2138 if (1. - value(d) < value(eps))
2139 goto bump_up;
2140 if (++i >= ilim)
2141 break;
2142 value(eps) *= 10.;
2143 value(d) *= 10.;
2144 }
2145 }
2146 else {
2147#endif
2148 /* Generate ilim digits, then fix them up. */
2149 value(eps) *= tens[ilim-1];
2150 for(i = 1;; i++, value(d) *= 10.) {
2151 L = value(d);
2152 value(d) -= L;
2153 *s++ = '0' + (int)L;
2154 if (i == ilim) {
2155 if (value(d) > 0.5 + value(eps))
2156 goto bump_up;
2157 else if (value(d) < 0.5 - value(eps)) {
2158 while(*--s == '0');
2159 s++;
2160 goto ret1;
2161 }
2162 break;
2163 }
2164 }
2165#ifndef No_leftright
2166 }
2167#endif
2168 fast_failed:
2169 s = s0;
2170 value(d) = value(d2);
2171 k = k0;
2172 ilim = ilim0;
2173 }
2174
2175 /* Do we have a "small" integer? */
2176
2177 if (be >= 0 && k <= Int_max) {
2178 /* Yes. */
2179 ds = tens[k];
2180 if (ndigits < 0 && ilim <= 0) {
2181 S = mhi = 0;
2182 if (ilim < 0 || value(d) <= 5*ds)
2183 goto no_digits;
2184 goto one_digit;
2185 }
2186 for(i = 1;; i++) {
2187 L = value(d) / ds;
2188 value(d) -= L*ds;
2189#ifdef Check_FLT_ROUNDS
2190 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2191 if (value(d) < 0) {
2192 L--;
2193 value(d) += ds;
2194 }
2195#endif
2196 *s++ = '0' + (int)L;
2197 if (i == ilim) {
2198 value(d) += value(d);
2199 if (value(d) > ds || value(d) == ds && L & 1) {
2200 bump_up:
2201 while(*--s == '9')
2202 if (s == s0) {
2203 k++;
2204 *s = '0';
2205 break;
2206 }
2207 ++*s++;
2208 }
2209 break;
2210 }
2211 if (!(value(d) *= 10.))
2212 break;
2213 }
2214 goto ret1;
2215 }
2216
2217 m2 = b2;
2218 m5 = b5;
2219 mhi = mlo = 0;
2220 if (leftright) {
2221 if (mode < 2) {
2222 i =
2223#ifndef Sudden_Underflow
2224 denorm ? be + (Bias + (P-1) - 1 + 1) :
2225#endif
2226#ifdef IBM
2227 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
2228#else
2229 1 + P - bbits;
2230#endif
2231 }
2232 else {
2233 j = ilim - 1;
2234 if (m5 >= j)
2235 m5 -= j;
2236 else {
2237 s5 += j -= m5;
2238 b5 += j;
2239 m5 = 0;
2240 }
2241 if ((i = ilim) < 0) {
2242 m2 -= i;
2243 i = 0;
2244 }
2245 }
2246 b2 += i;
2247 s2 += i;
2248 mhi = i2b(1);
2249 }
2250 if (m2 > 0 && s2 > 0) {
2251 i = m2 < s2 ? m2 : s2;
2252 b2 -= i;
2253 m2 -= i;
2254 s2 -= i;
2255 }
2256 if (b5 > 0) {
2257 if (leftright) {
2258 if (m5 > 0) {
2259 mhi = pow5mult(mhi, m5);
2260 b1 = mult(mhi, b);
2261 Bfree(b);
2262 b = b1;
2263 }
2264 if (j = b5 - m5)
2265 b = pow5mult(b, j);
2266 }
2267 else
2268 b = pow5mult(b, b5);
2269 }
2270 S = i2b(1);
2271 if (s5 > 0)
2272 S = pow5mult(S, s5);
2273
2274 /* Check for special case that d is a normalized power of 2. */
2275
2276 if (mode < 2) {
2277 if (!word1(d) && !(word0(d) & Bndry_mask)
2278#ifndef Sudden_Underflow
2279 && word0(d) & Exp_mask
2280#endif
2281 ) {
2282 /* The special case */
2283 b2 += Log2P;
2284 s2 += Log2P;
2285 spec_case = 1;
2286 }
2287 else
2288 spec_case = 0;
2289 }
2290
2291 /* Arrange for convenient computation of quotients:
2292 * shift left if necessary so divisor has 4 leading 0 bits.
2293 *
2294 * Perhaps we should just compute leading 28 bits of S once
2295 * and for all and pass them and a shift to quorem, so it
2296 * can do shifts and ors to compute the numerator for q.
2297 */
2298#ifdef Pack_32
2299 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f)
2300 i = 32 - i;
2301#else
2302 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
2303 i = 16 - i;
2304#endif
2305 if (i > 4) {
2306 i -= 4;
2307 b2 += i;
2308 m2 += i;
2309 s2 += i;
2310 }
2311 else if (i < 4) {
2312 i += 28;
2313 b2 += i;
2314 m2 += i;
2315 s2 += i;
2316 }
2317 if (b2 > 0)
2318 b = lshift(b, b2);
2319 if (s2 > 0)
2320 S = lshift(S, s2);
2321 if (k_check) {
2322 if (cmp(b,S) < 0) {
2323 k--;
2324 b = multadd(b, 10, 0); /* we botched the k estimate */
2325 if (leftright)
2326 mhi = multadd(mhi, 10, 0);
2327 ilim = ilim1;
2328 }
2329 }
2330 if (ilim <= 0 && mode > 2) {
2331 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
2332 /* no digits, fcvt style */
2333 no_digits:
2334 k = -1 - ndigits;
2335 goto ret;
2336 }
2337 one_digit:
2338 *s++ = '1';
2339 k++;
2340 goto ret;
2341 }
2342 if (leftright) {
2343 if (m2 > 0)
2344 mhi = lshift(mhi, m2);
2345
2346 /* Compute mlo -- check for special case
2347 * that d is a normalized power of 2.
2348 */
2349
2350 mlo = mhi;
2351 if (spec_case) {
2352 mhi = Balloc(mhi->k);
2353 Bcopy(mhi, mlo);
2354 mhi = lshift(mhi, Log2P);
2355 }
2356
2357 for(i = 1;;i++) {
2358 dig = quorem(b,S) + '0';
2359 /* Do we yet have the shortest decimal string
2360 * that will round to d?
2361 */
2362 j = cmp(b, mlo);
2363 delta = diff(S, mhi);
2364 j1 = delta->sign ? 1 : cmp(b, delta);
2365 Bfree(delta);
2366#ifndef ROUND_BIASED
2367 if (j1 == 0 && !mode && !(word1(d) & 1)) {
2368 if (dig == '9')
2369 goto round_9_up;
2370 if (j > 0)
2371 dig++;
2372 *s++ = dig;
2373 goto ret;
2374 }
2375#endif
2376 if (j < 0 || j == 0 && !mode
2377#ifndef ROUND_BIASED
2378 && !(word1(d) & 1)
2379#endif
2380 ) {
2381 if (j1 > 0) {
2382 b = lshift(b, 1);
2383 j1 = cmp(b, S);
2384 if ((j1 > 0 || j1 == 0 && dig & 1)
2385 && dig++ == '9')
2386 goto round_9_up;
2387 }
2388 *s++ = dig;
2389 goto ret;
2390 }
2391 if (j1 > 0) {
2392 if (dig == '9') { /* possible if i == 1 */
2393 round_9_up:
2394 *s++ = '9';
2395 goto roundoff;
2396 }
2397 *s++ = dig + 1;
2398 goto ret;
2399 }
2400 *s++ = dig;
2401 if (i == ilim)
2402 break;
2403 b = multadd(b, 10, 0);
2404 if (mlo == mhi)
2405 mlo = mhi = multadd(mhi, 10, 0);
2406 else {
2407 mlo = multadd(mlo, 10, 0);
2408 mhi = multadd(mhi, 10, 0);
2409 }
2410 }
2411 }
2412 else
2413 for(i = 1;; i++) {
2414 *s++ = dig = quorem(b,S) + '0';
2415 if (i >= ilim)
2416 break;
2417 b = multadd(b, 10, 0);
2418 }
2419
2420 /* Round off last digit */
2421
2422 b = lshift(b, 1);
2423 j = cmp(b, S);
2424 if (j > 0 || j == 0 && dig & 1) {
2425 roundoff:
2426 while(*--s == '9')
2427 if (s == s0) {
2428 k++;
2429 *s++ = '1';
2430 goto ret;
2431 }
2432 ++*s++;
2433 }
2434 else {
2435 while(*--s == '0');
2436 s++;
2437 }
2438 ret:
2439 Bfree(S);
2440 if (mhi) {
2441 if (mlo && mlo != mhi)
2442 Bfree(mlo);
2443 Bfree(mhi);
2444 }
2445 ret1:
2446 Bfree(b);
2447 if (s == s0) { /* don't return empty string */
2448 *s++ = '0';
2449 k = 0;
2450 }
2451 *s = 0;
2452 *decpt = k + 1;
2453 if (rve)
2454 *rve = s;
2455 return s0;
2456 }
2457#ifdef __cplusplus
2458}
2459#endif
diff --git a/src/lib/libc/stdlib/strtof.c b/src/lib/libc/stdlib/strtof.c
deleted file mode 100644
index 8c8db47ad8..0000000000
--- a/src/lib/libc/stdlib/strtof.c
+++ /dev/null
@@ -1,39 +0,0 @@
1/* $OpenBSD: strtof.c,v 1.1 2008/06/13 21:04:24 landry Exp $ */
2
3/*
4 * Copyright (c) 2008 Landry Breuil
5 * All rights reserved.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <errno.h>
21#include <limits.h>
22#include <stdlib.h>
23#include <math.h>
24
25float
26strtof(const char *s00, char **se)
27{
28 double d;
29
30 d = strtod(s00, se);
31 if (d > FLT_MAX) {
32 errno = ERANGE;
33 return (FLT_MAX);
34 } else if (d < -FLT_MAX) {
35 errno = ERANGE;
36 return (-FLT_MAX);
37 }
38 return ((float) d);
39}
diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile
index 689f87aa03..dc9beab1e1 100644
--- a/src/regress/lib/libc/Makefile
+++ b/src/regress/lib/libc/Makefile
@@ -1,8 +1,8 @@
1# $OpenBSD: Makefile,v 1.26 2007/09/03 14:42:43 millert Exp $ 1# $OpenBSD: Makefile,v 1.27 2008/09/07 20:36:10 martynas Exp $
2 2
3SUBDIR+= _setjmp alloca atexit basename cxa-atexit db dirname getaddrinfo 3SUBDIR+= _setjmp alloca atexit basename cxa-atexit db dirname fpclassify
4SUBDIR+= getcap getopt_long hsearch longjmp locale malloc 4SUBDIR+= getaddrinfo getcap getopt_long hsearch longjmp locale malloc
5SUBDIR+= netdb popen regex setjmp setjmp-signal sigreturn sigsetjmp 5SUBDIR+= netdb popen printf regex setjmp setjmp-signal sigreturn sigsetjmp
6SUBDIR+= sprintf strerror strtod strtonum telldir time vis 6SUBDIR+= sprintf strerror strtod strtonum telldir time vis
7 7
8.if (${MACHINE_ARCH} != "vax") 8.if (${MACHINE_ARCH} != "vax")
diff --git a/src/regress/lib/libc/fpclassify/Makefile b/src/regress/lib/libc/fpclassify/Makefile
new file mode 100644
index 0000000000..96916d20e7
--- /dev/null
+++ b/src/regress/lib/libc/fpclassify/Makefile
@@ -0,0 +1,5 @@
1# $OpenBSD: Makefile,v 1.1 2008/09/07 20:36:10 martynas Exp $
2
3PROG= fpclassify
4
5.include <bsd.regress.mk>
diff --git a/src/regress/lib/libc/fpclassify/fpclassify.c b/src/regress/lib/libc/fpclassify/fpclassify.c
new file mode 100644
index 0000000000..174c04d983
--- /dev/null
+++ b/src/regress/lib/libc/fpclassify/fpclassify.c
@@ -0,0 +1,76 @@
1/* $OpenBSD: fpclassify.c,v 1.1 2008/09/07 20:36:10 martynas Exp $ */
2/*-
3 * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: src/tools/regression/lib/libc/gen/test-fpclassify.c,v 1.3 2003/03/27 05:32:28 das Exp $
28 */
29
30#include <assert.h>
31#include <float.h>
32#include <math.h>
33#include <stdlib.h>
34
35int
36main(void)
37{
38
39 assert(fpclassify((float)0) == FP_ZERO);
40 assert(fpclassify((float)-0.0) == FP_ZERO);
41 assert(fpclassify((float)1) == FP_NORMAL);
42 assert(fpclassify((float)1000) == FP_NORMAL);
43#ifndef __alpha__
44 assert(fpclassify(0x1.2p-150f) == FP_SUBNORMAL);
45#endif /* !__alpha__ */
46 assert(fpclassify(HUGE_VALF) == FP_INFINITE);
47 assert(fpclassify((float)HUGE_VAL) == FP_INFINITE);
48 assert(fpclassify((float)HUGE_VALL) == FP_INFINITE);
49 assert(fpclassify(NAN) == FP_NAN);
50
51 assert(fpclassify((double)0) == FP_ZERO);
52 assert(fpclassify((double)-0) == FP_ZERO);
53 assert(fpclassify((double)1) == FP_NORMAL);
54 assert(fpclassify((double)1000) == FP_NORMAL);
55#ifndef __alpha__
56 assert(fpclassify(0x1.2p-1075) == FP_SUBNORMAL);
57#endif /* !__alpha__ */
58 assert(fpclassify(HUGE_VAL) == FP_INFINITE);
59 assert(fpclassify((double)HUGE_VALF) == FP_INFINITE);
60 assert(fpclassify((double)HUGE_VALL) == FP_INFINITE);
61 assert(fpclassify((double)NAN) == FP_NAN);
62
63 assert(fpclassify((long double)0) == FP_ZERO);
64 assert(fpclassify((long double)-0.0) == FP_ZERO);
65 assert(fpclassify((long double)1) == FP_NORMAL);
66 assert(fpclassify((long double)1000) == FP_NORMAL);
67#if (LDBL_MANT_DIG > DBL_MANT_DIG)
68 assert(fpclassify(0x1.2p-16383L) == FP_SUBNORMAL);
69#endif /* (LDBL_MANT_DIG > DBL_MANT_DIG) */
70 assert(fpclassify(HUGE_VALL) == FP_INFINITE);
71 assert(fpclassify((long double)HUGE_VALF) == FP_INFINITE);
72 assert(fpclassify((long double)HUGE_VAL) == FP_INFINITE);
73 assert(fpclassify((long double)NAN) == FP_NAN);
74
75 return (0);
76}
diff --git a/src/regress/lib/libc/printf/Makefile b/src/regress/lib/libc/printf/Makefile
new file mode 100644
index 0000000000..c2e2732d6e
--- /dev/null
+++ b/src/regress/lib/libc/printf/Makefile
@@ -0,0 +1,5 @@
1# $OpenBSD: Makefile,v 1.1 2008/09/07 20:36:10 martynas Exp $
2
3PROG= fp
4
5.include <bsd.regress.mk>
diff --git a/src/regress/lib/libc/printf/fp.c b/src/regress/lib/libc/printf/fp.c
new file mode 100644
index 0000000000..6ed52fdb49
--- /dev/null
+++ b/src/regress/lib/libc/printf/fp.c
@@ -0,0 +1,217 @@
1/* $OpenBSD: fp.c,v 1.1 2008/09/07 20:36:10 martynas Exp $ */
2/*-
3 * Copyright (c) 2002, 2005 David Schultz <das@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28/*
29 * Test for printf() floating point formats.
30 */
31
32#include <assert.h>
33#include <err.h>
34#include <float.h>
35#include <math.h>
36#include <stdio.h>
37#include <stdarg.h>
38#include <stdint.h>
39#include <stdlib.h>
40#include <string.h>
41
42#define testfmt(result, fmt, ...) \
43 _testfmt((result), __LINE__, #__VA_ARGS__, fmt, __VA_ARGS__)
44void _testfmt(const char *, int, const char *, const char *, ...);
45void smash_stack(void);
46
47int
48main(int argc, char *argv[])
49{
50 /*
51 * Basic tests of decimal output functionality.
52 */
53 testfmt(" 1.000000E+00", "%13E", 1.0);
54 testfmt(" 1.000000", "%13f", 1.0);
55 testfmt(" 1", "%13G", 1.0);
56 testfmt(" 1.000000E+00", "%13LE", 1.0L);
57 testfmt(" 1.000000", "%13Lf", 1.0L);
58 testfmt(" 1", "%13LG", 1.0L);
59
60 testfmt("2.718282", "%.*f", -2, 2.7182818);
61
62 testfmt("1.234568e+06", "%e", 1234567.8);
63 testfmt("1234567.800000", "%f", 1234567.8);
64 testfmt("1.23457E+06", "%G", 1234567.8);
65 testfmt("1.234568e+06", "%Le", 1234567.8L);
66 testfmt("1234567.800000", "%Lf", 1234567.8L);
67 testfmt("1.23457E+06", "%LG", 1234567.8L);
68
69#if (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__)
70 testfmt("123456789.864210", "%Lf", 123456789.8642097531L);
71 testfmt("-1.23457E+08", "%LG", -123456789.8642097531L);
72 testfmt("123456789.8642097531", "%.10Lf", 123456789.8642097531L);
73 testfmt(" 3.141592653589793238e-4000", "%L27.18Le",
74 3.14159265358979323846e-4000L);
75#endif /* (LDBL_MANT_DIG > DBL_MANT_DIG) && !defined(__i386__) */
76
77 /*
78 * Infinities and NaNs
79 */
80#ifdef NAN
81 testfmt("nan", "%e", NAN);
82 testfmt("NAN", "%F", NAN);
83 testfmt("nan", "%g", NAN);
84 testfmt("NAN", "%LE", (long double)NAN);
85 testfmt(" nan", "%05e", NAN);
86#endif /* NAN */
87
88 testfmt("INF", "%E", HUGE_VAL);
89 testfmt("-inf", "%f", -HUGE_VAL);
90 testfmt("+inf", "%+g", HUGE_VAL);
91 testfmt(" inf", "%4.2Le", HUGE_VALL);
92 testfmt("-inf", "%Lf", -HUGE_VALL);
93 testfmt(" inf", "%05e", HUGE_VAL);
94 testfmt(" -inf", "%05e", -HUGE_VAL);
95
96 /*
97 * Padding
98 */
99 testfmt("0.000000e+00", "%e", 0.0);
100 testfmt("0.000000", "%F", (double)0.0);
101 testfmt("0", "%G", 0.0);
102 testfmt(" 0", "%3.0Lg", 0.0L);
103 testfmt(" 0", "%5.0f", 0.001);
104
105 /*
106 * Precision specifiers
107 */
108 testfmt("1.0123e+00", "%.4e", 1.0123456789);
109 testfmt("1.0123", "%.4f", 1.0123456789);
110 testfmt("1.012", "%.4g", 1.0123456789);
111 testfmt("1.2346e-02", "%.4e", 0.0123456789);
112 testfmt("0.0123", "%.4f", 0.0123456789);
113 testfmt("0.01235", "%.4g", 0.0123456789);
114
115 /*
116 * Signed conversions
117 */
118 testfmt("+2.500000e-01", "%+e", 0.25);
119 testfmt("+0.000000", "%+F", 0.0);
120 testfmt("-1", "%+g", -1.0);
121
122 testfmt("-1.000000e+00", "% e", -1.0);
123 testfmt("+1.000000", "% +f", 1.0);
124 testfmt(" 1", "% g", 1.0);
125 testfmt(" 0", "% g", 0.0);
126
127 /*
128 * ``Alternate form''
129 */
130 testfmt("1.250e+00", "%#.3e", 1.25);
131 testfmt("123.000000", "%#f", 123.0);
132 testfmt(" 12345.", "%#7.5g", 12345.0);
133 testfmt(" 1.00000", "%#8g", 1.0);
134 testfmt("0.0", "%#.2g", 0.0);
135
136 /*
137 * Padding and decimal point placement
138 */
139 testfmt("03.2E+00", "%08.1E", 3.25);
140 testfmt("003.25", "%06.2F", 3.25);
141 testfmt("0003.25", "%07.4G", 3.25);
142
143 testfmt("3.14159e-05", "%g", 3.14159e-5);
144 testfmt("0.000314159", "%g", 3.14159e-4);
145 testfmt("3.14159e+06", "%g", 3.14159e6);
146 testfmt("314159", "%g", 3.14159e5);
147 testfmt("314159.", "%#g", 3.14159e5);
148
149 testfmt(" 9.000000e+03", "%13e", 9000.0);
150 testfmt(" 9000.000000", "%12f", 9000.0);
151 testfmt(" 9000", "%5g", 9000.0);
152 testfmt(" 900000.", "%#8g", 900000.0);
153 testfmt(" 9e+06", "%6g", 9000000.0);
154 testfmt(" 9.000000e-04", "%13e", 0.0009);
155 testfmt(" 0.000900", "%9f", 0.0009);
156 testfmt(" 0.0009", "%7g", 0.0009);
157 testfmt(" 9e-05", "%6g", 0.00009);
158 testfmt(" 9.00000e-05", "%#12g", 0.00009);
159 testfmt(" 9.e-05", "%#7.1g", 0.00009);
160
161 testfmt(" 0.0", "%4.1f", 0.0);
162 testfmt("90.0", "%4.1f", 90.0);
163 testfmt(" 100", "%4.0f", 100.0);
164 testfmt("9.0e+01", "%4.1e", 90.0);
165 testfmt("1e+02", "%4.0e", 100.0);
166
167 /*
168 * Hexadecimal floating point (%a, %A) tests. Some of these
169 * are only valid if the implementation converts to hex digits
170 * on nibble boundaries.
171 */
172 testfmt("0x0p+0", "%a", 0x0.0p0);
173 testfmt("0X0.P+0", "%#LA", 0x0.0p0L);
174#ifdef NAN
175 testfmt("inf", "%La", (long double)INFINITY);
176 testfmt("+INF", "%+A", INFINITY);
177 testfmt("nan", "%La", (long double)NAN);
178 testfmt("NAN", "%A", NAN);
179#endif /* NAN */
180
181 testfmt(" 0x1.23p+0", "%10a", 0x1.23p0);
182 testfmt(" 0x1.23p-500", "%12a", 0x1.23p-500);
183 testfmt(" 0x1.2p+40", "%10.1a", 0x1.23p40);
184 testfmt(" 0X1.230000000000000000000000P-4", "%32.24A", 0x1.23p-4);
185 testfmt("0x1p-1074", "%a", 0x1p-1074);
186 testfmt("0x1.2345p-1024", "%a", 0x1.2345p-1024);
187
188 return (0);
189}
190
191void
192smash_stack(void)
193{
194 static uint32_t junk = 0xdeadbeef;
195 uint32_t buf[512];
196 int i;
197
198 for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)
199 buf[i] = junk;
200}
201
202void
203_testfmt(const char *result, int line, const char *argstr, const char *fmt,...)
204{
205 char s[100];
206 va_list ap;
207
208 va_start(ap, fmt);
209 smash_stack();
210 vsnprintf(s, sizeof(s), fmt, ap);
211 if (strcmp(result, s) != 0) {
212 fprintf(stderr,
213 "%d: printf(\"%s\", %s) ==> [%s], expected [%s]\n",
214 line, fmt, argstr, s, result);
215 abort();
216 }
217}