Commit: a24fbf3323101cd35332161b12a04e687b5583e4
Author: Brecht Van Lommel
Date:   Wed Aug 2 02:23:03 2017 +0200
Branches: master
https://developer.blender.org/rBa24fbf3323101cd35332161b12a04e687b5583e4

Code refactor: add, remove, optimize various SSE functions.

* Remove some unnecessary SSE emulation defines.
* Use full precision float division so we can enable it.
* Add sqrt(), sqr(), fabs(), shuffle variations, mask().
* Optimize reduce_add(), select().

Differential Revision: https://developer.blender.org/D2764

===================================================================

M       intern/cycles/kernel/filter/filter_features_sse.h
M       intern/cycles/kernel/geom/geom_object.h
M       intern/cycles/util/util_math.h
M       intern/cycles/util/util_math_float3.h
M       intern/cycles/util/util_math_float4.h
M       intern/cycles/util/util_simd.h

===================================================================

diff --git a/intern/cycles/kernel/filter/filter_features_sse.h 
b/intern/cycles/kernel/filter/filter_features_sse.h
index 3185330994c..27e220923a0 100644
--- a/intern/cycles/kernel/filter/filter_features_sse.h
+++ b/intern/cycles/kernel/filter/filter_features_sse.h
@@ -92,13 +92,13 @@ ccl_device_inline void filter_get_feature_scales_sse(__m128 
x, __m128 y,
 
 ccl_device_inline void filter_calculate_scale_sse(__m128 *scale)
 {
-       scale[0] = _mm_rcp_ps(_mm_max_ps(_mm_hmax_ps(scale[0]), 
_mm_set1_ps(0.01f)));
-       scale[1] = _mm_rcp_ps(_mm_max_ps(_mm_hmax_ps(scale[1]), 
_mm_set1_ps(0.01f)));
-       scale[2] = _mm_rcp_ps(_mm_max_ps(_mm_hmax_ps(scale[2]), 
_mm_set1_ps(0.01f)));
-       scale[6] = _mm_rcp_ps(_mm_max_ps(_mm_hmax_ps(scale[4]), 
_mm_set1_ps(0.01f)));
+       scale[0] = _mm_div_ps(_mm_set1_ps(1.0f), 
_mm_max_ps(_mm_hmax_ps(scale[0]), _mm_set1_ps(0.01f)));
+       scale[1] = _mm_div_ps(_mm_set1_ps(1.0f), 
_mm_max_ps(_mm_hmax_ps(scale[1]), _mm_set1_ps(0.01f)));
+       scale[2] = _mm_div_ps(_mm_set1_ps(1.0f), 
_mm_max_ps(_mm_hmax_ps(scale[2]), _mm_set1_ps(0.01f)));
+       scale[6] = _mm_div_ps(_mm_set1_ps(1.0f), 
_mm_max_ps(_mm_hmax_ps(scale[4]), _mm_set1_ps(0.01f)));
 
-       scale[7] = scale[8] = scale[9] = 
_mm_rcp_ps(_mm_max_ps(_mm_hmax_ps(_mm_sqrt_ps(scale[5])), _mm_set1_ps(0.01f)));
-       scale[3] = scale[4] = scale[5] = 
_mm_rcp_ps(_mm_max_ps(_mm_hmax_ps(_mm_sqrt_ps(scale[3])), _mm_set1_ps(0.01f)));
+       scale[7] = scale[8] = scale[9] = _mm_div_ps(_mm_set1_ps(1.0f), 
_mm_max_ps(_mm_hmax_ps(_mm_sqrt_ps(scale[5])), _mm_set1_ps(0.01f)));
+       scale[3] = scale[4] = scale[5] = _mm_div_ps(_mm_set1_ps(1.0f), 
_mm_max_ps(_mm_hmax_ps(_mm_sqrt_ps(scale[3])), _mm_set1_ps(0.01f)));
 }
 
 
diff --git a/intern/cycles/kernel/geom/geom_object.h 
b/intern/cycles/kernel/geom/geom_object.h
index 6ecdfe0173a..1ffc143be34 100644
--- a/intern/cycles/kernel/geom/geom_object.h
+++ b/intern/cycles/kernel/geom/geom_object.h
@@ -415,12 +415,7 @@ ccl_device_inline float3 bvh_clamp_direction(float3 dir)
 
 ccl_device_inline float3 bvh_inverse_direction(float3 dir)
 {
-       /* TODO(sergey): Currently disabled, gives speedup but causes precision 
issues. */
-#if defined(__KERNEL_SSE__) && 0
        return rcp(dir);
-#else
-       return 1.0f / dir;
-#endif
 }
 
 /* Transform ray into object space to enter static object in BVH */
diff --git a/intern/cycles/util/util_math.h b/intern/cycles/util/util_math.h
index b719640b19c..4d51ec5570a 100644
--- a/intern/cycles/util/util_math.h
+++ b/intern/cycles/util/util_math.h
@@ -94,6 +94,7 @@ ccl_device_inline float fminf(float a, float b)
 #ifndef __KERNEL_GPU__
 using std::isfinite;
 using std::isnan;
+using std::sqrt;
 
 ccl_device_inline int abs(int x)
 {
diff --git a/intern/cycles/util/util_math_float3.h 
b/intern/cycles/util/util_math_float3.h
index bb04c4aa2d9..e73e5bc17a2 100644
--- a/intern/cycles/util/util_math_float3.h
+++ b/intern/cycles/util/util_math_float3.h
@@ -108,8 +108,7 @@ ccl_device_inline float3 operator*(const float3& a, const 
float f)
 
 ccl_device_inline float3 operator*(const float f, const float3& a)
 {
-       /* TODO(sergey): Currently disabled, gives speedup but causes precision 
issues. */
-#if defined(__KERNEL_SSE__) && 0
+#if defined(__KERNEL_SSE__)
        return float3(_mm_mul_ps(_mm_set1_ps(f), a.m128));
 #else
        return make_float3(a.x*f, a.y*f, a.z*f);
@@ -118,10 +117,8 @@ ccl_device_inline float3 operator*(const float f, const 
float3& a)
 
 ccl_device_inline float3 operator/(const float f, const float3& a)
 {
-       /* TODO(sergey): Currently disabled, gives speedup but causes precision 
issues. */
-#if defined(__KERNEL_SSE__) && 0
-       __m128 rc = _mm_rcp_ps(a.m128);
-       return float3(_mm_mul_ps(_mm_set1_ps(f),rc));
+#if defined(__KERNEL_SSE__)
+       return float3(_mm_div_ps(_mm_set1_ps(f), a.m128));
 #else
        return make_float3(f / a.x, f / a.y, f / a.z);
 #endif
@@ -135,10 +132,8 @@ ccl_device_inline float3 operator/(const float3& a, const 
float f)
 
 ccl_device_inline float3 operator/(const float3& a, const float3& b)
 {
-       /* TODO(sergey): Currently disabled, gives speedup but causes precision 
issues. */
-#if defined(__KERNEL_SSE__) && 0
-       __m128 rc = _mm_rcp_ps(b.m128);
-       return float3(_mm_mul_ps(a, rc));
+#if defined(__KERNEL_SSE__)
+       return float3(_mm_div_ps(a.m128, b.m128));
 #else
        return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
 #endif
@@ -282,9 +277,8 @@ ccl_device_inline float3 mix(const float3& a, const float3& 
b, float t)
 ccl_device_inline float3 rcp(const float3& a)
 {
 #ifdef __KERNEL_SSE__
-       const float4 r(_mm_rcp_ps(a.m128));
-       return float3(_mm_sub_ps(_mm_add_ps(r, r),
-                                _mm_mul_ps(_mm_mul_ps(r, r), a)));
+       /* Don't use _mm_rcp_ps due to poor precision. */
+       return float3(_mm_div_ps(_mm_set_ps1(1.0f), a.m128));
 #else
        return make_float3(1.0f/a.x, 1.0f/a.y, 1.0f/a.z);
 #endif
diff --git a/intern/cycles/util/util_math_float4.h 
b/intern/cycles/util/util_math_float4.h
index d89121b3a1d..007b3fc5082 100644
--- a/intern/cycles/util/util_math_float4.h
+++ b/intern/cycles/util/util_math_float4.h
@@ -48,6 +48,8 @@ ccl_device_inline bool operator==(const float4& a, const 
float4& b);
 ccl_device_inline float dot(const float4& a, const float4& b);
 ccl_device_inline float len_squared(const float4& a);
 ccl_device_inline float4 rcp(const float4& a);
+ccl_device_inline float4 sqrt(const float4& a);
+ccl_device_inline float4 sqr(const float4& a);
 ccl_device_inline float4 cross(const float4& a, const float4& b);
 ccl_device_inline bool is_zero(const float4& a);
 ccl_device_inline float reduce_add(const float4& a);
@@ -57,14 +59,20 @@ ccl_device_inline float4 normalize(const float4& a);
 ccl_device_inline float4 safe_normalize(const float4& a);
 ccl_device_inline float4 min(const float4& a, const float4& b);
 ccl_device_inline float4 max(const float4& a, const float4& b);
+ccl_device_inline float4 fabs(const float4& a);
 #endif  /* !__KERNEL_OPENCL__*/
 
 #ifdef __KERNEL_SSE__
 template<size_t index_0, size_t index_1, size_t index_2, size_t index_3>
 __forceinline const float4 shuffle(const float4& b);
+template<size_t index_0, size_t index_1, size_t index_2, size_t index_3>
+__forceinline const float4 shuffle(const float4& a, const float4& b);
 
 template<> __forceinline const float4 shuffle<0, 1, 0, 1>(const float4& b);
 
+template<> __forceinline const float4 shuffle<0, 1, 0, 1>(const float4& a, 
const float4& b);
+template<> __forceinline const float4 shuffle<2, 3, 2, 3>(const float4& a, 
const float4& b);
+
 #  ifdef __KERNEL_SSE3__
 template<> __forceinline const float4 shuffle<0, 0, 2, 2>(const float4& b);
 template<> __forceinline const float4 shuffle<1, 1, 3, 3>(const float4& b);
@@ -77,9 +85,6 @@ ccl_device_inline float4 select(const int4& mask,
                                 const float4& b);
 ccl_device_inline float4 reduce_min(const float4& a);
 ccl_device_inline float4 reduce_max(const float4& a);
-#  if 0
-ccl_device_inline float4 reduce_add(const float4& a);
-#  endif
 #endif  /* !__KERNEL_GPU__ */
 
 
/*******************************************************************************
@@ -128,7 +133,7 @@ ccl_device_inline float4 operator/(const float4& a, float f)
 ccl_device_inline float4 operator/(const float4& a, const float4& b)
 {
 #ifdef __KERNEL_SSE__
-       return a * rcp(b);
+       return float4(_mm_div_ps(a.m128, b.m128));
 #else
        return make_float4(a.x/b.x, a.y/b.y, a.z/b.z, a.w/b.w);
 #endif
@@ -224,14 +229,30 @@ ccl_device_inline float len_squared(const float4& a)
 ccl_device_inline float4 rcp(const float4& a)
 {
 #ifdef __KERNEL_SSE__
-       float4 r(_mm_rcp_ps(a.m128));
-       return float4(_mm_sub_ps(_mm_add_ps(r, r),
-                                _mm_mul_ps(_mm_mul_ps(r, r), a)));
+       /* Don't use _mm_rcp_ps due to poor precision. */
+       return float4(_mm_div_ps(_mm_set_ps1(1.0f), a.m128));
 #else
        return make_float4(1.0f/a.x, 1.0f/a.y, 1.0f/a.z, 1.0f/a.w);
 #endif
 }
 
+ccl_device_inline float4 sqrt(const float4& a)
+{
+#ifdef __KERNEL_SSE__
+       return float4(_mm_sqrt_ps(a.m128));
+#else
+       return make_float4(sqrtf(a.x),
+                          sqrtf(a.y),
+                          sqrtf(a.z),
+                          sqrtf(a.w));
+#endif
+}
+
+ccl_device_inline float4 sqr(const float4& a)
+{
+       return a * a;
+}
+
 ccl_device_inline float4 cross(const float4& a, const float4& b)
 {
 #ifdef __KERNEL_SSE__
@@ -257,9 +278,13 @@ ccl_device_inline bool is_zero(const float4& a)
 ccl_device_inline float reduce_add(const float4& a)
 {
 #ifdef __KERNEL_SSE__
+#  ifdef __KERNEL_SSE3__
+    float4 h(_mm_hadd_ps(a.m128, a.m128));
+    return  _mm_cvtss_f32(_mm_hadd_ps(h.m128, h.m128));
+#  else
        float4 h(shuffle<1,0,3,2>(a) + a);
-       /* TODO(sergey): Investigate efficiency. */
-       return _mm_cvtss_f32(shuffle<2,3,0,1>(h) + h);
+       return  _mm_cvtss_f32(shuffle<2,3,0,1>(h) + h);
+#  endif
 #else
        return ((a.x + a.y) + (a.z + a.w));
 #endif
@@ -309,6 +334,18 @@ ccl_device_inline float4 max(const float4& a, const 
float4& b)
                           max(a.w, b.w));
 #endif
 }
+
+ccl_device_inline float4 fabs(const float4& a)
+{
+#ifdef __KERNEL_SSE__
+       return float4(_mm_and_ps(a.m128, 
_mm_castsi128_ps(_mm_set1_epi32(0x7fffffff))));
+#else
+       return make_float4(fabsf(a.x),
+                          fabsf(a.y),
+                          fabsf(a.z),
+                          fabsf(a.w));
+#endif
+}
 #endif  /* !__KERNEL_OPENCL__*/
 
 #ifdef __KERNEL_SSE__
@@ -320,11 +357,28 @@ __forceinline const float4 shuffle(const float4& b)
                                  _MM_SHUFFLE(index_3, index_2, index_1, 
index_0))));
 }
 
+template<size_t index_0, size_t index_1, size_t index_2, size_t index_3>
+__forceinline const float4 shuffle(const float4& a, const float4& b)
+{
+       return float4(_mm_shuffle_ps(a.m128, b.m128,
+                                    _MM_SHUFFLE(index_3, index_2, index_1, 
index_0)));
+}
+
 template<> __forceinline const float4 shuffle<0, 1, 0, 1>(const float4& b)
 {
        return float4(_mm_castpd_ps(_mm_movedup_pd(_mm_castps_pd(b))));
 }
 
+template<> __forceinline const float4 shuffle<0, 1, 0, 1>(const float4& a, 
const float4& b)
+{
+       return float4(_mm_movelh_ps(a.m128, b.m128));
+}
+
+template<> __forceinline const float4 shuffle<2, 3, 2, 3>(const float4& a, 
const float4& b)
+{
+       return float4(_mm_movehl_ps(b.m128, a.m128));
+}
+
 #  ifdef __KERNEL_SSE3__
 template<> __forceinline const float4 shuffle<0, 0, 2, 2>(const

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to