10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*6812Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
30*6812Sraf #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * This file is based on /usr/src/lib/libc/port/gen/strtod.c and
340Sstevel@tonic-gate * /usr/src/lib/libc/sparc/fp/string_decim.c
350Sstevel@tonic-gate */
360Sstevel@tonic-gate
37*6812Sraf #pragma weak _wcstod = wcstod
38*6812Sraf #pragma weak _wstod = wstod
390Sstevel@tonic-gate
40*6812Sraf #include "lint.h"
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <values.h>
440Sstevel@tonic-gate #include <floatingpoint.h>
450Sstevel@tonic-gate #include <stddef.h>
460Sstevel@tonic-gate #include <wctype.h>
470Sstevel@tonic-gate #include "base_conversion.h" /* from usr/src/lib/libc/inc */
480Sstevel@tonic-gate #include <locale.h>
490Sstevel@tonic-gate #include "libc.h"
500Sstevel@tonic-gate #include "xpg6.h"
510Sstevel@tonic-gate
520Sstevel@tonic-gate static void wstring_to_decimal(const wchar_t **, int, decimal_record *, int *);
530Sstevel@tonic-gate
540Sstevel@tonic-gate double
wcstod(const wchar_t * cp,wchar_t ** ptr)55*6812Sraf wcstod(const wchar_t *cp, wchar_t **ptr)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate double x;
580Sstevel@tonic-gate decimal_mode mr;
590Sstevel@tonic-gate decimal_record dr;
600Sstevel@tonic-gate fp_exception_field_type fs;
610Sstevel@tonic-gate int form;
620Sstevel@tonic-gate
630Sstevel@tonic-gate wstring_to_decimal(&cp, __xpg6 & _C99SUSv3_recognize_hexfp, &dr, &form);
640Sstevel@tonic-gate if (ptr != NULL)
650Sstevel@tonic-gate *ptr = (wchar_t *)cp;
660Sstevel@tonic-gate if (form == 0)
670Sstevel@tonic-gate return (0.0); /* Shameful kluge for SVID's sake. */
680Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
690Sstevel@tonic-gate mr.rd = __xgetRD();
700Sstevel@tonic-gate #elif defined(__sparc)
710Sstevel@tonic-gate mr.rd = _QgetRD();
720Sstevel@tonic-gate #else
730Sstevel@tonic-gate #error Unknown architecture!
740Sstevel@tonic-gate #endif
750Sstevel@tonic-gate if (form < 0)
760Sstevel@tonic-gate __hex_to_double(&dr, mr.rd, &x, &fs);
770Sstevel@tonic-gate else
780Sstevel@tonic-gate decimal_to_double(&x, &mr, &dr, &fs);
790Sstevel@tonic-gate if (fs & ((1 << fp_overflow) | (1 << fp_underflow)))
800Sstevel@tonic-gate errno = ERANGE;
810Sstevel@tonic-gate return (x);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate float
wcstof(const wchar_t * cp,wchar_t ** ptr)850Sstevel@tonic-gate wcstof(const wchar_t *cp, wchar_t **ptr)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate float x;
880Sstevel@tonic-gate decimal_mode mr;
890Sstevel@tonic-gate decimal_record dr;
900Sstevel@tonic-gate fp_exception_field_type fs;
910Sstevel@tonic-gate int form;
920Sstevel@tonic-gate
930Sstevel@tonic-gate wstring_to_decimal(&cp, 1, &dr, &form);
940Sstevel@tonic-gate if (ptr != NULL)
950Sstevel@tonic-gate *ptr = (wchar_t *)cp;
960Sstevel@tonic-gate if (form == 0)
970Sstevel@tonic-gate return (0.0f);
980Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
990Sstevel@tonic-gate mr.rd = __xgetRD();
1000Sstevel@tonic-gate #elif defined(__sparc)
1010Sstevel@tonic-gate mr.rd = _QgetRD();
1020Sstevel@tonic-gate #else
1030Sstevel@tonic-gate #error Unknown architecture!
1040Sstevel@tonic-gate #endif
1050Sstevel@tonic-gate if (form < 0)
1060Sstevel@tonic-gate __hex_to_single(&dr, mr.rd, &x, &fs);
1070Sstevel@tonic-gate else
1080Sstevel@tonic-gate decimal_to_single(&x, &mr, &dr, &fs);
1090Sstevel@tonic-gate if (fs & ((1 << fp_overflow) | (1 << fp_underflow)))
1100Sstevel@tonic-gate errno = ERANGE;
1110Sstevel@tonic-gate return (x);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate long double
wcstold(const wchar_t * cp,wchar_t ** ptr)1150Sstevel@tonic-gate wcstold(const wchar_t *cp, wchar_t **ptr)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate long double x;
1180Sstevel@tonic-gate decimal_mode mr;
1190Sstevel@tonic-gate decimal_record dr;
1200Sstevel@tonic-gate fp_exception_field_type fs;
1210Sstevel@tonic-gate int form;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate wstring_to_decimal(&cp, 1, &dr, &form);
1240Sstevel@tonic-gate if (ptr != NULL)
1250Sstevel@tonic-gate *ptr = (wchar_t *)cp;
1260Sstevel@tonic-gate if (form == 0)
1270Sstevel@tonic-gate return (0.0L);
1280Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
1290Sstevel@tonic-gate mr.rd = __xgetRD();
1300Sstevel@tonic-gate if (form < 0)
1310Sstevel@tonic-gate __hex_to_extended(&dr, mr.rd, (extended *)&x, &fs);
1320Sstevel@tonic-gate else
1330Sstevel@tonic-gate decimal_to_extended((extended *)&x, &mr, &dr, &fs);
1340Sstevel@tonic-gate #elif defined(__sparc)
1350Sstevel@tonic-gate mr.rd = _QgetRD();
1360Sstevel@tonic-gate if (form < 0)
1370Sstevel@tonic-gate __hex_to_quadruple(&dr, mr.rd, &x, &fs);
1380Sstevel@tonic-gate else
1390Sstevel@tonic-gate decimal_to_quadruple(&x, &mr, &dr, &fs);
1400Sstevel@tonic-gate #else
1410Sstevel@tonic-gate #error Unknown architecture!
1420Sstevel@tonic-gate #endif
1430Sstevel@tonic-gate if (fs & ((1 << fp_overflow) | (1 << fp_underflow)))
1440Sstevel@tonic-gate errno = ERANGE;
1450Sstevel@tonic-gate return (x);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate double
wstod(const wchar_t * cp,wchar_t ** ptr)149*6812Sraf wstod(const wchar_t *cp, wchar_t **ptr)
1500Sstevel@tonic-gate {
151*6812Sraf return (wcstod(cp, ptr));
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate static const char *infstring = "INFINITY";
1550Sstevel@tonic-gate static const char *nanstring = "NAN";
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate * The following macro is applied to wchar_t arguments solely for the
1590Sstevel@tonic-gate * purpose of comparing the result with one of the characters in the
1600Sstevel@tonic-gate * strings above.
1610Sstevel@tonic-gate */
1620Sstevel@tonic-gate #define UCASE(c) (((L'a' <= c) && (c <= L'z'))? c - 32 : c)
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate * The following macro yields an expression that is true whenever
1660Sstevel@tonic-gate * the argument is a valid nonzero digit for the form being parsed.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate #define NZDIGIT(c) ((L'1' <= c && c <= L'9') || (form < 0 && \
1690Sstevel@tonic-gate ((L'a' <= c && c <= L'f') || (L'A' <= c && c <= L'F'))))
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate * wstring_to_decimal is modelled on string_to_decimal, the majority
1730Sstevel@tonic-gate * of which can be found in the common file char_to_decimal.h. The
1740Sstevel@tonic-gate * significant differences are:
1750Sstevel@tonic-gate *
1760Sstevel@tonic-gate * 1. This code recognizes only C99 (hex fp strings and restricted
1770Sstevel@tonic-gate * characters in parentheses following "nan") vs. C90 modes, no
1780Sstevel@tonic-gate * Fortran conventions.
1790Sstevel@tonic-gate *
1800Sstevel@tonic-gate * 2. *pform is an int rather than an enum decimal_string_form. On
1810Sstevel@tonic-gate * return, *pform == 0 if no valid token was found, *pform < 0
1820Sstevel@tonic-gate * if a C99 hex fp string was found, and *pform > 0 if a decimal
1830Sstevel@tonic-gate * string was found.
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate static void
wstring_to_decimal(const wchar_t ** ppc,int c99,decimal_record * pd,int * pform)1860Sstevel@tonic-gate wstring_to_decimal(const wchar_t **ppc, int c99, decimal_record *pd,
1870Sstevel@tonic-gate int *pform)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate const wchar_t *cp = *ppc; /* last character seen */
1900Sstevel@tonic-gate const wchar_t *good = cp - 1; /* last character accepted */
1910Sstevel@tonic-gate wchar_t current; /* always equal to *cp */
1920Sstevel@tonic-gate int sigfound;
1930Sstevel@tonic-gate int ids = 0;
1940Sstevel@tonic-gate int i, agree;
1950Sstevel@tonic-gate int nzbp = 0; /* number of zeros before point */
1960Sstevel@tonic-gate int nzap = 0; /* number of zeros after point */
1970Sstevel@tonic-gate char decpt;
1980Sstevel@tonic-gate int nfast, nfastlimit;
1990Sstevel@tonic-gate char *pfast;
2000Sstevel@tonic-gate int e, esign;
2010Sstevel@tonic-gate int expshift = 0;
2020Sstevel@tonic-gate int form;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate /*
2050Sstevel@tonic-gate * This routine assumes that the radix point is a single
2060Sstevel@tonic-gate * ASCII character, so that following this assignment, the
2070Sstevel@tonic-gate * condition (current == decpt) will correctly detect it.
2080Sstevel@tonic-gate */
2090Sstevel@tonic-gate decpt = *(localeconv()->decimal_point);
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate /* input is invalid until we find something */
2120Sstevel@tonic-gate pd->fpclass = fp_signaling;
2130Sstevel@tonic-gate pd->sign = 0;
2140Sstevel@tonic-gate pd->exponent = 0;
2150Sstevel@tonic-gate pd->ds[0] = '\0';
2160Sstevel@tonic-gate pd->more = 0;
2170Sstevel@tonic-gate pd->ndigits = 0;
2180Sstevel@tonic-gate *pform = form = 0;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate /* skip white space */
2210Sstevel@tonic-gate current = *cp;
2220Sstevel@tonic-gate while (iswspace((wint_t)current))
2230Sstevel@tonic-gate current = *++cp;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /* look for optional leading sign */
2260Sstevel@tonic-gate if (current == L'+') {
2270Sstevel@tonic-gate current = *++cp;
2280Sstevel@tonic-gate } else if (current == L'-') {
2290Sstevel@tonic-gate pd->sign = 1;
2300Sstevel@tonic-gate current = *++cp;
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate sigfound = -1; /* -1 = no digits found yet */
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate * Admissible first non-white-space, non-sign characters are
2370Sstevel@tonic-gate * 0-9, i, I, n, N, or the radix point.
2380Sstevel@tonic-gate */
2390Sstevel@tonic-gate if (L'1' <= current && current <= L'9') {
2400Sstevel@tonic-gate pd->fpclass = fp_normal;
2410Sstevel@tonic-gate form = 1;
2420Sstevel@tonic-gate good = cp;
2430Sstevel@tonic-gate sigfound = 1; /* 1 = significant digits found */
2440Sstevel@tonic-gate pd->ds[ids++] = (char)current;
2450Sstevel@tonic-gate current = *++cp;
2460Sstevel@tonic-gate } else {
2470Sstevel@tonic-gate switch (current) {
2480Sstevel@tonic-gate case L'0':
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * Accept the leading zero and set pd->fpclass
2510Sstevel@tonic-gate * accordingly, but don't set sigfound until we
2520Sstevel@tonic-gate * determine that this isn't a "fake" hex string
2530Sstevel@tonic-gate * (i.e., 0x.p...).
2540Sstevel@tonic-gate */
2550Sstevel@tonic-gate good = cp;
2560Sstevel@tonic-gate pd->fpclass = fp_zero;
2570Sstevel@tonic-gate if (c99) {
2580Sstevel@tonic-gate /* look for a hex fp string */
2590Sstevel@tonic-gate current = *++cp;
2600Sstevel@tonic-gate if (current == L'X' || current == L'x') {
2610Sstevel@tonic-gate /* assume hex fp form */
2620Sstevel@tonic-gate form = -1;
2630Sstevel@tonic-gate expshift = 2;
2640Sstevel@tonic-gate current = *++cp;
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate * Only a digit or radix point can
2670Sstevel@tonic-gate * follow "0x".
2680Sstevel@tonic-gate */
2690Sstevel@tonic-gate if (NZDIGIT(current)) {
2700Sstevel@tonic-gate pd->fpclass = fp_normal;
2710Sstevel@tonic-gate good = cp;
2720Sstevel@tonic-gate sigfound = 1;
2730Sstevel@tonic-gate pd->ds[ids++] = (char)current;
2740Sstevel@tonic-gate current = *++cp;
2750Sstevel@tonic-gate break;
2760Sstevel@tonic-gate } else if (current == (wchar_t)decpt) {
2770Sstevel@tonic-gate current = *++cp;
2780Sstevel@tonic-gate goto afterpoint;
2790Sstevel@tonic-gate } else if (current != L'0') {
2800Sstevel@tonic-gate /* not hex fp after all */
2810Sstevel@tonic-gate form = 1;
2820Sstevel@tonic-gate expshift = 0;
2830Sstevel@tonic-gate goto done;
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate } else {
2860Sstevel@tonic-gate form = 1;
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate } else {
2890Sstevel@tonic-gate form = 1;
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /* skip all leading zeros */
2930Sstevel@tonic-gate while (current == L'0')
2940Sstevel@tonic-gate current = *++cp;
2950Sstevel@tonic-gate good = cp - 1;
2960Sstevel@tonic-gate sigfound = 0; /* 0 = only zeros found so far */
2970Sstevel@tonic-gate break;
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate case L'i':
3000Sstevel@tonic-gate case L'I':
3010Sstevel@tonic-gate /* look for inf or infinity */
3020Sstevel@tonic-gate current = *++cp;
3030Sstevel@tonic-gate agree = 1;
3040Sstevel@tonic-gate while (agree <= 7 &&
3050Sstevel@tonic-gate UCASE(current) == (wchar_t)infstring[agree]) {
3060Sstevel@tonic-gate current = *++cp;
3070Sstevel@tonic-gate agree++;
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate if (agree >= 3) {
3100Sstevel@tonic-gate /* found valid infinity */
3110Sstevel@tonic-gate pd->fpclass = fp_infinity;
3120Sstevel@tonic-gate form = 1;
3130Sstevel@tonic-gate good = (agree < 8)? cp + 2 - agree : cp - 1;
3140Sstevel@tonic-gate __inf_read = 1;
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate goto done;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate case L'n':
3190Sstevel@tonic-gate case L'N':
3200Sstevel@tonic-gate /* look for nan or nan(string) */
3210Sstevel@tonic-gate current = *++cp;
3220Sstevel@tonic-gate agree = 1;
3230Sstevel@tonic-gate while (agree <= 2 &&
3240Sstevel@tonic-gate UCASE(current) == (wchar_t)nanstring[agree]) {
3250Sstevel@tonic-gate current = *++cp;
3260Sstevel@tonic-gate agree++;
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate if (agree == 3) {
3290Sstevel@tonic-gate /* found valid NaN */
3300Sstevel@tonic-gate pd->fpclass = fp_quiet;
3310Sstevel@tonic-gate form = 1;
3320Sstevel@tonic-gate good = cp - 1;
3330Sstevel@tonic-gate __nan_read = 1;
3340Sstevel@tonic-gate if (current == L'(') {
3350Sstevel@tonic-gate /* accept parenthesized string */
3360Sstevel@tonic-gate if (c99) {
3370Sstevel@tonic-gate do {
3380Sstevel@tonic-gate current = *++cp;
3390Sstevel@tonic-gate } while (iswalnum(current) ||
3400Sstevel@tonic-gate current == L'_');
3410Sstevel@tonic-gate } else {
3420Sstevel@tonic-gate do {
3430Sstevel@tonic-gate current = *++cp;
3440Sstevel@tonic-gate } while (current &&
3450Sstevel@tonic-gate current != L')');
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate if (current == L')')
3480Sstevel@tonic-gate good = cp;
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate goto done;
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate default:
3540Sstevel@tonic-gate if (current == (wchar_t)decpt) {
3550Sstevel@tonic-gate /*
3560Sstevel@tonic-gate * Don't accept the radix point just yet;
3570Sstevel@tonic-gate * we need to see at least one digit.
3580Sstevel@tonic-gate */
3590Sstevel@tonic-gate current = *++cp;
3600Sstevel@tonic-gate goto afterpoint;
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate goto done;
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate nextnumber:
3670Sstevel@tonic-gate /*
3680Sstevel@tonic-gate * Admissible characters after the first digit are a valid
3690Sstevel@tonic-gate * digit, an exponent delimiter (E or e for decimal form,
3700Sstevel@tonic-gate * P or p for hex form), or the radix point. (Note that we
3710Sstevel@tonic-gate * can't get here unless we've already found a digit.)
3720Sstevel@tonic-gate */
3730Sstevel@tonic-gate if (NZDIGIT(current)) {
3740Sstevel@tonic-gate /*
3750Sstevel@tonic-gate * Found another nonzero digit. If there's enough room
3760Sstevel@tonic-gate * in pd->ds, store any intervening zeros we've found so far
3770Sstevel@tonic-gate * and then store this digit. Otherwise, stop storing
3780Sstevel@tonic-gate * digits in pd->ds and set pd->more.
3790Sstevel@tonic-gate */
3800Sstevel@tonic-gate if (ids + nzbp + 2 < DECIMAL_STRING_LENGTH) {
3810Sstevel@tonic-gate for (i = 0; i < nzbp; i++)
3820Sstevel@tonic-gate pd->ds[ids++] = '0';
3830Sstevel@tonic-gate pd->ds[ids++] = (char)current;
3840Sstevel@tonic-gate } else {
3850Sstevel@tonic-gate pd->exponent += (nzbp + 1) << expshift;
3860Sstevel@tonic-gate pd->more = 1;
3870Sstevel@tonic-gate if (ids < DECIMAL_STRING_LENGTH) {
3880Sstevel@tonic-gate pd->ds[ids] = '\0';
3890Sstevel@tonic-gate pd->ndigits = ids;
3900Sstevel@tonic-gate /* don't store any more digits */
3910Sstevel@tonic-gate ids = DECIMAL_STRING_LENGTH;
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate pd->fpclass = fp_normal;
3950Sstevel@tonic-gate sigfound = 1;
3960Sstevel@tonic-gate nzbp = 0;
3970Sstevel@tonic-gate current = *++cp;
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate * Use an optimized loop to grab a consecutive sequence
4010Sstevel@tonic-gate * of nonzero digits quickly.
4020Sstevel@tonic-gate */
4030Sstevel@tonic-gate nfastlimit = DECIMAL_STRING_LENGTH - 3 - ids;
4040Sstevel@tonic-gate for (nfast = 0, pfast = &(pd->ds[ids]);
4050Sstevel@tonic-gate nfast < nfastlimit && NZDIGIT(current);
4060Sstevel@tonic-gate nfast++) {
4070Sstevel@tonic-gate *pfast++ = (char)current;
4080Sstevel@tonic-gate current = *++cp;
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate ids += nfast;
4110Sstevel@tonic-gate if (current == L'0')
4120Sstevel@tonic-gate goto nextnumberzero; /* common case */
4130Sstevel@tonic-gate /* advance good to the last accepted digit */
4140Sstevel@tonic-gate good = cp - 1;
4150Sstevel@tonic-gate goto nextnumber;
4160Sstevel@tonic-gate } else {
4170Sstevel@tonic-gate switch (current) {
4180Sstevel@tonic-gate case L'0':
4190Sstevel@tonic-gate nextnumberzero:
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate * Count zeros before the radix point. Later we
4220Sstevel@tonic-gate * will either put these zeros into pd->ds or add
4230Sstevel@tonic-gate * nzbp to pd->exponent to account for them.
4240Sstevel@tonic-gate */
4250Sstevel@tonic-gate while (current == L'0') {
4260Sstevel@tonic-gate nzbp++;
4270Sstevel@tonic-gate current = *++cp;
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate good = cp - 1;
4300Sstevel@tonic-gate goto nextnumber;
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate case L'E':
4330Sstevel@tonic-gate case L'e':
4340Sstevel@tonic-gate if (form < 0)
4350Sstevel@tonic-gate goto done;
4360Sstevel@tonic-gate goto exponent;
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate case L'P':
4390Sstevel@tonic-gate case L'p':
4400Sstevel@tonic-gate if (form > 0)
4410Sstevel@tonic-gate goto done;
4420Sstevel@tonic-gate goto exponent;
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate default:
4450Sstevel@tonic-gate if (current == decpt) {
4460Sstevel@tonic-gate /* accept the radix point */
4470Sstevel@tonic-gate good = cp;
4480Sstevel@tonic-gate current = *++cp;
4490Sstevel@tonic-gate goto afterpoint;
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate goto done;
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate afterpoint:
4560Sstevel@tonic-gate /*
4570Sstevel@tonic-gate * Admissible characters after the radix point are a valid digit
4580Sstevel@tonic-gate * or an exponent delimiter. (Note that it is possible to get
4590Sstevel@tonic-gate * here even though we haven't found any digits yet.)
4600Sstevel@tonic-gate */
4610Sstevel@tonic-gate if (NZDIGIT(current)) {
4620Sstevel@tonic-gate if (form == 0)
4630Sstevel@tonic-gate form = 1;
4640Sstevel@tonic-gate if (sigfound < 1) {
4650Sstevel@tonic-gate /* no significant digits found until now */
4660Sstevel@tonic-gate pd->fpclass = fp_normal;
4670Sstevel@tonic-gate sigfound = 1;
4680Sstevel@tonic-gate pd->ds[ids++] = (char)current;
4690Sstevel@tonic-gate pd->exponent = (-(nzap + 1)) << expshift;
4700Sstevel@tonic-gate } else {
4710Sstevel@tonic-gate /* significant digits have been found */
4720Sstevel@tonic-gate if (ids + nzbp + nzap + 2 < DECIMAL_STRING_LENGTH) {
4730Sstevel@tonic-gate for (i = 0; i < nzbp + nzap; i++)
4740Sstevel@tonic-gate pd->ds[ids++] = '0';
4750Sstevel@tonic-gate pd->ds[ids++] = (char)current;
4760Sstevel@tonic-gate pd->exponent -= (nzap + 1) << expshift;
4770Sstevel@tonic-gate } else {
4780Sstevel@tonic-gate pd->exponent += nzbp << expshift;
4790Sstevel@tonic-gate pd->more = 1;
4800Sstevel@tonic-gate if (ids < DECIMAL_STRING_LENGTH) {
4810Sstevel@tonic-gate pd->ds[ids] = '\0';
4820Sstevel@tonic-gate pd->ndigits = ids;
4830Sstevel@tonic-gate /* don't store any more digits */
4840Sstevel@tonic-gate ids = DECIMAL_STRING_LENGTH;
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate nzbp = 0;
4890Sstevel@tonic-gate nzap = 0;
4900Sstevel@tonic-gate current = *++cp;
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate /*
4930Sstevel@tonic-gate * Use an optimized loop to grab a consecutive sequence
4940Sstevel@tonic-gate * of nonzero digits quickly.
4950Sstevel@tonic-gate */
4960Sstevel@tonic-gate nfastlimit = DECIMAL_STRING_LENGTH - 3 - ids;
4970Sstevel@tonic-gate for (nfast = 0, pfast = &(pd->ds[ids]);
4980Sstevel@tonic-gate nfast < nfastlimit && NZDIGIT(current);
4990Sstevel@tonic-gate nfast++) {
5000Sstevel@tonic-gate *pfast++ = (char)current;
5010Sstevel@tonic-gate current = *++cp;
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate ids += nfast;
5040Sstevel@tonic-gate pd->exponent -= nfast << expshift;
5050Sstevel@tonic-gate if (current == L'0')
5060Sstevel@tonic-gate goto zeroafterpoint;
5070Sstevel@tonic-gate /* advance good to the last accepted digit */
5080Sstevel@tonic-gate good = cp - 1;
5090Sstevel@tonic-gate goto afterpoint;
5100Sstevel@tonic-gate } else {
5110Sstevel@tonic-gate switch (current) {
5120Sstevel@tonic-gate case L'0':
5130Sstevel@tonic-gate if (form == 0)
5140Sstevel@tonic-gate form = 1;
5150Sstevel@tonic-gate if (sigfound == -1) {
5160Sstevel@tonic-gate pd->fpclass = fp_zero;
5170Sstevel@tonic-gate sigfound = 0;
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate zeroafterpoint:
5200Sstevel@tonic-gate /*
5210Sstevel@tonic-gate * Count zeros after the radix point. If we find
5220Sstevel@tonic-gate * any more nonzero digits later, we will put these
5230Sstevel@tonic-gate * zeros into pd->ds and decrease pd->exponent by
5240Sstevel@tonic-gate * nzap.
5250Sstevel@tonic-gate */
5260Sstevel@tonic-gate while (current == L'0') {
5270Sstevel@tonic-gate nzap++;
5280Sstevel@tonic-gate current = *++cp;
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate good = cp - 1;
5310Sstevel@tonic-gate goto afterpoint;
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate case L'E':
5340Sstevel@tonic-gate case L'e':
5350Sstevel@tonic-gate /* don't accept exponent without preceding digits */
5360Sstevel@tonic-gate if (sigfound == -1 || form < 0)
5370Sstevel@tonic-gate goto done;
5380Sstevel@tonic-gate break;
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate case L'P':
5410Sstevel@tonic-gate case L'p':
5420Sstevel@tonic-gate /* don't accept exponent without preceding digits */
5430Sstevel@tonic-gate if (sigfound == -1 || form > 0)
5440Sstevel@tonic-gate goto done;
5450Sstevel@tonic-gate break;
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate default:
5480Sstevel@tonic-gate goto done;
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate exponent:
5530Sstevel@tonic-gate e = 0;
5540Sstevel@tonic-gate esign = 0;
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate /* look for optional exponent sign */
5570Sstevel@tonic-gate current = *++cp;
5580Sstevel@tonic-gate if (current == L'+') {
5590Sstevel@tonic-gate current = *++cp;
5600Sstevel@tonic-gate } else if (current == L'-') {
5610Sstevel@tonic-gate esign = 1;
5620Sstevel@tonic-gate current = *++cp;
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate * Accumulate explicit exponent. Note that if we don't find at
5670Sstevel@tonic-gate * least one digit, good won't be updated and e will remain 0.
5680Sstevel@tonic-gate * Also, we keep e from getting too large so we don't overflow
5690Sstevel@tonic-gate * the range of int (but notice that the threshold is large
5700Sstevel@tonic-gate * enough that any larger e would cause the result to underflow
5710Sstevel@tonic-gate * or overflow anyway).
5720Sstevel@tonic-gate */
5730Sstevel@tonic-gate while (L'0' <= current && current <= L'9') {
5740Sstevel@tonic-gate good = cp;
5750Sstevel@tonic-gate if (e <= 1000000)
5760Sstevel@tonic-gate e = 10 * e + current - L'0';
5770Sstevel@tonic-gate current = *++cp;
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate if (esign)
5800Sstevel@tonic-gate pd->exponent -= e;
5810Sstevel@tonic-gate else
5820Sstevel@tonic-gate pd->exponent += e;
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate done:
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate * If we found any zeros before the radix point that were not
5870Sstevel@tonic-gate * accounted for earlier, adjust the exponent. (This is only
5880Sstevel@tonic-gate * relevant when pd->fpclass == fp_normal, but it's harmless
5890Sstevel@tonic-gate * in all other cases.)
5900Sstevel@tonic-gate */
5910Sstevel@tonic-gate pd->exponent += nzbp << expshift;
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate /* terminate pd->ds if we haven't already */
5940Sstevel@tonic-gate if (ids < DECIMAL_STRING_LENGTH) {
5950Sstevel@tonic-gate pd->ds[ids] = '\0';
5960Sstevel@tonic-gate pd->ndigits = ids;
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate /*
6000Sstevel@tonic-gate * If we accepted any characters, advance *ppc to point to the
6010Sstevel@tonic-gate * first character we didn't accept; otherwise, pass back a
6020Sstevel@tonic-gate * signaling nan.
6030Sstevel@tonic-gate */
6040Sstevel@tonic-gate if (good >= *ppc) {
6050Sstevel@tonic-gate *ppc = good + 1;
6060Sstevel@tonic-gate } else {
6070Sstevel@tonic-gate pd->fpclass = fp_signaling;
6080Sstevel@tonic-gate pd->sign = 0;
6090Sstevel@tonic-gate form = 0;
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate *pform = form;
6130Sstevel@tonic-gate }
614