aboutsummaryrefslogtreecommitdiff
path: root/stdlib/strtod_l.c
blob: 9bef59892a4c6f5114096c611c96a720c79e48dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
/* Convert string representing a number to float value, using given locale.
   Copyright (C) 1997-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <bits/floatn.h>

#ifdef FLOAT
# define BUILD_DOUBLE 0
#else
# define BUILD_DOUBLE 1
#endif

#if BUILD_DOUBLE
# if __HAVE_FLOAT64 && !__HAVE_DISTINCT_FLOAT64
#  define strtof64_l __hide_strtof64_l
#  define wcstof64_l __hide_wcstof64_l
# endif
# if __HAVE_FLOAT32X && !__HAVE_DISTINCT_FLOAT32X
#  define strtof32x_l __hide_strtof32x_l
#  define wcstof32x_l __hide_wcstof32x_l
# endif
#endif

#include <locale.h>

extern double ____strtod_l_internal (const char *, char **, int, locale_t);

/* Configuration part.  These macros are defined by `strtold.c',
   `strtof.c', `wcstod.c', `wcstold.c', and `wcstof.c' to produce the
   `long double' and `float' versions of the reader.  */
#ifndef FLOAT
# include <math_ldbl_opt.h>
# define FLOAT		double
# define FLT		DBL
# ifdef USE_WIDE_CHAR
#  define STRTOF	wcstod_l
#  define __STRTOF	__wcstod_l
#  define STRTOF_NAN	__wcstod_nan
# else
#  define STRTOF	strtod_l
#  define __STRTOF	__strtod_l
#  define STRTOF_NAN	__strtod_nan
# endif
# define MPN2FLOAT	__mpn_construct_double
# define FLOAT_HUGE_VAL	HUGE_VAL
#endif
/* End of configuration part.  */

#include <ctype.h>
#include <errno.h>
#include <float.h>
#include "../locale/localeinfo.h"
#include <math.h>
#include <math-barriers.h>
#include <math-narrow-eval.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <rounding-mode.h>
#include <tininess.h>

/* The gmp headers need some configuration frobs.  */
#define HAVE_ALLOCA 1

/* Include gmp-mparam.h first, such that definitions of _SHORT_LIMB
   and _LONG_LONG_LIMB in it can take effect into gmp.h.  */
#include <gmp-mparam.h>
#include <gmp.h>
#include "gmp-impl.h"
#include "longlong.h"
#include "fpioconst.h"

#include <assert.h>


/* We use this code for the extended locale handling where the
   function gets as an additional argument the locale which has to be
   used.  To access the values we have to redefine the _NL_CURRENT and
   _NL_CURRENT_WORD macros.  */
#undef _NL_CURRENT
#define _NL_CURRENT(category, item) \
  (current->values[_NL_ITEM_INDEX (item)].string)
#undef _NL_CURRENT_WORD
#define _NL_CURRENT_WORD(category, item) \
  ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)

#if defined _LIBC || defined HAVE_WCHAR_H
# include <wchar.h>
#endif

#ifdef USE_WIDE_CHAR
# include <wctype.h>
# define STRING_TYPE wchar_t
# define CHAR_TYPE wint_t
# define L_(Ch) L##Ch
# define ISSPACE(Ch) __iswspace_l ((Ch), loc)
# define ISDIGIT(Ch) __iswdigit_l ((Ch), loc)
# define ISXDIGIT(Ch) __iswxdigit_l ((Ch), loc)
# define TOLOWER(Ch) __towlower_l ((Ch), loc)
# define TOLOWER_C(Ch) __towlower_l ((Ch), _nl_C_locobj_ptr)
# define STRNCASECMP(S1, S2, N) \
  __wcsncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
#else
# define STRING_TYPE char
# define CHAR_TYPE char
# define L_(Ch) Ch
# define ISSPACE(Ch) __isspace_l ((Ch), loc)
# define ISDIGIT(Ch) __isdigit_l ((Ch), loc)
# define ISXDIGIT(Ch) __isxdigit_l ((Ch), loc)
# define TOLOWER(Ch) __tolower_l ((Ch), loc)
# define TOLOWER_C(Ch) __tolower_l ((Ch), _nl_C_locobj_ptr)
# define STRNCASECMP(S1, S2, N) \
  __strncasecmp_l ((S1), (S2), (N), _nl_C_locobj_ptr)
#endif


/* Constants we need from float.h; select the set for the FLOAT precision.  */
#define MANT_DIG	PASTE(FLT,_MANT_DIG)
#define	DIG		PASTE(FLT,_DIG)
#define	MAX_EXP		PASTE(FLT,_MAX_EXP)
#define	MIN_EXP		PASTE(FLT,_MIN_EXP)
#define MAX_10_EXP	PASTE(FLT,_MAX_10_EXP)
#define MIN_10_EXP	PASTE(FLT,_MIN_10_EXP)
#define MAX_VALUE	PASTE(FLT,_MAX)
#define MIN_VALUE	PASTE(FLT,_MIN)

/* Extra macros required to get FLT expanded before the pasting.  */
#define PASTE(a,b)	PASTE1(a,b)
#define PASTE1(a,b)	a##b

/* Function to construct a floating point number from an MP integer
   containing the fraction bits, a base 2 exponent, and a sign flag.  */
extern FLOAT MPN2FLOAT (mp_srcptr mpn, int exponent, int negative);

/* Definitions according to limb size used.  */
#if	BITS_PER_MP_LIMB == 32
# define MAX_DIG_PER_LIMB	9
# define MAX_FAC_PER_LIMB	1000000000UL
#elif	BITS_PER_MP_LIMB == 64
# define MAX_DIG_PER_LIMB	19
# define MAX_FAC_PER_LIMB	10000000000000000000ULL
#else
# error "mp_limb_t size " BITS_PER_MP_LIMB "not accounted for"
#endif

extern const mp_limb_t _tens_in_limb[MAX_DIG_PER_LIMB + 1];

#ifndef	howmany
#define	howmany(x,y)		(((x)+((y)-1))/(y))
#endif
#define SWAP(x, y)		({ typeof(x) _tmp = x; x = y; y = _tmp; })

#define	RETURN_LIMB_SIZE		howmany (MANT_DIG, BITS_PER_MP_LIMB)

#define RETURN(val,end)							      \
    do { if (endptr != NULL) *endptr = (STRING_TYPE *) (end);		      \
	 return val; } while (0)

/* Maximum size necessary for mpn integers to hold floating point
   numbers.  The largest number we need to hold is 10^n where 2^-n is
   1/4 ulp of the smallest representable value (that is, n = MANT_DIG
   - MIN_EXP + 2).  Approximate using 10^3 < 2^10.  */
#define	MPNSIZE		(howmany (1 + ((MANT_DIG - MIN_EXP + 2) * 10) / 3, \
				  BITS_PER_MP_LIMB) + 2)
/* Declare an mpn integer variable that big.  */
#define	MPN_VAR(name)	mp_limb_t name[MPNSIZE]; mp_size_t name##size
/* Copy an mpn integer value.  */
#define MPN_ASSIGN(dst, src) \
	memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))


/* Set errno and return an overflowing value with sign specified by
   NEGATIVE.  */
static FLOAT
overflow_value (int negative)
{
  __set_errno (ERANGE);
  FLOAT result = math_narrow_eval ((negative ? -MAX_VALUE : MAX_VALUE)
				   * MAX_VALUE);
  return result;
}


/* Set errno and return an underflowing value with sign specified by
   NEGATIVE.  */
static FLOAT
underflow_value (int negative)
{
  __set_errno (ERANGE);
  FLOAT result = math_narrow_eval ((negative ? -MIN_VALUE : MIN_VALUE)
				   * MIN_VALUE);
  return result;
}


