SDL_stdlib.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  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. #include "SDL_internal.h"
  19. /* This file contains portable stdlib functions for SDL */
  20. #include "../libm/math_libm.h"
  21. double SDL_atan(double x)
  22. {
  23. #ifdef HAVE_ATAN
  24. return atan(x);
  25. #else
  26. return SDL_uclibc_atan(x);
  27. #endif
  28. }
  29. float SDL_atanf(float x)
  30. {
  31. #ifdef HAVE_ATANF
  32. return atanf(x);
  33. #else
  34. return (float)SDL_atan((double)x);
  35. #endif
  36. }
  37. double SDL_atan2(double y, double x)
  38. {
  39. #ifdef HAVE_ATAN2
  40. return atan2(y, x);
  41. #else
  42. return SDL_uclibc_atan2(y, x);
  43. #endif
  44. }
  45. float SDL_atan2f(float y, float x)
  46. {
  47. #ifdef HAVE_ATAN2F
  48. return atan2f(y, x);
  49. #else
  50. return (float)SDL_atan2((double)y, (double)x);
  51. #endif
  52. }
  53. double SDL_acos(double val)
  54. {
  55. #ifdef HAVE_ACOS
  56. return acos(val);
  57. #else
  58. double result;
  59. if (val == -1.0) {
  60. result = SDL_PI_D;
  61. } else {
  62. result = SDL_atan(SDL_sqrt(1.0 - val * val) / val);
  63. if (result < 0.0) {
  64. result += SDL_PI_D;
  65. }
  66. }
  67. return result;
  68. #endif
  69. }
  70. float SDL_acosf(float val)
  71. {
  72. #ifdef HAVE_ACOSF
  73. return acosf(val);
  74. #else
  75. return (float)SDL_acos((double)val);
  76. #endif
  77. }
  78. double SDL_asin(double val)
  79. {
  80. #ifdef HAVE_ASIN
  81. return asin(val);
  82. #else
  83. double result;
  84. if (val == -1.0) {
  85. result = -(SDL_PI_D / 2.0);
  86. } else {
  87. result = (SDL_PI_D / 2.0) - SDL_acos(val);
  88. }
  89. return result;
  90. #endif
  91. }
  92. float SDL_asinf(float val)
  93. {
  94. #ifdef HAVE_ASINF
  95. return asinf(val);
  96. #else
  97. return (float)SDL_asin((double)val);
  98. #endif
  99. }
  100. double SDL_ceil(double x)
  101. {
  102. #ifdef HAVE_CEIL
  103. return ceil(x);
  104. #else
  105. double integer = SDL_floor(x);
  106. double fraction = x - integer;
  107. if (fraction > 0.0) {
  108. integer += 1.0;
  109. }
  110. return integer;
  111. #endif /* HAVE_CEIL */
  112. }
  113. float SDL_ceilf(float x)
  114. {
  115. #ifdef HAVE_CEILF
  116. return ceilf(x);
  117. #else
  118. return (float)SDL_ceil((double)x);
  119. #endif
  120. }
  121. double SDL_copysign(double x, double y)
  122. {
  123. #ifdef HAVE_COPYSIGN
  124. return copysign(x, y);
  125. #elif defined(HAVE__COPYSIGN)
  126. return _copysign(x, y);
  127. #elif defined(__WATCOMC__) && defined(__386__)
  128. /* this is nasty as hell, but it works.. */
  129. unsigned int *xi = (unsigned int *)&x,
  130. *yi = (unsigned int *)&y;
  131. xi[1] = (yi[1] & 0x80000000) | (xi[1] & 0x7fffffff);
  132. return x;
  133. #else
  134. return SDL_uclibc_copysign(x, y);
  135. #endif /* HAVE_COPYSIGN */
  136. }
  137. float SDL_copysignf(float x, float y)
  138. {
  139. #ifdef HAVE_COPYSIGNF
  140. return copysignf(x, y);
  141. #else
  142. return (float)SDL_copysign((double)x, (double)y);
  143. #endif
  144. }
  145. double SDL_cos(double x)
  146. {
  147. #ifdef HAVE_COS
  148. return cos(x);
  149. #else
  150. return SDL_uclibc_cos(x);
  151. #endif
  152. }
  153. float SDL_cosf(float x)
  154. {
  155. #ifdef HAVE_COSF
  156. return cosf(x);
  157. #else
  158. return (float)SDL_cos((double)x);
  159. #endif
  160. }
  161. double SDL_exp(double x)
  162. {
  163. #ifdef HAVE_EXP
  164. return exp(x);
  165. #else
  166. return SDL_uclibc_exp(x);
  167. #endif
  168. }
  169. float SDL_expf(float x)
  170. {
  171. #ifdef HAVE_EXPF
  172. return expf(x);
  173. #else
  174. return (float)SDL_exp((double)x);
  175. #endif
  176. }
  177. double SDL_fabs(double x)
  178. {
  179. #ifdef HAVE_FABS
  180. return fabs(x);
  181. #else
  182. return SDL_uclibc_fabs(x);
  183. #endif
  184. }
  185. float SDL_fabsf(float x)
  186. {
  187. #ifdef HAVE_FABSF
  188. return fabsf(x);
  189. #else
  190. return (float)SDL_fabs((double)x);
  191. #endif
  192. }
  193. double SDL_floor(double x)
  194. {
  195. #ifdef HAVE_FLOOR
  196. return floor(x);
  197. #else
  198. return SDL_uclibc_floor(x);
  199. #endif
  200. }
  201. float SDL_floorf(float x)
  202. {
  203. #ifdef HAVE_FLOORF
  204. return floorf(x);
  205. #else
  206. return (float)SDL_floor((double)x);
  207. #endif
  208. }
  209. double SDL_trunc(double x)
  210. {
  211. #ifdef HAVE_TRUNC
  212. return trunc(x);
  213. #else
  214. if (x >= 0.0f) {
  215. return SDL_floor(x);
  216. } else {
  217. return SDL_ceil(x);
  218. }
  219. #endif
  220. }
  221. float SDL_truncf(float x)
  222. {
  223. #ifdef HAVE_TRUNCF
  224. return truncf(x);
  225. #else
  226. return (float)SDL_trunc((double)x);
  227. #endif
  228. }
  229. double SDL_fmod(double x, double y)
  230. {
  231. #ifdef HAVE_FMOD
  232. return fmod(x, y);
  233. #else
  234. return SDL_uclibc_fmod(x, y);
  235. #endif
  236. }
  237. float SDL_fmodf(float x, float y)
  238. {
  239. #ifdef HAVE_FMODF
  240. return fmodf(x, y);
  241. #else
  242. return (float)SDL_fmod((double)x, (double)y);
  243. #endif
  244. }
  245. double SDL_log(double x)
  246. {
  247. #ifdef HAVE_LOG
  248. return log(x);
  249. #else
  250. return SDL_uclibc_log(x);
  251. #endif
  252. }
  253. float SDL_logf(float x)
  254. {
  255. #ifdef HAVE_LOGF
  256. return logf(x);
  257. #else
  258. return (float)SDL_log((double)x);
  259. #endif
  260. }
  261. double SDL_log10(double x)
  262. {
  263. #ifdef HAVE_LOG10
  264. return log10(x);
  265. #else
  266. return SDL_uclibc_log10(x);
  267. #endif
  268. }
  269. float SDL_log10f(float x)
  270. {
  271. #ifdef HAVE_LOG10F
  272. return log10f(x);
  273. #else
  274. return (float)SDL_log10((double)x);
  275. #endif
  276. }
  277. double SDL_modf(double x, double *y)
  278. {
  279. #ifdef HAVE_MODF
  280. return modf(x, y);
  281. #else
  282. return SDL_uclibc_modf(x, y);
  283. #endif
  284. }
  285. float SDL_modff(float x, float *y)
  286. {
  287. #ifdef HAVE_MODFF
  288. return modff(x, y);
  289. #else
  290. double double_result, double_y;
  291. double_result = SDL_modf((double)x, &double_y);
  292. *y = (float)double_y;
  293. return (float)double_result;
  294. #endif
  295. }
  296. double SDL_pow(double x, double y)
  297. {
  298. #ifdef HAVE_POW
  299. return pow(x, y);
  300. #else
  301. return SDL_uclibc_pow(x, y);
  302. #endif
  303. }
  304. float SDL_powf(float x, float y)
  305. {
  306. #ifdef HAVE_POWF
  307. return powf(x, y);
  308. #else
  309. return (float)SDL_pow((double)x, (double)y);
  310. #endif
  311. }
  312. double SDL_round(double arg)
  313. {
  314. #if defined HAVE_ROUND
  315. return round(arg);
  316. #else
  317. if (arg >= 0.0) {
  318. return SDL_floor(arg + 0.5);
  319. } else {
  320. return SDL_ceil(arg - 0.5);
  321. }
  322. #endif
  323. }
  324. float SDL_roundf(float arg)
  325. {
  326. #if defined HAVE_ROUNDF
  327. return roundf(arg);
  328. #else
  329. return (float)SDL_round((double)arg);
  330. #endif
  331. }
  332. long SDL_lround(double arg)
  333. {
  334. #if defined HAVE_LROUND
  335. return lround(arg);
  336. #else
  337. return (long)SDL_round(arg);
  338. #endif
  339. }
  340. long SDL_lroundf(float arg)
  341. {
  342. #if defined HAVE_LROUNDF
  343. return lroundf(arg);
  344. #else
  345. return (long)SDL_round((double)arg);
  346. #endif
  347. }
  348. double SDL_scalbn(double x, int n)
  349. {
  350. #ifdef HAVE_SCALBN
  351. return scalbn(x, n);
  352. #elif defined(HAVE__SCALB)
  353. return _scalb(x, n);
  354. #elif defined(HAVE_LIBC) && defined(HAVE_FLOAT_H) && (FLT_RADIX == 2)
  355. /* from scalbn(3): If FLT_RADIX equals 2 (which is
  356. * usual), then scalbn() is equivalent to ldexp(3). */
  357. return ldexp(x, n);
  358. #else
  359. return SDL_uclibc_scalbn(x, n);
  360. #endif
  361. }
  362. float SDL_scalbnf(float x, int n)
  363. {
  364. #ifdef HAVE_SCALBNF
  365. return scalbnf(x, n);
  366. #else
  367. return (float)SDL_scalbn((double)x, n);
  368. #endif
  369. }
  370. double SDL_sin(double x)
  371. {
  372. #ifdef HAVE_SIN
  373. return sin(x);
  374. #else
  375. return SDL_uclibc_sin(x);
  376. #endif
  377. }
  378. float SDL_sinf(float x)
  379. {
  380. #ifdef HAVE_SINF
  381. return sinf(x);
  382. #else
  383. return (float)SDL_sin((double)x);
  384. #endif
  385. }
  386. double SDL_sqrt(double x)
  387. {
  388. #ifdef HAVE_SQRT
  389. return sqrt(x);
  390. #else
  391. return SDL_uclibc_sqrt(x);
  392. #endif
  393. }
  394. float SDL_sqrtf(float x)
  395. {
  396. #ifdef HAVE_SQRTF
  397. return sqrtf(x);
  398. #else
  399. return (float)SDL_sqrt((double)x);
  400. #endif
  401. }
  402. double SDL_tan(double x)
  403. {
  404. #ifdef HAVE_TAN
  405. return tan(x);
  406. #else
  407. return SDL_uclibc_tan(x);
  408. #endif
  409. }
  410. float SDL_tanf(float x)
  411. {
  412. #ifdef HAVE_TANF
  413. return tanf(x);
  414. #else
  415. return (float)SDL_tan((double)x);
  416. #endif
  417. }
  418. int SDL_abs(int x)
  419. {
  420. #ifdef HAVE_ABS
  421. return abs(x);
  422. #else
  423. return (x < 0) ? -x : x;
  424. #endif
  425. }
  426. int SDL_isalpha(int x) { return (SDL_isupper(x)) || (SDL_islower(x)); }
  427. int SDL_isalnum(int x) { return (SDL_isalpha(x)) || (SDL_isdigit(x)); }
  428. int SDL_isdigit(int x) { return ((x) >= '0') && ((x) <= '9'); }
  429. int SDL_isxdigit(int x) { return (((x) >= 'A') && ((x) <= 'F')) || (((x) >= 'a') && ((x) <= 'f')) || (SDL_isdigit(x)); }
  430. int SDL_ispunct(int x) { return (SDL_isgraph(x)) && (!SDL_isalnum(x)); }
  431. int SDL_isspace(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n') || ((x) == '\f') || ((x) == '\v'); }
  432. int SDL_isupper(int x) { return ((x) >= 'A') && ((x) <= 'Z'); }
  433. int SDL_islower(int x) { return ((x) >= 'a') && ((x) <= 'z'); }
  434. int SDL_isprint(int x) { return ((x) >= ' ') && ((x) < '\x7f'); }
  435. int SDL_isgraph(int x) { return (SDL_isprint(x)) && ((x) != ' '); }
  436. int SDL_iscntrl(int x) { return (((x) >= '\0') && ((x) <= '\x1f')) || ((x) == '\x7f'); }
  437. int SDL_toupper(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A' + ((x) - 'a')) : (x); }
  438. int SDL_tolower(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a' + ((x) - 'A')) : (x); }
  439. int SDL_isblank(int x) { return ((x) == ' ') || ((x) == '\t'); }
  440. void *SDL_aligned_alloc(size_t alignment, size_t size)
  441. {
  442. size_t padding;
  443. Uint8 *retval = NULL;
  444. if (alignment < sizeof(void*)) {
  445. alignment = sizeof(void*);
  446. }
  447. padding = (alignment - (size % alignment));
  448. if (SDL_size_add_overflow(size, alignment, &size) == 0 &&
  449. SDL_size_add_overflow(size, sizeof(void *), &size) == 0 &&
  450. SDL_size_add_overflow(size, padding, &size) == 0) {
  451. void *original = SDL_malloc(size);
  452. if (original) {
  453. /* Make sure we have enough space to store the original pointer */
  454. retval = (Uint8 *)original + sizeof(original);
  455. /* Align the pointer we're going to return */
  456. retval += alignment - (((size_t)retval) % alignment);
  457. /* Store the original pointer right before the returned value */
  458. SDL_memcpy(retval - sizeof(original), &original, sizeof(original));
  459. }
  460. }
  461. return retval;
  462. }
  463. void SDL_aligned_free(void *mem)
  464. {
  465. if (mem) {
  466. void *original;
  467. SDL_memcpy(&original, ((Uint8 *)mem - sizeof(original)), sizeof(original));
  468. SDL_free(original);
  469. }
  470. }