diff options
author | Arne Juul <arnej@verizonmedia.com> | 2021-03-02 18:07:35 +0000 |
---|---|---|
committer | Arne Juul <arnej@verizonmedia.com> | 2021-03-02 18:18:22 +0000 |
commit | 8521832abb54bf3cf3431020da6bc400366dc610 (patch) | |
tree | abb49d984dbe3dbdb207e6bf19b7830fd43abc1f /vespalib | |
parent | f526e6787353c4fa6f559199e4dea93f83bee4d3 (diff) |
use static_assert to require knowledge of endianness
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/vespalib/util/bfloat16.h | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/vespalib/src/vespa/vespalib/util/bfloat16.h b/vespalib/src/vespa/vespalib/util/bfloat16.h index 8090f0562c7..f5066d2f8e5 100644 --- a/vespalib/src/vespa/vespalib/util/bfloat16.h +++ b/vespalib/src/vespa/vespalib/util/bfloat16.h @@ -46,27 +46,29 @@ public: constexpr uint16_t get_bits() const { return _bits; } constexpr void assign_bits(uint16_t value) noexcept { _bits = value; } + template<std::endian native_endian = std::endian::native> static constexpr uint16_t float_to_bits(float value) noexcept { TwoU16 both{0,0}; static_assert(sizeof(TwoU16) == sizeof(float)); memcpy(&both, &value, sizeof(float)); - if constexpr (std::endian::native == std::endian::big) { + if constexpr (native_endian == std::endian::big) { return both.u1; - } else if constexpr (std::endian::native == std::endian::little) { - return both.u2; } else { - return 0; + static_assert(native_endian == std::endian::little, + "Unknown endian, cannot handle"); + return both.u2; } } + template<std::endian native_endian = std::endian::native> static constexpr float bits_to_float(uint16_t bits) noexcept { TwoU16 both{0,0}; - if constexpr (std::endian::native == std::endian::big) { + if constexpr (native_endian == std::endian::big) { both.u1 = bits; - } else if constexpr (std::endian::native == std::endian::little) { - both.u2 = bits; } else { - return 0.0; + static_assert(native_endian == std::endian::little, + "Unknown endian, cannot handle"); + both.u2 = bits; } float result = 0.0; static_assert(sizeof(TwoU16) == sizeof(float)); |