From 292229f71579d1449690c470349730767828a4ac Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 10 Jan 2009 18:32:24 +0000 Subject: [PATCH] Fixed Visual C++ release build for Visual C++ 2005 * Some math functions become intrinsic in release mode, so we need to convert all the math functions into SDL math functions, like we did with the stdlib functions. * Constant initializers of 8-bit values become calls to memset() in release mode, but memset() itself is an intrinsic when explicitly called. So we'll just explicitly call memset() in those cases. --- acinclude.m4 | 15 ++++++++ configure.in | 7 ++-- include/SDL_config.h.in | 13 ++++++- include/SDL_config_win32.h | 12 +++++++ include/SDL_stdinc.h | 73 ++++++++++++++++++++++++++++++++++++++ src/audio/SDL_audiocvt.c | 16 +++------ src/libm/math.h | 62 +++++++++++++++++++++----------- src/video/SDL_fillrect.c | 64 ++++++++++++++++++++++++++++++--- src/video/SDL_gamma.c | 6 ++-- 9 files changed, 226 insertions(+), 42 deletions(-) diff --git a/acinclude.m4 b/acinclude.m4 index c408be4be..2c7d39ed6 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -1,3 +1,18 @@ +define(AC_CHECK_DEFINE,[dnl + AC_CACHE_CHECK(for $1 in $2, ac_cv_define_$1, + AC_EGREP_CPP([YES_IS_DEFINED], [ +#include <$2> +#ifdef $1 +YES_IS_DEFINED +#endif + ], ac_cv_define_$1=yes, ac_cv_define_$1=no) + ) + if test "$ac_cv_define_$1" = "yes" ; then + AC_DEFINE(HAVE_$1) + fi +])dnl +AC_DEFINE(HAVE_$1) + ############################################################################## dnl Configure Paths for Alsa dnl Some modifications by Richard Boulton diff --git a/configure.in b/configure.in index 1c57e1509..52e07ced5 100644 --- a/configure.in +++ b/configure.in @@ -129,6 +129,9 @@ if test x$enable_libc = xyes; then have_inttypes=yes fi + dnl Check for defines + AC_CHECK_DEFINE(M_PI, math.h) + dnl Checks for library functions. case "$host" in *-*-cygwin* | *-*-mingw32*) @@ -146,10 +149,10 @@ if test x$enable_libc = xyes; then if test x$ac_cv_func_strtod = xyes; then AC_DEFINE(HAVE_STRTOD) fi - AC_CHECK_FUNCS(malloc calloc realloc free getenv putenv unsetenv qsort abs bcopy memset memcpy memmove strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp sscanf snprintf vsnprintf iconv sigaction setjmp nanosleep) + AC_CHECK_FUNCS(malloc calloc realloc free getenv putenv unsetenv qsort abs bcopy memset memcpy memmove strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp sscanf snprintf vsnprintf copysign cos cosf fabs floor log pow scalbn sin sinf sqrt iconv sigaction setjmp nanosleep) - AC_CHECK_LIB(iconv, libiconv_open, [EXTRA_LDFLAGS="$EXTRA_LDFLAGS -liconv"]) AC_CHECK_LIB(m, pow, [EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"]) + AC_CHECK_LIB(iconv, libiconv_open, [EXTRA_LDFLAGS="$EXTRA_LDFLAGS -liconv"]) fi if test x$have_inttypes != xyes; then diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in index ae7442b4a..9692ad87c 100644 --- a/include/SDL_config.h.in +++ b/include/SDL_config.h.in @@ -129,7 +129,18 @@ #undef HAVE_SSCANF #undef HAVE_SNPRINTF #undef HAVE_VSNPRINTF -#undef HAVE_ICONV +#undef HAVE_M_PI +#undef HAVE_COPYSIGN +#undef HAVE_COS +#undef HAVE_COSF +#undef HAVE_FABS +#undef HAVE_FLOOR +#undef HAVE_LOG +#undef HAVE_POW +#undef HAVE_SCALBN +#undef HAVE_SIN +#undef HAVE_SINF +#undef HAVE_SQRT #undef HAVE_SIGACTION #undef HAVE_SETJMP #undef HAVE_NANOSLEEP diff --git a/include/SDL_config_win32.h b/include/SDL_config_win32.h index 535f4d1b9..e3b5fa883 100644 --- a/include/SDL_config_win32.h +++ b/include/SDL_config_win32.h @@ -116,6 +116,18 @@ typedef unsigned int uintptr_t; #define HAVE__STRICMP 1 #define HAVE__STRNICMP 1 #define HAVE_SSCANF 1 +#define HAVE_M_PI 1 +#define HAVE_COPYSIGN 1 +#define HAVE_COS 1 +#define HAVE_COSF 1 +#define HAVE_FABS 1 +#define HAVE_FLOOR 1 +#define HAVE_LOG 1 +#define HAVE_POW 1 +#define HAVE_SCALBN 1 +#define HAVE_SIN 1 +#define HAVE_SINF 1 +#define HAVE_SQRT 1 #else #define HAVE_STDARG_H 1 #define HAVE_STDDEF_H 1 diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h index 467cda64a..5eb3b1224 100644 --- a/include/SDL_stdinc.h +++ b/include/SDL_stdinc.h @@ -72,6 +72,9 @@ #ifdef HAVE_CTYPE_H # include #endif +#ifdef HAVE_MATH_H +# include +#endif #ifdef HAVE_ICONV_H # include #endif @@ -639,6 +642,76 @@ extern DECLSPEC int SDLCALL SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap); #endif +#ifndef HAVE_M_PI +#define M_PI 3.14159265358979323846264338327950288 /* pi */ +#endif + +#ifdef HAVE_COPYSIGN +#define SDL_copysign copysign +#else +extern DECLSPEC double SDLCALL SDL_copysign(double x, double y); +#endif + +#ifdef HAVE_COS +#define SDL_cos cos +#else +extern DECLSPEC double SDLCALL SDL_cos(double x); +#endif + +#ifdef HAVE_COSF +#define SDL_cosf cosf +#else +#define SDL_cosf(x) (float)SDL_cos((double)x) +#endif + +#ifdef HAVE_FABS +#define SDL_fabs fabs +#else +extern DECLSPEC double SDLCALL SDL_fabs(double x); +#endif + +#ifdef HAVE_FLOOR +#define SDL_floor floor +#else +extern DECLSPEC double SDLCALL SDL_floor(double x); +#endif + +#ifdef HAVE_LOG +#define SDL_log log +#else +extern DECLSPEC double SDLCALL SDL_log(double x); +#endif + +#ifdef HAVE_POW +#define SDL_pow pow +#else +extern DECLSPEC double SDLCALL SDL_pow(double x, double y); +#endif + +#ifdef HAVE_SCALBN +#define SDL_scalbn scalbn +#else +extern DECLSPEC double SDLCALL SDL_scalbn(double x, int n); +#endif + +#ifdef HAVE_SIN +#define SDL_sin sin +#else +extern DECLSPEC double SDLCALL SDL_sin(double x); +#endif + +#ifdef HAVE_SINF +#define SDL_sinf sinf +#else +#define SDL_sinf(x) (float)SDL_sin((double)x) +#endif + +#ifdef HAVE_SQRT +#define SDL_sqrt sqrt +#else +extern DECLSPEC double SDLCALL SDL_sqrt(double x); +#endif + /* The SDL implementation of iconv() returns these error codes */ #define SDL_ICONV_ERROR (size_t)-1 #define SDL_ICONV_E2BIG (size_t)-2 diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c index b8e54e595..f0eec35de 100644 --- a/src/audio/SDL_audiocvt.c +++ b/src/audio/SDL_audiocvt.c @@ -26,8 +26,6 @@ #include "SDL_audio.h" #include "SDL_audio_c.h" -#include "../libm/math.h" - //#define DEBUG_CONVERT /* These are fractional multiplication routines. That is, their inputs @@ -1658,13 +1656,9 @@ SDL_BuildWindowedSinc(SDL_AudioCVT * cvt, SDL_AudioFormat format, if (i == m / 2) { fSinc[i] = two_pi_fc; } else { - fSinc[i] = - sinf(two_pi_fc * ((float) i - m_over_two)) / ((float) i - - m_over_two); + fSinc[i] = SDL_sinf(two_pi_fc * ((float) i - m_over_two)) / ((float) i - m_over_two); /* Apply blackman window */ - fSinc[i] *= - 0.42f - 0.5f * cosf(two_pi_over_m * (float) i) + - 0.08f * cosf(four_pi_over_m * (float) i); + fSinc[i] *= 0.42f - 0.5f * SDL_cosf(two_pi_over_m * (float) i) + 0.08f * SDL_cosf(four_pi_over_m * (float) i); } norm_sum += fSinc[i] < 0 ? -fSinc[i] : fSinc[i]; /* fabs(fSinc[i]); */ } @@ -1740,7 +1734,7 @@ SDL_GCD(int a, int b) static void SDLCALL SDL_Resample(SDL_AudioCVT * cvt, SDL_AudioFormat format) { - int i, j; + int i; #ifdef DEBUG_CONVERT printf("Converting audio rate via proper resampling (mono)\n"); @@ -1752,8 +1746,8 @@ SDL_Resample(SDL_AudioCVT * cvt, SDL_AudioFormat format) for (i = cvt->len / sizeof (type); i; --i) { \ src--; \ dst[-1] = src[0]; \ - for( j = -cvt->len_mult; j < -1; ++j ) { \ - dst[j] = 0; \ + if (cvt->len_mult > 1) { \ + SDL_memset(dst-cvt->len_mult, 0, cvt->len_mult-1); \ } \ dst -= cvt->len_mult; \ } \ diff --git a/src/libm/math.h b/src/libm/math.h index a1552789d..228f6de99 100644 --- a/src/libm/math.h +++ b/src/libm/math.h @@ -20,34 +20,56 @@ slouken@libsdl.org */ #include "SDL_config.h" +#include "SDL_stdinc.h" -#ifdef HAVE_MATH_H -#define _USE_MATH_DEFINES -#include +/* Math routines from uClibc: http://www.uclibc.org */ + +#ifdef HAVE_COPYSIGN +#define copysign SDL_uclibc_copysign #else +#define copysign SDL_copysign +#endif -/* Math routines from uClibc: http://www.uclibc.org */ +#ifdef HAVE_COS +#define cos SDL_uclibc_cos +#else +#define cos SDL_cos +#endif -#define M_PI 3.14159265358979323846264338327950288 /* pi */ +#ifdef HAVE_FABS +#define fabs SDL_uclibc_fabs +#else +#define fabs SDL_fabs +#endif -extern double __ieee754_log(double x); -extern double __ieee754_pow(double x, double y); -extern double __ieee754_sqrt(double x); +#ifdef HAVE_FLOOR +#define floor SDL_uclibc_floor +#else +#define floor SDL_floor +#endif + +#ifndef HAVE_LOG +#define __ieee754_log SDL_log +#endif -#define log(x) __ieee754_log(x) -#define pow(x, y) __ieee754_pow(x, y) -#define sqrt(x) __ieee754_sqrt(x) +#ifndef HAVE_POW +#define __ieee754_pow SDL_pow +#endif -extern double copysign(double x, double y); -extern double cos(double x); -extern double fabs(double x); -extern double floor(double x); -extern double scalbn(double x, int n); -extern double sin(double x); +#ifdef HAVE_SCALBN +#define scalbn SDL_uclibc_scalbn +#else +#define scalbn SDL_scalbn +#endif -#define sinf(x) (float)sin((double)x) -#define cosf(x) (float)cos((double)x) +#ifdef HAVE_SIN +#define sin SDL_uclibc_sin +#else +#define sin SDL_sin +#endif -#endif /* HAVE_MATH_H */ +#ifndef HAVE_SQRT +#define __ieee754_sqrt SDL_sqrt +#endif /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/SDL_fillrect.c b/src/video/SDL_fillrect.c index 3ced3e7fe..f86e675f1 100644 --- a/src/video/SDL_fillrect.c +++ b/src/video/SDL_fillrect.c @@ -66,7 +66,7 @@ SDL_FillRect##bpp##SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \ int i, n = w * bpp; \ Uint8 *p = pixels; \ \ - if (n > 15) { \ + if (n > 63) { \ int adjust = 16 - ((uintptr_t)p & 15); \ if (adjust < 16) { \ n -= adjust; \ @@ -92,7 +92,35 @@ SDL_FillRect##bpp##SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \ SSE_END; \ } -DEFINE_SSE_FILLRECT(1, Uint8) +static void +SDL_FillRect1SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h) +{ + SSE_BEGIN; + + while (h--) { + int i, n = w; + Uint8 *p = pixels; + + if (n > 63) { + int adjust = 16 - ((uintptr_t)p & 15); + if (adjust) { + n -= adjust; + SDL_memset(p, color, adjust); + p += adjust; + } + SSE_WORK; + } + if (n & 63) { + int remainder = (n & 63); + SDL_memset(p, color, remainder); + p += remainder; + } + pixels += pitch; + } + + SSE_END; +} +/*DEFINE_SSE_FILLRECT(1, Uint8)*/ DEFINE_SSE_FILLRECT(2, Uint16) DEFINE_SSE_FILLRECT(4, Uint32) @@ -131,7 +159,7 @@ SDL_FillRect##bpp##MMX(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \ int i, n = w * bpp; \ Uint8 *p = pixels; \ \ - if (n > 7) { \ + if (n > 63) { \ int adjust = 8 - ((uintptr_t)p & 7); \ if (adjust < 8) { \ n -= adjust; \ @@ -157,7 +185,35 @@ SDL_FillRect##bpp##MMX(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \ MMX_END; \ } -DEFINE_MMX_FILLRECT(1, Uint8) +static void +SDL_FillRect1MMX(Uint8 *pixels, int pitch, Uint32 color, int w, int h) +{ + MMX_BEGIN; + + while (h--) { + int i, n = w; + Uint8 *p = pixels; + + if (n > 63) { + int adjust = 8 - ((uintptr_t)p & 7); + if (adjust) { + n -= adjust; + SDL_memset(p, color, adjust); + p += adjust; + } + MMX_WORK; + } + if (n & 63) { + int remainder = (n & 63); + SDL_memset(p, color, remainder); + p += remainder; + } + pixels += pitch; + } + + MMX_END; +} +/*DEFINE_MMX_FILLRECT(1, Uint8)*/ DEFINE_MMX_FILLRECT(2, Uint16) DEFINE_MMX_FILLRECT(4, Uint32) diff --git a/src/video/SDL_gamma.c b/src/video/SDL_gamma.c index b68f37343..b551d0b73 100644 --- a/src/video/SDL_gamma.c +++ b/src/video/SDL_gamma.c @@ -23,8 +23,6 @@ /* Gamma correction support */ -#include "../libm/math.h" - #include "SDL_sysvideo.h" @@ -52,7 +50,7 @@ CalculateGammaRamp(float gamma, Uint16 * ramp) int value; gamma = 1.0f / gamma; for (i = 0; i < 256; ++i) { - value = (int) (pow((double) i / 256.0, gamma) * 65535.0 + 0.5); + value = (int) (SDL_pow((double) i / 256.0, gamma) * 65535.0 + 0.5); if (value > 65535) { value = 65535; } @@ -75,7 +73,7 @@ CalculateGammaFromRamp(float *gamma, Uint16 * ramp) if ((ramp[i] != 0) && (ramp[i] != 65535)) { double B = (double) i / 256.0; double A = ramp[i] / 65535.0; - sum += (float) (log(A) / log(B)); + sum += (float) (SDL_log(A) / SDL_log(B)); count++; } }