[Bf-blender-cvs] [d9e52ac] master: Code cleanup: move half float functions to separate header file.

Brecht Van Lommel noreply at git.blender.org
Wed Jan 15 15:29:59 CET 2014


Commit: d9e52ac98b74e2be3186f448177c84772f928871
Author: Brecht Van Lommel
Date:   Wed Jan 15 15:29:22 2014 +0100
https://developer.blender.org/rBd9e52ac98b74e2be3186f448177c84772f928871

Code cleanup: move half float functions to separate header file.

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

M	intern/cycles/device/device.cpp
M	intern/cycles/device/device_memory.h
M	intern/cycles/kernel/CMakeLists.txt
M	intern/cycles/kernel/kernel_compat_cpu.h
M	intern/cycles/kernel/kernel_compat_cuda.h
M	intern/cycles/kernel/kernel_compat_opencl.h
M	intern/cycles/render/buffers.h
M	intern/cycles/util/CMakeLists.txt
A	intern/cycles/util/util_half.h
M	intern/cycles/util/util_types.h

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

diff --git a/intern/cycles/device/device.cpp b/intern/cycles/device/device.cpp
index 6283e34..a3264f0 100644
--- a/intern/cycles/device/device.cpp
+++ b/intern/cycles/device/device.cpp
@@ -23,6 +23,7 @@
 #include "util_cuda.h"
 #include "util_debug.h"
 #include "util_foreach.h"
+#include "util_half.h"
 #include "util_math.h"
 #include "util_opencl.h"
 #include "util_opengl.h"
diff --git a/intern/cycles/device/device_memory.h b/intern/cycles/device/device_memory.h
index 352a46f..1427d12 100644
--- a/intern/cycles/device/device_memory.h
+++ b/intern/cycles/device/device_memory.h
@@ -29,6 +29,7 @@
  * to and from. */
 
 #include "util_debug.h"
+#include "util_half.h"
 #include "util_types.h"
 #include "util_vector.h"
 
