1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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
22 // Modified by Lasse Oorni for Urho3D
23
24 #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
25 #define SDL_DISABLE_ANALYZE_MACROS 1
26 #endif
27
28 #include "../SDL_internal.h"
29
30 /* This file contains portable stdlib functions for SDL */
31
32 #include "SDL_stdinc.h"
33 #include "../libm/math_libm.h"
34
35
36 double
SDL_atan(double x)37 SDL_atan(double x)
38 {
39 #if defined(HAVE_ATAN)
40 return atan(x);
41 #else
42 return SDL_uclibc_atan(x);
43 #endif /* HAVE_ATAN */
44 }
45
46 double
SDL_atan2(double x,double y)47 SDL_atan2(double x, double y)
48 {
49 #if defined(HAVE_ATAN2)
50 return atan2(x, y);
51 #else
52 return SDL_uclibc_atan2(x, y);
53 #endif /* HAVE_ATAN2 */
54 }
55
56 double
SDL_acos(double val)57 SDL_acos(double val)
58 {
59 #if defined(HAVE_ACOS)
60 return acos(val);
61 #else
62 double result;
63 if (val == -1.0) {
64 result = M_PI;
65 } else {
66 result = SDL_atan(SDL_sqrt(1.0 - val * val) / val);
67 if (result < 0.0)
68 {
69 result += M_PI;
70 }
71 }
72 return result;
73 #endif
74 }
75
76 double
SDL_asin(double val)77 SDL_asin(double val)
78 {
79 #if defined(HAVE_ASIN)
80 return asin(val);
81 #else
82 double result;
83 if (val == -1.0) {
84 result = -(M_PI / 2.0);
85 } else {
86 result = (M_PI / 2.0) - SDL_acos(val);
87 }
88 return result;
89 #endif
90 }
91
92 double
SDL_ceil(double x)93 SDL_ceil(double x)
94 {
95 #if defined(HAVE_CEIL)
96 return ceil(x);
97 #else
98 double integer = SDL_floor(x);
99 double fraction = x - integer;
100 if (fraction > 0.0) {
101 integer += 1.0;
102 }
103 return integer;
104 #endif /* HAVE_CEIL */
105 }
106
107 double
SDL_copysign(double x,double y)108 SDL_copysign(double x, double y)
109 {
110 #if defined(HAVE_COPYSIGN)
111 return copysign(x, y);
112 #elif defined(HAVE__COPYSIGN)
113 return _copysign(x, y);
114 #else
115 return SDL_uclibc_copysign(x, y);
116 #endif /* HAVE_COPYSIGN */
117 }
118
119 double
SDL_cos(double x)120 SDL_cos(double x)
121 {
122 #if defined(HAVE_COS)
123 return cos(x);
124 #else
125 return SDL_uclibc_cos(x);
126 #endif /* HAVE_COS */
127 }
128
129 float
SDL_cosf(float x)130 SDL_cosf(float x)
131 {
132 #if defined(HAVE_COSF)
133 return cosf(x);
134 #else
135 return (float)SDL_cos((double)x);
136 #endif
137 }
138
139 double
SDL_fabs(double x)140 SDL_fabs(double x)
141 {
142 #if defined(HAVE_FABS)
143 return fabs(x);
144 #else
145 return SDL_uclibc_fabs(x);
146 #endif /* HAVE_FABS */
147 }
148
149 double
SDL_floor(double x)150 SDL_floor(double x)
151 {
152 #if defined(HAVE_FLOOR)
153 return floor(x);
154 #else
155 return SDL_uclibc_floor(x);
156 #endif /* HAVE_FLOOR */
157 }
158
159 double
SDL_log(double x)160 SDL_log(double x)
161 {
162 #if defined(HAVE_LOG)
163 return log(x);
164 #else
165 return SDL_uclibc_log(x);
166 #endif /* HAVE_LOG */
167 }
168
169 double
SDL_pow(double x,double y)170 SDL_pow(double x, double y)
171 {
172 #if defined(HAVE_POW)
173 return pow(x, y);
174 #else
175 return SDL_uclibc_pow(x, y);
176 #endif /* HAVE_POW */
177 }
178
179 double
SDL_scalbn(double x,int n)180 SDL_scalbn(double x, int n)
181 {
182 #if defined(HAVE_SCALBN)
183 return scalbn(x, n);
184 #elif defined(HAVE__SCALB)
185 return _scalb(x, n);
186 #else
187 return SDL_uclibc_scalbn(x, n);
188 #endif /* HAVE_SCALBN */
189 }
190
191 double
SDL_sin(double x)192 SDL_sin(double x)
193 {
194 #if defined(HAVE_SIN)
195 return sin(x);
196 #else
197 return SDL_uclibc_sin(x);
198 #endif /* HAVE_SIN */
199 }
200
201 float
SDL_sinf(float x)202 SDL_sinf(float x)
203 {
204 #if defined(HAVE_SINF)
205 return sinf(x);
206 #else
207 return (float)SDL_sin((double)x);
208 #endif /* HAVE_SINF */
209 }
210
211 double
SDL_sqrt(double x)212 SDL_sqrt(double x)
213 {
214 #if defined(HAVE_SQRT)
215 return sqrt(x);
216 #else
217 return SDL_uclibc_sqrt(x);
218 #endif
219 }
220
221 float
SDL_sqrtf(float x)222 SDL_sqrtf(float x)
223 {
224 #if defined(HAVE_SQRTF)
225 return sqrtf(x);
226 #else
227 return (float)SDL_sqrt((double)x);
228 #endif
229 }
230
231 double
SDL_tan(double x)232 SDL_tan(double x)
233 {
234 #if defined(HAVE_TAN)
235 return tan(x);
236 #else
237 return SDL_uclibc_tan(x);
238 #endif
239 }
240
241 float
SDL_tanf(float x)242 SDL_tanf(float x)
243 {
244 #if defined(HAVE_TANF)
245 return tanf(x);
246 #else
247 return (float)SDL_tan((double)x);
248 #endif
249 }
250
SDL_abs(int x)251 int SDL_abs(int x)
252 {
253 #if defined(HAVE_ABS)
254 return abs(x);
255 #else
256 return ((x) < 0 ? -(x) : (x));
257 #endif
258 }
259
260 #if defined(HAVE_CTYPE_H)
SDL_isdigit(int x)261 int SDL_isdigit(int x) { return isdigit(x); }
SDL_isspace(int x)262 int SDL_isspace(int x) { return isspace(x); }
SDL_toupper(int x)263 int SDL_toupper(int x) { return toupper(x); }
SDL_tolower(int x)264 int SDL_tolower(int x) { return tolower(x); }
265 #else
SDL_isdigit(int x)266 int SDL_isdigit(int x) { return ((x) >= '0') && ((x) <= '9'); }
SDL_isspace(int x)267 int SDL_isspace(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n') || ((x) == '\f') || ((x) == '\v'); }
SDL_toupper(int x)268 int SDL_toupper(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A'+((x)-'a')) : (x); }
SDL_tolower(int x)269 int SDL_tolower(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a'+((x)-'A')) : (x); }
270 #endif
271
272
273 #ifndef HAVE_LIBC
274 // Urho3D: disable MSVC runtime intrinsic replacements
275 #endif /* !HAVE_LIBC */
276
277 /* vi: set ts=4 sw=4 expandtab: */
278