📄 sc_nbutils.h
字号:
#ifdef DEBUG_SYSTEMC assert((n > 0) && (u != NULL) && (v != NULL));#endif for (register int i = 0; i < n; ++i) u[i] = v[i];}// Copy v to u, where ulen >= vlen, and zero the rest of the digits in u.inlinevoidvec_copy_and_zero(int ulen, sc_digit *u, int vlen, const sc_digit *v){#ifdef DEBUG_SYSTEMC assert((ulen > 0) && (u != NULL)); assert((vlen > 0) && (v != NULL)); assert(ulen >= vlen);#endif vec_copy(vlen, u, v); vec_zero(vlen, ulen, u);}// 2's-complement the digits in u.inlinevoidvec_complement(int ulen, sc_digit *u){#ifdef DEBUG_SYSTEMC assert((ulen > 0) && (u != NULL));#endif register sc_digit carry = 1; for (register int i = 0; i < ulen; ++i) { carry += (~u[i] & DIGIT_MASK); u[i] = carry & DIGIT_MASK; carry >>= BITS_PER_DIGIT; } }// ----------------------------------------------------------------------------// Functions to handle built-in types or signs.// ----------------------------------------------------------------------------// u = v// - v is an unsigned long or uint64, and positive integer.template< class Type >inlinevoidfrom_uint(int ulen, sc_digit *u, Type v){#ifdef DEBUG_SYSTEMC // assert((ulen <= 0) || (u != NULL)); assert((ulen > 0) && (u != NULL)); assert(v >= 0);#endif register int i = 0; while (v && (i < ulen)) {#ifndef WIN32 u[i++] = static_cast<sc_digit>( v & DIGIT_MASK );#else u[i++] = ((sc_digit) v) & DIGIT_MASK;#endif v >>= BITS_PER_DIGIT; } vec_zero(i, ulen, u);}// Get u's sign and return its absolute value.// u can be long, unsigned long, int64, or uint64.template< class Type >inlinesmall_typeget_sign(Type &u) { if (u > 0) return SC_POS; else if (u == 0) return SC_ZERO; else { u = -u; return SC_NEG; }}// Return us * vs:// - Return SC_ZERO if either sign is SC_ZERO.// - Return SC_POS if us == vs// - Return SC_NEG if us != vs.inlinesmall_typemul_signs(small_type us, small_type vs){ if ((us == SC_ZERO) || (vs == SC_ZERO)) return SC_ZERO; if (us == vs) return SC_POS; return SC_NEG;}// ----------------------------------------------------------------------------// Functions to test for errors and print out error messages.// ----------------------------------------------------------------------------#ifdef SC_MAX_NBITSinlinevoidtest_bound(int nb){ if (nb > SC_MAX_NBITS) { char msg[BUFSIZ]; std::sprintf( msg, "test_bound( int nb ) : " "nb = %d > SC_MAX_NBITS = %d is not valid", nb, SC_MAX_NBITS ); SC_REPORT_ERROR( sc_core::SC_ID_OUT_OF_BOUNDS_, msg ); }}#endiftemplate< class Type >inlinevoiddiv_by_zero(Type s){ if (s == 0) { SC_REPORT_ERROR( sc_core::SC_ID_OPERATION_FAILED_, "div_by_zero<Type>( Type ) : division by zero" ); }}// ----------------------------------------------------------------------------// Functions to check if a given vector is zero or make one.// ----------------------------------------------------------------------------// If u[i] is zero for every i = 0,..., ulen - 1, return SC_ZERO,// else return s.inlinesmall_type check_for_zero(small_type s, int ulen, const sc_digit *u){#ifdef DEBUG_SYSTEMC // assert(ulen >= 0); assert((ulen > 0) && (u != NULL));#endif if (vec_find_first_nonzero(ulen, u) < 0) return SC_ZERO; return s;}// If u[i] is zero for every i = 0,..., ulen - 1, return true,// else return false.inlineboolcheck_for_zero(int ulen, const sc_digit *u){#ifdef DEBUG_SYSTEMC // assert(ulen >= 0); assert((ulen > 0) && (u != NULL));#endif if (vec_find_first_nonzero(ulen, u) < 0) return true; return false;}inlinesmall_typemake_zero(int nd, sc_digit *d){ vec_zero(nd, d); return SC_ZERO;}// ----------------------------------------------------------------------------// Functions for both signed and unsigned numbers to convert sign-magnitude// (SM) and 2's complement (2C) representations.// added = 1 => for signed.// added = 0 => for unsigned.// IF_SC_SIGNED can be used as 'added'.// ----------------------------------------------------------------------------// Trim the extra leading bits of a signed or unsigned number.inlinevoidtrim(small_type added, int nb, int nd, sc_digit *d){#ifdef DEBUG_SYSTEMC assert((nb > 0) && (nd > 0) && (d != NULL));#endif d[nd - 1] &= one_and_ones(bit_ord(nb - 1) + added); }// Convert an (un)signed number from sign-magnitude representation to// 2's complement representation and trim the extra bits.inlinevoidconvert_SM_to_2C_trimmed(small_type added, small_type s, int nb, int nd, sc_digit *d){ if (s == SC_NEG) { vec_complement(nd, d); trim(added, nb, nd, d); }}// Convert an (un)signed number from sign-magnitude representation to// 2's complement representation but do not trim the extra bits.inlinevoidconvert_SM_to_2C(small_type s, int nd, sc_digit *d){ if (s == SC_NEG) vec_complement(nd, d);}// ----------------------------------------------------------------------------// Functions to convert between sign-magnitude (SM) and 2's complement// (2C) representations of signed numbers.// ----------------------------------------------------------------------------// Trim the extra leading bits off a signed number.inlinevoidtrim_signed(int nb, int nd, sc_digit *d){#ifdef DEBUG_SYSTEMC assert((nb > 0) && (nd > 0) && (d != NULL));#endif d[nd - 1] &= one_and_ones(bit_ord(nb - 1) + 1);}// Convert a signed number from 2's complement representation to// sign-magnitude representation, and return its sign. nd is d's// actual size, without zeros eliminated.inlinesmall_typeconvert_signed_2C_to_SM(int nb, int nd, sc_digit *d){#ifdef DEBUG_SYSTEMC assert((nb > 0) && (nd > 0) && (d != NULL));#endif small_type s; int xnb = bit_ord(nb - 1) + 1; // Test the sign bit. if (d[nd - 1] & one_and_zeros(xnb - 1)) { s = SC_NEG; vec_complement(nd, d); } else s = SC_POS; // Trim the last digit. d[nd - 1] &= one_and_ones(xnb); // Check if the new number is zero. if (s == SC_POS) return check_for_zero(s, nd, d); return s;}// Convert a signed number from sign-magnitude representation to 2's// complement representation, get its sign, convert back to// sign-magnitude representation, and return its sign. nd is d's// actual size, without zeros eliminated.inlinesmall_type convert_signed_SM_to_2C_to_SM(small_type s, int nb, int nd, sc_digit *d){ convert_SM_to_2C(s, nd, d); return convert_signed_2C_to_SM(nb, nd, d);}// Convert a signed number from sign-magnitude representation to 2's// complement representation and trim the extra bits.inlinevoidconvert_signed_SM_to_2C_trimmed(small_type s, int nb, int nd, sc_digit *d){ convert_SM_to_2C_trimmed(1, s, nb, nd, d);}// Convert a signed number from sign-magnitude representation to 2's// complement representation but do not trim the extra bits.inlinevoidconvert_signed_SM_to_2C(small_type s, int nd, sc_digit *d){ convert_SM_to_2C(s, nd, d);}// ----------------------------------------------------------------------------// Functions to convert between sign-magnitude (SM) and 2's complement// (2C) representations of unsigned numbers.// ----------------------------------------------------------------------------// Trim the extra leading bits off an unsigned number.inlinevoidtrim_unsigned(int nb, int nd, sc_digit *d){#ifdef DEBUG_SYSTEMC assert((nb > 0) && (nd > 0) && (d != NULL));#endif d[nd - 1] &= one_and_ones(bit_ord(nb - 1)); }// Convert an unsigned number from 2's complement representation to// sign-magnitude representation, and return its sign. nd is d's// actual size, without zeros eliminated.inlinesmall_typeconvert_unsigned_2C_to_SM(int nb, int nd, sc_digit *d){ trim_unsigned(nb, nd, d); return check_for_zero(SC_POS, nd, d);}// Convert an unsigned number from sign-magnitude representation to// 2's complement representation, get its sign, convert back to// sign-magnitude representation, and return its sign. nd is d's// actual size, without zeros eliminated.inlinesmall_typeconvert_unsigned_SM_to_2C_to_SM(small_type s, int nb, int nd, sc_digit *d){ convert_SM_to_2C(s, nd, d); return convert_unsigned_2C_to_SM(nb, nd, d);}// Convert an unsigned number from sign-magnitude representation to// 2's complement representation and trim the extra bits.inlinevoidconvert_unsigned_SM_to_2C_trimmed(small_type s, int nb, int nd, sc_digit *d){ convert_SM_to_2C_trimmed(0, s, nb, nd, d);}// Convert an unsigned number from sign-magnitude representation to// 2's complement representation but do not trim the extra bits.inlinevoidconvert_unsigned_SM_to_2C(small_type s, int nd, sc_digit *d){ convert_SM_to_2C(s, nd, d);}// ----------------------------------------------------------------------------// Functions to copy one (un)signed number to another.// ----------------------------------------------------------------------------// Copy v to u.inlinevoidcopy_digits_signed(small_type &us, int unb, int und, sc_digit *ud, int vnb, int vnd, const sc_digit *vd){ if (und <= vnd) { vec_copy(und, ud, vd); if (unb <= vnb) us = convert_signed_SM_to_2C_to_SM(us, unb, und, ud); } else // und > vnd vec_copy_and_zero(und, ud, vnd, vd);}// Copy v to u.inlinevoidcopy_digits_unsigned(small_type &us, int unb, int und, sc_digit *ud, int vnb, int vnd, const sc_digit *vd){ if (und <= vnd) vec_copy(und, ud, vd); else // und > vnd vec_copy_and_zero(und, ud, vnd, vd); us = convert_unsigned_SM_to_2C_to_SM(us, unb, und, ud);}// ----------------------------------------------------------------------------// Faster set(i, v), without bound checking.// ----------------------------------------------------------------------------// A version of set(i, v) without bound checking.inlinevoidsafe_set(int i, bool v, sc_digit *d){#ifdef DEBUG_SYSTEMC assert((i >= 0) && (d != NULL));#endif int bit_num = bit_ord(i); int digit_num = digit_ord(i); if (v) d[digit_num] |= one_and_zeros(bit_num); else d[digit_num] &= ~(one_and_zeros(bit_num)); }// ----------------------------------------------------------------------------// Function to check if a double number is bad (NaN or infinite).// ----------------------------------------------------------------------------inlinevoidis_bad_double(double v){// Windows throws exception.#if !defined(WIN32) && !defined(i386) && !defined(__x86_64__) && !defined( __EDG__ )#if defined( __hpux ) && defined( isfinite ) // HP-UX 11.00 does not have finite anymore if( ! isfinite( v ) ) {#else if (! finite(v)) {#endif SC_REPORT_ERROR( sc_core::SC_ID_VALUE_NOT_VALID_, "is_bad_double( double v ) : " "v is not finite - NaN or Inf" ); }#endif}} // namespace sc_dt#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -