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 #if defined(__KERNEL_SSE__)
112 return float3(_mm_mul_ps(_mm_set1_ps(f), a.m128));
114 return make_float3(a.x*f, a.y*f, a.z*f);
118 ccl_device_inline float3 operator/(const float f, const float3& a)
120 #if defined(__KERNEL_SSE__)
121 return float3(_mm_div_ps(_mm_set1_ps(f), a.m128));
123 return make_float3(f / a.x, f / a.y, f / a.z);
127 ccl_device_inline float3 operator/(const float3& a, const float f)
133 ccl_device_inline float3 operator/(const float3& a, const float3& b)
135 #if defined(__KERNEL_SSE__)
136 return float3(_mm_div_ps(a.m128, b.m128));
138 return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
142 ccl_device_inline float3 operator+(const float3& a, const float3& b)
144 #ifdef __KERNEL_SSE__
145 return float3(_mm_add_ps(a.m128, b.m128));
147 return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
151 ccl_device_inline float3 operator-(const float3& a, const float3& b)
153 #ifdef __KERNEL_SSE__
154 return float3(_mm_sub_ps(a.m128, b.m128));
156 return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
160 ccl_device_inline float3 operator+=(float3& a, const float3& b)
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, float f)
180 ccl_device_inline float3 operator/=(float3& a, const float3& b)
185 ccl_device_inline float3 operator/=(float3& a, float f)
191 ccl_device_inline bool operator==(const float3& a, const float3& b)
193 #ifdef __KERNEL_SSE__
194 return (_mm_movemask_ps(_mm_cmpeq_ps(a.m128, b.m128)) & 7) == 7;
196 return (a.x == b.x && a.y == b.y && a.z == b.z);
200 ccl_device_inline bool operator!=(const float3& a, const float3& b)
205 ccl_device_inline float dot(const float3& a, const float3& b)
207 #if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
208 return _mm_cvtss_f32(_mm_dp_ps(a, b, 0x7F));
210 return a.x*b.x + a.y*b.y + a.z*b.z;
214 ccl_device_inline float dot_xy(const float3& a, const float3& b)
216 #if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
217 return _mm_cvtss_f32(_mm_hadd_ps(_mm_mul_ps(a,b),b));
219 return a.x*b.x + a.y*b.y;
223 ccl_device_inline float3 cross(const float3& a, const float3& b)
225 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);
229 ccl_device_inline float3 normalize(const float3& a)
231 #if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
232 __m128 norm = _mm_sqrt_ps(_mm_dp_ps(a.m128, a.m128, 0x7F));
233 return float3(_mm_div_ps(a.m128, norm));
239 ccl_device_inline float3 min(const float3& a, const float3& b)
241 #ifdef __KERNEL_SSE__
242 return float3(_mm_min_ps(a.m128, b.m128));
244 return make_float3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
248 ccl_device_inline float3 max(const float3& a, const float3& b)
250 #ifdef __KERNEL_SSE__
251 return float3(_mm_max_ps(a.m128, b.m128));
253 return make_float3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
257 ccl_device_inline float3 clamp(const float3& a, const float3& mn, const float3& mx)
259 return min(max(a, mn), mx);
262 ccl_device_inline float3 fabs(const float3& a)
264 #ifdef __KERNEL_SSE__
265 __m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff));
266 return float3(_mm_and_ps(a.m128, mask));
268 return make_float3(fabsf(a.x), fabsf(a.y), fabsf(a.z));
272 ccl_device_inline float3 mix(const float3& a, const float3& b, float t)
274 return a + t*(b - a);
277 ccl_device_inline float3 rcp(const float3& a)
279 #ifdef __KERNEL_SSE__
280 /* Don't use _mm_rcp_ps due to poor precision. */
281 return float3(_mm_div_ps(_mm_set_ps1(1.0f), a.m128));
283 return make_float3(1.0f/a.x, 1.0f/a.y, 1.0f/a.z);
286 #endif /* !__KERNEL_OPENCL__ */
288 ccl_device_inline float max3(float3 a)
290 return max(max(a.x, a.y), a.z);
293 ccl_device_inline float len(const float3 a)
295 #if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
296 return _mm_cvtss_f32(_mm_sqrt_ss(_mm_dp_ps(a.m128, a.m128, 0x7F)));
298 return sqrtf(dot(a, a));
302 ccl_device_inline float len_squared(const float3 a)
307 ccl_device_inline float3 saturate3(float3 a)
309 return make_float3(saturate(a.x), saturate(a.y), saturate(a.z));
312 ccl_device_inline float3 normalize_len(const float3 a, float *t)
319 ccl_device_inline float3 safe_normalize(const float3 a)
322 return (t != 0.0f)? a * (1.0f/t) : a;
325 ccl_device_inline float3 safe_normalize_len(const float3 a, float *t)
328 return (*t != 0.0f)? a/(*t): a;
331 ccl_device_inline float3 interp(float3 a, float3 b, float t)
333 return a + t*(b - a);
336 ccl_device_inline bool is_zero(const float3 a)
338 #ifdef __KERNEL_SSE__
339 return a == make_float3(0.0f);
341 return (a.x == 0.0f && a.y == 0.0f && a.z == 0.0f);
345 ccl_device_inline float reduce_add(const float3 a)
347 return (a.x + a.y + a.z);
350 ccl_device_inline float average(const float3 a)
352 return reduce_add(a)*(1.0f/3.0f);
355 ccl_device_inline bool isequal_float3(const float3 a, const float3 b)
357 #ifdef __KERNEL_OPENCL__
364 ccl_device_inline bool isfinite3_safe(float3 v)
366 return isfinite_safe(v.x) && isfinite_safe(v.y) && isfinite_safe(v.z);
369 ccl_device_inline float3 ensure_finite3(float3 v)
371 if(!isfinite_safe(v.x)) v.x = 0.0f;
372 if(!isfinite_safe(v.y)) v.y = 0.0f;
373 if(!isfinite_safe(v.z)) v.z = 0.0f;
379 #endif /* __UTIL_MATH_FLOAT3_H__ */