diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2017-02-23 16:42:06 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2017-02-23 16:42:06 +0300 |
commit | 13e075600ac985bb0d5ff171d7554a4e465db829 (patch) | |
tree | fbab37d40f3fadf8eb144238f1aa42928e2580bb /intern/cycles/util | |
parent | 9eb647f1c8d8d7ea7da8f7dff7c19a2008afe26c (diff) |
Cycles: Add utility function to convert float to half
handles overflow and underflow, but not NaN/inf.
Diffstat (limited to 'intern/cycles/util')
-rw-r--r-- | intern/cycles/util/util_half.h | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/intern/cycles/util/util_half.h b/intern/cycles/util/util_half.h index 5db3384cda4..7285c6ef600 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 |