14#ifndef SST_CORE_DECIMAL_FIXEDPOINT_H
15#define SST_CORE_DECIMAL_FIXEDPOINT_H
17#include "sst/core/from_string.h"
43template <
int whole_words,
int fraction_words>
48 static constexpr uint32_t storage_radix = 100000000;
49 static constexpr uint64_t storage_radix_long = 100000000l;
50 static constexpr int32_t digits_per_word = 8;
64 template <
int A,
int B>
65 friend class sst_dec_fixed;
76 uint32_t data[whole_words + fraction_words];
90 void from_string(
const std::string& init_str)
92 std::string init(init_str);
94 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
99 if ( init[0] ==
'-' ) {
101 init = init.substr(1, init.npos);
105 size_t exponent_pos = init.find_last_of(
"eE");
106 int32_t exponent = 0;
107 if ( exponent_pos != init.npos ) {
108 exponent =
static_cast<int32_t
>(SST::Core::from_string<double>(init.substr(exponent_pos + 1, init.npos)));
109 init = init.substr(0, exponent_pos);
112 int dp = init.length();
113 for (
size_t i = 0; i < init.length(); ++i ) {
114 if ( init[i] ==
'.' ) {
123 int start_of_digits = (fraction_words * digits_per_word) - (init.length() - dp) + exponent;
127 int start_pos_word = start_of_digits % digits_per_word;
129 for (
int i = 0; i < start_pos_word; i++ ) {
133 for (
int i = init.length() - 1; i >= 0; --i ) {
134 int digit = start_of_digits + (init.length() - 1 - i);
135 int word = (digit / digits_per_word);
137 data[word] += (SST::Core::from_string<uint32_t>(init.substr(i, 1)) * mult);
139 if ( mult == storage_radix ) mult = 1;
148 void from_uint64(uint64_t init)
152 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
165 for (
int i = fraction_words; i < whole_words + fraction_words; ++i ) {
166 data[i] = init % storage_radix_long;
167 init /= storage_radix_long;
176 void from_double(
double init)
180 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
185 for (
int i = 0; i < whole_words - 1; ++i ) {
186 factor *= storage_radix;
189 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
190 data[i] =
static_cast<uint32_t
>(init / factor);
191 init -= (data[i] * factor);
192 factor /= storage_radix;
205 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
236 decimal_fixedpoint(T init, std::enable_if_t<std::is_signed_v<T> && std::is_integral_v<T>>* =
nullptr)
265 negative = init.negative;
266 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
267 data[i] = init.data[i];
276 if ( &v ==
this )
return *
this;
277 negative = v.negative;
278 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
329 void negate() { negative = negative ^ 0x1; }
339 for (
int i = 0; i < fraction_words; ++i ) {
340 factor /= storage_radix;
343 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
344 ret += (
static_cast<double>(data[i]) * factor);
345 factor *= storage_radix;
359 for (
int i = 0; i < whole_words; ++i ) {
360 ret += (
static_cast<int64_t
>(data[fraction_words + i]) * factor);
361 factor *= storage_radix;
366 if ( data[fraction_words - 1] > (storage_radix / 2) ) {
369 else if ( data[fraction_words - 1] == (storage_radix / 2) ) {
370 for (
int i = fraction_words - 2; i >= 0; --i ) {
371 if ( data[i] != 0 ) {
380 if ( ret % 2 == 1 ) round =
true;
384 if ( negative ) ret = -ret;
396 for (
int i = 0; i < whole_words; ++i ) {
397 ret += (
static_cast<int64_t
>(data[i]) * factor);
398 factor *= storage_radix;
408 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
409 if ( data[i] != 0 )
return false;
417 template <
typename T>
418 T
convert_to(std::enable_if_t<std::is_unsigned_v<T>>* =
nullptr)
const
426 template <
typename T>
427 T
convert_to(std::enable_if_t<std::is_signed_v<T> && std::is_integral_v<T>>* =
nullptr)
const
429 return static_cast<T
>(
toLong());
435 template <
typename T>
436 T
convert_to(std::enable_if_t<std::is_floating_point_v<T>>* =
nullptr)
const
450 std::stringstream stream;
451 if ( precision <= 0 || precision > ((whole_words + fraction_words) * digits_per_word) )
452 precision = (whole_words + fraction_words) * digits_per_word;
456 constexpr int num_digits = (whole_words + fraction_words) * digits_per_word;
458 unsigned char digits[num_digits];
459 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
460 uint32_t value = data[i];
461 for (
int j = 0; j < digits_per_word; ++j ) {
462 digits[i * digits_per_word + j] = value % 10;
468 int first_non_zero = -1;
469 for (
int i = num_digits - 1; i >= 0; --i ) {
470 if ( digits[i] != 0 ) {
477 if ( first_non_zero == -1 )
return "0";
480 int round_position = first_non_zero - precision;
485 if ( round_position >= 0 ) {
486 if ( digits[round_position] > 5 )
488 else if ( digits[round_position] < 5 )
492 for (
int i = round_position - 1; i >= 0; --i ) {
493 if ( digits[i] != 0 ) {
501 if ( digits[round_position + 1] % 2 == 1 ) round =
true;
507 unsigned char carry = 1;
508 for (
int i = round_position + 1; i < num_digits; ++i ) {
510 carry = digits[i] / 10;
511 digits[i] = digits[i] % 10;
516 for (
int i = 0; i <= round_position; ++i ) {
522 if ( negative ) stream <<
'-';
526 for (
int i = num_digits - 1; i >= 0; --i ) {
527 if ( digits[i] != 0 ) {
534 if ( first_non_zero == -1 ) {
537 stream <<
"1e+" << (whole_words * digits_per_word);
544 if ( first_non_zero >= ((fraction_words * digits_per_word) + precision) ) {
546 int exponent = first_non_zero - (fraction_words * digits_per_word);
547 stream << static_cast<uint32_t>(digits[first_non_zero]) <<
".";
549 for (
int i = first_non_zero - 1; i >= first_non_zero - precision; --i ) {
553 if ( digits[i] == 0 )
556 for (
int j = 0; j < zeros; ++j ) {
559 stream << static_cast<uint32_t>(digits[i]);
563 std::string ret = stream.str();
564 if ( ret[ret.length() - 1] ==
'.' ) {
565 ret = ret.substr(0, ret.length() - 1);
566 stream.str(std::string(
""));
569 stream <<
"e+" << std::setfill(
'0') << std::setw(2) << exponent;
574 else if ( first_non_zero >= (fraction_words * digits_per_word) ) {
576 for (
int i = first_non_zero; i >= (fraction_words * digits_per_word); --i ) {
578 stream << static_cast<uint32_t>(digits[i]);
583 for (
int i = (fraction_words * digits_per_word) - 1; i >= first_non_zero - precision && (i >= 0); --i ) {
587 if ( digits[i] == 0 )
590 for (
int j = 0; j < zeros; ++j ) {
593 stream << static_cast<uint32_t>(digits[i]);
597 std::string ret = stream.str();
598 if ( ret[ret.length() - 1] ==
'.' ) {
599 ret = ret.substr(0, ret.length() - 1);
600 stream.str(std::string(
""));
608 else if ( first_non_zero > (fraction_words * digits_per_word) - 5 ) {
610 for (
int i = (fraction_words * digits_per_word) - 1; i > first_non_zero; --i ) {
614 for (
int i = first_non_zero; (i >= first_non_zero - precision) && (i >= 0); --i ) {
618 if ( digits[i] == 0 )
621 for (
int j = 0; j < zeros; ++j ) {
624 stream << static_cast<uint32_t>(digits[i]);
632 int exponent = first_non_zero - (fraction_words * digits_per_word);
633 exponent = -exponent;
634 stream << static_cast<uint32_t>(digits[first_non_zero]) <<
".";
636 for (
int i = first_non_zero - 1; (i >= first_non_zero - precision) && (i >= 0); --i ) {
640 if ( digits[i] == 0 )
643 for (
int j = 0; j < zeros; ++j ) {
646 stream << static_cast<uint32_t>(digits[i]);
650 std::string ret = stream.str();
651 if ( ret[ret.length() - 1] ==
'.' ) {
652 ret = ret.substr(0, ret.length() - 1);
653 stream.str(std::string(
""));
656 stream <<
"e-" << std::setfill(
'0') << std::setw(2) << exponent;
671 if ( (negative ^ v.negative) == 0 ) {
674 uint64_t carry_over = 0;
675 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
676 uint64_t value =
static_cast<uint64_t
>(data[i]) +
static_cast<uint64_t
>(v.data[i]) + carry_over;
677 carry_over = value / storage_radix;
679 data[i] = value % storage_radix;
685 if (
operator>=(v) ) {
688 uint64_t carry_over = 1;
689 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
690 uint64_t
negate =
static_cast<uint64_t
>(storage_radix - 1) -
static_cast<uint64_t
>(v.data[i]);
692 uint64_t value =
static_cast<uint64_t
>(data[i]) +
negate + carry_over;
693 carry_over = value / storage_radix;
694 data[i] =
static_cast<uint32_t
>(value % storage_radix);
701 uint64_t carry_over = 1;
702 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
703 uint64_t
negate =
static_cast<uint64_t
>(storage_radix - 1) -
static_cast<uint64_t
>(data[i]);
705 uint64_t value =
static_cast<uint64_t
>(v.data[i]) +
negate + carry_over;
706 carry_over = value / storage_radix;
707 data[i] =
static_cast<uint32_t
>(value % storage_radix);
710 negative = v.negative;
742 uint64_t carry_over = 0;
743 for (
int i = 0; i < fraction_words; ++i ) {
744 uint64_t sum = carry_over;
745 for (
int j = 0; j <= i; ++j ) {
746 sum +=
static_cast<uint64_t
>(me.data[j]) *
static_cast<uint64_t
>(v.data[i - j]);
748 carry_over = sum / storage_radix_long;
752 for (
int i = fraction_words; i < whole_words + fraction_words; ++i ) {
753 uint64_t sum = carry_over;
754 for (
int j = 0; j <= i; ++j ) {
755 sum +=
static_cast<uint64_t
>(me.data[j]) *
static_cast<uint64_t
>(v.data[i - j]);
757 carry_over = sum / storage_radix_long;
758 data[i - fraction_words] =
static_cast<uint32_t
>(sum % storage_radix_long);
761 for (
int i = 0; i < fraction_words; ++i ) {
762 uint64_t sum = carry_over;
763 for (
int j = i + 1; j < whole_words + fraction_words; ++j ) {
764 sum +=
static_cast<uint64_t
>(me.data[j]) *
765 static_cast<uint64_t
>(v.data[whole_words + fraction_words + i - j]);
767 carry_over = sum / storage_radix_long;
768 data[i + whole_words] =
static_cast<uint32_t
>(sum % storage_radix_long);
771 negative = negative ^ v.negative;
810 int digits_of_prec = std::numeric_limits<double>::digits10 / 2;
813 for (
int i = digits_of_prec; i <= (whole_words + fraction_words) * digits_per_word; i *= 2 ) {
832 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
833 if ( data[i] != v.data[i] )
return false;
845 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
846 if ( data[i] != v.data[i] )
return true;
858 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
859 if ( data[i] > v.data[i] )
return true;
860 if ( data[i] < v.data[i] )
return false;
873 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
874 if ( data[i] > v.data[i] )
return true;
875 if ( data[i] < v.data[i] )
return false;
887 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
888 if ( data[i] < v.data[i] )
return true;
889 if ( data[i] > v.data[i] )
return false;
902 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
903 if ( data[i] < v.data[i] )
return true;
904 if ( data[i] > v.data[i] )
return false;
910template <
int whole_words,
int fraction_words>
911decimal_fixedpoint<whole_words, fraction_words>
912operator+(decimal_fixedpoint<whole_words, fraction_words> lhs, decimal_fixedpoint<whole_words, fraction_words> rhs)
914 decimal_fixedpoint<whole_words, fraction_words> ret(lhs);
918template <
int whole_words,
int fraction_words>
926template <
int whole_words,
int fraction_words>
934template <
int whole_words,
int fraction_words>
942template <
int whole_words,
int fraction_words,
typename T>
949template <
int whole_words,
int fraction_words,
typename T>
956template <
int whole_words,
int fraction_words>
960 os << rhs.toString(os.precision());
Class that implements a decimal fixed-point number.
Definition decimal_fixedpoint.h:45
decimal_fixedpoint(const std::string &init)
Build a decimal_fixedpoint using a string initializer.
Definition decimal_fixedpoint.h:217
std::string toString(int32_t precision=6) const
Create a string representation of this decimal_fixedpoint.
Definition decimal_fixedpoint.h:447
decimal_fixedpoint & inverse()
Inverts the number (1 divided by this number).
Definition decimal_fixedpoint.h:793
bool operator>(const decimal_fixedpoint &v) const
Checks to see if this number is greater than another number.
Definition decimal_fixedpoint.h:856
decimal_fixedpoint(T init, std::enable_if_t< std::is_signed_v< T > &&std::is_integral_v< T > > *=nullptr)
Build a decimal_fixedpoint using a 64-bit signed number.
Definition decimal_fixedpoint.h:236
decimal_fixedpoint & operator=(const std::string &v)
Equal operator for string.
Definition decimal_fixedpoint.h:320
decimal_fixedpoint & operator=(const decimal_fixedpoint &v)
Equal operator for other decimal_fixedpoint objects.
Definition decimal_fixedpoint.h:274
decimal_fixedpoint(const decimal_fixedpoint &init)
Build a decimal_fixedpoint using another decimal_fixedpoint.
Definition decimal_fixedpoint.h:263
T convert_to(std::enable_if_t< std::is_signed_v< T > &&std::is_integral_v< T > > *=nullptr) const
Templated conversion function for signed integral types.
Definition decimal_fixedpoint.h:427
bool operator!=(const decimal_fixedpoint &v) const
Checks to see if two numbers are not equal.
Definition decimal_fixedpoint.h:843
constexpr int getWholeWords() const
Get the value of whole_words template parameter.
Definition decimal_fixedpoint.h:55
int64_t toLong() const
Return a int64_t version of the decimal_fixedpoint.
Definition decimal_fixedpoint.h:355
T convert_to(std::enable_if_t< std::is_unsigned_v< T > > *=nullptr) const
Templated conversion function for unsigned types.
Definition decimal_fixedpoint.h:418
decimal_fixedpoint & operator=(int64_t v)
Equal operator for 64-bit signed int.
Definition decimal_fixedpoint.h:296
double toDouble() const
Return a double precision version of the decimal_fixedpoint.
Definition decimal_fixedpoint.h:335
decimal_fixedpoint & operator+=(const decimal_fixedpoint &v)
Adds another number to this one and sets it equal to the result.
Definition decimal_fixedpoint.h:668
decimal_fixedpoint(T init, std::enable_if_t< std::is_unsigned_v< T > > *=nullptr)
Build a decimal_fixedpoint using a 64-bit unsigned number.
Definition decimal_fixedpoint.h:225
decimal_fixedpoint & operator=(double v)
Equal operator for double.
Definition decimal_fixedpoint.h:311
bool isZero() const
Return true if value is zero, otherwise return false.
Definition decimal_fixedpoint.h:406
decimal_fixedpoint()
Default constructor.
Definition decimal_fixedpoint.h:202
uint64_t toUnsignedLong() const
Return a uint64_t version of the decimal_fixedpoint.
Definition decimal_fixedpoint.h:392
decimal_fixedpoint & operator/=(const decimal_fixedpoint &v)
Divides another number from this one and sets it equal to the result.
Definition decimal_fixedpoint.h:781
bool operator<(const decimal_fixedpoint &v) const
Checks to see if this number is less than another number.
Definition decimal_fixedpoint.h:885
bool operator==(const decimal_fixedpoint &v) const
Checks to see if two numbers are equal.
Definition decimal_fixedpoint.h:830
void negate()
Negate the value (change the sign bit).
Definition decimal_fixedpoint.h:329
decimal_fixedpoint(const T init, std::enable_if_t< std::is_floating_point_v< T > > *=nullptr)
Build a decimal_fixedpoint using a double.
Definition decimal_fixedpoint.h:253
decimal_fixedpoint & operator=(uint64_t v)
Equal operator for 64-bit unsigned int.
Definition decimal_fixedpoint.h:287
decimal_fixedpoint & operator-=(const decimal_fixedpoint &v)
Subtracts another number from this one and sets it equal to the result.
Definition decimal_fixedpoint.h:721
T convert_to(std::enable_if_t< std::is_floating_point_v< T > > *=nullptr) const
Templated conversion function for floating point types.
Definition decimal_fixedpoint.h:436
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:900
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:871
constexpr int getFractionWords() const
Get the value of fraction_words template parameter.
Definition decimal_fixedpoint.h:60
decimal_fixedpoint & operator*=(const decimal_fixedpoint &v)
Multiplies another number to this one and sets it equal to the result.
Definition decimal_fixedpoint.h:734