From: Chia-I Wu Date: Wed, 20 Aug 2014 06:40:22 +0000 (+0800) Subject: util: add _mesa_strtod and _mesa_strtof X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=e3f20294799d727107012d40f6c973975084e4e6;p=mesa.git util: add _mesa_strtod and _mesa_strtof Both core mesa and glsl have their own wrappers for strtof_l. Merge and move them to util/. They are compiled with a C++ compiler so that we can make them thread-safe in a following commit. Signed-off-by: Chia-I Wu Reviewed-by: Kenneth Graunke --- diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources index 0c55327d608..85280eeb297 100644 --- a/src/glsl/Makefile.sources +++ b/src/glsl/Makefile.sources @@ -104,8 +104,7 @@ LIBGLSL_FILES = \ $(GLSL_SRCDIR)/opt_swizzle_swizzle.cpp \ $(GLSL_SRCDIR)/opt_tree_grafting.cpp \ $(GLSL_SRCDIR)/opt_vectorize.cpp \ - $(GLSL_SRCDIR)/s_expression.cpp \ - $(GLSL_SRCDIR)/strtod.c + $(GLSL_SRCDIR)/s_expression.cpp # glsl_compiler diff --git a/src/glsl/glsl_lexer.ll b/src/glsl/glsl_lexer.ll index e66a935916c..ad6d32f9bc0 100644 --- a/src/glsl/glsl_lexer.ll +++ b/src/glsl/glsl_lexer.ll @@ -23,7 +23,7 @@ */ #include #include -#include "strtod.h" +#include "util/strtod.h" #include "ast.h" #include "glsl_parser_extras.h" #include "glsl_parser.h" @@ -451,23 +451,23 @@ layout { } [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? { - yylval->real = glsl_strtof(yytext, NULL); + yylval->real = _mesa_strtof(yytext, NULL); return FLOATCONSTANT; } \.[0-9]+([eE][+-]?[0-9]+)?[fF]? { - yylval->real = glsl_strtof(yytext, NULL); + yylval->real = _mesa_strtof(yytext, NULL); return FLOATCONSTANT; } [0-9]+\.([eE][+-]?[0-9]+)?[fF]? { - yylval->real = glsl_strtof(yytext, NULL); + yylval->real = _mesa_strtof(yytext, NULL); return FLOATCONSTANT; } [0-9]+[eE][+-]?[0-9]+[fF]? { - yylval->real = glsl_strtof(yytext, NULL); + yylval->real = _mesa_strtof(yytext, NULL); return FLOATCONSTANT; } [0-9]+[fF] { - yylval->real = glsl_strtof(yytext, NULL); + yylval->real = _mesa_strtof(yytext, NULL); return FLOATCONSTANT; } diff --git a/src/glsl/s_expression.cpp b/src/glsl/s_expression.cpp index 1a28e1d5248..2928a4db832 100644 --- a/src/glsl/s_expression.cpp +++ b/src/glsl/s_expression.cpp @@ -73,7 +73,7 @@ read_atom(void *ctx, const char *&src, char *&symbol_buffer) } else { // Check if the atom is a number. char *float_end = NULL; - float f = glsl_strtof(src, &float_end); + float f = _mesa_strtof(src, &float_end); if (float_end != src) { char *int_end = NULL; int i = strtol(src, &int_end, 10); diff --git a/src/glsl/s_expression.h b/src/glsl/s_expression.h index 642af19b439..1d4753530bc 100644 --- a/src/glsl/s_expression.h +++ b/src/glsl/s_expression.h @@ -27,7 +27,7 @@ #define S_EXPRESSION_H #include "main/core.h" /* for Elements */ -#include "strtod.h" +#include "util/strtod.h" #include "list.h" /* Type-safe downcasting macros (also safe to pass NULL) */ diff --git a/src/glsl/strtod.c b/src/glsl/strtod.c deleted file mode 100644 index 5d4346b5a2e..00000000000 --- a/src/glsl/strtod.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2010 VMware, Inc. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - - -#include - -#ifdef _GNU_SOURCE -#include -#ifdef __APPLE__ -#include -#endif -#endif - -#include "strtod.h" - - - -/** - * Wrapper around strtod which uses the "C" locale so the decimal - * point is always '.' - */ -double -glsl_strtod(const char *s, char **end) -{ -#if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \ - !defined(__HAIKU__) && !defined(__UCLIBC__) - static locale_t loc = NULL; - if (!loc) { - loc = newlocale(LC_CTYPE_MASK, "C", NULL); - } - return strtod_l(s, end, loc); -#else - return strtod(s, end); -#endif -} - - -/** - * Wrapper around strtof which uses the "C" locale so the decimal - * point is always '.' - */ -float -glsl_strtof(const char *s, char **end) -{ -#if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \ - !defined(__HAIKU__) && !defined(__UCLIBC__) - static locale_t loc = NULL; - if (!loc) { - loc = newlocale(LC_CTYPE_MASK, "C", NULL); - } - return strtof_l(s, end, loc); -#elif _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE - return strtof(s, end); -#else - return (float) strtod(s, end); -#endif -} diff --git a/src/glsl/strtod.h b/src/glsl/strtod.h deleted file mode 100644 index ad847dbb010..00000000000 --- a/src/glsl/strtod.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2010 VMware, Inc. - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sub license, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice (including the - * next paragraph) shall be included in all copies or substantial portions - * of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR - * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - - -#ifndef STRTOD_H -#define STRTOD_H - -#ifdef __cplusplus -extern "C" { -#endif - -extern double -glsl_strtod(const char *s, char **end); - -extern float -glsl_strtof(const char *s, char **end); - - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/src/mesa/main/imports.c b/src/mesa/main/imports.c index b8c75481516..4f5a2d11fa9 100644 --- a/src/mesa/main/imports.c +++ b/src/mesa/main/imports.c @@ -499,25 +499,6 @@ _mesa_strdup( const char *s ) } } -/** Wrapper around strtof() */ -float -_mesa_strtof( const char *s, char **end ) -{ -#if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \ - !defined(ANDROID) && !defined(__HAIKU__) && !defined(__UCLIBC__) && \ - !defined(__NetBSD__) - static locale_t loc = NULL; - if (!loc) { - loc = newlocale(LC_CTYPE_MASK, "C", NULL); - } - return strtof_l(s, end, loc); -#elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) - return strtof(s, end); -#else - return (float)strtod(s, end); -#endif -} - /** Compute simple checksum/hash for a string */ unsigned int _mesa_str_checksum(const char *str) diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h index 436d1651a82..0fcba4fb986 100644 --- a/src/mesa/main/imports.h +++ b/src/mesa/main/imports.h @@ -540,9 +540,6 @@ _mesa_half_is_negative(GLhalfARB h) extern char * _mesa_strdup( const char *s ); -extern float -_mesa_strtof( const char *s, char **end ); - extern unsigned int _mesa_str_checksum(const char *str); diff --git a/src/mesa/program/program_lexer.l b/src/mesa/program/program_lexer.l index d5dbcf347b3..e3639122058 100644 --- a/src/mesa/program/program_lexer.l +++ b/src/mesa/program/program_lexer.l @@ -28,6 +28,7 @@ #include "program/symbol_table.h" #include "program/program_parser.h" #include "program/program_parse.tab.h" +#include "util/strtod.h" #define require_ARB_vp (yyextra->mode == ARB_vertex) #define require_ARB_fp (yyextra->mode == ARB_fragment) diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources index 952b79905de..9e274241d27 100644 --- a/src/util/Makefile.sources +++ b/src/util/Makefile.sources @@ -3,7 +3,8 @@ MESA_UTIL_FILES := \ ralloc.c \ register_allocate.c \ register_allocate.h \ - rgtc.c + rgtc.c \ + strtod.cpp MESA_UTIL_GENERATED_FILES = \ format_srgb.c diff --git a/src/util/strtod.cpp b/src/util/strtod.cpp new file mode 100644 index 00000000000..2f1d2290879 --- /dev/null +++ b/src/util/strtod.cpp @@ -0,0 +1,81 @@ +/* + * Copyright 2010 VMware, Inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + +#include + +#ifdef _GNU_SOURCE +#include +#ifdef __APPLE__ +#include +#endif +#endif + +#include "strtod.h" + + + +/** + * Wrapper around strtod which uses the "C" locale so the decimal + * point is always '.' + */ +double +_mesa_strtod(const char *s, char **end) +{ +#if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \ + !defined(ANDROID) && !defined(__HAIKU__) && !defined(__UCLIBC__) && \ + !defined(__NetBSD__) + static locale_t loc = NULL; + if (!loc) { + loc = newlocale(LC_CTYPE_MASK, "C", NULL); + } + return strtod_l(s, end, loc); +#else + return strtod(s, end); +#endif +} + + +/** + * Wrapper around strtof which uses the "C" locale so the decimal + * point is always '.' + */ +float +_mesa_strtof(const char *s, char **end) +{ +#if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \ + !defined(ANDROID) && !defined(__HAIKU__) && !defined(__UCLIBC__) && \ + !defined(__NetBSD__) + static locale_t loc = NULL; + if (!loc) { + loc = newlocale(LC_CTYPE_MASK, "C", NULL); + } + return strtof_l(s, end, loc); +#elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) + return strtof(s, end); +#else + return (float) strtod(s, end); +#endif +} diff --git a/src/util/strtod.h b/src/util/strtod.h new file mode 100644 index 00000000000..02c25ddb78f --- /dev/null +++ b/src/util/strtod.h @@ -0,0 +1,46 @@ +/* + * Copyright 2010 VMware, Inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + +#ifndef STRTOD_H +#define STRTOD_H + +#ifdef __cplusplus +extern "C" { +#endif + +extern double +_mesa_strtod(const char *s, char **end); + +extern float +_mesa_strtof(const char *s, char **end); + + +#ifdef __cplusplus +} +#endif + + +#endif