Skip to content

Commit 1d6c3b8

Browse files
ultramiraculousChris Williams
authored andcommitted
Move Float->Int initializer to FixedPoint
1 parent 6b1210e commit 1d6c3b8

File tree

2 files changed

+42
-55
lines changed

2 files changed

+42
-55
lines changed

stdlib/public/core/FixedPoint.swift.gyb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
3333
# Number of bits in integer literals.
3434
builtinIntLiteralBits = 2048
3535

36+
# Mapping from float bits to significand bits
37+
allFloatBits = [32, 64, 80]
38+
floatName = { 32:'Float', 64:'Double', 80:'Float80' }
39+
explicitFloatSignificandBits = { 32:24, 64:53, 80:64 }
40+
41+
42+
def getFtoIBounds(floatBits, intBits, signed):
43+
if not signed:
44+
return (-1, 1 << intBits)
45+
upper = 1 << intBits - 1
46+
if intBits <= explicitFloatSignificandBits[floatBits]:
47+
return (-upper - 1, upper)
48+
ulp = 1 << intBits - explicitFloatSignificandBits[floatBits]
49+
return (-upper - ulp, upper)
50+
3651
def maskBits(n):
3752
"""Return an n-bit mask in hex"""
3853
return hexify((1 << n) - 1)
@@ -524,6 +539,33 @@ extension ${Self} {
524539
% end
525540

526541
extension ${Self} {
542+
// Construction of integers from floating point numbers.
543+
% for srcBits in allFloatBits:
544+
% Src = floatName[srcBits]
545+
% (lower, upper) = getFtoIBounds(srcBits, int(bits), signed)
546+
547+
% if srcBits == 80:
548+
#if !os(Windows) && (arch(i386) || arch(x86_64))
549+
% end
550+
551+
/// Creates a ${Self} whose value is `value` rounded towards zero.
552+
@_transparent
553+
public init(_ value: ${Src}) {
554+
_precondition(value.isFinite,
555+
"${Src} value cannot be converted to ${Self} because it is either infinite or NaN")
556+
_precondition(value > ${str(lower)}.0,
557+
"${Src} value cannot be converted to ${Self} because the result would be less than ${Self}.min")
558+
_precondition(value < ${str(upper)}.0,
559+
"${Src} value cannot be converted to ${Self} because the result would be greater than ${Self}.max")
560+
self._value = Builtin.fpto${sign}i_FPIEEE${srcBits}_${BuiltinName}(value._value)
561+
}
562+
563+
}
564+
% if srcBits == 80:
565+
#endif
566+
% end
567+
% end
568+
527569
/// Construct a `${Self}` having the same memory representation as
528570
/// the `${OtherSelf}` `bitPattern`. No range or overflow checking
529571
/// occurs, and the resulting `${Self}` may not have the same numeric

stdlib/public/core/FloatingPointTypes.swift.gyb

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,58 +1081,3 @@ public postfix func -- (lhs: inout ${Self}) -> ${Self} {
10811081
#endif
10821082
% end
10831083
% end # for bits in allFloatBits
1084-
1085-
// Construction of integers from floating point numbers.
1086-
%{
1087-
def getFtoIBounds(floatBits, intBits, signed):
1088-
if not signed:
1089-
return (-1, 1 << intBits)
1090-
upper = 1 << intBits - 1
1091-
if intBits <= explicitSignificandBits[floatBits]:
1092-
return (-upper - 1, upper)
1093-
ulp = 1 << intBits - explicitSignificandBits[floatBits]
1094-
return (-upper - ulp, upper)
1095-
}%
1096-
% for (bits, signed) in allInts():
1097-
% sign = 's' if signed else 'u'
1098-
% Self = intName(bits, signed)
1099-
% BuiltinName = builtinIntName(bits)
1100-
% intBits = intFormatFix(bits)
1101-
@_transparent
1102-
extension ${Self} {
1103-
% for srcBits in allFloatBits:
1104-
% That = floatName[srcBits]
1105-
1106-
% if srcBits == 80:
1107-
#if !os(Windows) && (arch(i386) || arch(x86_64))
1108-
% end
1109-
/// Creates a new instance by rounding the given floating-point value toward
1110-
/// zero.
1111-
///
1112-
/// - Parameter other: A floating-point value. When `other` is rounded toward
1113-
/// zero, the result must be within the range `${Self}.min...${Self}.max`.
1114-
public init(_ other: ${That}) {
1115-
_precondition(other.isFinite,
1116-
"${That} value cannot be converted to ${Self} because it is either infinite or NaN")
1117-
% (lower, upper) = getFtoIBounds(srcBits, int(intBits), signed)
1118-
_precondition(other > ${str(lower)}.0,
1119-
"${That} value cannot be converted to ${Self} because the result would be less than ${Self}.min")
1120-
_precondition(other < ${str(upper)}.0,
1121-
"${That} value cannot be converted to ${Self} because the result would be greater than ${Self}.max")
1122-
self._value = Builtin.fpto${sign}i_FPIEEE${srcBits}_${BuiltinName}(other._value)
1123-
}
1124-
% if srcBits == 80:
1125-
#endif
1126-
% end
1127-
% end
1128-
}
1129-
1130-
1131-
extension ${Self} {
1132-
@available(*, unavailable, message: "Please use the `abs(_:)` free function")
1133-
public static func abs(_ x: ${Self}) -> ${Self} {
1134-
fatalError("unavailable")
1135-
}
1136-
}
1137-
1138-
% end # for (bits, signed) in allInts()

0 commit comments

Comments
 (0)