13
13
import SwiftShims
14
14
15
15
% {
16
+ from SwiftFloatingPointTypes import all_floating_point_types
17
+
16
18
#
17
19
# Utility code for later in this template
18
20
#
19
21
20
- # // Bit counts for all floating point types.
21
- # // 80-bit floating point types are only permitted on x86 architectures. This
22
- # // restriction is handled via #if's in the generated code.
23
- allFloatBits = [ 32 , 64 , 80 ]
24
-
25
22
# Bit counts for all int types
26
23
allIntBits = [ 8 , 16 , 32 , 64 , 'Int']
27
24
@@ -31,12 +28,6 @@ word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
31
28
# Number of bits in integer literals.
32
29
builtinIntLiteralBits = 2048
33
30
34
- # Mapping from float bits to significand bits
35
- explicitSignificandBits = { 32 : 24 , 64 : 53 , 80 : 64 }
36
- SignificandSizes = { 32 : 32 , 64 : 64 , 80 : 64 }
37
- SignificandBitCounts = { 32 : 23 , 64 : 52 , 80 : 63 }
38
- ExponentBitCounts = { 32 : 8 , 64 : 11 , 80 : 15 }
39
-
40
31
def allInts( ) :
41
32
for bits in allIntBits:
42
33
for signed in False, True:
@@ -50,21 +41,6 @@ def builtinIntName(name):
50
41
51
42
def intName( name, signed) :
52
43
return ( '' if signed else 'U') + baseIntName( name)
53
-
54
- floatName = { 32 : 'Float', 64 : 'Double', 80 : 'Float80 ' }
55
-
56
- def cFuncSuffix( bits) :
57
- if bits == 32 :
58
- return 'f'
59
- if bits == 64 :
60
- return ''
61
- if bits == 80 :
62
- return 'l'
63
-
64
- def intFormatFix( bits) :
65
- if bits == 'Int':
66
- return int( CMAKE_SIZEOF_VOID_P) * 8
67
- return bits
68
44
} %
69
45
70
46
// TODO: remove once integer proposal is available ----------------------------
@@ -79,12 +55,14 @@ extension UInt${bits} {
79
55
}
80
56
% end
81
57
82
- % for bits in allFloatBits :
58
+ % for self_type in all_floating_point_types ( ) :
83
59
% {
84
- Self = floatName [ bits]
85
- SignificandSize = SignificandSizes [ bits]
86
- SignificandBitCount = SignificandBitCounts [ bits]
87
- ExponentBitCount = ExponentBitCounts [ bits]
60
+ Self = self_type. stdlib_name
61
+ bits = self_type. bits
62
+ cFuncSuffix = self_type. cFuncSuffix
63
+ SignificandSize = self_type. significand_size
64
+ SignificandBitCount = self_type. significand_bits
65
+ ExponentBitCount = self_type. exponent_bits
88
66
RawSignificand = 'UInt' + str( SignificandSize)
89
67
90
68
if Self == 'Float':
@@ -601,7 +579,7 @@ extension ${Self}: BinaryFloatingPoint {
601
579
var other = other
602
580
_swift_stdlib_remainderl ( & self , & other)
603
581
% else :
604
- self = _swift_stdlib_remainder ${ cFuncSuffix ( bits ) } ( self , other)
582
+ self = _swift_stdlib_remainder ${ cFuncSuffix} ( self , other)
605
583
% end
606
584
}
607
585
@@ -615,7 +593,7 @@ extension ${Self}: BinaryFloatingPoint {
615
593
% if bits == 80 :
616
594
_swift_stdlib_squareRootl( & self )
617
595
% else:
618
- self = _swift_stdlib_squareRoot ${ cFuncSuffix ( bits ) } ( self )
596
+ self = _swift_stdlib_squareRoot ${ cFuncSuffix} ( self )
619
597
% end
620
598
}
621
599
@@ -867,8 +845,9 @@ extension ${Self} {
867
845
// Construction from other floating point numbers.
868
846
@_transparent
869
847
extension ${ Self} {
870
- % for srcBits in allFloatBits:
871
- % That = floatName [ srcBits]
848
+ % for src_type in all_floating_point_types ( ) :
849
+ % srcBits = src_type. bits
850
+ % That = src_type. stdlib_name
872
851
873
852
% if srcBits == 80 :
874
853
#if !os(Windows) && (arch(i386) || arch(x86_64))
@@ -1077,62 +1056,14 @@ public postfix func -- (lhs: inout ${Self}) -> ${Self} {
1077
1056
fatalError( " -- is not available" )
1078
1057
}
1079
1058
1059
+ extension ${ Self} {
1060
+ @available ( * , unavailable, message: " Please use the `abs(_:)` free function " )
1061
+ public static func abs( _ x: ${ Self} ) -> ${ Self} {
1062
+ fatalError( " unavailable" )
1063
+ }
1064
+ }
1065
+
1080
1066
% if bits == 80:
1081
1067
#endif
1082
1068
% end
1083
- % 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( )
1069
+ % end # for bits in all_floating_point_types
0 commit comments