Revision 7fa2f28e
Added by David Sorber over 4 years ago
| software/fixed/fixed.h | ||
|---|---|---|
|
|
||
|
#ifndef FIXED_H_
|
||
|
#define FIXED_H_
|
||
|
#ifndef __FIXED_H_
|
||
|
#define __FIXED_H_
|
||
|
|
||
|
#include <cctype>
|
||
|
#include <cmath>
|
||
| ... | ... | |
|
#include <sstream>
|
||
|
#include <type_traits>
|
||
|
|
||
|
#if __GNUC__ >= 3
|
||
|
# define __unlikely(cond) __builtin_expect ((cond), 0)
|
||
|
# define __likely(cond) __builtin_expect ((cond), 1)
|
||
|
#else
|
||
|
# define __unlikely(cond) (cond)
|
||
|
# define __likely(cond) (cond)
|
||
|
#endif
|
||
|
|
||
|
template <typename IntegerType, typename FractionalType>
|
||
|
class fixed
|
||
|
{
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
// Parse value from string input
|
||
|
void parse(const char* input)
|
||
|
uint32_t parse(const char* input)
|
||
|
{
|
||
|
char* endPtr = nullptr;
|
||
|
m_integer = __checkIntOverflow(strtoull(input, &endPtr, 10));
|
||
| ... | ... | |
|
//~ std::cout << "invt len: " << fracLen << std::endl;
|
||
|
|
||
|
m_fractional = __checkFracOverflow(fracTemp * SCALE_VALUES[fracLen]);
|
||
|
endPtr = fracEndPtr;
|
||
|
}
|
||
|
// TODO: could calculate and return overall length
|
||
|
//~ uint32_t length = endPtr - input;
|
||
|
return (endPtr - input);
|
||
|
}
|
||
|
|
||
|
constexpr inline fixed operator-() const noexcept = delete;
|
||
| ... | ... | |
|
|
||
|
static inline IntegerType __checkIntOverflow(IntegerType integerVal)
|
||
|
{
|
||
|
if (integerVal > MAX_INTEGER_VALUE)
|
||
|
if (__unlikely(integerVal > MAX_INTEGER_VALUE))
|
||
|
{
|
||
|
std::ostringstream msg;
|
||
|
msg << "Integer value: " << integerVal << " exceeds maximum "
|
||
| ... | ... | |
|
|
||
|
static inline FractionalType __checkFracOverflow(FractionalType fractionalVal)
|
||
|
{
|
||
|
if (fractionalVal > MAX_FRACTIONAL_VALUE)
|
||
|
if (__unlikely(fractionalVal > MAX_FRACTIONAL_VALUE))
|
||
|
{
|
||
|
std::ostringstream msg;
|
||
|
msg << "Fractional value: " << fractionalVal << " exceeds maximum "
|
||
Adding int128.h header with support for GCC specific int128_t and
uint128_t types.