libfxd 0.2.dev
A fixed-point library for C++.
Loading...
Searching...
No Matches
saturate.hpp File Reference
#include <concepts>
#include <csignal>
#include "concepts.hpp"
#include "error.hpp"
#include "detail/safe.hpp"
#include "detail/safe-div.hpp"
#include "detail/safe-mul.hpp"

Go to the source code of this file.

Namespaces

namespace  fxd
 This is the namespace where the entire library is defined.
 
namespace  fxd::saturate
 Clamp on overflow.
 
namespace  fxd::saturate::down
 Round down, and clamp on overflow.
 
namespace  fxd::saturate::up
 Round up, and clamp on overflow.
 
namespace  fxd::saturate::zero
 Round to zero, and clamp on overflow.
 

Macros

#define WRAP(expr)
 

Functions

template<typename T >
constexpr T saturate (error e) noexcept
 Error handler that clamps the value on error.
 
template<fixed_point Fxd, std::integral I>
constexpr Fxd from_raw (I val) noexcept
 Construct from raw value, clamp on overflow.
 
template<fixed_point Dst, std::convertible_to< Dst > Src>
constexpr Dst make_fixed (Src src) noexcept
 Construct from numerical value, clamp on overflow.
 
template<unsigned_fixed_point Dst, std::convertible_to< Dst > Src>
constexpr Dst make_ufixed (Src src) noexcept
 Construct from numerical value, clamp on overflow (unsigned version).
 
template<int Int, int Frac, typename Raw = detail::select_int_t<Int + Frac>, std::convertible_to< fixed< Int, Frac, Raw > > Src>
constexpr fixed< Int, Frac, Raw > make_fixed (Src src) noexcept
 Convenience overload.
 
template<fixed_point Dst, fixed_point Src>
constexpr Dst fixed_cast (Src src) noexcept
 Convert between fxd::fixed types, clamp on overflow.
 
template<int Int, int Frac, typename Raw = detail::select_int_t<Int + Frac>, fixed_point Src>
constexpr fixed< Int, Frac, Raw > fixed_cast (Src src) noexcept
 Convenience overload.
 
template<int Int, int Frac, typename Raw = detail::select_uint_t<Int + Frac>, fixed_point Src>
constexpr fixed< Int, Frac, Raw > ufixed_cast (Src src) noexcept
 Convenience overload (unsigned version).
 
template<std::integral I, fixed_point Fxd>
constexpr I to_int (Fxd f) noexcept
 Convert to integer, clamp on overflow.
 
template<fixed_point Fxd>
constexpr detail::select_int_for< Fxd::int_bits, typename Fxd::raw_type > to_int (Fxd f)
 Convert to the natural integer type, clamp on overflow.
 
template<fixed_point Dst, std::convertible_to< Dst > Src>
constexpr Dst & assign (Dst &dst, Src src) noexcept
 Assignment, clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd & pre_inc (Fxd &f) noexcept
 Pre-increment (++f), clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd post_inc (Fxd &f) noexcept
 Post-increment (f++), clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd & pre_dec (Fxd &f) noexcept
 Pre-decrement (--f), clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd post_dec (Fxd &f) noexcept
 Post-decrement (f--), clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd negate (Fxd f) noexcept
 Negate, clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd add (Fxd a, Fxd b) noexcept
 Add, clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd sub (Fxd a, Fxd b) noexcept
 Subtract, clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd div (Fxd a, Fxd b) noexcept
 Divide rounding down, clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd mul (Fxd a, Fxd b) noexcept
 Multiply rounding down, clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd div (Fxd a, Fxd b) noexcept
 Divide rounding up, clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd mul (Fxd a, Fxd b) noexcept
 Multiply rounding up, clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd div (Fxd a, Fxd b) noexcept
 Divide rounding to zero, clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd mul (Fxd a, Fxd b) noexcept
 Multiply rounding to zero, clamp on overflow.
 
template<fixed_point Fxd>
constexpr Fxd abs (Fxd f) noexcept
 Same as fxd::abs(), clamp on overflow.
 

Macro Definition Documentation

◆ WRAP

#define WRAP (   expr)
Value:
do { \
auto result = detail::safe::expr; \
using Ret = typename decltype(result)::value_type; \
if (!result) \
return saturate<Ret>(result.error()); \
return *result; \
} while (false)

Definition at line 54 of file saturate.hpp.