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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
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