Blender V5.0
math_fast.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004 NVIDIA Corporation
2 * SPDX-FileCopyrightText: 2008-2014 Larry Gritz
3 * SPDX-FileCopyrightText: 2009-2014 Sony Pictures Imageworks Inc., et al.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Adapted from OpenImageIO
8 * Copyright 2008-2014 Larry Gritz and the other authors and contributors.
9 * All Rights Reserved.
10 *
11 * A few bits here are based upon code from NVIDIA that was also released
12 * under the same modified BSD license, and marked as:
13 * `Copyright 2004 NVIDIA Corporation. All Rights Reserved.`
14 *
15 * Some parts of this file were first open-sourced in Open Shading Language,
16 * then later moved here. The original copyright notice was:
17 * `Copyright (c) 2009-2014 Sony Pictures Imageworks Inc., et al.`
18 *
19 * Many of the math functions were copied from or inspired by other
20 * public domain sources or open source packages with compatible licenses.
21 * The individual functions give references were applicable.
22 */
23
24#pragma once
25
26#include "util/math_base.h"
27#include "util/math_float3.h"
28#include "util/math_float4.h"
29#include "util/math_int4.h"
30#include "util/types_float3.h"
31#include "util/types_float4.h"
32
34
35ccl_device_inline float madd(const float a, const float b, const float c)
36{
37 /* NOTE: In the future we may want to explicitly ask for a fused
38 * multiply-add in a specialized version for float.
39 *
40 * NOTE: GCC/ICC will turn this (for float) into a FMA unless
41 * explicitly asked not to, clang seems to leave the code alone.
42 */
43 return a * b + c;
44}
45
47{
48 return a * b + c;
49}
50
51/*
52 * FAST & APPROXIMATE MATH
53 *
54 * The functions named "fast_*" provide a set of replacements to `libm` that
55 * are much faster at the expense of some accuracy and robust handling of
56 * extreme values. One design goal for these approximation was to avoid
57 * branches as much as possible and operate on single precision values only
58 * so that SIMD versions should be straightforward ports We also try to
59 * implement "safe" semantics (ie: clamp to valid range where possible)
60 * natively since wrapping these inline calls in another layer would be
61 * wasteful.
62 *
63 * Some functions are fast_safe_*, which is both a faster approximation as
64 * well as clamped input domain to ensure no NaN, Inf, or divide by zero.
65 */
66
67/* Round to nearest integer, returning as an int. */
69{
70 /* used by sin/cos/tan range reduction. */
71#ifdef __KERNEL_SSE42__
72 /* Single `roundps` instruction on SSE4.1+ for gcc/clang but not MSVC 19.35:
73 * float_to_int(rintf(x)); so we use the equivalent intrinsics. */
74 __m128 vec = _mm_set_ss(x);
75 vec = _mm_round_ss(vec, vec, (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
76 return _mm_cvtss_si32(vec);
77#else
78 /* emulate rounding by adding/subtracting 0.5. */
79 return float_to_int(x + copysignf(0.5f, x));
80#endif
81}
82
84{
85 /* Very accurate argument reduction from SLEEF,
86 * starts failing around x=262000
87 *
88 * Results on: [-2pi,2pi].
89 *
90 * Examined 2173837240 values of sin: 0.00662760244 avg ULP diff, 2 max ULP,
91 * 1.19209e-07 max error
92 */
93 const int q = fast_rint(x * M_1_PI_F);
94 const float qf = (float)q;
95 x = madd(qf, -0.78515625f * 4, x);
96 x = madd(qf, -0.00024187564849853515625f * 4, x);
97 x = madd(qf, -3.7747668102383613586e-08f * 4, x);
98 x = madd(qf, -1.2816720341285448015e-12f * 4, x);
99 x = M_PI_2_F - (M_PI_2_F - x); /* Crush denormals */
100 const float s = x * x;
101 if ((q & 1) != 0) {
102 x = -x;
103 }
104 /* This polynomial approximation has very low error on [-pi/2,+pi/2]
105 * 1.19209e-07 max error in total over [-2pi,+2pi]. */
106 float u = 2.6083159809786593541503e-06f;
107 u = madd(u, s, -0.0001981069071916863322258f);
108 u = madd(u, s, +0.00833307858556509017944336f);
109 u = madd(u, s, -0.166666597127914428710938f);
110 u = madd(s, u * x, x);
111 /* For large x, the argument reduction can fail and the polynomial can be
112 * evaluated with arguments outside the valid internal. Just clamp the bad
113 * values away. */
114 u = clamp(u, -1.0f, 1.0f);
115 return u;
116}
117
119{
120 /* Same argument reduction as fast_sinf(). */
121 const int q = fast_rint(x * M_1_PI_F);
122 const float qf = (float)q;
123 x = madd(qf, -0.78515625f * 4, x);
124 x = madd(qf, -0.00024187564849853515625f * 4, x);
125 x = madd(qf, -3.7747668102383613586e-08f * 4, x);
126 x = madd(qf, -1.2816720341285448015e-12f * 4, x);
127 x = M_PI_2_F - (M_PI_2_F - x); /* Crush denormals. */
128 const float s = x * x;
129 /* Polynomial from SLEEF's sincosf, max error is
130 * 4.33127e-07 over [-2pi,2pi] (98% of values are "exact"). */
131 float u = -2.71811842367242206819355e-07f;
132 u = madd(u, s, +2.47990446951007470488548e-05f);
133 u = madd(u, s, -0.00138888787478208541870117f);
134 u = madd(u, s, +0.0416666641831398010253906f);
135 u = madd(u, s, -0.5f);
136 u = madd(u, s, +1.0f);
137 if ((q & 1) != 0) {
138 u = -u;
139 }
140 u = clamp(u, -1.0f, 1.0f);
141 return u;
142}
143
144ccl_device void fast_sincosf(float x, ccl_private float *sine, ccl_private float *cosine)
145{
146 /* Same argument reduction as fast_sin. */
147 const int q = fast_rint(x * M_1_PI_F);
148 const float qf = (float)q;
149 x = madd(qf, -0.78515625f * 4, x);
150 x = madd(qf, -0.00024187564849853515625f * 4, x);
151 x = madd(qf, -3.7747668102383613586e-08f * 4, x);
152 x = madd(qf, -1.2816720341285448015e-12f * 4, x);
153 x = M_PI_2_F - (M_PI_2_F - x); // crush denormals
154 const float s = x * x;
155 /* NOTE: same exact polynomials as fast_sinf() and fast_cosf() above. */
156 if ((q & 1) != 0) {
157 x = -x;
158 }
159 float su = 2.6083159809786593541503e-06f;
160 su = madd(su, s, -0.0001981069071916863322258f);
161 su = madd(su, s, +0.00833307858556509017944336f);
162 su = madd(su, s, -0.166666597127914428710938f);
163 su = madd(s, su * x, x);
164 float cu = -2.71811842367242206819355e-07f;
165 cu = madd(cu, s, +2.47990446951007470488548e-05f);
166 cu = madd(cu, s, -0.00138888787478208541870117f);
167 cu = madd(cu, s, +0.0416666641831398010253906f);
168 cu = madd(cu, s, -0.5f);
169 cu = madd(cu, s, +1.0f);
170 if ((q & 1) != 0) {
171 cu = -cu;
172 }
173 su = clamp(su, -1.0f, 1.0f);
174 cu = clamp(cu, -1.0f, 1.0f);
175 *sine = su;
176 *cosine = cu;
177}
178
179/* NOTE: this approximation is only valid on [-8192.0,+8192.0], it starts
180 * becoming really poor outside of this range because the reciprocal amplifies
181 * errors.
182 */
184{
185 /* Derived from SLEEF implementation.
186 *
187 * Note that we cannot apply the "denormal crush" trick everywhere because
188 * we sometimes need to take the reciprocal of the polynomial
189 */
190 const int q = fast_rint(x * 2.0f * M_1_PI_F);
191 const float qf = (float)q;
192 x = madd(qf, -0.78515625f * 2, x);
193 x = madd(qf, -0.00024187564849853515625f * 2, x);
194 x = madd(qf, -3.7747668102383613586e-08f * 2, x);
195 x = madd(qf, -1.2816720341285448015e-12f * 2, x);
196 if ((q & 1) == 0) {
197 /* Crush denormals (only if we aren't inverting the result later). */
198 x = M_PI_4_F - (M_PI_4_F - x);
199 }
200 const float s = x * x;
201 float u = 0.00927245803177356719970703f;
202 u = madd(u, s, 0.00331984995864331722259521f);
203 u = madd(u, s, 0.0242998078465461730957031f);
204 u = madd(u, s, 0.0534495301544666290283203f);
205 u = madd(u, s, 0.133383005857467651367188f);
206 u = madd(u, s, 0.333331853151321411132812f);
207 u = madd(s, u * x, x);
208 if ((q & 1) != 0) {
209 u = -1.0f / u;
210 }
211 return u;
212}
213
214/* Fast, approximate sin(x*M_PI) with maximum absolute error of 0.000918954611.
215 *
216 * Adapted from http://devmaster.net/posts/9648/fast-and-accurate-sine-cosine#comment-76773
217 */
218ccl_device float fast_sinpif(const float x)
219{
220 /* Fast trick to strip the integral part off, so our domain is [-1, 1]. */
221 const float z = x - ((x + 25165824.0f) - 25165824.0f);
222 const float y = z - z * fabsf(z);
223 const float Q = 3.10396624f;
224 const float P = 3.584135056f; /* P = 16-4*Q */
225 return y * (Q + P * fabsf(y));
226
227 /* The original article used inferior constants for Q and P and
228 * so had max error 1.091e-3.
229 *
230 * The optimal value for Q was determined by exhaustive search, minimizing
231 * the absolute numerical error relative to float(std::sin(double(phi*M_PI)))
232 * over the interval [0,2] (which is where most of the invocations happen).
233 *
234 * The basic idea of this approximation starts with the coarse approximation:
235 * sin(pi*x) ~= f(x) = 4 * (x - x * abs(x))
236 *
237 * This approximation always _over_ estimates the target. On the other hand,
238 * the curve:
239 * sin(pi*x) ~= f(x) * abs(f(x)) / 4
240 *
241 * always lies _under_ the target. Thus we can simply numerically search for
242 * the optimal constant to LERP these curves into a more precise
243 * approximation.
244 *
245 * After folding the constants together and simplifying the resulting math,
246 * we end up with the compact implementation above.
247 *
248 * NOTE: this function actually computes sin(x * pi) which avoids one or two
249 * mults in many cases and guarantees exact values at integer periods.
250 */
251}
252
253/* Fast approximate cos(x*M_PI) with ~0.1% absolute error. */
255{
256 return fast_sinpif(x + 0.5f);
257}
258
259ccl_device float fast_acosf(const float x)
260{
261 const float f = fabsf(x);
262 /* clamp and crush denormals. */
263 const float m = (f < 1.0f) ? 1.0f - (1.0f - f) : 1.0f;
264 /* Based on http://www.pouet.net/topic.php?which=9132&page=2
265 * 85% accurate (ULP 0)
266 * Examined 2130706434 values of acos:
267 * 15.2000597 avg ULP diff, 4492 max ULP, 4.51803e-05 max error // without "denormal crush"
268 * Examined 2130706434 values of acos:
269 * 15.2007108 avg ULP diff, 4492 max ULP, 4.51803e-05 max error // with "denormal crush"
270 */
271 const float a = sqrtf(1.0f - m) *
272 (1.5707963267f + m * (-0.213300989f + m * (0.077980478f + m * -0.02164095f)));
273 return x < 0 ? M_PI_F - a : a;
274}
275
276ccl_device float fast_asinf(const float x)
277{
278 /* Based on acosf approximation above.
279 * Max error is 4.51133e-05 (ULPS are higher because we are consistently off
280 * by a little amount). */
281 const float f = fabsf(x);
282 /* Clamp and crush denormals. */
283 const float m = (f < 1.0f) ? 1.0f - (1.0f - f) : 1.0f;
284 const float a = M_PI_2_F -
285 sqrtf(1.0f - m) * (1.5707963267f +
286 m * (-0.213300989f + m * (0.077980478f + m * -0.02164095f)));
287 return copysignf(a, x);
288}
289
290ccl_device float fast_atanf(const float x)
291{
292 const float a = fabsf(x);
293 const float k = a > 1.0f ? 1 / a : a;
294 const float s = 1.0f - (1.0f - k); /* Crush denormals. */
295 const float t = s * s;
296 /* http://mathforum.org/library/drmath/view/62672.html
297 * Examined 4278190080 values of atan:
298 * 2.36864877 avg ULP diff, 302 max ULP, 6.55651e-06 max error // (with denormals)
299 * Examined 4278190080 values of atan:
300 * 171160502 avg ULP diff, 855638016 max ULP, 6.55651e-06 max error // (crush denormals)
301 */
302 float r = s * madd(0.43157974f, t, 1.0f) / madd(madd(0.05831938f, t, 0.76443945f), t, 1.0f);
303 if (a > 1.0f) {
304 r = M_PI_2_F - r;
305 }
306 return copysignf(r, x);
307}
308
309ccl_device float fast_atan2f(const float y, const float x)
310{
311 /* Based on atan approximation above.
312 *
313 * The special cases around 0 and infinity were tested explicitly.
314 *
315 * The only case not handled correctly is x=NaN,y=0 which returns 0 instead
316 * of nan.
317 */
318 const float a = fabsf(x);
319 const float b = fabsf(y);
320
321 const float k = (b == 0) ? 0.0f : ((a == b) ? 1.0f : (b > a ? a / b : b / a));
322 const float s = 1.0f - (1.0f - k); /* Crush denormals */
323 const float t = s * s;
324
325 float r = s * madd(0.43157974f, t, 1.0f) / madd(madd(0.05831938f, t, 0.76443945f), t, 1.0f);
326
327 if (b > a) {
328 /* Account for arg reduction. */
329 r = M_PI_2_F - r;
330 }
331 /* Test sign bit of x. */
332 if (__float_as_uint(x) & 0x80000000u) {
333 r = M_PI_F - r;
334 }
335 return copysignf(r, y);
336}
337
338/* Same as precise_angle, but using fast_atan2f. Still much better that acos(dot(a, b)). */
340{
341 return 2.0f * fast_atan2f(len(a - b), len(a + b));
342}
343
345{
346 return (int)(__float_as_uint(x) >> 23) - 127;
347}
348
349/* Based on:
350 *
351 * https://github.com/LiraNuna/glsl-sse2/blob/master/source/vec4.h
352 */
354{
355 /* NOTE: clamp to avoid special cases and make result "safe" from large
356 * negative values/NAN's. */
357 x = clamp(x, FLT_MIN, FLT_MAX);
358 const unsigned bits = __float_as_uint(x);
359 const int exponent = floor_log2f(x);
360 const float f = __uint_as_float((bits & 0x007FFFFF) | 0x3f800000) - 1.0f;
361 /* Examined 2130706432 values of log2 on [1.17549435e-38,3.40282347e+38]:
362 * 0.0797524457 avg ULP diff, 3713596 max ULP, 7.62939e-06 max error.
363 * ULP histogram:
364 * 0 = 97.46%
365 * 1 = 2.29%
366 * 2 = 0.11%
367 */
368 const float f2 = f * f;
369 const float f4 = f2 * f2;
370 float hi = madd(f, -0.00931049621349f, 0.05206469089414f);
371 float lo = madd(f, 0.47868480909345f, -0.72116591947498f);
372 hi = madd(f, hi, -0.13753123777116f);
373 hi = madd(f, hi, 0.24187369696082f);
374 hi = madd(f, hi, -0.34730547155299f);
375 lo = madd(f, lo, 1.442689881667200f);
376 return ((f4 * hi) + (f * lo)) + exponent;
377}
378
379ccl_device_inline float fast_logf(const float x)
380{
381 /* Examined 2130706432 values of logf on [1.17549435e-38,3.40282347e+38]:
382 * 0.313865375 avg ULP diff, 5148137 max ULP, 7.62939e-06 max error.
383 */
384 return fast_log2f(x) * M_LN2_F;
385}
386
388{
389 /* Examined 2130706432 values of log10f on [1.17549435e-38,3.40282347e+38]:
390 * 0.631237033 avg ULP diff, 4471615 max ULP, 3.8147e-06 max error.
391 */
392 return fast_log2f(x) * M_LN2_F / M_LN10_F;
393}
394
396{
397 /* Don't bother with denormals. */
398 x = fabsf(x);
399 x = clamp(x, FLT_MIN, FLT_MAX);
400 const unsigned bits = __float_as_uint(x);
401 return (float)((int)(bits >> 23) - 127);
402}
403
405{
406 /* Clamp to safe range for final addition. */
407 x = clamp(x, -126.0f, 126.0f);
408 /* Range reduction. */
409 const int m = (int)x;
410 x -= m;
411 x = 1.0f - (1.0f - x); /* Crush denormals (does not affect max ULPS!). */
412 /* 5th degree polynomial generated with sollya
413 * Examined 2247622658 values of exp2 on [-126,126]: 2.75764912 avg ULP diff,
414 * 232 max ULP.
415 *
416 * ULP histogram:
417 * 0 = 87.81%
418 * 1 = 4.18%
419 */
420 float r = 1.33336498402e-3f;
421 r = madd(x, r, 9.810352697968e-3f);
422 r = madd(x, r, 5.551834031939e-2f);
423 r = madd(x, r, 0.2401793301105f);
424 r = madd(x, r, 0.693144857883f);
425 r = madd(x, r, 1.0f);
426 /* Multiply by 2 ^ m by adding in the exponent. */
427 /* NOTE: left-shift of negative number is undefined behavior. */
428 return __uint_as_float(__float_as_uint(r) + ((unsigned)m << 23));
429}
430
431ccl_device_inline float fast_expf(const float x)
432{
433 /* Examined 2237485550 values of exp on [-87.3300018,87.3300018]:
434 * 2.6666452 avg ULP diff, 230 max ULP.
435 */
436 return fast_exp2f(x / M_LN2_F);
437}
438
439#if !defined(__KERNEL_GPU__) && !defined(_MSC_VER)
440/* MSVC seems to have a code-gen bug here in at least SSE41/AVX, see
441 * #78047 and #78869 for details. Just disable for now, it only makes
442 * a small difference in denoising performance. */
444{
445 const float4 one = make_float4(1.0f);
446 const float4 limit = make_float4(126.0f);
447 x = clamp(x, -limit, limit);
448 const int4 m = make_int4(x);
449 x = one - (one - (x - make_float4(m)));
450 float4 r = make_float4(1.33336498402e-3f);
451 r = madd4(x, r, make_float4(9.810352697968e-3f));
452 r = madd4(x, r, make_float4(5.551834031939e-2f));
453 r = madd4(x, r, make_float4(0.2401793301105f));
454 r = madd4(x, r, make_float4(0.693144857883f));
455 r = madd4(x, r, make_float4(1.0f));
456 return __int4_as_float4(__float4_as_int4(r) + (m << 23));
457}
458
463#else
465{
466 return make_float4(fast_expf(x.x), fast_expf(x.y), fast_expf(x.z), fast_expf(x.w));
467}
468#endif
469
471{
472 /* Examined 2217701018 values of exp10 on [-37.9290009,37.9290009]:
473 * 2.71732409 avg ULP diff, 232 max ULP.
474 */
475 return fast_exp2f(x * M_LN10_F / M_LN2_F);
476}
477
479{
480 if (fabsf(x) < 1e-5f) {
481 x = 1.0f - (1.0f - x); /* Crush denormals. */
482 return madd(0.5f, x * x, x);
483 }
484 return fast_expf(x) - 1.0f;
485}
486
487ccl_device float fast_sinhf(const float x)
488{
489 float a = fabsf(x);
490 if (a > 1.0f) {
491 /* Examined 53389559 values of sinh on [1,87.3300018]:
492 * 33.6886442 avg ULP diff, 178 max ULP. */
493 const float e = fast_expf(a);
494 return copysignf(0.5f * e - 0.5f / e, x);
495 }
496 a = 1.0f - (1.0f - a); /* Crush denorms. */
497 const float a2 = a * a;
498 /* Degree 7 polynomial generated with sollya. */
499 /* Examined 2130706434 values of sinh on [-1,1]: 1.19209e-07 max error. */
500 float r = 2.03945513931e-4f;
501 r = madd(r, a2, 8.32990277558e-3f);
502 r = madd(r, a2, 0.1666673421859f);
503 r = madd(r * a, a2, a);
504 return copysignf(r, x);
505}
506
508{
509 /* Examined 2237485550 values of cosh on [-87.3300018,87.3300018]:
510 * 1.78256726 avg ULP diff, 178 max ULP.
511 */
512 const float e = fast_expf(fabsf(x));
513 return 0.5f * e + 0.5f / e;
514}
515
517{
518 /* Examined 4278190080 values of tanh on [-3.40282347e+38,3.40282347e+38]:
519 * 3.12924e-06 max error.
520 */
521 /* NOTE: ULP error is high because of sub-optimal handling around the origin. */
522 const float e = fast_expf(2.0f * fabsf(x));
523 return copysignf(1.0f - 2.0f / (1.0f + e), x);
524}
525
526ccl_device float fast_safe_powf(const float x, const float y)
527{
528 if (y == 0) {
529 return 1.0f; /* x^1=1 */
530 }
531 if (x == 0) {
532 return 0.0f; /* 0^y=0 */
533 }
534 float sign = 1.0f;
535 if (x < 0.0f) {
536 /* if x is negative, only deal with integer powers
537 * powf returns NaN for non-integers, we will return 0 instead.
538 */
539 const int ybits = __float_as_int(y) & 0x7fffffff;
540 if (ybits >= 0x4b800000) {
541 // always even int, keep positive
542 }
543 else if (ybits >= 0x3f800000) {
544 /* Bigger than 1, check. */
545 const int k = (ybits >> 23) - 127; /* Get exponent. */
546 const int j = ybits >> (23 - k); /* Shift out possible fractional bits. */
547 if ((j << (23 - k)) == ybits) { /* rebuild number and check for a match. */
548 /* +1 for even, -1 for odd. */
549 sign = __int_as_float(0x3f800000 | (j << 31));
550 }
551 else {
552 /* Not an integer. */
553 return 0.0f;
554 }
555 }
556 else {
557 /* Not an integer. */
558 return 0.0f;
559 }
560 }
561 return sign * fast_exp2f(y * fast_log2f(fabsf(x)));
562}
563
564/* TODO(sergey): Check speed with our erf functions implementation from
565 * bsdf_microfacet.h.
566 */
567
568ccl_device_inline float fast_erff(const float x)
569{
570 /* Examined 1082130433 values of erff on [0,4]: 1.93715e-06 max error. */
571 /* Abramowitz and Stegun, 7.1.28. */
572 const float a1 = 0.0705230784f;
573 const float a2 = 0.0422820123f;
574 const float a3 = 0.0092705272f;
575 const float a4 = 0.0001520143f;
576 const float a5 = 0.0002765672f;
577 const float a6 = 0.0000430638f;
578 const float a = fabsf(x);
579 if (a >= 12.3f) {
580 return copysignf(1.0f, x);
581 }
582 const float b = 1.0f - (1.0f - a); /* Crush denormals. */
583 const float r = madd(
584 madd(madd(madd(madd(madd(a6, b, a5), b, a4), b, a3), b, a2), b, a1), b, 1.0f);
585 const float s = r * r; /* ^2 */
586 const float t = s * s; /* ^4 */
587 const float u = t * t; /* ^8 */
588 const float v = u * u; /* ^16 */
589 return copysignf(1.0f - 1.0f / v, x);
590}
591
593{
594 /* Examined 2164260866 values of erfcf on [-4,4]: 1.90735e-06 max error.
595 *
596 * ULP histogram:
597 *
598 * 0 = 80.30%
599 */
600 return 1.0f - fast_erff(x);
601}
602
604{
605 /* From: Approximating the `erfinv` function by Mike Giles. */
606 /* To avoid trouble at the limit, clamp input to 1-epsilon. */
607 const float a = min(fabsf(x), 0.99999994f);
608 float w = -fast_logf((1.0f - a) * (1.0f + a));
609 float p;
610 if (w < 5.0f) {
611 w = w - 2.5f;
612 p = 2.81022636e-08f;
613 p = madd(p, w, 3.43273939e-07f);
614 p = madd(p, w, -3.5233877e-06f);
615 p = madd(p, w, -4.39150654e-06f);
616 p = madd(p, w, 0.00021858087f);
617 p = madd(p, w, -0.00125372503f);
618 p = madd(p, w, -0.00417768164f);
619 p = madd(p, w, 0.246640727f);
620 p = madd(p, w, 1.50140941f);
621 }
622 else {
623 w = sqrtf(w) - 3.0f;
624 p = -0.000200214257f;
625 p = madd(p, w, 0.000100950558f);
626 p = madd(p, w, 0.00134934322f);
627 p = madd(p, w, -0.00367342844f);
628 p = madd(p, w, 0.00573950773f);
629 p = madd(p, w, -0.0076224613f);
630 p = madd(p, w, 0.00943887047f);
631 p = madd(p, w, 1.00167406f);
632 p = madd(p, w, 2.83297682f);
633 }
634 return p * x;
635}
636
637/* Fast inverse cube root for positive x, with two Newton iterations to improve accuracy. */
639{
640 util_assert(x >= 0.0f);
641
642 /* Constant is roughly `cbrt(2^127)`, but tweaked a bit to balance the error across the entire
643 * range. The exact value is not critical. */
644 float y = __int_as_float(0x54a24242 - __float_as_int(x) / 3);
645 y = (2.0f / 3) * y + 1 / (3 * y * y * x);
646 y = (2.0f / 3) * y + 1 / (3 * y * y * x);
647 return y;
648}
649
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
nullptr float
#define M_LN10_F
#define util_assert(statement)
#define M_PI_2_F
#define M_PI_4_F
#define ccl_private
#define ccl_device_inline
#define M_1_PI_F
#define M_LN2_F
#define CCL_NAMESPACE_END
#define copysignf(x, y)
#define __int_as_float(x)
#define __float_as_int(x)
#define __float_as_uint(x)
ccl_device_forceinline int4 make_int4(const int x, const int y, const int z, const int w)
#define __uint_as_float(x)
constexpr T sign(T) RET
constexpr T clamp(T, U, U) RET
ccl_device_inline int float_to_int(const float f)
Definition math_base.h:407
ccl_device float fast_exp2f(float x)
Definition math_fast.h:404
ccl_device float fast_atan2f(const float y, const float x)
Definition math_fast.h:309
ccl_device float fast_acosf(const float x)
Definition math_fast.h:259
ccl_device_inline float fast_exp10(const float x)
Definition math_fast.h:470
CCL_NAMESPACE_BEGIN ccl_device_inline float madd(const float a, const float b, const float c)
Definition math_fast.h:35
ccl_device_inline float4 madd4(const float4 a, const float4 b, const float4 c)
Definition math_fast.h:46
ccl_device void fast_sincosf(float x, ccl_private float *sine, ccl_private float *cosine)
Definition math_fast.h:144
ccl_device float4 fast_exp2f4(float4 x)
Definition math_fast.h:443
ccl_device float fast_asinf(const float x)
Definition math_fast.h:276
ccl_device float fast_sinpif(const float x)
Definition math_fast.h:218
ccl_device_inline float fast_log10(const float x)
Definition math_fast.h:387
ccl_device_inline int fast_rint(const float x)
Definition math_fast.h:68
ccl_device_inline float fast_erfcf(const float x)
Definition math_fast.h:592
ccl_device_inline float fast_erff(const float x)
Definition math_fast.h:568
ccl_device float fast_logb(float x)
Definition math_fast.h:395
ccl_device float fast_tanf(float x)
Definition math_fast.h:183
ccl_device_inline float fast_expf(const float x)
Definition math_fast.h:431
ccl_device_inline float fast_coshf(const float x)
Definition math_fast.h:507
ccl_device float fast_sinhf(const float x)
Definition math_fast.h:487
ccl_device_inline float fast_cospif(const float x)
Definition math_fast.h:254
ccl_device_inline float fast_tanhf(const float x)
Definition math_fast.h:516
ccl_device_inline float fast_expm1f(float x)
Definition math_fast.h:478
ccl_device float fast_safe_powf(const float x, const float y)
Definition math_fast.h:526
ccl_device_inline int floor_log2f(const float x)
Definition math_fast.h:344
ccl_device float fast_atanf(const float x)
Definition math_fast.h:290
ccl_device_inline float vector_angle(const float3 a, const float3 b)
Definition math_fast.h:339
ccl_device float fast_sinf(float x)
Definition math_fast.h:83
ccl_device float fast_cosf(float x)
Definition math_fast.h:118
ccl_device_inline float fast_inv_cbrtf(const float x)
Definition math_fast.h:638
ccl_device float fast_log2f(float x)
Definition math_fast.h:353
ccl_device_inline float fast_logf(const float x)
Definition math_fast.h:379
ccl_device_inline float4 fast_expf4(const float4 x)
Definition math_fast.h:459
ccl_device_inline float fast_ierff(const float x)
Definition math_fast.h:603
ccl_device_inline int4 __float4_as_int4(const float4 f)
ccl_device_inline float4 __int4_as_float4(const int4 i)
#define fabsf
#define sqrtf
#define ccl_device
#define make_float4
#define M_PI_F
#define min(a, b)
Definition sort.cc:36
#define FLT_MAX
Definition stdcycles.h:14
uint len