8#ifndef LIBFXD_SATURATE_HPP
9#define LIBFXD_SATURATE_HPP
17#include "detail/safe.hpp"
18#include "detail/safe-div.hpp"
19#include "detail/safe-mul.hpp"
43 return std::numeric_limits<T>::lowest();
45 return std::numeric_limits<T>::max();
56 auto result = detail::safe::expr; \
57 using Ret = typename decltype(result)::value_type; \
59 return saturate<Ret>(result.error()); \
70 template<fixed_point Fxd,
78 WRAP(from_raw<Fxd>(val));
84 std::convertible_to<Dst> Src>
91 WRAP(make_fixed<Dst>(src));
97 std::convertible_to<Dst> Src>
104 WRAP(make_fixed<Dst>(src));
109 template<
int Int,
int Frac,
110 typename Raw = detail::select_int_t<Int + Frac>,
111 std::convertible_to<fixed<Int, Frac, Raw>> Src>
119 return saturate::make_fixed<Dst>(src);
132 WRAP(fixed_cast<Dst>(src));
137 template<
int Int,
int Frac,
138 typename Raw = detail::select_int_t<Int + Frac>,
147 return saturate::fixed_cast<Dst>(src);
152 template<
int Int,
int Frac,
153 typename Raw = detail::select_uint_t<Int + Frac>,
162 return saturate::fixed_cast<Dst>(src);
167 template<std::integral I,
180 template<fixed_po
int Fxd>
183 detail::select_int_for<Fxd::int_bits, typename Fxd::raw_type>
186 using I = detail::select_int_for<Fxd::int_bits, typename Fxd::raw_type>;
187 return saturate::to_int<I>(f);
193 std::convertible_to<Dst> Src>
200 return dst = saturate::make_fixed<Dst>(src);
205 template<fixed_po
int Fxd>
211 auto result = detail::safe::incremented(f);
213 return f = saturate<Fxd>(result.error());
219 template<fixed_po
int Fxd>
232 template<fixed_po
int Fxd>
238 auto result = detail::safe::decremented(f);
240 return f = saturate<Fxd>(result.error());
246 template<fixed_po
int Fxd>
259 template<fixed_po
int Fxd>
271 template<fixed_po
int Fxd>
284 template<fixed_po
int Fxd>
300 template<fixed_po
int Fxd>
313 template<fixed_po
int Fxd>
331 template<fixed_po
int Fxd>
344 template<fixed_po
int Fxd>
363 template<fixed_po
int Fxd>
375 template<fixed_po
int Fxd>
390 template<fixed_po
int Fxd>
Concept to match any fxd::fixed
Concept to match only unsigned fxd::fixed
constexpr Fxd mul(Fxd a, Fxd b) noexcept
Multiply rounding down, clamp on overflow.
constexpr Fxd div(Fxd a, Fxd b) noexcept
Divide rounding down, clamp on overflow.
constexpr Fxd mul(Fxd a, Fxd b) noexcept
Multiply rounding up, clamp on overflow.
constexpr Fxd div(Fxd a, Fxd b) noexcept
Divide rounding up, clamp on overflow.
constexpr Fxd mul(Fxd a, Fxd b) noexcept
Multiply rounding to zero, clamp on overflow.
constexpr Fxd div(Fxd a, Fxd b) noexcept
Divide rounding to zero, clamp on overflow.
constexpr Dst & assign(Dst &dst, Src src) noexcept
Assignment, clamp on overflow.
constexpr Fxd post_dec(Fxd &f) noexcept
Post-decrement (f--), clamp on overflow.
constexpr Fxd & pre_inc(Fxd &f) noexcept
Pre-increment (++f), clamp on overflow.
constexpr T saturate(error e) noexcept
Error handler that clamps the value on error.
constexpr Dst make_ufixed(Src src) noexcept
Construct from numerical value, clamp on overflow (unsigned version).
constexpr Fxd post_inc(Fxd &f) noexcept
Post-increment (f++), clamp on overflow.
constexpr Fxd & pre_dec(Fxd &f) noexcept
Pre-decrement (--f), clamp on overflow.
constexpr fixed< Int, Frac, Raw > ufixed_cast(Src src) noexcept
Convenience overload (unsigned version).
constexpr Dst fixed_cast(Src src) noexcept
Convert a fixed point to a different type of fixed point.
constexpr I to_int(Fxd f) noexcept
Convert to integer.
error
Error values reported by fxd::expect function.
The fixed-point class template.