2 Simple DirectMedia Layer
3 Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
23 #define SDL_DISABLE_ANALYZE_MACROS 1
26 #include "../SDL_internal.h"
28 /* This file contains portable stdlib functions for SDL */
30 #include "SDL_stdinc.h"
31 #include "../libm/math_libm.h"
37 #if defined(HAVE_ATAN)
40 return SDL_uclibc_atan(x);
47 #if defined(HAVE_ATANF)
50 return (float)SDL_atan((double)x);
55 SDL_atan2(double x, double y)
57 #if defined(HAVE_ATAN2)
60 return SDL_uclibc_atan2(x, y);
65 SDL_atan2f(float x, float y)
67 #if defined(HAVE_ATAN2F)
70 return (float)SDL_atan2((double)x, (double)y);
77 #if defined(HAVE_ACOS)
84 result = SDL_atan(SDL_sqrt(1.0 - val * val) / val);
97 #if defined(HAVE_ACOSF)
100 return (float)SDL_acos((double)val);
107 #if defined(HAVE_ASIN)
112 result = -(M_PI / 2.0);
114 result = (M_PI / 2.0) - SDL_acos(val);
123 #if defined(HAVE_ASINF)
126 return (float)SDL_asin((double)val);
133 #if defined(HAVE_CEIL)
136 double integer = SDL_floor(x);
137 double fraction = x - integer;
138 if (fraction > 0.0) {
142 #endif /* HAVE_CEIL */
148 #if defined(HAVE_CEILF)
151 return (float)SDL_ceil((float)x);
156 SDL_copysign(double x, double y)
158 #if defined(HAVE_COPYSIGN)
159 return copysign(x, y);
160 #elif defined(HAVE__COPYSIGN)
161 return _copysign(x, y);
162 #elif defined(__WATCOMC__) && defined(__386__)
163 /* this is nasty as hell, but it works.. */
164 unsigned int *xi = (unsigned int *) &x,
165 *yi = (unsigned int *) &y;
166 xi[1] = (yi[1] & 0x80000000) | (xi[1] & 0x7fffffff);
169 return SDL_uclibc_copysign(x, y);
170 #endif /* HAVE_COPYSIGN */
174 SDL_copysignf(float x, float y)
176 #if defined(HAVE_COPYSIGNF)
177 return copysignf(x, y);
179 return (float)SDL_copysign((double)x, (double)y);
186 #if defined(HAVE_COS)
189 return SDL_uclibc_cos(x);
196 #if defined(HAVE_COSF)
199 return (float)SDL_cos((double)x);
206 #if defined(HAVE_EXP)
209 return SDL_uclibc_exp(x);
216 #if defined(HAVE_EXPF)
219 return (float)SDL_exp((double)x);
226 #if defined(HAVE_FABS)
229 return SDL_uclibc_fabs(x);
236 #if defined(HAVE_FABSF)
239 return (float)SDL_fabs((double)x);
246 #if defined(HAVE_FLOOR)
249 return SDL_uclibc_floor(x);
256 #if defined(HAVE_FLOORF)
259 return (float)SDL_floor((double)x);
264 SDL_fmod(double x, double y)
266 #if defined(HAVE_FMOD)
269 return SDL_uclibc_fmod(x, y);
274 SDL_fmodf(float x, float y)
276 #if defined(HAVE_FMODF)
279 return (float)SDL_fmod((double)x, (double)y);
286 #if defined(HAVE_LOG)
289 return SDL_uclibc_log(x);
296 #if defined(HAVE_LOGF)
299 return (float)SDL_log((double)x);
306 #if defined(HAVE_LOG10)
309 return SDL_uclibc_log10(x);
316 #if defined(HAVE_LOG10F)
319 return (float)SDL_log10((double)x);
324 SDL_pow(double x, double y)
326 #if defined(HAVE_POW)
329 return SDL_uclibc_pow(x, y);
334 SDL_powf(float x, float y)
336 #if defined(HAVE_POWF)
339 return (float)SDL_pow((double)x, (double)y);
344 SDL_scalbn(double x, int n)
346 #if defined(HAVE_SCALBN)
348 #elif defined(HAVE__SCALB)
350 #elif defined(HAVE_LIBC) && defined(HAVE_FLOAT_H) && (FLT_RADIX == 2)
351 /* from scalbn(3): If FLT_RADIX equals 2 (which is
352 * usual), then scalbn() is equivalent to ldexp(3). */
355 return SDL_uclibc_scalbn(x, n);
360 SDL_scalbnf(float x, int n)
362 #if defined(HAVE_SCALBNF)
363 return scalbnf(x, n);
365 return (float)SDL_scalbn((double)x, n);
372 #if defined(HAVE_SIN)
375 return SDL_uclibc_sin(x);
382 #if defined(HAVE_SINF)
385 return (float)SDL_sin((double)x);
392 #if defined(HAVE_SQRT)
395 return SDL_uclibc_sqrt(x);
402 #if defined(HAVE_SQRTF)
405 return (float)SDL_sqrt((double)x);
412 #if defined(HAVE_TAN)
415 return SDL_uclibc_tan(x);
422 #if defined(HAVE_TANF)
425 return (float)SDL_tan((double)x);
431 #if defined(HAVE_ABS)
434 return ((x) < 0 ? -(x) : (x));
438 #if defined(HAVE_CTYPE_H)
439 int SDL_isdigit(int x) { return isdigit(x); }
440 int SDL_isspace(int x) { return isspace(x); }
441 int SDL_toupper(int x) { return toupper(x); }
442 int SDL_tolower(int x) { return tolower(x); }
444 int SDL_isdigit(int x) { return ((x) >= '0') && ((x) <= '9'); }
445 int SDL_isspace(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n') || ((x) == '\f') || ((x) == '\v'); }
446 int SDL_toupper(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A'+((x)-'a')) : (x); }
447 int SDL_tolower(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a'+((x)-'A')) : (x); }
452 /* These are some C runtime intrinsics that need to be defined */
454 #if defined(_MSC_VER)
458 __declspec(selectany) int _fltused = 1;
461 /* The optimizer on Visual Studio 2005 and later generates memcpy() calls */
462 #if (_MSC_VER >= 1400) && defined(_WIN64) && !defined(_DEBUG) && !(_MSC_VER >= 1900 && defined(_MT))
465 #pragma function(memcpy)
466 void * memcpy ( void * destination, const void * source, size_t num )
468 const Uint8 *src = (const Uint8 *)source;
469 Uint8 *dst = (Uint8 *)destination;
472 /* All WIN64 architectures have SSE, right? */
473 if (!((uintptr_t) src & 15) && !((uintptr_t) dst & 15)) {
475 for (i = num / 64; i--;) {
476 _mm_prefetch(src, _MM_HINT_NTA);
477 values[0] = *(__m128 *) (src + 0);
478 values[1] = *(__m128 *) (src + 16);
479 values[2] = *(__m128 *) (src + 32);
480 values[3] = *(__m128 *) (src + 48);
481 _mm_stream_ps((float *) (dst + 0), values[0]);
482 _mm_stream_ps((float *) (dst + 16), values[1]);
483 _mm_stream_ps((float *) (dst + 32), values[2]);
484 _mm_stream_ps((float *) (dst + 48), values[3]);
496 #endif /* _MSC_VER == 1600 && defined(_WIN64) && !defined(_DEBUG) */
512 fst dword ptr [esp+18h]
513 fistp qword ptr [esp+10h]
514 fild qword ptr [esp+10h]
515 mov edx,dword ptr [esp+18h]
516 mov eax,dword ptr [esp+10h]
518 je integer_QnaN_or_zero
519 arg_is_not_integer_QnaN:
524 mov ecx,dword ptr [esp]
528 mov edx,dword ptr [esp+14h]
533 mov ecx,dword ptr [esp]
536 mov edx,dword ptr [esp+14h]
539 integer_QnaN_or_zero:
540 mov edx,dword ptr [esp+14h]
542 jne arg_is_not_integer_QnaN
543 fstp dword ptr [esp+18h]
544 fstp dword ptr [esp+18h]
558 /* 64-bit math operators for 32-bit systems */
565 mov eax, dword ptr[esp+8]
566 mov ecx, dword ptr[esp+10h]
568 mov ecx, dword ptr[esp+0Ch]
570 mov eax, dword ptr[esp+4]
577 mov eax, dword ptr[esp+8]
578 mul dword ptr[esp+14h]
580 mov eax, dword ptr[esp+8]
599 mov eax,dword ptr [esp+14h]
603 mov edx,dword ptr [esp+10h]
607 mov dword ptr [esp+14h],eax
608 mov dword ptr [esp+10h],edx
610 mov eax,dword ptr [esp+1Ch]
614 mov edx,dword ptr [esp+18h]
618 mov dword ptr [esp+1Ch],eax
619 mov dword ptr [esp+18h],edx
623 mov ecx,dword ptr [esp+18h]
624 mov eax,dword ptr [esp+14h]
628 mov eax,dword ptr [esp+10h]
634 mov ecx,dword ptr [esp+18h]
635 mov edx,dword ptr [esp+14h]
636 mov eax,dword ptr [esp+10h]
646 mul dword ptr [esp+1Ch]
648 mov eax,dword ptr [esp+18h]
652 cmp edx,dword ptr [esp+14h]
655 cmp eax,dword ptr [esp+10h]
685 mov eax,dword ptr [esp+18h]
688 mov ecx,dword ptr [esp+14h]
689 mov eax,dword ptr [esp+10h]
693 mov eax,dword ptr [esp+0Ch]
699 mov ebx,dword ptr [esp+14h]
700 mov edx,dword ptr [esp+10h]
701 mov eax,dword ptr [esp+0Ch]
711 mul dword ptr [esp+18h]
713 mov eax,dword ptr [esp+14h]
717 cmp edx,dword ptr [esp+10h]
720 cmp eax,dword ptr [esp+0Ch]
744 mov eax,dword ptr [esp+10h]
748 mov edx,dword ptr [esp+0Ch]
752 mov dword ptr [esp+10h],eax
753 mov dword ptr [esp+0Ch],edx
755 mov eax,dword ptr [esp+18h]
758 mov edx,dword ptr [esp+14h]
762 mov dword ptr [esp+18h],eax
763 mov dword ptr [esp+14h],edx
767 mov ecx,dword ptr [esp+14h]
768 mov eax,dword ptr [esp+10h]
771 mov eax,dword ptr [esp+0Ch]
780 mov ecx,dword ptr [esp+14h]
781 mov edx,dword ptr [esp+10h]
782 mov eax,dword ptr [esp+0Ch]
792 mul dword ptr [esp+18h]
794 mul dword ptr [esp+14h]
797 cmp edx,dword ptr [esp+10h]
800 cmp eax,dword ptr [esp+0Ch]
803 sub eax,dword ptr [esp+14h]
804 sbb edx,dword ptr [esp+18h]
806 sub eax,dword ptr [esp+0Ch]
807 sbb edx,dword ptr [esp+10h]
829 mov eax,dword ptr [esp+14h]
832 mov ecx,dword ptr [esp+10h]
833 mov eax,dword ptr [esp+0Ch]
836 mov eax,dword ptr [esp+8]
843 mov ebx,dword ptr [esp+10h]
844 mov edx,dword ptr [esp+0Ch]
845 mov eax,dword ptr [esp+8]
855 mul dword ptr [esp+14h]
857 mul dword ptr [esp+10h]
860 cmp edx,dword ptr [esp+0Ch]
863 cmp eax,dword ptr [esp+8]
866 sub eax,dword ptr [esp+10h]
867 sbb edx,dword ptr [esp+14h]
869 sub eax,dword ptr [esp+8]
870 sbb edx,dword ptr [esp+0Ch]
892 mov eax,dword ptr [esp+14h]
897 mov edx,dword ptr [esp+10h]
901 mov dword ptr [esp+14h],eax
902 mov dword ptr [esp+10h],edx
904 mov eax,dword ptr [esp+1Ch]
908 mov edx,dword ptr [esp+18h]
912 mov dword ptr [esp+1Ch],eax
913 mov dword ptr [esp+18h],edx
917 mov ecx,dword ptr [esp+18h]
918 mov eax,dword ptr [esp+14h]
922 mov eax,dword ptr [esp+10h]
926 mul dword ptr [esp+18h]
929 mul dword ptr [esp+18h]
934 mov ecx,dword ptr [esp+18h]
935 mov edx,dword ptr [esp+14h]
936 mov eax,dword ptr [esp+10h]
946 mul dword ptr [esp+1Ch]
948 mov eax,dword ptr [esp+18h]
952 cmp edx,dword ptr [esp+14h]
955 cmp eax,dword ptr [esp+10h]
959 sub eax,dword ptr [esp+18h]
960 sbb edx,dword ptr [esp+1Ch]
964 sub eax,dword ptr [esp+10h]
965 sbb edx,dword ptr [esp+14h]
998 mov eax,dword ptr [esp+14h]
1001 mov ecx,dword ptr [esp+10h]
1002 mov eax,dword ptr [esp+0Ch]
1006 mov eax,dword ptr [esp+8]
1010 mul dword ptr [esp+10h]
1013 mul dword ptr [esp+10h]
1018 mov ebx,dword ptr [esp+10h]
1019 mov edx,dword ptr [esp+0Ch]
1020 mov eax,dword ptr [esp+8]
1030 mul dword ptr [esp+14h]
1032 mov eax,dword ptr [esp+10h]
1036 cmp edx,dword ptr [esp+0Ch]
1039 cmp eax,dword ptr [esp+8]
1043 sub eax,dword ptr [esp+10h]
1044 sbb edx,dword ptr [esp+14h]
1048 sub eax,dword ptr [esp+8]
1049 sbb edx,dword ptr [esp+0Ch]
1145 #endif /* _M_IX86 */
1147 #endif /* MSC_VER */
1149 #endif /* !HAVE_LIBC */
1151 /* vi: set ts=4 sw=4 expandtab: */