Contiki 2.5
small_strtod.c
1 /*
2 FUNCTON
3  <<strtod>>, <<strtof>>---string to double or float
4 
5 INDEX
6  strtod
7 INDEX
8  _strtod_r
9 INDEX
10  strtof
11 
12 ANSI_SYNOPSIS
13  #include <stdlib.h>
14  double strtod(const char *<[str]>, char **<[tail]>);
15  float strtof(const char *<[str]>, char **<[tail]>);
16 
17  double _strtod_r(void *<[reent]>,
18  const char *<[str]>, char **<[tail]>);
19 
20 TRAD_SYNOPSIS
21  #include <stdlib.h>
22  double strtod(<[str]>,<[tail]>)
23  char *<[str]>;
24  char **<[tail]>;
25 
26  float strtof(<[str]>,<[tail]>)
27  char *<[str]>;
28  char **<[tail]>;
29 
30  double _strtod_r(<[reent]>,<[str]>,<[tail]>)
31  char *<[reent]>;
32  char *<[str]>;
33  char **<[tail]>;
34 
35 DESCRIPTION
36  The function <<strtod>> parses the character string <[str]>,
37  producing a substring which can be converted to a double
38  value. The substring converted is the longest initial
39  subsequence of <[str]>, beginning with the first
40  non-whitespace character, that has the format:
41  .[+|-]<[digits]>[.][<[digits]>][(e|E)[+|-]<[digits]>]
42  The substring contains no characters if <[str]> is empty, consists
43  entirely of whitespace, or if the first non-whitespace
44  character is something other than <<+>>, <<->>, <<.>>, or a
45  digit. If the substring is empty, no conversion is done, and
46  the value of <[str]> is stored in <<*<[tail]>>>. Otherwise,
47  the substring is converted, and a pointer to the final string
48  (which will contain at least the terminating null character of
49  <[str]>) is stored in <<*<[tail]>>>. If you want no
50  assignment to <<*<[tail]>>>, pass a null pointer as <[tail]>.
51  <<strtof>> is identical to <<strtod>> except for its return type.
52 
53  This implementation returns the nearest machine number to the
54  input decimal string. Ties are broken by using the IEEE
55  round-even rule.
56 
57  The alternate function <<_strtod_r>> is a reentrant version.
58  The extra argument <[reent]> is a pointer to a reentrancy structure.
59 
60 RETURNS
61  <<strtod>> returns the converted substring value, if any. If
62  no conversion could be performed, 0 is returned. If the
63  correct value is out of the range of representable values,
64  plus or minus <<HUGE_VAL>> is returned, and <<ERANGE>> is
65  stored in errno. If the correct value would cause underflow, 0
66  is returned and <<ERANGE>> is stored in errno.
67 
68 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
69 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
70 */
71 
72 /****************************************************************
73  *
74  * The author of this software is David M. Gay.
75  *
76  * Copyright (c) 1991 by AT&T.
77  *
78  * Permission to use, copy, modify, and distribute this software for any
79  * purpose without fee is hereby granted, provided that this entire notice
80  * is included in all copies of any software which is or includes a copy
81  * or modification of this software and in all copies of the supporting
82  * documentation for such software.
83  *
84  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
85  * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
86  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
87  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
88  *
89  ***************************************************************/
90 
91 /* Please send bug reports to
92  David M. Gay
93  AT&T Bell Laboratories, Room 2C-463
94  600 Mountain Avenue
95  Murray Hill, NJ 07974-2070
96  U.S.A.
97  dmg@research.att.com or research!dmg
98  */
99 
100 
101 /* Scanf and printf call both the small_mprec.c file if small_scanf
102  * has not been specfied optimizations concerning small_mprec.c and
103  * call of balloc will be performed anyway for scanf.
104  */
105 
106 #ifdef _SMALL_PRINTF
107 #ifndef SMALL_SCANF
108 #define SMALL_SCANF
109 #endif
110 #endif
111 
112 
113 #include <_ansi.h>
114 #include <reent.h>
115 #include <string.h>
116 #include "small_mprec.h"
117 
118 double
119 _DEFUN (_strtod_r, (ptr, s00, se),
120  struct _reent *ptr _AND
121  _CONST char *s00 _AND
122  char **se)
123 {
124  int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, e1, esign, i, j,
125  k, nd, nd0, nf, nz, nz0, sign;
126  long e;
127  _CONST char *s, *s0, *s1;
128  double aadj, aadj1, adj;
129  long L;
130  unsigned long z;
131  __ULong y;
132  union double_union rv, rv0;
133 
134  _Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
135 
136  #ifdef SMALL_SCANF
137 
138  /*
139  * For the SMALL_SCANF implementation for floating points numbers :
140  * - To avoid the call of allocator we defined a buffer for each variable : instead of taking the adress
141  * provided by Balloc variables are initialized to the beginning of the array.
142  * - For some variables many buffers have been declared, in fact for each call of small_lshift we used a
143  * buffer that has not been used at the moment
144  * - This buffers are used in the call of function declared in small_mprec.h
145  * To have more informations look at small_mprec.c
146  */
147 
148 
149 
150  #define BUF_SIZE 32
151  #define BUF_LSHIFT_SIZE 40
152 
153  _Bigint tab_bb[BUF_LSHIFT_SIZE],tab_bb1[BUF_SIZE],tab_bd[BUF_SIZE],tab_bd0[BUF_SIZE],tab_bs[BUF_LSHIFT_SIZE], tab_delta[BUF_LSHIFT_SIZE];
154  _Bigint tab_bblshift[BUF_LSHIFT_SIZE],tab_bslshift[BUF_LSHIFT_SIZE], tab_deltalshift[BUF_LSHIFT_SIZE],tab_bdlshift[BUF_LSHIFT_SIZE];
155  #endif
156 
157  sign = nz0 = nz = 0;
158  rv.d = 0.;
159  for (s = s00;; s++)
160  switch (*s)
161  {
162  case '-':
163  sign = 1;
164  /* no break */
165  case '+':
166  if (*++s)
167  goto break2;
168  /* no break */
169  case 0:
170  s = s00;
171  goto ret;
172  case '\t':
173  case '\n':
174  case '\v':
175  case '\f':
176  case '\r':
177  case ' ':
178  continue;
179  default:
180  goto break2;
181  }
182 break2:
183  if (*s == '0')
184  {
185  nz0 = 1;
186  while (*++s == '0');
187  if (!*s)
188  goto ret;
189  }
190  s0 = s;
191  y = z = 0;
192  for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
193  if (nd < 9)
194  y = 10 * y + c - '0';
195  else if (nd < 16)
196  z = 10 * z + c - '0';
197  nd0 = nd;
198  if (c == '.')
199  {
200  c = *++s;
201  if (!nd)
202  {
203  for (; c == '0'; c = *++s)
204  nz++;
205  if (c > '0' && c <= '9')
206  {
207  s0 = s;
208  nf += nz;
209  nz = 0;
210  goto have_dig;
211  }
212  goto dig_done;
213  }
214  for (; c >= '0' && c <= '9'; c = *++s)
215  {
216  have_dig:
217  nz++;
218  if (c -= '0')
219  {
220  nf += nz;
221  for (i = 1; i < nz; i++)
222  if (nd++ < 9)
223  y *= 10;
224  else if (nd <= DBL_DIG + 1)
225  z *= 10;
226  if (nd++ < 9)
227  y = 10 * y + c;
228  else if (nd <= DBL_DIG + 1)
229  z = 10 * z + c;
230  nz = 0;
231  }
232  }
233  }
234 dig_done:
235  e = 0;
236  if (c == 'e' || c == 'E')
237  {
238  if (!nd && !nz && !nz0)
239  {
240  s = s00;
241  goto ret;
242  }
243  s00 = s;
244  esign = 0;
245  switch (c = *++s)
246  {
247  case '-':
248  esign = 1;
249  case '+':
250  c = *++s;
251  }
252  if (c >= '0' && c <= '9')
253  {
254  while (c == '0')
255  c = *++s;
256  if (c > '0' && c <= '9')
257  {
258  e = c - '0';
259  s1 = s;
260  while ((c = *++s) >= '0' && c <= '9')
261  e = 10 * e + c - '0';
262  if (s - s1 > 8)
263  /* Avoid confusion from exponents
264  * so large that e might overflow.
265  */
266  e = 9999999L;
267  if (esign)
268  e = -e;
269  }
270  else
271  e = 0;
272  }
273  else
274  s = s00;
275  }
276  if (!nd)
277  {
278  if (!nz && !nz0)
279  s = s00;
280  goto ret;
281  }
282  e1 = e -= nf;
283 
284  /* Now we have nd0 digits, starting at s0, followed by a
285  * decimal point, followed by nd-nd0 digits. The number we're
286  * after is the integer represented by those digits times
287  * 10**e */
288 
289  if (!nd0)
290  nd0 = nd;
291  k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
292  rv.d = y;
293  if (k > 9)
294  #ifndef SMALL_SCANF
295  rv.d = tens[k - 9] * rv.d + z;
296  #else
297  rv.d = small_tens[k - 9] * rv.d + z;
298  #endif
299  bd0 = 0;
300  if (nd <= DBL_DIG
301 #ifndef RND_PRODQUOT
302  && FLT_ROUNDS == 1
303 #endif
304  )
305  {
306  if (!e)
307  goto ret;
308  if (e > 0)
309  {
310  if (e <= Ten_pmax)
311  {
312 #ifdef VAX
313  goto vax_ovfl_check;
314 #else
315  #ifndef SMALL_SCANF
316  /* rv.d = */ rounded_product (rv.d, tens[e]);
317  #else
318  rounded_product (rv.d, small_tens[e]);
319  #endif
320  goto ret;
321 #endif
322  }
323  i = DBL_DIG - nd;
324  if (e <= Ten_pmax + i)
325  {
326  /* A fancier test would sometimes let us do
327  * this for larger i values.
328  */
329  e -= i;
330  #ifndef SMALL_SCANF
331  rv.d *= tens[i];
332  #else
333  rv.d *= small_tens[i];
334  #endif
335 #ifdef VAX
336  /* VAX exponent range is so narrow we must
337  * worry about overflow here...
338  */
339  vax_ovfl_check:
340  word0 (rv) -= P * Exp_msk1;
341  #ifndef SMALL_SCANF
342  /* rv.d = */ rounded_product (rv.d, tens[e]);
343  #else
344  /* rv.d = */ rounded_product (rv.d, small_tens[e]);
345  #endif
346  if ((word0 (rv) & Exp_mask)
347  > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P))
348  goto ovfl;
349  word0 (rv) += P * Exp_msk1;
350 #else
351  #ifndef SMALL_SCANF
352  /* rv.d = */ rounded_product (rv.d, tens[e]);
353  #else
354  /* rv.d = */ rounded_product (rv.d, small_tens[e]);
355  #endif
356 #endif
357  goto ret;
358  }
359  }
360 #ifndef Inaccurate_Divide
361  else if (e >= -Ten_pmax)
362  {
363  #ifndef SMALL_SCANF
364  /* rv.d = */ rounded_quotient (rv.d, tens[-e]);
365  #else
366  /* rv.d = */ rounded_quotient (rv.d, small_tens[-e]);
367  #endif
368  goto ret;
369  }
370 #endif
371  }
372  e1 += nd - k;
373 
374  /* Get starting approximation = rv.d * 10**e1 */
375 
376  if (e1 > 0)
377  {
378  if ((i = e1 & 15) != 0)
379  #ifndef SMALL_SCANF
380  rv.d *= tens[i];
381  #else
382  rv.d *= small_tens[i];
383  #endif
384  if (e1 &= ~15)
385  {
386  if (e1 > DBL_MAX_10_EXP)
387  {
388  ovfl:
389  ptr->_errno = ERANGE;
390 #ifdef _HAVE_STDC
391  rv.d = HUGE_VAL;
392 #else
393  /* Can't trust HUGE_VAL */
394 #ifdef IEEE_Arith
395  word0 (rv) = Exp_mask;
396 #ifndef _DOUBLE_IS_32BITS
397  word1 (rv) = 0;
398 #endif
399 #else
400  word0 (rv) = Big0;
401 #ifndef _DOUBLE_IS_32BITS
402  word1 (rv) = Big1;
403 #endif
404 #endif
405 #endif
406  if (bd0)
407  goto retfree;
408  goto ret;
409  }
410  if (e1 >>= 4)
411  {
412  for (j = 0; e1 > 1; j++, e1 >>= 1)
413  if (e1 & 1)
414  #ifndef SMALL_SCANF
415  rv.d *= bigtens[j];
416  #else
417  rv.d *= small_bigtens[j];
418  #endif
419 
420  /* The last multiplication could overflow. */
421  word0 (rv) -= P * Exp_msk1;
422  #ifndef SMALL_SCANF
423  rv.d *= bigtens[j];
424  #else
425  rv.d *= small_bigtens[j];
426  #endif
427 
428  if ((z = word0 (rv) & Exp_mask)
429  > Exp_msk1 * (DBL_MAX_EXP + Bias - P))
430  goto ovfl;
431  if (z > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P))
432  {
433  /* set to largest number */
434  /* (Can't trust DBL_MAX) */
435  word0 (rv) = Big0;
436 #ifndef _DOUBLE_IS_32BITS
437  word1 (rv) = Big1;
438 #endif
439  }
440  else
441  word0 (rv) += P * Exp_msk1;
442  }
443 
444  }
445  }
446  else if (e1 < 0)
447  {
448  e1 = -e1;
449  if ((i = e1 & 15) != 0)
450  #ifndef SMALL_SCANF
451  rv.d /= tens[i];
452  #else
453  rv.d /= small_tens[i];
454  #endif
455  if (e1 &= ~15)
456  {
457  e1 >>= 4;
458  if (e1 >= 1 << n_bigtens)
459  goto undfl;
460  for (j = 0; e1 > 1; j++, e1 >>= 1)
461  if (e1 & 1)
462  #ifndef SMALL_SCANF
463  rv.d *= tinytens[j];
464  /* The last multiplication could underflow. */
465  rv0.d = rv.d;
466  rv.d *=tinytens[j];
467  #else
468  rv.d *= small_tinytens[j];
469  /* The last multiplication could underflow. */
470  rv0.d = rv.d;
471  rv.d *= small_tinytens[j];
472  #endif
473  if (!rv.d)
474  {
475  rv.d = 2. * rv0.d;
476  #ifndef SMALL_SCANF
477  rv.d *= tinytens[j];
478  #else
479  rv.d *= small_tinytens[j];
480  #endif
481  if (!rv.d)
482  {
483  undfl:
484  rv.d = 0.;
485  ptr->_errno = ERANGE;
486  if (bd0)
487  goto retfree;
488  goto ret;
489  }
490 #ifndef _DOUBLE_IS_32BITS
491  word0 (rv) = Tiny0;
492  word1 (rv) = Tiny1;
493 #else
494  word0 (rv) = Tiny1;
495 #endif
496  /* The refinement below will clean
497  * this approximation up.
498  */
499  }
500  }
501  }
502 
503  /* Now the hard part -- adjusting rv to the correct value.*/
504 
505  /* Put digits into bd: true value = bd * 10^e */
506  #ifndef SMALL_SCANF
507  bd0 = s2b (ptr, s0, nd0, nd, y);
508  #else
509  bd0 = small_s2b(ptr,s0, nd0, nd, y, &tab_bd0[0]);
510  #endif
511 
512  for (;;)
513  {
514  #ifndef SMALL_SCANF
515  bd = Balloc (ptr, bd0->_k);
516  #else
517  bd = &tab_bd[0];
518  bd->_k = bd0->_k;
519  bd->_maxwds = 1 << (bd0->_k);
520  bd->_sign = bd->_wds =0;
521 
522  #endif
523  Bcopy (bd, bd0);
524  #ifndef SMALL_SCANF
525  bb = d2b (ptr, rv.d, &bbe, &bbbits); /* rv.d = bb * 2^bbe */
526  bs = i2b (ptr, 1);
527  #else
528  bb = small_d2b (ptr, rv.d, &bbe, &bbbits, &tab_bb[0]); /* rv.d = bb * 2^bbe */
529  bs = small_i2b (ptr, 1, &tab_bs[0]);
530  #endif
531  if (e >= 0)
532  {
533  bb2 = bb5 = 0;
534  bd2 = bd5 = e;
535  }
536  else
537  {
538  bb2 = bb5 = -e;
539  bd2 = bd5 = 0;
540  }
541  if (bbe >= 0)
542  bb2 += bbe;
543  else
544  bd2 -= bbe;
545  bs2 = bb2;
546 #ifdef Sudden_Underflow
547 #ifdef IBM
548  j = 1 + 4 * P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
549 #else
550  j = P + 1 - bbbits;
551 #endif
552 #else
553  i = bbe + bbbits - 1; /* logb(rv.d) */
554  if (i < Emin) /* denormal */
555  j = bbe + (P - Emin);
556  else
557  j = P + 1 - bbbits;
558 #endif
559  bb2 += j;
560  bd2 += j;
561  i = bb2 < bd2 ? bb2 : bd2;
562  if (i > bs2)
563  i = bs2;
564  if (i > 0)
565  {
566  bb2 -= i;
567  bd2 -= i;
568  bs2 -= i;
569  }
570  if (bb5 > 0)
571  {
572  #ifndef SMALL_SCANF
573  bs = pow5mult (ptr, bs, bb5);
574  bb1 = mult (ptr, bs, bb);
575  Bfree (ptr, bb);
576  bb = bb1;
577  #else
578  if (bs == &tab_bs[0]){
579  bs = small_pow5mult (ptr, bs, bb5,&tab_bslshift[0]);
580  }
581  else{
582  bs = small_pow5mult (ptr, bs, bb5,&tab_bs[0]);
583  }
584  bb1 = small_mult (ptr, bs, bb,&tab_bb1[0]);
585  bb = bb1;
586  #endif
587 
588  }
589 
590  #ifndef SMALL_SCANF
591  if (bb2 > 0)
592  bb = lshift (ptr, bb, bb2);
593  if (bd5 > 0)
594  bd = pow5mult (ptr, bd, bd5);
595  if (bd2 > 0)
596  bd = lshift (ptr, bd, bd2);
597  if (bs2 > 0)
598  bs = lshift (ptr, bs, bs2);
599  delta = diff (ptr, bb, bd);
600  dsign = delta->_sign;
601  delta->_sign = 0;
602  i = cmp (delta, bs);
603  #else
604  if (bb2 > 0){
605  if (bb == &tab_bb[0] ){
606  bb = small_lshift (ptr, bb, bb2,&tab_bblshift[0]);
607  }
608  else {
609  bb = small_lshift (ptr, bb, bb2,&tab_bblshift[0]);
610  }
611  }
612  if (bd5 > 0){
613  if (bd == &tab_bd[0]){
614  bd = small_pow5mult (ptr, bd, bd5, &tab_bdlshift[0]);
615  }
616  else{
617  bd = small_pow5mult (ptr, bd, bd5, &tab_bd[0]);
618  }
619  }
620  if (bd2 > 0){
621  if (bd == &tab_bd[0] ){
622  bd = small_lshift (ptr, bb, bd2,&tab_bdlshift[0]);
623  }
624  else {
625  bd = small_lshift (ptr, bd, bd2,&tab_bd[0]);
626  }
627  }
628  if (bs2 > 0){
629  if ( bs == &tab_bs[0] ){
630  bs = small_lshift (ptr, bs, bs2,&tab_bslshift[0]);
631  }
632  else{
633  bs = small_lshift (ptr, bs, bs2,&tab_bs[0]);
634  }
635  }
636 
637  delta = small_diff (ptr, bb, bd,&tab_delta[0]);
638  dsign = delta->_sign;
639  delta->_sign = 0;
640  i = small_cmp (delta, bs);
641 
642  #endif
643  if (i < 0)
644  {
645  /* Error is less than half an ulp -- check for
646  * special case of mantissa a power of two.
647  */
648  if (dsign || word1 (rv) || word0 (rv) & Bndry_mask)
649  break;
650 
651  #ifndef SMALL_SCANF
652  delta = lshift (ptr, delta, Log2P);
653  if (cmp (delta, bs) > 0)
654  goto drop_down;
655  #else
656  if (delta == &tab_delta[0]){
657  delta = small_lshift (ptr, delta, Log2P,&tab_deltalshift[0]);
658  }
659  else{
660  delta = small_lshift (ptr, delta, Log2P,&tab_delta[0]);
661  }
662  if (small_cmp (delta, bs) > 0)
663  goto drop_down;
664  #endif
665  break;
666  }
667  if (i == 0)
668  {
669  /* exactly half-way between */
670  if (dsign)
671  {
672  if ((word0 (rv) & Bndry_mask1) == Bndry_mask1
673  && word1 (rv) == 0xffffffff)
674  {
675  /*boundary case -- increment exponent*/
676  word0 (rv) = (word0 (rv) & Exp_mask)
677  + Exp_msk1
678 #ifdef IBM
679  | Exp_msk1 >> 4
680 #endif
681  ;
682 #ifndef _DOUBLE_IS_32BITS
683  word1 (rv) = 0;
684 #endif
685  break;
686  }
687  }
688  else if (!(word0 (rv) & Bndry_mask) && !word1 (rv))
689  {
690  drop_down:
691  /* boundary case -- decrement exponent */
692 #ifdef Sudden_Underflow
693  L = word0 (rv) & Exp_mask;
694 #ifdef IBM
695  if (L < Exp_msk1)
696 #else
697  if (L <= Exp_msk1)
698 #endif
699  goto undfl;
700  L -= Exp_msk1;
701 #else
702  L = (word0 (rv) & Exp_mask) - Exp_msk1;
703 #endif
704  word0 (rv) = L | Bndry_mask1;
705 #ifndef _DOUBLE_IS_32BITS
706  word1 (rv) = 0xffffffff;
707 #endif
708 #ifdef IBM
709  goto cont;
710 #else
711  break;
712 #endif
713  }
714 #ifndef ROUND_BIASED
715  if (!(word1 (rv) & LSB))
716  break;
717 #endif
718  if (dsign)
719  #ifndef SMALL_SCANF
720  rv.d += ulp (rv.d);
721  #else
722  rv.d += small_ulp (rv.d);
723  #endif
724 #ifndef ROUND_BIASED
725  else
726  {
727  #ifndef SMALL_SCANF
728  rv.d -= ulp (rv.d);
729  #else
730  rv.d -= small_ulp (rv.d);
731  #endif
732 #ifndef Sudden_Underflow
733  if (!rv.d)
734  goto undfl;
735 #endif
736  }
737 #endif
738  break;
739  }
740 
741  #ifndef SMALL_SCANF
742  if ((aadj = ratio (delta, bs)) <= 2.)
743  {
744  #else
745  if ((aadj = small_ratio (delta, bs)) <= 2.)
746  {
747  #endif
748  if (dsign)
749  aadj = aadj1 = 1.;
750  else if (word1 (rv) || word0 (rv) & Bndry_mask)
751  {
752 #ifndef Sudden_Underflow
753  if (word1 (rv) == Tiny1 && !word0 (rv))
754  goto undfl;
755 #endif
756  aadj = 1.;
757  aadj1 = -1.;
758  }
759  else
760  {
761  /* special case -- power of FLT_RADIX to be */
762  /* rounded down... */
763 
764  if (aadj < 2. / FLT_RADIX)
765  aadj = 1. / FLT_RADIX;
766  else
767  aadj *= 0.5;
768  aadj1 = -aadj;
769  }
770  }
771  else
772  {
773  aadj *= 0.5;
774  aadj1 = dsign ? aadj : -aadj;
775 #ifdef Check_FLT_ROUNDS
776  switch (FLT_ROUNDS)
777  {
778  case 2: /* towards +infinity */
779  aadj1 -= 0.5;
780  break;
781  case 0: /* towards 0 */
782  case 3: /* towards -infinity */
783  aadj1 += 0.5;
784  }
785 #else
786  if (FLT_ROUNDS == 0)
787  aadj1 += 0.5;
788 #endif
789  }
790  y = word0 (rv) & Exp_mask;
791 
792  /* Check for overflow */
793 
794  if (y == Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
795  {
796  rv0.d = rv.d;
797  word0 (rv) -= P * Exp_msk1;
798  #ifndef SMALL_SCANF
799  adj = aadj1 * ulp (rv.d);
800  #else
801  adj = aadj1 * small_ulp (rv.d);
802  #endif
803  rv.d += adj;
804  if ((word0 (rv) & Exp_mask) >=
805  Exp_msk1 * (DBL_MAX_EXP + Bias - P))
806  {
807  if (word0 (rv0) == Big0 && word1 (rv0) == Big1)
808  goto ovfl;
809 #ifdef _DOUBLE_IS_32BITS
810  word0 (rv) = Big1;
811 #else
812  word0 (rv) = Big0;
813  word1 (rv) = Big1;
814 #endif
815  goto cont;
816  }
817  else
818  word0 (rv) += P * Exp_msk1;
819  }
820  else
821  {
822 #ifdef Sudden_Underflow
823  if ((word0 (rv) & Exp_mask) <= P * Exp_msk1)
824  {
825  rv0.d = rv.d;
826  word0 (rv) += P * Exp_msk1;
827  #ifndef SMALL_SCANF
828  adj = aadj1 * ulp (rv.d);
829  #else
830  adj = aadj1 * small_ulp (rv.d);
831  #endif
832  rv.d += adj;
833  #ifdef IBM
834  if ((word0 (rv) & Exp_mask) < P * Exp_msk1)
835  #else
836  if ((word0 (rv) & Exp_mask) <= P * Exp_msk1)
837  #endif
838  {
839  if (word0 (rv0) == Tiny0
840  && word1 (rv0) == Tiny1)
841  goto undfl;
842  word0 (rv) = Tiny0;
843  word1 (rv) = Tiny1;
844  goto cont;
845  }
846  else
847  word0 (rv) -= P * Exp_msk1;
848  }
849  else
850  {
851  #ifndef SMALL_SCANF
852  adj = aadj1 * ulp (rv.d);
853  #else
854  adj = aadj1 * small_ulp (rv.d);
855  #endif
856  rv.d += adj;
857  }
858 #else
859  /* Compute adj so that the IEEE rounding rules will
860  * correctly round rv.d + adj in some half-way cases.
861  * If rv.d * ulp(rv.d) is denormalized (i.e.,
862  * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
863  * trouble from bits lost to denormalization;
864  * example: 1.2e-307 .
865  */
866  if (y <= (P - 1) * Exp_msk1 && aadj >= 1.)
867  {
868  aadj1 = (double) (int) (aadj + 0.5);
869  if (!dsign)
870  aadj1 = -aadj1;
871  }
872  #ifndef SMALL_SCANF
873  adj = aadj1 * ulp (rv.d);
874  #else
875  adj = aadj1 * small_ulp (rv.d);
876  rv.d += adj;
877  #endif
878 #endif
879  }
880  z = word0 (rv) & Exp_mask;
881  if (y == z)
882  {
883  /* Can we stop now? */
884  L = aadj;
885  aadj -= L;
886  /* The tolerances below are conservative. */
887  if (dsign || word1 (rv) || word0 (rv) & Bndry_mask)
888  {
889  if (aadj < .4999999 || aadj > .5000001)
890  break;
891  }
892  else if (aadj < .4999999 / FLT_RADIX)
893  break;
894  }
895  cont:
896  #ifndef SMALL_SCANF
897  Bfree (ptr, bb);
898  Bfree (ptr, bd);
899  Bfree (ptr, bs);
900  Bfree (ptr, delta);
901  #else
902  ;
903  #endif
904  }
905 retfree:
906  #ifndef SMALL_SCANF
907  Bfree (ptr, bb);
908  Bfree (ptr, bd);
909  Bfree (ptr, bs);
910  Bfree (ptr, bd0);
911  Bfree (ptr, delta);
912  #endif
913 ret:
914  if (se)
915  *se = (char *) s;
916  return sign ? -rv.d : rv.d;
917 }
918 
919 #ifndef NO_REENT
920 
921 double
922 _DEFUN (strtod, (s00, se),
923  _CONST char *s00 _AND char **se)
924 {
925  return _strtod_r (_REENT, s00, se);
926 }
927 
928 float
929 _DEFUN (strtof, (s00, se),
930  _CONST char *s00 _AND
931  char **se)
932 {
933  return (float)_strtod_r (_REENT, s00, se);
934 }
935 
936 #endif