/* Return a floating point number of the needed type according to the given
   multi-precision number after possible rounding.  */
static FLOAT
round_and_return (mp_limb_t *retval, intmax_t exponent, int negative,
		  mp_limb_t round_limb, mp_size_t round_bit, int more_bits)
{
  int mode = get_rounding_mode ();

  if (exponent < MIN_EXP - 1)
    {
      if (exponent < MIN_EXP - 1 - MANT_DIG)
	return underflow_value (negative);

      mp_size_t shift = MIN_EXP - 1 - exponent;
      bool is_tiny = true;

      more_bits |= (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0;
      if (shift == MANT_DIG)
	/* This is a special case to handle the very seldom case where
	   the mantissa will be empty after the shift.  */
	{
	  int i;

	  round_limb = retval[RETURN_LIMB_SIZE - 1];
	  round_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
	  for (i = 0; i < RETURN_LIMB_SIZE - 1; ++i)
	    more_bits |= retval[i] != 0;
	  MPN_ZERO (retval, RETURN_LIMB_SIZE);
	}
      else if (shift >= BITS_PER_MP_LIMB)
	{
	  int i;

	  round_limb = retval[(shift - 1) / BITS_PER_MP_LIMB];
	  round_bit = (shift - 1) % BITS_PER_MP_LIMB;
	  for (i = 0; i < (shift - 1) / BITS_PER_MP_LIMB; ++i)
	    more_bits |= retval[i] != 0;
	  more_bits |= ((round_limb & ((((mp_limb_t) 1) << round_bit) - 1))
			!= 0);

	  /* __mpn_rshift requires 0 < shift < BITS_PER_MP_LIMB.  */
	  if ((shift % BITS_PER_MP_LIMB) != 0)
	    (void) __mpn_rshift (retval, &retval[shift / BITS_PER_MP_LIMB],
			         RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB),
			         shift % BITS_PER_MP_LIMB);
	  else
	    for (i = 0; i < RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB); i++)
	      retval[i] = retval[i + (shift / BITS_PER_MP_LIMB)];
	  MPN_ZERO (&retval[RETURN_LIMB_SIZE - (shift / BITS_PER_MP_LIMB)],
		    shift / BITS_PER_MP_LIMB);
	}
      else if (shift > 0)
	{
	  if (TININESS_AFTER_ROUNDING && shift == 1)
	    {
	      /* Whether the result counts as tiny depends on whether,
		 after rounding to the normal precision, it still has
		 a subnormal exponent.  */
	      mp_limb_t retval_normal[RETURN_LIMB_SIZE];
	      if (round_away (negative,
			      (retval[0] & 1) != 0,
			      (round_limb
			       & (((mp_limb_t) 1) << round_bit)) != 0,
			      (more_bits
			       || ((round_limb
				    & ((((mp_limb_t) 1) << round_bit) - 1))
				   != 0)),
			      mode))
		{
		  mp_limb_t cy = __mpn_add_1 (retval_normal, retval,
					      RETURN_LIMB_SIZE, 1);

		  if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy)
		      || ((MANT_DIG % BITS_PER_MP_LIMB) != 0
			  && ((retval_normal[RETURN_LIMB_SIZE - 1]
			       & (((mp_limb_t) 1)
				  << (MANT_DIG % BITS_PER_MP_LIMB)))
			      != 0)))
		    is_tiny = false;
		}
	    }
	  round_limb = retval[0];
	  round_bit = shift - 1;
	  (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, shift);
	}
      /* This is a hook for the m68k long double format, where the
	 exponent bias is the same for normalized and denormalized
	 numbers.  */
#ifndef DENORM_EXP
# define DENORM_EXP (MIN_EXP - 2)
#endif
      exponent = DENORM_EXP;
      if (is_tiny
	  && ((round_limb & (((mp_limb_t) 1) << round_bit)) != 0
	      || more_bits
	      || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0))
	{
	  __set_errno (ERANGE);
	  FLOAT force_underflow = MIN_VALUE * MIN_VALUE;
	  math_force_eval (force_underflow);
	}
    }

  if (exponent >= MAX_EXP)
    goto overflow;

  bool half_bit = (round_limb & (((mp_limb_t) 1) << round_bit)) != 0;
  bool more_bits_nonzero
    = (more_bits
       || (round_limb & ((((mp_limb_t) 1) << round_bit) - 1)) != 0);
  if (round_away (negative,
		  (retval[0] & 1) != 0,
		  half_bit,
		  more_bits_nonzero,
		  mode))
    {
      mp_limb_t cy = __mpn_add_1 (retval, retval, RETURN_LIMB_SIZE, 1);

      if (((MANT_DIG % BITS_PER_MP_LIMB) == 0 && cy)
	  || ((MANT_DIG % BITS_PER_MP_LIMB) != 0
	      && (retval[RETURN_LIMB_SIZE - 1]
		  & (((mp_limb_t) 1) << (MANT_DIG % BITS_PER_MP_LIMB))) != 0))
	{
	  ++exponent;
	  (void) __mpn_rshift (retval, retval, RETURN_LIMB_SIZE, 1);
	  retval[RETURN_LIMB_SIZE - 1]
	    |= ((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB);
	}
      else if (exponent == DENORM_EXP
	       && (retval[RETURN_LIMB_SIZE - 1]
		   & (((mp_limb_t) 1) << ((MANT_DIG - 1) % BITS_PER_MP_LIMB)))
	       != 0)
	  /* The number was denormalized but now normalized.  */
	exponent = MIN_EXP - 1;
    }

  if (exponent >= MAX_EXP)
  overflow:
    return overflow_value (negative);

  if (half_bit || more_bits_nonzero)
    {
      FLOAT force_inexact = (FLOAT) 1 + MIN_VALUE;
      math_force_eval (force_inexact);
    }
  return MPN2FLOAT (retval, exponent, negative);
}


/* Read a multi-precision integer starting at STR with exactly DIGCNT digits
   into N.  Return the size of the number limbs in NSIZE at the first
   character od the string that is not part of the integer as the function
   value.  If the EXPONENT is small enough to be taken as an additional
   factor for the resulting number (see code) multiply by it.  */
static const STRING_TYPE *
str_to_mpn (const STRING_TYPE *str, int digcnt, mp_limb_t *n, mp_size_t *nsize,
	    intmax_t *exponent
#ifndef USE_WIDE_CHAR
	    , const char *decimal, size_t decimal_len, const char *thousands
#endif

	    )
{
  /* Number of digits for actual limb.  */
  int cnt = 0;
  mp_limb_t low = 0;
  mp_limb_t start;

  *nsize = 0;
  assert (digcnt > 0);
  do
    {
      if (cnt == MAX_DIG_PER_LIMB)
	{
	  if (*nsize == 0)
	    {
	      n[0] = low;
	      *nsize = 1;
	    }
	  else
	    {
	      mp_limb_t cy;
	      cy = __mpn_mul_1 (n, n, *nsize, MAX_FAC_PER_LIMB);
	      cy += __mpn_add_1 (n, n, *nsize, low);
	      if (cy != 0)
		{
		  assert (*nsize < MPNSIZE);
		  n[*nsize] = cy;
		  ++(*nsize);
		}
	    }
	  cnt = 0;
	  low = 0;
	}

      /* There might be thousands separators or radix characters in
	 the string.  But these all can be ignored because we know the
	 format of the number is correct and we have an exact number
	 of characters to read.  */
#ifdef USE_WIDE_CHAR
      if (*str < L'0' || *str > L'9')
	++str;
#else
      if (*str < '0' || *str > '9')
	{
	  int inner = 0;
	  if (thousands != NULL && *str == *thousands
	      && ({ for (inner = 1; thousands[inner] != '\0'; ++inner)
		      if (thousands[inner] != str[inner])
			break;
		    thousands[inner] == '\0'; }))
	    str += inner;
	  else
	    str += decimal_len;
	}
#endif
      low = low * 10 + *str++ - L_('0');
      ++cnt;
    }
  while (--digcnt > 0);

  if (*exponent > 0 && *exponent <= MAX_DIG_PER_LIMB - cnt)
    {
      low *= _tens_in_limb[*exponent];
      start = _tens_in_limb[cnt + *exponent];
      *exponent = 0;
    }
  else
    start = _tens_in_limb[cnt];

  if (*nsize == 0)
    {
      n[0] = low;
      *nsize = 1;
    }
  else
    {
      mp_limb_t cy;
      cy = __mpn_mul_1 (n, n, *nsize, start);
      cy += __mpn_add_1 (n, n, *nsize, low);
      if (cy != 0)
	{
	  assert (*nsize < MPNSIZE);
	  n[(*nsize)++] = cy;
	}
    }

  return str;
}


