2 * Copyright 2011-2017 Blender Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __UTIL_MATH_FLOAT3_H__
18 #define __UTIL_MATH_FLOAT3_H__
20 #ifndef __UTIL_MATH_H__
21 # error "Do not include this file directly, include util_types.h instead."
26 /*******************************************************************************
30 #ifndef __KERNEL_OPENCL__
31 ccl_device_inline float3 operator-(const float3& a);
32 ccl_device_inline float3 operator*(const float3& a, const float3& b);
33 ccl_device_inline float3 operator*(const float3& a, const float f);
34 ccl_device_inline float3 operator*(const float f, const float3& a);
35 ccl_device_inline float3 operator/(const float f, const float3& a);
36 ccl_device_inline float3 operator/(const float3& a, const float f);
37 ccl_device_inline float3 operator/(const float3& a, const float3& b);
38 ccl_device_inline float3 operator+(const float3& a, const float3& b);
39 ccl_device_inline float3 operator-(const float3& a, const float3& b);
40 ccl_device_inline float3 operator+=(float3& a, const float3& b);
41 ccl_device_inline float3 operator-=(float3& a, const float3& b);
42 ccl_device_inline float3 operator*=(float3& a, const float3& b);
43 ccl_device_inline float3 operator*=(float3& a, float f);
44 ccl_device_inline float3 operator/=(float3& a, const float3& b);
45 ccl_device_inline float3 operator/=(float3& a, float f);
47 ccl_device_inline bool operator==(const float3& a, const float3& b);
48 ccl_device_inline bool operator!=(const float3& a, const float3& b);
50 ccl_device_inline float dot(const float3& a, const float3& b);
51 ccl_device_inline float dot_xy(const float3& a, const float3& b);
52 ccl_device_inline float3 cross(const float3& a, const float3& b);
53 ccl_device_inline float3 normalize(const float3& a);
54 ccl_device_inline float3 min(const float3& a, const float3& b);
55 ccl_device_inline float3 max(const float3& a, const float3& b);
56 ccl_device_inline float3 clamp(const float3& a, const float3& mn, const float3& mx);
57 ccl_device_inline float3 fabs(const float3& a);
58 ccl_device_inline float3 mix(const float3& a, const float3& b, float t);
59 ccl_device_inline float3 rcp(const float3& a);
60 #endif /* !__KERNEL_OPENCL__ */
62 ccl_device_inline float max3(float3 a);
63 ccl_device_inline float len(const float3 a);
64 ccl_device_inline float len_squared(const float3 a);
66 ccl_device_inline float3 saturate3(float3 a);
67 ccl_device_inline float3 safe_normalize(const float3 a);
68 ccl_device_inline float3 normalize_len(const float3 a, float *t);;
69 ccl_device_inline float3 safe_normalize_len(const float3 a, float *t);
70 ccl_device_inline float3 interp(float3 a, float3 b, float t);
72 ccl_device_inline bool is_zero(const float3 a);
73 ccl_device_inline float reduce_add(const float3 a);
74 ccl_device_inline float average(const float3 a);
75 ccl_device_inline bool isequal_float3(const float3 a, const float3 b);
77 /*******************************************************************************
81 #ifndef __KERNEL_OPENCL__
82 ccl_device_inline float3 operator-(const float3& a)
85 return float3(_mm_xor_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x80000000))));
87 return make_float3(-a.x, -a.y, -a.z);
91 ccl_device_inline float3 operator*(const float3& a, const float3& b)
94 return float3(_mm_mul_ps(a.m128,b.m128));
96 return make_float3(a.x*b.x, a.y*b.y, a.z*b.z);
100 ccl_device_inline float3 operator*(const float3& a, const float f)
102 #ifdef __KERNEL_SSE__
103 return float3(_mm_mul_ps(a.m128,_mm_set1_ps(f)));
105 return make_float3(a.x*f, a.y*f, a.z*f);
109 ccl_device_inline float3 operator*(const float f, const float3& a)
111 /* TODO(sergey): Currently disabled, gives speedup but causes precision issues. */
112 #if defined(__KERNEL_SSE__) && 0
113 return float3(_mm_mul_ps(_mm_set1_ps(f), a.m128));
115 return make_float3(a.x*f, a.y*f, a.z*f);
119 ccl_device_inline float3 operator/(const float f, const float3& a)
121 /* TODO(sergey): Currently disabled, gives speedup but causes precision issues. */
122 #if defined(__KERNEL_SSE__) && 0
123 __m128 rc = _mm_rcp_ps(a.m128);
124 return float3(_mm_mul_ps(_mm_set1_ps(f),rc));
126 return make_float3(f / a.x, f / a.y, f / a.z);
130 ccl_device_inline float3 operator/(const float3& a, const float f)
136 ccl_device_inline float3 operator/(const float3& a, const float3& b)
138 /* TODO(sergey): Currently disabled, gives speedup but causes precision issues. */
139 #if defined(__KERNEL_SSE__) && 0
140 __m128 rc = _mm_rcp_ps(b.m128);
141 return float3(_mm_mul_ps(a, rc));
143 return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
147 ccl_device_inline float3 operator+(const float3& a, const float3& b)
149 #ifdef __KERNEL_SSE__
150 return float3(_mm_add_ps(a.m128, b.m128));
152 return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
156 ccl_device_inline float3 operator-(const float3& a, const float3& b)
158 #ifdef __KERNEL_SSE__
159 return float3(_mm_sub_ps(a.m128, b.m128));
161 return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
165 ccl_device_inline float3 operator+=(float3& a, const float3& b)
170 ccl_device_inline float3 operator-=(float3& a, const float3& b)
175 ccl_device_inline float3 operator*=(float3& a, const float3& b)
180 ccl_device_inline float3 operator*=(float3& a, float f)
185 ccl_device_inline float3 operator/=(float3& a, const float3& b)
190 ccl_device_inline float3 operator/=(float3& a, float f)
196 ccl_device_inline bool operator==(const float3& a, const float3& b)
198 #ifdef __KERNEL_SSE__
199 return (_mm_movemask_ps(_mm_cmpeq_ps(a.m128, b.m128)) & 7) == 7;
201 return (a.x == b.x && a.y == b.y && a.z == b.z);
205 ccl_device_inline bool operator!=(const float3& a, const float3& b)
210 ccl_device_inline float dot(const float3& a, const float3& b)
212 #if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
213 return _mm_cvtss_f32(_mm_dp_ps(a, b, 0x7F));
215 return a.x*b.x + a.y*b.y + a.z*b.z;
219 ccl_device_inline float dot_xy(const float3& a, const float3& b)
221 #if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
222 return _mm_cvtss_f32(_mm_hadd_ps(_mm_mul_ps(a,b),b));
224 return a.x*b.x + a.y*b.y;
228 ccl_device_inline float3 cross(const float3& a, const float3& b)
230 float3 r = make_float3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
234 ccl_device_inline float3 normalize(const float3& a)
236 #if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
237 __m128 norm = _mm_sqrt_ps(_mm_dp_ps(a.m128, a.m128, 0x7F));
238 return float3(_mm_div_ps(a.m128, norm));
244 ccl_device_inline float3 min(const float3& a, const float3& b)
246 #ifdef __KERNEL_SSE__
247 return float3(_mm_min_ps(a.m128, b.m128));
249 return make_float3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
253 ccl_device_inline float3 max(const float3& a, const float3& b)
255 #ifdef __KERNEL_SSE__
256 return float3(_mm_max_ps(a.m128, b.m128));
258 return make_float3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
262 ccl_device_inline float3 clamp(const float3& a, const float3& mn, const float3& mx)
264 return min(max(a, mn), mx);
267 ccl_device_inline float3 fabs(const float3& a)
269 #ifdef __KERNEL_SSE__
270 __m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff));
271 return float3(_mm_and_ps(a.m128, mask));
273 return make_float3(fabsf(a.x), fabsf(a.y), fabsf(a.z));
277 ccl_device_inline float3 mix(const float3& a, const float3& b, float t)
279 return a + t*(b - a);
282 ccl_device_inline float3 rcp(const float3& a)
284 #ifdef __KERNEL_SSE__
285 const float4 r(_mm_rcp_ps(a.m128));
286 return float3(_mm_sub_ps(_mm_add_ps(r, r),
287 _mm_mul_ps(_mm_mul_ps(r, r), a)));
289 return make_float3(1.0f/a.x, 1.0f/a.y, 1.0f/a.z);
292 #endif /* !__KERNEL_OPENCL__ */
294 ccl_device_inline float max3(float3 a)
296 return max(max(a.x, a.y), a.z);
299 ccl_device_inline float len(const float3 a)
301 #if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
302 return _mm_cvtss_f32(_mm_sqrt_ss(_mm_dp_ps(a.m128, a.m128, 0x7F)));
304 return sqrtf(dot(a, a));
308 ccl_device_inline float len_squared(const float3 a)
313 ccl_device_inline float3 saturate3(float3 a)
315 return make_float3(saturate(a.x), saturate(a.y), saturate(a.z));
318 ccl_device_inline float3 normalize_len(const float3 a, float *t)
325 ccl_device_inline float3 safe_normalize(const float3 a)
328 return (t != 0.0f)? a * (1.0f/t) : a;
331 ccl_device_inline float3 safe_normalize_len(const float3 a, float *t)
334 return (*t != 0.0f)? a/(*t): a;
337 ccl_device_inline float3 interp(float3 a, float3 b, float t)
339 return a + t*(b - a);
342 ccl_device_inline bool is_zero(const float3 a)
344 #ifdef __KERNEL_SSE__
345 return a == make_float3(0.0f);
347 return (a.x == 0.0f && a.y == 0.0f && a.z == 0.0f);
351 ccl_device_inline float reduce_add(const float3 a)
353 return (a.x + a.y + a.z);
356 ccl_device_inline float average(const float3 a)
358 return reduce_add(a)*(1.0f/3.0f);
361 ccl_device_inline bool isequal_float3(const float3 a, const float3 b)
363 #ifdef __KERNEL_OPENCL__
370 ccl_device_inline float3 ensure_finite3(float3 v)
372 if(!isfinite_safe(v.x)) v.x = 0.0;
373 if(!isfinite_safe(v.y)) v.y = 0.0;
374 if(!isfinite_safe(v.z)) v.z = 0.0;
380 #endif /* __UTIL_MATH_FLOAT3_H__ */