Skip to content

Commit

Permalink
Add strtof_l, strtod_l and strtold_l.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsonn committed Apr 18, 2013
1 parent f4b203e commit c99aac4
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 112 deletions.
6 changes: 5 additions & 1 deletion include/stdlib.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: stdlib.h,v 1.101 2013/04/16 21:44:06 joerg Exp $ */
/* $NetBSD: stdlib.h,v 1.102 2013/04/18 21:54:10 joerg Exp $ */

/*-
* Copyright (c) 1990, 1993
Expand Down Expand Up @@ -334,6 +334,10 @@ qdiv_t qdiv(quad_t, quad_t);
typedef struct _locale *locale_t;
# define __LOCALE_T_DECLARED
# endif
double strtod_l(const char * __restrict, char ** __restrict, locale_t);
float strtof_l(const char * __restrict, char ** __restrict, locale_t);
long double strtold_l(const char * __restrict, char ** __restrict,
locale_t);
long strtol_l(const char * __restrict, char ** __restrict, int, locale_t);
unsigned long
strtoul_l(const char * __restrict, char ** __restrict, int, locale_t);
Expand Down
13 changes: 11 additions & 2 deletions lib/libc/gdtoa/_strtof.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: _strtof.c,v 1.2 2009/10/21 01:07:45 snj Exp $ */
/* $NetBSD: _strtof.c,v 1.3 2013/04/18 21:54:10 joerg Exp $ */

/*
* Copyright (c) 1996 Christos Zoulas. All rights reserved.
Expand Down Expand Up @@ -26,20 +26,29 @@

#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: _strtof.c,v 1.2 2009/10/21 01:07:45 snj Exp $");
__RCSID("$NetBSD: _strtof.c,v 1.3 2013/04/18 21:54:10 joerg Exp $");
#endif /* LIBC_SCCS and not lint */

#if defined(__indr_reference)
__indr_reference(_strtof, strtof)
__indr_reference(_strtof_l, strtof_l)
#else

#include <stdlib.h>
float _strtof(const char * __restrict, char ** __restrict);
float _strtof_l(const char * __restrict, char ** __restrict, locale_t);

float
strtof(const char *nptr, char **endptr)
{

return _strtof(nptr, endptr);
}

float
strtof_l(const char *nptr, char **endptr, locale_t loc)
{

return _strtof_l(nptr, endptr, loc);
}
#endif
18 changes: 12 additions & 6 deletions lib/libc/gdtoa/gdtoa.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: gdtoa.h,v 1.9 2011/03/20 23:15:35 christos Exp $ */
/* $NetBSD: gdtoa.h,v 1.10 2013/04/18 21:54:10 joerg Exp $ */

/****************************************************************
Expand Down Expand Up @@ -38,6 +38,11 @@ THIS SOFTWARE.
#include <stddef.h> /* for size_t */
#include <stdint.h>

#ifndef __LOCALE_T_DECLARED
typedef struct _locale *locale_t;
#define __LOCALE_T_DECLARED
#endif

