xref: /onnv-gate/usr/src/lib/libc/port/gen/strtod.c (revision 6812:febeba71273d)
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 
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <values.h>
360Sstevel@tonic-gate #include <floatingpoint.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include "libc.h"
400Sstevel@tonic-gate #include "xpg6.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate double
strtod(const char * cp,char ** ptr)430Sstevel@tonic-gate strtod(const char *cp, char **ptr)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	double		x;
460Sstevel@tonic-gate 	decimal_mode	mr;
470Sstevel@tonic-gate 	decimal_record  dr;
480Sstevel@tonic-gate 	fp_exception_field_type fs;
490Sstevel@tonic-gate 	enum decimal_string_form form;
500Sstevel@tonic-gate 	char		*pechar;
510Sstevel@tonic-gate 	int		lc;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	lc = (__xpg6 & _C99SUSv3_recognize_hexfp)? -1 : 0;
540Sstevel@tonic-gate 	string_to_decimal((char **)&cp, MAXINT, lc, &dr, &form, &pechar);
550Sstevel@tonic-gate 	if (ptr != NULL)
560Sstevel@tonic-gate 		*ptr = (char *)cp;
570Sstevel@tonic-gate 	if (form == invalid_form)
580Sstevel@tonic-gate 		return (0.0);	/* Shameful kluge for SVID's sake. */
590Sstevel@tonic-gate #if defined(__sparc)
600Sstevel@tonic-gate 	mr.rd = _QgetRD();
610Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
620Sstevel@tonic-gate 	mr.rd = __xgetRD();
630Sstevel@tonic-gate #else
640Sstevel@tonic-gate #error Unknown architecture
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate 	if ((int)form < 0)
670Sstevel@tonic-gate 		__hex_to_double(&dr, mr.rd, &x, &fs);
680Sstevel@tonic-gate 	else
690Sstevel@tonic-gate 		decimal_to_double(&x, &mr, &dr, &fs);
700Sstevel@tonic-gate 	if (fs & ((1 << fp_overflow) | (1 << fp_underflow)))
710Sstevel@tonic-gate 		errno = ERANGE;
720Sstevel@tonic-gate 	return (x);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate float
strtof(const char * cp,char ** ptr)760Sstevel@tonic-gate strtof(const char *cp, char **ptr)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	float		x;
790Sstevel@tonic-gate 	decimal_mode	mr;
800Sstevel@tonic-gate 	decimal_record	dr;
810Sstevel@tonic-gate 	fp_exception_field_type fs;
820Sstevel@tonic-gate 	enum decimal_string_form form;
830Sstevel@tonic-gate 	char		*pechar;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	string_to_decimal((char **)&cp, MAXINT, -1, &dr, &form, &pechar);
860Sstevel@tonic-gate 	if (ptr != NULL)
870Sstevel@tonic-gate 		*ptr = (char *)cp;
880Sstevel@tonic-gate 	if (form == invalid_form)
890Sstevel@tonic-gate 		return (0.0f);
900Sstevel@tonic-gate #if defined(__sparc)
910Sstevel@tonic-gate 	mr.rd = _QgetRD();
920Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
930Sstevel@tonic-gate 	mr.rd = __xgetRD();
940Sstevel@tonic-gate #else
950Sstevel@tonic-gate #error Unknown architecture
960Sstevel@tonic-gate #endif
970Sstevel@tonic-gate 	if ((int)form < 0)
980Sstevel@tonic-gate 		__hex_to_single(&dr, mr.rd, &x, &fs);
990Sstevel@tonic-gate 	else
1000Sstevel@tonic-gate 		decimal_to_single(&x, &mr, &dr, &fs);
1010Sstevel@tonic-gate 	if (fs & ((1 << fp_overflow) | (1 << fp_underflow)))
1020Sstevel@tonic-gate 		errno = ERANGE;
1030Sstevel@tonic-gate 	return (x);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate long double
strtold(const char * cp,char ** ptr)1070Sstevel@tonic-gate strtold(const char *cp, char **ptr)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	long double	x;
1100Sstevel@tonic-gate 	decimal_mode	mr;
1110Sstevel@tonic-gate 	decimal_record	dr;
1120Sstevel@tonic-gate 	fp_exception_field_type fs;
1130Sstevel@tonic-gate 	enum decimal_string_form form;
1140Sstevel@tonic-gate 	char		*pechar;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	string_to_decimal((char **)&cp, MAXINT, -1, &dr, &form, &pechar);
1170Sstevel@tonic-gate 	if (ptr != NULL)
1180Sstevel@tonic-gate 		*ptr = (char *)cp;
1190Sstevel@tonic-gate 	if (form == invalid_form)
1200Sstevel@tonic-gate 		return (0.0L);
1210Sstevel@tonic-gate #if defined(__sparc)
1220Sstevel@tonic-gate 	mr.rd = _QgetRD();
1230Sstevel@tonic-gate 	if ((int)form < 0)
1240Sstevel@tonic-gate 		__hex_to_quadruple(&dr, mr.rd, &x, &fs);
1250Sstevel@tonic-gate 	else
1260Sstevel@tonic-gate 		decimal_to_quadruple(&x, &mr, &dr, &fs);
1270Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
1280Sstevel@tonic-gate 	mr.rd = __xgetRD();
1290Sstevel@tonic-gate 	if ((int)form < 0)
1300Sstevel@tonic-gate 		__hex_to_extended(&dr, mr.rd, (extended *)&x, &fs);
1310Sstevel@tonic-gate 	else
1320Sstevel@tonic-gate 		decimal_to_extended((extended *)&x, &mr, &dr, &fs);
1330Sstevel@tonic-gate #else
1340Sstevel@tonic-gate #error Unknown architecture
1350Sstevel@tonic-gate #endif
1360Sstevel@tonic-gate 	if (fs & ((1 << fp_overflow) | (1 << fp_underflow)))
1370Sstevel@tonic-gate 		errno = ERANGE;
1380Sstevel@tonic-gate 	return (x);
1390Sstevel@tonic-gate }
140