@@ -13,6 +13,11 @@ pub(crate) fn f16_to_f32(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value
13
13
fx. lib_call ( "__extendhfsf2" , vec ! [ arg_ty] , vec ! [ AbiParam :: new( types:: F32 ) ] , & [ value] ) [ 0 ]
14
14
}
15
15
16
+ fn f16_to_f64 ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > , value : Value ) -> Value {
17
+ let ret = f16_to_f32 ( fx, value) ;
18
+ fx. bcx . ins ( ) . fpromote ( types:: F64 , ret)
19
+ }
20
+
16
21
pub ( crate ) fn f32_to_f16 ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > , value : Value ) -> Value {
17
22
let ret_ty = if fx. tcx . sess . target . vendor == "apple" && fx. tcx . sess . target . arch == "x86_64" {
18
23
types:: I16
@@ -28,6 +33,21 @@ pub(crate) fn f32_to_f16(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value
28
33
if ret_ty == types:: I16 { fx. bcx . ins ( ) . bitcast ( types:: F16 , MemFlags :: new ( ) , ret) } else { ret }
29
34
}
30
35
36
+ fn f64_to_f16 ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > , value : Value ) -> Value {
37
+ let ret_ty = if fx. tcx . sess . target . vendor == "apple" && fx. tcx . sess . target . arch == "x86_64" {
38
+ types:: I16
39
+ } else {
40
+ types:: F16
41
+ } ;
42
+ let ret = fx. lib_call (
43
+ "__truncdfhf2" ,
44
+ vec ! [ AbiParam :: new( types:: F64 ) ] ,
45
+ vec ! [ AbiParam :: new( ret_ty) ] ,
46
+ & [ value] ,
47
+ ) [ 0 ] ;
48
+ if ret_ty == types:: I16 { fx. bcx . ins ( ) . bitcast ( types:: F16 , MemFlags :: new ( ) , ret) } else { ret }
49
+ }
50
+
31
51
pub ( crate ) fn fcmp ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > , cc : FloatCC , lhs : Value , rhs : Value ) -> Value {
32
52
let ty = fx. bcx . func . dfg . value_type ( lhs) ;
33
53
match ty {
@@ -96,6 +116,109 @@ pub(crate) fn neg_f128(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
96
116
fx. bcx . ins ( ) . bitcast ( types:: F128 , MemFlags :: new ( ) , bits)
97
117
}
98
118
119
+ pub ( crate ) fn codegen_cast (
120
+ fx : & mut FunctionCx < ' _ , ' _ , ' _ > ,
121
+ from : Value ,
122
+ from_signed : bool ,
123
+ to_ty : Type ,
124
+ to_signed : bool ,
125
+ ) -> Value {
126
+ let from_ty = fx. bcx . func . dfg . value_type ( from) ;
127
+ if from_ty. is_float ( ) && to_ty. is_float ( ) {
128
+ let name = match ( from_ty, to_ty) {
129
+ ( types:: F16 , types:: F32 ) => return f16_to_f32 ( fx, from) ,
130
+ ( types:: F16 , types:: F64 ) => return f16_to_f64 ( fx, from) ,
131
+ ( types:: F16 , types:: F128 ) => "__extendhftf2" ,
132
+ ( types:: F32 , types:: F128 ) => "__extendsftf2" ,
133
+ ( types:: F64 , types:: F128 ) => "__extenddftf2" ,
134
+ ( types:: F128 , types:: F64 ) => "__trunctfdf2" ,
135
+ ( types:: F128 , types:: F32 ) => "__trunctfsf2" ,
136
+ ( types:: F128 , types:: F16 ) => "__trunctfhf2" ,
137
+ ( types:: F64 , types:: F16 ) => return f64_to_f16 ( fx, from) ,
138
+ ( types:: F32 , types:: F16 ) => return f32_to_f16 ( fx, from) ,
139
+ _ => unreachable ! ( "{from_ty:?} -> {to_ty:?}" ) ,
140
+ } ;
141
+ fx. lib_call ( name, vec ! [ AbiParam :: new( from_ty) ] , vec ! [ AbiParam :: new( to_ty) ] , & [ from] ) [ 0 ]
142
+ } else if from_ty. is_int ( ) && to_ty == types:: F16 {
143
+ let res = clif_int_or_float_cast ( fx, from, from_signed, types:: F32 , false ) ;
144
+ f32_to_f16 ( fx, res)
145
+ } else if from_ty == types:: F16 && to_ty. is_int ( ) {
146
+ let from = f16_to_f32 ( fx, from) ;
147
+ clif_int_or_float_cast ( fx, from, false , to_ty, to_signed)
148
+ } else if from_ty. is_int ( ) && to_ty == types:: F128 {
149
+ let ( from, from_ty) = if from_ty. bits ( ) < 32 {
150
+ ( clif_int_or_float_cast ( fx, from, from_signed, types:: I32 , from_signed) , types:: I32 )
151
+ } else {
152
+ ( from, from_ty)
153
+ } ;
154
+ let name = format ! (
155
+ "__float{sign}{size}itf" ,
156
+ sign = if from_signed { "" } else { "un" } ,
157
+ size = match from_ty {
158
+ types:: I32 => 's' ,
159
+ types:: I64 => 'd' ,
160
+ types:: I128 => 't' ,
161
+ _ => unreachable!( "{from_ty:?}" ) ,
162
+ } ,
163
+ ) ;
164
+ fx. lib_call (
165
+ & name,
166
+ vec ! [ lib_call_arg_param( fx. tcx, from_ty, from_signed) ] ,
167
+ vec ! [ AbiParam :: new( to_ty) ] ,
168
+ & [ from] ,
169
+ ) [ 0 ]
170
+ } else if from_ty == types:: F128 && to_ty. is_int ( ) {
171
+ let ret_ty = if to_ty. bits ( ) < 32 { types:: I32 } else { to_ty } ;
172
+ let name = format ! (
173
+ "__fix{sign}tf{size}i" ,
174
+ sign = if from_signed { "" } else { "un" } ,
175
+ size = match ret_ty {
176
+ types:: I32 => 's' ,
177
+ types:: I64 => 'd' ,
178
+ types:: I128 => 't' ,
179
+ _ => unreachable!( "{from_ty:?}" ) ,
180
+ } ,
181
+ ) ;
182
+ let ret =
183
+ fx. lib_call ( & name, vec ! [ AbiParam :: new( from_ty) ] , vec ! [ AbiParam :: new( to_ty) ] , & [ from] )
184
+ [ 0 ] ;
185
+ let val = if ret_ty == to_ty {
186
+ ret
187
+ } else {
188
+ let ( min, max) = match ( to_ty, to_signed) {
189
+ ( types:: I8 , false ) => ( 0 , i64:: from ( u8:: MAX ) ) ,
190
+ ( types:: I16 , false ) => ( 0 , i64:: from ( u16:: MAX ) ) ,
191
+ ( types:: I8 , true ) => ( i64:: from ( i8:: MIN as u32 ) , i64:: from ( i8:: MAX as u32 ) ) ,
192
+ ( types:: I16 , true ) => ( i64:: from ( i16:: MIN as u32 ) , i64:: from ( i16:: MAX as u32 ) ) ,
193
+ _ => unreachable ! ( "{to_ty:?}" ) ,
194
+ } ;
195
+ let min_val = fx. bcx . ins ( ) . iconst ( types:: I32 , min) ;
196
+ let max_val = fx. bcx . ins ( ) . iconst ( types:: I32 , max) ;
197
+
198
+ let val = if to_signed {
199
+ let has_underflow = fx. bcx . ins ( ) . icmp_imm ( IntCC :: SignedLessThan , ret, min) ;
200
+ let has_overflow = fx. bcx . ins ( ) . icmp_imm ( IntCC :: SignedGreaterThan , ret, max) ;
201
+ let bottom_capped = fx. bcx . ins ( ) . select ( has_underflow, min_val, ret) ;
202
+ fx. bcx . ins ( ) . select ( has_overflow, max_val, bottom_capped)
203
+ } else {
204
+ let has_overflow = fx. bcx . ins ( ) . icmp_imm ( IntCC :: UnsignedGreaterThan , ret, max) ;
205
+ fx. bcx . ins ( ) . select ( has_overflow, max_val, ret)
206
+ } ;
207
+ fx. bcx . ins ( ) . ireduce ( to_ty, val)
208
+ } ;
209
+
210
+ if let Some ( false ) = fx. tcx . sess . opts . unstable_opts . saturating_float_casts {
211
+ return val;
212
+ }
213
+
214
+ let is_not_nan = fcmp ( fx, FloatCC :: Equal , from, from) ;
215
+ let zero = type_zero_value ( & mut fx. bcx , to_ty) ;
216
+ fx. bcx . ins ( ) . select ( is_not_nan, val, zero)
217
+ } else {
218
+ unreachable ! ( "{from_ty:?} -> {to_ty:?}" ) ;
219
+ }
220
+ }
221
+
99
222
pub ( crate ) fn fmin_f128 ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > , a : Value , b : Value ) -> Value {
100
223
fx. lib_call (
101
224
"fminimumf128" ,
0 commit comments