@@ -115,74 +115,49 @@ 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
128
% OtherMin = int_ty.min
125
129
% OtherMax = int_ty.max
126
130
% (FloatMin, FloatMax) = getFtoIBounds(self_type.bits, int_ty.bits, int_ty.is_signed)
127
131
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
-
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})
141
- }
142
-
143
- % else:
144
-
145
- FixedPointConversionTruncations.test("${OtherInt}to${Self}Truncation/${testValue}") {
146
- let value: ${OtherInt} = ${testValue}
147
- let result = ${Self}(value)
148
- expectNotEqual(${OtherInt}(result), value)
149
- }
150
-
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)
132
+ FixedPointConversionTruncations.test("${OtherInt}to${Self}")
133
+ .forEach(in: [
134
+ (0, 0, 0),
135
+ % if int_ty.bits > self_type.significand_bits + 1:
136
+ % limit = ~(~0 << (self_type.significand_bits + 1))
137
+ % over = ~(~0 << (self_type.significand_bits + 2))
138
+ (${limit}, ${limit}, ${limit}),
139
+ (${over}, ${over + 1}, nil),
140
+ % if int_ty.is_signed:
141
+ (-${limit}, -${limit}, -${limit}),
142
+ (-${over}, -${over + 1}, nil),
143
+ % end
144
+ % else:
145
+ (${OtherInt}.min, ${OtherInt}.min, ${OtherInt}.min),
146
+ (${OtherInt}.max, ${OtherInt}.max, ${OtherInt}.max),
147
+ % end
148
+ ] as [(${OtherInt}, ${OtherInt}, ${OtherInt}?)]) { value, roundedExpectation, exactExpectation in
149
+ let roundedResult = ${Self}(value)
150
+ expectEqual(roundedResult, ${Self}(roundedExpectation))
151
+
152
+ let exactResult = ${Self}(exactly: value)
153
+ if let expectation = exactExpectation {
154
+ expectEqual(exactResult!, ${Self}(expectation))
155
+ } else {
156
+ expectNil(exactResult)
157
+ }
156
158
}
157
159
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
160
+ % end # for in int_types
186
161
187
162
% if Self == 'Float80':
188
163
#endif
0 commit comments