1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #if defined HAVE_FASTMATH_H |
---|
16 | # include <fastmath.h> |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <cmath> |
---|
20 | |
---|
21 | #include "core.h" |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | namespace lol |
---|
26 | { |
---|
27 | |
---|
28 | static const double PI = 3.14159265358979323846264; |
---|
29 | static const double NEG_PI = -3.14159265358979323846264; |
---|
30 | static const double PI_2 = 1.57079632679489661923132; |
---|
31 | static const double PI_4 = 0.785398163397448309615661; |
---|
32 | static const double INV_PI = 0.318309886183790671537768; |
---|
33 | static const double ROOT3 = 1.73205080756887729352745; |
---|
34 | |
---|
35 | static const double ZERO = 0.0; |
---|
36 | static const double ONE = 1.0; |
---|
37 | static const double NEG_ONE = -1.0; |
---|
38 | static const double HALF = 0.5; |
---|
39 | static const double QUARTER = 0.25; |
---|
40 | static const double TWO = 2.0; |
---|
41 | #if defined __GNUC__ |
---|
42 | static const double VERY_SMALL_NUMBER = 0x1.0p-128; |
---|
43 | #else |
---|
44 | static const double VERY_SMALL_NUMBER = 3e-39; |
---|
45 | #endif |
---|
46 | static const double TWO_EXP_52 = 4503599627370496.0; |
---|
47 | static const double TWO_EXP_54 = 18014398509481984.0; |
---|
48 | |
---|
49 | /** sin Taylor series coefficients. */ |
---|
50 | static const double SC[] = |
---|
51 | { |
---|
52 | -1.6449340668482264364724e-0, // π^2/3! |
---|
53 | +8.1174242528335364363700e-1, // π^4/5! |
---|
54 | -1.9075182412208421369647e-1, // π^6/7! |
---|
55 | +2.6147847817654800504653e-2, // π^8/9! |
---|
56 | -2.3460810354558236375089e-3, // π^10/11! |
---|
57 | +1.4842879303107100368487e-4, // π^12/13! |
---|
58 | -6.9758736616563804745344e-6, // π^14/15! |
---|
59 | +2.5312174041370276513517e-7, // π^16/17! |
---|
60 | }; |
---|
61 | |
---|
62 | /* Note: the last value should be -1.3878952462213772114468e-7 (ie. |
---|
63 | * π^18/18!) but we tweak it in order to get the better average precision |
---|
64 | * required for tan() computations when close to π/2+kπ values. */ |
---|
65 | static const double CC[] = |
---|
66 | { |
---|
67 | -4.9348022005446793094172e-0, // π^2/2! |
---|
68 | +4.0587121264167682181850e-0, // π^4/4! |
---|
69 | -1.3352627688545894958753e-0, // π^6/6! |
---|
70 | +2.3533063035889320454188e-1, // π^8/8! |
---|
71 | -2.5806891390014060012598e-2, // π^10/10! |
---|
72 | +1.9295743094039230479033e-3, // π^12/12! |
---|
73 | -1.0463810492484570711802e-4, // π^14/14! |
---|
74 | +4.3030695870329470072978e-6, // π^16/16! |
---|
75 | -1.3777e-7, |
---|
76 | }; |
---|
77 | |
---|
78 | /* These coefficients use Sloane’s http://oeis.org/A002430 and |
---|
79 | * http://oeis.org/A036279 sequences for the Taylor series of tan(). |
---|
80 | * Note: the last value should be 2.12485922978838540352881e5 (ie. |
---|
81 | * 443861162*π^18/1856156927625), but we tweak it in order to get |
---|
82 | * sub 1e-11 average precision in a larger range. */ |
---|
83 | static const double TC[] = |
---|
84 | { |
---|
85 | 3.28986813369645287294483e0, // π^2/3 |
---|
86 | 1.29878788045336582981920e1, // 2*π^4/15 |
---|
87 | 5.18844961612069061254404e1, // 17*π^6/315 |
---|
88 | 2.07509320280908496804928e2, // 62*π^8/2835 |
---|
89 | 8.30024701695986756361561e2, // 1382*π^10/155925 |
---|
90 | 3.32009324029001216460018e3, // 21844*π^12/6081075 |
---|
91 | 1.32803704909665483598490e4, // 929569*π^14/638512875 |
---|
92 | 5.31214808666037709352112e4, // 6404582*π^16/10854718875 |
---|
93 | 2.373e5, |
---|
94 | }; |
---|
95 | |
---|
96 | #if defined __CELLOS_LV2__ |
---|
97 | static inline double lol_fctid(double x) INLINEATTR; |
---|
98 | static inline double lol_fctidz(double x) INLINEATTR; |
---|
99 | static inline double lol_fcfid(double x) INLINEATTR; |
---|
100 | static inline double lol_frsqrte(double x) INLINEATTR; |
---|
101 | static inline double lol_fsel(double c, double gte, double lt) INLINEATTR; |
---|
102 | static inline double lol_fres(double x) INLINEATTR; |
---|
103 | static inline double lol_fdiv(double a, double b) INLINEATTR; |
---|
104 | #endif |
---|
105 | static inline double lol_fabs(double x) INLINEATTR; |
---|
106 | #if defined __GNUC__ |
---|
107 | static inline double lol_round(double x) INLINEATTR; |
---|
108 | static inline double lol_trunc(double x) INLINEATTR; |
---|
109 | #endif |
---|
110 | |
---|
111 | #if defined __CELLOS_LV2__ |
---|
112 | static inline double lol_fctid(double x) |
---|
113 | { |
---|
114 | double r; |
---|
115 | #if defined __SNC__ |
---|
116 | r = __builtin_fctid(x); |
---|
117 | #else |
---|
118 | __asm__ ("fctid %0, %1" |
---|
119 | : "=f" (r) : "f" (x)); |
---|
120 | #endif |
---|
121 | return r; |
---|
122 | } |
---|
123 | |
---|
124 | static double lol_fctidz(double x) |
---|
125 | { |
---|
126 | double r; |
---|
127 | #if defined __SNC__ |
---|
128 | r = __builtin_fctidz(x); |
---|
129 | #else |
---|
130 | __asm__ ("fctidz %0, %1" |
---|
131 | : "=f" (r) : "f" (x)); |
---|
132 | #endif |
---|
133 | return r; |
---|
134 | } |
---|
135 | |
---|
136 | static double lol_fcfid(double x) |
---|
137 | { |
---|
138 | double r; |
---|
139 | #if defined __SNC__ |
---|
140 | r = __builtin_fcfid(x); |
---|
141 | #else |
---|
142 | __asm__ ("fcfid %0, %1" |
---|
143 | : "=f" (r) : "f" (x)); |
---|
144 | #endif |
---|
145 | return r; |
---|
146 | } |
---|
147 | |
---|
148 | static double lol_frsqrte(double x) |
---|
149 | { |
---|
150 | #if defined __SNC__ |
---|
151 | return __builtin_frsqrte(x); |
---|
152 | #else |
---|
153 | double r; |
---|
154 | __asm__ ("frsqrte %0, %1" |
---|
155 | : "=f" (r) : "f" (x)); |
---|
156 | return r; |
---|
157 | #endif |
---|
158 | } |
---|
159 | |
---|
160 | static inline double lol_fsel(double c, double gte, double lt) |
---|
161 | { |
---|
162 | #if defined __CELLOS_LV2__ && defined __SNC__ |
---|
163 | return __fsel(c, gte, lt); |
---|
164 | #elif defined __CELLOS_LV2__ |
---|
165 | double r; |
---|
166 | __asm__ ("fsel %0, %1, %2, %3" |
---|
167 | : "=f" (r) : "f" (c), "f" (gte), "f" (lt)); |
---|
168 | return r; |
---|
169 | #else |
---|
170 | return (c >= 0) ? gte : lt; |
---|
171 | #endif |
---|
172 | } |
---|
173 | |
---|
174 | static inline double lol_fres(double x) |
---|
175 | { |
---|
176 | double ret; |
---|
177 | #if defined __SNC__ |
---|
178 | ret = __builtin_fre(x); |
---|
179 | #else |
---|
180 | __asm__ ("fres %0, %1" |
---|
181 | : "=f" (ret) : "f" (x)); |
---|
182 | #endif |
---|
183 | return ret; |
---|
184 | } |
---|
185 | |
---|
186 | static inline double lol_fdiv(double a, double b) |
---|
187 | { |
---|
188 | /* Estimate */ |
---|
189 | double x0 = lol_fres(b); |
---|
190 | |
---|
191 | /* Two steps of Newton-Raphson */ |
---|
192 | x0 = (b * x0 - ONE) * -x0 + x0; |
---|
193 | x0 = (b * x0 - ONE) * -x0 + x0; |
---|
194 | |
---|
195 | return a * x0; |
---|
196 | } |
---|
197 | #endif /* __CELLOS_LV2__ */ |
---|
198 | |
---|
199 | static inline double lol_fabs(double x) |
---|
200 | { |
---|
201 | #if defined __CELLOS_LV2__ && defined __SNC__ |
---|
202 | return __fabs(x); |
---|
203 | #elif defined __CELLOS_LV2__ |
---|
204 | double r; |
---|
205 | __asm__ ("fabs %0, %1" |
---|
206 | : "=f" (r) : "f" (x)); |
---|
207 | return r; |
---|
208 | #elif defined __GNUC__ |
---|
209 | return __builtin_fabs(x); |
---|
210 | #else |
---|
211 | return fabs(x); |
---|
212 | #endif |
---|
213 | } |
---|
214 | |
---|
215 | #if defined __GNUC__ |
---|
216 | static inline double lol_round(double x) |
---|
217 | { |
---|
218 | #if defined __CELLOS_LV2__ |
---|
219 | return lol_fcfid(lol_fctid(x)); |
---|
220 | #else |
---|
221 | return __builtin_round(x); |
---|
222 | #endif |
---|
223 | } |
---|
224 | |
---|
225 | static inline double lol_trunc(double x) |
---|
226 | { |
---|
227 | #if defined __CELLOS_LV2__ |
---|
228 | return lol_fcfid(lol_fctidz(x)); |
---|
229 | #else |
---|
230 | return __builtin_trunc(x); |
---|
231 | #endif |
---|
232 | } |
---|
233 | #endif |
---|
234 | |
---|
235 | double lol_sin(double x) |
---|
236 | { |
---|
237 | double absx = lol_fabs(x * INV_PI); |
---|
238 | |
---|
239 | /* If branches are cheap, skip the cycle count when |x| < π/4, |
---|
240 | * and only do the Taylor series up to the required precision. */ |
---|
241 | #if defined LOL_FEATURE_CHEAP_BRANCHES |
---|
242 | if (absx < QUARTER) |
---|
243 | { |
---|
244 | /* Computing x^4 is one multiplication too many we do, but it helps |
---|
245 | * interleave the Taylor series operations a lot better. */ |
---|
246 | double x2 = absx * absx; |
---|
247 | double x4 = x2 * x2; |
---|
248 | double sub1 = (SC[3] * x4 + SC[1]) * x4 + ONE; |
---|
249 | double sub2 = (SC[4] * x4 + SC[2]) * x4 + SC[0]; |
---|
250 | double taylor = sub2 * x2 + sub1; |
---|
251 | return x * taylor; |
---|
252 | } |
---|
253 | #endif |
---|
254 | |
---|
255 | /* Wrap |x| to the range [-1, 1] and keep track of the number of |
---|
256 | * cycles required. If odd, we'll need to change the sign of the |
---|
257 | * result. */ |
---|
258 | #if defined __CELLOS_LV2__ |
---|
259 | double sign = lol_fsel(x, PI, NEG_PI); |
---|
260 | double num_cycles = lol_round(absx); |
---|
261 | double is_even = lol_trunc(num_cycles * HALF) - (num_cycles * HALF); |
---|
262 | sign = lol_fsel(is_even, sign, -sign); |
---|
263 | #else |
---|
264 | double num_cycles = absx + TWO_EXP_52; |
---|
265 | FP_USE(num_cycles); num_cycles -= TWO_EXP_52; |
---|
266 | |
---|
267 | double is_even = TWO * num_cycles - ONE; |
---|
268 | FP_USE(is_even); is_even += TWO_EXP_54; |
---|
269 | FP_USE(is_even); is_even -= TWO_EXP_54; |
---|
270 | FP_USE(is_even); |
---|
271 | is_even -= TWO * num_cycles - ONE; |
---|
272 | double sign = is_even; |
---|
273 | #endif |
---|
274 | absx -= num_cycles; |
---|
275 | |
---|
276 | /* If branches are very cheap, we have the option to do the Taylor |
---|
277 | * series at a much lower degree by splitting. */ |
---|
278 | #if defined LOL_FEATURE_VERY_CHEAP_BRANCHES |
---|
279 | if (lol_fabs(absx) > QUARTER) |
---|
280 | { |
---|
281 | sign = (x * absx >= 0.0) ? sign : -sign; |
---|
282 | |
---|
283 | double x1 = HALF - lol_fabs(absx); |
---|
284 | double x2 = x1 * x1; |
---|
285 | double x4 = x2 * x2; |
---|
286 | double sub1 = ((CC[5] * x4 + CC[3]) * x4 + CC[1]) * x4 + ONE; |
---|
287 | double sub2 = (CC[4] * x4 + CC[2]) * x4 + CC[0]; |
---|
288 | double taylor = sub2 * x2 + sub1; |
---|
289 | |
---|
290 | return taylor * sign; |
---|
291 | } |
---|
292 | #endif |
---|
293 | |
---|
294 | #if !defined __CELLOS_LV2__ |
---|
295 | sign *= (x >= 0.0) ? PI : NEG_PI; |
---|
296 | #endif |
---|
297 | |
---|
298 | /* Compute a Tailor series for sin() and combine sign information. */ |
---|
299 | double x2 = absx * absx; |
---|
300 | double x4 = x2 * x2; |
---|
301 | #if defined LOL_FEATURE_VERY_CHEAP_BRANCHES |
---|
302 | double sub1 = (SC[3] * x4 + SC[1]) * x4 + ONE; |
---|
303 | double sub2 = (SC[4] * x4 + SC[2]) * x4 + SC[0]; |
---|
304 | #else |
---|
305 | double sub1 = (((SC[7] * x4 + SC[5]) * x4 + SC[3]) * x4 + SC[1]) * x4 + ONE; |
---|
306 | double sub2 = ((SC[6] * x4 + SC[4]) * x4 + SC[2]) * x4 + SC[0]; |
---|
307 | #endif |
---|
308 | double taylor = sub2 * x2 + sub1; |
---|
309 | |
---|
310 | return absx * taylor * sign; |
---|
311 | } |
---|
312 | |
---|
313 | double lol_cos(double x) |
---|
314 | { |
---|
315 | double absx = lol_fabs(x * INV_PI); |
---|
316 | |
---|
317 | #if defined LOL_FEATURE_CHEAP_BRANCHES |
---|
318 | if (absx < QUARTER) |
---|
319 | { |
---|
320 | double x2 = absx * absx; |
---|
321 | double x4 = x2 * x2; |
---|
322 | double sub1 = (CC[5] * x4 + CC[3]) * x4 + CC[1]; |
---|
323 | double sub2 = (CC[4] * x4 + CC[2]) * x4 + CC[0]; |
---|
324 | double taylor = (sub1 * x2 + sub2) * x2 + ONE; |
---|
325 | return taylor; |
---|
326 | } |
---|
327 | #endif |
---|
328 | |
---|
329 | #if defined __CELLOS_LV2__ |
---|
330 | double num_cycles = lol_round(absx); |
---|
331 | double is_even = lol_trunc(num_cycles * HALF) - (num_cycles * HALF); |
---|
332 | double sign = lol_fsel(is_even, ONE, NEG_ONE); |
---|
333 | #else |
---|
334 | double num_cycles = absx + TWO_EXP_52; |
---|
335 | FP_USE(num_cycles); num_cycles -= TWO_EXP_52; |
---|
336 | |
---|
337 | double is_even = TWO * num_cycles - ONE; |
---|
338 | FP_USE(is_even); is_even += TWO_EXP_54; |
---|
339 | FP_USE(is_even); is_even -= TWO_EXP_54; |
---|
340 | FP_USE(is_even); |
---|
341 | is_even -= TWO * num_cycles - ONE; |
---|
342 | double sign = is_even; |
---|
343 | #endif |
---|
344 | absx -= num_cycles; |
---|
345 | |
---|
346 | #if defined LOL_FEATURE_VERY_CHEAP_BRANCHES |
---|
347 | if (lol_fabs(absx) > QUARTER) |
---|
348 | { |
---|
349 | double x1 = HALF - lol_fabs(absx); |
---|
350 | double x2 = x1 * x1; |
---|
351 | double x4 = x2 * x2; |
---|
352 | double sub1 = (SC[3] * x4 + SC[1]) * x4 + ONE; |
---|
353 | double sub2 = (SC[4] * x4 + SC[2]) * x4 + SC[0]; |
---|
354 | double taylor = sub2 * x2 + sub1; |
---|
355 | |
---|
356 | return x1 * taylor * sign * PI; |
---|
357 | } |
---|
358 | #endif |
---|
359 | |
---|
360 | double x2 = absx * absx; |
---|
361 | double x4 = x2 * x2; |
---|
362 | #if defined LOL_FEATURE_VERY_CHEAP_BRANCHES |
---|
363 | double sub1 = ((CC[5] * x4 + CC[3]) * x4 + CC[1]) * x4 + ONE; |
---|
364 | double sub2 = (CC[4] * x4 + CC[2]) * x4 + CC[0]; |
---|
365 | #else |
---|
366 | double sub1 = (((CC[7] * x4 + CC[5]) * x4 + CC[3]) * x4 + CC[1]) * x4 + ONE; |
---|
367 | double sub2 = ((CC[6] * x4 + CC[4]) * x4 + CC[2]) * x4 + CC[0]; |
---|
368 | #endif |
---|
369 | double taylor = sub2 * x2 + sub1; |
---|
370 | |
---|
371 | return taylor * sign; |
---|
372 | } |
---|
373 | |
---|
374 | void lol_sincos(double x, double *sinx, double *cosx) |
---|
375 | { |
---|
376 | double absx = lol_fabs(x * INV_PI); |
---|
377 | |
---|
378 | #if defined LOL_FEATURE_CHEAP_BRANCHES |
---|
379 | if (absx < QUARTER) |
---|
380 | { |
---|
381 | double x2 = absx * absx; |
---|
382 | double x4 = x2 * x2; |
---|
383 | |
---|
384 | /* Computing the Taylor series to the 11th order is enough to get |
---|
385 | * x * 1e-11 precision, but we push it to the 13th order so that |
---|
386 | * tan() has a better precision. */ |
---|
387 | double subs1 = ((SC[5] * x4 + SC[3]) * x4 + SC[1]) * x4 + ONE; |
---|
388 | double subs2 = (SC[4] * x4 + SC[2]) * x4 + SC[0]; |
---|
389 | double taylors = subs2 * x2 + subs1; |
---|
390 | *sinx = x * taylors; |
---|
391 | |
---|
392 | double subc1 = (CC[5] * x4 + CC[3]) * x4 + CC[1]; |
---|
393 | double subc2 = (CC[4] * x4 + CC[2]) * x4 + CC[0]; |
---|
394 | double taylorc = (subc1 * x2 + subc2) * x2 + ONE; |
---|
395 | *cosx = taylorc; |
---|
396 | |
---|
397 | return; |
---|
398 | } |
---|
399 | #endif |
---|
400 | |
---|
401 | #if defined __CELLOS_LV2__ |
---|
402 | double num_cycles = lol_round(absx); |
---|
403 | double is_even = lol_trunc(num_cycles * HALF) - (num_cycles * HALF); |
---|
404 | |
---|
405 | double sin_sign = lol_fsel(x, PI, NEG_PI); |
---|
406 | sin_sign = lol_fsel(is_even, sin_sign, -sin_sign); |
---|
407 | double cos_sign = lol_fsel(is_even, ONE, NEG_ONE); |
---|
408 | #else |
---|
409 | double num_cycles = absx + TWO_EXP_52; |
---|
410 | FP_USE(num_cycles); num_cycles -= TWO_EXP_52; |
---|
411 | |
---|
412 | double is_even = TWO * num_cycles - ONE; |
---|
413 | FP_USE(is_even); is_even += TWO_EXP_54; |
---|
414 | FP_USE(is_even); is_even -= TWO_EXP_54; |
---|
415 | FP_USE(is_even); |
---|
416 | is_even -= TWO * num_cycles - ONE; |
---|
417 | double sin_sign = is_even; |
---|
418 | double cos_sign = is_even; |
---|
419 | #endif |
---|
420 | absx -= num_cycles; |
---|
421 | |
---|
422 | #if defined LOL_FEATURE_VERY_CHEAP_BRANCHES |
---|
423 | if (lol_fabs(absx) > QUARTER) |
---|
424 | { |
---|
425 | cos_sign = sin_sign; |
---|
426 | sin_sign = (x * absx >= 0.0) ? sin_sign : -sin_sign; |
---|
427 | |
---|
428 | double x1 = HALF - lol_fabs(absx); |
---|
429 | double x2 = x1 * x1; |
---|
430 | double x4 = x2 * x2; |
---|
431 | |
---|
432 | double subs1 = ((CC[5] * x4 + CC[3]) * x4 + CC[1]) * x4 + ONE; |
---|
433 | double subs2 = (CC[4] * x4 + CC[2]) * x4 + CC[0]; |
---|
434 | double taylors = subs2 * x2 + subs1; |
---|
435 | *sinx = taylors * sin_sign; |
---|
436 | |
---|
437 | double subc1 = ((SC[5] * x4 + SC[3]) * x4 + SC[1]) * x4 + ONE; |
---|
438 | double subc2 = (SC[4] * x4 + SC[2]) * x4 + SC[0]; |
---|
439 | double taylorc = subc2 * x2 + subc1; |
---|
440 | *cosx = x1 * taylorc * cos_sign * PI; |
---|
441 | |
---|
442 | return; |
---|
443 | } |
---|
444 | #endif |
---|
445 | |
---|
446 | #if !defined __CELLOS_LV2__ |
---|
447 | sin_sign *= (x >= 0.0) ? PI : NEG_PI; |
---|
448 | #endif |
---|
449 | |
---|
450 | double x2 = absx * absx; |
---|
451 | double x4 = x2 * x2; |
---|
452 | #if defined LOL_FEATURE_VERY_CHEAP_BRANCHES |
---|
453 | double subs1 = ((SC[5] * x4 + SC[3]) * x4 + SC[1]) * x4 + ONE; |
---|
454 | double subs2 = (SC[4] * x4 + SC[2]) * x4 + SC[0]; |
---|
455 | double subc1 = ((CC[5] * x4 + CC[3]) * x4 + CC[1]) * x4 + ONE; |
---|
456 | double subc2 = (CC[4] * x4 + CC[2]) * x4 + CC[0]; |
---|
457 | #else |
---|
458 | double subs1 = (((SC[7] * x4 + SC[5]) * x4 + SC[3]) * x4 + SC[1]) * x4 + ONE; |
---|
459 | double subs2 = ((SC[6] * x4 + SC[4]) * x4 + SC[2]) * x4 + SC[0]; |
---|
460 | /* Push Taylor series to the 19th order to enhance tan() accuracy. */ |
---|
461 | double subc1 = (((CC[7] * x4 + CC[5]) * x4 + CC[3]) * x4 + CC[1]) * x4 + ONE; |
---|
462 | double subc2 = (((CC[8] * x4 + CC[6]) * x4 + CC[4]) * x4 + CC[2]) * x4 + CC[0]; |
---|
463 | #endif |
---|
464 | double taylors = subs2 * x2 + subs1; |
---|
465 | *sinx = absx * taylors * sin_sign; |
---|
466 | |
---|
467 | double taylorc = subc2 * x2 + subc1; |
---|
468 | *cosx = taylorc * cos_sign; |
---|
469 | } |
---|
470 | |
---|
471 | void lol_sincos(float x, float *sinx, float *cosx) |
---|
472 | { |
---|
473 | double x2 = static_cast<double>(x); |
---|
474 | double s2, c2; |
---|
475 | lol_sincos(x2, &s2, &c2); |
---|
476 | *sinx = static_cast<float>(s2); |
---|
477 | *cosx = static_cast<float>(c2); |
---|
478 | } |
---|
479 | |
---|
480 | double lol_tan(double x) |
---|
481 | { |
---|
482 | #if defined LOL_FEATURE_CHEAP_BRANCHES |
---|
483 | double absx = lol_fabs(x * INV_PI); |
---|
484 | |
---|
485 | /* This value was determined empirically to ensure an error of no |
---|
486 | * more than x * 1e-11 in this range. */ |
---|
487 | if (absx < 0.163) |
---|
488 | { |
---|
489 | double x2 = absx * absx; |
---|
490 | double x4 = x2 * x2; |
---|
491 | double sub1 = (((TC[7] * x4 + TC[5]) * x4 |
---|
492 | + TC[3]) * x4 + TC[1]) * x4 + ONE; |
---|
493 | double sub2 = (((TC[8] * x4 + TC[6]) * x4 |
---|
494 | + TC[4]) * x4 + TC[2]) * x4 + TC[0]; |
---|
495 | double taylor = sub2 * x2 + sub1; |
---|
496 | return x * taylor; |
---|
497 | } |
---|
498 | #endif |
---|
499 | |
---|
500 | double sinx, cosx; |
---|
501 | lol_sincos(x, &sinx, &cosx); |
---|
502 | |
---|
503 | /* Ensure cosx isn't zero. FIXME: we lose the cosx sign here. */ |
---|
504 | double absc = lol_fabs(cosx); |
---|
505 | #if defined __CELLOS_LV2__ |
---|
506 | double is_cos_not_zero = absc - VERY_SMALL_NUMBER; |
---|
507 | cosx = lol_fsel(is_cos_not_zero, cosx, VERY_SMALL_NUMBER); |
---|
508 | return lol_fdiv(sinx, cosx); |
---|
509 | #else |
---|
510 | if (__unlikely(absc < VERY_SMALL_NUMBER)) |
---|
511 | cosx = VERY_SMALL_NUMBER; |
---|
512 | return sinx / cosx; |
---|
513 | #endif |
---|
514 | } |
---|
515 | |
---|
516 | } /* namespace lol */ |
---|
517 | |
---|