6#include "../macros.hpp"
8#include "cpu_id_x86.hpp"
10#include "cpu_id_generic.hpp"
23hi_export_module(hikogui.utility.float_to_half);
25hi_export
namespace hi {
inline namespace v1 {
27[[
nodiscard]]
constexpr static uint16_t float_to_half_generic(
float a)
noexcept
29 auto u32 = std::bit_cast<uint32_t>(a);
32 auto exponent =
static_cast<int16_t
>(
static_cast<uint8_t
>((
u32 << 1) >> 24)) - 127 + 15;
37 auto const is_inf = exponent >= 0x1f;
53 auto shift = 1 - exponent;
71 auto r =
static_cast<uint16_t
>((
static_cast<int32_t
>(
u32) >> 31) << 15);
80 for (
size_t i = 0; i != 4; ++i) {
81 r[i] = float_to_half_generic(a[i]);
87hi_target(
"sse,sse2,f16c")
95hi_target(
"sse,sse2,f16c")
101 return std::get<0>(r);
124 return float_to_half_generic(a);
137 return float_to_half_generic(a);
169 return std::get<0>(r);
173[[
nodiscard]]
constexpr uint16_t float_to_half(
float v)
noexcept
175 if (
not std::is_constant_evaluated()) {
180 return std::get<0>(
tmp);
185 return float_to_half_generic(v);
192 if (
not std::is_constant_evaluated()) {
203 for (
size_t i = 0; i != 4; ++i) {
204 r[i] = float_to_half_generic(v[i]);
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
@ shift
The shift key is being held.
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
hi_inline bool has_sse2() noexcept
This CPU has the SSE2 instruction set.
Definition cpu_id_x86.hpp:672
hi_inline bool has_f16c() noexcept
This CPU has float-16 conversion instructions.
Definition cpu_id_x86.hpp:752
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378