Skip to content

sc_bigint/sc_biguint set(int) and clear(int) leave stale bits in top digit #146

Description

@almalkim

Describe the bug

According to the IEEE Standard for SystemC® Language Reference Manual
(2023), section 7.2.9 — Bit-select for sc_signed and section 7.6.4.9
for sc_unsigned, writing to individual bits via operator[] shall
modify the value correctly.
The set(int i) and clear(int i) member
functions in both sc_signed and sc_unsigned modify digit[]
directly but do not call adjust_hod() afterwards.

adjust_hod() normalizes the top digit after any mutation:

  • sc_signed: Arithmetic sign-extends spare bits
  • sc_unsigned: Zero-fills spare bits

Without this call, the spare bits in the top digit contain stale data
from the previous value, causing to_int(), to_uint(), and other
accessors to return incorrect results.

To Reproduce

SystemC version: 3.0.2

Minimal Example

#include <systemc>
using namespace sc_dt;
using std::cout;
using std::endl;

int sc_main(int, char*[]) {
    sc_bigint<6> x;
    x = 0x2c; // -20 in 6-bit two's complement

    x[5] = 0; // clear sign bit via bitref (calls clear(5))
    cout << "After x[5]=0: to_int() = " << x.to_int() << "  (expect 12)" << endl;
    // Unfixed SystemC: returns -52 (stale sign-extension in digit[0])

    return 0;
}

Expected behavior

After clearing bit 5, sc_bigint<6>(0x2c) should become 001100 = 12.
to_int() should return 12.

Instead, to_int() returns -52 because digit[0] is 0xFFFFFFCC
(stale sign-extension bits) instead of 0x0000000C.

Logs / Output

Unfixed output:

After x[5]=0: to_int() = -52  (expect 12)

Fixed output:

After x[5]=0: to_int() = 12  (expect 12)

Environment

  • OS: RHEL 7 / Linux x86_64
  • Compiler: GCC 12.3.0
  • SystemC Version: 3.0.2-Accellera (commit 16b57dc38, main branch)

Additional context

Both sc_signed::set(int i) / sc_signed::clear(int i) and
sc_unsigned::set(int i) / sc_unsigned::clear(int i) are affected.
The fix is to add adjust_hod(); after modifying the bit in digit[].

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions