@@ -1160,11 +1160,19 @@ public struct ${Self}
1160
1160
public init( _ source: ${ FloatType} ) {
1161
1161
_precondition ( source. isFinite,
1162
1162
" ${FloatType} value cannot be converted to ${Self} because it is either infinite or NaN " )
1163
- % if not ( FloatBits == 16 and bits >= 32 ) : # Float16 is always in- range for 32 - and 64 - bit ints.
1163
+ % if FloatBits == 16 and signed and bits >= 32 :
1164
+ // A Float16 value, if finite, is always in-range for 32- and 64-bit signed
1165
+ // integer types.
1166
+ % else:
1164
1167
_precondition( source > ${ str ( lower) } . 0 ,
1165
1168
" ${FloatType} value cannot be converted to ${Self} because the result would be less than ${Self}.min " )
1169
+ % if FloatBits == 16 and not signed and bits >= 16 :
1170
+ // A Float16 value, if greater than -1 and finite, is always in-range for
1171
+ // 16-, 32-, and 64-bit unsigned integer types.
1172
+ % else:
1166
1173
_precondition( source < ${ str ( upper) } . 0 ,
1167
1174
" ${FloatType} value cannot be converted to ${Self} because the result would be greater than ${Self}.max " )
1175
+ % end
1168
1176
% end
1169
1177
self . _value = Builtin . fpto ${ u} i_FPIEEE${ FloatBits} _${ BuiltinName} ( source. _value)
1170
1178
}
@@ -1191,10 +1199,16 @@ public struct ${Self}
1191
1199
// The value passed as `source` must not be infinite, NaN, or exceed the
1192
1200
// bounds of the integer type; the result of `fptosi` or `fptoui` is
1193
1201
// undefined if it overflows.
1194
- % if not ( FloatBits == 16 and bits >= 32 ) : # Float 16 is always in - range for 32 - and 64 - bit ints .
1195
- guard source > $ { str ( lower ) } . 0 && source < $ { str ( upper ) } . 0 else {
1196
- % else :
1202
+ % if FloatBits == 16 and signed and bits >= 32 :
1203
+ // A Float16 value, if finite, is always in-range for 32- and 64-bit signed
1204
+ // integer types.
1197
1205
guard source. isFinite else {
1206
+ % elif FloatBits == 16 and not signed and bits >= 16 :
1207
+ // A Float16 value, if greater than -1 and finite, is always in-range for
1208
+ // 16-, 32-, and 64-bit unsigned integer types.
1209
+ guard source > ${ str ( lower) } . 0 && source. isFinite else {
1210
+ % else :
1211
+ guard source > ${ str ( lower) } . 0 && source < ${ str ( upper) } . 0 else {
1198
1212
% end
1199
1213
return nil
1200
1214
}
0 commit comments