Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug report: dependent arithmetic/muldiv single-precision operations cause wrong output #315

Closed
flaviens opened this issue Feb 28, 2023 · 1 comment

Comments

@flaviens
Copy link

flaviens commented Feb 28, 2023

Hi there!

I've detected a bug in VexRiscv.

Brief bug description

A pair of dependent single-precision floating arithmetic/muldiv operations (not necessarily immediately following each other), provides wrong results in some cases, differing by one from the expectation.

Example instance 1

fadd.s -> fsub.s

Snippet

Here is an example RISC-V (rv32imfd) snippet:

  .section ".text.init","ax",@progbits
  .globl _start
  .align 2
_start:
  # Enable the FPU
  li t0, 0x2000
  csrs mstatus, t0
  csrw	fcsr,x0

  la t0, .fdata0
  la t1, .fdata1

  fld ft0, (t0)
  fld ft3, (t1)

  fmul.s	ft1, ft3, ft3, rtz
  fsub.s ft2, ft0, ft1, rmm

  li t2, 0x18
  fsd ft2, 0(t2)

  sw x0, (x0)

infinite_loop:
  jal	x0,infinite_loop

.section ".fdata0","ax",@progbits
  .8byte 0xffffffff100e4696
.section ".fdata1","ax",@progbits
  .8byte 0xfffffffff6ad0aa4

Expected and actual results

We expect ft2= 0xffffffffff7fffff. I verified this with Spike.
However, VexRiscv returns ft0 = 0xffffffffff800000.

Example instance 2

fmul.s -> fsub.s

Snippet

Here is an example RISC-V (rv32imfd) snippet:

  .section ".text.init","ax",@progbits
  .globl _start
  .align 2
_start:
  # Enable the FPU
  li t0, 0x2000
  csrs mstatus, t0
  csrw	fcsr,x0

  la t0, .fdata0
  la t1, .fdata1
  la t2, .fdata2
  fld ft1, (t0)
  fld ft2, (t1)
  fld ft0, (t2)

  fadd.s ft3,ft0,ft1,rup
  fsub.s ft4,ft2,ft3,rdn

  li t2, 0x18
  fsd ft4, 0(t2)

  sw x0, (x0)

infinite_loop:
  jal	x0,infinite_loop

.section ".fdata0","ax",@progbits
  .8byte 0xffffffffab7d8deb
.section ".fdata1","ax",@progbits
  .8byte 0xffffffff2074e8c5
.section ".fdata2","ax",@progbits
  .8byte 0xffffffff0687a703

Expected and actual results

We expect ft4= 0xffffffff2b7d8ded. I verified this with Spike.
However, VexRiscv returns ft0 = 0xffffffff2b7d8dee.

Notes

There may be commonality in the root cause with #310, #311, #312, # 313 and #314, however this time we do not require conversions to trigger the bug.

Thanks!
Flavien

@flaviens flaviens changed the title Bug report: fmul.s followed by fsub.s causes wrong output Bug report: dependent single-precision operations cause wrong output Feb 28, 2023
@flaviens flaviens changed the title Bug report: dependent single-precision operations cause wrong output Bug report: dependent arithmetic/muldiv single-precision operations cause wrong output Feb 28, 2023
@Dolu1990
Copy link
Member

Dolu1990 commented Mar 1, 2023

5f67075 seems to fix it too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants