compiler: fix float <-> int conversions

Этот коммит содержится в:
Ayke van Laethem 2018-11-03 12:20:55 +01:00
родитель 8da1a5bc17
коммит e66d457c42
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
3 изменённых файлов: 21 добавлений и 6 удалений

Просмотреть файл

@ -3306,19 +3306,19 @@ func (c *Compiler) parseConvert(typeFrom, typeTo types.Type, value llvm.Value) (
if typeFrom.Info()&types.IsFloat != 0 && typeTo.Info()&types.IsInteger != 0 {
// Conversion from float to int.
if typeTo.Info()&types.IsUnsigned != 0 { // to signed int
return c.builder.CreateFPToSI(value, llvmTypeTo, ""), nil
} else { // to unsigned int
if typeTo.Info()&types.IsUnsigned != 0 { // if unsigned
return c.builder.CreateFPToUI(value, llvmTypeTo, ""), nil
} else { // if signed
return c.builder.CreateFPToSI(value, llvmTypeTo, ""), nil
}
}
if typeFrom.Info()&types.IsInteger != 0 && typeTo.Info()&types.IsFloat != 0 {
// Conversion from int to float.
if typeFrom.Info()&types.IsUnsigned != 0 { // from signed int
return c.builder.CreateSIToFP(value, llvmTypeTo, ""), nil
} else { // from unsigned int
if typeFrom.Info()&types.IsUnsigned != 0 { // if unsigned
return c.builder.CreateUIToFP(value, llvmTypeTo, ""), nil
} else { // if signed
return c.builder.CreateSIToFP(value, llvmTypeTo, ""), nil
}
}

13
testdata/float.go предоставленный
Просмотреть файл

@ -24,6 +24,19 @@ func main() {
println(float32(f64))
println(float64(f32))
// float -> int
var f1 float32 = 3.3
var f2 float32 = 5.7
var f3 float32 = -2.3
var f4 float32 = -11.8
println(int32(f1), int32(f2), int32(f3), int32(f4))
// int -> float
var i1 int32 = 53
var i2 int32 = -8
var i3 uint32 = 20
println(float32(i1), float32(i2), float32(i3))
// complex64
c64 := complex(f32, 1.2)
println(c64)

2
testdata/float.txt предоставленный
Просмотреть файл

@ -11,6 +11,8 @@
+3.333333e-001
+6.666667e-001
+6.666667e-001
3 5 -2 -11
+5.300000e+001 -8.000000e+000 +2.000000e+001
(+6.666667e-001+1.200000e+000i)
+6.666667e-001
+1.200000e+000