Joshua Landau

  • Content count

  • Joined

  • Last visited

  1. I don't have the test files on hand to double-check, but IIRC I believe it gets turned negative during the overflow step (called by cast, in turn called by sc_fxnum::sc_fxnum). Bear in mind that I'm new to SystemC, so I don't have a clear idea of what behaviours are expected or vice-versa.
  2. Thank you both. The explicit functions are implemented much the same way; static_cast<unsigned short>( m_rep->to_double() ) However, I expect you are right that doing the conversion before, rather than after, the multiplication by -1 is a better implementation than I gave. To elaborate, the code was invoking undefined behaviour before because b[i-1] was implicitly converting to double, which resulted in b[i-1] * i * -1 being a negative double, which was only then cast to ushort. An early cast would mean the multiplication results in an integral (likely of type int), which will then get truncated safely. On the other hand, this can cause UB: (uint)(sc_fix)(uint)-1 sc_fix wraps the (uint)-1 result back around to negative. Thus, this won't help if b[i-1] is ever expected to go over 32768, which it is.
  3. The regression suite, files systemc-regressions-2.3.1a/tests/systemc/datatypes/fx/constructors/array.cpp systemc-regressions-2.3.1a/tests/systemc/datatypes/fx/fast_constructors/array.cpp systemc-regressions-2.3.1a/tests/systemc/datatypes/misc/test02/test02.cpp invoke undefined behaviour by converting out-of-range doubles to unsigned integers directly. This is undefined in the C++ standard See for further information. This happens multiple most prominently in the code sc_fxval *b = new sc_fxval[4]; b[0] = (ushort)-1; for (i = 1; i < 4; ++i) b[i] = (ushort)(b[i-1] * i * -1); where negative doubles are converted to unsigned shorts, because sc_fxval only has conversions to double. It is unclear whether the test is correct and sc_fxval wrong, or vice-versa. If the later, one can avoid undefined behaviour by indirecting through a sufficiently large signed type. sc_fxval *b = new sc_fxval[4]; b[0] = (ushort)-1; for (i = 1; i < 4; ++i) b[i] = (ushort)(int64)(b[i-1] * i * -1); I was caught by this issue when testing a build of SystemC for AArch64, as part of a port I am making. As an aside, how (and where) should I attempt to upstream my port when it is ready?