1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 30*0Sstevel@tonic-gate /* All Rights Reserved */ 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate /* 34*0Sstevel@tonic-gate * This file is based on /usr/src/lib/libc/port/gen/strtod.c and 35*0Sstevel@tonic-gate * /usr/src/lib/libc/sparc/fp/string_decim.c 36*0Sstevel@tonic-gate */ 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate #pragma weak wcstod = _wcstod 39*0Sstevel@tonic-gate #pragma weak wstod = _wstod 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #include "synonyms.h" 42*0Sstevel@tonic-gate #include <errno.h> 43*0Sstevel@tonic-gate #include <stdio.h> 44*0Sstevel@tonic-gate #include <values.h> 45*0Sstevel@tonic-gate #include <floatingpoint.h> 46*0Sstevel@tonic-gate #include <stddef.h> 47*0Sstevel@tonic-gate #include <wctype.h> 48*0Sstevel@tonic-gate #include "base_conversion.h" /* from usr/src/lib/libc/inc */ 49*0Sstevel@tonic-gate #include <locale.h> 50*0Sstevel@tonic-gate #include "libc.h" 51*0Sstevel@tonic-gate #include "xpg6.h" 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate static void wstring_to_decimal(const wchar_t **, int, decimal_record *, int *); 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate double 56*0Sstevel@tonic-gate _wcstod(const wchar_t *cp, wchar_t **ptr) 57*0Sstevel@tonic-gate { 58*0Sstevel@tonic-gate double x; 59*0Sstevel@tonic-gate decimal_mode mr; 60*0Sstevel@tonic-gate decimal_record dr; 61*0Sstevel@tonic-gate fp_exception_field_type fs; 62*0Sstevel@tonic-gate int form; 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate wstring_to_decimal(&cp, __xpg6 & _C99SUSv3_recognize_hexfp, &dr, &form); 65*0Sstevel@tonic-gate if (ptr != NULL) 66*0Sstevel@tonic-gate *ptr = (wchar_t *)cp; 67*0Sstevel@tonic-gate if (form == 0) 68*0Sstevel@tonic-gate return (0.0); /* Shameful kluge for SVID's sake. */ 69*0Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 70*0Sstevel@tonic-gate mr.rd = __xgetRD(); 71*0Sstevel@tonic-gate #elif defined(__sparc) 72*0Sstevel@tonic-gate mr.rd = _QgetRD(); 73*0Sstevel@tonic-gate #else 74*0Sstevel@tonic-gate #error Unknown architecture! 75*0Sstevel@tonic-gate #endif 76*0Sstevel@tonic-gate if (form < 0) 77*0Sstevel@tonic-gate __hex_to_double(&dr, mr.rd, &x, &fs); 78*0Sstevel@tonic-gate else 79*0Sstevel@tonic-gate decimal_to_double(&x, &mr, &dr, &fs); 80*0Sstevel@tonic-gate if (fs & ((1 << fp_overflow) | (1 << fp_underflow))) 81*0Sstevel@tonic-gate errno = ERANGE; 82*0Sstevel@tonic-gate return (x); 83*0Sstevel@tonic-gate } 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate float 86*0Sstevel@tonic-gate wcstof(const wchar_t *cp, wchar_t **ptr) 87*0Sstevel@tonic-gate { 88*0Sstevel@tonic-gate float x; 89*0Sstevel@tonic-gate decimal_mode mr; 90*0Sstevel@tonic-gate decimal_record dr; 91*0Sstevel@tonic-gate fp_exception_field_type fs; 92*0Sstevel@tonic-gate int form; 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate wstring_to_decimal(&cp, 1, &dr, &form); 95*0Sstevel@tonic-gate if (ptr != NULL) 96*0Sstevel@tonic-gate *ptr = (wchar_t *)cp; 97*0Sstevel@tonic-gate if (form == 0) 98*0Sstevel@tonic-gate return (0.0f); 99*0Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 100*0Sstevel@tonic-gate mr.rd = __xgetRD(); 101*0Sstevel@tonic-gate #elif defined(__sparc) 102*0Sstevel@tonic-gate mr.rd = _QgetRD(); 103*0Sstevel@tonic-gate #else 104*0Sstevel@tonic-gate #error Unknown architecture! 105*0Sstevel@tonic-gate #endif 106*0Sstevel@tonic-gate if (form < 0) 107*0Sstevel@tonic-gate __hex_to_single(&dr, mr.rd, &x, &fs); 108*0Sstevel@tonic-gate else 109*0Sstevel@tonic-gate decimal_to_single(&x, &mr, &dr, &fs); 110*0Sstevel@tonic-gate if (fs & ((1 << fp_overflow) | (1 << fp_underflow))) 111*0Sstevel@tonic-gate errno = ERANGE; 112*0Sstevel@tonic-gate return (x); 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate long double 116*0Sstevel@tonic-gate wcstold(const wchar_t *cp, wchar_t **ptr) 117*0Sstevel@tonic-gate { 118*0Sstevel@tonic-gate long double x; 119*0Sstevel@tonic-gate decimal_mode mr; 120*0Sstevel@tonic-gate decimal_record dr; 121*0Sstevel@tonic-gate fp_exception_field_type fs; 122*0Sstevel@tonic-gate int form; 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate wstring_to_decimal(&cp, 1, &dr, &form); 125*0Sstevel@tonic-gate if (ptr != NULL) 126*0Sstevel@tonic-gate *ptr = (wchar_t *)cp; 127*0Sstevel@tonic-gate if (form == 0) 128*0Sstevel@tonic-gate return (0.0L); 129*0Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 130*0Sstevel@tonic-gate mr.rd = __xgetRD(); 131*0Sstevel@tonic-gate if (form < 0) 132*0Sstevel@tonic-gate __hex_to_extended(&dr, mr.rd, (extended *)&x, &fs); 133*0Sstevel@tonic-gate else 134*0Sstevel@tonic-gate decimal_to_extended((extended *)&x, &mr, &dr, &fs); 135*0Sstevel@tonic-gate #elif defined(__sparc) 136*0Sstevel@tonic-gate mr.rd = _QgetRD(); 137*0Sstevel@tonic-gate if (form < 0) 138*0Sstevel@tonic-gate __hex_to_quadruple(&dr, mr.rd, &x, &fs); 139*0Sstevel@tonic-gate else 140*0Sstevel@tonic-gate decimal_to_quadruple(&x, &mr, &dr, &fs); 141*0Sstevel@tonic-gate #else 142*0Sstevel@tonic-gate #error Unknown architecture! 143*0Sstevel@tonic-gate #endif 144*0Sstevel@tonic-gate if (fs & ((1 << fp_overflow) | (1 << fp_underflow))) 145*0Sstevel@tonic-gate errno = ERANGE; 146*0Sstevel@tonic-gate return (x); 147*0Sstevel@tonic-gate } 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate double 150*0Sstevel@tonic-gate _wstod(const wchar_t *cp, wchar_t **ptr) 151*0Sstevel@tonic-gate { 152*0Sstevel@tonic-gate return (_wcstod(cp, ptr)); 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate static const char *infstring = "INFINITY"; 156*0Sstevel@tonic-gate static const char *nanstring = "NAN"; 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate /* 159*0Sstevel@tonic-gate * The following macro is applied to wchar_t arguments solely for the 160*0Sstevel@tonic-gate * purpose of comparing the result with one of the characters in the 161*0Sstevel@tonic-gate * strings above. 162*0Sstevel@tonic-gate */ 163*0Sstevel@tonic-gate #define UCASE(c) (((L'a' <= c) && (c <= L'z'))? c - 32 : c) 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate /* 166*0Sstevel@tonic-gate * The following macro yields an expression that is true whenever 167*0Sstevel@tonic-gate * the argument is a valid nonzero digit for the form being parsed. 168*0Sstevel@tonic-gate */ 169*0Sstevel@tonic-gate #define NZDIGIT(c) ((L'1' <= c && c <= L'9') || (form < 0 && \ 170*0Sstevel@tonic-gate ((L'a' <= c && c <= L'f') || (L'A' <= c && c <= L'F')))) 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate /* 173*0Sstevel@tonic-gate * wstring_to_decimal is modelled on string_to_decimal, the majority 174*0Sstevel@tonic-gate * of which can be found in the common file char_to_decimal.h. The 175*0Sstevel@tonic-gate * significant differences are: 176*0Sstevel@tonic-gate * 177*0Sstevel@tonic-gate * 1. This code recognizes only C99 (hex fp strings and restricted 178*0Sstevel@tonic-gate * characters in parentheses following "nan") vs. C90 modes, no 179*0Sstevel@tonic-gate * Fortran conventions. 180*0Sstevel@tonic-gate * 181*0Sstevel@tonic-gate * 2. *pform is an int rather than an enum decimal_string_form. On 182*0Sstevel@tonic-gate * return, *pform == 0 if no valid token was found, *pform < 0 183*0Sstevel@tonic-gate * if a C99 hex fp string was found, and *pform > 0 if a decimal 184*0Sstevel@tonic-gate * string was found. 185*0Sstevel@tonic-gate */ 186*0Sstevel@tonic-gate static void 187*0Sstevel@tonic-gate wstring_to_decimal(const wchar_t **ppc, int c99, decimal_record *pd, 188*0Sstevel@tonic-gate int *pform) 189*0Sstevel@tonic-gate { 190*0Sstevel@tonic-gate const wchar_t *cp = *ppc; /* last character seen */ 191*0Sstevel@tonic-gate const wchar_t *good = cp - 1; /* last character accepted */ 192*0Sstevel@tonic-gate wchar_t current; /* always equal to *cp */ 193*0Sstevel@tonic-gate int sigfound; 194*0Sstevel@tonic-gate int ids = 0; 195*0Sstevel@tonic-gate int i, agree; 196*0Sstevel@tonic-gate int nzbp = 0; /* number of zeros before point */ 197*0Sstevel@tonic-gate int nzap = 0; /* number of zeros after point */ 198*0Sstevel@tonic-gate char decpt; 199*0Sstevel@tonic-gate int nfast, nfastlimit; 200*0Sstevel@tonic-gate char *pfast; 201*0Sstevel@tonic-gate int e, esign; 202*0Sstevel@tonic-gate int expshift = 0; 203*0Sstevel@tonic-gate int form; 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate /* 206*0Sstevel@tonic-gate * This routine assumes that the radix point is a single 207*0Sstevel@tonic-gate * ASCII character, so that following this assignment, the 208*0Sstevel@tonic-gate * condition (current == decpt) will correctly detect it. 209*0Sstevel@tonic-gate */ 210*0Sstevel@tonic-gate decpt = *(localeconv()->decimal_point); 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate /* input is invalid until we find something */ 213*0Sstevel@tonic-gate pd->fpclass = fp_signaling; 214*0Sstevel@tonic-gate pd->sign = 0; 215*0Sstevel@tonic-gate pd->exponent = 0; 216*0Sstevel@tonic-gate pd->ds[0] = '\0'; 217*0Sstevel@tonic-gate pd->more = 0; 218*0Sstevel@tonic-gate pd->ndigits = 0; 219*0Sstevel@tonic-gate *pform = form = 0; 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate /* skip white space */ 222*0Sstevel@tonic-gate current = *cp; 223*0Sstevel@tonic-gate while (iswspace((wint_t)current)) 224*0Sstevel@tonic-gate current = *++cp; 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate /* look for optional leading sign */ 227*0Sstevel@tonic-gate if (current == L'+') { 228*0Sstevel@tonic-gate current = *++cp; 229*0Sstevel@tonic-gate } else if (current == L'-') { 230*0Sstevel@tonic-gate pd->sign = 1; 231*0Sstevel@tonic-gate current = *++cp; 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate sigfound = -1; /* -1 = no digits found yet */ 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate /* 237*0Sstevel@tonic-gate * Admissible first non-white-space, non-sign characters are 238*0Sstevel@tonic-gate * 0-9, i, I, n, N, or the radix point. 239*0Sstevel@tonic-gate */ 240*0Sstevel@tonic-gate if (L'1' <= current && current <= L'9') { 241*0Sstevel@tonic-gate pd->fpclass = fp_normal; 242*0Sstevel@tonic-gate form = 1; 243*0Sstevel@tonic-gate good = cp; 244*0Sstevel@tonic-gate sigfound = 1; /* 1 = significant digits found */ 245*0Sstevel@tonic-gate pd->ds[ids++] = (char)current; 246*0Sstevel@tonic-gate current = *++cp; 247*0Sstevel@tonic-gate } else { 248*0Sstevel@tonic-gate switch (current) { 249*0Sstevel@tonic-gate case L'0': 250*0Sstevel@tonic-gate /* 251*0Sstevel@tonic-gate * Accept the leading zero and set pd->fpclass 252*0Sstevel@tonic-gate * accordingly, but don't set sigfound until we 253*0Sstevel@tonic-gate * determine that this isn't a "fake" hex string 254*0Sstevel@tonic-gate * (i.e., 0x.p...). 255*0Sstevel@tonic-gate */ 256*0Sstevel@tonic-gate good = cp; 257*0Sstevel@tonic-gate pd->fpclass = fp_zero; 258*0Sstevel@tonic-gate if (c99) { 259*0Sstevel@tonic-gate /* look for a hex fp string */ 260*0Sstevel@tonic-gate current = *++cp; 261*0Sstevel@tonic-gate if (current == L'X' || current == L'x') { 262*0Sstevel@tonic-gate /* assume hex fp form */ 263*0Sstevel@tonic-gate form = -1; 264*0Sstevel@tonic-gate expshift = 2; 265*0Sstevel@tonic-gate current = *++cp; 266*0Sstevel@tonic-gate /* 267*0Sstevel@tonic-gate * Only a digit or radix point can 268*0Sstevel@tonic-gate * follow "0x". 269*0Sstevel@tonic-gate */ 270*0Sstevel@tonic-gate if (NZDIGIT(current)) { 271*0Sstevel@tonic-gate pd->fpclass = fp_normal; 272*0Sstevel@tonic-gate good = cp; 273*0Sstevel@tonic-gate sigfound = 1; 274*0Sstevel@tonic-gate pd->ds[ids++] = (char)current; 275*0Sstevel@tonic-gate current = *++cp; 276*0Sstevel@tonic-gate break; 277*0Sstevel@tonic-gate } else if (current == (wchar_t)decpt) { 278*0Sstevel@tonic-gate current = *++cp; 279*0Sstevel@tonic-gate goto afterpoint; 280*0Sstevel@tonic-gate } else if (current != L'0') { 281*0Sstevel@tonic-gate /* not hex fp after all */ 282*0Sstevel@tonic-gate form = 1; 283*0Sstevel@tonic-gate expshift = 0; 284*0Sstevel@tonic-gate goto done; 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate } else { 287*0Sstevel@tonic-gate form = 1; 288*0Sstevel@tonic-gate } 289*0Sstevel@tonic-gate } else { 290*0Sstevel@tonic-gate form = 1; 291*0Sstevel@tonic-gate } 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate /* skip all leading zeros */ 294*0Sstevel@tonic-gate while (current == L'0') 295*0Sstevel@tonic-gate current = *++cp; 296*0Sstevel@tonic-gate good = cp - 1; 297*0Sstevel@tonic-gate sigfound = 0; /* 0 = only zeros found so far */ 298*0Sstevel@tonic-gate break; 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate case L'i': 301*0Sstevel@tonic-gate case L'I': 302*0Sstevel@tonic-gate /* look for inf or infinity */ 303*0Sstevel@tonic-gate current = *++cp; 304*0Sstevel@tonic-gate agree = 1; 305*0Sstevel@tonic-gate while (agree <= 7 && 306*0Sstevel@tonic-gate UCASE(current) == (wchar_t)infstring[agree]) { 307*0Sstevel@tonic-gate current = *++cp; 308*0Sstevel@tonic-gate agree++; 309*0Sstevel@tonic-gate } 310*0Sstevel@tonic-gate if (agree >= 3) { 311*0Sstevel@tonic-gate /* found valid infinity */ 312*0Sstevel@tonic-gate pd->fpclass = fp_infinity; 313*0Sstevel@tonic-gate form = 1; 314*0Sstevel@tonic-gate good = (agree < 8)? cp + 2 - agree : cp - 1; 315*0Sstevel@tonic-gate __inf_read = 1; 316*0Sstevel@tonic-gate } 317*0Sstevel@tonic-gate goto done; 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate case L'n': 320*0Sstevel@tonic-gate case L'N': 321*0Sstevel@tonic-gate /* look for nan or nan(string) */ 322*0Sstevel@tonic-gate current = *++cp; 323*0Sstevel@tonic-gate agree = 1; 324*0Sstevel@tonic-gate while (agree <= 2 && 325*0Sstevel@tonic-gate UCASE(current) == (wchar_t)nanstring[agree]) { 326*0Sstevel@tonic-gate current = *++cp; 327*0Sstevel@tonic-gate agree++; 328*0Sstevel@tonic-gate } 329*0Sstevel@tonic-gate if (agree == 3) { 330*0Sstevel@tonic-gate /* found valid NaN */ 331*0Sstevel@tonic-gate pd->fpclass = fp_quiet; 332*0Sstevel@tonic-gate form = 1; 333*0Sstevel@tonic-gate good = cp - 1; 334*0Sstevel@tonic-gate __nan_read = 1; 335*0Sstevel@tonic-gate if (current == L'(') { 336*0Sstevel@tonic-gate /* accept parenthesized string */ 337*0Sstevel@tonic-gate if (c99) { 338*0Sstevel@tonic-gate do { 339*0Sstevel@tonic-gate current = *++cp; 340*0Sstevel@tonic-gate } while (iswalnum(current) || 341*0Sstevel@tonic-gate current == L'_'); 342*0Sstevel@tonic-gate } else { 343*0Sstevel@tonic-gate do { 344*0Sstevel@tonic-gate current = *++cp; 345*0Sstevel@tonic-gate } while (current && 346*0Sstevel@tonic-gate current != L')'); 347*0Sstevel@tonic-gate } 348*0Sstevel@tonic-gate if (current == L')') 349*0Sstevel@tonic-gate good = cp; 350*0Sstevel@tonic-gate } 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate goto done; 353*0Sstevel@tonic-gate 354*0Sstevel@tonic-gate default: 355*0Sstevel@tonic-gate if (current == (wchar_t)decpt) { 356*0Sstevel@tonic-gate /* 357*0Sstevel@tonic-gate * Don't accept the radix point just yet; 358*0Sstevel@tonic-gate * we need to see at least one digit. 359*0Sstevel@tonic-gate */ 360*0Sstevel@tonic-gate current = *++cp; 361*0Sstevel@tonic-gate goto afterpoint; 362*0Sstevel@tonic-gate } 363*0Sstevel@tonic-gate goto done; 364*0Sstevel@tonic-gate } 365*0Sstevel@tonic-gate } 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gate nextnumber: 368*0Sstevel@tonic-gate /* 369*0Sstevel@tonic-gate * Admissible characters after the first digit are a valid 370*0Sstevel@tonic-gate * digit, an exponent delimiter (E or e for decimal form, 371*0Sstevel@tonic-gate * P or p for hex form), or the radix point. (Note that we 372*0Sstevel@tonic-gate * can't get here unless we've already found a digit.) 373*0Sstevel@tonic-gate */ 374*0Sstevel@tonic-gate if (NZDIGIT(current)) { 375*0Sstevel@tonic-gate /* 376*0Sstevel@tonic-gate * Found another nonzero digit. If there's enough room 377*0Sstevel@tonic-gate * in pd->ds, store any intervening zeros we've found so far 378*0Sstevel@tonic-gate * and then store this digit. Otherwise, stop storing 379*0Sstevel@tonic-gate * digits in pd->ds and set pd->more. 380*0Sstevel@tonic-gate */ 381*0Sstevel@tonic-gate if (ids + nzbp + 2 < DECIMAL_STRING_LENGTH) { 382*0Sstevel@tonic-gate for (i = 0; i < nzbp; i++) 383*0Sstevel@tonic-gate pd->ds[ids++] = '0'; 384*0Sstevel@tonic-gate pd->ds[ids++] = (char)current; 385*0Sstevel@tonic-gate } else { 386*0Sstevel@tonic-gate pd->exponent += (nzbp + 1) << expshift; 387*0Sstevel@tonic-gate pd->more = 1; 388*0Sstevel@tonic-gate if (ids < DECIMAL_STRING_LENGTH) { 389*0Sstevel@tonic-gate pd->ds[ids] = '\0'; 390*0Sstevel@tonic-gate pd->ndigits = ids; 391*0Sstevel@tonic-gate /* don't store any more digits */ 392*0Sstevel@tonic-gate ids = DECIMAL_STRING_LENGTH; 393*0Sstevel@tonic-gate } 394*0Sstevel@tonic-gate } 395*0Sstevel@tonic-gate pd->fpclass = fp_normal; 396*0Sstevel@tonic-gate sigfound = 1; 397*0Sstevel@tonic-gate nzbp = 0; 398*0Sstevel@tonic-gate current = *++cp; 399*0Sstevel@tonic-gate 400*0Sstevel@tonic-gate /* 401*0Sstevel@tonic-gate * Use an optimized loop to grab a consecutive sequence 402*0Sstevel@tonic-gate * of nonzero digits quickly. 403*0Sstevel@tonic-gate */ 404*0Sstevel@tonic-gate nfastlimit = DECIMAL_STRING_LENGTH - 3 - ids; 405*0Sstevel@tonic-gate for (nfast = 0, pfast = &(pd->ds[ids]); 406*0Sstevel@tonic-gate nfast < nfastlimit && NZDIGIT(current); 407*0Sstevel@tonic-gate nfast++) { 408*0Sstevel@tonic-gate *pfast++ = (char)current; 409*0Sstevel@tonic-gate current = *++cp; 410*0Sstevel@tonic-gate } 411*0Sstevel@tonic-gate ids += nfast; 412*0Sstevel@tonic-gate if (current == L'0') 413*0Sstevel@tonic-gate goto nextnumberzero; /* common case */ 414*0Sstevel@tonic-gate /* advance good to the last accepted digit */ 415*0Sstevel@tonic-gate good = cp - 1; 416*0Sstevel@tonic-gate goto nextnumber; 417*0Sstevel@tonic-gate } else { 418*0Sstevel@tonic-gate switch (current) { 419*0Sstevel@tonic-gate case L'0': 420*0Sstevel@tonic-gate nextnumberzero: 421*0Sstevel@tonic-gate /* 422*0Sstevel@tonic-gate * Count zeros before the radix point. Later we 423*0Sstevel@tonic-gate * will either put these zeros into pd->ds or add 424*0Sstevel@tonic-gate * nzbp to pd->exponent to account for them. 425*0Sstevel@tonic-gate */ 426*0Sstevel@tonic-gate while (current == L'0') { 427*0Sstevel@tonic-gate nzbp++; 428*0Sstevel@tonic-gate current = *++cp; 429*0Sstevel@tonic-gate } 430*0Sstevel@tonic-gate good = cp - 1; 431*0Sstevel@tonic-gate goto nextnumber; 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate case L'E': 434*0Sstevel@tonic-gate case L'e': 435*0Sstevel@tonic-gate if (form < 0) 436*0Sstevel@tonic-gate goto done; 437*0Sstevel@tonic-gate goto exponent; 438*0Sstevel@tonic-gate 439*0Sstevel@tonic-gate case L'P': 440*0Sstevel@tonic-gate case L'p': 441*0Sstevel@tonic-gate if (form > 0) 442*0Sstevel@tonic-gate goto done; 443*0Sstevel@tonic-gate goto exponent; 444*0Sstevel@tonic-gate 445*0Sstevel@tonic-gate default: 446*0Sstevel@tonic-gate if (current == decpt) { 447*0Sstevel@tonic-gate /* accept the radix point */ 448*0Sstevel@tonic-gate good = cp; 449*0Sstevel@tonic-gate current = *++cp; 450*0Sstevel@tonic-gate goto afterpoint; 451*0Sstevel@tonic-gate } 452*0Sstevel@tonic-gate goto done; 453*0Sstevel@tonic-gate } 454*0Sstevel@tonic-gate } 455*0Sstevel@tonic-gate 456*0Sstevel@tonic-gate afterpoint: 457*0Sstevel@tonic-gate /* 458*0Sstevel@tonic-gate * Admissible characters after the radix point are a valid digit 459*0Sstevel@tonic-gate * or an exponent delimiter. (Note that it is possible to get 460*0Sstevel@tonic-gate * here even though we haven't found any digits yet.) 461*0Sstevel@tonic-gate */ 462*0Sstevel@tonic-gate if (NZDIGIT(current)) { 463*0Sstevel@tonic-gate if (form == 0) 464*0Sstevel@tonic-gate form = 1; 465*0Sstevel@tonic-gate if (sigfound < 1) { 466*0Sstevel@tonic-gate /* no significant digits found until now */ 467*0Sstevel@tonic-gate pd->fpclass = fp_normal; 468*0Sstevel@tonic-gate sigfound = 1; 469*0Sstevel@tonic-gate pd->ds[ids++] = (char)current; 470*0Sstevel@tonic-gate pd->exponent = (-(nzap + 1)) << expshift; 471*0Sstevel@tonic-gate } else { 472*0Sstevel@tonic-gate /* significant digits have been found */ 473*0Sstevel@tonic-gate if (ids + nzbp + nzap + 2 < DECIMAL_STRING_LENGTH) { 474*0Sstevel@tonic-gate for (i = 0; i < nzbp + nzap; i++) 475*0Sstevel@tonic-gate pd->ds[ids++] = '0'; 476*0Sstevel@tonic-gate pd->ds[ids++] = (char)current; 477*0Sstevel@tonic-gate pd->exponent -= (nzap + 1) << expshift; 478*0Sstevel@tonic-gate } else { 479*0Sstevel@tonic-gate pd->exponent += nzbp << expshift; 480*0Sstevel@tonic-gate pd->more = 1; 481*0Sstevel@tonic-gate if (ids < DECIMAL_STRING_LENGTH) { 482*0Sstevel@tonic-gate pd->ds[ids] = '\0'; 483*0Sstevel@tonic-gate pd->ndigits = ids; 484*0Sstevel@tonic-gate /* don't store any more digits */ 485*0Sstevel@tonic-gate ids = DECIMAL_STRING_LENGTH; 486*0Sstevel@tonic-gate } 487*0Sstevel@tonic-gate } 488*0Sstevel@tonic-gate } 489*0Sstevel@tonic-gate nzbp = 0; 490*0Sstevel@tonic-gate nzap = 0; 491*0Sstevel@tonic-gate current = *++cp; 492*0Sstevel@tonic-gate 493*0Sstevel@tonic-gate /* 494*0Sstevel@tonic-gate * Use an optimized loop to grab a consecutive sequence 495*0Sstevel@tonic-gate * of nonzero digits quickly. 496*0Sstevel@tonic-gate */ 497*0Sstevel@tonic-gate nfastlimit = DECIMAL_STRING_LENGTH - 3 - ids; 498*0Sstevel@tonic-gate for (nfast = 0, pfast = &(pd->ds[ids]); 499*0Sstevel@tonic-gate nfast < nfastlimit && NZDIGIT(current); 500*0Sstevel@tonic-gate nfast++) { 501*0Sstevel@tonic-gate *pfast++ = (char)current; 502*0Sstevel@tonic-gate current = *++cp; 503*0Sstevel@tonic-gate } 504*0Sstevel@tonic-gate ids += nfast; 505*0Sstevel@tonic-gate pd->exponent -= nfast << expshift; 506*0Sstevel@tonic-gate if (current == L'0') 507*0Sstevel@tonic-gate goto zeroafterpoint; 508*0Sstevel@tonic-gate /* advance good to the last accepted digit */ 509*0Sstevel@tonic-gate good = cp - 1; 510*0Sstevel@tonic-gate goto afterpoint; 511*0Sstevel@tonic-gate } else { 512*0Sstevel@tonic-gate switch (current) { 513*0Sstevel@tonic-gate case L'0': 514*0Sstevel@tonic-gate if (form == 0) 515*0Sstevel@tonic-gate form = 1; 516*0Sstevel@tonic-gate if (sigfound == -1) { 517*0Sstevel@tonic-gate pd->fpclass = fp_zero; 518*0Sstevel@tonic-gate sigfound = 0; 519*0Sstevel@tonic-gate } 520*0Sstevel@tonic-gate zeroafterpoint: 521*0Sstevel@tonic-gate /* 522*0Sstevel@tonic-gate * Count zeros after the radix point. If we find 523*0Sstevel@tonic-gate * any more nonzero digits later, we will put these 524*0Sstevel@tonic-gate * zeros into pd->ds and decrease pd->exponent by 525*0Sstevel@tonic-gate * nzap. 526*0Sstevel@tonic-gate */ 527*0Sstevel@tonic-gate while (current == L'0') { 528*0Sstevel@tonic-gate nzap++; 529*0Sstevel@tonic-gate current = *++cp; 530*0Sstevel@tonic-gate } 531*0Sstevel@tonic-gate good = cp - 1; 532*0Sstevel@tonic-gate goto afterpoint; 533*0Sstevel@tonic-gate 534*0Sstevel@tonic-gate case L'E': 535*0Sstevel@tonic-gate case L'e': 536*0Sstevel@tonic-gate /* don't accept exponent without preceding digits */ 537*0Sstevel@tonic-gate if (sigfound == -1 || form < 0) 538*0Sstevel@tonic-gate goto done; 539*0Sstevel@tonic-gate break; 540*0Sstevel@tonic-gate 541*0Sstevel@tonic-gate case L'P': 542*0Sstevel@tonic-gate case L'p': 543*0Sstevel@tonic-gate /* don't accept exponent without preceding digits */ 544*0Sstevel@tonic-gate if (sigfound == -1 || form > 0) 545*0Sstevel@tonic-gate goto done; 546*0Sstevel@tonic-gate break; 547*0Sstevel@tonic-gate 548*0Sstevel@tonic-gate default: 549*0Sstevel@tonic-gate goto done; 550*0Sstevel@tonic-gate } 551*0Sstevel@tonic-gate } 552*0Sstevel@tonic-gate 553*0Sstevel@tonic-gate exponent: 554*0Sstevel@tonic-gate e = 0; 555*0Sstevel@tonic-gate esign = 0; 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gate /* look for optional exponent sign */ 558*0Sstevel@tonic-gate current = *++cp; 559*0Sstevel@tonic-gate if (current == L'+') { 560*0Sstevel@tonic-gate current = *++cp; 561*0Sstevel@tonic-gate } else if (current == L'-') { 562*0Sstevel@tonic-gate esign = 1; 563*0Sstevel@tonic-gate current = *++cp; 564*0Sstevel@tonic-gate } 565*0Sstevel@tonic-gate 566*0Sstevel@tonic-gate /* 567*0Sstevel@tonic-gate * Accumulate explicit exponent. Note that if we don't find at 568*0Sstevel@tonic-gate * least one digit, good won't be updated and e will remain 0. 569*0Sstevel@tonic-gate * Also, we keep e from getting too large so we don't overflow 570*0Sstevel@tonic-gate * the range of int (but notice that the threshold is large 571*0Sstevel@tonic-gate * enough that any larger e would cause the result to underflow 572*0Sstevel@tonic-gate * or overflow anyway). 573*0Sstevel@tonic-gate */ 574*0Sstevel@tonic-gate while (L'0' <= current && current <= L'9') { 575*0Sstevel@tonic-gate good = cp; 576*0Sstevel@tonic-gate if (e <= 1000000) 577*0Sstevel@tonic-gate e = 10 * e + current - L'0'; 578*0Sstevel@tonic-gate current = *++cp; 579*0Sstevel@tonic-gate } 580*0Sstevel@tonic-gate if (esign) 581*0Sstevel@tonic-gate pd->exponent -= e; 582*0Sstevel@tonic-gate else 583*0Sstevel@tonic-gate pd->exponent += e; 584*0Sstevel@tonic-gate 585*0Sstevel@tonic-gate done: 586*0Sstevel@tonic-gate /* 587*0Sstevel@tonic-gate * If we found any zeros before the radix point that were not 588*0Sstevel@tonic-gate * accounted for earlier, adjust the exponent. (This is only 589*0Sstevel@tonic-gate * relevant when pd->fpclass == fp_normal, but it's harmless 590*0Sstevel@tonic-gate * in all other cases.) 591*0Sstevel@tonic-gate */ 592*0Sstevel@tonic-gate pd->exponent += nzbp << expshift; 593*0Sstevel@tonic-gate 594*0Sstevel@tonic-gate /* terminate pd->ds if we haven't already */ 595*0Sstevel@tonic-gate if (ids < DECIMAL_STRING_LENGTH) { 596*0Sstevel@tonic-gate pd->ds[ids] = '\0'; 597*0Sstevel@tonic-gate pd->ndigits = ids; 598*0Sstevel@tonic-gate } 599*0Sstevel@tonic-gate 600*0Sstevel@tonic-gate /* 601*0Sstevel@tonic-gate * If we accepted any characters, advance *ppc to point to the 602*0Sstevel@tonic-gate * first character we didn't accept; otherwise, pass back a 603*0Sstevel@tonic-gate * signaling nan. 604*0Sstevel@tonic-gate */ 605*0Sstevel@tonic-gate if (good >= *ppc) { 606*0Sstevel@tonic-gate *ppc = good + 1; 607*0Sstevel@tonic-gate } else { 608*0Sstevel@tonic-gate pd->fpclass = fp_signaling; 609*0Sstevel@tonic-gate pd->sign = 0; 610*0Sstevel@tonic-gate form = 0; 611*0Sstevel@tonic-gate } 612*0Sstevel@tonic-gate 613*0Sstevel@tonic-gate *pform = form; 614*0Sstevel@tonic-gate } 615