Add support to NaN in Float32ToFloat16

This will be used to test NaN is correctly sampled from float16
textures.

BUG=dawn:128

Change-Id: I6e3b79f438e9a48c3a167ab45baf9f9d019ce48b
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/8361
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
This commit is contained in:
Corentin Wallez 2019-06-26 20:26:13 +00:00 committed by Commit Bot service account
parent 7ac0815347
commit 77aa5b59aa
2 changed files with 21 additions and 2 deletions

View File

@ -85,8 +85,10 @@ uint16_t Float32ToFloat16(float fp32) {
uint32_t sign16 = (fp32i & 0x80000000) >> 16; uint32_t sign16 = (fp32i & 0x80000000) >> 16;
uint32_t mantissaAndExponent = fp32i & 0x7FFFFFFF; uint32_t mantissaAndExponent = fp32i & 0x7FFFFFFF;
if (mantissaAndExponent > 0x47FFEFFF) { // Infinity if (mantissaAndExponent > 0x7F800000) { // NaN
return static_cast<uint16_t>(sign16 | 0x7FFF); return 0x7FFF;
} else if (mantissaAndExponent > 0x47FFEFFF) { // Infinity
return static_cast<uint16_t>(sign16 | 0x7C00);
} else if (mantissaAndExponent < 0x38800000) { // Denormal } else if (mantissaAndExponent < 0x38800000) { // Denormal
uint32_t mantissa = (mantissaAndExponent & 0x007FFFFF) | 0x00800000; uint32_t mantissa = (mantissaAndExponent & 0x007FFFFF) | 0x00800000;
int32_t exponent = 113 - (mantissaAndExponent >> 23); int32_t exponent = 113 - (mantissaAndExponent >> 23);

View File

@ -16,6 +16,8 @@
#include "common/Math.h" #include "common/Math.h"
#include <cmath>
// Tests for ScanForward // Tests for ScanForward
TEST(Math, ScanForward) { TEST(Math, ScanForward) {
// Test extrema // Test extrema
@ -133,3 +135,18 @@ TEST(Math, IsAligned) {
ASSERT_FALSE(IsAligned(64 + i, 64)); ASSERT_FALSE(IsAligned(64 + i, 64));
} }
} }
// Tests for float32 to float16 conversion
TEST(Math, Float32ToFloat16) {
ASSERT_EQ(Float32ToFloat16(0.0f), 0x0000);
ASSERT_EQ(Float32ToFloat16(-0.0f), 0x8000);
ASSERT_EQ(Float32ToFloat16(INFINITY), 0x7C00);
ASSERT_EQ(Float32ToFloat16(-INFINITY), 0xFC00);
// Check that NaN is converted to a value in one of the float16 NaN ranges
uint16_t nan16 = Float32ToFloat16(NAN);
ASSERT_TRUE(nan16 > 0xFC00 || (nan16 < 0x8000 && nan16 > 0x7C00));
ASSERT_EQ(Float32ToFloat16(1.0f), 0x3C00);
}