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(std::string init) {
92 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
97 if ( init[0] ==
'-' ) {
99 init = init.substr(1,init.npos);
103 size_t exponent_pos = init.find_last_of(
"eE");
104 uint32_t exponent = 0;
105 if ( exponent_pos != init.npos ) {
106 exponent =
static_cast<uint32_t
>(SST::Core::from_string<double>(init.substr(exponent_pos+1,init.npos)));
107 init = init.substr(0,exponent_pos);
110 int dp = init.length();
111 for (
size_t i = 0; i < init.length(); ++i ) {
112 if ( init[i] ==
'.' ) {
121 int start_of_digits = (fraction_words * digits_per_word) - (init.length() - dp) + exponent;
125 int start_pos_word = start_of_digits % digits_per_word;
127 for (
int i = 0; i < start_pos_word; i++ ) {
131 for (
int i = init.length() - 1; i >= 0; --i ) {
132 int digit = start_of_digits + ( init.length() - 1 - i );
133 int word = ( digit / digits_per_word );
135 data[word] += (SST::Core::from_string<uint32_t>(init.substr(i,1)) * mult);
137 if (mult == storage_radix) mult = 1;
147 void from_uint64(uint64_t init) {
150 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
163 for (
int i = fraction_words; i < whole_words + fraction_words; ++i ) {
164 data[i] = init % storage_radix_long;
165 init /= storage_radix_long;
174 void from_double(
double init) {
177 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
182 for (
int i = 0; i < whole_words - 1; ++i ) {
183 factor *= storage_radix;
186 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
187 data[i] =
static_cast<uint32_t
>(init / factor);
188 init -= (data[i] * factor);
189 factor /= storage_radix;
202 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
237 std::is_integral<T>::value >::type* = 0) {
253 decimal_fixedpoint(
const T init,
typename std::enable_if<std::is_floating_point<T>::value >::type* = 0) {
263 negative = init.negative;
264 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
265 data[i] = init.data[i];
273 negative = v.negative;
274 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
323 negative = negative ^ 0x1;
333 for (
int i = 0; i < fraction_words; ++i ) {
334 factor /= storage_radix;
337 for (
int i = 0; i <whole_words + fraction_words; ++i ) {
338 ret += (
static_cast<double>(data[i]) * factor );
339 factor *= storage_radix;
352 for (
int i = 0; i < whole_words; ++i ) {
353 ret += (
static_cast<int64_t
>(data[fraction_words + i]) * factor );
354 factor *= storage_radix;
359 if ( data[fraction_words - 1] > ( storage_radix / 2 ) ) {
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;
388 for (
int i = 0; i < whole_words; ++i ) {
389 ret += (
static_cast<int64_t
>(data[i]) * factor );
390 factor *= storage_radix;
399 T
convert_to(
typename std::enable_if<std::is_unsigned<T>::value>::type* = 0)
const {
407 T
convert_to(
typename std::enable_if<std::is_signed<T>::value &&
408 std::is_integral<T>::value >::type* = 0)
const {
409 return static_cast<T
>(
toLong());
416 T
convert_to(
typename std::enable_if<std::is_floating_point<T>::value >::type* = 0)
const {
426 std::string
toString(int32_t precision = 6)
const {
428 std::stringstream stream;
429 if ( precision <= 0 || precision > ((whole_words + fraction_words) * digits_per_word ) )
430 precision = (whole_words + fraction_words) * digits_per_word;
434 constexpr
int num_digits = (whole_words + fraction_words) * digits_per_word;
436 unsigned char digits[num_digits];
437 for (
int i = 0; i < whole_words + fraction_words; ++i ) {
438 uint32_t value = data[i];
439 for (
int j = 0; j < digits_per_word; ++j ) {
440 digits[i*digits_per_word+j] = value % 10;
446 int first_non_zero = -1;
447 for (
int i = num_digits - 1; i >= 0; --i ) {
448 if ( digits[i] != 0 ) {
455 if ( first_non_zero == -1 )
return "0";
458 int round_position = first_non_zero - precision;
463 if ( round_position >= 0 ) {
464 if ( digits[round_position] > 5 ) round =
true;
465 else if ( digits[round_position] < 5 ) round =
false;
468 for (
int i = round_position - 1; i >= 0; --i ) {
469 if ( digits[i] != 0 ) {
477 if ( digits[round_position+1] % 2 == 1 ) round =
true;
483 unsigned char carry = 1;
484 for (
int i = round_position + 1; i < num_digits; ++i ) {
486 carry = digits[i] / 10;
487 digits[i] = digits[i] % 10;
492 for (
int i = 0; i <= round_position; ++i ) {
498 if ( negative ) stream <<
'-';
503 for (
int i = num_digits - 1; i >= 0; --i ) {
504 if ( digits[i] != 0 ) {
511 if ( first_non_zero == -1 ) {
514 stream <<
"1e+" << (whole_words*digits_per_word);
521 if ( first_non_zero >= ((fraction_words*digits_per_word) + precision) ) {
523 int exponent = first_non_zero - (fraction_words * digits_per_word );
524 stream << static_cast<uint32_t>(digits[first_non_zero]) <<
".";
526 for (
int i = first_non_zero - 1; i >= first_non_zero - precision; --i ) {
530 if ( digits[i] == 0 ) zeros++;
532 for (
int j = 0; j < zeros; ++j ) {
535 stream << static_cast<uint32_t>(digits[i]);
539 std::string ret = stream.str();
540 if ( ret[ret.length()-1] ==
'.' ) {
541 ret = ret.substr(0,ret.length()-1);
542 stream.str(std::string(
""));
545 stream <<
"e+" << std::setfill(
'0') << std::setw(2) << exponent;
550 else if ( first_non_zero >= (fraction_words * digits_per_word)) {
552 for (
int i = first_non_zero; i >= (fraction_words * digits_per_word); --i ) {
554 stream << static_cast<uint32_t>(digits[i]);
559 for (
int i = (fraction_words * digits_per_word) - 1;
560 i >= first_non_zero - precision && (i >= 0);
565 if ( digits[i] == 0 ) zeros++;
567 for (
int j = 0; j < zeros; ++j ) {
570 stream << static_cast<uint32_t>(digits[i]);
574 std::string ret = stream.str();
575 if ( ret[ret.length()-1] ==
'.' ) {
576 ret = ret.substr(0,ret.length()-1);
577 stream.str(std::string(
""));
585 else if ( first_non_zero > (fraction_words * digits_per_word) - 5 ) {
587 for (
int i = (fraction_words * digits_per_word) - 1; i > first_non_zero; --i ) {
591 for (
int i = first_non_zero;
592 (i >= first_non_zero - precision) && (i >= 0);
597 if ( digits[i] == 0 ) zeros++;
599 for (
int j = 0; j < zeros; ++j ) {
602 stream << static_cast<uint32_t>(digits[i]);
610 int exponent = first_non_zero - (fraction_words * digits_per_word );
611 exponent = -exponent;
612 stream << static_cast<uint32_t>(digits[first_non_zero]) <<
".";
614 for (
int i = first_non_zero - 1;
615 (i >= first_non_zero - precision) && (i >= 0);
620 if ( digits[i] == 0 ) zeros++;
622 for (
int j = 0; j < zeros; ++j ) {
625 stream << static_cast<uint32_t>(digits[i]);
629 std::string ret = stream.str();
630 if ( ret[ret.length()-1] ==
'.' ) {
631 ret = ret.substr(0,ret.length()-1);
632 stream.str(std::string(
""));
635 stream <<
"e-" << std::setfill(
'0') << std::setw(2) << exponent;
649 if ( (negative ^ v.negative) == 0 ) {
652 uint64_t carry_over = 0;
653 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
654 uint64_t value =
static_cast<uint64_t
>(data[i])
655 + static_cast<uint64_t>(v.data[i])
657 carry_over = value / storage_radix;
659 data[i] = value % storage_radix;
665 if (
operator>=(v) ) {
668 uint64_t carry_over = 1;
669 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
670 uint64_t
negate =
static_cast<uint64_t
>(storage_radix - 1) - static_cast<uint64_t>(v.data[i]);
672 uint64_t value =
static_cast<uint64_t
>(data[i])
675 carry_over = value / storage_radix;
676 data[i] =
static_cast<uint32_t
>(value % storage_radix);
683 uint64_t carry_over = 1;
684 for (
int i = 0; i < whole_words + fraction_words; i++ ) {
685 uint64_t
negate =
static_cast<uint64_t
>(storage_radix - 1) - static_cast<uint64_t>(data[i]);
687 uint64_t value =
static_cast<uint64_t
>(v.data[i])
690 carry_over = value / storage_radix;
691 data[i] =
static_cast<uint32_t
>(value % storage_radix);
694 negative = v.negative;
726 uint64_t carry_over = 0;
727 for (
int i = 0; i < fraction_words; ++i ) {
728 uint64_t sum = carry_over;
729 for (
int j = 0; j <= i; ++j ) {
730 sum +=
static_cast<uint64_t
>(me.data[j]) * static_cast<uint64_t>(v.data[i-j]);
732 carry_over = sum / storage_radix_long;
736 for (
int i = fraction_words; i < whole_words + 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;
742 data[i-fraction_words] =
static_cast<uint32_t
>(sum % storage_radix_long);
745 for (
int i = 0; i < fraction_words; ++i ) {
746 uint64_t sum = carry_over;
747 for (
int j = i+1; j < whole_words + fraction_words; ++j ) {
748 sum +=
static_cast<uint64_t
>(me.data[j])
749 * static_cast<uint64_t>(v.data[whole_words+fraction_words+i-j]);
751 carry_over = sum / storage_radix_long;
752 data[i+whole_words] =
static_cast<uint32_t
>(sum % storage_radix_long);
790 int digits_of_prec = std::numeric_limits<double>::digits10 / 2;
793 for (
int i = digits_of_prec; i <= (whole_words + fraction_words) * digits_per_word; i *= 2 ) {
811 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
812 if ( data[i] != v.data[i] )
return false;
823 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
824 if ( data[i] != v.data[i] )
return true;
835 for (
int i = whole_words +fraction_words - 1; i >= 0; --i ) {
836 if ( data[i] > v.data[i] )
return true;
837 if ( data[i] < v.data[i] )
return false;
849 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
850 if ( data[i] > v.data[i] )
return true;
851 if ( data[i] < v.data[i] )
return false;
862 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
863 if ( data[i] < v.data[i] )
return true;
864 if ( data[i] > v.data[i] )
return false;
876 for (
int i = whole_words + fraction_words - 1; i >= 0; --i ) {
877 if ( data[i] < v.data[i] )
return true;
878 if ( data[i] > v.data[i] )
return false;
885 template <
int whole_words,
int fraction_words>
886 decimal_fixedpoint<whole_words,fraction_words> operator+(decimal_fixedpoint<whole_words,fraction_words> lhs,
887 decimal_fixedpoint<whole_words,fraction_words> rhs) {
888 decimal_fixedpoint<whole_words,fraction_words> ret(lhs);
892 template <
int whole_words,
int fraction_words>
893 decimal_fixedpoint<whole_words,fraction_words> operator-(decimal_fixedpoint<whole_words,fraction_words> lhs,
894 decimal_fixedpoint<whole_words,fraction_words> rhs) {
895 decimal_fixedpoint<whole_words,fraction_words> ret(lhs);
899 template <
int whole_words,
int fraction_words>
900 decimal_fixedpoint<whole_words,fraction_words> operator*(decimal_fixedpoint<whole_words,fraction_words> lhs,
901 decimal_fixedpoint<whole_words,fraction_words> rhs) {
902 decimal_fixedpoint<whole_words,fraction_words> ret(lhs);
906 template <
int whole_words,
int fraction_words>
907 decimal_fixedpoint<whole_words,fraction_words> operator/(decimal_fixedpoint<whole_words,fraction_words> lhs,
908 decimal_fixedpoint<whole_words,fraction_words> rhs) {
909 decimal_fixedpoint<whole_words,fraction_words> ret(lhs);
913 template <
int whole_words,
int fraction_words,
typename T>
914 bool operator==(
const T& lhs,
const decimal_fixedpoint<whole_words,fraction_words>& rhs) {
915 return rhs == decimal_fixedpoint<whole_words,fraction_words>(lhs);
918 template <
int whole_words,
int fraction_words,
typename T>
919 bool operator!=(
const T& lhs,
const decimal_fixedpoint<whole_words,fraction_words>& rhs) {
920 return rhs != decimal_fixedpoint<whole_words,fraction_words>(lhs);
923 template <
int whole_words,
int fraction_words>
924 std::ostream& operator <<(std::ostream& os, const decimal_fixedpoint<whole_words,fraction_words>& rhs) {
925 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:426
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:283
uint64_t toUnsignedLong() const
Return a uint64_t version of the decimal_fixedpoint.
Definition: decimal_fixedpoint.h:385
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:407
decimal_fixedpoint & operator-=(const decimal_fixedpoint &v)
Subtracts another number from this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:706
decimal_fixedpoint(const T init, typename std::enable_if< std::is_floating_point< T >::value >::type *=0)
Build a decimal_fixedpoint using a double.
Definition: decimal_fixedpoint.h:253
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:416
bool operator<(const decimal_fixedpoint &v) const
Checks to see if this number is less than another number.
Definition: decimal_fixedpoint.h:861
decimal_fixedpoint & inverse()
Inverts the number (1 divided by this number)
Definition: decimal_fixedpoint.h:774
decimal_fixedpoint & operator/=(const decimal_fixedpoint &v)
Divides another number from this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:763
decimal_fixedpoint(const decimal_fixedpoint &init)
Build a decimal_fixedpoint using another decimal_fixedpoint.
Definition: decimal_fixedpoint.h:262
bool operator>(const decimal_fixedpoint &v) const
Checks to see if this number is greater than another number.
Definition: decimal_fixedpoint.h:834
decimal_fixedpoint & operator=(double v)
Equal operator for double.
Definition: decimal_fixedpoint.h:305
decimal_fixedpoint(T init, typename std::enable_if< std::is_unsigned< T >::value >::type *=0)
Build a decimal_fixedpoint using a 64-bit unsigned number.
Definition: decimal_fixedpoint.h:225
decimal_fixedpoint(T init, typename std::enable_if< std::is_signed< T >::value &&std::is_integral< T >::value >::type *=0)
Build a decimal_fixedpoint using a 64-bit signed number.
Definition: decimal_fixedpoint.h:236
decimal_fixedpoint & operator=(int64_t v)
Equal operator for 64-bit signed int.
Definition: decimal_fixedpoint.h:291
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:719
decimal_fixedpoint & operator=(std::string v)
Equal operator for string.
Definition: decimal_fixedpoint.h:313
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:875
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:399
bool operator!=(const decimal_fixedpoint &v) const
Checks to see if two numbers are not equal.
Definition: decimal_fixedpoint.h:822
decimal_fixedpoint(std::string init)
Build a decimal_fixedpoint using a string initializer.
Definition: decimal_fixedpoint.h:214
int64_t toLong() const
Return a int64_t version of the decimal_fixedpoint.
Definition: decimal_fixedpoint.h:349
constexpr int getFractionWords() const
Get the value of fraction_words template parameter.
Definition: decimal_fixedpoint.h:56
decimal_fixedpoint()
Default constructor.
Definition: decimal_fixedpoint.h:200
decimal_fixedpoint & operator=(const decimal_fixedpoint &v)
Equal operator for other decimal_fixedpoint objects.
Definition: decimal_fixedpoint.h:272
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:848
decimal_fixedpoint & operator+=(const decimal_fixedpoint &v)
Adds another number to this one and sets it equal to the result.
Definition: decimal_fixedpoint.h:647
void negate()
Negate the value (change the sign bit).
Definition: decimal_fixedpoint.h:322
bool operator==(const decimal_fixedpoint &v) const
Checks to see if two numbers are equal.
Definition: decimal_fixedpoint.h:810
double toDouble() const
Return a double precision version of the decimal_fixedpoint.
Definition: decimal_fixedpoint.h:330