@@ -115,74 +115,58 @@ FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/AlwaysSuc
115
115
116
116
% end # for in all_floating_point_types (Other)
117
117
118
- %{
118
+ #if arch(i386) || arch(arm)
119
+ % int_types = all_integer_types(32)
120
+ #elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
121
+ % int_types = all_integer_types(64)
122
+ #else
123
+ _UnimplementedError()
124
+ #endif
119
125
120
- float_to_int_conversion_template = gyb.parse_template("float_to_int_conversion",
121
- """
122
- % for int_ty in all_integer_types(word_bits):
126
+ % for int_ty in int_types:
123
127
% OtherInt = int_ty.stdlib_name
124
- % OtherMin = int_ty.min
125
- % OtherMax = int_ty.max
126
- % (FloatMin, FloatMax) = getFtoIBounds(self_type.bits, int_ty.bits, int_ty.is_signed)
127
-
128
- % for testValue in [0, FloatMin, FloatMax, FloatMin - 1, FloatMax + 1, OtherMin, OtherMax]:
129
-
130
- % if testValue < OtherMin or testValue > OtherMax:
131
- % # Can't construct `other` value, do nothing and continue.
132
128
133
- % elif testValue >= FloatMin and testValue <= FloatMax:
134
-
135
- FixedPointConversionTruncations.test("${OtherInt}to${Self}Conversion/${testValue}") {
136
- expectEqual(${Self}(${testValue} as ${OtherInt}), ${testValue})
137
- }
138
-
139
- FixedPointConversionFailures.test("${OtherInt}to${Self}FailableConversion/${testValue}") {
140
- expectEqual(${Self}(exactly: ${testValue} as ${OtherInt}), ${testValue})
129
+ extension ${OtherInt} {
130
+ static var _test${Self}Conversion: [(${OtherInt}, ${OtherInt}, ${OtherInt}?)] {
131
+ if bitWidth > ${Self}.significandBitCount + 1 {
132
+ let bitOffset = ${Self}.significandBitCount + 1
133
+ let limit: ${OtherInt} = ~(~0 << bitOffset)
134
+ let over: ${OtherInt} = 1 + limit << 1
135
+ return [
136
+ (0, 0, 0),
137
+ (limit, limit, limit),
138
+ (over, over + 1, nil),
139
+ % if int_ty.is_signed:
140
+ (-limit, -limit, -limit),
141
+ (-over, -(over + 1), nil),
142
+ % end
143
+ ]
144
+ } else {
145
+ return [
146
+ (0, 0, 0),
147
+ (.min, .min, .min),
148
+ (.max, .max, .max),
149
+ ]
150
+ }
151
+ }
141
152
}
142
153
143
- % else:
154
+ FixedPointConversionTruncations.test("${OtherInt}to${Self}")
155
+ .forEach(in: ${OtherInt}._test${Self}Conversion) {
156
+ value, roundedExpectation, exactExpectation in
144
157
145
- FixedPointConversionTruncations.test("${OtherInt}to${Self}Truncation/${testValue}") {
146
- let value: ${OtherInt} = ${testValue}
147
- let result = ${Self}(value)
148
- expectNotEqual(${OtherInt}(result), value)
149
- }
158
+ let roundedResult = ${Self}(value)
159
+ expectEqual(roundedResult, ${Self}(roundedExpectation))
150
160
151
- FixedPointConversionFailures.test("${OtherInt}to${Self}Failure/${testValue}") {
152
- let value: ${OtherInt} = ${testValue}
153
- let result = ${Self}(exactly: value)
154
- expectEqual(result, ${OtherMin} as ${Self})
155
- expectEqual(${OtherInt}(result!), value)
161
+ let exactResult = ${Self}(exactly: value)
162
+ if let expectation = exactExpectation {
163
+ expectEqual(exactResult!, ${Self}(expectation))
164
+ } else {
165
+ expectNil(exactResult)
166
+ }
156
167
}
157
168
158
- % end
159
-
160
- % end # testValue in testValues
161
- % end # for in all_integer_types (Other)
162
- """)
163
- }%
164
-
165
- #if arch(i386) || arch(arm)
166
-
167
- ${gyb.execute_template(
168
- float_to_int_conversion_template,
169
- word_bits=32,
170
- **locals()
171
- )}
172
-
173
- #elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
174
-
175
- ${gyb.execute_template(
176
- float_to_int_conversion_template,
177
- word_bits=64,
178
- **locals()
179
- )}
180
-
181
- #else
182
-
183
- _UnimplementedError()
184
-
185
- #endif
169
+ % end # for in int_types
186
170
187
171
% if Self == 'Float80':
188
172
#endif
0 commit comments