Fixed bug 2274 - SDL_ceil is incorrectly implemented when HAVE_LIBC is not defined
authorSam Lantinga <slouken@libsdl.org>
Wed, 27 Nov 2013 00:29:46 -0800
changeset 80338181c3a4a055
parent 8032 e2096994e8b8
child 8034 b80feead1d04
Fixed bug 2274 - SDL_ceil is incorrectly implemented when HAVE_LIBC is not defined

Ghassan Al-Mashareqa

The SDL_ceil function is implemented incorrectly when HAVE_CEIL is not defined (HAVE_LIBC not defined).

The following code:

double val = SDL_ceil(2.3);
printf("%g", val);

prints "2.0", as STD_ceil is defined as:

double
SDL_ceil(double x)
{
#ifdef HAVE_CEIL
return ceil(x);
#else
return (double)(int)((x)+0.5);
#endif /* HAVE_CEIL */
}

This functions is used in the SDL_BuildAudioResampleCVT function of the audio subsystem (SDL_audiocvt.c), and causes a bug in that function.
src/stdlib/SDL_stdlib.c
     1.1 --- a/src/stdlib/SDL_stdlib.c	Tue Nov 26 11:50:54 2013 -0300
     1.2 +++ b/src/stdlib/SDL_stdlib.c	Wed Nov 27 00:29:46 2013 -0800
     1.3 @@ -52,7 +52,12 @@
     1.4  #ifdef HAVE_CEIL
     1.5      return ceil(x);
     1.6  #else
     1.7 -    return (double)(int)((x)+0.5);
     1.8 +    double integer = SDL_floor(x);
     1.9 +    double fraction = x - integer;
    1.10 +    if (fraction > 0.0) {
    1.11 +        integer += 1.0;
    1.12 +    }
    1.13 +    return integer;
    1.14  #endif /* HAVE_CEIL */
    1.15  }
    1.16