libfxd 0.2.dev
A fixed-point library for C++.
Loading...
Searching...
No Matches
fixed.hpp
Go to the documentation of this file.
1/*
2 * libfxd - a fixed-point library for C++
3 *
4 * Copyright 2023 Daniel K. O.
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef LIBFXD_FIXED_HPP
9#define LIBFXD_FIXED_HPP
10
11//#include <compare>
12#include <concepts>
13#include <limits>
14
15#include "detail/types.hpp"
16
18namespace fxd {
19
34 template<int Int,
35 int Frac,
36 typename Raw = detail::select_int_t<Int + Frac>
37 >
38 struct fixed {
39
41 using raw_type = Raw;
42 static_assert(std::numeric_limits<Raw>::is_specialized,
43 "raw type is not usable, lacks std::numeric_limits support");
44
46 static constexpr int int_bits = Int;
47
49 static constexpr int frac_bits = Frac;
50
52 static constexpr int bits = int_bits + frac_bits;
53
55 static constexpr int raw_bits =
56 detail::type_width<raw_type>;
57
58
59 static_assert(raw_bits >= bits);
60 static_assert(bits > 0);
61 static_assert(int_bits <= 2 * raw_bits);
62 static_assert(frac_bits <= 2 * raw_bits);
63
66
67
68
69 // constructors
70
72 constexpr fixed() noexcept = default;
73
74 // Defaulted copy constructor.
75 // constexpr fixed(const fixed&) noexcept = default;
76
78 template<std::integral I>
79 constexpr fixed(I i) noexcept;
80
82 template<std::floating_point Flt>
83 constexpr fixed(Flt f) noexcept;
84
85
87 template<int Int2, int Frac2, typename Raw2>
88 requires (detail::is_safe_conversion_v<Int2, Frac2, Raw2,
89 Int, Frac, Raw>)
90 constexpr fixed(const fixed<Int2, Frac2, Raw2>& other) noexcept;
91
92
93 // named constructors
94
96 static constexpr fixed from_raw(raw_type val) noexcept;
97
98
99 // Defaulted copy assignment.
100 //constexpr fixed& operator =(const fixed&) noexcept = default;
101
102
103 // conversion
104
106 explicit constexpr
107 operator bool() const noexcept;
108
110 template<std::integral I>
111 explicit constexpr
112 operator I() const noexcept;
113
115 template<std::floating_point F>
116 explicit constexpr
117 operator F() const noexcept;
118
120 template<int Int2, int Frac2, typename Raw2>
121 explicit
122 constexpr
123 operator fixed<Int2, Frac2, Raw2>() const noexcept;
124
125 };
126
127
129 template<int Int,
130 int Frac>
131 using ufixed = fixed<Int, Frac, detail::select_uint_t<Int + Frac>>;
132
133
134}
135
136#endif
This is the namespace where the entire library is defined.
Definition: casting.hpp:19
STL namespace.
The fixed-point class template.
Definition: fixed.hpp:38
constexpr fixed() noexcept=default
Defaulted constructor.
static constexpr int raw_bits
How many bits are used for storage (can be larger than bits).
Definition: fixed.hpp:55
static constexpr int int_bits
How many integral bits were requested.
Definition: fixed.hpp:46
static constexpr int bits
How many bits were requested in total.
Definition: fixed.hpp:52
static constexpr int frac_bits
How many fractional bits were requested.
Definition: fixed.hpp:49
Raw raw_type
The integral type that will hold the raw representation of the value.
Definition: fixed.hpp:41
raw_type raw_value
The raw value, stored as a bitfield.
Definition: fixed.hpp:65