Menu

#968 conversion from unsigned to signed using $signed()

devel
closed-fixed
None
2
2015-01-10
2015-01-07
Iztok Jeras
No

The next code should produce a negative value (-0.007812) but instead produced (1.992188):
$itor($signed(8'hff))
I did not try exactly this code, my example code tested against ModelSim is here:
https://github.com/jeras/rp_8bit/tree/master/tmp/flags

Regards,
Iztok Jeras

Discussion

  • Martin Whitaker

    Martin Whitaker - 2015-01-07

    I'm a little puzzled by your expected results - I would expect $itor($signed(8'hff)) to have the value -1. Indeed, if I write

    initial $display($itor($signed(8'hff)));
    

    Icarus outputs the result -1.0. However, if I write

    reg [7:0] value = 8'hff;
    initial $display($itor($signed(value)));
    

    Icarus outputs the result 255.0, so it does look like there's a bug here. The difference is that the first result is calculated at compile time, and the second is calculated at run time.

     
  • Cary R.

    Cary R. - 2015-01-07

    Note that the second example has a race in that the initialization of value and the display may happen in any order. Icarus has a deterministic order for this, but that may not hold for other simulators. SystemVerilog fixes this race, but this is normal Verilog and Icarus does not currently implement the SystemVerilog initialization step.

    This should also be checked against other simulators since cvc and gplcver produce 255 for both examples. Icarus obviously has a bug since it gives different results depending on the context, but we need to make sure we pick the correct result. Though I agree, -1.0 would be what I expect for this expression.

    Skipping $signed() and using signed values 8'shff and reg signed both produce -1.0.

    I am attaching the example code I used.

     
  • Martin Whitaker

    Martin Whitaker - 2015-01-10
    • assigned_to: Martin Whitaker
     
  • Martin Whitaker

    Martin Whitaker - 2015-01-10

    I've checked that Cary's test case passes with several other simulators. I'm looking at this now.

     
  • Martin Whitaker

    Martin Whitaker - 2015-01-10
    • status: open --> closed-fixed
     
  • Martin Whitaker

    Martin Whitaker - 2015-01-10

    I've pushed a fix for this to both the master and v0.9 branches.

     

Log in to post a comment.