8#ifndef LIBFXD_CONVERSIONS_HPP
9#define LIBFXD_CONVERSIONS_HPP
19#include "detail/bias.hpp"
20#include "detail/int_to_float.hpp"
21#include "detail/shift.hpp"
40 template<std::integral I,
48 using Raw =
typename Fxd::raw_type;
49 Raw raw = f.raw_value;
50 if constexpr (Fxd::frac_bits >= 0) {
53 raw += detail::make_bias_for(Fxd::frac_bits, raw);
54 return detail::shr_real(raw, Fxd::frac_bits);
59 using RawWide = detail::max_int_for<Raw>;
60 return detail::shl_real<RawWide>(raw, -Fxd::frac_bits);
67 template<fixed_po
int Fxd>
68 requires detail::has_int_for<Fxd::int_bits, typename Fxd::raw_type>
71 detail::select_int_for<Fxd::int_bits, typename Fxd::raw_type>
75 using I = detail::select_int_for<Fxd::int_bits, typename Fxd::raw_type>;
83 template<std::
integral I>
89 return to_int<I>(*
this);
94 template<std::floating_point Flt,
101 const auto r = f.raw_value;
102 const Flt fraw = detail::int_to_float<Flt>(r);
103 return std::ldexp(fraw, -Fxd::frac_bits);
108 template<fixed_po
int Fxd>
111 typename std::numeric_limits<Fxd>::float_type
115 return to_float<typename std::numeric_limits<Fxd>::float_type>(f);
122 template<std::
floating_po
int F>
128 return to_float<F>(*
this);
132 template<
int Int,
int Frac,
typename Raw>
133 template<
int Int2,
int Frac2,
typename Raw2>
139 return fixed_cast<fixed<Int2, Frac2, Raw2>>(*this);
Concept to match any fxd::fixed
This is the namespace where the entire library is defined.
constexpr I to_int(Fxd f) noexcept
Convert to integer.
constexpr Flt to_float(Fxd f) noexcept
Convert a fixed-point to a floating-point type, rounds to zero.
The fixed-point class template.