Blender V5.3
math_float4.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2013 Intel Corporation
2 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0 */
5
6#pragma once
7
8#include "util/math_base.h"
9#include "util/types_float4.h"
10
12
14{
15#ifdef __KERNEL_SSE__
16 return float4(_mm_setzero_ps());
17#else
18 return make_float4(0.0f);
19#endif
20}
21
26
31
33{
34#ifdef __KERNEL_SSE__
35 return int4(_mm_castps_si128(a));
36#else
37 return make_int4(
39#endif
40}
41
42#if !defined(__KERNEL_METAL__)
44{
45# ifdef __KERNEL_SSE__
46 const __m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x80000000));
47 return float4(_mm_xor_ps(a.m128, mask));
48# else
49 return make_float4(-a.x, -a.y, -a.z, -a.w);
50# endif
51}
52
54{
55# ifdef __KERNEL_SSE__
56 return float4(_mm_mul_ps(a.m128, b.m128));
57# else
58 return make_float4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
59# endif
60}
61
63{
64# if defined(__KERNEL_SSE__)
65 return a * make_float4(f);
66# else
67 return make_float4(a.x * f, a.y * f, a.z * f, a.w * f);
68# endif
69}
70
72{
73 return a * f;
74}
75
77{
78# ifdef __KERNEL_SSE__
79 return float4(_mm_div_ps(_mm_set1_ps(f), a.m128));
80# else
81 return make_float4(f / a.x, f / a.y, f / a.z, f / a.w);
82# endif
83}
84
86{
87 return a * (1.0f / f);
88}
89
91{
92# ifdef __KERNEL_SSE__
93 return float4(_mm_div_ps(a.m128, b.m128));
94# else
95 return make_float4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
96# endif
97}
98
100{
101# ifdef __KERNEL_SSE__
102 return float4(_mm_add_ps(a.m128, b.m128));
103# else
104 return make_float4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
105# endif
106}
107
109{
110 return a + make_float4(f);
111}
112
114{
115# ifdef __KERNEL_SSE__
116 return float4(_mm_sub_ps(a.m128, b.m128));
117# else
118 return make_float4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
119# endif
120}
121
123{
124 return a - make_float4(f);
125}
126
128{
129 return a = a + b;
130}
131
133{
134 return a = a - b;
135}
136
138{
139 return a = a * b;
140}
141
143{
144 return a = a * f;
145}
146
148{
149 return a = a / b;
150}
151
153{
154 return a = a / f;
155}
156
158{
159# ifdef __KERNEL_SSE__
160 return int4(_mm_castps_si128(_mm_cmplt_ps(a.m128, b.m128)));
161# else
162 return make_int4(a.x < b.x, a.y < b.y, a.z < b.z, a.w < b.w);
163# endif
164}
165
167{
168# ifdef __KERNEL_SSE__
169 return int4(_mm_castps_si128(_mm_cmpge_ps(a.m128, b.m128)));
170# else
171 return make_int4(a.x >= b.x, a.y >= b.y, a.z >= b.z, a.w >= b.w);
172# endif
173}
174
176{
177# ifdef __KERNEL_SSE__
178 return int4(_mm_castps_si128(_mm_cmpgt_ps(a.m128, b.m128)));
179# else
180 return make_int4(a.x > b.x, a.y > b.y, a.z > b.z, a.w > b.w);
181# endif
182}
183
185{
186# ifdef __KERNEL_SSE__
187 return int4(_mm_castps_si128(_mm_cmple_ps(a.m128, b.m128)));
188# else
189 return make_int4(a.x <= b.x, a.y <= b.y, a.z <= b.z, a.w <= b.w);
190# endif
191}
192
194{
195# ifdef __KERNEL_SSE__
196 return (_mm_movemask_ps(_mm_cmpeq_ps(a.m128, b.m128)) & 15) == 15;
197# else
198 return (a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w);
199# endif
200}
201
203{
204# ifdef __KERNEL_SSE__
205 return float4(_mm_xor_ps(a.m128, b.m128));
206# else
211# endif
212}
213
215{
216# ifdef __KERNEL_SSE__
217 return float4(_mm_min_ps(a.m128, b.m128));
218# else
219 return make_float4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
220# endif
221}
222
224{
225# ifdef __KERNEL_SSE__
226 return float4(_mm_max_ps(a.m128, b.m128));
227# else
228 return make_float4(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
229# endif
230}
231
232ccl_device_inline float4 clamp(const float4 a, const float4 mn, const float4 mx)
233{
234 return min(max(a, mn), mx);
235}
236#endif /* !__KERNEL_METAL__ */
237
239{
240#ifdef __KERNEL_SSE__
241# ifdef __KERNEL_NEON__
242 return float4(vfmaq_f32(c, a, b));
243# elif defined(__KERNEL_AVX2__)
244 return float4(_mm_fmadd_ps(a, b, c));
245# else
246 return a * b + c;
247# endif
248#else
249 return a * b + c;
250#endif
251}
252
254{
255#ifdef __KERNEL_SSE__
256# ifdef __KERNEL_NEON__
257 return float4(vfmaq_f32(vnegq_f32(c), a, b));
258# elif defined(__KERNEL_AVX2__)
259 return float4(_mm_fmsub_ps(a, b, c));
260# else
261 return a * b - c;
262# endif
263#else
264 return a * b - c;
265#endif
266}
267
268#ifdef __KERNEL_SSE__
269template<size_t i0, const size_t i1, const size_t i2, const size_t i3>
270__forceinline float4 shuffle(const float4 a)
271{
272# ifdef __KERNEL_NEON__
273 return float4(shuffle_neon<float32x4_t, i0, i1, i2, i3>(a.m128));
274# else
275 return float4(
276 _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(a), _MM_SHUFFLE(i3, i2, i1, i0))));
277# endif
278}
279
280template<> __forceinline float4 shuffle<0, 1, 0, 1>(const float4 a)
281{
282 return float4(_mm_movelh_ps(a, a));
283}
284
285template<> __forceinline float4 shuffle<2, 3, 2, 3>(const float4 a)
286{
287 return float4(_mm_movehl_ps(a, a));
288}
289
290# ifdef __KERNEL_SSE3__
291template<> __forceinline float4 shuffle<0, 0, 2, 2>(const float4 a)
292{
293 return float4(_mm_moveldup_ps(a));
294}
295
296template<> __forceinline float4 shuffle<1, 1, 3, 3>(const float4 a)
297{
298 return float4(_mm_movehdup_ps(a));
299}
300# endif /* __KERNEL_SSE3__ */
301
302template<size_t i0, const size_t i1, const size_t i2, const size_t i3>
303__forceinline float4 shuffle(const float4 a, const float4 b)
304{
305# ifdef __KERNEL_NEON__
306 return float4(shuffle_neon<float32x4_t, i0, i1, i2, i3>(a, b));
307# else
308 return float4(_mm_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0)));
309# endif
310}
311
312template<size_t i0> __forceinline float4 shuffle(const float4 a)
313{
314 return shuffle<i0, i0, i0, i0>(a);
315}
316template<size_t i0> __forceinline float4 shuffle(const float4 a, const float4 b)
317{
318# ifdef __KERNEL_NEON__
319 return float4(shuffle_neon<float32x4_t, i0, i0, i0, i0>(a, b));
320# else
321 return float4(_mm_shuffle_ps(a, b, _MM_SHUFFLE(i0, i0, i0, i0)));
322# endif
323}
324
325template<> __forceinline float4 shuffle<0, 1, 0, 1>(const float4 a, const float4 b)
326{
327 return float4(_mm_movelh_ps(a, b));
328}
329
330template<> __forceinline float4 shuffle<2, 3, 2, 3>(const float4 a, const float4 b)
331{
332 return float4(_mm_movehl_ps(b, a));
333}
334
335template<size_t i> __forceinline float extract(const float4 a)
336{
337 return _mm_cvtss_f32(shuffle<i, i, i, i>(a));
338}
339template<> __forceinline float extract<0>(const float4 a)
340{
341 return _mm_cvtss_f32(a);
342}
343#endif
344
346{
347#if defined(__KERNEL_SSE__)
348# if defined(__KERNEL_NEON__)
349 return vaddvq_f32(a);
350# elif defined(__KERNEL_SSE3__)
351 float4 h(_mm_hadd_ps(a.m128, a.m128));
352 return _mm_cvtss_f32(_mm_hadd_ps(h.m128, h.m128));
353# else
354 float4 h(shuffle<1, 0, 3, 2>(a) + a);
355 return _mm_cvtss_f32(shuffle<2, 3, 0, 1>(h) + h);
356# endif
357#else
358 return a.x + a.y + a.z + a.w;
359#endif
360}
361
363{
364#if defined(__KERNEL_SSE__)
365# if defined(__KERNEL_NEON__)
366 return vminvq_f32(a);
367# else
368 float4 h = min(shuffle<1, 0, 3, 2>(a), a);
369 return _mm_cvtss_f32(min(shuffle<2, 3, 0, 1>(h), h));
370# endif
371#else
372 return min(min(a.x, a.y), min(a.z, a.w));
373#endif
374}
375
377{
378#if defined(__KERNEL_SSE__)
379# if defined(__KERNEL_NEON__)
380 return vmaxvq_f32(a);
381# else
382 float4 h = max(shuffle<1, 0, 3, 2>(a), a);
383 return _mm_cvtss_f32(max(shuffle<2, 3, 0, 1>(h), h));
384# endif
385#else
386 return max(max(a.x, a.y), max(a.z, a.w));
387#endif
388}
389
390#if !defined(__KERNEL_METAL__)
391ccl_device_inline float dot(const float4 a, const float4 b)
392{
393# if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
394# if defined(__KERNEL_NEON__)
395 const __m128 t = vmulq_f32(a, b);
396 return vaddvq_f32(t);
397# else
398 return _mm_cvtss_f32(_mm_dp_ps(a, b, 0xFF));
399# endif
400# else
401 return (a.x * b.x + a.y * b.y) + (a.z * b.z + a.w * b.w);
402# endif
403}
404#endif /* !defined(__KERNEL_METAL__) */
405
407{
408 return sqrtf(dot(a, a));
409}
410
412{
413 return dot(a, a);
414}
415
417{
418 return a * a;
419}
420
421#if !defined(__KERNEL_METAL__)
423{
424 return len(a - b);
425}
426
428{
429# ifdef __KERNEL_SSE__
430 return float4(_mm_sqrt_ps(a.m128));
431# else
432 return make_float4(sqrtf(a.x), sqrtf(a.y), sqrtf(a.z), sqrtf(a.w));
433# endif
434}
435
437{
438# ifdef __KERNEL_SSE__
439 return (shuffle<1, 2, 0, 0>(a) * shuffle<2, 0, 1, 0>(b)) -
440 (shuffle<2, 0, 1, 0>(a) * shuffle<1, 2, 0, 0>(b));
441# else
442 return make_float4(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x, 0.0f);
443# endif
444}
445
447{
448# ifdef __KERNEL_SSE__
449 return a == zero_float4();
450# else
451 return (a.x == 0.0f && a.y == 0.0f && a.z == 0.0f && a.w == 0.0f);
452# endif
453}
454
456{
457 return reduce_add(a) * 0.25f;
458}
459
461{
462 return a / len(a);
463}
464
466{
467 const float t = len(a);
468 return (t != 0.0f) ? a / t : a;
469}
470
472{
473# if defined(__KERNEL_SSE__)
474# if defined(__KERNEL_NEON__)
475 return float4(vabsq_f32(a));
476# else
477 return float4(_mm_and_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff))));
478# endif
479# else
480 return make_float4(fabsf(a.x), fabsf(a.y), fabsf(a.z), fabsf(a.w));
481# endif
482}
483
484/* The floating-point remainder of the division operation `a / b` calculated by this function is
485 * exactly the value `a - iquot * b`, where `iquot` is `a / b with` its fractional part truncated.
486 *
487 * The returned value has the same sign as `a` and is less than `b` in magnitude. */
488ccl_device_inline float4 fmod(const float4 a, const float b)
489{
490# if defined(__KERNEL_NEON__)
491 /* Use native Neon instructions.
492 * The logic is the same as the SSE code below, but on Apple M2 Ultra this seems to be faster.
493 * Possibly due to some runtime checks in _mm_round_ps which do not get properly inlined. */
494 const float32x4_t iquot = vrndq_f32(a / b);
495 return float4(vsubq_f32(a, vmulq_f32(iquot, vdupq_n_f32(b))));
496# elif defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
497 const __m128 iquot = _mm_round_ps(a / b, _MM_FROUND_TRUNC);
498 return float4(_mm_sub_ps(a, _mm_mul_ps(iquot, _mm_set1_ps(b))));
499# else
500 return make_float4(fmodf(a.x, b), fmodf(a.y, b), fmodf(a.z, b), fmodf(a.w, b));
501# endif
502}
503
505{
506# ifdef __KERNEL_SSE__
507# if defined(__KERNEL_NEON__)
508 return float4(vrndmq_f32(a));
509# else
510 return float4(_mm_floor_ps(a));
511# endif
512# else
513 return make_float4(floorf(a.x), floorf(a.y), floorf(a.z), floorf(a.w));
514# endif
515}
516
518{
519# ifdef __KERNEL_SSE__
520 const float4 f = floor(x);
521 *i = int4(_mm_cvttps_epi32(f.m128));
522 return x - f;
523# else
524 float4 r;
525 r.x = floorfrac(x.x, &i->x);
526 r.y = floorfrac(x.y, &i->y);
527 r.z = floorfrac(x.z, &i->z);
528 r.w = floorfrac(x.w, &i->w);
529 return r;
530# endif
531}
532
537
539{
540 return make_float4(expf(v.x), expf(v.y), expf(v.z), expf(v.z));
541}
542
544{
545 return make_float4(logf(v.x), logf(v.y), logf(v.z), logf(v.z));
546}
547
548#endif /* !__KERNEL_METAL__ */
549
551{
552#if defined(__KERNEL_METAL__)
553 return all(a == b);
554#else
555 return a == b;
556#endif
557}
558
559template<class MaskType>
560ccl_device_inline float4 select(const MaskType mask, const float4 a, const float4 b)
561{
562#if defined(__KERNEL_METAL__)
563 return metal::select(b, a, bool4(mask));
564#elif defined(__KERNEL_SSE__)
565# ifdef __KERNEL_SSE42__
566 return float4(_mm_blendv_ps(b.m128, a.m128, _mm_castsi128_ps(mask.m128)));
567# else
568 return float4(
569 _mm_or_ps(_mm_and_ps(_mm_castsi128_ps(mask), a), _mm_andnot_ps(_mm_castsi128_ps(mask), b)));
570# endif
571#else
572 return make_float4(
573 (mask.x) ? a.x : b.x, (mask.y) ? a.y : b.y, (mask.z) ? a.z : b.z, (mask.w) ? a.w : b.w);
574#endif
575}
576
577template<class MaskType> ccl_device_inline float4 mask(const MaskType mask, const float4 a)
578{
579 /* Replace elements of x with zero where mask isn't set. */
580 return select(mask, a, zero_float4());
581}
582
583#ifndef __KERNEL_GPU__
584
586{
587# ifdef __KERNEL_SSE__
588 return float4(_mm_loadu_ps(v));
589# else
590 return make_float4(v[0], v[1], v[2], v[3]);
591# endif
592}
593
594#endif /* !__KERNEL_GPU__ */
595
597{
598 return (b != 0.0f) ? a / b : zero_float4();
599}
600
602{
603 return make_float4((b.x != 0.0f) ? a.x / b.x : 0.0f,
604 (b.y != 0.0f) ? a.y / b.y : 0.0f,
605 (b.z != 0.0f) ? a.z / b.z : 0.0f,
606 (b.w != 0.0f) ? a.w / b.w : 0.0f);
607}
608
610{
611 return isfinite_safe(v.x) && isfinite_safe(v.y) && isfinite_safe(v.z) && isfinite_safe(v.w);
612}
613
615{
616 float4 r = v;
617 if (!isfinite_safe(r.x)) {
618 r.x = 0.0f;
619 }
620 if (!isfinite_safe(r.y)) {
621 r.y = 0.0f;
622 }
623 if (!isfinite_safe(r.z)) {
624 r.z = 0.0f;
625 }
626 if (!isfinite_safe(r.w)) {
627 r.w = 0.0f;
628 }
629 return r;
630}
631
632/* Consistent name for this would be pow, but HIP compiler crashes in name mangling. */
633ccl_device_inline float4 power(const float4 v, const float e)
634{
635 return make_float4(powf(v.x, e), powf(v.y, e), powf(v.z, e), powf(v.w, e));
636}
637
639{
640 return a + t * (b - a);
641}
642
643#if !defined(__KERNEL_METAL__) && !defined(__KERNEL_ONEAPI__)
644/* Int/Float conversion */
646{
647# ifdef __KERNEL_SSE__
648 return int4(_mm_castps_si128(f.m128));
649# else
650 return make_int4(
652# endif
653}
654
656{
657# ifdef __KERNEL_SSE__
658 return float4(_mm_castsi128_ps(i.m128));
659# else
660 return make_float4(
662# endif
663}
664#endif /* !defined(__KERNEL_METAL__) && !defined(__KERNEL_ONEAPI__) */
665
667{
668 r[0] = val.x;
669 r[1] = val.y;
670 r[2] = val.z;
671 r[3] = val.w;
672}
673
__forceinline float extract(const int4 &b)
Definition binning.cpp:27
btScalar max
#define ccl_private
#define ccl_device_inline
#define ccl_device_template_spec
#define __forceinline
#define logf(x)
#define expf(x)
#define powf(x, y)
#define CCL_NAMESPACE_END
#define saturatef(x)
#define fmodf(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)
#define log
#define exp
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define select(A, B, C)
#define floor
float distance(float, float) RET
bool all(VecOp< bool, D >) RET
#define sqrt
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
constexpr T clamp(T, U, U) RET
VecBase< bool, 4 > bool4
bool is_zero(float3x3 a)
float safe_divide(float a, float b)
float reduce_add(float2 a)
float reduce_min(float2 a)
float average(float2 a)
float reduce_max(float2 a)
VecT safe_normalize(VecT vector)
__forceinline LightTreeMeasure operator+(const LightTreeMeasure &a, const LightTreeMeasure &b)
ccl_device_inline float floorfrac(const float x, ccl_private int *i)
Definition math_base.h:464
ccl_device_inline Interval< T1 > operator/=(ccl_private Interval< T1 > &interval, const T2 f)
Definition math_base.h:938
ccl_device_inline Extrema< T > operator*(const Extrema< T > a, const T b)
Definition math_base.h:967
ccl_device_inline Extrema< T > operator+=(ccl_private Extrema< T > &a, const ccl_private Extrema< T > &b)
Definition math_base.h:980
ccl_device_inline float ensure_finite(const float v)
Definition math_base.h:370
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:362
ccl_device_inline T make_zero()
Definition math_dual.h:20
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
ccl_device_inline dual< T > operator/(const dual< T > a, T b)
Definition math_dual.h:71
ccl_device_inline dual< T1 > operator-(const dual< T1 > a, T2 b)
Definition math_dual.h:113
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
ccl_device_inline bool operator<(const ccl_private dual1 &a, const float b)
Definition math_dual.h:45
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 float2 power(const float2 v, const float e)
ccl_device_inline float2 fmod(const float2 a, const float b)
ccl_device_inline bool isequal(const float2 a, const float2 b)
ccl_device_inline bool operator==(const float2 a, const float2 b)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
ccl_device_inline int2 operator>=(const float2 a, const float2 b)
ccl_device_inline float2 operator*=(float2 &a, const float2 b)
Definition math_float2.h:90
ccl_device_inline int3 operator>(const float3 a, const float3 b)
ccl_device_inline float3 operator-=(float3 &a, const float3 b)
ccl_device_inline float4 msub(const float4 a, const float4 b, const float4 c)
ccl_device_inline int4 __float4_as_int4(const float4 f)
ccl_device_inline float4 one_float4()
Definition math_float4.h:22
ccl_device_inline int4 operator<=(const float4 a, const float4 b)
ccl_device_inline int4 cast(const float4 a)
Definition math_float4.h:32
ccl_device_inline float4 operator^(const float4 a, const float4 b)
ccl_device_inline float4 __int4_as_float4(const int4 i)
ccl_device_inline float4 load_float4(const ccl_private float *v)
ccl_device_inline void copy_v4_v4(ccl_private float *r, const float4 val)
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:13
#define sqr
#define floorf
#define fabsf
#define sqrtf
#define make_float4
#define saturate(a)
Definition smaa.cc:319
#define min(a, b)
Definition sort.cc:38
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225