Skip to content

Commit

Permalink
cmd/compile: Fix float32 constant generation
Browse files Browse the repository at this point in the history
Fixes golang#13

math.Float32bits returns a uint32, and then a cast to int64 used to convert
it into a 64bit immediate which overflowed the signed int32 bounds.
Casting it into a signed int32 first forces the compiler to sign extend
the value returned by math.Float32bits which correctly generates the 32bit
signed immediate for the MOV instruction.

Change-Id: Ia531a26eeb4e344aff490ab73a35b8cf31c9e6dc
  • Loading branch information
ammubhave committed Nov 17, 2016
1 parent f3de070 commit 9f00711
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion riscvtest/fmv.go
Expand Up @@ -2,5 +2,14 @@ package main

func main() {
a := float32(5)
riscvexit(int(a))
if int(a) != 5 {
riscvexit(1)
}

b := float32(1e6)
if b < -1e7 || b > 1e7 {
riscvexit(2)
}

riscvexit(0)
}
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/riscv/ssa.go
Expand Up @@ -288,7 +288,7 @@ func ssaGenValue(s *gc.SSAGenState, v *ssa.Value) {
// Convert the float to the equivalent integer literal so we can
// move it using existing infrastructure.
p.From.Type = obj.TYPE_CONST
p.From.Offset = int64(math.Float32bits(float32(math.Float64frombits(uint64(v.AuxInt)))))
p.From.Offset = int64(int32(math.Float32bits(float32(math.Float64frombits(uint64(v.AuxInt))))))
p.To.Type = obj.TYPE_REG
p.To.Reg = v.Reg()
case ssa.OpRISCVMOVaddr:
Expand Down

0 comments on commit 9f00711

Please sign in to comment.