Project

General

Profile

Download (16.5 KB) Statistics
| Branch: | Tag: | Revision:
835f4592 David Sorber
7fa2f28e David Sorber
#ifndef __FIXED_H_
#define __FIXED_H_
835f4592 David Sorber
92782e4a David Sorber
#define __STDC_FORMAT_MACROS

11ae9ac5 David Sorber
#include <cctype>
835f4592 David Sorber
#include <cmath>
92782e4a David Sorber
#include <cinttypes>
#include <cstdio>
835f4592 David Sorber
#include <cstdint>
c7e0510c David Sorber
#include <cstdlib>
11ae9ac5 David Sorber
#include <iomanip>
#include <iostream>
835f4592 David Sorber
#include <limits>
11ae9ac5 David Sorber
#include <sstream>
c7e0510c David Sorber
#include <stdexcept>
835f4592 David Sorber
#include <type_traits>
92782e4a David Sorber
#include <map>
835f4592 David Sorber
852d0468 David Sorber
7fa2f28e David Sorber
#if __GNUC__ >= 3
c7e0510c David Sorber
#define __unlikely(cond) __builtin_expect((cond), 0)
#define __likely(cond) __builtin_expect((cond), 1)
7fa2f28e David Sorber
#else
c7e0510c David Sorber
#define __unlikely(cond) (cond)
#define __likely(cond) (cond)
7fa2f28e David Sorber
#endif

852d0468 David Sorber
template<typename T>
constexpr T calculateMax(size_t decimal_digits)
{
T val = 0;
for (uint32_t idx = 0; idx < decimal_digits; ++idx)
{
val *= 10;
val += 9;
}
return val;
}


