tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
half.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_CFLOAT_HALF_HPP
5#define TETL_CFLOAT_HALF_HPP
6
7#include <etl/_bit/bit_cast.hpp>
8#include <etl/_cstdint/uint_t.hpp>
9
10namespace etl {
11
12struct binary_t { };
13
14inline constexpr auto binary = binary_t{};
15
16struct half {
17 using storage_type = etl::uint16_t;
18
19 constexpr half() = default;
20 constexpr half(binary_t /*tag*/, storage_type bits);
21
22private:
23 storage_type _bits{0};
24};
25
26[[nodiscard]] constexpr auto isfinite(half arg) noexcept -> bool;
27[[nodiscard]] constexpr auto isinf(half arg) noexcept -> bool;
28[[nodiscard]] constexpr auto isnan(half arg) noexcept -> bool;
29[[nodiscard]] constexpr auto isnormal(half arg) noexcept -> bool;
30[[nodiscard]] constexpr auto signbit(half arg) noexcept -> bool;
31
32// IMPL
33namespace detail {
34inline constexpr etl::half::storage_type exp_mask{0b0111'1100'0000'0000};
35inline constexpr etl::half::storage_type man_mask{0b0000'0011'1111'1111};
36inline constexpr etl::half::storage_type inf_mask{0b0111'1111'1111'1111};
37inline constexpr etl::half::storage_type sign_mask{0b1000'0000'0000'0000};
38} // namespace detail
39
40constexpr half::half(binary_t /*tag*/, half::storage_type bits)
41 : _bits{bits}
42{
43 // [tobi] This needs to be here, or clang will complain about an unused
44 // member. All free functions use bit_cast to access the underlying bits of
45 // the half float, so no "getter" method exists.
46 (void)_bits;
47}
48
49constexpr auto isfinite(half arg) noexcept -> bool
50{
51 using uint_t = half::storage_type;
52 return (etl::bit_cast<uint_t>(arg) & detail::exp_mask) != detail::exp_mask;
53}
54
55constexpr auto isinf(half arg) noexcept -> bool
56{
57 using uint_t = half::storage_type;
58 return (etl::bit_cast<uint_t>(arg) & detail::inf_mask) == detail::exp_mask;
59}
60
61constexpr auto isnan(half arg) noexcept -> bool
62{
63 using uint_t = half::storage_type;
64 return (etl::bit_cast<uint_t>(arg) & detail::inf_mask) > detail::exp_mask;
65}
66
67constexpr auto isnormal(half arg) noexcept -> bool
68{
69 using uint_t = half::storage_type;
70 auto const mask = detail::exp_mask;
71 auto const bits = etl::bit_cast<uint_t>(arg);
72 return ((bits & mask) != 0) & ((bits & mask) != mask); // NOLINT
73}
74
75constexpr auto signbit(half arg) noexcept -> bool
76{
77 using uint_t = half::storage_type;
78 return (etl::bit_cast<uint_t>(arg) & detail::sign_mask) != 0;
79}
80
81} // namespace etl
82
83#endif // TETL_CFLOAT_HALF_HPP
constexpr auto bit_cast(From const &src) noexcept -> To
Obtain a value of type To by reinterpreting the object representation of from. Every bit in the value...
Definition bit_cast.hpp:39
Definition adjacent_find.hpp:9
constexpr auto isfinite(half arg) noexcept -> bool
Definition half.hpp:49
constexpr auto isinf(half arg) noexcept -> bool
Definition half.hpp:55
constexpr auto signbit(half arg) noexcept -> bool
Definition half.hpp:75
constexpr auto isnormal(half arg) noexcept -> bool
Definition half.hpp:67
constexpr auto isnan(half arg) noexcept -> bool
Definition half.hpp:61
constexpr auto binary
Definition half.hpp:14
Definition half.hpp:12
Definition half.hpp:16
constexpr half(binary_t, storage_type bits)
Definition half.hpp:40
constexpr half()=default