#ifndef Long
#define Long int32_t
#endif
Expand Down Expand Up @@ -133,7 +138,8 @@ extern char* gdtoa ANSI((FPI *fpi, int be, ULong *bits, int *kindp,
extern void freedtoa ANSI((char*));
extern float strtof ANSI((CONST char *, char **));
extern double strtod ANSI((CONST char *, char **));
extern int strtodg ANSI((CONST char*, char**, CONST FPI*, Long*, ULong*));
extern int strtodg ANSI((CONST char*, char**, CONST FPI*, Long*, ULong*,
locale_t));

extern char* g_ddfmt ANSI((char*, double*, int, size_t));
extern char* g_dfmt ANSI((char*, double*, int, size_t));
Expand All @@ -148,7 +154,7 @@ extern int strtoIf ANSI((CONST char*, char**, float*, float*));
extern int strtoIQ ANSI((CONST char*, char**, void*, void*));
extern int strtoIx ANSI((CONST char*, char**, void*, void*));
extern int strtoIxL ANSI((CONST char*, char**, void*, void*));
extern int strtord ANSI((CONST char*, char**, int, double*));
extern int strtord ANSI((CONST char*, char**, int, double*, locale_t));
extern int strtordd ANSI((CONST char*, char**, int, double*));
extern int strtorf ANSI((CONST char*, char**, int, float*));
extern int strtorQ ANSI((CONST char*, char**, int, void*));
Expand All @@ -159,9 +165,9 @@ extern int strtodI ANSI((CONST char*, char**, double*));
extern int strtopd ANSI((CONST char*, char**, double*));
extern int strtopdd ANSI((CONST char*, char**, double*));
extern int strtopf ANSI((CONST char*, char**, float*));
extern int strtopQ ANSI((CONST char*, char**, void*));
extern int strtopx ANSI((CONST char*, char**, void*));
extern int strtopxL ANSI((CONST char*, char**, void*));
extern int strtopQ ANSI((CONST char*, char**, void*, locale_t));
extern int strtopx ANSI((CONST char*, char**, void*, locale_t));
extern int strtopxL ANSI((CONST char*, char**, void*, locale_t));
#else
#define strtopd(s,se,x) strtord(s,se,1,x)
#define strtopdd(s,se,x) strtordd(s,se,1,x)
Expand Down
53 changes: 26 additions & 27 deletions lib/libc/gdtoa/strtod.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: strtod.c,v 1.11 2012/03/22 15:34:14 christos Exp $ */
/* $NetBSD: strtod.c,v 1.12 2013/04/18 21:54:10 joerg Exp $ */

/****************************************************************
Expand Down Expand Up @@ -31,13 +31,15 @@ THIS SOFTWARE.
/* Please send bug reports to David M. Gay (dmg at acm dot org,
* with " at " changed at "@" and " dot " changed to "."). */

#include "namespace.h"
#include "gdtoaimp.h"
#ifndef NO_FENV_H
#include <fenv.h>
#endif

#ifdef USE_LOCALE
#include "locale.h"
#include <locale.h>
#include "setlocale_local.h"
#endif

#ifdef IEEE_Arith
Expand All @@ -62,6 +64,8 @@ static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
#ifndef __HAVE_LONG_DOUBLE
__strong_alias(_strtold, strtod)
__weak_alias(strtold, _strtold)
__strong_alias(_strtold_l, strtod_l)
__weak_alias(strtold_l, _strtold_l)
#endif

#ifdef Avoid_Underflow /*{*/
Expand All @@ -86,13 +90,8 @@ sulp
}
#endif /*}*/

double
strtod
#ifdef KR_headers
(s00, se) CONST char *s00; char **se;
#else
(CONST char *s00, char **se)
#endif
static double
_int_strtod_l(CONST char *s00, char **se, locale_t loc)
{
#ifdef Avoid_Underflow
int scale;
Expand All @@ -116,25 +115,8 @@ strtod
int inexact, oldinexact;
#endif
#ifdef USE_LOCALE /*{{*/
#ifdef NO_LOCALE_CACHE
char *decimalpoint = localeconv()->decimal_point;
char *decimalpoint = localeconv_l(loc)->decimal_point;
size_t dplen = strlen(decimalpoint);
#else
char *decimalpoint;
static char *decimalpoint_cache;
static size_t dplen;
if (!(s0 = decimalpoint_cache)) {
s0 = localeconv()->decimal_point;
if ((decimalpoint_cache = MALLOC(strlen(s0) + 1)) != NULL) {
strcpy(decimalpoint_cache, s0);
s0 = decimalpoint_cache;
}
dplen = strlen(s0);
}
decimalpoint = __UNCONST(s0);
#endif /*NO_LOCALE_CACHE*/
#else /*USE_LOCALE}{*/
#define dplen 1
#endif /*USE_LOCALE}}*/

#ifdef Honor_FLT_ROUNDS /*{*/
Expand Down Expand Up @@ -1118,3 +1100,20 @@ strtod
return sign ? -dval(&rv) : dval(&rv);
}

double
strtod(CONST char *s, char **sp)
{
return _int_strtod_l(s, sp, *_current_locale());
}

#ifdef __weak_alias
__weak_alias(strtod_l, _strtod_l)
#endif

double
strtod_l(CONST char *s, char **sp, locale_t loc)
{
if (loc == NULL)
loc = _C_locale;
return _int_strtod_l(s, sp, loc);
}
30 changes: 4 additions & 26 deletions lib/libc/gdtoa/strtodg.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: strtodg.c,v 1.10 2012/03/22 13:09:12 he Exp $ */
/* $NetBSD: strtodg.c,v 1.11 2013/04/18 21:54:10 joerg Exp $ */

/****************************************************************
Expand Down Expand Up @@ -319,13 +319,8 @@ mantbits(U *d)
#endif /* !VAX */

int
strtodg
#ifdef KR_headers
(s00, se, fpi, expt, bits)
CONST char *s00; char **se; CONST FPI *fpi; Long *expt; ULong *bits;
#else
(CONST char *s00, char **se, CONST FPI *fpi, Long *expt, ULong *bits)
#endif
strtodg(CONST char *s00, char **se, CONST FPI *fpi, Long *expt, ULong *bits,
locale_t loc)
{
int abe, abits, asub;
#ifdef INFNAN_CHECK
Expand All @@ -342,25 +337,8 @@ strtodg
ULong *b, *be, y, z;
Bigint *ab, *bb, *bb1, *bd, *bd0, *bs, *delta, *rvb, *rvb0;
#ifdef USE_LOCALE /*{{*/
#ifdef NO_LOCALE_CACHE
char *decimalpoint = localeconv()->decimal_point;
char *decimalpoint = localeconv_l(loc)->decimal_point;
size_t dplen = strlen(decimalpoint);
#else
char *decimalpoint;
static char *decimalpoint_cache;
static size_t dplen;
if (!(s0 = decimalpoint_cache)) {
s0 = localeconv()->decimal_point;
if ((decimalpoint_cache = MALLOC(strlen(s0) + 1)) != NULL) {
strcpy(decimalpoint_cache, s0);
s0 = decimalpoint_cache;
}
dplen = strlen(s0);
}
decimalpoint = __UNCONST(s0);
#endif /*NO_LOCALE_CACHE*/
#else /*USE_LOCALE}{*/
#define dplen 1
#endif /*USE_LOCALE}}*/

e2 = 0; /* XXX gcc */
Expand Down
30 changes: 22 additions & 8 deletions lib/libc/gdtoa/strtof.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: strtof.c,v 1.5 2011/06/20 09:11:17 mrg Exp $ */
/* $NetBSD: strtof.c,v 1.6 2013/04/18 21:54:11 joerg Exp $ */

/****************************************************************
Expand Down Expand Up @@ -34,16 +34,16 @@ THIS SOFTWARE.
#include "namespace.h"
#include "gdtoaimp.h"

#include <locale.h>
#include "setlocale_local.h"

#ifdef __weak_alias
__weak_alias(strtof, _strtof)
__weak_alias(strtof_l, _strtof_l)
#endif

float
#ifdef KR_headers
strtof(s, sp) CONST char *s; char **sp;
#else
strtof(CONST char *s, char **sp)
#endif
static float
_int_strtof_l(CONST char *s, char **sp, locale_t loc)
{
static CONST FPI fpi0 = { 24, 1-127-24+1, 254-127-24+1, 1, SI };
ULong bits[1];
Expand All @@ -56,7 +56,7 @@ strtof(CONST char *s, char **sp)
#define fpi &fpi0
#endif

k = strtodg(s, sp, fpi, &expt, bits);
k = strtodg(s, sp, fpi, &expt, bits, loc);
if (k == STRTOG_NoMemory) {
errno = ERANGE;
return HUGE_VALF;
Expand Down Expand Up @@ -92,3 +92,17 @@ strtof(CONST char *s, char **sp)
u.L[0] |= 0x80000000L;
return u.f;
}

float
strtof(CONST char *s, char **sp)
{
return _int_strtof_l(s, sp, *_current_locale());
}

float
strtof_l(CONST char *s, char **sp, locale_t loc)
{
if (loc == NULL)
loc = _C_locale;
return _int_strtof_l(s, sp, loc);
}
29 changes: 21 additions & 8 deletions lib/libc/gdtoa/strtof_vaxf.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: strtof_vaxf.c,v 1.6 2011/07/01 03:20:06 matt Exp $ */
/* $NetBSD: strtof_vaxf.c,v 1.7 2013/04/18 21:54:11 joerg Exp $ */

/****************************************************************
Expand Down Expand Up @@ -35,25 +35,24 @@ THIS SOFTWARE.

#include "namespace.h"
#include "gdtoaimp.h"
#include <locale.h>
#include "setlocale_local.h"

#ifdef __weak_alias
__weak_alias(strtof, _strtof)
__weak_alias(strtof_l, _strtof_l)
#endif

float
#ifdef KR_headers
strtof(s, sp) CONST char *s; char **sp;
#else
strtof(CONST char *s, char **sp)
#endif
static float
_int_strtof_l(CONST char *s, char **sp, locale_t loc)
{
static CONST FPI fpi = { 24, 1-128-1-24+1, 255-128-1-24+1, 1, SI };
ULong bits[1];
Long expt;
int k;
union { ULong L[1]; float f; } u;

k = strtodg(s, sp, &fpi, &expt, bits);
k = strtodg(s, sp, &fpi, &expt, bits, loc);
if (k == STRTOG_NoMemory) {
errno = ERANGE;
return HUGE_VALF;
Expand All @@ -80,3 +79,17 @@ strtof(CONST char *s, char **sp)
u.L[0] |= 0x00008000L;
return u.f;
}

float
strtof(CONST char *s, char **sp)
{
return _int_strtof_l(s, sp, *_current_locale());
}

float
strtof_l(CONST char *s, char **sp, locale_t loc)
{
if (loc == NULL)
loc = _C_locale;
return _int_strtof_l(s, sp, loc);
}
Loading

0 comments on commit c99aac4

Please sign in to comment.