14 #ifndef SST_CORE_DECIMAL_FIXEDPOINT_H
15 #define SST_CORE_DECIMAL_FIXEDPOINT_H
17 #include "sst/core/from_string.h"
23 #include <type_traits>
40 template <
int whole_words,
int fraction_words>
45 static constexpr uint32_t storage_radix = 100000000;
46 static constexpr uint64_t storage_radix_long = 100000000l;
47 static constexpr int32_t digits_per_word = 8;
61 template <
int A,
int B>
62 friend class sst_dec_fixed;
73 uint32_t data[whole_words + fraction_words];
87 void from_string(
const std::string& init_str)
89 std::string init(init_str);
91 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
96 if ( init[0] ==
'-' ) {
98 init = init.substr(1, init.npos);
102 size_t exponent_pos = init.find_last_of(
"eE");
103 int32_t exponent = 0;
104 if ( exponent_pos != init.npos ) {
105 exponent =
static_cast<int32_t
>(SST::Core::from_string<double>(init.substr(exponent_pos + 1, init.npos)));
106 init = init.substr(0, exponent_pos);
109 int dp = init.length();
110 for (
size_t i = 0; i < init.length(); ++i ) {
111 if ( init[i] ==
'.' ) { dp = i; }
118 int start_of_digits = (fraction_words * digits_per_word) - (init.length() - dp) + exponent;
122 int start_pos_word = start_of_digits % digits_per_word;
124 for (
int i = 0; i < start_pos_word; i++ ) {
128 for (
int i = init.length() - 1; i >= 0; --i ) {
129 int digit = start_of_digits + (init.length() - 1 - i);
130 int word = (digit / digits_per_word);
132 data[word] += (SST::Core::from_string<uint32_t>(init.substr(i, 1)) * mult);
134 if ( mult == storage_radix ) mult = 1;
143 void from_uint64(uint64_t init)
147 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
160 for (
int i = fraction_words; i < whole_words + fraction_words; ++i ) {
161 data[i] = init % storage_radix_long;
162 init /= storage_radix_long;
171 void from_double(
double init)
175 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
180 for (
int i = 0; i < whole_words - 1; ++i ) {
181 factor *= storage_radix;
184 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
185 data[i] =
static_cast<uint32_t
>(init / factor);
186 init -= (data[i] * factor);
187 factor /= storage_radix;
200 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
232 T init,
typename std::enable_if<std::is_signed<T>::value && std::is_integral<T>::value>::type* =
nullptr)
249 decimal_fixedpoint(
const T init,
typename std::enable_if<std::is_floating_point<T>::value>::type* =
nullptr)
261 negative = init.negative;
262 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
263 data[i] = init.data[i];
272 negative = v.negative;
273 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
324 void negate() { negative = negative ^ 0x1; }
334 for (
int i = 0; i < fraction_words; ++i ) {
335 factor /= storage_radix;
338 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
339 ret += (
static_cast<double>(data[i]) * factor);
340 factor *= storage_radix;
354 for (
int i = 0; i < whole_words; ++i ) {
355 ret += (
static_cast<int64_t
>(data[fraction_words + i]) * factor);
356 factor *= storage_radix;
361 if ( data[fraction_words - 1] > (storage_radix / 2) ) { round =
true; }
362 else if ( data[fraction_words - 1] == (storage_radix / 2) ) {
363 for (
int i = fraction_words - 2; i >= 0; --i ) {
364 if ( data[i] != 0 ) {
373 if ( ret % 2 == 1 ) round =
true;
377 if ( negative ) ret = -ret;
389 for (
int i = 0; i < whole_words; ++i ) {
390 ret += (
static_cast<int64_t
>(data[i]) * factor);
391 factor *= storage_radix;
401 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
402 if ( data[i] != 0 )
return false;
410 template <
typename T>
411 T
convert_to(
typename std::enable_if<std::is_unsigned<T>::value>::type* = 0)
const
419 template <
typename T>
420 T
convert_to(
typename std::enable_if<std::is_signed<T>::value && std::is_integral<T>::value>::type* = 0)
const
422 return static_cast<T
>(
toLong());
428 template <
typename T>
429 T
convert_to(
typename std::enable_if<std::is_floating_point<T>::value>::type* = 0)
const
443 std::stringstream stream;
444 if ( precision <= 0 || precision > ((whole_words + fraction_words) * digits_per_word) )
445 precision = (whole_words + fraction_words) * digits_per_word;
449 constexpr
int num_digits = (whole_words + fraction_words) * digits_per_word;
451 unsigned char digits[num_digits];
452 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
453 uint32_t value = data[i];
454 for (
int j = 0; j < digits_per_word; ++j ) {
455 digits[i * digits_per_word + j] = value % 10;
461 int first_non_zero = -1;
462 for (
int i = num_digits - 1; i >= 0; --i ) {
463 if ( digits[i] != 0 ) {
470 if ( first_non_zero == -1 )
return "0";
473 int round_position = first_non_zero - precision;
478 if ( round_position >= 0 ) {
479 if ( digits[round_position] > 5 )
481 else if ( digits[round_position] < 5 )
485 for (
int i = round_position - 1; i >= 0; --i ) {
486 if ( digits[i] != 0 ) {
494 if ( digits[round_position + 1] % 2 == 1 ) round =
true;
500 unsigned char carry = 1;
501 for (
int i = round_position + 1; i < num_digits; ++i ) {
503 carry = digits[i] / 10;
504 digits[i] = digits[i] % 10;
509 for (
int i = 0; i <= round_position; ++i ) {
515 if ( negative ) stream <<
'-';
519 for (
int i = num_digits - 1; i >= 0; --i ) {
520 if ( digits[i] != 0 ) {
527 if ( first_non_zero == -1 ) {
530 stream <<
"1e+" << (whole_words * digits_per_word);
537 if ( first_non_zero >= ((fraction_words * digits_per_word) + precision) ) {
539 int exponent = first_non_zero - (fraction_words * digits_per_word);
540 stream << static_cast<uint32_t>(digits[first_non_zero]) <<
".";
542 for (
int i = first_non_zero - 1; i >= first_non_zero - precision; --i ) {
546 if ( digits[i] == 0 )
549 for (
int j = 0; j < zeros; ++j ) {
552 stream << static_cast<uint32_t>(digits[i]);
556 std::string ret = stream.str();
557 if ( ret[ret.length() - 1] ==
'.' ) {
558 ret = ret.substr(0, ret.length() - 1);
559 stream.str(std::string(
""));
562 stream <<
"e+" << std::setfill(
'0') << std::setw(2) << exponent;
567 else if ( first_non_zero >= (fraction_words * digits_per_word) ) {
569 for (
int i = first_non_zero; i >= (fraction_words * digits_per_word); --i ) {
571 stream << static_cast<uint32_t>(digits[i]);
576 for (
int i = (fraction_words * digits_per_word) - 1; i >= first_non_zero - precision && (i >= 0); --i ) {
580 if ( digits[i] == 0 )
583 for (
int j = 0; j < zeros; ++j ) {
586 stream << static_cast<uint32_t>(digits[i]);
590 std::string ret = stream.str();
591 if ( ret[ret.length() - 1] ==
'.' ) {
592 ret = ret.substr(0, ret.length() - 1);
593 stream.str(std::string(
""));
601 else if ( first_non_zero > (fraction_words * digits_per_word) - 5 ) {
603 for (
int i = (fraction_words * digits_per_word) - 1; i > first_non_zero; --i ) {
607 for (
int i = first_non_zero; (i >= first_non_zero - precision) && (i >= 0); --i ) {
611 if ( digits[i] == 0 )
614 for (
int j = 0; j < zeros; ++j ) {
617 stream << static_cast<uint32_t>(digits[i]);
625 int exponent = first_non_zero - (fraction_words * digits_per_word);
626 exponent = -exponent;
627 stream << static_cast<uint32_t>(digits[first_non_zero]) <<
".";
629 for (
int i = first_non_zero - 1; (i >= first_non_zero - precision) && (i >= 0); --i ) {
633 if ( digits[i] == 0 )
636 for (
int j = 0; j < zeros; ++j ) {
639 stream << static_cast<uint32_t>(digits[i]);
643 std::string ret = stream.str();
644 if ( ret[ret.length() - 1] ==
'.' ) {
645 ret = ret.substr(0, ret.length() - 1);
646 stream.str(std::string(
""));
649 stream <<
"e-" << std::setfill(
'0') << std::setw(2) << exponent;
664 if ( (negative ^ v.negative) == 0 ) {
667 uint64_t carry_over = 0;
668 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
669 uint64_t value =
static_cast<uint64_t
>(data[i]) +
static_cast<uint64_t
>(v.data[i]) + carry_over;
670 carry_over = value / storage_radix;
672 data[i] = value % storage_radix;
678 if (
operator>=(v) ) {
681 uint64_t carry_over = 1;
682 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
683 uint64_t
negate =
static_cast<uint64_t
>(storage_radix - 1) -
static_cast<uint64_t
>(v.data[i]);
685 uint64_t value =
static_cast<uint64_t
>(data[i]) +
negate + carry_over;
686 carry_over = value / storage_radix;
687 data[i] =
static_cast<uint32_t
>(value % storage_radix);
694 uint64_t carry_over = 1;
695 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
696 uint64_t
negate =
static_cast<uint64_t
>(storage_radix - 1) -
static_cast<uint64_t
>(data[i]);
698 uint64_t value =
static_cast<uint64_t
>(v.data[i]) +
negate + carry_over;
699 carry_over = value / storage_radix;
700 data[i] =
static_cast<uint32_t
>(value % storage_radix);
703 negative = v.negative;
735 uint64_t carry_over = 0;
736 for (
int i = 0; i < fraction_words; ++i ) {
737 uint64_t sum = carry_over;
738 for (
int j = 0; j <= i; ++j ) {
739 sum +=
static_cast<uint64_t
>(me.data[j]) *
static_cast<uint64_t
>(v.data[i - j]);
741 carry_over = sum / storage_radix_long;
745 for (
int i = fraction_words; i < whole_words + fraction_words; ++i ) {
746 uint64_t sum = carry_over;
747 for (
int j = 0; j <= i; ++j ) {
748 sum +=
static_cast<uint64_t
>(me.data[j]) *
static_cast<uint64_t
>(v.data[i - j]);
750 carry_over = sum / storage_radix_long;
751 data[i - fraction_words] =
static_cast<uint32_t
>(sum % storage_radix_long);
754 for (
int i = 0; i < fraction_words; ++i ) {
755 uint64_t sum = carry_over;
756 for (
int j = i + 1; j < whole_words + fraction_words; ++j ) {
757 sum +=
static_cast<uint64_t
>(me.data[j]) *
758 static_cast<uint64_t
>(v.data[whole_words + fraction_words + i - j]);
760 carry_over = sum / storage_radix_long;
761 data[i + whole_words] =
static_cast<uint32_t
>(sum % storage_radix_long);
764 negative = negative ^ v.negative;
803 int digits_of_prec = std::numeric_limits<double>::digits10 / 2;
806 for (
int i = digits_of_prec; i <= (whole_words + fraction_words) * digits_per_word; i *= 2 ) {
825 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
826 if ( data[i] != v.data[i] )
return false;
838 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
839 if ( data[i] != v.data[i] )
return true;
851 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
852 if ( data[i] > v.data[i] )
return true;
853 if ( data[i] < v.data[i] )
return false;
866 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
867 if ( data[i] > v.data[i] )
return true;
868 if ( data[i] < v.data[i] )
return false;
880 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
881 if ( data[i] < v.data[i] )
return true;
882 if ( data[i] > v.data[i] )
return false;
895 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
896 if ( data[i] < v.data[i] )
return true;
897 if ( data[i] > v.data[i] )
return false;
903 template <
int whole_words,
int fraction_words>
904 decimal_fixedpoint<whole_words, fraction_words>
905 operator+(decimal_fixedpoint<whole_words, fraction_words> lhs, decimal_fixedpoint<whole_words, fraction_words> rhs)
907 decimal_fixedpoint<whole_words, fraction_words> ret(lhs);
911 template <
int whole_words,
int fraction_words>
912 decimal_fixedpoint<whole_words, fraction_words>
913 operator-(decimal_fixedpoint<whole_words, fraction_words> lhs, decimal_fixedpoint<whole_words, fraction_words> rhs)
915 decimal_fixedpoint<whole_words, fraction_words> ret(lhs);
919 template <
int whole_words,
int fraction_words>
920 decimal_fixedpoint<whole_words, fraction_words>
921 operator*(decimal_fixedpoint<whole_words, fraction_words> lhs, decimal_fixedpoint<whole_words, fraction_words> rhs)
923 decimal_fixedpoint<whole_words, fraction_words> ret(lhs);
927 template <
int whole_words,
int fraction_words>
928 decimal_fixedpoint<whole_words, fraction_words>
929 operator/(decimal_fixedpoint<whole_words, fraction_words> lhs, decimal_fixedpoint<whole_words, fraction_words> rhs)
931 decimal_fixedpoint<whole_words, fraction_words> ret(lhs);
935 template <
int whole_words,
int fraction_words,
typename T>
937 operator==(
const T& lhs,
const decimal_fixedpoint<whole_words, fraction_words>& rhs)
939 return rhs == decimal_fixedpoint<whole_words, fraction_words>(lhs);
942 template <
int whole_words,
int fraction_words,
typename T>
944 operator!=(
const T& lhs,
const decimal_fixedpoint<whole_words, fraction_words>& rhs)
946 return rhs != decimal_fixedpoint<whole_words, fraction_words>(lhs);
949 template <
int whole_words,
int fraction_words>
951 operator<<(std::ostream& os,
const decimal_fixedpoint<whole_words, fraction_words>& rhs)
953 os << rhs.toString(os.precision());
Class that implements a decimal fixed-point number.
Definition: decimal_fixedpoint.h:42
decimal_fixedpoint(const std::string &init)
Build a decimal_fixedpoint using a string initializer.
Definition: decimal_fixedpoint.h:212
T convert_to(typename std::enable_if< std::is_unsigned< T >::value >::type *=0) const
Templated conversion function for unsigned types.
Definition: decimal_fixedpoint.h:411
decimal_fixedpoint & operator+=(const decimal_fixedpoint &v)
Adds another number to this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:661
decimal_fixedpoint(T init, typename std::enable_if< std::is_unsigned< T >::value >::type *=nullptr)
Build a decimal_fixedpoint using a 64-bit unsigned number.
Definition: decimal_fixedpoint.h:220
std::string toString(int32_t precision=6) const
Create a string representation of this decimal_fixedpoint.
Definition: decimal_fixedpoint.h:440
decimal_fixedpoint(const T init, typename std::enable_if< std::is_floating_point< T >::value >::type *=nullptr)
Build a decimal_fixedpoint using a double.
Definition: decimal_fixedpoint.h:249
bool operator>(const decimal_fixedpoint &v) const
Checks to see if this number is greater than another number.
Definition: decimal_fixedpoint.h:849
decimal_fixedpoint & operator=(const std::string &v)
Equal operator for string.
Definition: decimal_fixedpoint.h:315
decimal_fixedpoint & operator-=(const decimal_fixedpoint &v)
Subtracts another number from this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:714
decimal_fixedpoint & operator=(uint64_t v)
Equal operator for 64-bit unsigned int.
Definition: decimal_fixedpoint.h:282
decimal_fixedpoint(T init, typename std::enable_if< std::is_signed< T >::value &&std::is_integral< T >::value >::type *=nullptr)
Build a decimal_fixedpoint using a 64-bit signed number.
Definition: decimal_fixedpoint.h:231
decimal_fixedpoint(const decimal_fixedpoint &init)
Build a decimal_fixedpoint using another decimal_fixedpoint.
Definition: decimal_fixedpoint.h:259
T convert_to(typename std::enable_if< std::is_signed< T >::value &&std::is_integral< T >::value >::type *=0) const
Templated conversion function for signed integral types.
Definition: decimal_fixedpoint.h:420
bool operator!=(const decimal_fixedpoint &v) const
Checks to see if two numbers are not equal.
Definition: decimal_fixedpoint.h:836
constexpr int getWholeWords() const
Get the value of whole_words template parameter.
Definition: decimal_fixedpoint.h:52
int64_t toLong() const
Return a int64_t version of the decimal_fixedpoint.
Definition: decimal_fixedpoint.h:350
double toDouble() const
Return a double precision version of the decimal_fixedpoint.
Definition: decimal_fixedpoint.h:330
decimal_fixedpoint & inverse()
Inverts the number (1 divided by this number)
Definition: decimal_fixedpoint.h:786
decimal_fixedpoint & operator=(int64_t v)
Equal operator for 64-bit signed int.
Definition: decimal_fixedpoint.h:291
decimal_fixedpoint & operator=(double v)
Equal operator for double.
Definition: decimal_fixedpoint.h:306
bool isZero() const
Return true if value is zero, otherwise return false.
Definition: decimal_fixedpoint.h:399
decimal_fixedpoint()
Default constructor.
Definition: decimal_fixedpoint.h:197
uint64_t toUnsignedLong() const
Return a uint64_t version of the decimal_fixedpoint.
Definition: decimal_fixedpoint.h:385
decimal_fixedpoint & operator=(const decimal_fixedpoint &v)
Equal operator for other decimal_fixedpoint objects.
Definition: decimal_fixedpoint.h:270
bool operator<(const decimal_fixedpoint &v) const
Checks to see if this number is less than another number.
Definition: decimal_fixedpoint.h:878
bool operator==(const decimal_fixedpoint &v) const
Checks to see if two numbers are equal.
Definition: decimal_fixedpoint.h:823
void negate()
Negate the value (change the sign bit).
Definition: decimal_fixedpoint.h:324
decimal_fixedpoint & operator/=(const decimal_fixedpoint &v)
Divides another number from this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:774
T convert_to(typename std::enable_if< std::is_floating_point< T >::value >::type *=0) const
Templated conversion function for floating point types.
Definition: decimal_fixedpoint.h:429
bool operator<=(const decimal_fixedpoint &v) const
Checks to see if this number is less than or equal to another number.
Definition: decimal_fixedpoint.h:893
decimal_fixedpoint & operator*=(const decimal_fixedpoint &v)
Multiplies another number to this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:727
bool operator>=(const decimal_fixedpoint &v) const
Checks to see if this number is greater than or equal to another number.
Definition: decimal_fixedpoint.h:864
constexpr int getFractionWords() const
Get the value of fraction_words template parameter.
Definition: decimal_fixedpoint.h:57