SDL_stdlib.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. // Modified by Lasse Oorni for Urho3D
  19. #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
  20. #define SDL_DISABLE_ANALYZE_MACROS 1
  21. #endif
  22. #include "../SDL_internal.h"
  23. /* This file contains portable stdlib functions for SDL */
  24. #include "SDL_stdinc.h"
  25. #include "../libm/math_libm.h"
  26. double
  27. SDL_atan(double x)
  28. {
  29. #ifdef HAVE_ATAN
  30. return atan(x);
  31. #else
  32. return SDL_uclibc_atan(x);
  33. #endif /* HAVE_ATAN */
  34. }
  35. double
  36. SDL_atan2(double x, double y)
  37. {
  38. #if defined(HAVE_ATAN2)
  39. return atan2(x, y);
  40. #else
  41. return SDL_uclibc_atan2(x, y);
  42. #endif /* HAVE_ATAN2 */
  43. }
  44. double
  45. SDL_acos(double val)
  46. {
  47. #if defined(HAVE_ACOS)
  48. return acos(val);
  49. #else
  50. double result;
  51. if (val == -1.0) {
  52. result = M_PI;
  53. } else {
  54. result = SDL_atan(SDL_sqrt(1.0 - val * val) / val);
  55. if (result < 0.0)
  56. {
  57. result += M_PI;
  58. }
  59. }
  60. return result;
  61. #endif
  62. }
  63. double
  64. SDL_asin(double val)
  65. {
  66. #if defined(HAVE_ASIN)
  67. return asin(val);
  68. #else
  69. double result;
  70. if (val == -1.0) {
  71. result = -(M_PI / 2.0);
  72. } else {
  73. result = (M_PI / 2.0) - SDL_acos(val);
  74. }
  75. return result;
  76. #endif
  77. }
  78. double
  79. SDL_ceil(double x)
  80. {
  81. #ifdef HAVE_CEIL
  82. return ceil(x);
  83. #else
  84. double integer = SDL_floor(x);
  85. double fraction = x - integer;
  86. if (fraction > 0.0) {
  87. integer += 1.0;
  88. }
  89. return integer;
  90. #endif /* HAVE_CEIL */
  91. }
  92. double
  93. SDL_copysign(double x, double y)
  94. {
  95. #if defined(HAVE_COPYSIGN)
  96. return copysign(x, y);
  97. #elif defined(HAVE__COPYSIGN)
  98. return _copysign(x, y);
  99. #else
  100. return SDL_uclibc_copysign(x, y);
  101. #endif /* HAVE_COPYSIGN */
  102. }
  103. double
  104. SDL_cos(double x)
  105. {
  106. #if defined(HAVE_COS)
  107. return cos(x);
  108. #else
  109. return SDL_uclibc_cos(x);
  110. #endif /* HAVE_COS */
  111. }
  112. float
  113. SDL_cosf(float x)
  114. {
  115. #ifdef HAVE_COSF
  116. return cosf(x);
  117. #else
  118. return (float)SDL_cos((double)x);
  119. #endif
  120. }
  121. double
  122. SDL_fabs(double x)
  123. {
  124. #if defined(HAVE_FABS)
  125. return fabs(x);
  126. #else
  127. return SDL_uclibc_fabs(x);
  128. #endif /* HAVE_FABS */
  129. }
  130. double
  131. SDL_floor(double x)
  132. {
  133. #if defined(HAVE_FLOOR)
  134. return floor(x);
  135. #else
  136. return SDL_uclibc_floor(x);
  137. #endif /* HAVE_FLOOR */
  138. }
  139. double
  140. SDL_log(double x)
  141. {
  142. #if defined(HAVE_LOG)
  143. return log(x);
  144. #else
  145. return SDL_uclibc_log(x);
  146. #endif /* HAVE_LOG */
  147. }
  148. double
  149. SDL_pow(double x, double y)
  150. {
  151. #if defined(HAVE_POW)
  152. return pow(x, y);
  153. #else
  154. return SDL_uclibc_pow(x, y);
  155. #endif /* HAVE_POW */
  156. }
  157. double
  158. SDL_scalbn(double x, int n)
  159. {
  160. #if defined(HAVE_SCALBN)
  161. return scalbn(x, n);
  162. #elif defined(HAVE__SCALB)
  163. return _scalb(x, n);
  164. #else
  165. return SDL_uclibc_scalbn(x, n);
  166. #endif /* HAVE_SCALBN */
  167. }
  168. double
  169. SDL_sin(double x)
  170. {
  171. #if defined(HAVE_SIN)
  172. return sin(x);
  173. #else
  174. return SDL_uclibc_sin(x);
  175. #endif /* HAVE_SIN */
  176. }
  177. float
  178. SDL_sinf(float x)
  179. {
  180. #ifdef HAVE_SINF
  181. return sinf(x);
  182. #else
  183. return (float)SDL_sin((double)x);
  184. #endif /* HAVE_SINF */
  185. }
  186. double
  187. SDL_sqrt(double x)
  188. {
  189. #if defined(HAVE_SQRT)
  190. return sqrt(x);
  191. #else
  192. return SDL_uclibc_sqrt(x);
  193. #endif
  194. }
  195. float
  196. SDL_sqrtf(float x)
  197. {
  198. #if defined(HAVE_SQRTF)
  199. return sqrtf(x);
  200. #else
  201. return (float)SDL_sqrt((double)x);
  202. #endif
  203. }
  204. double
  205. SDL_tan(double x)
  206. {
  207. #if defined(HAVE_TAN)
  208. return tan(x);
  209. #else
  210. return SDL_uclibc_tan(x);
  211. #endif
  212. }
  213. float
  214. SDL_tanf(float x)
  215. {
  216. #if defined(HAVE_TANF)
  217. return tanf(x);
  218. #else
  219. return (float)SDL_tan((double)x);
  220. #endif
  221. }
  222. int SDL_abs(int x)
  223. {
  224. #ifdef HAVE_ABS
  225. return abs(x);
  226. #else
  227. return ((x) < 0 ? -(x) : (x));
  228. #endif
  229. }
  230. #ifdef HAVE_CTYPE_H
  231. int SDL_isdigit(int x) { return isdigit(x); }
  232. int SDL_isspace(int x) { return isspace(x); }
  233. int SDL_toupper(int x) { return toupper(x); }
  234. int SDL_tolower(int x) { return tolower(x); }
  235. #else
  236. int SDL_isdigit(int x) { return ((x) >= '0') && ((x) <= '9'); }
  237. int SDL_isspace(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n') || ((x) == '\f') || ((x) == '\v'); }
  238. int SDL_toupper(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A'+((x)-'a')) : (x); }
  239. int SDL_tolower(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a'+((x)-'A')) : (x); }
  240. #endif
  241. #ifndef HAVE_LIBC
  242. // Urho3D: disable MSVC runtime intrinsic replacements
  243. #endif /* !HAVE_LIBC */
  244. /* vi: set ts=4 sw=4 expandtab: */