6 #ifndef VNIX_UNITS_DIM_HPP 7 #define VNIX_UNITS_DIM_HPP 10 #include <vnix/rat.hpp> 11 #include <vnix/units/dim-base-off.hpp> 26 using rat = rational<4, 2>;
38 static_assert(
NUM_BITS <= 64,
"too many bits for bases");
49 template <
typename T>
constexpr static word
encode(T t) {
59 template <
typename T,
typename... U>
60 constexpr static word
encode(T t, U... us) {
61 constexpr auto N =
sizeof...(us);
62 static_assert(
N <
NUM_BASES,
"too many exponents");
63 constexpr auto SHIFT =
MAX_SHIFT - N * rat::BITS;
64 constexpr word MASK = bit_range<word>(0, rat::BITS - 1) << SHIFT;
66 return (e & MASK) | (encode(us...) & ~MASK);
72 for (uint_fast8_t i = 0; i <
NUM_BASES; ++i) {
73 auto const SHIFT = i * rat::BITS;
74 word
const MASK = bit_range<word>(0, rat::BITS - 1) << SHIFT;
96 template <
typename... T>
constexpr basic_dim(T... ts) :
e_(encode(ts...)) {
97 static_assert(
sizeof...(
ts) ==
NUM_BASES,
"illegal number of bases");
108 constexpr rat
exp(DBO off)
const {
109 constexpr auto mask = bit_range<word>(0, rat::BITS - 1);
120 constexpr void set(DBO off, rat r) {
121 unsigned const bit_off = off * rat::BITS;
123 auto const mask = bit_range<word>(0, rat::BITS - 1) << bit_off;
124 e_ = (
e_ & ~mask) | (expon & mask);
132 template <
typename F>
135 for (
auto b : off::array) {
136 auto const os = DBO(b);
137 r.set(os, f(
exp(os
), d.exp(os)));
148 for (
auto b : off::array) {
149 auto const os = DBO(b);
150 r.set(os, f(
exp(os
)));
156 constexpr static rat
add(rat x, rat y) {
return x
+ y; }
159 constexpr static rat
sbtrct(rat x, rat y) {
return x
- y; }
195 return combine(a,
add);
204 return combine(s,
sbtrct);
224 return transform(
divd(f));
235 static std::ostream &
print_unit(std::ostream &s,
char const *u, rat e) {
240 if (e
.d() != 1) { s <<
"["; }
242 if (e
.d() != 1) { s <<
"]"; }
static constexpr utype encode(rational r)
Encoding from rational number.
constexpr auto operator-(rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
Difference between two rational numbers.
Maximum number of bits to shift an encoding of a rational exponent.
constexpr basic_dim(std::array< rat, NUM_BASES > const &a)
constexpr word encode() const
Encode data for this dim into a word.
static constexpr rat sbtrct(rat x, rat y)
Function used to subtract corresponding exponents.
constexpr basic_dim combine(basic_dim const &d, F f) const
Combine each exponent with corresponding other exponent via function.
static constexpr rational decode(utype u)
Rational number from encoding.
constexpr rat operator()(rat x) const
Divide an exponent by the factor.
constexpr int_types< DNM_BITS >::UF d() const
Normalized denominator.
Function object used for dividing every exponent by a single factor.
static constexpr word encode(T t)
Encode exponent associated with highest DBO at highest bit-offsets.
constexpr basic_dim operator+(basic_dim const &a) const
Add corresponding exponents.
rat f
Factor by which to divide exponents.
constexpr basic_dim(word u)
Initialize from dim that has been encoded into a word.
constexpr basic_dim transform(F f) const
Transform each exponent according to a function.
static constexpr word encode(std::array< rat, NUM_BASES > const &a)
rat f
Factor by which to multiply exponents.
constexpr auto operator+(rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
Sum of two rational numbers.
constexpr void set(DBO off, rat r)
Set rational exponent at specified offset.
constexpr rational(stype n=0, stype d=1)
Initialize from numerator and denominator.
constexpr basic_dim operator-(basic_dim const &s) const
Subtract corresponding exponents.
Smallest integer types for holding certain number of bits.
static constexpr dim nul_dim
Null dimension.
static constexpr word encode(T t, U...us)
Encode exponents, with higher DBO at higher bit-offsets.
Rational exponent for each basis-element of dimension.
friend std::ostream & operator<<(std::ostream &s, basic_dim d)
Print to to output stream.
constexpr rat exp(DBO off) const
Rational exponent at specified offset.
constexpr rat operator[](DBO off) const
Rational exponent at specified offset.
constexpr bool operator!=(rational< NB1, DB1 > r1, rational< NB2, DB2 > r2)
Compare rationals for inequality.
static std::ostream & print_unit(std::ostream &s, char const *u, rat e)
Print to output stream the symbolic contribution from a given unit.
constexpr mult(rat ff)
Initialize from factor by which to multiply exponents.
constexpr bool to_bool() const
Automatically convert to boolean.
constexpr bool operator==(basic_dim const &d) const
constexpr basic_dim operator*(rat f) const
Multiply exponents by rational factor.
constexpr basic_dim operator/(rat::stype f) const
Divide exponents by rational factor.
Number of bits in instance of dim.
Thomas E. Vaughan's public software.
static constexpr rat add(rat x, rat y)
Function used to add corresponding exponents.
Classes and functions supporting a model of physically dimensioned quantities.
Number of bases for dimension.
word e_
Storage for exponents.
Function object used for multiplying every exponent by a single factor.
constexpr divd(rat ff)
Initialize from factor by which to divide exponents.
constexpr bool operator!=(basic_dim const &d) const
constexpr rat operator()(rat x) const
Multiply an exponent by the factor.
constexpr basic_dim operator/(rat f) const
Divide exponents by rational factor.
constexpr basic_dim(T...ts)
Initialize exponents, one for each base quantity.