mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-17 11:13:27 +00:00
compiler: fix float <-> int conversions
This commit is contained in:
@@ -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 {
|
if typeFrom.Info()&types.IsFloat != 0 && typeTo.Info()&types.IsInteger != 0 {
|
||||||
// Conversion from float to int.
|
// Conversion from float to int.
|
||||||
if typeTo.Info()&types.IsUnsigned != 0 { // to signed int
|
if typeTo.Info()&types.IsUnsigned != 0 { // if unsigned
|
||||||
return c.builder.CreateFPToSI(value, llvmTypeTo, ""), nil
|
|
||||||
} else { // to unsigned int
|
|
||||||
return c.builder.CreateFPToUI(value, llvmTypeTo, ""), nil
|
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 {
|
if typeFrom.Info()&types.IsInteger != 0 && typeTo.Info()&types.IsFloat != 0 {
|
||||||
// Conversion from int to float.
|
// Conversion from int to float.
|
||||||
if typeFrom.Info()&types.IsUnsigned != 0 { // from signed int
|
if typeFrom.Info()&types.IsUnsigned != 0 { // if unsigned
|
||||||
return c.builder.CreateSIToFP(value, llvmTypeTo, ""), nil
|
|
||||||
} else { // from unsigned int
|
|
||||||
return c.builder.CreateUIToFP(value, llvmTypeTo, ""), nil
|
return c.builder.CreateUIToFP(value, llvmTypeTo, ""), nil
|
||||||
|
} else { // if signed
|
||||||
|
return c.builder.CreateSIToFP(value, llvmTypeTo, ""), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+13
@@ -24,6 +24,19 @@ func main() {
|
|||||||
println(float32(f64))
|
println(float32(f64))
|
||||||
println(float64(f32))
|
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
|
// complex64
|
||||||
c64 := complex(f32, 1.2)
|
c64 := complex(f32, 1.2)
|
||||||
println(c64)
|
println(c64)
|
||||||
|
|||||||
Vendored
+2
@@ -11,6 +11,8 @@
|
|||||||
+3.333333e-001
|
+3.333333e-001
|
||||||
+6.666667e-001
|
+6.666667e-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+000i)
|
||||||
+6.666667e-001
|
+6.666667e-001
|
||||||
+1.200000e+000
|
+1.200000e+000
|
||||||
|
|||||||
Reference in New Issue
Block a user