/* Shift {PTR, SIZE} COUNT bits to the left, and fill the vacated bits
   with the COUNT most significant bits of LIMB.

   Implemented as a macro, so that __builtin_constant_p works even at -O0.

   Tege doesn't like this macro so I have to write it here myself. :)
   --drepper */
#define __mpn_lshift_1(ptr, size, count, limb) \
  do									\
    {									\
      mp_limb_t *__ptr = (ptr);						\
      if (__builtin_constant_p (count) && count == BITS_PER_MP_LIMB)	\
	{								\
	  mp_size_t i;							\
	  for (i = (size) - 1; i > 0; --i)				\
	    __ptr[i] = __ptr[i - 1];					\
	  __ptr[0] = (limb);						\
	}								\
      else								\
	{								\
	  /* We assume count > 0 && count < BITS_PER_MP_LIMB here.  */	\
	  unsigned int __count = (count);				\
	  (void) __mpn_lshift (__ptr, __ptr, size, __count);		\
	  __ptr[0] |= (limb) >> (BITS_PER_MP_LIMB - __count);		\
	}								\
    }									\
  while (0)


#define INTERNAL(x) INTERNAL1(x)
#define INTERNAL1(x) __##x##_internal
#ifndef ____STRTOF_INTERNAL
# define ____STRTOF_INTERNAL INTERNAL (__STRTOF)
#endif

/* This file defines a function to check for correct grouping.  */
#include "grouping.h"


/* Return a floating point number with the value of the given string NPTR.
   Set *ENDPTR to the character after the last used one.  If the number is
   smaller than the smallest representable number, set `errno' to ERANGE and
   return 0.0.  If the number is too big to be represented, set `errno' to
   ERANGE and return HUGE_VAL with the appropriate sign.  */
FLOAT
____STRTOF_INTERNAL (const STRING_TYPE *nptr, STRING_TYPE **endptr, int group,
		     locale_t loc)
{
  int negative;			/* The sign of the number.  */
  MPN_VAR (num);		/* MP representation of the number.  */
  intmax_t exponent;		/* Exponent of the number.  */

  /* Numbers starting `0X' or `0x' have to be processed with base 16.  */
  int base = 10;

  /* When we have to compute fractional digits we form a fraction with a
     second multi-precision number (and we sometimes need a second for
     temporary results).  */
  MPN_VAR (den);

  /* Representation for the return value.  */
  mp_limb_t retval[RETURN_LIMB_SIZE];
  /* Number of bits currently in result value.  */
  int bits;

  /* Running pointer after the last character processed in the string.  */
  const STRING_TYPE *cp, *tp;
  /* Start of significant part of the number.  */
  const STRING_TYPE *startp, *start_of_digits;
  /* Points at the character following the integer and fractional digits.  */
  const STRING_TYPE *expp;
  /* Total number of digit and number of digits in integer part.  */
  size_t dig_no, int_no, lead_zero;
  /* Contains the last character read.  */
  CHAR_TYPE c;

/* We should get wint_t from <stddef.h>, but not all GCC versions define it
   there.  So define it ourselves if it remains undefined.  */
#ifndef _WINT_T
  typedef unsigned int wint_t;
#endif
  /* The radix character of the current locale.  */
#ifdef USE_WIDE_CHAR
  wchar_t decimal;
#else
  const char *decimal;
  size_t decimal_len;
#endif
  /* The thousands character of the current locale.  */
#ifdef USE_WIDE_CHAR
  wchar_t thousands = L'\0';
#else
  const char *thousands = NULL;
#endif
  /* The numeric grouping specification of the current locale,
     in the format described in <locale.h>.  */
  const char *grouping;
  /* Used in several places.  */
  int cnt;

  struct __locale_data *current = loc->__locales[LC_NUMERIC];

  if (__glibc_unlikely (group))
    {
      grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
      if (*grouping <= 0 || *grouping == CHAR_MAX)
	grouping = NULL;
      else
	{
	  /* Figure out the thousands separator character.  */
#ifdef USE_WIDE_CHAR
	  thousands = _NL_CURRENT_WORD (LC_NUMERIC,
					_NL_NUMERIC_THOUSANDS_SEP_WC);
	  if (thousands == L'\0')
	    grouping = NULL;
#else
	  thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
	  if (*thousands == '\0')
	    {
	      thousands = NULL;
	      grouping = NULL;
	    }
#endif
	}
    }
  else
    grouping = NULL;

  /* Find the locale's decimal point character.  */
#ifdef USE_WIDE_CHAR
  decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
  assert (decimal != L'\0');
# define decimal_len 1
#else
  decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
  decimal_len = strlen (decimal);
  assert (decimal_len > 0);
#endif

  /* Prepare number representation.  */
  exponent = 0;
  negative = 0;
  bits = 0;

  /* Parse string to get maximal legal prefix.  We need the number of
     characters of the integer part, the fractional part and the exponent.  */
  cp = nptr - 1;
  /* Ignore leading white space.  */
  do
    c = *++cp;
  while (ISSPACE (c));

  /* Get sign of the result.  */
  if (c == L_('-'))
    {
      negative = 1;
      c = *++cp;
    }
  else if (c == L_('+'))
    c = *++cp;

  /* Return 0.0 if no legal string is found.
     No character is used even if a sign was found.  */
#ifdef USE_WIDE_CHAR
  if (c == (wint_t) decimal
      && (wint_t) cp[1] >= L'0' && (wint_t) cp[1] <= L'9')
    {
      /* We accept it.  This funny construct is here only to indent
	 the code correctly.  */
    }
#else
  for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
    if (cp[cnt] != decimal[cnt])
      break;
  if (decimal[cnt] == '\0' && cp[cnt] >= '0' && cp[cnt] <= '9')
    {
      /* We accept it.  This funny construct is here only to indent
	 the code correctly.  */
    }
#endif
  else if (c < L_('0') || c > L_('9'))
    {
      /* Check for `INF' or `INFINITY'.  */
      CHAR_TYPE lowc = TOLOWER_C (c);

      if (lowc == L_('i') && STRNCASECMP (cp, L_("inf"), 3) == 0)
	{
	  /* Return +/- infinity.  */
	  if (endptr != NULL)
	    *endptr = (STRING_TYPE *)
		      (cp + (STRNCASECMP (cp + 3, L_("inity"), 5) == 0
			     ? 8 : 3));

	  return negative ? -FLOAT_HUGE_VAL : FLOAT_HUGE_VAL;
	}

      if (lowc == L_('n') && STRNCASECMP (cp, L_("nan"), 3) == 0)
	{
	  /* Return NaN.  */
	  FLOAT retval = NAN;

	  cp += 3;

	  /* Match `(n-char-sequence-digit)'.  */
	  if (*cp == L_('('))
	    {
	      const STRING_TYPE *startp = cp;
	      STRING_TYPE *endp;
	      retval = STRTOF_NAN (cp + 1, &endp, L_(')'));
	      if (*endp == L_(')'))
		/* Consume the closing parenthesis.  */
		cp = endp + 1;
	      else
		/* Only match the NAN part.  */
		cp = startp;
	    }

	  if (endptr != NULL)
	    *endptr = (STRING_TYPE *) cp;

	  return negative ? -retval : retval;
	}

      /* It is really a text we do not recognize.  */
      RETURN (0.0, nptr);
    }

  /* First look whether we are faced with a hexadecimal number.  */
  if (c == L_('0') && TOLOWER (cp[1]) == L_('x'))
    {
      /* Okay, it is a hexa-decimal number.  Remember this and skip
	 the characters.  BTW: hexadecimal numbers must not be
	 grouped.  */
      base = 16;
      cp += 2;
      c = *cp;
      grouping = NULL;
    }

  /* Record the start of the digits, in case we will check their grouping.  */
  start_of_digits = startp = cp;

  /* Ignore leading zeroes.  This helps us to avoid useless computations.  */
#ifdef USE_WIDE_CHAR
  while (c == L'0' || ((wint_t) thousands != L'\0' && c == (wint_t) thousands))
    c = *++cp;
#else
  if (__glibc_likely (thousands == NULL))
    while (c == '0')
      c = *++cp;
  else
    {
      /* We also have the multibyte thousands string.  */
      while (1)
	{
	  if (c != '0')
	    {
	      for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
		if (thousands[cnt] != cp[cnt])
		  break;
	      if (thousands[cnt] != '\0')
		break;
	      cp += cnt - 1;
	    }
	  c = *++cp;
	}
    }
#endif

  /* If no other digit but a '0' is found the result is 0.0.
     Return current read pointer.  */
  CHAR_TYPE lowc = TOLOWER (c);
  if (!((c >= L_('0') && c <= L_('9'))
	|| (base == 16 && lowc >= L_('a') && lowc <= L_('f'))
	|| (
#ifdef USE_WIDE_CHAR
	    c == (wint_t) decimal
#else
	    ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
		 if (decimal[cnt] != cp[cnt])
		   break;
	       decimal[cnt] == '\0'; })
#endif
	    /* '0x.' alone is not a valid hexadecimal number.
	       '.' alone is not valid either, but that has been checked
	       already earlier.  */
	    && (base != 16
		|| cp != start_of_digits
		|| (cp[decimal_len] >= L_('0') && cp[decimal_len] <= L_('9'))
		|| ({ CHAR_TYPE lo = TOLOWER (cp[decimal_len]);
		      lo >= L_('a') && lo <= L_('f'); })))
	|| (base == 16 && (cp != start_of_digits
			   && lowc == L_('p')))
	|| (base != 16 && lowc == L_('e'))))
    {
#ifdef USE_WIDE_CHAR
      tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
					 grouping);
#else
      tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
					 grouping);
#endif
      /* If TP is at the start of the digits, there was no correctly
	 grouped prefix of the string; so no number found.  */
      RETURN (negative ? -0.0 : 0.0,
	      tp == start_of_digits ? (base == 16 ? cp - 1 : nptr) : tp);
    }

  /* Remember first significant digit and read following characters until the
     decimal point, exponent character or any non-FP number character.  */
  startp = cp;
  dig_no = 0;
  while (1)
    {
      if ((c >= L_('0') && c <= L_('9'))
	  || (base == 16
	      && ({ CHAR_TYPE lo = TOLOWER (c);
		    lo >= L_('a') && lo <= L_('f'); })))
	++dig_no;
      else
	{
#ifdef USE_WIDE_CHAR
	  if (__builtin_expect ((wint_t) thousands == L'\0', 1)
	      || c != (wint_t) thousands)
	    /* Not a digit or separator: end of the integer part.  */
	    break;
#else
	  if (__glibc_likely (thousands == NULL))
	    break;
	  else
	    {
	      for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
		if (thousands[cnt] != cp[cnt])
		  break;
	      if (thousands[cnt] != '\0')
		break;
	      cp += cnt - 1;
	    }
#endif
	}
      c = *++cp;
    }

  if (__builtin_expect (grouping != NULL, 0) && cp > start_of_digits)
    {
      /* Check the grouping of the digits.  */
#ifdef USE_WIDE_CHAR
      tp = __correctly_grouped_prefixwc (start_of_digits, cp, thousands,
					 grouping);
#else
      tp = __correctly_grouped_prefixmb (start_of_digits, cp, thousands,
					 grouping);
#endif
      if (cp != tp)
	{
	  /* Less than the entire string was correctly grouped.  */

	  if (tp == start_of_digits)
	    /* No valid group of numbers at all: no valid number.  */
	    RETURN (0.0, nptr);

	  if (tp < startp)
	    /* The number is validly grouped, but consists
	       only of zeroes.  The whole value is zero.  */
	    RETURN (negative ? -0.0 : 0.0, tp);

	  /* Recompute DIG_NO so we won't read more digits than
	     are properly grouped.  */
	  cp = tp;
	  dig_no = 0;
	  for (tp = startp; tp < cp; ++tp)
	    if (*tp >= L_('0') && *tp <= L_('9'))
	      ++dig_no;

	  int_no = dig_no;
	  lead_zero = 0;

	  goto number_parsed;
	}
    }

  /* We have the number of digits in the integer part.  Whether these
     are all or any is really a fractional digit will be decided
     later.  */
  int_no = dig_no;
  lead_zero = int_no == 0 ? (size_t) -1 : 0;

  /* Read the fractional digits.  A special case are the 'american
     style' numbers like `16.' i.e. with decimal point but without
     trailing digits.  */
  if (
#ifdef USE_WIDE_CHAR
      c == (wint_t) decimal
#else
      ({ for (cnt = 0; decimal[cnt] != '\0'; ++cnt)
	   if (decimal[cnt] != cp[cnt])
	     break;
	 decimal[cnt] == '\0'; })
#endif
      )
    {
      cp += decimal_len;
      c = *cp;
      while ((c >= L_('0') && c <= L_('9'))
	     || (base == 16 && ({ CHAR_TYPE lo = TOLOWER (c);
				  lo >= L_('a') && lo <= L_('f'); })))
	{
	  if (c != L_('0') && lead_zero == (size_t) -1)
	    lead_zero = dig_no - int_no;
	  ++dig_no;
	  c = *++cp;
	}
    }
  assert (dig_no <= (uintmax_t) INTMAX_MAX);

  /* Remember start of exponent (if any).  */
  expp = cp;

  /* Read exponent.  */
  lowc = TOLOWER (c);
  if ((base == 16 && lowc == L_('p'))
      || (base != 16 && lowc == L_('e')))
    {
      int exp_negative = 0;

      c = *++cp;
      if (c == L_('-'))
	{
	  exp_negative = 1;
	  c = *++cp;
	}
      else if (c == L_('+'))
	c = *++cp;

      if (c >= L_('0') && c <= L_('9'))
	{
	  intmax_t exp_limit;

	  /* Get the exponent limit. */
	  if (base == 16)
	    {
	      if (exp_negative)
		{
		  assert (int_no <= (uintmax_t) (INTMAX_MAX
						 + MIN_EXP - MANT_DIG) / 4);
		  exp_limit = -MIN_EXP + MANT_DIG + 4 * (intmax_t) int_no;
		}
	      else
		{
		  if (int_no)
		    {
		      assert (lead_zero == 0
			      && int_no <= (uintmax_t) INTMAX_MAX / 4);
		      exp_limit = MAX_EXP - 4 * (intmax_t) int_no + 3;
		    }
		  else if (lead_zero == (size_t) -1)
		    {
		      /* The number is zero and this limit is
			 arbitrary.  */
		      exp_limit = MAX_EXP + 3;
		    }
		  else
		    {
		      assert (lead_zero
			      <= (uintmax_t) (INTMAX_MAX - MAX_EXP - 3) / 4);
		      exp_limit = (MAX_EXP
				   + 4 * (intmax_t) lead_zero
				   + 3);
		    }
		}
	    }
	  else
	    {
	      if (exp_negative)
		{
		  assert (int_no
			  <= (uintmax_t) (INTMAX_MAX + MIN_10_EXP - MANT_DIG));
		  exp_limit = -MIN_10_EXP + MANT_DIG + (intmax_t) int_no;
		}
	      else
		{
		  if (int_no)
		    {
		      assert (lead_zero == 0
			      && int_no <= (uintmax_t) INTMAX_MAX);
		      exp_limit = MAX_10_EXP - (intmax_t) int_no + 1;
		    }
		  else if (lead_zero == (size_t) -1)
		    {
		      /* The number is zero and this limit is
			 arbitrary.  */
		      exp_limit = MAX_10_EXP + 1;
		    }
		  else
		    {
		      assert (lead_zero
			      <= (uintmax_t) (INTMAX_MAX - MAX_10_EXP - 1));
		      exp_limit = MAX_10_EXP + (intmax_t) lead_zero + 1;
		    }
		}
	    }

	  if (exp_limit < 0)
	    exp_limit = 0;

	  do
	    {
	      if (__builtin_expect ((exponent > exp_limit / 10
				     || (exponent == exp_limit / 10
					 && c - L_('0') > exp_limit % 10)), 0))
		/* The exponent is too large/small to represent a valid
		   number.  */
		{
		  FLOAT result;

		  /* We have to take care for special situation: a joker
		     might have written "0.0e100000" which is in fact
		     zero.  */
		  if (lead_zero == (size_t) -1)
		    result = negative ? -0.0 : 0.0;
		  else
		    {
		      /* Overflow or underflow.  */
		      result = (exp_negative
				? underflow_value (negative)
				: overflow_value (negative));
		    }

		  /* Accept all following digits as part of the exponent.  */
		  do
		    ++cp;
		  while (*cp >= L_('0') && *cp <= L_('9'));

		  RETURN (result, cp);
		  /* NOTREACHED */
		}

	      exponent *= 10;
	      exponent += c - L_('0');

	      c = *++cp;
	    }
	  while (c >= L_('0') && c <= L_('9'));

	  if (exp_negative)
	    exponent = -exponent;
	}
      else
	cp = expp;
    }

  /* We don't want to have to work with trailing zeroes after the radix.  */
  if (dig_no > int_no)
    {
      while (expp[-1] == L_('0'))
	{
	  --expp;
	  --dig_no;
	}
      assert (dig_no >= int_no);
    }

  if (dig_no == int_no && dig_no > 0 && exponent < 0)
    do
      {
	while (! (base == 16 ? ISXDIGIT (expp[-1]) : ISDIGIT (expp[-1])))
	  --expp;

	if (expp[-1] != L_('0'))
	  break;

	--expp;
	--dig_no;
	--int_no;
	exponent += base == 16 ? 4 : 1;
      }
    while (dig_no > 0 && exponent < 0);

 number_parsed:

  /* The whole string is parsed.  Store the address of the next character.  */
  if (endptr)
    *endptr = (STRING_TYPE *) cp;

  if (dig_no == 0)
    return negative ? -0.0 : 0.0;

  if (lead_zero)
    {
      /* Find the decimal point */
#ifdef USE_WIDE_CHAR
      while (*startp != decimal)
	++startp;
#else
      while (1)
	{
	  if (*startp == decimal[0])
	    {
	      for (cnt = 1; decimal[cnt] != '\0'; ++cnt)
		if (decimal[cnt] != startp[cnt])
		  break;
	      if (decimal[cnt] == '\0')
		break;
	    }
	  ++startp;
	}
#endif
      startp += lead_zero + decimal_len;
      assert (lead_zero <= (base == 16
			    ? (uintmax_t) INTMAX_MAX / 4
			    : (uintmax_t) INTMAX_MAX));
      assert (lead_zero <= (base == 16
			    ? ((uintmax_t) exponent
			       - (uintmax_t) INTMAX_MIN) / 4
			    : ((uintmax_t) exponent - (uintmax_t) INTMAX_MIN)));
      exponent -= base == 16 ? 4 * (intmax_t) lead_zero : (intmax_t) lead_zero;
      dig_no -= lead_zero;
    }

  /* If the BASE is 16 we can use a simpler algorithm.  */
  if (base == 16)
    {
      static const int nbits[16] = { 0, 1, 2, 2, 3, 3, 3, 3,
				     4, 4, 4, 4, 4, 4, 4, 4 };
      int idx = (MANT_DIG - 1) / BITS_PER_MP_LIMB;
      int pos = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
      mp_limb_t val;

      while (!ISXDIGIT (*startp))
	++startp;
      while (*startp == L_('0'))
	++startp;
      if (ISDIGIT (*startp))
	val = *startp++ - L_('0');
      else
	val = 10 + TOLOWER (*startp++) - L_('a');
      bits = nbits[val];
      /* We cannot have a leading zero.  */
      assert (bits != 0);

      if (pos + 1 >= 4 || pos + 1 >= bits)
	{
	  /* We don't have to care for wrapping.  This is the normal
	     case so we add the first clause in the `if' expression as
	     an optimization.  It is a compile-time constant and so does
	     not cost anything.  */
	  retval[idx] = val << (pos - bits + 1);
	  pos -= bits;
	}
      else
	{
	  retval[idx--] = val >> (bits - pos - 1);
	  retval[idx] = val << (BITS_PER_MP_LIMB - (bits - pos - 1));
	  pos = BITS_PER_MP_LIMB - 1 - (bits - pos - 1);
	}

      /* Adjust the exponent for the bits we are shifting in.  */
      assert (int_no <= (uintmax_t) (exponent < 0
				     ? (INTMAX_MAX - bits + 1) / 4
				     : (INTMAX_MAX - exponent - bits + 1) / 4));
      exponent += bits - 1 + ((intmax_t) int_no - 1) * 4;

      while (--dig_no > 0 && idx >= 0)
	{
	  if (!ISXDIGIT (*startp))
	    startp += decimal_len;
	  if (ISDIGIT (*startp))
	    val = *startp++ - L_('0');
	  else
	    val = 10 + TOLOWER (*startp++) - L_('a');

	  if (pos + 1 >= 4)
	    {
	      retval[idx] |= val << (pos - 4 + 1);
	      pos -= 4;
	    }
	  else
	    {
	      retval[idx--] |= val >> (4 - pos - 1);
	      val <<= BITS_PER_MP_LIMB - (4 - pos - 1);
	      if (idx < 0)
		{
		  int rest_nonzero = 0;
		  while (--dig_no > 0)
		    {
		      if (*startp != L_('0'))
			{
			  rest_nonzero = 1;
			  break;
			}
		      startp++;
		    }
		  return round_and_return (retval, exponent, negative, val,
					   BITS_PER_MP_LIMB - 1, rest_nonzero);
		}

	      retval[idx] = val;
	      pos = BITS_PER_MP_LIMB - 1 - (4 - pos - 1);
	    }
	}

      /* We ran out of digits.  */
      MPN_ZERO (retval, idx);

      return round_and_return (retval, exponent, negative, 0, 0, 0);
    }

  /* Now we have the number of digits in total and the integer digits as well
     as the exponent and its sign.  We can decide whether the read digits are
     really integer digits or belong to the fractional part; i.e. we normalize
     123e-2 to 1.23.  */
  {
    intmax_t incr = (exponent < 0
		     ? MAX (-(intmax_t) int_no, exponent)
		     : MIN ((intmax_t) dig_no - (intmax_t) int_no, exponent));
    int_no += incr;
    exponent -= incr;
  }

  if (__glibc_unlikely (exponent > MAX_10_EXP + 1 - (intmax_t) int_no))
    return overflow_value (negative);

  /* 10^(MIN_10_EXP-1) is not normal.  Thus, 10^(MIN_10_EXP-1) /
     2^MANT_DIG is below half the least subnormal, so anything with a
     base-10 exponent less than the base-10 exponent (which is
     MIN_10_EXP - 1 - ceil(MANT_DIG*log10(2))) of that value
     underflows.  DIG is floor((MANT_DIG-1)log10(2)), so an exponent
     below MIN_10_EXP - (DIG + 3) underflows.  But EXPONENT is
     actually an exponent multiplied only by a fractional part, not an
     integer part, so an exponent below MIN_10_EXP - (DIG + 2)
     underflows.  */
  if (__glibc_unlikely (exponent < MIN_10_EXP - (DIG + 2)))
    return underflow_value (negative);

  if (int_no > 0)
    {
      /* Read the integer part as a multi-precision number to NUM.  */
      startp = str_to_mpn (startp, int_no, num, &numsize, &exponent
#ifndef USE_WIDE_CHAR
			   , decimal, decimal_len, thousands
#endif
			   );

      if (exponent > 0)
	{
	  /* We now multiply the gained number by the given power of ten.  */
	  mp_limb_t *psrc = num;
	  mp_limb_t *pdest = den;
	  int expbit = 1;
	  const struct mp_power *ttab = &_fpioconst_pow10[0];

	  do
	    {
	      if ((exponent & expbit) != 0)
		{
		  size_t size = ttab->arraysize - _FPIO_CONST_OFFSET;
		  mp_limb_t cy;
		  exponent ^= expbit;

		  /* FIXME: not the whole multiplication has to be
		     done.  If we have the needed number of bits we
		     only need the information whether more non-zero
		     bits follow.  */
		  if (numsize >= ttab->arraysize - _FPIO_CONST_OFFSET)
		    cy = __mpn_mul (pdest, psrc, numsize,
				    &__tens[ttab->arrayoff
					   + _FPIO_CONST_OFFSET],
				    size);
		  else
		    cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
						  + _FPIO_CONST_OFFSET],
				    size, psrc, numsize);
		  numsize += size;
		  if (cy == 0)
		    --numsize;
		  (void) SWAP (psrc, pdest);
		}
	      expbit <<= 1;
	      ++ttab;
	    }
	  while (exponent != 0);

	  if (psrc == den)
	    memcpy (num, den, numsize * sizeof (mp_limb_t));
	}

      /* Determine how many bits of the result we already have.  */
      count_leading_zeros (bits, num[numsize - 1]);
      bits = numsize * BITS_PER_MP_LIMB - bits;

      /* Now we know the exponent of the number in base two.
	 Check it against the maximum possible exponent.  */
      if (__glibc_unlikely (bits > MAX_EXP))
	return overflow_value (negative);

      /* We have already the first BITS bits of the result.  Together with
	 the information whether more non-zero bits follow this is enough
	 to determine the result.  */
      if (bits > MANT_DIG)
	{
	  int i;
	  const mp_size_t least_idx = (bits - MANT_DIG) / BITS_PER_MP_LIMB;
	  const mp_size_t least_bit = (bits - MANT_DIG) % BITS_PER_MP_LIMB;
	  const mp_size_t round_idx = least_bit == 0 ? least_idx - 1
						     : least_idx;
	  const mp_size_t round_bit = least_bit == 0 ? BITS_PER_MP_LIMB - 1
						     : least_bit - 1;

	  if (least_bit == 0)
	    memcpy (retval, &num[least_idx],
		    RETURN_LIMB_SIZE * sizeof (mp_limb_t));
	  else
	    {
	      for (i = least_idx; i < numsize - 1; ++i)
		retval[i - least_idx] = (num[i] >> least_bit)
					| (num[i + 1]
					   << (BITS_PER_MP_LIMB - least_bit));
	      if (i - least_idx < RETURN_LIMB_SIZE)
		retval[RETURN_LIMB_SIZE - 1] = num[i] >> least_bit;
	    }

	  /* Check whether any limb beside the ones in RETVAL are non-zero.  */
	  for (i = 0; num[i] == 0; ++i)
	    ;

	  return round_and_return (retval, bits - 1, negative,
				   num[round_idx], round_bit,
				   int_no < dig_no || i < round_idx);
	  /* NOTREACHED */
	}
      else if (dig_no == int_no)
	{
	  const mp_size_t target_bit = (MANT_DIG - 1) % BITS_PER_MP_LIMB;
	  const mp_size_t is_bit = (bits - 1) % BITS_PER_MP_LIMB;

	  if (target_bit == is_bit)
	    {
	      memcpy (&retval[RETURN_LIMB_SIZE - numsize], num,
		      numsize * sizeof (mp_limb_t));
	      /* FIXME: the following loop can be avoided if we assume a
		 maximal MANT_DIG value.  */
	      MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
	    }
	  else if (target_bit > is_bit)
	    {
	      (void) __mpn_lshift (&retval[RETURN_LIMB_SIZE - numsize],
				   num, numsize, target_bit - is_bit);
	      /* FIXME: the following loop can be avoided if we assume a
		 maximal MANT_DIG value.  */
	      MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize);
	    }
	  else
	    {
	      mp_limb_t cy;
	      assert (numsize < RETURN_LIMB_SIZE);

	      cy = __mpn_rshift (&retval[RETURN_LIMB_SIZE - numsize],
				 num, numsize, is_bit - target_bit);
	      retval[RETURN_LIMB_SIZE - numsize - 1] = cy;
	      /* FIXME: the following loop can be avoided if we assume a
		 maximal MANT_DIG value.  */
	      MPN_ZERO (retval, RETURN_LIMB_SIZE - numsize - 1);
	    }

	  return round_and_return (retval, bits - 1, negative, 0, 0, 0);
	  /* NOTREACHED */
	}

      /* Store the bits we already have.  */
      memcpy (retval, num, numsize * sizeof (mp_limb_t));
#if RETURN_LIMB_SIZE > 1
      if (numsize < RETURN_LIMB_SIZE)
# if RETURN_LIMB_SIZE == 2
	retval[numsize] = 0;
# else
	MPN_ZERO (retval + numsize, RETURN_LIMB_SIZE - numsize);
# endif
#endif
    }

  /* We have to compute at least some of the fractional digits.  */
  {
    /* We construct a fraction and the result of the division gives us
       the needed digits.  The denominator is 1.0 multiplied by the
       exponent of the lowest digit; i.e. 0.123 gives 123 / 1000 and
       123e-6 gives 123 / 1000000.  */

    int expbit;
    int neg_exp;
    int more_bits;
    int need_frac_digits;
    mp_limb_t cy;
    mp_limb_t *psrc = den;
    mp_limb_t *pdest = num;
    const struct mp_power *ttab = &_fpioconst_pow10[0];

    assert (dig_no > int_no
	    && exponent <= 0
	    && exponent >= MIN_10_EXP - (DIG + 2));

    /* We need to compute MANT_DIG - BITS fractional bits that lie
       within the mantissa of the result, the following bit for
       rounding, and to know whether any subsequent bit is 0.
       Computing a bit with value 2^-n means looking at n digits after
       the decimal point.  */
    if (bits > 0)
      {
	/* The bits required are those immediately after the point.  */
	assert (int_no > 0 && exponent == 0);
	need_frac_digits = 1 + MANT_DIG - bits;
      }
    else
      {
	/* The number is in the form .123eEXPONENT.  */
	assert (int_no == 0 && *startp != L_('0'));
	/* The number is at least 10^(EXPONENT-1), and 10^3 <
	   2^10.  */
	int neg_exp_2 = ((1 - exponent) * 10) / 3 + 1;
	/* The number is at least 2^-NEG_EXP_2.  We need up to
	   MANT_DIG bits following that bit.  */
	need_frac_digits = neg_exp_2 + MANT_DIG;
	/* However, we never need bits beyond 1/4 ulp of the smallest
	   representable value.  (That 1/4 ulp bit is only needed to
	   determine tinyness on machines where tinyness is determined
	   after rounding.)  */
	if (need_frac_digits > MANT_DIG - MIN_EXP + 2)
	  need_frac_digits = MANT_DIG - MIN_EXP + 2;
	/* At this point, NEED_FRAC_DIGITS is the total number of
	   digits needed after the point, but some of those may be
	   leading 0s.  */
	need_frac_digits += exponent;
	/* Any cases underflowing enough that none of the fractional
	   digits are needed should have been caught earlier (such
	   cases are on the order of 10^-n or smaller where 2^-n is
	   the least subnormal).  */
	assert (need_frac_digits > 0);
      }

    if (need_frac_digits > (intmax_t) dig_no - (intmax_t) int_no)
      need_frac_digits = (intmax_t) dig_no - (intmax_t) int_no;

    if ((intmax_t) dig_no > (intmax_t) int_no + need_frac_digits)
      {
	dig_no = int_no + need_frac_digits;
	more_bits = 1;
      }
    else
      more_bits = 0;

    neg_exp = (intmax_t) dig_no - (intmax_t) int_no - exponent;

    /* Construct the denominator.  */
    densize = 0;
    expbit = 1;
    do
      {
	if ((neg_exp & expbit) != 0)
	  {
	    mp_limb_t cy;
	    neg_exp ^= expbit;

	    if (densize == 0)
	      {
		densize = ttab->arraysize - _FPIO_CONST_OFFSET;
		memcpy (psrc, &__tens[ttab->arrayoff + _FPIO_CONST_OFFSET],
			densize * sizeof (mp_limb_t));
	      }
	    else
	      {
		cy = __mpn_mul (pdest, &__tens[ttab->arrayoff
					      + _FPIO_CONST_OFFSET],
				ttab->arraysize - _FPIO_CONST_OFFSET,
				psrc, densize);
		densize += ttab->arraysize - _FPIO_CONST_OFFSET;
		if (cy == 0)
		  --densize;
		(void) SWAP (psrc, pdest);
	      }
	  }
	expbit <<= 1;
	++ttab;
      }
    while (neg_exp != 0);

    if (psrc == num)
      memcpy (den, num, densize * sizeof (mp_limb_t));

    /* Read the fractional digits from the string.  */
    (void) str_to_mpn (startp, dig_no - int_no, num, &numsize, &exponent
#ifndef USE_WIDE_CHAR
		       , decimal, decimal_len, thousands
#endif
		       );

    /* We now have to shift both numbers so that the highest bit in the
       denominator is set.  In the same process we copy the numerator to
       a high place in the array so that the division constructs the wanted
       digits.  This is done by a "quasi fix point" number representation.

       num:   ddddddddddd . 0000000000000000000000
	      |--- m ---|
       den:                            ddddddddddd      n >= m
				       |--- n ---|
     */

    count_leading_zeros (cnt, den[densize - 1]);

    if (cnt > 0)
      {
	/* Don't call `mpn_shift' with a count of zero since the specification
	   does not allow this.  */
	(void) __mpn_lshift (den, den, densize, cnt);
	cy = __mpn_lshift (num, num, numsize, cnt);
	if (cy != 0)
	  num[numsize++] = cy;
      }

    /* Now we are ready for the division.  But it is not necessary to
       do a full multi-precision division because we only need a small
       number of bits for the result.  So we do not use __mpn_divmod
       here but instead do the division here by hand and stop whenever
       the needed number of bits is reached.  The code itself comes
       from the GNU MP Library by Torbj\"orn Granlund.  */

    exponent = bits;

    switch (densize)
      {
      case 1:
	{
	  mp_limb_t d, n, quot;
	  int used = 0;

	  n = num[0];
	  d = den[0];
	  assert (numsize == 1 && n < d);

	  do
	    {
	      udiv_qrnnd (quot, n, n, 0, d);

#define got_limb							      \
	      if (bits == 0)						      \
		{							      \
		  int cnt;						      \
		  if (quot == 0)					      \
		    cnt = BITS_PER_MP_LIMB;				      \
		  else							      \
		    count_leading_zeros (cnt, quot);			      \
		  exponent -= cnt;					      \
		  if (BITS_PER_MP_LIMB - cnt > MANT_DIG)		      \
		    {							      \
		      used = MANT_DIG + cnt;				      \
		      retval[0] = quot >> (BITS_PER_MP_LIMB - used);	      \
		      bits = MANT_DIG + 1;				      \
		    }							      \
		  else							      \
		    {							      \
		      /* Note that we only clear the second element.  */      \
		      /* The conditional is determined at compile time.  */   \
		      if (RETURN_LIMB_SIZE > 1)				      \
			retval[1] = 0;					      \
		      retval[0] = quot;					      \
		      bits = -cnt;					      \
		    }							      \
		}							      \
	      else if (bits + BITS_PER_MP_LIMB <= MANT_DIG)		      \
		__mpn_lshift_1 (retval, RETURN_LIMB_SIZE, BITS_PER_MP_LIMB,   \
				quot);					      \
	      else							      \
		{							      \
		  used = MANT_DIG - bits;				      \
		  if (used > 0)						      \
		    __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, quot);    \
		}							      \
	      bits += BITS_PER_MP_LIMB

	      got_limb;
	    }
	  while (bits <= MANT_DIG);

	  return round_and_return (retval, exponent - 1, negative,
				   quot, BITS_PER_MP_LIMB - 1 - used,
				   more_bits || n != 0);
	}
      case 2:
	{
	  mp_limb_t d0, d1, n0, n1;
	  mp_limb_t quot = 0;
	  int used = 0;

	  d0 = den[0];
	  d1 = den[1];

	  if (numsize < densize)
	    {
	      if (num[0] >= d1)
		{
		  /* The numerator of the number occupies fewer bits than
		     the denominator but the one limb is bigger than the
		     high limb of the numerator.  */
		  n1 = 0;
		  n0 = num[0];
		}
	      else
		{
		  if (bits <= 0)
		    exponent -= BITS_PER_MP_LIMB;
		  else
		    {
		      if (bits + BITS_PER_MP_LIMB <= MANT_DIG)
			__mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
					BITS_PER_MP_LIMB, 0);
		      else
			{
			  used = MANT_DIG - bits;
			  if (used > 0)
			    __mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
			}
		      bits += BITS_PER_MP_LIMB;
		    }
		  n1 = num[0];
		  n0 = 0;
		}
	    }
	  else
	    {
	      n1 = num[1];
	      n0 = num[0];
	    }

	  while (bits <= MANT_DIG)
	    {
	      mp_limb_t r;

	      if (n1 == d1)
		{
		  /* QUOT should be either 111..111 or 111..110.  We need
		     special treatment of this rare case as normal division
		     would give overflow.  */
		  quot = ~(mp_limb_t) 0;

		  r = n0 + d1;
		  if (r < d1)	/* Carry in the addition?  */
		    {
		      add_ssaaaa (n1, n0, r - d0, 0, 0, d0);
		      goto have_quot;
		    }
		  n1 = d0 - (d0 != 0);
		  n0 = -d0;
		}
	      else
		{
		  udiv_qrnnd (quot, r, n1, n0, d1);
		  umul_ppmm (n1, n0, d0, quot);
		}

	    q_test:
	      if (n1 > r || (n1 == r && n0 > 0))
		{
		  /* The estimated QUOT was too large.  */
		  --quot;

		  sub_ddmmss (n1, n0, n1, n0, 0, d0);
		  r += d1;
		  if (r >= d1)	/* If not carry, test QUOT again.  */
		    goto q_test;
		}
	      sub_ddmmss (n1, n0, r, 0, n1, n0);

	    have_quot:
	      got_limb;
	    }

	  return round_and_return (retval, exponent - 1, negative,
				   quot, BITS_PER_MP_LIMB - 1 - used,
				   more_bits || n1 != 0 || n0 != 0);
	}
      default:
	{
	  int i;
	  mp_limb_t cy, dX, d1, n0, n1;
	  mp_limb_t quot = 0;
	  int used = 0;

	  dX = den[densize - 1];
	  d1 = den[densize - 2];

	  /* The division does not work if the upper limb of the two-limb
	     numerator is greater than the denominator.  */
	  if (__mpn_cmp (num, &den[densize - numsize], numsize) > 0)
	    num[numsize++] = 0;

	  if (numsize < densize)
	    {
	      mp_size_t empty = densize - numsize;
	      int i;

	      if (bits <= 0)
		exponent -= empty * BITS_PER_MP_LIMB;
	      else
		{
		  if (bits + empty * BITS_PER_MP_LIMB <= MANT_DIG)
		    {
		      /* We make a difference here because the compiler
			 cannot optimize the `else' case that good and
			 this reflects all currently used FLOAT types
			 and GMP implementations.  */
#if RETURN_LIMB_SIZE <= 2
		      assert (empty == 1);
		      __mpn_lshift_1 (retval, RETURN_LIMB_SIZE,
				      BITS_PER_MP_LIMB, 0);
#else
		      for (i = RETURN_LIMB_SIZE - 1; i >= empty; --i)
			retval[i] = retval[i - empty];
		      while (i >= 0)
			retval[i--] = 0;
#endif
		    }
		  else
		    {
		      used = MANT_DIG - bits;
		      if (used >= BITS_PER_MP_LIMB)
			{
			  int i;
			  (void) __mpn_lshift (&retval[used
						       / BITS_PER_MP_LIMB],
					       retval,
					       (RETURN_LIMB_SIZE
						- used / BITS_PER_MP_LIMB),
					       used % BITS_PER_MP_LIMB);
			  for (i = used / BITS_PER_MP_LIMB - 1; i >= 0; --i)
			    retval[i] = 0;
			}
		      else if (used > 0)
			__mpn_lshift_1 (retval, RETURN_LIMB_SIZE, used, 0);
		    }
		  bits += empty * BITS_PER_MP_LIMB;
		}
	      for (i = numsize; i > 0; --i)
		num[i + empty] = num[i - 1];
	      MPN_ZERO (num, empty + 1);
	    }
	  else
	    {
	      int i;
	      assert (numsize == densize);
	      for (i = numsize; i > 0; --i)
		num[i] = num[i - 1];
	      num[0] = 0;
	    }

	  den[densize] = 0;
	  n0 = num[densize];

	  while (bits <= MANT_DIG)
	    {
	      if (n0 == dX)
		/* This might over-estimate QUOT, but it's probably not
		   worth the extra code here to find out.  */
		quot = ~(mp_limb_t) 0;
	      else
		{
		  mp_limb_t r;

		  udiv_qrnnd (quot, r, n0, num[densize - 1], dX);
		  umul_ppmm (n1, n0, d1, quot);

		  while (n1 > r || (n1 == r && n0 > num[densize - 2]))
		    {
		      --quot;
		      r += dX;
		      if (r < dX) /* I.e. "carry in previous addition?" */
			break;
		      n1 -= n0 < d1;
		      n0 -= d1;
		    }
		}

	      /* Possible optimization: We already have (q * n0) and (1 * n1)
		 after the calculation of QUOT.  Taking advantage of this, we
		 could make this loop make two iterations less.  */

	      cy = __mpn_submul_1 (num, den, densize + 1, quot);

	      if (num[densize] != cy)
		{
		  cy = __mpn_add_n (num, num, den, densize);
		  assert (cy != 0);
		  --quot;
		}
	      n0 = num[densize] = num[densize - 1];
	      for (i = densize - 1; i > 0; --i)
		num[i] = num[i - 1];
	      num[0] = 0;

	      got_limb;
	    }

	  for (i = densize; i >= 0 && num[i] == 0; --i)
	    ;
	  return round_and_return (retval, exponent - 1, negative,
				   quot, BITS_PER_MP_LIMB - 1 - used,
				   more_bits || i >= 0);
	}
      }
  }

  /* NOTREACHED */
}
#if defined _LIBC && !defined USE_WIDE_CHAR
libc_hidden_def (____STRTOF_INTERNAL)
#endif

