Skip to content

fmadd.s instruction can produce the wrong output #119

@KeshavBansal0122

Description

@KeshavBansal0122

When the final addition is between a negative float and 0, the output will be wrong

How to reproduce

li t2, 0x3f800000 # hex for one
li t1, 0xbea50ad3 # hex for -.3
fmv.s.x ft1, t1
fmv.s.x ft2, t2
fmv.s.x ft0, zero
# does ft0 = *ft1 * ft2) + ft0., with ft1=1,ft0=0.
# expected: ft0=ft2=0xbea50ad3
fmadd.s ft0, ft1, ft2, ft0


# print this in hex for verification
fmv.x.s a0, ft0
li a7, 34
ecall # should print 0xbea50ad3, but actually prints 0x3ea50ad3, value: +.3

Cause

When adding two floats of opposite sign, the code compares their magnitude to determine the final sign. This is done in the ExactFloat Class, and this comparison fails if other is 0. It considers 0 to have a larger magnitude, which causes the sign to flip

This issue may affect other fused operations but does not affect simple addition, since it checks the both operands for 0 before adding them.

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