| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Math/Vec4.h> |
| 8 |
#include <Jolt/Core/FPException.h> |
| 9 |
|
| 10 |
JPH_NAMESPACE_BEGIN |
| 11 |
|
| 12 |
using HalfFloat = uint16; |
| 13 |
|
| 14 |
|
| 15 |
static constexpr HalfFloat HALF_FLT_MAX = 0x7bff; |
| 16 |
static constexpr HalfFloat HALF_FLT_MAX_NEGATIVE = 0xfbff; |
| 17 |
static constexpr HalfFloat HALF_FLT_INF = 0x7c00; |
| 18 |
static constexpr HalfFloat HALF_FLT_INF_NEGATIVE = 0xfc00; |
| 19 |
static constexpr HalfFloat HALF_FLT_NANQ = 0x7e00; |
| 20 |
static constexpr HalfFloat HALF_FLT_NANQ_NEGATIVE = 0xfe00; |
| 21 |
|
| 22 |
namespace HalfFloatConversion { |
| 23 |
|
| 24 |
|
| 25 |
static constexpr int FLOAT_SIGN_POS = 31; |
| 26 |
static constexpr int FLOAT_EXPONENT_POS = 23; |
| 27 |
static constexpr int FLOAT_EXPONENT_BITS = 8; |
| 28 |
static constexpr int FLOAT_EXPONENT_MASK = (1 << FLOAT_EXPONENT_BITS) - 1; |
| 29 |
static constexpr int FLOAT_EXPONENT_BIAS = 127; |
| 30 |
static constexpr int FLOAT_MANTISSA_BITS = 23; |
| 31 |
static constexpr int FLOAT_MANTISSA_MASK = (1 << FLOAT_MANTISSA_BITS) - 1; |
| 32 |
static constexpr int FLOAT_EXPONENT_AND_MANTISSA_MASK = FLOAT_MANTISSA_MASK + (FLOAT_EXPONENT_MASK << FLOAT_EXPONENT_POS); |
| 33 |
|
| 34 |
|
| 35 |
static constexpr int HALF_FLT_SIGN_POS = 15; |
| 36 |
static constexpr int HALF_FLT_EXPONENT_POS = 10; |
| 37 |
static constexpr int HALF_FLT_EXPONENT_BITS = 5; |
| 38 |
static constexpr int HALF_FLT_EXPONENT_MASK = (1 << HALF_FLT_EXPONENT_BITS) - 1; |
| 39 |
static constexpr int HALF_FLT_EXPONENT_BIAS = 15; |
| 40 |
static constexpr int HALF_FLT_MANTISSA_BITS = 10; |
| 41 |
static constexpr int HALF_FLT_MANTISSA_MASK = (1 << HALF_FLT_MANTISSA_BITS) - 1; |
| 42 |
static constexpr int HALF_FLT_EXPONENT_AND_MANTISSA_MASK = HALF_FLT_MANTISSA_MASK + (HALF_FLT_EXPONENT_MASK << HALF_FLT_EXPONENT_POS); |
| 43 |
|
| 44 |
|
| 45 |
enum ERoundingMode |
| 46 |
{ |
| 47 |
ROUND_TO_NEG_INF, |
| 48 |
ROUND_TO_POS_INF, |
| 49 |
ROUND_TO_NEAREST, |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
template <int RoundingMode> |
| 54 |
inline HalfFloat FromFloatFallback(float inV) |
| 55 |
{ |
| 56 |
|
| 57 |
uint32 value = BitCast<uint32>(inV); |
| 58 |
|
| 59 |
|
| 60 |
uint32 exponent = (value >> FLOAT_EXPONENT_POS) & FLOAT_EXPONENT_MASK; |
| 61 |
|
| 62 |
|
| 63 |
uint32 mantissa = value & FLOAT_MANTISSA_MASK; |
| 64 |
|
| 65 |
|
| 66 |
HalfFloat hf_sign = HalfFloat(value >> (FLOAT_SIGN_POS - HALF_FLT_SIGN_POS)) & (1 << HALF_FLT_SIGN_POS); |
| 67 |
|
| 68 |
|
| 69 |
if (exponent == FLOAT_EXPONENT_MASK) |
| 70 |
return hf_sign | (mantissa == 0? HALF_FLT_INF : HALF_FLT_NANQ); |
| 71 |
|
| 72 |
|
| 73 |
int rebiased_exponent = int(exponent) - FLOAT_EXPONENT_BIAS + HALF_FLT_EXPONENT_BIAS; |
| 74 |
|
| 75 |
|
| 76 |
if (rebiased_exponent >= HALF_FLT_EXPONENT_MASK) |
| 77 |
{ |
| 78 |
bool round_up = RoundingMode == ROUND_TO_NEAREST || (hf_sign == 0) == (RoundingMode == ROUND_TO_POS_INF); |
| 79 |
return hf_sign | (round_up? HALF_FLT_INF : HALF_FLT_MAX); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
if (rebiased_exponent < -HALF_FLT_MANTISSA_BITS) |
| 84 |
{ |
| 85 |
bool round_up = RoundingMode != ROUND_TO_NEAREST && (hf_sign == 0) == (RoundingMode == ROUND_TO_POS_INF) && (value & FLOAT_EXPONENT_AND_MANTISSA_MASK) != 0; |
| 86 |
return hf_sign | (round_up? 1 : 0); |
| 87 |
} |
| 88 |
|
| 89 |
HalfFloat hf_exponent; |
| 90 |
int shift; |
| 91 |
if (rebiased_exponent <= 0) |
| 92 |
{ |
| 93 |
|
| 94 |
hf_exponent = 0; |
| 95 |
mantissa |= 1 << FLOAT_MANTISSA_BITS; |
| 96 |
shift = FLOAT_MANTISSA_BITS - HALF_FLT_MANTISSA_BITS + 1 - rebiased_exponent; |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
|
| 101 |
hf_exponent = HalfFloat(rebiased_exponent << HALF_FLT_EXPONENT_POS); |
| 102 |
shift = FLOAT_MANTISSA_BITS - HALF_FLT_MANTISSA_BITS; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
HalfFloat hf_mantissa = HalfFloat(mantissa >> shift); |
| 107 |
HalfFloat hf = hf_sign | hf_exponent | hf_mantissa; |
| 108 |
|
| 109 |
|
| 110 |
uint remainder = mantissa & ((1 << shift) - 1); |
| 111 |
|
| 112 |
if constexpr (RoundingMode == ROUND_TO_NEAREST) |
| 113 |
{ |
| 114 |
|
| 115 |
uint round_threshold = 1 << (shift - 1); |
| 116 |
if (remainder > round_threshold |
| 117 |
|| (remainder == round_threshold && (hf_mantissa & 1))) |
| 118 |
hf++; |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
|
| 123 |
bool round_up = (hf_sign == 0) == (RoundingMode == ROUND_TO_POS_INF) && remainder != 0; |
| 124 |
if (round_up) |
| 125 |
hf++; |
| 126 |
} |
| 127 |
|
| 128 |
return hf; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
template <int RoundingMode> |
| 133 |
JPH_INLINE HalfFloat FromFloat(float inV) |
| 134 |
{ |
| 135 |
#ifdef JPH_USE_F16C |
| 136 |
FPExceptionDisableOverflow disable_overflow; |
| 137 |
JPH_UNUSED(disable_overflow); |
| 138 |
|
| 139 |
union |
| 140 |
{ |
| 141 |
__m128i u128; |
| 142 |
HalfFloat u16[8]; |
| 143 |
} hf; |
| 144 |
__m128 val = _mm_load_ss(&inV); |
| 145 |
switch (RoundingMode) |
| 146 |
{ |
| 147 |
case ROUND_TO_NEG_INF: |
| 148 |
hf.u128 = _mm_cvtps_ph(val, _MM_FROUND_TO_NEG_INF); |
| 149 |
break; |
| 150 |
case ROUND_TO_POS_INF: |
| 151 |
hf.u128 = _mm_cvtps_ph(val, _MM_FROUND_TO_POS_INF); |
| 152 |
break; |
| 153 |
case ROUND_TO_NEAREST: |
| 154 |
hf.u128 = _mm_cvtps_ph(val, _MM_FROUND_TO_NEAREST_INT); |
| 155 |
break; |
| 156 |
} |
| 157 |
return hf.u16[0]; |
| 158 |
#else |
| 159 |
return FromFloatFallback<RoundingMode>(inV); |
| 160 |
#endif |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
inline Vec4 ToFloatFallback(UVec4Arg inValue) |
| 165 |
{ |
| 166 |
|
| 167 |
UVec4 value = inValue.Expand4Uint16Lo(); |
| 168 |
|
| 169 |
|
| 170 |
UVec4 exponent_mantissa = UVec4::sAnd(value, UVec4::sReplicate(HALF_FLT_EXPONENT_AND_MANTISSA_MASK)).LogicalShiftLeft<FLOAT_EXPONENT_POS - HALF_FLT_EXPONENT_POS>() + UVec4::sReplicate((FLOAT_EXPONENT_BIAS - HALF_FLT_EXPONENT_BIAS) << FLOAT_EXPONENT_POS); |
| 171 |
|
| 172 |
|
| 173 |
UVec4 exponent_mantissa_denormalized = ((exponent_mantissa + UVec4::sReplicate(1 << FLOAT_EXPONENT_POS)).ReinterpretAsFloat() - UVec4::sReplicate((FLOAT_EXPONENT_BIAS - HALF_FLT_EXPONENT_BIAS + 1) << FLOAT_EXPONENT_POS).ReinterpretAsFloat()).ReinterpretAsInt(); |
| 174 |
|
| 175 |
|
| 176 |
UVec4 exponent_mantissa_nan_inf = UVec4::sOr(exponent_mantissa, UVec4::sReplicate(FLOAT_EXPONENT_MASK << FLOAT_EXPONENT_POS)); |
| 177 |
|
| 178 |
|
| 179 |
UVec4 exponent_mask = UVec4::sReplicate(HALF_FLT_EXPONENT_MASK << HALF_FLT_EXPONENT_POS); |
| 180 |
UVec4 exponent = UVec4::sAnd(value, exponent_mask); |
| 181 |
UVec4 is_denormalized = UVec4::sEquals(exponent, UVec4::sZero()); |
| 182 |
UVec4 is_nan_inf = UVec4::sEquals(exponent, exponent_mask); |
| 183 |
|
| 184 |
|
| 185 |
UVec4 result_exponent_mantissa = UVec4::sSelect(UVec4::sSelect(exponent_mantissa, exponent_mantissa_nan_inf, is_nan_inf), exponent_mantissa_denormalized, is_denormalized); |
| 186 |
|
| 187 |
|
| 188 |
UVec4 sign = UVec4::sAnd(value, UVec4::sReplicate(1 << HALF_FLT_SIGN_POS)).LogicalShiftLeft<FLOAT_SIGN_POS - HALF_FLT_SIGN_POS>(); |
| 189 |
|
| 190 |
|
| 191 |
return UVec4::sOr(sign, result_exponent_mantissa).ReinterpretAsFloat(); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
JPH_INLINE Vec4 ToFloat(UVec4Arg inValue) |
| 196 |
{ |
| 197 |
#if defined(JPH_USE_F16C) |
| 198 |
return _mm_cvtph_ps(inValue.mValue); |
| 199 |
#elif defined(JPH_USE_NEON) |
| 200 |
return vcvt_f32_f16(vreinterpret_f16_u32(vget_low_u32(inValue.mValue))); |
| 201 |
#else |
| 202 |
return ToFloatFallback(inValue); |
| 203 |
#endif |
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
JPH_NAMESPACE_END |