Skip to content

Commit 072ed8a

Browse files
committed
tests: add an end-to-end to check complete constant folding of generic floating point literal construction.
1 parent d977624 commit 072ed8a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

test/SILOptimizer/constant_fold_float.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,40 @@ public func fold_inf_inf_cmp() -> Bool {
1818
0x1.0p128 < Float.infinity
1919
}
2020

21+
public struct FP32: FixedPoint {
22+
public var bits: Int32
23+
24+
public init(bits: Bits) {
25+
self.bits = bits
26+
}
27+
}
28+
29+
// CHECK-LABEL: sil @$s4test0A19FloatLiteralFoldingAA4FP32VyF :
30+
// CHECK: [[L:%.*]] = integer_literal $Builtin.Int32, 65536
31+
// CHECK: [[I:%.*]] = struct $Int32 ([[L]])
32+
// CHECK: [[F:%.*]] = struct $FP32 ([[I]])
33+
// CHECK: return [[F]]
34+
// CHECK: } // end sil function '$s4test0A19FloatLiteralFoldingAA4FP32VyF'
35+
public func testFloatLiteralFolding() -> FP32 {
36+
return 1.0
37+
}
38+
39+
public protocol FixedPoint: ExpressibleByFloatLiteral {
40+
associatedtype Bits: FixedWidthInteger
41+
var bits: Bits { get set }
42+
43+
init(bits: Bits)
44+
}
45+
46+
extension FixedPoint {
47+
public init(floatLiteral value: Double) {
48+
self.init(value)
49+
}
50+
51+
init<F: BinaryFloatingPoint>(_ value: F) {
52+
let s = F(sign: .plus, exponent: F.Exponent(16), significand: 1)
53+
let r = (s * value).rounded(.toNearestOrEven)
54+
self.init(bits: Bits(exactly: r)!)
55+
}
56+
}
57+

0 commit comments

Comments
 (0)