/* External user entry point.  */

FLOAT
#ifdef weak_function
weak_function
#endif
__STRTOF (const STRING_TYPE *nptr, STRING_TYPE **endptr, locale_t loc)
{
  return ____STRTOF_INTERNAL (nptr, endptr, 0, loc);
}
#if defined _LIBC
libc_hidden_def (__STRTOF)
libc_hidden_ver (__STRTOF, STRTOF)
#endif
weak_alias (__STRTOF, STRTOF)

#ifdef LONG_DOUBLE_COMPAT
# if LONG_DOUBLE_COMPAT(libc, GLIBC_2_1)
#  ifdef USE_WIDE_CHAR
compat_symbol (libc, __wcstod_l, __wcstold_l, GLIBC_2_1);
#  else
compat_symbol (libc, __strtod_l, __strtold_l, GLIBC_2_1);
#  endif
# endif
# if LONG_DOUBLE_COMPAT(libc, GLIBC_2_3)
#  ifdef USE_WIDE_CHAR
compat_symbol (libc, wcstod_l, wcstold_l, GLIBC_2_3);
#  else
compat_symbol (libc, strtod_l, strtold_l, GLIBC_2_3);
#  endif
# endif
#endif

#if BUILD_DOUBLE
# if __HAVE_FLOAT64 && !__HAVE_DISTINCT_FLOAT64
#  undef strtof64_l
#  undef wcstof64_l
#  ifdef USE_WIDE_CHAR
weak_alias (wcstod_l, wcstof64_l)
#  else
weak_alias (strtod_l, strtof64_l)
#  endif
# endif
# if __HAVE_FLOAT32X && !__HAVE_DISTINCT_FLOAT32X
#  undef strtof32x_l
#  undef wcstof32x_l
#  ifdef USE_WIDE_CHAR
weak_alias (wcstod_l, wcstof32x_l)
#  else
weak_alias (strtod_l, strtof32x_l)
#  endif
# endif
#endif