Atlas - SDL_stdlib.c
Home / ext / SDL / src / stdlib Lines: 2 | Size: 10704 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 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. 8 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: 12 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. 20*/ 21#include "SDL_internal.h" 22 23// This file contains portable stdlib functions for SDL 24 25#include "../libm/math_libm.h" 26 27double SDL_atan(double x) 28{ 29#ifdef HAVE_ATAN 30 return atan(x); 31#else 32 return SDL_uclibc_atan(x); 33#endif 34} 35 36float SDL_atanf(float x) 37{ 38#ifdef HAVE_ATANF 39 return atanf(x); 40#else 41 return (float)SDL_atan((double)x); 42#endif 43} 44 45double SDL_atan2(double y, double x) 46{ 47#ifdef HAVE_ATAN2 48 return atan2(y, x); 49#else 50 return SDL_uclibc_atan2(y, x); 51#endif 52} 53 54float SDL_atan2f(float y, float x) 55{ 56#ifdef HAVE_ATAN2F 57 return atan2f(y, x); 58#else 59 return (float)SDL_atan2((double)y, (double)x); 60#endif 61} 62 63double SDL_acos(double val) 64{ 65#ifdef HAVE_ACOS 66 return acos(val); 67#else 68 double result; 69 if (val == -1.0) { 70 result = SDL_PI_D; 71 } else { 72 result = SDL_atan(SDL_sqrt(1.0 - val * val) / val); 73 if (result < 0.0) { 74 result += SDL_PI_D; 75 } 76 } 77 return result; 78#endif 79} 80 81float SDL_acosf(float val) 82{ 83#ifdef HAVE_ACOSF 84 return acosf(val); 85#else 86 return (float)SDL_acos((double)val); 87#endif 88} 89 90double SDL_asin(double val) 91{ 92#ifdef HAVE_ASIN 93 return asin(val); 94#else 95 double result; 96 if (val == -1.0) { 97 result = -(SDL_PI_D / 2.0); 98 } else { 99 result = (SDL_PI_D / 2.0) - SDL_acos(val); 100 } 101 return result; 102#endif 103} 104 105float SDL_asinf(float val) 106{ 107#ifdef HAVE_ASINF 108 return asinf(val); 109#else 110 return (float)SDL_asin((double)val); 111#endif 112} 113 114double SDL_ceil(double x) 115{ 116#ifdef HAVE_CEIL 117 return ceil(x); 118#else 119 double integer = SDL_floor(x); 120 double fraction = x - integer; 121 if (fraction > 0.0) { 122 integer += 1.0; 123 } 124 return integer; 125#endif // HAVE_CEIL 126} 127 128float SDL_ceilf(float x) 129{ 130#ifdef HAVE_CEILF 131 return ceilf(x); 132#else 133 return (float)SDL_ceil((double)x); 134#endif 135} 136 137double SDL_copysign(double x, double y) 138{ 139#ifdef HAVE_COPYSIGN 140 return copysign(x, y); 141#elif defined(HAVE__COPYSIGN) 142 return _copysign(x, y); 143#else 144 return SDL_uclibc_copysign(x, y); 145#endif // HAVE_COPYSIGN 146} 147 148float SDL_copysignf(float x, float y) 149{ 150#ifdef HAVE_COPYSIGNF 151 return copysignf(x, y); 152#else 153 return (float)SDL_copysign((double)x, (double)y); 154#endif 155} 156 157double SDL_cos(double x) 158{ 159#ifdef HAVE_COS 160 return cos(x); 161#else 162 return SDL_uclibc_cos(x); 163#endif 164} 165 166float SDL_cosf(float x) 167{ 168#ifdef HAVE_COSF 169 return cosf(x); 170#else 171 return (float)SDL_cos((double)x); 172#endif 173} 174 175double SDL_exp(double x) 176{ 177#ifdef HAVE_EXP 178 return exp(x); 179#else 180 return SDL_uclibc_exp(x); 181#endif 182} 183 184float SDL_expf(float x) 185{ 186#ifdef HAVE_EXPF 187 return expf(x); 188#else 189 return (float)SDL_exp((double)x); 190#endif 191} 192 193double SDL_fabs(double x) 194{ 195#ifdef HAVE_FABS 196 return fabs(x); 197#else 198 return SDL_uclibc_fabs(x); 199#endif 200} 201 202float SDL_fabsf(float x) 203{ 204#ifdef HAVE_FABSF 205 return fabsf(x); 206#else 207 return (float)SDL_fabs((double)x); 208#endif 209} 210 211double SDL_floor(double x) 212{ 213#ifdef HAVE_FLOOR 214 return floor(x); 215#else 216 return SDL_uclibc_floor(x); 217#endif 218} 219 220float SDL_floorf(float x) 221{ 222#ifdef HAVE_FLOORF 223 return floorf(x); 224#else 225 return (float)SDL_floor((double)x); 226#endif 227} 228 229double SDL_trunc(double x) 230{ 231#ifdef HAVE_TRUNC 232 return trunc(x); 233#else 234 if (x >= 0.0f) { 235 return SDL_floor(x); 236 } else { 237 return SDL_ceil(x); 238 } 239#endif 240} 241 242float SDL_truncf(float x) 243{ 244#ifdef HAVE_TRUNCF 245 return truncf(x); 246#else 247 return (float)SDL_trunc((double)x); 248#endif 249} 250 251double SDL_fmod(double x, double y) 252{ 253#ifdef HAVE_FMOD 254 return fmod(x, y); 255#else 256 return SDL_uclibc_fmod(x, y); 257#endif 258} 259 260float SDL_fmodf(float x, float y) 261{ 262#ifdef HAVE_FMODF 263 return fmodf(x, y); 264#else 265 return (float)SDL_fmod((double)x, (double)y); 266#endif 267} 268 269int SDL_isinf(double x) 270{ 271#ifdef HAVE_ISINF 272 return isinf(x); 273#else 274 return SDL_uclibc_isinf(x); 275#endif 276} 277 278int SDL_isinff(float x) 279{ 280#ifdef HAVE_ISINF_FLOAT_MACRO 281 return isinf(x); 282#elif defined(HAVE_ISINFF) 283 return isinff(x); 284#else 285 return SDL_uclibc_isinff(x); 286#endif 287} 288 289int SDL_isnan(double x) 290{ 291#ifdef HAVE_ISNAN 292 return isnan(x); 293#else 294 return SDL_uclibc_isnan(x); 295#endif 296} 297 298int SDL_isnanf(float x) 299{ 300#ifdef HAVE_ISNAN_FLOAT_MACRO 301 return isnan(x); 302#elif defined(HAVE_ISNANF) 303 return isnanf(x); 304#else 305 return SDL_uclibc_isnanf(x); 306#endif 307} 308 309double SDL_log(double x) 310{ 311#ifdef HAVE_LOG 312 return log(x); 313#else 314 return SDL_uclibc_log(x); 315#endif 316} 317 318float SDL_logf(float x) 319{ 320#ifdef HAVE_LOGF 321 return logf(x); 322#else 323 return (float)SDL_log((double)x); 324#endif 325} 326 327double SDL_log10(double x) 328{ 329#ifdef HAVE_LOG10 330 return log10(x); 331#else 332 return SDL_uclibc_log10(x); 333#endif 334} 335 336float SDL_log10f(float x) 337{ 338#ifdef HAVE_LOG10F 339 return log10f(x); 340#else 341 return (float)SDL_log10((double)x); 342#endif 343} 344 345double SDL_modf(double x, double *y) 346{ 347#ifdef HAVE_MODF 348 return modf(x, y); 349#else 350 return SDL_uclibc_modf(x, y); 351#endif 352} 353 354float SDL_modff(float x, float *y) 355{ 356#ifdef HAVE_MODFF 357 return modff(x, y); 358#else 359 double double_result, double_y; 360 double_result = SDL_modf((double)x, &double_y); 361 *y = (float)double_y; 362 return (float)double_result; 363#endif 364} 365 366double SDL_pow(double x, double y) 367{ 368#ifdef HAVE_POW 369 return pow(x, y); 370#else 371 return SDL_uclibc_pow(x, y); 372#endif 373} 374 375float SDL_powf(float x, float y) 376{ 377#ifdef HAVE_POWF 378 return powf(x, y); 379#else 380 return (float)SDL_pow((double)x, (double)y); 381#endif 382} 383 384double SDL_round(double arg) 385{ 386#if defined HAVE_ROUND 387 return round(arg); 388#else 389 if (arg >= 0.0) { 390 return SDL_floor(arg + 0.5); 391 } else { 392 return SDL_ceil(arg - 0.5); 393 } 394#endif 395} 396 397float SDL_roundf(float arg) 398{ 399#if defined HAVE_ROUNDF 400 return roundf(arg); 401#else 402 return (float)SDL_round((double)arg); 403#endif 404} 405 406long SDL_lround(double arg) 407{ 408#if defined HAVE_LROUND 409 return lround(arg); 410#else 411 return (long)SDL_round(arg); 412#endif 413} 414 415long SDL_lroundf(float arg) 416{ 417#if defined HAVE_LROUNDF 418 return lroundf(arg); 419#else 420 return (long)SDL_round((double)arg); 421#endif 422} 423 424double SDL_scalbn(double x, int n) 425{ 426#ifdef HAVE_SCALBN 427 return scalbn(x, n); 428#elif defined(HAVE__SCALB) 429 return _scalb(x, n); 430#elif defined(HAVE_LIBC) && defined(HAVE_FLOAT_H) && (FLT_RADIX == 2) 431 /* from scalbn(3): If FLT_RADIX equals 2 (which is 432 * usual), then scalbn() is equivalent to ldexp(3). */ 433 return ldexp(x, n); 434#else 435 return SDL_uclibc_scalbn(x, n); 436#endif 437} 438 439float SDL_scalbnf(float x, int n) 440{ 441#ifdef HAVE_SCALBNF 442 return scalbnf(x, n); 443#else 444 return (float)SDL_scalbn((double)x, n); 445#endif 446} 447 448double SDL_sin(double x) 449{ 450#ifdef HAVE_SIN 451 return sin(x); 452#else 453 return SDL_uclibc_sin(x); 454#endif 455} 456 457float SDL_sinf(float x) 458{ 459#ifdef HAVE_SINF 460 return sinf(x); 461#else 462 return (float)SDL_sin((double)x); 463#endif 464} 465 466double SDL_sqrt(double x) 467{ 468#ifdef HAVE_SQRT 469 return sqrt(x); 470#else 471 return SDL_uclibc_sqrt(x); 472#endif 473} 474 475float SDL_sqrtf(float x) 476{ 477#ifdef HAVE_SQRTF 478 return sqrtf(x); 479#else 480 return (float)SDL_sqrt((double)x); 481#endif 482} 483 484double SDL_tan(double x) 485{ 486#ifdef HAVE_TAN 487 return tan(x); 488#else 489 return SDL_uclibc_tan(x); 490#endif 491} 492 493float SDL_tanf(float x) 494{ 495#ifdef HAVE_TANF 496 return tanf(x); 497#else 498 return (float)SDL_tan((double)x); 499#endif 500} 501 502int SDL_abs(int x) 503{ 504#ifdef HAVE_ABS 505 return abs(x); 506#else 507 return (x < 0) ? -x : x; 508#endif 509} 510 511int SDL_isalpha(int x) { return (SDL_isupper(x)) || (SDL_islower(x)); } 512int SDL_isalnum(int x) { return (SDL_isalpha(x)) || (SDL_isdigit(x)); } 513int SDL_isdigit(int x) { return ((x) >= '0') && ((x) <= '9'); } 514int SDL_isxdigit(int x) { return (((x) >= 'A') && ((x) <= 'F')) || (((x) >= 'a') && ((x) <= 'f')) || (SDL_isdigit(x)); } 515int SDL_ispunct(int x) { return (SDL_isgraph(x)) && (!SDL_isalnum(x)); } 516int SDL_isspace(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n') || ((x) == '\f') || ((x) == '\v'); } 517int SDL_isupper(int x) { return ((x) >= 'A') && ((x) <= 'Z'); } 518int SDL_islower(int x) { return ((x) >= 'a') && ((x) <= 'z'); } 519int SDL_isprint(int x) { return ((x) >= ' ') && ((x) < '\x7f'); } 520int SDL_isgraph(int x) { return (SDL_isprint(x)) && ((x) != ' '); } 521int SDL_iscntrl(int x) { return (((x) >= '\0') && ((x) <= '\x1f')) || ((x) == '\x7f'); } 522int SDL_toupper(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A' + ((x) - 'a')) : (x); } 523int SDL_tolower(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a' + ((x) - 'A')) : (x); } 524int SDL_isblank(int x) { return ((x) == ' ') || ((x) == '\t'); } 525 526void *SDL_aligned_alloc(size_t alignment, size_t size) 527{ 528 size_t padding; 529 Uint8 *result = NULL; 530 size_t requested_size = size; 531 532 if (alignment < sizeof(void *)) { 533 alignment = sizeof(void *); 534 } 535 padding = (alignment - (size % alignment)); 536 537 if (SDL_size_add_check_overflow(size, alignment, &size) && 538 SDL_size_add_check_overflow(size, sizeof(void *), &size) && 539 SDL_size_add_check_overflow(size, padding, &size)) { 540 void *original = SDL_malloc(size); 541 if (original) { 542 // Make sure we have enough space to store the original pointer 543 result = (Uint8 *)original + sizeof(original); 544 545 // Align the pointer we're going to return 546 result += alignment - (((size_t)result) % alignment); 547 548 // Store the original pointer right before the returned value 549 SDL_memcpy(result - sizeof(original), &original, sizeof(original)); 550 551 // Initialize the padding to zero 552 if (padding > 0) { 553 SDL_memset(result + requested_size, 0, padding); 554 } 555 } 556 } 557 return result; 558} 559 560void SDL_aligned_free(void *mem) 561{ 562 if (mem) { 563 void *original; 564 SDL_memcpy(&original, ((Uint8 *)mem - sizeof(original)), sizeof(original)); 565 SDL_free(original); 566 } 567} 568[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.