Commit 93ccdee8c1895e88ca77347711257ef0549a438a

Sam Lantinga 2020-12-23T13:47:49

Fixed bug 5404 - stdlib: Added SDL_round, SDL_roundf, SDL_lround and 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.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 57c5b55..8772447 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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()
@@ -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})
diff --git a/configure b/configure
index eb67bd8..21c3bbb 100755
--- a/configure
+++ b/configure
@@ -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"
diff --git a/configure.ac b/configure.ac
index 1f262cb..9c0bda2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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)
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 754b570..e3aae65 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -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
diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in
index 3a2a714..07b94f4 100644
--- a/include/SDL_config.h.in
+++ b/include/SDL_config.h.in
@@ -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
diff --git a/include/SDL_config_android.h b/include/SDL_config_android.h
index d1148cf..b53096b 100644
--- a/include/SDL_config_android.h
+++ b/include/SDL_config_android.h
@@ -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
diff --git a/include/SDL_config_iphoneos.h b/include/SDL_config_iphoneos.h
index f06ad33..2a8ae54 100644
--- a/include/SDL_config_iphoneos.h
+++ b/include/SDL_config_iphoneos.h
@@ -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
diff --git a/include/SDL_config_macosx.h b/include/SDL_config_macosx.h
index 4b45f16..e98e360 100644
--- a/include/SDL_config_macosx.h
+++ b/include/SDL_config_macosx.h
@@ -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
diff --git a/include/SDL_config_os2.h b/include/SDL_config_os2.h
index 1922217..c9e9334 100644
--- a/include/SDL_config_os2.h
+++ b/include/SDL_config_os2.h
@@ -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 */
diff --git a/include/SDL_config_pandora.h b/include/SDL_config_pandora.h
index 8728a63..974922e 100644
--- a/include/SDL_config_pandora.h
+++ b/include/SDL_config_pandora.h
@@ -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
diff --git a/include/SDL_config_windows.h b/include/SDL_config_windows.h
index 18a3638..5190148 100644
--- a/include/SDL_config_windows.h
+++ b/include/SDL_config_windows.h
@@ -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
diff --git a/include/SDL_config_winrt.h b/include/SDL_config_winrt.h
index 02079f5..2144b79 100644
--- a/include/SDL_config_winrt.h
+++ b/include/SDL_config_winrt.h
@@ -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
diff --git a/include/SDL_config_wiz.h b/include/SDL_config_wiz.h
index f65af8e..3d176fb 100644
--- a/include/SDL_config_wiz.h
+++ b/include/SDL_config_wiz.h
@@ -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
diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h
index 91ccaa4..2ada8d6 100644
--- a/include/SDL_stdinc.h
+++ b/include/SDL_stdinc.h
@@ -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);
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 8a1cc92..5d6eb62 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -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
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index f4c2232..719113b 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -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)
diff --git a/src/stdlib/SDL_stdlib.c b/src/stdlib/SDL_stdlib.c
index 5334a51..5ffa284 100644
--- a/src/stdlib/SDL_stdlib.c
+++ b/src/stdlib/SDL_stdlib.c
@@ -365,6 +365,50 @@ SDL_powf(float x, float y)
 }
 
 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)
 {
 #if defined(HAVE_SCALBN)
diff --git a/src/video/winrt/SDL_winrtpointerinput.cpp b/src/video/winrt/SDL_winrtpointerinput.cpp
index 9b36aaa..534bd4d 100644
--- a/src/video/winrt/SDL_winrtpointerinput.cpp
+++ b/src/video/winrt/SDL_winrtpointerinput.cpp
@@ -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)
 {
@@ -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
diff --git a/src/video/x11/SDL_x11modes.c b/src/video/x11/SDL_x11modes.c
index 79ad247..ebd0a39 100644
--- a/src/video/x11/SDL_x11modes.c
+++ b/src/video/x11/SDL_x11modes.c
@@ -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