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
3#ifndef TETL_CFLOAT_HALF_HPP
4#define TETL_CFLOAT_HALF_HPP
5
8
9namespace etl {
10
11struct binary_t { };
12
13inline constexpr auto binary = binary_t{};
14
15struct half {
17
18 constexpr half() = default;
19 constexpr half(binary_t /*tag*/, storage_type bits);
20
21private:
22 storage_type _bits{0};
23};
24
25[[nodiscard]] constexpr auto isfinite(half arg) noexcept -> bool;
26[[nodiscard]] constexpr auto isinf(half arg) noexcept -> bool;
27[[nodiscard]] constexpr auto isnan(half arg) noexcept -> bool;
28[[nodiscard]] constexpr auto isnormal(half arg) noexcept -> bool;
29[[nodiscard]] constexpr auto signbit(half arg) noexcept -> bool;
30
31// IMPL
32namespace detail {
33inline constexpr etl::half::storage_type exp_mask{0b0111'1100'0000'0000};
34inline constexpr etl::half::storage_type man_mask{0b0000'0011'1111'1111};
35inline constexpr etl::half::storage_type inf_mask{0b0111'1111'1111'1111};
36inline constexpr etl::half::storage_type sign_mask{0b1000'0000'0000'0000};
37} // namespace detail
38
39constexpr half::half(binary_t /*tag*/, half::storage_type bits)
40 : _bits{bits}
41{
42 // [tobi] This needs to be here, or clang will complain about an unused
43 // member. All free functions use bit_cast to access the underlying bits of
44 // the half float, so no "getter" method exists.
45 (void)_bits;
46}
47
48constexpr auto isfinite(half arg) noexcept -> bool
49{
50 using uint_t = half::storage_type;
51 return (etl::bit_cast<uint_t>(arg) & detail::exp_mask) != detail::exp_mask;
52}
53
54constexpr auto isinf(half arg) noexcept -> bool
55{
56 using uint_t = half::storage_type;
57 return (etl::bit_cast<uint_t>(arg) & detail::inf_mask) == detail::exp_mask;
58}
59
60constexpr auto isnan(half arg) noexcept -> bool
61{
62 using uint_t = half::storage_type;
63 return (etl::bit_cast<uint_t>(arg) & detail::inf_mask) > detail::exp_mask;
64}
65
66constexpr auto isnormal(half arg) noexcept -> bool
67{
68 using uint_t = half::storage_type;
69 auto const mask = detail::exp_mask;
70 auto const bits = etl::bit_cast<uint_t>(arg);
71 return ((bits & mask) != 0) & ((bits & mask) != mask); // NOLINT
72}
73
74constexpr auto signbit(half arg) noexcept -> bool
75{
76 using uint_t = half::storage_type;
77 return (etl::bit_cast<uint_t>(arg) & detail::sign_mask) != 0;
78}
79
80} // namespace etl
81
82#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:38
constexpr auto arg(complex< T > const &z) noexcept -> T
Definition arg.hpp:15
Definition adjacent_find.hpp:8
constexpr auto isfinite(half arg) noexcept -> bool
Definition half.hpp:48
constexpr auto isinf(half arg) noexcept -> bool
Definition half.hpp:54
TETL_BUILTIN_UINT16 uint16_t
Unsigned integer type with width of exactly 16 bits.
Definition uint_t.hpp:14
constexpr auto signbit(half arg) noexcept -> bool
Definition half.hpp:74
constexpr auto isnormal(half arg) noexcept -> bool
Definition half.hpp:66
constexpr auto isnan(half arg) noexcept -> bool
Definition half.hpp:60
constexpr auto binary
Definition half.hpp:13
Definition half.hpp:11
Definition half.hpp:15
etl::uint16_t storage_type
Definition half.hpp:16
constexpr half()=default