From d292f6bd4f5e50ed4813cf7deac8aba667059a3c Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Fri, 10 Apr 2020 12:17:14 -0400 Subject: [PATCH] stdlib: Add SDL_trunc and SDL_truncf --- CMakeLists.txt | 5 +++-- configure | 2 +- configure.ac | 2 +- include/SDL_stdinc.h | 2 ++ src/dynapi/SDL_dynapi_overrides.h | 2 ++ src/dynapi/SDL_dynapi_procs.h | 2 ++ src/stdlib/SDL_stdlib.c | 21 +++++++++++++++++++++ 7 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 93603de2c..c783d2ca9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -767,7 +767,8 @@ if(LIBC) _stricmp _strnicmp strtok_s sscanf acos acosf asin asinf atan atanf atan2 atan2f ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf fmod fmodf - log logf log10 log10f pow powf scalbn scalbnf sin sinf sqrt sqrtf tan tanf) + log logf log10 log10f pow powf scalbn scalbnf sin sinf sqrt sqrtf tan tanf + trunc truncf) string(TOUPPER ${_FN} _UPPER) set(HAVE_${_UPPER} 1) endforeach() @@ -817,7 +818,7 @@ if(LIBC) atan atan2 atanf atan2f ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf fmod fmodf log logf log10 log10f pow powf scalbn scalbnf sin sinf sqrt sqrtf tan tanf acos acosf - asin asinf) + asin asinf trunc truncf) string(TOUPPER ${_FN} _UPPER) set(_HAVEVAR "HAVE_${_UPPER}") check_function_exists("${_FN}" ${_HAVEVAR}) diff --git a/configure b/configure index 60d536246..a08ec422c 100755 --- a/configure +++ b/configure @@ -17132,7 +17132,7 @@ if test "x$ac_cv_lib_m_pow" = xyes; then : LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm" fi - for ac_func in acos acosf asin asinf atan atanf atan2 atan2f ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf fmod fmodf log logf log10 log10f pow powf scalbn scalbnf sin sinf sqrt sqrtf tan tanf + for ac_func in acos acosf asin asinf atan atanf atan2 atan2f ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf trunc truncf fmod fmodf log logf log10 log10f pow powf scalbn scalbnf sin sinf sqrt sqrtf tan tanf do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/configure.ac b/configure.ac index 3ada39bc1..e2c9ef1f8 100644 --- a/configure.ac +++ b/configure.ac @@ -339,7 +339,7 @@ if test x$enable_libc = xyes; then AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcsdup wcsstr wcscmp wcsncmp strlen strlcpy strlcat _strrev _strupr _strlwr strchr strrchr strstr strtok_r itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval poll _Exit) AC_CHECK_LIB(m, pow, [LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"]) - AC_CHECK_FUNCS(acos acosf asin asinf atan atanf atan2 atan2f ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf fmod fmodf log logf log10 log10f pow powf scalbn scalbnf sin sinf sqrt sqrtf tan tanf) + AC_CHECK_FUNCS(acos acosf asin asinf atan atanf atan2 atan2f ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf trunc truncf fmod fmodf log logf log10 log10f pow powf scalbn scalbnf sin sinf sqrt sqrtf tan tanf) AC_CHECK_LIB(iconv, iconv_open, [LIBS="$LIBS -liconv"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -liconv"]) AC_CHECK_FUNCS(iconv) diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h index d96e18bc2..ad1344d36 100644 --- a/include/SDL_stdinc.h +++ b/include/SDL_stdinc.h @@ -529,6 +529,8 @@ extern DECLSPEC double SDLCALL SDL_fabs(double x); extern DECLSPEC float SDLCALL SDL_fabsf(float x); extern DECLSPEC double SDLCALL SDL_floor(double x); extern DECLSPEC float SDLCALL SDL_floorf(float x); +extern DECLSPEC double SDLCALL SDL_trunc(double x); +extern DECLSPEC float SDLCALL SDL_truncf(float x); extern DECLSPEC double SDLCALL SDL_fmod(double x, double y); extern DECLSPEC float SDLCALL SDL_fmodf(float x, float y); extern DECLSPEC double SDLCALL SDL_log(double x); diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index cf0dc1287..4474a519c 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -761,3 +761,5 @@ #define SDL_UnlockSensors SDL_UnlockSensors_REAL #define SDL_Metal_GetLayer SDL_Metal_GetLayer_REAL #define SDL_Metal_GetDrawableSize SDL_Metal_GetDrawableSize_REAL +#define SDL_trunc SDL_trunc_REAL +#define SDL_truncf SDL_truncf_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 9bbcc6cce..c4822a6c8 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -820,3 +820,5 @@ SDL_DYNAPI_PROC(void,SDL_LockSensors,(void),(),) SDL_DYNAPI_PROC(void,SDL_UnlockSensors,(void),(),) SDL_DYNAPI_PROC(void*,SDL_Metal_GetLayer,(SDL_MetalView a),(a),return) SDL_DYNAPI_PROC(void,SDL_Metal_GetDrawableSize,(SDL_Window *a, int *b, int *c),(a,b,c),) +SDL_DYNAPI_PROC(double,SDL_trunc,(double a),(a),return) +SDL_DYNAPI_PROC(float,SDL_truncf,(float a),(a),return) diff --git a/src/stdlib/SDL_stdlib.c b/src/stdlib/SDL_stdlib.c index c2774cb2c..848a1fd05 100644 --- a/src/stdlib/SDL_stdlib.c +++ b/src/stdlib/SDL_stdlib.c @@ -260,6 +260,27 @@ SDL_floorf(float x) #endif } +double +SDL_trunc(double x) +{ +#if defined(HAVE_TRUNC) + return trunc(x); +#else + /* !!! FIXME: there are more formal (correct!) ways to do this. */ + return (double) ((Sint64) x); +#endif +} + +float +SDL_truncf(float x) +{ +#if defined(HAVE_TRUNCF) + return truncf(x); +#else + return (float)SDL_trunc((double)x); +#endif +} + double SDL_fmod(double x, double y) {