[Bf-blender-cvs] [13e075600a] master: Cycles: Add utility function to convert float to half

Sergey Sharybin noreply at git.blender.org
Thu Feb 23 15:04:33 CET 2017


Commit: 13e075600ac985bb0d5ff171d7554a4e465db829
Author: Sergey Sharybin
Date:   Thu Feb 23 14:42:06 2017 +0100
Branches: master
https://developer.blender.org/rB13e075600ac985bb0d5ff171d7554a4e465db829

Cycles: Add utility function to convert float to half

handles overflow and underflow, but not NaN/inf.

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

M	intern/cycles/util/util_half.h

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

diff --git a/intern/cycles/util/util_half.h b/intern/cycles/util/util_half.h
index 5db3384cda..7285c6ef60 100644
--- a/intern/cycles/util/util_half.h
+++ b/intern/cycles/util/util_half.h
@@ -110,6 +110,28 @@ ccl_device_inline float4 half4_to_float4(half4 h)
 	return f;
 }
 
+ccl_device_inline half float_to_half(float f)
+{
+	const uint u = __float_as_uint(f);
+	/* Sign bit, shifted to it's position. */
+	uint sign_bit = u & 0x80000000;
+	sign_bit >>= 16;
+	/* Exponent. */
+	uint exponent_bits = u & 0x7f800000;
+	/* Non-sign bits. */
+	uint value_bits = u & 0x7fffffff;
+	value_bits >>= 13;  /* Align mantissa on MSB. */
+	value_bits -= 0x1c000;  /* Adjust bias. */
+	/* Flush-to-zero. */
+	value_bits = (exponent_bits < 0x38800000) ? 0 : value_bits;
+	/* Clamp-to-max. */
+	value_bits = (exponent_bits > 0x47000000) ? 0x7bff : value_bits;
+	/* Denormals-as-zero. */
+	value_bits = (exponent_bits == 0 ? 0 : value_bits);
+	/* Re-insert sign bit and return. */
+	return (value_bits | sign_bit);
+}
+
 #endif
 
 #endif




More information about the Bf-blender-cvs mailing list