template<typename IntegerType, typename FractionalType,
typename StrToIntegerTypeFunc,
typename StrToFracTypeFunc>
c7e0510c David Sorber
class fixed
835f4592 David Sorber
{
c7e0510c David Sorber
static_assert(std::is_integral<IntegerType>::value,
"IntegerType must be an integral type");
static_assert(std::is_integral<FractionalType>::value,
"FractionalType must be an integral type");

static_assert(std::is_signed<IntegerType>::value,
"IntegerType must be a signed type");
static_assert(std::is_unsigned<FractionalType>::value,
"FractionalType must be an unsigned type");

835f4592 David Sorber
public:

static constexpr size_t integer_bits = sizeof(IntegerType) * 8;
static constexpr size_t fractional_bits = sizeof(FractionalType) * 8;
c7e0510c David Sorber
static constexpr size_t integer_decimal_digits =
std::floor(std::log10(std::numeric_limits<IntegerType>::max()));
static constexpr size_t fractional_decimal_digits =
std::floor(std::log10(std::numeric_limits<FractionalType>::max()));

852d0468 David Sorber
static constexpr IntegerType MAX_INTEGER_VALUE =
(calculateMax<IntegerType>(integer_decimal_digits));
static constexpr IntegerType MIN_INTEGER_VALUE = 0;
c7e0510c David Sorber
852d0468 David Sorber
static constexpr FractionalType MAX_FRACTIONAL_VALUE =
(calculateMax<FractionalType>(fractional_decimal_digits));
static constexpr FractionalType MIN_FRACTIONAL_VALUE = 0;
c7e0510c David Sorber
11ae9ac5 David Sorber
static const uint64_t SCALE_VALUES[20];
92782e4a David Sorber
static const std::map<uint64_t, uint32_t> DIGIT_LOOKUP_TABLE;
c7e0510c David Sorber
11ae9ac5 David Sorber
// Constructors
835f4592 David Sorber
fixed()
c7e0510c David Sorber
: m_integer(0),
m_fractional(0)
11ae9ac5 David Sorber
{}
c7e0510c David Sorber
11ae9ac5 David Sorber
fixed(IntegerType integerVal)
c7e0510c David Sorber
: m_integer(__checkIntOverflow(integerVal)),
m_fractional(0)
11ae9ac5 David Sorber
{}
c7e0510c David Sorber
835f4592 David Sorber
fixed(IntegerType integerVal, FractionalType fractionalVal)
c7e0510c David Sorber
: m_integer(__checkIntOverflow(integerVal)),
m_fractional(__checkFracOverflow(fractionalVal))
11ae9ac5 David Sorber
{
// Scale the fractional value appropriately
92782e4a David Sorber
m_fractional = __checkFracOverflow(m_fractional *
getFracScaleValue(m_fractional));
}
fixed(IntegerType integerVal, FractionalType fractionalVal, uint32_t leadingZeros)
: m_integer(__checkIntOverflow(integerVal)),
m_fractional(__checkFracOverflow(fractionalVal))
{
// Scale the fractional value appropriately
m_fractional = __checkFracOverflow(m_fractional *
getFracScaleValue(m_fractional,
leadingZeros));
11ae9ac5 David Sorber
}
c7e0510c David Sorber
835f4592 David Sorber
// Default copy constructor, and assignment operator
c7e0510c David Sorber
fixed(const fixed&) = default;
fixed& operator=(const fixed&) = default;

92782e4a David Sorber
// Reassign value to type with a prescaled fractional value.
11ae9ac5 David Sorber
void assign(IntegerType integerVal, FractionalType fractionalVal)
{
m_integer = __checkIntOverflow(integerVal);
92782e4a David Sorber
// Fractional value is prescaled to fractional type precision
m_fractional = __checkFracOverflow(fractionalVal);
}
c7e0510c David Sorber
92782e4a David Sorber
// Assign a new value with an *unscaled* fractional part (NOTE: use the
// "leadingZeros" parameter to represent the number of leading zeros in the
// unscaled fractional part.
void assignUnscaledFrac(
IntegerType integerVal,
FractionalType fractionalVal,
uint32_t leadingZeros=0)
{
m_integer = __checkIntOverflow(integerVal);
// Scale the fractional value appropriately
m_fractional = __checkFracOverflow(fractionalVal *
getFracScaleValue(fractionalVal,
leadingZeros));
11ae9ac5 David Sorber
}
c7e0510c David Sorber
11ae9ac5 David Sorber
// Parse value from string input
7fa2f28e David Sorber
uint32_t parse(const char* input)
835f4592 David Sorber
{
11ae9ac5 David Sorber
char* endPtr = nullptr;
852d0468 David Sorber
m_integer = __checkIntOverflow(strto_inttype(input, &endPtr, 10));
11ae9ac5 David Sorber
m_fractional = 0;
c7e0510c David Sorber
11ae9ac5 David Sorber
if (std::isdigit(*endPtr))
{
throw std::out_of_range("Integer value is out of range.");
}
c7e0510c David Sorber
11ae9ac5 David Sorber
// If the ending char is a period we can now parse the fractional part
if (*endPtr == '.')
{
char* fracEndPtr = nullptr;
c7e0510c David Sorber
FractionalType fracTemp =
852d0468 David Sorber
__checkFracOverflow(strto_fractype(endPtr + 1, &fracEndPtr, 10));
92782e4a David Sorber
11ae9ac5 David Sorber
uint32_t fracLen = (fracEndPtr - endPtr) - 1;
fracLen = fractional_decimal_digits - fracLen;
c7e0510c David Sorber
11ae9ac5 David Sorber
m_fractional = __checkFracOverflow(fracTemp * SCALE_VALUES[fracLen]);
7fa2f28e David Sorber
endPtr = fracEndPtr;
11ae9ac5 David Sorber
}
c7e0510c David Sorber
// Calculate and return overall length
7fa2f28e David Sorber
return (endPtr - input);
835f4592 David Sorber
}
c7e0510c David Sorber
92782e4a David Sorber
void absval()
{
m_integer = abs(m_integer);
}

835f4592 David Sorber
constexpr inline fixed operator-() const noexcept = delete;
11ae9ac5 David Sorber
constexpr inline fixed operator!() const noexcept = delete;
constexpr inline fixed operator~() const noexcept = delete;
835f4592 David Sorber
inline fixed& operator+=(const fixed& y) noexcept = delete;
inline fixed& operator-=(const fixed& y) noexcept = delete;
inline fixed& operator*=(const fixed& y) noexcept = delete;
c7e0510c David Sorber
inline fixed& operator/=(const fixed& y) noexcept = delete;

835f4592 David Sorber
// Comparison operators
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
friend constexpr inline bool operator==(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept;
c7e0510c David Sorber
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
friend constexpr inline bool operator!=(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept;
c7e0510c David Sorber
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
friend constexpr inline bool operator<(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept;
c7e0510c David Sorber
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
friend constexpr inline bool operator>(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept;
c7e0510c David Sorber
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
friend constexpr inline bool operator<=(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept;
c7e0510c David Sorber
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
friend constexpr inline bool operator>=(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept;
c7e0510c David Sorber
92782e4a David Sorber
// Divide by unsigned int
template<typename I, typename F, typename STI, typename STF>
friend constexpr inline fixed<I, F, STI, STF> operator/(
const fixed<I, F, STI, STF>& x, const uint64_t& y);

11ae9ac5 David Sorber
// Output stream operator
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
friend inline std::ostream& operator<<(
852d0468 David Sorber
std::ostream& os, const fixed<I, F, STI, STF>& rhs);
c7e0510c David Sorber
92782e4a David Sorber
835f4592 David Sorber
private:

852d0468 David Sorber
static StrToIntegerTypeFunc strto_inttype;
static StrToFracTypeFunc strto_fractype;

11ae9ac5 David Sorber
static inline IntegerType __checkIntOverflow(IntegerType integerVal)
{
7fa2f28e David Sorber
if (__unlikely(integerVal > MAX_INTEGER_VALUE))
11ae9ac5 David Sorber
{
std::ostringstream msg;
msg << "Integer value: " << integerVal << " exceeds maximum "
c7e0510c David Sorber
<< "integer range of type (" << MAX_INTEGER_VALUE << ")!";
11ae9ac5 David Sorber
throw std::out_of_range(msg.str());
}
return integerVal;
}
c7e0510c David Sorber
11ae9ac5 David Sorber
static inline FractionalType __checkFracOverflow(FractionalType fractionalVal)
{
7fa2f28e David Sorber
if (__unlikely(fractionalVal > MAX_FRACTIONAL_VALUE))
11ae9ac5 David Sorber
{
std::ostringstream msg;
msg << "Fractional value: " << fractionalVal << " exceeds maximum "
c7e0510c David Sorber
<< "fractional range of type (" << MAX_FRACTIONAL_VALUE << ")!";
11ae9ac5 David Sorber
throw std::out_of_range(msg.str());
}
return fractionalVal;
}
92782e4a David Sorber
public:
// Helper function to get the approprate scale value for an unscaled input
// value of the fractional type
static inline FractionalType getFracScaleValue(
FractionalType value,
uint32_t leadingZeros=0)
{
uint32_t index = fractional_decimal_digits -
(DIGIT_LOOKUP_TABLE.upper_bound(value)->second +
leadingZeros);
return SCALE_VALUES[index];
}
11ae9ac5 David Sorber
92782e4a David Sorber
public:
835f4592 David Sorber
IntegerType m_integer;
FractionalType m_fractional;
};

852d0468 David Sorber
// Functor for calling std::strtoll()
struct strtoll_ftor {
inline int64_t operator()(const char* str, char** str_end, int base)
{
return std::strtoll(str, str_end, base);
}
};

// Functor for calling std::strtoull()
struct strtoull_ftor {
inline uint64_t operator()(const char* str, char** str_end, int base)
{
return std::strtoull(str, str_end, base);
}
};

11ae9ac5 David Sorber
// Predefined types
852d0468 David Sorber
using fixed_8_8 = fixed<int8_t, uint8_t, strtoll_ftor, strtoull_ftor>;
using fixed_8_16 = fixed<int8_t, uint16_t, strtoll_ftor, strtoull_ftor>;
using fixed_8_32 = fixed<int8_t, uint32_t, strtoll_ftor, strtoull_ftor>;
using fixed_8_64 = fixed<int8_t, uint64_t, strtoll_ftor, strtoull_ftor>;

using fixed_16_8 = fixed<int16_t, uint8_t, strtoll_ftor, strtoull_ftor>;
using fixed_16_16 = fixed<int16_t, uint16_t, strtoll_ftor, strtoull_ftor>;
using fixed_16_32 = fixed<int16_t, uint32_t, strtoll_ftor, strtoull_ftor>;
using fixed_16_64 = fixed<int16_t, uint64_t, strtoll_ftor, strtoull_ftor>;

using fixed_32_8 = fixed<int32_t, uint8_t, strtoll_ftor, strtoull_ftor>;
using fixed_32_16 = fixed<int32_t, uint16_t, strtoll_ftor, strtoull_ftor>;
using fixed_32_32 = fixed<int32_t, uint32_t, strtoll_ftor, strtoull_ftor>;
using fixed_32_64 = fixed<int32_t, uint64_t, strtoll_ftor, strtoull_ftor>;

using fixed_64_8 = fixed<int64_t, uint8_t, strtoll_ftor, strtoull_ftor>;
using fixed_64_16 = fixed<int64_t, uint16_t, strtoll_ftor, strtoull_ftor>;
using fixed_64_32 = fixed<int64_t, uint32_t, strtoll_ftor, strtoull_ftor>;
using fixed_64_64 = fixed<int64_t, uint64_t, strtoll_ftor, strtoull_ftor>;

11ae9ac5 David Sorber
// Precomputed scale value constants
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
const uint64_t fixed<I, F, STI, STF>::SCALE_VALUES[20] =
11ae9ac5 David Sorber
{
c7e0510c David Sorber
/* 0 */ 1ULL,
/* 1 */ 10ULL,
/* 2 */ 100ULL,
11ae9ac5 David Sorber
/* 3 */ 1000ULL,
/* 4 */ 10000ULL,
/* 5 */ 100000ULL,
/* 6 */ 1000000ULL,
/* 7 */ 10000000ULL,
/* 8 */ 100000000ULL,
/* 9 */ 1000000000ULL,
/* 10 */ 10000000000ULL,
/* 11 */ 100000000000ULL,
/* 12 */ 1000000000000ULL,
/* 13 */ 10000000000000ULL,
/* 14 */ 100000000000000ULL,
/* 15 */ 1000000000000000ULL,
/* 16 */ 10000000000000000ULL,
/* 17 */ 100000000000000000ULL,
/* 18 */ 1000000000000000000ULL,
/* 19 */ 10000000000000000000ULL
};

92782e4a David Sorber
template<typename I, typename F, typename STI, typename STF>
const std::map<uint64_t, uint32_t> fixed<I, F, STI, STF>::DIGIT_LOOKUP_TABLE{
{1ULL, 0},
{10ULL, 1},
{100ULL, 2},
{1000ULL, 3},
{10000ULL, 4},
{100000ULL, 5},
{1000000ULL, 6},
{10000000ULL, 7},
{100000000ULL, 8},
{1000000000ULL, 9},
{10000000000ULL, 10},
{100000000000ULL, 11},
{1000000000000ULL, 12},
{10000000000000ULL, 13},
{100000000000000ULL, 14},
{1000000000000000ULL, 15},
{10000000000000000ULL, 16},
{100000000000000000ULL, 17},
{1000000000000000000ULL, 18},
{std::numeric_limits<uint64_t>::max(), 19} // Special case for uint64_t max value
};

852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
constexpr inline bool operator==(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept
835f4592 David Sorber
{
return (x.m_integer == y.m_integer && x.m_fractional == y.m_fractional);
}

852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
constexpr inline bool operator!=(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept
835f4592 David Sorber
{
11ae9ac5 David Sorber
return (! (x == y));
835f4592 David Sorber
}

852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
constexpr inline bool operator<(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept
835f4592 David Sorber
{
if (x.m_integer == y.m_integer)
{
return (x.m_fractional < y.m_fractional);
}
else
{
return (x.m_integer < y.m_integer);
}
c7e0510c David Sorber
return false;
835f4592 David Sorber
}

852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
constexpr inline bool operator>(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept
835f4592 David Sorber
{
if (x.m_integer == y.m_integer)
{
return (x.m_fractional > y.m_fractional);
}
else
{
return (x.m_integer > y.m_integer);
}
c7e0510c David Sorber
return false;
835f4592 David Sorber
}

852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
constexpr inline bool operator<=(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept
835f4592 David Sorber
{
if (x.m_integer == y.m_integer)
{
return (x.m_fractional <= y.m_fractional);
}
else
{
return (x.m_integer < y.m_integer);
}
c7e0510c David Sorber
return false;
835f4592 David Sorber
}

852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
c7e0510c David Sorber
constexpr inline bool operator>=(
852d0468 David Sorber
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept
835f4592 David Sorber
{
if (x.m_integer == y.m_integer)
{
11ae9ac5 David Sorber
return (x.m_fractional >= y.m_fractional);
835f4592 David Sorber
}
else
{
return (x.m_integer > y.m_integer);
}
c7e0510c David Sorber
835f4592 David Sorber
return false;
}

11ae9ac5 David Sorber
// Addition -- not yet supported
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
constexpr inline fixed<I, F, STI, STF> operator+(
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept = delete;
835f4592 David Sorber
11ae9ac5 David Sorber
// Subtraction -- not yet supported
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
constexpr inline fixed<I, F, STI, STF> operator-(
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept = delete;
835f4592 David Sorber
92782e4a David Sorber
// Multiplication -- not yet supported
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
constexpr inline fixed<I, F, STI, STF> operator*(
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept = delete;
835f4592 David Sorber
11ae9ac5 David Sorber
// Division -- not yet supported
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
constexpr inline fixed<I, F, STI, STF> operator/(
const fixed<I, F, STI, STF>& x, const fixed<I, F, STI, STF>& y) noexcept = delete;
835f4592 David Sorber
92782e4a David Sorber
// Division by unsigned int
template<typename I, typename F, typename STI, typename STF>
constexpr inline fixed<I, F, STI, STF> operator/(
const fixed<I, F, STI, STF>& x, const uint64_t& y)
{
fixed<I, F, STI, STF> newVal;
newVal.m_integer = x.m_integer / y;
newVal.m_fractional = x.m_integer % y;
newVal.m_fractional *= x.getFracScaleValue(newVal.m_fractional);
newVal.m_fractional += x.m_fractional / y;
return newVal;
}

11ae9ac5 David Sorber
// Stream output
852d0468 David Sorber
template<typename I, typename F, typename STI, typename STF>
inline std::ostream& operator<<(std::ostream& os, const fixed<I, F, STI, STF>& rhs)
11ae9ac5 David Sorber
{
92782e4a David Sorber
#if 0
// Print full fractional precision always (even with trailing zeros)
c7e0510c David Sorber
return os << std::fixed << rhs.m_integer << "."
852d0468 David Sorber
<< std::setw(fixed<I, F, STI, STF>::fractional_decimal_digits)
c7e0510c David Sorber
<< std::setfill('0') << rhs.m_fractional;
92782e4a David Sorber
#else
// Print fractional part without trailing zeros
char buffer[fixed<I, F, STI, STF>::fractional_decimal_digits + 1]{};
std::snprintf(buffer, fixed<I, F, STI, STF>::fractional_decimal_digits + 1,
"%" PRIu64, rhs.m_fractional);
// NOTE: if fractional part is zero always print at least one zero
int idx = fixed<I, F, STI, STF>::fractional_decimal_digits;
bool found = false;
for (; idx >= 0; --idx)
{
if (std::isdigit(buffer[idx]) && buffer[idx] != '0')
{
found = true;
break;
}
}
if (found)
{
buffer[idx + 1] = '\0';
}
uint32_t leadingZeros =
fixed<I, F, STI, STF>::fractional_decimal_digits -
fixed<I, F, STI, STF>::DIGIT_LOOKUP_TABLE.upper_bound(rhs.m_fractional)->second;
// Output any leading zeros
os << std::fixed << rhs.m_integer << ".";
for (uint32_t idx = 0; idx < leadingZeros; ++idx)
{
os << "0";
}
return os << buffer;
#endif
11ae9ac5 David Sorber
}

c7e0510c David Sorber
#endif // FIXED_H_