100 m_mant(
min_mant), m_wp(), m_sign(), m_state(), m_msw(), m_lsw(),
107 m_msw(), m_lsw(), m_r_flag(false)
111 m_wp = m_msw = m_lsw = 2;
126 m_state(), m_msw(), m_lsw(), m_r_flag(false)
130 m_wp = m_msw = m_lsw = 2;
139 scfx_rep::scfx_rep(
long a) :
140 m_mant(
min_mant), m_wp(), m_sign(), m_state(), m_msw(), m_lsw(),
169 m_mant(
min_mant), m_wp(), m_sign(), m_state(), m_msw(), m_lsw(),
174 m_wp = m_msw = m_lsw = 2;
178 m_mant[1] =
static_cast<word>(
a);
194 m_mant(
min_mant), m_wp(0), m_sign(), m_state(normal), m_msw(0),
195 m_lsw(0), m_r_flag(false)
201 m_sign =
id.negative() ? -1 : 1;
207 }
else if (
id.is_subnormal()) {
208 m_mant[0] =
id.mantissa1();
209 m_mant[1] =
id.mantissa0();
212 m_mant[0] =
id.mantissa1();
219 m_mant(
min_mant), m_wp(), m_sign(), m_state(), m_msw(), m_lsw(),
242 m_mant(
min_mant), m_wp(), m_sign(), m_state(), m_msw(), m_lsw(),
259 m_mant(
min_mant), m_wp(), m_sign(), m_state(), m_msw(), m_lsw(),
265 int words =
n_word(
a.length());
273 for (
int i = 0;
i < a2.length(); ++
i) {
281 for (
int i = 0;
i <
a.length(); ++
i) {
294 m_mant(
min_mant), m_wp(), m_sign(), m_state(), m_msw(), m_lsw(),
300 int words =
n_word(
a.length());
306 for (
int i = 0;
i <
a.length(); ++
i) {
319 m_mant(
a.m_mant), m_wp(
a.m_wp), m_sign(
a.m_sign), m_state(
a.m_state),
320 m_msw(
a.m_msw), m_lsw(
a.m_lsw), m_r_flag(false)
339 scfx_rep::operator
new(std::size_t size)
341 const int ALLOC_SIZE = 1024;
344 return ::operator
new(size);
348 for (
int i = 0;
i < ALLOC_SIZE - 1;
i++)
360 scfx_rep::operator
delete(
void *ptr, std::size_t size)
363 ::operator
delete(ptr);
379 #define SCFX_FAIL_IF_(cnd) \
382 m_state = not_a_number; \
462 s = (
const char *)s2 + 4;
475 bool based_point =
false;
503 for (
const char *
e = end + 2; *
e; ++
e)
505 exponent = std::atoi(end + 1);
511 bool mant_is_neg =
false;
538 int_digits += bit_offset;
539 frac_digits -= bit_offset;
548 for (;
s < end;
s++) {
575 int_digits += bit_offset;
576 frac_digits -= bit_offset;
585 for (;
s < end;
s++) {
587 case '7':
case '6':
case '5':
case '4':
588 case '3':
case '2':
case '1':
607 int length = int_digits + frac_digits;
613 for (;
s < end;
s++) {
615 case '9':
case '8':
case '7':
case '6':
case '5':
616 case '4':
case '3':
case '2':
case '1':
case '0':
635 int denominator = frac_digits - exponent;
657 int_digits += bit_offset;
658 frac_digits -= bit_offset;
667 for (;
s < end;
s ++) {
669 case 'f':
case 'e':
case 'd':
case 'c':
case 'b':
case 'a':
673 case 'F':
case 'E':
case 'D':
case 'C':
case 'B':
case 'A':
677 case '9':
case '8':
case '7':
case '6':
case '5':
678 case '4':
case '3':
case '2':
case '1':
765 unsigned int guard = 0;
774 }
else if (
shift < 0) {
799 m1 = m1 >> subnormal_shift |
801 m0 = m0 >> subnormal_shift;
846 while (shift < 64 && m_msw >= idx && idx >=
m_lsw) {
852 return m_sign > 0 ? result : -result;
878 scfx_rep int_part = num;
879 scfx_rep frac_part = num;
883 for (
i = int_part.m_lsw;
i <= int_part.m_msw &&
i < int_part.m_wp;
i++)
884 int_part.m_mant[
i] = 0;
886 if (int_part.m_wp < int_part.m_lsw)
887 int_part.resize_to(int_part.size() - int_part.m_wp, -1);
889 for (
i = frac_part.m_msw;
890 i >= frac_part.m_lsw &&
i >= frac_part.m_wp;
i--)
891 frac_part.m_mant[
i] = 0;
893 if (frac_part.m_msw == frac_part.size() - 1)
894 frac_part.resize_to(frac_part.size() + 1, 1);
900 if (!int_part.is_zero()) {
901 double int_wl = (int_part.m_msw - int_part.m_wp) *
bits_in_word +
903 int_digits = (int)std::ceil(int_wl * std::log10(2.));
905 int len =
s.length();
906 s.append(int_digits);
908 bool zero_digits = (frac_part.is_zero() && fmt !=
SC_F);
910 for (
i = int_digits +
len - 1;
i >=
len;
i--) {
911 unsigned int remainder = int_part.divide_by_ten();
923 s.discard(int_zeros);
936 if (!frac_part.is_zero()) {
939 bool zero_digits = (int_digits == 0 && fmt !=
SC_F);
941 double frac_wl = (frac_part.m_wp - frac_part.m_msw) *
bits_in_word -
943 frac_zeros = (int)std::floor(frac_wl * std::log10(2.));
948 if (frac_part.m_msw == frac_part.size() - 1)
949 frac_part.resize_to(frac_part.size() + 1, 1);
951 frac_digits = frac_zeros;
953 for (
i = 0;
i < frac_zeros;
i++)
958 while (!frac_part.is_zero()) {
959 frac_part.multiply_by_ten();
960 int n = frac_part.m_mant[frac_part.m_msw + 1];
970 s +=
static_cast<char>(
'0' +
n);
972 frac_part.m_mant[frac_part.m_msw + 1] = 0;
979 if (frac_digits == 0)
981 else if (int_digits == 0)
988 sc_fmt fmt,
const scfx_params *params)
994 bool numrep_is_sm = (numrep ==
SC_BIN_SM ||
1018 if (w_prefix != 0) {
1027 msb = params->iwl() - 1;
1028 lsb = params->iwl() - params->wl();
1030 if (params->enc() ==
SC_TC_ &&
1037 }
else if (params->enc() ==
SC_US_ &&
1051 while (
b.get_bit(msb) ==
b.get_bit(msb - 1))
1084 "unexpected sc_numrep");
1088 msb = (int)std::ceil(
double(msb + 1) / step) * step - 1;
1090 lsb = (int)std::floor(
double(lsb) / step) * step;
1095 int sign = (
b.is_neg()) ? (1 << step) - 1 : 0;
1096 for (
int i = (msb + 1) / step;
i < 0;
i++) {
1098 s +=
static_cast<char>(sign +
'0');
1100 s +=
static_cast<char>(sign +
'a' - 10);
1108 for (
int j = step - 1;
j >= 0; --
j) {
1109 value +=
static_cast<int>(
b.get_bit(
i)) <<
j;
1113 s +=
static_cast<char>(value +
'0');
1115 s +=
static_cast<char>(value +
'a' - 10);
1120 if (lsb > 0 && fmt ==
SC_F) {
1121 for (
int i = lsb / step;
i > 0;
i--)
1125 if (
s[
s.length() - 1] ==
'.')
1141 sc_fmt fmt,
const scfx_params *params)
const
1143 static scfx_string
s;
1177 unsigned int carry = 0;
1190 }
while (++
index < size);
1192 return (carry ? 1 : 0);
1211 }
while (++
index < size);
1213 return (carry ? 1 : 0);
1217 add_scfx_rep(
const scfx_rep &lhs,
const scfx_rep &rhs,
int max_wl)
1219 scfx_rep &result = *
new scfx_rep;
1224 if (lhs.is_nan() || rhs.is_nan() ||
1225 (lhs.is_inf() && rhs.is_inf() && lhs.m_sign != rhs.m_sign)) {
1231 result.set_inf(lhs.m_sign);
1236 result.set_inf(rhs.m_sign);
1243 scfx_mant_ref lhs_mant;
1244 scfx_mant_ref rhs_mant;
1246 int len_mant = lhs.
size();
1247 int new_wp = lhs.m_wp;
1249 align(lhs, rhs, new_wp, len_mant, lhs_mant, rhs_mant);
1254 result.resize_to(len_mant);
1255 result.m_wp = new_wp;
1260 if (lhs.m_sign == rhs.m_sign) {
1261 add_mants(len_mant, result.m_mant, lhs_mant, rhs_mant);
1262 result.m_sign = lhs.m_sign;
1267 sub_mants(len_mant, result.m_mant, lhs_mant, rhs_mant);
1268 result.m_sign = lhs.m_sign;
1269 }
else if (cmp == -1) {
1270 sub_mants(len_mant, result.m_mant, rhs_mant, lhs_mant);
1271 result.m_sign = rhs.m_sign;
1273 result.m_mant.clear();
1279 result.round(max_wl);
1295 const scfx_mant &
b,
int b_msw,
int b_lsw)
1299 int size = b_msw - b_lsw;
1300 int a_index = a_msw - size;
1301 int b_index = b_msw - size;
1305 word y =
b[b_index];
1322 return (carry ? 1 : 0);
1355 int len_mant = lhs.
size();
1356 int new_wp = lhs.
m_wp;
1358 align(lhs, rhs, new_wp, len_mant, lhs_mant, rhs_mant);
1364 result.
m_wp = new_wp;
1378 }
else if (cmp == -1) {
1388 result.
round(max_wl);
1403 #if defined(SC_BOOST_BIG_ENDIAN)
1406 #elif defined(SC_BOOST_LITTLE_ENDIAN)
1413 #if defined(SC_BOOST_BIG_ENDIAN)
1414 static const int half_word_incr = -1;
1415 #elif defined(SC_BOOST_LITTLE_ENDIAN)
1416 static const int half_word_incr = 1;
1420 multiply(scfx_rep &result,
const scfx_rep &lhs,
const scfx_rep &rhs,
1426 if (lhs.is_nan() || rhs.is_nan() ||
1427 (lhs.is_inf() && rhs.is_zero()) ||
1428 (lhs.is_zero() && rhs.is_inf())) {
1433 if (lhs.is_inf() || rhs.is_inf()) {
1434 result.set_inf(lhs.m_sign * rhs.m_sign);
1438 if (lhs.is_zero() || rhs.is_zero()) {
1439 result.set_zero(lhs.m_sign * rhs.m_sign);
1446 int len_lhs = lhs.m_msw - lhs.m_lsw + 1;
1447 int len_rhs = rhs.m_msw - rhs.m_lsw + 1;
1450 int new_wp = (lhs.m_wp - lhs.m_lsw) + (rhs.m_wp - rhs.m_lsw);
1451 int new_sign = lhs.m_sign * rhs.m_sign;
1453 result.resize_to(new_size);
1454 result.m_mant.clear();
1455 result.m_wp = new_wp;
1456 result.m_sign = new_sign;
1459 half_word *s1 = lhs.m_mant.half_addr(lhs.m_lsw);
1460 half_word *s2 = rhs.m_mant.half_addr(rhs.m_lsw);
1469 for (i1 = 0; i1 * half_word_incr < len_lhs; i1 += half_word_incr) {
1475 for (i2 = 0; i2 * half_word_incr < len_rhs; i2 += half_word_incr) {
1476 ls.l += v1 * s2[i2];
1477 ls.s.l = ls.s.u + ((
t[i2] += ls.s.l) < ls.s.l);
1482 t += half_word_incr;
1486 result.round(max_wl);
1495 div_scfx_rep(
const scfx_rep &lhs,
const scfx_rep &rhs,
int div_wl)
1497 scfx_rep &result = *
new scfx_rep;
1502 if (lhs.is_nan() || rhs.is_nan() || (lhs.is_inf() && rhs.is_inf()) ||
1503 (lhs.is_zero() && rhs.is_zero())) {
1508 if (lhs.is_inf() || rhs.is_zero()) {
1509 result.set_inf(lhs.m_sign * rhs.m_sign);
1513 if (lhs.is_zero() || rhs.is_inf()) {
1514 result.set_zero(lhs.m_sign * rhs.m_sign);
1526 result.m_mant.clear();
1527 result.m_sign = lhs.m_sign * rhs.m_sign;
1534 int msb_res = msb_lhs - msb_rhs;
1553 int msw_diff = rhs.m_msw -
remainder.m_msw;
1559 for (counter = div_wl; counter && !
remainder.is_zero(); counter--) {
1561 result.set_bin(result_index);
1563 rhs.m_mant, rhs.m_msw, rhs.m_lsw);
1574 scfx_index
x = result.calc_indices(
index);
1575 scfx_index x1 = result.calc_indices(
index + 1);
1577 if (result.o_bit_at(
x) && result.o_bit_at(x1))
1580 result.m_r_flag =
true;
1613 m_wp -= shift_words;
1643 m_wp += shift_words;
1660 word a_word =
a.m_mant[
a.m_msw];
1661 word b_word =
b.m_mant[
b.m_msw];
1663 if (a_word == 0 || b_word == 0) {
1672 int a_msw =
a.m_msw -
a.m_wp;
1673 int b_msw =
b.m_msw -
b.m_wp;
1685 while (a_i >=
a.m_lsw && b_i >=
b.m_lsw) {
1686 a_word =
a.m_mant[a_i];
1687 b_word =
b.m_mant[b_i];
1688 if (a_word > b_word)
1690 if (a_word < b_word)
1697 while (a_i >=
a.m_lsw) {
1698 a_zero = a_zero && (
a.m_mant[a_i] == 0);
1703 while (b_i >=
b.m_lsw) {
1704 b_zero = b_zero && (
b.m_mant[b_i] == 0);
1710 if (!a_zero && b_zero)
1713 if (a_zero && !b_zero)
1730 if (
a.is_nan() ||
b.is_nan()) {
1734 if (
a.is_inf() ||
b.is_inf()) {
1737 if (
b.is_inf() && !
b.is_neg()) {
1743 if (
b.is_inf() &&
b.is_neg()) {
1759 if (
a.is_zero() &&
b.is_zero()) {
1764 if (
a.m_sign !=
b.m_sign) {
1786 if (
x.wi() >=
size())
1792 q_flag = (qb || ! qz);
1795 switch (params.q_mode()) {
1825 if ((qb && !qz) || (qb && qz &&
q_odd(
x)))
1867 if (
x.wi() >=
size())
1882 sc_enc enc = params.enc();
1887 under = (!zero_left || bit_at);
1889 under = (!zero_left || (zero_left && bit_at && ! zero_right));
1891 over = (! zero_left || bit_at);
1897 over = (!zero_left);
1900 o_flag = (under ||
over);
1907 x.wi(
x.wi() - x2.
wi());
1911 switch (params.o_mode()) {
1914 int n_bits = params.n_bits();
1921 }
else if (n_bits < params.wl()) {
1922 scfx_index x3 =
calc_indices(params.iwl() - 1 - n_bits);
1927 o_set(
x, x3, enc, under);
1969 int n_bits = params.n_bits();
1974 if (x4.wi() >=
size())
1982 }
else if (n_bits == 1) {
1988 }
else if (n_bits < params.wl()) {
1989 scfx_index x3 =
calc_indices(params.iwl() - 1 - n_bits);
2024 scfx_rep::cast(
const scfx_params ¶ms,
bool &q_flag,
bool &o_flag)
2051 align(
const scfx_rep &lhs,
const scfx_rep &rhs,
int &new_wp,
2052 int &len_mant, scfx_mant_ref &lhs_mant, scfx_mant_ref &rhs_mant)
2054 bool need_lhs =
true;
2055 bool need_rhs =
true;
2057 if (lhs.m_wp != rhs.m_wp || lhs.size() != rhs.size()) {
2058 int lower_bound_lhs = lhs.m_lsw - lhs.m_wp;
2059 int upper_bound_lhs = lhs.m_msw - lhs.m_wp;
2060 int lower_bound_rhs = rhs.m_lsw - rhs.m_wp;
2061 int upper_bound_rhs = rhs.m_msw - rhs.m_wp;
2063 int lower_bound =
sc_min(lower_bound_lhs, lower_bound_rhs);
2064 int upper_bound =
sc_max(upper_bound_lhs, upper_bound_rhs);
2066 new_wp = -lower_bound;
2069 if (new_wp != lhs.m_wp || len_mant != lhs.size()) {
2070 lhs_mant = lhs.resize(len_mant, new_wp);
2074 if (new_wp != rhs.m_wp || len_mant != rhs.size()) {
2075 rhs_mant = rhs.resize(len_mant, new_wp);
2081 lhs_mant = lhs.m_mant;
2085 rhs_mant = rhs.m_mant;
2098 if (rhs.m_msw < rhs.size() - 1 && rhs.m_mant[rhs.m_msw + 1 ] != 0) {
2102 int lhs_size = lhs.m_msw - lhs.m_lsw + 1;
2103 int rhs_size = rhs.m_msw - rhs.m_lsw + 1;
2105 int size =
sc_min(lhs_size, rhs_size);
2107 int lhs_index = lhs.m_msw;
2108 int rhs_index = rhs.m_msw;
2113 i < size && lhs.m_mant[lhs_index] == rhs.m_mant[rhs_index];
2120 if (lhs_size == rhs_size) {
2124 if (lhs_size < rhs_size) {
2131 if (lhs.m_mant[lhs_index] < rhs.m_mant[rhs_index]) {
2146 #if defined(SC_BOOST_BIG_ENDIAN)
2148 #elif defined(SC_BOOST_LITTLE_ENDIAN)
2157 #if defined(SC_BOOST_BIG_ENDIAN)
2158 for (
int i = 0, end = (
m_msw -
m_wp + 1) * 2;
i < end;
i++) {
2159 #elif defined(SC_BOOST_LITTLE_ENDIAN)
2160 for (
int i = 0, end = -(
m_msw -
m_wp + 1) * 2;
i > end;
i--) {
2197 mant8[0] = (
m_mant[0] << 3);
2198 mant2[0] = (
m_mant[0] << 1);
2318 "shift_left overflow");
2320 for (
int i =
size() - 1;
i > 0;
i--) {
2344 for (
int i = 0;
i <
size() - 1;
i++) {
2367 if (
x.wi() >=
size())
2375 bool result = (
m_mant[
x.wi()] & (1 <<
x.bi())) != 0;
2397 if (
x.wi() >=
size()) {
2402 }
else if (
x.wi() < 0) {
2411 if (
i == params.
iwl() - 1)
2436 if (
x.wi() >=
size()) {
2441 }
else if (
x.wi() < 0) {
2449 if (
i == params.
iwl() - 1)
2528 os <<
"scfx_rep" << ::std::endl;
2529 os <<
"(" << ::std::endl;
2531 os <<
"mant =" << ::std::endl;
2532 for (
int i =
size() - 1;
i >= 0;
i--) {
2534 std::sprintf(buf,
" %d: %10u (%8x)",
i,
2536 os << buf << ::std::endl;
2539 os <<
"wp = " <<
m_wp << ::std::endl;
2540 os <<
"sign = " <<
m_sign << ::std::endl;
2551 os <<
"not_a_number";
2558 os <<
"msw = " <<
m_msw << ::std::endl;
2559 os <<
"lsw = " <<
m_lsw << ::std::endl;
2561 os <<
")" << ::std::endl;
2624 if (wl_effective <= wl)
2632 if (wl_effective <= wl)