WIP: Vulkan: Workbench #107886
|
@ -107,6 +107,8 @@ template<bool HasSignBit, uint8_t MantissaBitLen, uint8_t ExponentBitLen>
|
|||
class FloatingPointFormat {
|
||||
public:
|
||||
static constexpr bool HasSign = HasSignBit;
|
||||
static constexpr uint8_t SignShift = MantissaBitLen + ExponentBitLen;
|
||||
static constexpr uint32_t SignMask = HasSignBit ? 1 : 0;
|
||||
static constexpr uint8_t MantissaLen = MantissaBitLen;
|
||||
static constexpr uint8_t MantissaShift = 0;
|
||||
static constexpr uint32_t MantissaMask = (1 << MantissaBitLen) - 1;
|
||||
|
@ -114,10 +116,8 @@ class FloatingPointFormat {
|
|||
static constexpr uint8_t ExponentShift = MantissaBitLen;
|
||||
static constexpr uint8_t ExponentLen = ExponentBitLen;
|
||||
static constexpr uint32_t ExponentMask = (1 << ExponentBitLen) - 1;
|
||||
static constexpr uint32_t ExponentBias = (1 << (ExponentBitLen - 1)) - 1;
|
||||
static constexpr int32_t ExponentBias = (1 << (ExponentBitLen - 1)) - 1;
|
||||
static constexpr int32_t ExponentSpecialMask = ExponentMask;
|
||||
static constexpr uint8_t SignShift = MantissaBitLen + ExponentBitLen;
|
||||
static constexpr uint32_t SignMask = HasSignBit ? 1 : 0;
|
||||
|
||||
static uint32_t get_mantissa(uint32_t floating_point_number)
|
||||
{
|
||||
|
@ -212,6 +212,7 @@ uint32_t convert_float_formats(uint32_t value)
|
|||
|
||||
const bool is_nan = (exponent == SourceFormat::ExponentSpecialMask) && mantissa;
|
||||
const bool is_inf = (exponent == SourceFormat::ExponentSpecialMask) && (mantissa == 0);
|
||||
const bool is_zero = (exponent == 0 && mantissa == 0);
|
||||
|
||||
/* Sign conversion */
|
||||
if constexpr (!DestinationFormat::HasSign && ClampNegativeToZero) {
|
||||
|
@ -219,6 +220,9 @@ uint32_t convert_float_formats(uint32_t value)
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
if (is_zero) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (is_inf) {
|
||||
exponent = DestinationFormat::ExponentSpecialMask;
|
||||
|
@ -236,7 +240,7 @@ uint32_t convert_float_formats(uint32_t value)
|
|||
exponent = 0;
|
||||
mantissa = SourceFormat::MantissaMask;
|
||||
}
|
||||
else if (exponent < -int32_t(DestinationFormat::ExponentBias)) {
|
||||
else if (exponent < -DestinationFormat::ExponentBias) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,22 +3,26 @@
|
|||
#include "vk_data_conversion.hh"
|
||||
|
||||
namespace blender::gpu::tests {
|
||||
static void test_f32_f16(uint32_t f32_in, uint32_t f16_expected)
|
||||
{
|
||||
const uint32_t f16 = convert_float_formats<FormatF16, FormatF32>(f32_in);
|
||||
EXPECT_EQ(f16, f16_expected);
|
||||
const uint32_t f32_reverse = convert_float_formats<FormatF32, FormatF16>(f16);
|
||||
EXPECT_EQ(f32_reverse, f32_in);
|
||||
}
|
||||
|
||||
TEST(VulkanDataConversion, ConvertF32F16)
|
||||
{
|
||||
const uint32_t f32_2 = 0b01000000000000000000000000000000;
|
||||
const uint32_t f16_2_expected = 0b0100000000000000;
|
||||
const uint32_t f16_2 = convert_float_formats<FormatF16, FormatF32>(f32_2);
|
||||
EXPECT_EQ(f16_2, f16_2_expected);
|
||||
|
||||
const uint32_t f32_3 = 0b01000000010000000000000000000000;
|
||||
const uint32_t f16_3_expected = 0b0100001000000000;
|
||||
const uint32_t f16_3 = convert_float_formats<FormatF16, FormatF32>(f32_3);
|
||||
EXPECT_EQ(f16_3, f16_3_expected);
|
||||
|
||||
const uint32_t f32_4 = 0b01000000100000000000000000000000;
|
||||
const uint32_t f16_4_expected = 0b0100010000000000;
|
||||
const uint32_t f16_4 = convert_float_formats<FormatF16, FormatF32>(f32_4);
|
||||
EXPECT_EQ(f16_4, f16_4_expected);
|
||||
/* 0.0 */
|
||||
test_f32_f16(0b00000000000000000000000000000000, 0b0000000000000000);
|
||||
/* 0.125 */
|
||||
test_f32_f16(0b00111110000000000000000000000000, 0b0011000000000000);
|
||||
/* 2.0 */
|
||||
test_f32_f16(0b01000000000000000000000000000000, 0b0100000000000000);
|
||||
/* 3.0 */
|
||||
test_f32_f16(0b01000000010000000000000000000000, 0b0100001000000000);
|
||||
/* 4.0 */
|
||||
test_f32_f16(0b01000000100000000000000000000000, 0b0100010000000000);
|
||||
}
|
||||
|
||||
TEST(VulkanDataConversion, clamp_negative_to_zero)
|
||||
|
|
Loading…
Reference in New Issue