9#include "../microfmt.hpp"
18constexpr uint64_t
ct_pow10(uint8_t exp)
noexcept {
20 for (uint8_t i = 0; i < exp; ++i) {
34template <u
int64_t Scale, u
int8_t Decimals = 2,
typename IntType =
int32_t>
struct fixed_point_view {
35 static_assert(Scale > 0,
"fixed_point Scale must be greater than 0");
36 static_assert(Decimals <= 18,
"fixed_point Decimals exceeds 64-bit precision");
37 static_assert(std::is_integral_v<IntType>,
"fixed_point value must be an integral type");
43template <u
int64_t Scale, u
int8_t Decimals = 2,
typename IntType>
44[[nodiscard]]
constexpr auto fixed(IntType raw_val)
noexcept {
53template <
typename IntType =
int32_t>
56template <
typename IntType> [[nodiscard]]
constexpr auto milli(IntType raw_val)
noexcept {
60template <
typename IntType> [[nodiscard]]
constexpr auto centi(IntType raw_val)
noexcept {
64template <
typename IntType> [[nodiscard]]
constexpr auto micro(IntType raw_val)
noexcept {
71template <u
int64_t Scale, u
int8_t Decimals,
typename IntType>
77 std::make_unsigned_t<std::conditional_t<(
sizeof(IntType) < 4), uint32_t, std::make_unsigned_t<IntType>>>;
79 bool is_negative =
false;
80 UnsignedType abs_raw = 0;
82 if constexpr (std::is_signed_v<IntType>) {
83 if (fp.raw_value < 0) {
86 abs_raw =
static_cast<UnsignedType
>(0) -
static_cast<UnsignedType
>(fp.raw_value);
88 abs_raw =
static_cast<UnsignedType
>(fp.raw_value);
91 abs_raw = fp.raw_value;
94 const uint64_t integer_part =
static_cast<uint64_t
>(abs_raw / Scale);
95 const uint64_t remainder =
static_cast<uint64_t
>(abs_raw % Scale);
100 uint64_t frac_part = 0;
102 if constexpr (Decimals > 0) {
104 frac_part = (remainder * target_mult) / Scale;
108 if (is_negative && (integer_part > 0 || frac_part > 0)) {
113 detail::format_unsigned<detail::radix::decimal>(out, integer_part,
false, 0);
116 if constexpr (Decimals > 0) {
118 detail::format_unsigned<detail::radix::decimal>(out, frac_part,
false, Decimals);
Definition microfmt.hpp:881
constexpr uint64_t ct_pow10(uint8_t exp) noexcept
Definition fixed_point.hpp:18
constexpr auto fixed(IntType raw_val) noexcept
Definition fixed_point.hpp:44
constexpr auto centi(IntType raw_val) noexcept
Definition fixed_point.hpp:60
constexpr auto micro(IntType raw_val) noexcept
Definition fixed_point.hpp:64
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
constexpr auto milli(IntType raw_val) noexcept
Definition fixed_point.hpp:56
Definition fixed_point.hpp:34
IntType raw_value
Definition fixed_point.hpp:39