diff --git a/intern/cycles/kernel/CMakeLists.txt b/intern/cycles/kernel/CMakeLists.txt
index 780aa41..08427b1 100644
--- a/intern/cycles/kernel/CMakeLists.txt
+++ b/intern/cycles/kernel/CMakeLists.txt
@@ -119,6 +119,7 @@ set(SRC_SVM_HEADERS
 
 set(SRC_UTIL_HEADERS
 	../util/util_color.h
+	../util/util_half.h
 	../util/util_math.h
 	../util/util_transform.h
 	../util/util_types.h
diff --git a/intern/cycles/kernel/kernel_compat_cpu.h b/intern/cycles/kernel/kernel_compat_cpu.h
index 2b786bc..ce597a8 100644
--- a/intern/cycles/kernel/kernel_compat_cpu.h
+++ b/intern/cycles/kernel/kernel_compat_cpu.h
@@ -20,6 +20,7 @@
 #define __KERNEL_CPU__
 
 #include "util_debug.h"
+#include "util_half.h"
 #include "util_math.h"
 #include "util_simd.h"
 #include "util_types.h"
diff --git a/intern/cycles/kernel/kernel_compat_cuda.h b/intern/cycles/kernel/kernel_compat_cuda.h
index 76f885a..15e7353 100644
--- a/intern/cycles/kernel/kernel_compat_cuda.h
+++ b/intern/cycles/kernel/kernel_compat_cuda.h
@@ -40,6 +40,7 @@
 
 /* Types */
 
+#include "util_half.h"
 #include "util_types.h"
 
 /* Textures */
diff --git a/intern/cycles/kernel/kernel_compat_opencl.h b/intern/cycles/kernel/kernel_compat_opencl.h
index 1ff3615..4f4414c 100644
--- a/intern/cycles/kernel/kernel_compat_opencl.h
+++ b/intern/cycles/kernel/kernel_compat_opencl.h
@@ -113,6 +113,7 @@
 /* define NULL */
 #define NULL 0
 
+#include "util_half.h"
 #include "util_types.h"
 
 #endif /* __KERNEL_COMPAT_OPENCL_H__ */
diff --git a/intern/cycles/render/buffers.h b/intern/cycles/render/buffers.h
index 0b1f901..81eaf41 100644
--- a/intern/cycles/render/buffers.h
+++ b/intern/cycles/render/buffers.h
@@ -23,6 +23,7 @@
 
 #include "kernel_types.h"
 
+#include "util_half.h"
 #include "util_string.h"
 #include "util_thread.h"
 #include "util_types.h"
diff --git a/intern/cycles/util/CMakeLists.txt b/intern/cycles/util/CMakeLists.txt
index df188f8..c1150d2 100644
--- a/intern/cycles/util/CMakeLists.txt
+++ b/intern/cycles/util/CMakeLists.txt
@@ -38,6 +38,7 @@ set(SRC_HEADERS
 	util_dynlib.h
 	util_foreach.h
 	util_function.h
+	util_half.h
 	util_hash.h
 	util_image.h
 	util_list.h
diff --git a/intern/cycles/util/util_half.h b/intern/cycles/util/util_half.h
new file mode 100644
index 0000000..2119202
--- /dev/null
+++ b/intern/cycles/util/util_half.h
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2011-2013 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#ifndef __UTIL_HALF_H__
+#define __UTIL_HALF_H__
+
+#include "util_types.h"
+
+CCL_NAMESPACE_BEGIN
+
+/* Half Floats */
+
+#ifdef __KERNEL_OPENCL__
+
+#define float4_store_half(h, f, scale) vstore_half4(*(f) * (scale), 0, h);
+
+#else
+
+typedef unsigned short half;
+struct half4 { half x, y, z, w; };
+
+#ifdef __KERNEL_CUDA__
+
+ccl_device_inline void float4_store_half(half *h, const float4 *f, float scale)
+{
+	h[0] = __float2half_rn(f->x * scale);
+	h[1] = __float2half_rn(f->y * scale);
+	h[2] = __float2half_rn(f->z * scale);
+	h[3] = __float2half_rn(f->w * scale);
+}
+
+#else
+
+ccl_device_inline void float4_store_half(half *h, const float4 *f, float scale)
+{
+#ifndef __KERNEL_SSE2__
+	for(int i = 0; i < 4; i++) {
+		/* optimized float to half for pixels:
+		 * assumes no negative, no nan, no inf, and sets denormal to 0 */
+		union { uint i; float f; } in;
+		float fscale = (*f)[i] * scale;
+		in.f = (fscale > 0.0f)? ((fscale < 65500.0f)? fscale: 65500.0f): 0.0f;
+		int x = in.i;
+
+		int absolute = x & 0x7FFFFFFF;
+		int Z = absolute + 0xC8000000;
+		int result = (absolute < 0x38800000)? 0: Z;
+		int rshift = (result >> 13);
+
+		h[i] = (rshift & 0x7FFF);
+	}
+#else
+	/* same as above with SSE */
+	const __m128 mm_scale = _mm_set_ps1(scale);
+	const __m128i mm_38800000 = _mm_set1_epi32(0x38800000);
+	const __m128i mm_7FFF = _mm_set1_epi32(0x7FFF);
+	const __m128i mm_7FFFFFFF = _mm_set1_epi32(0x7FFFFFFF);
+	const __m128i mm_C8000000 = _mm_set1_epi32(0xC8000000);
+
+	__m128 mm_fscale = _mm_mul_ps(*(__m128*)f, mm_scale);
+	__m128i x = _mm_castps_si128(_mm_min_ps(_mm_max_ps(mm_fscale, _mm_set_ps1(0.0f)), _mm_set_ps1(65500.0f)));
+	__m128i absolute = _mm_and_si128(x, mm_7FFFFFFF);
+	__m128i Z = _mm_add_epi32(absolute, mm_C8000000);
+	__m128i result = _mm_andnot_si128(_mm_cmplt_epi32(absolute, mm_38800000), Z); 
+	__m128i rh = _mm_and_si128(_mm_srai_epi32(result, 13), mm_7FFF);
+
+	_mm_storel_pi((__m64*)h, _mm_castsi128_ps(_mm_packs_epi32(rh, rh)));
+#endif
+}
+
+#endif
+
+#endif
+
+CCL_NAMESPACE_END
+
+#endif /* __UTIL_HALF_H__ */
+
diff --git a/intern/cycles/util/util_types.h b/intern/cycles/util/util_types.h
index ebfd8b6..4503848 100644
--- a/intern/cycles/util/util_types.h
+++ b/intern/cycles/util/util_types.h
@@ -440,70 +440,6 @@ ccl_device_inline int4 make_int4(const float3& f)
 
 #endif
 
-/* Half Floats */
-
-#ifdef __KERNEL_OPENCL__
-
-#define float4_store_half(h, f, scale) vstore_half4(*(f) * (scale), 0, h);
-
-#else
-
-typedef unsigned short half;
-struct half4 { half x, y, z, w; };
-
-#ifdef __KERNEL_CUDA__
-
-ccl_device_inline void float4_store_half(half *h, const float4 *f, float scale)
-{
-	h[0] = __float2half_rn(f->x * scale);
-	h[1] = __float2half_rn(f->y * scale);
-	h[2] = __float2half_rn(f->z * scale);
-	h[3] = __float2half_rn(f->w * scale);
-}
-
-#else
-
-ccl_device_inline void float4_store_half(half *h, const float4 *f, float scale)
-{
-#ifndef __KERNEL_SSE2__
-	for(int i = 0; i < 4; i++) {
-		/* optimized float to half for pixels:
-		 * assumes no negative, no nan, no inf, and sets denormal to 0 */
-		union { uint i; float f; } in;
-		float fscale = (*f)[i] * scale;
-		in.f = (fscale > 0.0f)? ((fscale < 65500.0f)? fscale: 65500.0f): 0.0f;
-		int x = in.i;
-
-		int absolute = x & 0x7FFFFFFF;
-		int Z = absolute + 0xC8000000;
-		int result = (absolute < 0x38800000)? 0: Z;
-		int rshift = (result >> 13);
-
-		h[i] = (rshift & 0x7FFF);
-	}
-#else
-	/* same as above with SSE */
-	const __m128 mm_scale = _mm_set_ps1(scale);
-	const __m128i mm_38800000 = _mm_set1_epi32(0x38800000);
-	const __m128i mm_7FFF = _mm_set1_epi32(0x7FFF);
-	const __m128i mm_7FFFFFFF = _mm_set1_epi32(0x7FFFFFFF);
-	const __m128i mm_C8000000 = _mm_set1_epi32(0xC8000000);
-
-	__m128 mm_fscale = _mm_mul_ps(*(__m128*)f, mm_scale);
-	__m128i x = _mm_castps_si128(_mm_min_ps(_mm_max_ps(mm_fscale, _mm_set_ps1(0.0f)), _mm_set_ps1(65500.0f)));
-	__m128i absolute = _mm_and_si128(x, mm_7FFFFFFF);
-	__m128i Z = _mm_add_epi32(absolute, mm_C8000000);
-	__m128i result = _mm_andnot_si128(_mm_cmplt_epi32(absolute, mm_38800000), Z); 
-	__m128i rh = _mm_and_si128(_mm_srai_epi32(result, 13), mm_7FFF);
-
-	_mm_storel_pi((__m64*)h, _mm_castsi128_ps(_mm_packs_epi32(rh, rh)));
-#endif
-}
-
-#endif
-
-#endif
-
 CCL_NAMESPACE_END
 
 #endif /* __UTIL_TYPES_H__ */




More information about the Bf-blender-cvs mailing list