Skip to content

Commit

Permalink
Fixed bug 5404 - stdlib: Added SDL_round, SDL_roundf, SDL_lround and …
Browse files Browse the repository at this point in the history
…SDL_lroundf

Cameron Cawley

stdlib: Added SDL_round, SDL_roundf, SDL_lround and SDL_lroundf

The default implementation is based on the one used in the Windows RT video driver.
  • Loading branch information
slouken committed Dec 23, 2020
1 parent d0b8295 commit 93ccdee
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 19 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Expand Up @@ -783,8 +783,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
trunc truncf)
log logf log10 log10f lround lroundf pow powf round roundf scalbn scalbnf
sin sinf sqrt sqrtf tan tanf trunc truncf)
string(TOUPPER ${_FN} _UPPER)
set(HAVE_${_UPPER} 1)
endforeach()
Expand Down Expand Up @@ -843,8 +843,8 @@ if(LIBC)
foreach(_FN
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 trunc truncf)
lround lroundf pow powf round roundf scalbn scalbnf sin sinf sqrt
sqrtf tan tanf acos acosf asin asinf trunc truncf)
string(TOUPPER ${_FN} _UPPER)
set(_HAVEVAR "HAVE_${_UPPER}")
check_symbol_exists("${_FN}" "math.h" ${_HAVEVAR})
Expand Down
2 changes: 1 addition & 1 deletion configure
Expand Up @@ -17125,7 +17125,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 trunc truncf 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 lround lroundf pow powf round roundf 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"
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -348,7 +348,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 wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp 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 elf_aux_info 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 trunc truncf 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 lround lroundf pow powf round roundf 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)
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_config.h.cmake
Expand Up @@ -166,8 +166,12 @@
#cmakedefine HAVE_LOGF 1
#cmakedefine HAVE_LOG10 1
#cmakedefine HAVE_LOG10F 1
#cmakedefine HAVE_LROUND 1
#cmakedefine HAVE_LROUNDF 1
#cmakedefine HAVE_POW 1
#cmakedefine HAVE_POWF 1
#cmakedefine HAVE_ROUND 1
#cmakedefine HAVE_ROUNDF 1
#cmakedefine HAVE_SCALBN 1
#cmakedefine HAVE_SCALBNF 1
#cmakedefine HAVE_SIN 1
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_config.h.in
Expand Up @@ -170,8 +170,12 @@
#undef HAVE_LOGF
#undef HAVE_LOG10
#undef HAVE_LOG10F
#undef HAVE_LROUND
#undef HAVE_LROUNDF
#undef HAVE_POW
#undef HAVE_POWF
#undef HAVE_ROUND
#undef HAVE_ROUNDF
#undef HAVE_SCALBN
#undef HAVE_SCALBNF
#undef HAVE_SIN
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_config_android.h
Expand Up @@ -111,8 +111,12 @@
#define HAVE_LOGF 1
#define HAVE_LOG10 1
#define HAVE_LOG10F 1
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_POW 1
#define HAVE_POWF 1
#define HAVE_ROUND 1
#define HAVE_ROUNDF 1
#define HAVE_SCALBN 1
#define HAVE_SCALBNF 1
#define HAVE_SIN 1
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_config_iphoneos.h
Expand Up @@ -112,8 +112,12 @@
#define HAVE_LOGF 1
#define HAVE_LOG10 1
#define HAVE_LOG10F 1
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_POW 1
#define HAVE_POWF 1
#define HAVE_ROUND 1
#define HAVE_ROUNDF 1
#define HAVE_SCALBN 1
#define HAVE_SCALBNF 1
#define HAVE_SIN 1
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_config_macosx.h
Expand Up @@ -115,8 +115,12 @@
#define HAVE_LOGF 1
#define HAVE_LOG10 1
#define HAVE_LOG10F 1
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_POW 1
#define HAVE_POWF 1
#define HAVE_ROUND 1
#define HAVE_ROUNDF 1
#define HAVE_SCALBN 1
#define HAVE_SCALBNF 1
#define HAVE_SIN 1
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_config_os2.h
Expand Up @@ -172,8 +172,12 @@
/* #undef HAVE_LOGF */
#define HAVE_LOG10 1
/* #undef HAVE_LOG10F */
/* #undef HAVE_LROUND */
/* #undef HAVE_LROUNDF */
#define HAVE_POW 1
/* #undef HAVE_POWF */
/* #undef HAVE_ROUND */
/* #undef HAVE_ROUNDF */
#define HAVE_SIN 1
/* #undef HAVE_SINF */
/* #undef HAVE_SCALBN */
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_config_pandora.h
Expand Up @@ -95,6 +95,10 @@
#define HAVE_FLOOR 1
#define HAVE_LOG 1
#define HAVE_LOG10 1
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_ROUND 1
#define HAVE_ROUNDF 1
#define HAVE_SCALBN 1
#define HAVE_SIN 1
#define HAVE_SINF 1
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_config_windows.h
Expand Up @@ -173,6 +173,10 @@ typedef unsigned int uintptr_t;
#if _MSC_VER >= 1800
#define HAVE_STRTOLL 1
#define HAVE_VSSCANF 1
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_ROUND 1
#define HAVE_ROUNDF 1
#define HAVE_SCALBN 1
#define HAVE_SCALBNF 1
#define HAVE_TRUNC 1
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_config_winrt.h
Expand Up @@ -172,8 +172,12 @@ typedef unsigned int uintptr_t;
#define HAVE_LOGF 1
#define HAVE_LOG10 1
#define HAVE_LOG10F 1
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_POW 1
#define HAVE_POWF 1
#define HAVE_ROUND 1
#define HAVE_ROUNDF 1
#define HAVE__SCALB 1
#define HAVE_SIN 1
#define HAVE_SINF 1
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_config_wiz.h
Expand Up @@ -107,8 +107,12 @@
#define HAVE_LOGF 1
#define HAVE_LOG10 1
#define HAVE_LOG10F 1
#define HAVE_LROUND 1
#define HAVE_LROUNDF 1
#define HAVE_POW 1
#define HAVE_POWF 1
#define HAVE_ROUND 1
#define HAVE_ROUNDF 1
#define HAVE_SCALBN 1
#define HAVE_SCALBNF 1
#define HAVE_SIN 1
Expand Down
4 changes: 4 additions & 0 deletions include/SDL_stdinc.h
Expand Up @@ -549,6 +549,10 @@ extern DECLSPEC double SDLCALL SDL_log10(double x);
extern DECLSPEC float SDLCALL SDL_log10f(float x);
extern DECLSPEC double SDLCALL SDL_pow(double x, double y);
extern DECLSPEC float SDLCALL SDL_powf(float x, float y);
extern DECLSPEC double SDLCALL SDL_round(double x);
extern DECLSPEC float SDLCALL SDL_roundf(float x);
extern DECLSPEC long SDLCALL SDL_lround(double x);
extern DECLSPEC long SDLCALL SDL_lroundf(float x);
extern DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
extern DECLSPEC float SDLCALL SDL_scalbnf(float x, int n);
extern DECLSPEC double SDLCALL SDL_sin(double x);
Expand Down
4 changes: 4 additions & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Expand Up @@ -788,4 +788,8 @@
#define SDL_GameControllerGetSensorData SDL_GameControllerGetSensorData_REAL
#define SDL_wcscasecmp SDL_wcscasecmp_REAL
#define SDL_wcsncasecmp SDL_wcsncasecmp_REAL
#define SDL_round SDL_round_REAL
#define SDL_roundf SDL_roundf_REAL
#define SDL_lround SDL_lround_REAL
#define SDL_lroundf SDL_lroundf_REAL
#define SDL_SoftStretchLinear SDL_SoftStretchLinear_REAL
4 changes: 4 additions & 0 deletions src/dynapi/SDL_dynapi_procs.h
Expand Up @@ -849,4 +849,8 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_GameControllerIsSensorEnabled,(SDL_GameController *
SDL_DYNAPI_PROC(int,SDL_GameControllerGetSensorData,(SDL_GameController *a, SDL_SensorType b, float *c, int d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_wcscasecmp,(const wchar_t *a, const wchar_t *b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_wcsncasecmp,(const wchar_t *a, const wchar_t *b, size_t c),(a,b,c),return)
SDL_DYNAPI_PROC(double,SDL_round,(double a),(a),return)
SDL_DYNAPI_PROC(float,SDL_roundf,(float a),(a),return)
SDL_DYNAPI_PROC(long,SDL_lround,(double a),(a),return)
SDL_DYNAPI_PROC(long,SDL_lroundf,(float a),(a),return)
SDL_DYNAPI_PROC(int,SDL_SoftStretchLinear,(SDL_Surface *a, const SDL_Rect *b, SDL_Surface *c, const SDL_Rect *d),(a,b,c,d),return)
44 changes: 44 additions & 0 deletions src/stdlib/SDL_stdlib.c
Expand Up @@ -364,6 +364,50 @@ SDL_powf(float x, float y)
#endif
}

double
SDL_round(double arg)
{
#if defined HAVE_ROUND
return round(arg);
#else
if (arg >= 0.0) {
return SDL_floor(arg + 0.5);
} else {
return SDL_ceil(arg - 0.5);
}
#endif
}

float
SDL_roundf(float arg)
{
#if defined HAVE_ROUNDF
return roundf(arg);
#else
return (float)SDL_round((double)arg);
#endif
}

long
SDL_lround(double arg)
{
#if defined HAVE_LROUND
return lround(arg);
#else
return (long)SDL_round(arg);
#endif
}

long
SDL_lroundf(float arg)
{
#if defined HAVE_LROUNDF
return lroundf(arg);
#else
return (long)SDL_round((double)arg);
#endif
}

double
SDL_scalbn(double x, int n)
{
Expand Down
14 changes: 2 additions & 12 deletions src/video/winrt/SDL_winrtpointerinput.cpp
Expand Up @@ -116,16 +116,6 @@ WINRT_TransformCursorPosition(SDL_Window * window,
return outputPosition;
}

static inline int
_lround(float arg)
{
if (arg >= 0.0f) {
return (int)floor(arg + 0.5f);
} else {
return (int)ceil(arg - 0.5f);
}
}

Uint8
WINRT_GetSDLButtonForPointerPoint(Windows::UI::Input::PointerPoint ^pt)
{
Expand Down Expand Up @@ -389,8 +379,8 @@ WINRT_ProcessMouseMovedEvent(SDL_Window * window, Windows::Devices::Input::Mouse
window,
0,
1,
_lround(mouseDeltaInSDLWindowCoords.X),
_lround(mouseDeltaInSDLWindowCoords.Y));
SDL_lroundf(mouseDeltaInSDLWindowCoords.X),
SDL_lroundf(mouseDeltaInSDLWindowCoords.Y));
}

#endif // SDL_VIDEO_DRIVER_WINRT
2 changes: 1 addition & 1 deletion src/video/x11/SDL_x11modes.c
Expand Up @@ -257,7 +257,7 @@ static int
CalculateXRandRRefreshRate(const XRRModeInfo *info)
{
return (info->hTotal && info->vTotal) ?
round(((double)info->dotClock / (double)(info->hTotal * info->vTotal))) : 0;
SDL_round(((double)info->dotClock / (double)(info->hTotal * info->vTotal))) : 0;
}

static SDL_bool
Expand Down

0 comments on commit 93ccdee

Please sign in to comment.