Skip to content

[stdlib] Fix FloatingPoint.init(exactly:) #12739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions stdlib/public/core/FloatingPointTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1503,8 +1503,9 @@ extension ${Self} {
public init?(exactly value: ${That}) {
_value = Builtin.${sign}itofp_${ThatBuiltinName}_FPIEEE${bits}(value._value)

% if srcBits < SignificandBitCount:
if ${That}(self) != value {
% if srcBits >= SignificandBitCount:
guard let roundTrip = ${That}(exactly: self),
roundTrip == value else {
return nil
}
% end
Expand Down
16 changes: 8 additions & 8 deletions test/stdlib/TestNSNumberBridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -658,16 +658,16 @@ func testNSNumberBridgeFromUInt() {
let uint = (number!) as? UInt
expectEqual(UInt(exactly: interestingValue), uint)

// these are disabled because of https://bugs.swift.org/browse/SR-4634
if uint! != UInt(UInt32.max) && uint! != UInt(UInt32.max - 1) {
let float = (number!) as? Float
let expectedFloat = Float(uint!)
testFloat(expectedFloat, float)
let float = (number!) as? Float
let expectedFloat = Float(exactly: uint!)
if UInt.bitWidth == 32 && uint! >= UInt.max - 1 {
expectNil(expectedFloat)
} else {
testFloat(expectedFloat, float)
}



let double = (number!) as? Double
let expectedDouble = Double(uint!)
let expectedDouble = Double(exactly: uint!)
testDouble(expectedDouble, double)
}
let bridged = interestingValue as NSNumber
Expand Down
102 changes: 43 additions & 59 deletions validation-test/stdlib/FloatingPointConversion.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -115,74 +115,58 @@ FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/AlwaysSuc

% end # for in all_floating_point_types (Other)

%{
#if arch(i386) || arch(arm)
% int_types = all_integer_types(32)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
% int_types = all_integer_types(64)
#else
_UnimplementedError()
#endif

float_to_int_conversion_template = gyb.parse_template("float_to_int_conversion",
"""
% for int_ty in all_integer_types(word_bits):
% for int_ty in int_types:
% OtherInt = int_ty.stdlib_name
% OtherMin = int_ty.min
% OtherMax = int_ty.max
% (FloatMin, FloatMax) = getFtoIBounds(self_type.bits, int_ty.bits, int_ty.is_signed)

% for testValue in [0, FloatMin, FloatMax, FloatMin - 1, FloatMax + 1, OtherMin, OtherMax]:

% if testValue < OtherMin or testValue > OtherMax:
% # Can't construct `other` value, do nothing and continue.

% elif testValue >= FloatMin and testValue <= FloatMax:

FixedPointConversionTruncations.test("${OtherInt}to${Self}Conversion/${testValue}") {
expectEqual(${Self}(${testValue} as ${OtherInt}), ${testValue})
}

FixedPointConversionFailures.test("${OtherInt}to${Self}FailableConversion/${testValue}") {
expectEqual(${Self}(exactly: ${testValue} as ${OtherInt}), ${testValue})
extension ${OtherInt} {
static var _test${Self}Conversion: [(${OtherInt}, ${OtherInt}, ${OtherInt}?)] {
if bitWidth > ${Self}.significandBitCount + 1 {
let bitOffset = ${Self}.significandBitCount + 1
let limit: ${OtherInt} = ~(~0 << bitOffset)
let over: ${OtherInt} = 1 + limit << 1
return [
(0, 0, 0),
(limit, limit, limit),
(over, over + 1, nil),
% if int_ty.is_signed:
(-limit, -limit, -limit),
(-over, -(over + 1), nil),
% end
]
} else {
return [
(0, 0, 0),
(.min, .min, .min),
(.max, .max, .max),
]
}
}
}

% else:
FixedPointConversionTruncations.test("${OtherInt}to${Self}")
.forEach(in: ${OtherInt}._test${Self}Conversion) {
value, roundedExpectation, exactExpectation in

FixedPointConversionTruncations.test("${OtherInt}to${Self}Truncation/${testValue}") {
let value: ${OtherInt} = ${testValue}
let result = ${Self}(value)
expectNotEqual(${OtherInt}(result), value)
}
let roundedResult = ${Self}(value)
expectEqual(roundedResult, ${Self}(roundedExpectation))

FixedPointConversionFailures.test("${OtherInt}to${Self}Failure/${testValue}") {
let value: ${OtherInt} = ${testValue}
let result = ${Self}(exactly: value)
expectEqual(result, ${OtherMin} as ${Self})
expectEqual(${OtherInt}(result!), value)
let exactResult = ${Self}(exactly: value)
if let expectation = exactExpectation {
expectEqual(exactResult!, ${Self}(expectation))
} else {
expectNil(exactResult)
}
}

% end

% end # testValue in testValues
% end # for in all_integer_types (Other)
""")
}%

#if arch(i386) || arch(arm)

${gyb.execute_template(
float_to_int_conversion_template,
word_bits=32,
**locals()
)}

#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)

${gyb.execute_template(
float_to_int_conversion_template,
word_bits=64,
**locals()
)}

#else

_UnimplementedError()

#endif
% end # for in int_types

% if Self == 'Float80':
#endif
Expand Down
16 changes: 8 additions & 8 deletions validation-test/stdlib/ValidationNSNumberBridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -652,16 +652,16 @@ func testNSNumberBridgeFromUInt() {
let uint = (number!) as? UInt
expectEqual(UInt(exactly: interestingValue), uint)

// these are disabled because of https://bugs.swift.org/browse/SR-4634
if uint! != UInt(UInt32.max) && uint! != UInt(UInt32.max - 1) {
let float = (number!) as? Float
let expectedFloat = Float(uint!)
testFloat(expectedFloat, float)
let float = (number!) as? Float
let expectedFloat = Float(exactly: uint!)
if UInt.bitWidth == 32 && uint! >= UInt.max - 1 {
expectNil(expectedFloat)
} else {
testFloat(expectedFloat, float)
}



let double = (number!) as? Double
let expectedDouble = Double(uint!)
let expectedDouble = Double(exactly: uint!)
testDouble(expectedDouble, double)
}
let bridged = interestingValue as NSNumber
Expand Down