14 #ifndef SST_CORE_DECIMAL_FIXEDPOINT_H
15 #define SST_CORE_DECIMAL_FIXEDPOINT_H
19 #include <type_traits>
21 #include "sst/core/from_string.h"
38 template <
int whole_words,
int fraction_words>
42 static constexpr uint32_t storage_radix = 100000000;
43 static constexpr uint64_t storage_radix_long = 100000000l;
44 static constexpr int32_t digits_per_word = 8;
57 return fraction_words;
63 template <
int A,
int B>
64 friend class sst_dec_fixed;
75 uint32_t data[whole_words + fraction_words];
90 void from_string(
const std::string& init_str) {
91 std::string init(init_str);
93 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
98 if ( init[0] ==
'-' ) {
100 init = init.substr(1,init.npos);
104 size_t exponent_pos = init.find_last_of(
"eE");
105 int32_t exponent = 0;
106 if ( exponent_pos != init.npos ) {
107 exponent =
static_cast<int32_t
>(SST::Core::from_string<double>(init.substr(exponent_pos+1,init.npos)));
108 init = init.substr(0,exponent_pos);
111 int dp = init.length();
112 for (
size_t i = 0; i < init.length(); ++i ) {
113 if ( init[i] ==
'.' ) {
122 int start_of_digits = (fraction_words * digits_per_word) - (init.length() - dp) + exponent;
126 int start_pos_word = start_of_digits % digits_per_word;
128 for (
int i = 0; i < start_pos_word; i++ ) {
132 for (
int i = init.length() - 1; i >= 0; --i ) {
133 int digit = start_of_digits + ( init.length() - 1 - i );
134 int word = ( digit / digits_per_word );
136 data[word] += (SST::Core::from_string<uint32_t>(init.substr(i,1)) * mult);
138 if (mult == storage_radix) mult = 1;
148 void from_uint64(uint64_t init) {
151 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
164 for (
int i = fraction_words; i < whole_words + fraction_words; ++i ) {
165 data[i] = init % storage_radix_long;
166 init /= storage_radix_long;
175 void from_double(
double init) {
178 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
183 for (
int i = 0; i < whole_words - 1; ++i ) {
184 factor *= storage_radix;
187 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
188 data[i] =
static_cast<uint32_t
>(init / factor);
189 init -= (data[i] * factor);
190 factor /= storage_radix;
203 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
238 std::is_integral<T>::value >::type* =
nullptr) {
254 decimal_fixedpoint(
const T init,
typename std::enable_if<std::is_floating_point<T>::value >::type* =
nullptr) {
264 negative = init.negative;
265 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
266 data[i] = init.data[i];
274 negative = v.negative;
275 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
324 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;
353 for (
int i = 0; i < whole_words; ++i ) {
354 ret += (
static_cast<int64_t
>(data[fraction_words + i]) * factor );
355 factor *= storage_radix;
360 if ( data[fraction_words - 1] > ( storage_radix / 2 ) ) {
363 else if ( data[fraction_words - 1] == ( storage_radix / 2 ) ) {
364 for (
int i = fraction_words - 2; i >= 0; --i ) {
365 if ( data[i] != 0 ) {
374 if ( ret % 2 == 1 ) round =
true;
378 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;
400 T
convert_to(
typename std::enable_if<std::is_unsigned<T>::value>::type* = 0)
const {
408 T
convert_to(
typename std::enable_if<std::is_signed<T>::value &&
409 std::is_integral<T>::value >::type* = 0)
const {
410 return static_cast<T
>(
toLong());
417 T
convert_to(
typename std::enable_if<std::is_floating_point<T>::value >::type* = 0)
const {
427 std::string
toString(int32_t precision = 6)
const {
429 std::stringstream stream;
430 if ( precision <= 0 || precision > ((whole_words + fraction_words) * digits_per_word ) )
431 precision = (whole_words + fraction_words) * digits_per_word;
435 constexpr
int num_digits = (whole_words + fraction_words) * digits_per_word;
437 unsigned char digits[num_digits];
438 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
439 uint32_t value = data[i];
440 for (
int j = 0; j < digits_per_word; ++j ) {
441 digits[i*digits_per_word+j] = value % 10;
447 int first_non_zero = -1;
448 for (
int i = num_digits - 1; i >= 0; --i ) {
449 if ( digits[i] != 0 ) {
456 if ( first_non_zero == -1 )
return "0";
459 int round_position = first_non_zero - precision;
464 if ( round_position >= 0 ) {
465 if ( digits[round_position] > 5 ) round =
true;
466 else if ( digits[round_position] < 5 ) round =
false;
469 for (
int i = round_position - 1; i >= 0; --i ) {
470 if ( digits[i] != 0 ) {
478 if ( digits[round_position+1] % 2 == 1 ) round =
true;
484 unsigned char carry = 1;
485 for (
int i = round_position + 1; i < num_digits; ++i ) {
487 carry = digits[i] / 10;
488 digits[i] = digits[i] % 10;
493 for (
int i = 0; i <= round_position; ++i ) {
499 if ( negative ) stream <<
'-';
504 for (
int i = num_digits - 1; i >= 0; --i ) {
505 if ( digits[i] != 0 ) {
512 if ( first_non_zero == -1 ) {
515 stream <<
"1e+" << (whole_words*digits_per_word);
522 if ( first_non_zero >= ((fraction_words*digits_per_word) + precision) ) {
524 int exponent = first_non_zero - (fraction_words * digits_per_word );
525 stream << static_cast<uint32_t>(digits[first_non_zero]) <<
".";
527 for (
int i = first_non_zero - 1; i >= first_non_zero - precision; --i ) {
531 if ( digits[i] == 0 ) zeros++;
533 for (
int j = 0; j < zeros; ++j ) {
536 stream << static_cast<uint32_t>(digits[i]);
540 std::string ret = stream.str();
541 if ( ret[ret.length()-1] ==
'.' ) {
542 ret = ret.substr(0,ret.length()-1);
543 stream.str(std::string(
""));
546 stream <<
"e+" << std::setfill(
'0') << std::setw(2) << exponent;
551 else if ( first_non_zero >= (fraction_words * digits_per_word)) {
553 for (
int i = first_non_zero; i >= (fraction_words * digits_per_word); --i ) {
555 stream << static_cast<uint32_t>(digits[i]);
560 for (
int i = (fraction_words * digits_per_word) - 1;
561 i >= first_non_zero - precision && (i >= 0);
566 if ( digits[i] == 0 ) zeros++;
568 for (
int j = 0; j < zeros; ++j ) {
571 stream << static_cast<uint32_t>(digits[i]);
575 std::string ret = stream.str();
576 if ( ret[ret.length()-1] ==
'.' ) {
577 ret = ret.substr(0,ret.length()-1);
578 stream.str(std::string(
""));
586 else if ( first_non_zero > (fraction_words * digits_per_word) - 5 ) {
588 for (
int i = (fraction_words * digits_per_word) - 1; i > first_non_zero; --i ) {
592 for (
int i = first_non_zero;
593 (i >= first_non_zero - precision) && (i >= 0);
598 if ( digits[i] == 0 ) zeros++;
600 for (
int j = 0; j < zeros; ++j ) {
603 stream << static_cast<uint32_t>(digits[i]);
611 int exponent = first_non_zero - (fraction_words * digits_per_word );
612 exponent = -exponent;
613 stream << static_cast<uint32_t>(digits[first_non_zero]) <<
".";
615 for (
int i = first_non_zero - 1;
616 (i >= first_non_zero - precision) && (i >= 0);
621 if ( digits[i] == 0 ) zeros++;
623 for (
int j = 0; j < zeros; ++j ) {
626 stream << static_cast<uint32_t>(digits[i]);
630 std::string ret = stream.str();
631 if ( ret[ret.length()-1] ==
'.' ) {
632 ret = ret.substr(0,ret.length()-1);
633 stream.str(std::string(
""));
636 stream <<
"e-" << std::setfill(
'0') << std::setw(2) << exponent;
650 if ( (negative ^ v.negative) == 0 ) {
653 uint64_t carry_over = 0;
654 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
655 uint64_t value =
static_cast<uint64_t
>(data[i])
656 + static_cast<uint64_t>(v.data[i])
658 carry_over = value / storage_radix;
660 data[i] = value % storage_radix;
666 if (
operator>=(v) ) {
669 uint64_t carry_over = 1;
670 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
671 uint64_t
negate =
static_cast<uint64_t
>(storage_radix - 1) - static_cast<uint64_t>(v.data[i]);
673 uint64_t value =
static_cast<uint64_t
>(data[i])
676 carry_over = value / storage_radix;
677 data[i] =
static_cast<uint32_t
>(value % storage_radix);
684 uint64_t carry_over = 1;
685 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
686 uint64_t
negate =
static_cast<uint64_t
>(storage_radix - 1) - static_cast<uint64_t>(data[i]);
688 uint64_t value =
static_cast<uint64_t
>(v.data[i])
691 carry_over = value / storage_radix;
692 data[i] =
static_cast<uint32_t
>(value % storage_radix);
695 negative = v.negative;
727 uint64_t carry_over = 0;
728 for (
int i = 0; i < fraction_words; ++i ) {
729 uint64_t sum = carry_over;
730 for (
int j = 0; j <= i; ++j ) {
731 sum +=
static_cast<uint64_t
>(me.data[j]) * static_cast<uint64_t>(v.data[i-j]);
733 carry_over = sum / storage_radix_long;
737 for (
int i = fraction_words; i < whole_words + fraction_words; ++i ) {
738 uint64_t sum = carry_over;
739 for (
int j = 0; j <= i; ++j ) {
740 sum +=
static_cast<uint64_t
>(me.data[j]) * static_cast<uint64_t>(v.data[i-j]);
742 carry_over = sum / storage_radix_long;
743 data[i-fraction_words] =
static_cast<uint32_t
>(sum % storage_radix_long);
746 for (
int i = 0; i < fraction_words; ++i ) {
747 uint64_t sum = carry_over;
748 for (
int j = i+1; j < whole_words + fraction_words; ++j ) {
749 sum +=
static_cast<uint64_t
>(me.data[j])
750 * static_cast<uint64_t>(v.data[whole_words+fraction_words+i-j]);
752 carry_over = sum / storage_radix_long;
753 data[i+whole_words] =
static_cast<uint32_t
>(sum % storage_radix_long);
791 int digits_of_prec = std::numeric_limits<double>::digits10 / 2;
794 for (
int i = digits_of_prec; i <= (whole_words + fraction_words) * digits_per_word; i *= 2 ) {
812 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
813 if ( data[i] != v.data[i] )
return false;
824 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
825 if ( data[i] != v.data[i] )
return true;
836 for (
int i = whole_words +fraction_words - 1; i >= 0; --i ) {
837 if ( data[i] > v.data[i] )
return true;
838 if ( data[i] < v.data[i] )
return false;
850 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
851 if ( data[i] > v.data[i] )
return true;
852 if ( data[i] < v.data[i] )
return false;
863 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
864 if ( data[i] < v.data[i] )
return true;
865 if ( data[i] > v.data[i] )
return false;
877 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
878 if ( data[i] < v.data[i] )
return true;
879 if ( data[i] > v.data[i] )
return false;
886 template <
int whole_words,
int fraction_words>
887 decimal_fixedpoint<whole_words,fraction_words> operator+(decimal_fixedpoint<whole_words,fraction_words> lhs,
888 decimal_fixedpoint<whole_words,fraction_words> rhs) {
889 decimal_fixedpoint<whole_words,fraction_words> ret(lhs);
893 template <
int whole_words,
int fraction_words>
894 decimal_fixedpoint<whole_words,fraction_words> operator-(decimal_fixedpoint<whole_words,fraction_words> lhs,
895 decimal_fixedpoint<whole_words,fraction_words> rhs) {
896 decimal_fixedpoint<whole_words,fraction_words> ret(lhs);
900 template <
int whole_words,
int fraction_words>
901 decimal_fixedpoint<whole_words,fraction_words> operator*(decimal_fixedpoint<whole_words,fraction_words> lhs,
902 decimal_fixedpoint<whole_words,fraction_words> rhs) {
903 decimal_fixedpoint<whole_words,fraction_words> ret(lhs);
907 template <
int whole_words,
int fraction_words>
908 decimal_fixedpoint<whole_words,fraction_words> operator/(decimal_fixedpoint<whole_words,fraction_words> lhs,
909 decimal_fixedpoint<whole_words,fraction_words> rhs) {
910 decimal_fixedpoint<whole_words,fraction_words> ret(lhs);
914 template <
int whole_words,
int fraction_words,
typename T>
915 bool operator==(
const T& lhs,
const decimal_fixedpoint<whole_words,fraction_words>& rhs) {
916 return rhs == decimal_fixedpoint<whole_words,fraction_words>(lhs);
919 template <
int whole_words,
int fraction_words,
typename T>
920 bool operator!=(
const T& lhs,
const decimal_fixedpoint<whole_words,fraction_words>& rhs) {
921 return rhs != decimal_fixedpoint<whole_words,fraction_words>(lhs);
924 template <
int whole_words,
int fraction_words>
925 std::ostream& operator <<(std::ostream& os, const decimal_fixedpoint<whole_words,fraction_words>& rhs) {
926 os << rhs.toString(os.precision());
std::string toString(int32_t precision=6) const
Create a string representation of this decimal_fixedpoint.
Definition: decimal_fixedpoint.h:427
constexpr int getWholeWords() const
Get the value of whole_words template parameter.
Definition: decimal_fixedpoint.h:49
decimal_fixedpoint & operator=(uint64_t v)
Equal operator for 64-bit unsigned int.
Definition: decimal_fixedpoint.h:284
uint64_t toUnsignedLong() const
Return a uint64_t version of the decimal_fixedpoint.
Definition: decimal_fixedpoint.h:386
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:408
decimal_fixedpoint & operator-=(const decimal_fixedpoint &v)
Subtracts another number from this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:707
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:417
bool operator<(const decimal_fixedpoint &v) const
Checks to see if this number is less than another number.
Definition: decimal_fixedpoint.h:862
decimal_fixedpoint & inverse()
Inverts the number (1 divided by this number)
Definition: decimal_fixedpoint.h:775
decimal_fixedpoint & operator/=(const decimal_fixedpoint &v)
Divides another number from this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:764
decimal_fixedpoint(const decimal_fixedpoint &init)
Build a decimal_fixedpoint using another decimal_fixedpoint.
Definition: decimal_fixedpoint.h:263
bool operator>(const decimal_fixedpoint &v) const
Checks to see if this number is greater than another number.
Definition: decimal_fixedpoint.h:835
decimal_fixedpoint & operator=(double v)
Equal operator for double.
Definition: decimal_fixedpoint.h:306
decimal_fixedpoint & operator=(const std::string &v)
Equal operator for string.
Definition: decimal_fixedpoint.h:314
decimal_fixedpoint & operator=(int64_t v)
Equal operator for 64-bit signed int.
Definition: decimal_fixedpoint.h:292
decimal_fixedpoint(const std::string &init)
Build a decimal_fixedpoint using a string initializer.
Definition: decimal_fixedpoint.h:215
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:226
Class that implements a decimal fixed-point number.
Definition: decimal_fixedpoint.h:39
decimal_fixedpoint & operator*=(const decimal_fixedpoint &v)
Multiplies another number to this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:720
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:254
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:876
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:400
bool operator!=(const decimal_fixedpoint &v) const
Checks to see if two numbers are not equal.
Definition: decimal_fixedpoint.h:823
int64_t toLong() const
Return a int64_t version of the decimal_fixedpoint.
Definition: decimal_fixedpoint.h:350
constexpr int getFractionWords() const
Get the value of fraction_words template parameter.
Definition: decimal_fixedpoint.h:56
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:237
decimal_fixedpoint()
Default constructor.
Definition: decimal_fixedpoint.h:201
decimal_fixedpoint & operator=(const decimal_fixedpoint &v)
Equal operator for other decimal_fixedpoint objects.
Definition: decimal_fixedpoint.h:273
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:849
decimal_fixedpoint & operator+=(const decimal_fixedpoint &v)
Adds another number to this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:648
void negate()
Negate the value (change the sign bit).
Definition: decimal_fixedpoint.h:323
bool operator==(const decimal_fixedpoint &v) const
Checks to see if two numbers are equal.
Definition: decimal_fixedpoint.h:811
double toDouble() const
Return a double precision version of the decimal_fixedpoint.
Definition: decimal_fixedpoint.h:331