From 33317b26134c4c3936897a31a2f89e1627ca7425 Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Mon, 14 Mar 2016 17:02:30 +0100 Subject: [PATCH] Add more C99 compat functions --- CMakeLists.txt | 5 ++- src/gmio_core/internal/c99_math_compat.h | 39 +++++++++++++++++++ src/gmio_core/internal/c99_stdio_compat.h | 4 +- src/gmio_core/internal/c99_stdlib_compat.h | 31 +++++++++++++++ src/gmio_core/internal/fast_atof.h | 8 +--- src/gmio_core/internal/stringstream.h | 13 ++----- .../internal/stringstream_fast_atof.h | 8 +--- src/gmio_core/internal/vecgeom_utils.h | 4 +- 8 files changed, 86 insertions(+), 26 deletions(-) create mode 100644 src/gmio_core/internal/c99_math_compat.h create mode 100644 src/gmio_core/internal/c99_stdlib_compat.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 82027fe..8ca08ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,7 +126,8 @@ if(NOT GMIO_BUILD_STRICT_C90) if(CMAKE_C_COMPILER_IS_GCC_COMPATIBLE) list(APPEND CMAKE_REQUIRED_LIBRARIES m) # -lm endif() - check_function_exists(powf GMIO_HAVE_POWF_FUNC) + check_function_exists(powf GMIO_HAVE_POWF_FUNC) + check_function_exists(sqrtf GMIO_HAVE_SQRTF_FUNC) set(CMAKE_REQUIRED_LIBRARIES) # Pop changes check_include_files(stdbool.h GMIO_HAVE_STDBOOL_H) @@ -294,6 +295,8 @@ if(GMIO_BUILD_EXAMPLES) add_subdirectory(examples) endif() +message(STATUS "CMAKE_C_FLAGS = ${CMAKE_C_FLAGS}") + # Examples: # cmake -DCMAKE_INSTALL_PREFIX=gcc-linux64 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_DEBUG_POSTFIX=_d # cmake -DCMAKE_INSTALL_PREFIX=gcc-linux64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_RELEASE_POSTFIX= diff --git a/src/gmio_core/internal/c99_math_compat.h b/src/gmio_core/internal/c99_math_compat.h new file mode 100644 index 0000000..d5ac6b0 --- /dev/null +++ b/src/gmio_core/internal/c99_math_compat.h @@ -0,0 +1,39 @@ +/**************************************************************************** +** gmio +** Copyright Fougue (2 Mar. 2015) +** contact@fougue.pro +** +** This software is a reusable library whose purpose is to provide complete +** I/O support for various CAD file formats (eg. STL) +** +** This software is governed by the CeCILL-B license under French law and +** abiding by the rules of distribution of free software. You can use, +** modify and/ or redistribute the software under the terms of the CeCILL-B +** license as circulated by CEA, CNRS and INRIA at the following URL +** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html". +****************************************************************************/ + +#ifndef GMIO_INTERNAL_C99_MATH_COMPAT_H +#define GMIO_INTERNAL_C99_MATH_COMPAT_H + +#include "../global.h" + +#include + +#ifdef GMIO_HAVE_POWF_FUNC +# define gmio_powf powf +#else +/* No powf() function, call pow(double) as fallback */ +GMIO_INLINE float gmio_powf(float x) +{ return (float)pow((double)x); } +#endif + +#ifdef GMIO_HAVE_SQRTF_FUNC +# define gmio_sqrtf sqrtf +#else +/* No sqrtf() function, call sqrt(double) as fallback */ +GMIO_INLINE float gmio_sqrtf(float x) +{ return (float)sqrt((double)x); } +#endif + +#endif /* GMIO_INTERNAL_C99_MATH_COMPAT_H */ diff --git a/src/gmio_core/internal/c99_stdio_compat.h b/src/gmio_core/internal/c99_stdio_compat.h index 450c794..7697e10 100644 --- a/src/gmio_core/internal/c99_stdio_compat.h +++ b/src/gmio_core/internal/c99_stdio_compat.h @@ -33,7 +33,7 @@ #elif defined(GMIO_HAVE_WIN__VSNPRINTF_FUNC) # define gmio_vsnprintf _vsnprintf #else -/* No existing vsnprintf()-like function, call unsafe vsprintf() as fallback */ +/* No vsnprintf()-like function, call unsafe vsprintf() as fallback */ GMIO_INLINE int gmio_vsnprintf( char* buf, size_t bufn, const char* fmt, va_list args) { @@ -53,7 +53,7 @@ GMIO_INLINE int gmio_vsnprintf( #elif defined(GMIO_HAVE_WIN__SNPRINTF_FUNC) # define gmio_snprintf _snprintf #else -/* No existing snprintf()-like function, translate to gmio_vsnprintf() call */ +/* No snprintf()-like function, translate to gmio_vsnprintf() call */ GMIO_INLINE int gmio_snprintf(char* buf, size_t bufn, const char* fmt, ...) { int ret = 0; diff --git a/src/gmio_core/internal/c99_stdlib_compat.h b/src/gmio_core/internal/c99_stdlib_compat.h new file mode 100644 index 0000000..633e0e6 --- /dev/null +++ b/src/gmio_core/internal/c99_stdlib_compat.h @@ -0,0 +1,31 @@ +/**************************************************************************** +** gmio +** Copyright Fougue (2 Mar. 2015) +** contact@fougue.pro +** +** This software is a reusable library whose purpose is to provide complete +** I/O support for various CAD file formats (eg. STL) +** +** This software is governed by the CeCILL-B license under French law and +** abiding by the rules of distribution of free software. You can use, +** modify and/ or redistribute the software under the terms of the CeCILL-B +** license as circulated by CEA, CNRS and INRIA at the following URL +** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html". +****************************************************************************/ + +#ifndef GMIO_INTERNAL_C99_STDLIB_COMPAT_H +#define GMIO_INTERNAL_C99_STDLIB_COMPAT_H + +#include "../global.h" + +#include + +#ifdef GMIO_HAVE_STRTOF_FUNC +# define gmio_strtof strtof +#else +/* No strtof() function, call strtod() as fallback */ +GMIO_INLINE float gmio_strtof(const char* str, char** endptr) +{ return (float)strtod(str, endptr); } +#endif + +#endif /* GMIO_INTERNAL_C99_STDLIB_COMPAT_H */ diff --git a/src/gmio_core/internal/fast_atof.h b/src/gmio_core/internal/fast_atof.h index f5faaed..f67329e 100644 --- a/src/gmio_core/internal/fast_atof.h +++ b/src/gmio_core/internal/fast_atof.h @@ -10,6 +10,7 @@ #define GMIO_INTERNAL_FAST_ATOF_H #include "../global.h" +#include "c99_math_compat.h" #include "string_ascii_utils.h" #include @@ -375,12 +376,7 @@ GMIO_INLINE const char* fast_atof_move(const char* in, float* result) /* Assume that the exponent is a whole number. * strtol10() will deal with both + and - signs, * but calculate as float to prevent overflow at FLT_MAX */ - value *= -#ifdef GMIO_HAVE_POWF_FUNC - powf(10.f, (float)strtol10(in, &in)); -#else - (float)pow(10., (double)strtol10(in, &in)); -#endif + value *= gmio_powf(10.f, (float)strtol10(in, &in)); } *result = negative ? -value : value; return in; diff --git a/src/gmio_core/internal/stringstream.h b/src/gmio_core/internal/stringstream.h index 539ffc9..bf058d1 100644 --- a/src/gmio_core/internal/stringstream.h +++ b/src/gmio_core/internal/stringstream.h @@ -129,6 +129,7 @@ GMIO_INLINE float gmio_to_float32(const char* str); * -- Implementation */ +#include "c99_stdlib_compat.h" #include "helper_stream.h" #include "string_ascii_utils.h" #ifdef GMIO_STRINGSTREAM_USE_FAST_ATOF @@ -137,7 +138,6 @@ GMIO_INLINE float gmio_to_float32(const char* str); #endif #include -#include const char* gmio_stringstream_current_char( const struct gmio_stringstream* sstream) @@ -206,12 +206,9 @@ int gmio_get_float32(const char* str, float* value_ptr) #if defined(GMIO_STRINGSTREAM_USE_FAST_ATOF) const char* end_ptr = NULL; *value_ptr = fast_strtof(str, &end_ptr); -#elif defined(GMIO_HAVE_STRTOF_FUNC) /* Requires C99 */ - char* end_ptr = NULL; - *value_ptr = strtof(str, &end_ptr); #else char* end_ptr = NULL; - *value_ptr = (float)strtod(str, &end_ptr); + *value_ptr = gmio_strtof(str, &end_ptr); #endif return (end_ptr == str || errno == ERANGE) ? -1 : 0; } @@ -220,10 +217,8 @@ float gmio_to_float32(const char* str) { #if defined(GMIO_STRINGSTREAM_USE_FAST_ATOF) return fast_atof(str); -#elif defined(GMIO_HAVE_STRTOF_FUNC) /* Requires C99 */ - return strtof(str, NULL); #else - return (float)strtod(str, NULL); + return gmio_strtof(str, NULL); #endif } @@ -233,7 +228,7 @@ float gmio_stringstream_parse_float32(struct gmio_stringstream* sstream) return gmio_stringstream_fast_atof(sstream); #else char strbuff_ptr[64]; - struct gmio_string strbuff = { &strbuff_ptr[0], 0, sizeof(strbuff_ptr) }; + struct gmio_string strbuff = { strbuff_ptr, 0, sizeof(strbuff_ptr) }; gmio_stringstream_eat_word(sstream, &strbuff); return (float)atof(strbuff_ptr); #endif diff --git a/src/gmio_core/internal/stringstream_fast_atof.h b/src/gmio_core/internal/stringstream_fast_atof.h index 73d46ff..f01d481 100644 --- a/src/gmio_core/internal/stringstream_fast_atof.h +++ b/src/gmio_core/internal/stringstream_fast_atof.h @@ -10,6 +10,7 @@ #define GMIO_INTERNAL_STRINGSTREAM_FAST_ATOF_H #include "fast_atof.h" +#include "c99_math_compat.h" #include "stringstream.h" GMIO_INLINE uint32_t gmio_stringstream_strtoul10( @@ -105,12 +106,7 @@ GMIO_INLINE float gmio_stringstream_fast_atof(struct gmio_stringstream* sstream) /* Assume that the exponent is a whole number. * strtol10() will deal with both + and - signs, * but calculate as float to prevent overflow at FLT_MAX */ - value *= -#ifdef GMIO_HAVE_POWF_FUNC - powf(10.f, (float)gmio_stringstream_strtol10(sstream)); -#else - (float)pow(10., (double)gmio_stringstream_strtol10(sstream)); -#endif + value *= gmio_powf(10.f, (float)gmio_stringstream_strtol10(sstream)); } return negative ? -value : value; } diff --git a/src/gmio_core/internal/vecgeom_utils.h b/src/gmio_core/internal/vecgeom_utils.h index 11bfc3c..8d21b18 100644 --- a/src/gmio_core/internal/vecgeom_utils.h +++ b/src/gmio_core/internal/vecgeom_utils.h @@ -46,7 +46,7 @@ GMIO_INLINE void gmio_vec3d_normalize(struct gmio_vec3d* v); * Implementation */ -#include +#include "c99_math_compat.h" void gmio_vec3f_cross_product( const struct gmio_vec3f* u, @@ -88,7 +88,7 @@ double gmio_vec3d_sqr_length(const struct gmio_vec3d* v) void gmio_vec3f_normalize(struct gmio_vec3f* v) { - const float d = (float)sqrt(gmio_vec3f_sqr_length(v)); + const float d = gmio_sqrtf(gmio_vec3f_sqr_length(v)); if (d > 0.f) { v->x /= d; v->y /= d;