Skip to content

Commit f3f54ec

Browse files
authored
Merge pull request swiftlang#15048 from apple/revert-15021-better-nextups-and-ulps
2 parents 2de30ab + 7469783 commit f3f54ec

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

stdlib/public/core/FloatingPointTypes.swift.gyb

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -625,16 +625,7 @@ extension ${Self}: BinaryFloatingPoint {
625625
/// almost never is.
626626
@_inlineable // FIXME(sil-serialize-all)
627627
public var ulp: ${Self} {
628-
%if bits != 80:
629-
guard _fastPath(isFinite) else { return .nan }
630-
if _fastPath(isNormal) {
631-
let bitPattern_ = bitPattern & ${Self}.infinity.bitPattern
632-
return ${Self}(bitPattern: bitPattern_) * 0x1p-${SignificandBitCount}
633-
}
634-
// On arm, flush subnormal values to 0.
635-
return .leastNormalMagnitude * 0x1p-${SignificandBitCount}
636-
%else:
637-
guard _fastPath(isFinite) else { return .nan }
628+
if !isFinite { return ${Self}.nan }
638629
if exponentBitPattern > UInt(${Self}.significandBitCount) {
639630
// self is large enough that self.ulp is normal, so we just compute its
640631
// exponent and construct it with a significand of zero.
@@ -654,7 +645,6 @@ extension ${Self}: BinaryFloatingPoint {
654645
return ${Self}(sign: .plus,
655646
exponentBitPattern: 0,
656647
significandBitPattern: 1)
657-
%end
658648
}
659649

660650
/// The least positive normal number.
@@ -927,23 +917,17 @@ extension ${Self}: BinaryFloatingPoint {
927917
/// - If `x` is `greatestFiniteMagnitude`, then `x.nextUp` is `infinity`.
928918
@_inlineable // FIXME(sil-serialize-all)
929919
public var nextUp: ${Self} {
930-
%if bits != 80:
931-
// Silence signaling NaNs, map -0 to +0.
932-
let x = self + 0
920+
if isNaN { return self }
921+
if sign == .minus {
933922
#if arch(arm)
934-
// On arm, treat subnormal values as zero.
935-
if _slowPath(x == 0) { return .leastNonzeroMagnitude }
936-
if _slowPath(x == -.leastNonzeroMagnitude) { return -0.0 }
923+
// On arm, subnormals are flushed to zero.
924+
if (exponentBitPattern == 1 && significandBitPattern == 0) ||
925+
(exponentBitPattern == 0 && significandBitPattern != 0) {
926+
return ${Self}(sign: .minus,
927+
exponentBitPattern: 0,
928+
significandBitPattern: 0)
929+
}
937930
#endif
938-
if _fastPath(x < .infinity) {
939-
let increment = Int${bits}(bitPattern: x.bitPattern) &>> ${bits - 1} | 1
940-
let bitPattern_ = x.bitPattern &+ UInt${bits}(bitPattern: increment)
941-
return ${Self}(bitPattern: bitPattern_)
942-
}
943-
return x
944-
%else:
945-
if isNaN { /* Silence signaling NaNs. */ return self + 0 }
946-
if sign == .minus {
947931
if significandBitPattern == 0 {
948932
if exponentBitPattern == 0 {
949933
return .leastNonzeroMagnitude
@@ -962,10 +946,15 @@ extension ${Self}: BinaryFloatingPoint {
962946
exponentBitPattern: exponentBitPattern + 1,
963947
significandBitPattern: 0)
964948
}
949+
#if arch(arm)
950+
// On arm, subnormals are skipped.
951+
if exponentBitPattern == 0 {
952+
return .leastNonzeroMagnitude
953+
}
954+
#endif
965955
return ${Self}(sign: .plus,
966956
exponentBitPattern: exponentBitPattern,
967957
significandBitPattern: significandBitPattern + 1)
968-
%end
969958
}
970959

971960
/// Rounds the value to an integral value using the specified rounding rule.
@@ -1380,19 +1369,7 @@ extension ${Self}: BinaryFloatingPoint {
13801369
/// // y.exponent == 4
13811370
@_inlineable // FIXME(sil-serialize-all)
13821371
public var binade: ${Self} {
1383-
%if bits != 80:
1384-
guard _fastPath(isFinite) else { return .nan }
1385-
#if !arch(arm)
1386-
if _slowPath(isSubnormal) {
1387-
let bitPattern_ =
1388-
(self * 0x1p${SignificandBitCount}).bitPattern
1389-
& (-${Self}.infinity).bitPattern
1390-
return ${Self}(bitPattern: bitPattern_) * 0x1p-${SignificandBitCount}
1391-
}
1392-
#endif
1393-
return ${Self}(bitPattern: bitPattern & (-${Self}.infinity).bitPattern)
1394-
%else:
1395-
guard _fastPath(isFinite) else { return .nan }
1372+
if !isFinite { return .nan }
13961373
if exponentBitPattern != 0 {
13971374
return ${Self}(sign: sign, exponentBitPattern: exponentBitPattern,
13981375
significandBitPattern: 0)
@@ -1402,7 +1379,6 @@ extension ${Self}: BinaryFloatingPoint {
14021379
let index = significandBitPattern._binaryLogarithm()
14031380
return ${Self}(sign: sign, exponentBitPattern: 0,
14041381
significandBitPattern: 1 &<< index)
1405-
%end
14061382
}
14071383

14081384
/// The number of bits required to represent the value's significand.
@@ -1514,7 +1490,6 @@ extension ${Self} : _ExpressibleByBuiltinFloatLiteral {
15141490
% if bits == builtinFloatLiteralBits:
15151491
self = ${Self}(_bits: value)
15161492
% elif bits < builtinFloatLiteralBits:
1517-
// FIXME: This can result in double rounding errors (SR-7124).
15181493
self = ${Self}(_bits: Builtin.fptrunc_FPIEEE${builtinFloatLiteralBits}_FPIEEE${bits}(value))
15191494
% else:
15201495
// FIXME: This is actually losing precision <rdar://problem/14073102>.

0 commit comments

Comments
 (0)