Path: blob/master/thirdparty/sdl/stdlib/SDL_stdlib.c
10279 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122// This file contains portable stdlib functions for SDL2324#include "../libm/math_libm.h"2526double SDL_atan(double x)27{28#ifdef HAVE_ATAN29return atan(x);30#else31return SDL_uclibc_atan(x);32#endif33}3435float SDL_atanf(float x)36{37#ifdef HAVE_ATANF38return atanf(x);39#else40return (float)SDL_atan((double)x);41#endif42}4344double SDL_atan2(double y, double x)45{46#ifdef HAVE_ATAN247return atan2(y, x);48#else49return SDL_uclibc_atan2(y, x);50#endif51}5253float SDL_atan2f(float y, float x)54{55#ifdef HAVE_ATAN2F56return atan2f(y, x);57#else58return (float)SDL_atan2((double)y, (double)x);59#endif60}6162double SDL_acos(double val)63{64#ifdef HAVE_ACOS65return acos(val);66#else67double result;68if (val == -1.0) {69result = SDL_PI_D;70} else {71result = SDL_atan(SDL_sqrt(1.0 - val * val) / val);72if (result < 0.0) {73result += SDL_PI_D;74}75}76return result;77#endif78}7980float SDL_acosf(float val)81{82#ifdef HAVE_ACOSF83return acosf(val);84#else85return (float)SDL_acos((double)val);86#endif87}8889double SDL_asin(double val)90{91#ifdef HAVE_ASIN92return asin(val);93#else94double result;95if (val == -1.0) {96result = -(SDL_PI_D / 2.0);97} else {98result = (SDL_PI_D / 2.0) - SDL_acos(val);99}100return result;101#endif102}103104float SDL_asinf(float val)105{106#ifdef HAVE_ASINF107return asinf(val);108#else109return (float)SDL_asin((double)val);110#endif111}112113double SDL_ceil(double x)114{115#ifdef HAVE_CEIL116return ceil(x);117#else118double integer = SDL_floor(x);119double fraction = x - integer;120if (fraction > 0.0) {121integer += 1.0;122}123return integer;124#endif // HAVE_CEIL125}126127float SDL_ceilf(float x)128{129#ifdef HAVE_CEILF130return ceilf(x);131#else132return (float)SDL_ceil((double)x);133#endif134}135136double SDL_copysign(double x, double y)137{138#ifdef HAVE_COPYSIGN139return copysign(x, y);140#elif defined(HAVE__COPYSIGN)141return _copysign(x, y);142#elif defined(__WATCOMC__) && defined(__386__)143// this is nasty as hell, but it works..144unsigned int *xi = (unsigned int *)&x,145*yi = (unsigned int *)&y;146xi[1] = (yi[1] & 0x80000000) | (xi[1] & 0x7fffffff);147return x;148#else149return SDL_uclibc_copysign(x, y);150#endif // HAVE_COPYSIGN151}152153float SDL_copysignf(float x, float y)154{155#ifdef HAVE_COPYSIGNF156return copysignf(x, y);157#else158return (float)SDL_copysign((double)x, (double)y);159#endif160}161162double SDL_cos(double x)163{164#ifdef HAVE_COS165return cos(x);166#else167return SDL_uclibc_cos(x);168#endif169}170171float SDL_cosf(float x)172{173#ifdef HAVE_COSF174return cosf(x);175#else176return (float)SDL_cos((double)x);177#endif178}179180double SDL_exp(double x)181{182#ifdef HAVE_EXP183return exp(x);184#else185return SDL_uclibc_exp(x);186#endif187}188189float SDL_expf(float x)190{191#ifdef HAVE_EXPF192return expf(x);193#else194return (float)SDL_exp((double)x);195#endif196}197198double SDL_fabs(double x)199{200#ifdef HAVE_FABS201return fabs(x);202#else203return SDL_uclibc_fabs(x);204#endif205}206207float SDL_fabsf(float x)208{209#ifdef HAVE_FABSF210return fabsf(x);211#else212return (float)SDL_fabs((double)x);213#endif214}215216double SDL_floor(double x)217{218#ifdef HAVE_FLOOR219return floor(x);220#else221return SDL_uclibc_floor(x);222#endif223}224225float SDL_floorf(float x)226{227#ifdef HAVE_FLOORF228return floorf(x);229#else230return (float)SDL_floor((double)x);231#endif232}233234double SDL_trunc(double x)235{236#ifdef HAVE_TRUNC237return trunc(x);238#else239if (x >= 0.0f) {240return SDL_floor(x);241} else {242return SDL_ceil(x);243}244#endif245}246247float SDL_truncf(float x)248{249#ifdef HAVE_TRUNCF250return truncf(x);251#else252return (float)SDL_trunc((double)x);253#endif254}255256double SDL_fmod(double x, double y)257{258#ifdef HAVE_FMOD259return fmod(x, y);260#else261return SDL_uclibc_fmod(x, y);262#endif263}264265float SDL_fmodf(float x, float y)266{267#ifdef HAVE_FMODF268return fmodf(x, y);269#else270return (float)SDL_fmod((double)x, (double)y);271#endif272}273274int SDL_isinf(double x)275{276#ifdef HAVE_ISINF277return isinf(x);278#else279return SDL_uclibc_isinf(x);280#endif281}282283int SDL_isinff(float x)284{285#ifdef HAVE_ISINF_FLOAT_MACRO286return isinf(x);287#elif defined(HAVE_ISINFF)288return isinff(x);289#else290return SDL_uclibc_isinff(x);291#endif292}293294int SDL_isnan(double x)295{296#ifdef HAVE_ISNAN297return isnan(x);298#else299return SDL_uclibc_isnan(x);300#endif301}302303int SDL_isnanf(float x)304{305#ifdef HAVE_ISNAN_FLOAT_MACRO306return isnan(x);307#elif defined(HAVE_ISNANF)308return isnanf(x);309#else310return SDL_uclibc_isnanf(x);311#endif312}313314double SDL_log(double x)315{316#ifdef HAVE_LOG317return log(x);318#else319return SDL_uclibc_log(x);320#endif321}322323float SDL_logf(float x)324{325#ifdef HAVE_LOGF326return logf(x);327#else328return (float)SDL_log((double)x);329#endif330}331332double SDL_log10(double x)333{334#ifdef HAVE_LOG10335return log10(x);336#else337return SDL_uclibc_log10(x);338#endif339}340341float SDL_log10f(float x)342{343#ifdef HAVE_LOG10F344return log10f(x);345#else346return (float)SDL_log10((double)x);347#endif348}349350double SDL_modf(double x, double *y)351{352#ifdef HAVE_MODF353return modf(x, y);354#else355return SDL_uclibc_modf(x, y);356#endif357}358359float SDL_modff(float x, float *y)360{361#ifdef HAVE_MODFF362return modff(x, y);363#else364double double_result, double_y;365double_result = SDL_modf((double)x, &double_y);366*y = (float)double_y;367return (float)double_result;368#endif369}370371double SDL_pow(double x, double y)372{373#ifdef HAVE_POW374return pow(x, y);375#else376return SDL_uclibc_pow(x, y);377#endif378}379380float SDL_powf(float x, float y)381{382#ifdef HAVE_POWF383return powf(x, y);384#else385return (float)SDL_pow((double)x, (double)y);386#endif387}388389double SDL_round(double arg)390{391#if defined HAVE_ROUND392return round(arg);393#else394if (arg >= 0.0) {395return SDL_floor(arg + 0.5);396} else {397return SDL_ceil(arg - 0.5);398}399#endif400}401402float SDL_roundf(float arg)403{404#if defined HAVE_ROUNDF405return roundf(arg);406#else407return (float)SDL_round((double)arg);408#endif409}410411long SDL_lround(double arg)412{413#if defined HAVE_LROUND414return lround(arg);415#else416return (long)SDL_round(arg);417#endif418}419420long SDL_lroundf(float arg)421{422#if defined HAVE_LROUNDF423return lroundf(arg);424#else425return (long)SDL_round((double)arg);426#endif427}428429double SDL_scalbn(double x, int n)430{431#ifdef HAVE_SCALBN432return scalbn(x, n);433#elif defined(HAVE__SCALB)434return _scalb(x, n);435#elif defined(HAVE_LIBC) && defined(HAVE_FLOAT_H) && (FLT_RADIX == 2)436/* from scalbn(3): If FLT_RADIX equals 2 (which is437* usual), then scalbn() is equivalent to ldexp(3). */438return ldexp(x, n);439#else440return SDL_uclibc_scalbn(x, n);441#endif442}443444float SDL_scalbnf(float x, int n)445{446#ifdef HAVE_SCALBNF447return scalbnf(x, n);448#else449return (float)SDL_scalbn((double)x, n);450#endif451}452453double SDL_sin(double x)454{455#ifdef HAVE_SIN456return sin(x);457#else458return SDL_uclibc_sin(x);459#endif460}461462float SDL_sinf(float x)463{464#ifdef HAVE_SINF465return sinf(x);466#else467return (float)SDL_sin((double)x);468#endif469}470471double SDL_sqrt(double x)472{473#ifdef HAVE_SQRT474return sqrt(x);475#else476return SDL_uclibc_sqrt(x);477#endif478}479480float SDL_sqrtf(float x)481{482#ifdef HAVE_SQRTF483return sqrtf(x);484#else485return (float)SDL_sqrt((double)x);486#endif487}488489double SDL_tan(double x)490{491#ifdef HAVE_TAN492return tan(x);493#else494return SDL_uclibc_tan(x);495#endif496}497498float SDL_tanf(float x)499{500#ifdef HAVE_TANF501return tanf(x);502#else503return (float)SDL_tan((double)x);504#endif505}506507int SDL_abs(int x)508{509#ifdef HAVE_ABS510return abs(x);511#else512return (x < 0) ? -x : x;513#endif514}515516int SDL_isalpha(int x) { return (SDL_isupper(x)) || (SDL_islower(x)); }517int SDL_isalnum(int x) { return (SDL_isalpha(x)) || (SDL_isdigit(x)); }518int SDL_isdigit(int x) { return ((x) >= '0') && ((x) <= '9'); }519int SDL_isxdigit(int x) { return (((x) >= 'A') && ((x) <= 'F')) || (((x) >= 'a') && ((x) <= 'f')) || (SDL_isdigit(x)); }520int SDL_ispunct(int x) { return (SDL_isgraph(x)) && (!SDL_isalnum(x)); }521int SDL_isspace(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n') || ((x) == '\f') || ((x) == '\v'); }522int SDL_isupper(int x) { return ((x) >= 'A') && ((x) <= 'Z'); }523int SDL_islower(int x) { return ((x) >= 'a') && ((x) <= 'z'); }524int SDL_isprint(int x) { return ((x) >= ' ') && ((x) < '\x7f'); }525int SDL_isgraph(int x) { return (SDL_isprint(x)) && ((x) != ' '); }526int SDL_iscntrl(int x) { return (((x) >= '\0') && ((x) <= '\x1f')) || ((x) == '\x7f'); }527int SDL_toupper(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A' + ((x) - 'a')) : (x); }528int SDL_tolower(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a' + ((x) - 'A')) : (x); }529int SDL_isblank(int x) { return ((x) == ' ') || ((x) == '\t'); }530531void *SDL_aligned_alloc(size_t alignment, size_t size)532{533size_t padding;534Uint8 *result = NULL;535size_t requested_size = size;536537if (alignment < sizeof(void*)) {538alignment = sizeof(void*);539}540padding = (alignment - (size % alignment));541542if (SDL_size_add_check_overflow(size, alignment, &size) &&543SDL_size_add_check_overflow(size, sizeof(void *), &size) &&544SDL_size_add_check_overflow(size, padding, &size)) {545void *original = SDL_malloc(size);546if (original) {547// Make sure we have enough space to store the original pointer548result = (Uint8 *)original + sizeof(original);549550// Align the pointer we're going to return551result += alignment - (((size_t)result) % alignment);552553// Store the original pointer right before the returned value554SDL_memcpy(result - sizeof(original), &original, sizeof(original));555556// Initialize the padding to zero557if (padding > 0) {558SDL_memset(result + requested_size, 0, padding);559}560}561}562return result;563}564565void SDL_aligned_free(void *mem)566{567if (mem) {568void *original;569SDL_memcpy(&original, ((Uint8 *)mem - sizeof(original)), sizeof(original));570SDL_free(original);571}572}573574575