Implement fixed-width integer conversions from binary floating point

Make internal stdlib function public because it is called from stdlib tests

Add some first-thought optimizations
This commit is contained in:
Xiaodi Wu
2017-07-29 14:19:40 -05:00
parent 60a91bb736
commit 7e11f1822c
2 changed files with 184 additions and 71 deletions

View File

@@ -40,3 +40,89 @@ print(tentwenty)
// CHECK: 4294967295
// CHECK: 15
// CHECK: 1020
// Test generic conversions from floating point
print(Int8._convert(from: -128))
print(Int8._convert(from: 128))
print(Int8._convert(from: -127))
print(Int8._convert(from: 127))
// CHECK: (value: Optional(-128), exact: true)
// CHECK: (value: nil, exact: false)
// CHECK: (value: Optional(-127), exact: true)
// CHECK: (value: Optional(127), exact: true)
print(Int8._convert(from: -129))
print(Int8._convert(from: -128.999))
print(Int8._convert(from: -128.001))
print(Int8._convert(from: -127.999))
print(Int8._convert(from: -127.001))
print(Int8._convert(from: 127.001))
print(Int8._convert(from: 127.999))
// CHECK: (value: nil, exact: false)
// CHECK: (value: Optional(-128), exact: false)
// CHECK: (value: Optional(-128), exact: false)
// CHECK: (value: Optional(-127), exact: false)
// CHECK: (value: Optional(-127), exact: false)
// CHECK: (value: Optional(127), exact: false)
// CHECK: (value: Optional(127), exact: false)
print(Int8._convert(from: 0))
print(Int8._convert(from: -0.0))
print(Int8._convert(from: 0.001))
print(Int8._convert(from: -0.001))
// CHECK: (value: Optional(0), exact: true)
// CHECK: (value: Optional(0), exact: true)
// CHECK: (value: Optional(0), exact: false)
// CHECK: (value: Optional(0), exact: false)
print(Int8._convert(from: Double.leastNonzeroMagnitude))
print(Int8._convert(from: -Double.leastNonzeroMagnitude))
print(Int8._convert(from: Double.leastNormalMagnitude))
print(Int8._convert(from: -Double.leastNormalMagnitude))
// CHECK: (value: Optional(0), exact: false)
// CHECK: (value: Optional(0), exact: false)
// CHECK: (value: Optional(0), exact: false)
// CHECK: (value: Optional(0), exact: false)
print(UInt8._convert(from: -1))
print(UInt8._convert(from: 255))
print(UInt8._convert(from: 256))
print(UInt8._convert(from: Double.infinity))
print(UInt8._convert(from: -Double.infinity))
print(UInt8._convert(from: Double.nan))
// CHECK: (value: nil, exact: false)
// CHECK: (value: Optional(255), exact: true)
// CHECK: (value: nil, exact: false)
// CHECK: (value: nil, exact: false)
// CHECK: (value: nil, exact: false)
// CHECK: (value: nil, exact: false)
let f = Float(Int64.min)
let ff = Int64._convert(from: f)
let fff = Int64(f)
print(fff == ff.value!)
// CHECK: true
let g = f.nextUp
let gg = Int64._convert(from: g)
let ggg = Int64(g)
print(ggg == gg.value!)
// CHECK: true
let h = Float(Int64.max)
let hh = Int64._convert(from: h)
print(hh)
// CHECK: (value: nil, exact: false)
let i = h.nextDown
let ii = Int64._convert(from: i)
let iii = Int64(i)
print(iii == ii.value!)
// CHECK: true