@@ -13,6 +13,11 @@ pub(crate) fn f16_to_f32(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value
1313 fx. lib_call ( "__extendhfsf2" , vec ! [ arg_ty] , vec ! [ AbiParam :: new( types:: F32 ) ] , & [ value] ) [ 0 ]
1414}
1515
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+
1621pub ( crate ) fn f32_to_f16 ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > , value : Value ) -> Value {
1722 let ret_ty = if fx. tcx . sess . target . vendor == "apple" && fx. tcx . sess . target . arch == "x86_64" {
1823 types:: I16
@@ -28,6 +33,21 @@ pub(crate) fn f32_to_f16(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value
2833 if ret_ty == types:: I16 { fx. bcx . ins ( ) . bitcast ( types:: F16 , MemFlags :: new ( ) , ret) } else { ret }
2934}
3035
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+
3151pub ( crate ) fn fcmp ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > , cc : FloatCC , lhs : Value , rhs : Value ) -> Value {
3252 let ty = fx. bcx . func . dfg . value_type ( lhs) ;
3353 match ty {
@@ -99,6 +119,109 @@ pub(crate) fn neg_f128(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
99119 fx. bcx . ins ( ) . bitcast ( types:: F128 , MemFlags :: new ( ) , bits)
100120}
101121
122+ pub ( crate ) fn codegen_cast (
123+ fx : & mut FunctionCx < ' _ , ' _ , ' _ > ,
124+ from : Value ,
125+ from_signed : bool ,
126+ to_ty : Type ,
127+ to_signed : bool ,
128+ ) -> Value {
129+ let from_ty = fx. bcx . func . dfg . value_type ( from) ;
130+ if from_ty. is_float ( ) && to_ty. is_float ( ) {
131+ let name = match ( from_ty, to_ty) {
132+ ( types:: F16 , types:: F32 ) => return f16_to_f32 ( fx, from) ,
133+ ( types:: F16 , types:: F64 ) => return f16_to_f64 ( fx, from) ,
134+ ( types:: F16 , types:: F128 ) => "__extendhftf2" ,
135+ ( types:: F32 , types:: F128 ) => "__extendsftf2" ,
136+ ( types:: F64 , types:: F128 ) => "__extenddftf2" ,
137+ ( types:: F128 , types:: F64 ) => "__trunctfdf2" ,
138+ ( types:: F128 , types:: F32 ) => "__trunctfsf2" ,
139+ ( types:: F128 , types:: F16 ) => "__trunctfhf2" ,
140+ ( types:: F64 , types:: F16 ) => return f64_to_f16 ( fx, from) ,
141+ ( types:: F32 , types:: F16 ) => return f32_to_f16 ( fx, from) ,
142+ _ => unreachable ! ( "{from_ty:?} -> {to_ty:?}" ) ,
143+ } ;
144+ fx. lib_call ( name, vec ! [ AbiParam :: new( from_ty) ] , vec ! [ AbiParam :: new( to_ty) ] , & [ from] ) [ 0 ]
145+ } else if from_ty. is_int ( ) && to_ty == types:: F16 {
146+ let res = clif_int_or_float_cast ( fx, from, from_signed, types:: F32 , false ) ;
147+ f32_to_f16 ( fx, res)
148+ } else if from_ty == types:: F16 && to_ty. is_int ( ) {
149+ let from = f16_to_f32 ( fx, from) ;
150+ clif_int_or_float_cast ( fx, from, false , to_ty, to_signed)
151+ } else if from_ty. is_int ( ) && to_ty == types:: F128 {
152+ let ( from, from_ty) = if from_ty. bits ( ) < 32 {
153+ ( clif_int_or_float_cast ( fx, from, from_signed, types:: I32 , from_signed) , types:: I32 )
154+ } else {
155+ ( from, from_ty)
156+ } ;
157+ let name = format ! (
158+ "__float{sign}{size}itf" ,
159+ sign = if from_signed { "" } else { "un" } ,
160+ size = match from_ty {
161+ types:: I32 => 's' ,
162+ types:: I64 => 'd' ,
163+ types:: I128 => 't' ,
164+ _ => unreachable!( "{from_ty:?}" ) ,
165+ } ,
166+ ) ;
167+ fx. lib_call (
168+ & name,
169+ vec ! [ lib_call_arg_param( fx. tcx, from_ty, from_signed) ] ,
170+ vec ! [ AbiParam :: new( to_ty) ] ,
171+ & [ from] ,
172+ ) [ 0 ]
173+ } else if from_ty == types:: F128 && to_ty. is_int ( ) {
174+ let ret_ty = if to_ty. bits ( ) < 32 { types:: I32 } else { to_ty } ;
175+ let name = format ! (
176+ "__fix{sign}tf{size}i" ,
177+ sign = if from_signed { "" } else { "un" } ,
178+ size = match ret_ty {
179+ types:: I32 => 's' ,
180+ types:: I64 => 'd' ,
181+ types:: I128 => 't' ,
182+ _ => unreachable!( "{from_ty:?}" ) ,
183+ } ,
184+ ) ;
185+ let ret =
186+ fx. lib_call ( & name, vec ! [ AbiParam :: new( from_ty) ] , vec ! [ AbiParam :: new( to_ty) ] , & [ from] )
187+ [ 0 ] ;
188+ let val = if ret_ty == to_ty {
189+ ret
190+ } else {
191+ let ( min, max) = match ( to_ty, to_signed) {
192+ ( types:: I8 , false ) => ( 0 , i64:: from ( u8:: MAX ) ) ,
193+ ( types:: I16 , false ) => ( 0 , i64:: from ( u16:: MAX ) ) ,
194+ ( types:: I8 , true ) => ( i64:: from ( i8:: MIN as u32 ) , i64:: from ( i8:: MAX as u32 ) ) ,
195+ ( types:: I16 , true ) => ( i64:: from ( i16:: MIN as u32 ) , i64:: from ( i16:: MAX as u32 ) ) ,
196+ _ => unreachable ! ( "{to_ty:?}" ) ,
197+ } ;
198+ let min_val = fx. bcx . ins ( ) . iconst ( types:: I32 , min) ;
199+ let max_val = fx. bcx . ins ( ) . iconst ( types:: I32 , max) ;
200+
201+ let val = if to_signed {
202+ let has_underflow = fx. bcx . ins ( ) . icmp_imm ( IntCC :: SignedLessThan , ret, min) ;
203+ let has_overflow = fx. bcx . ins ( ) . icmp_imm ( IntCC :: SignedGreaterThan , ret, max) ;
204+ let bottom_capped = fx. bcx . ins ( ) . select ( has_underflow, min_val, ret) ;
205+ fx. bcx . ins ( ) . select ( has_overflow, max_val, bottom_capped)
206+ } else {
207+ let has_overflow = fx. bcx . ins ( ) . icmp_imm ( IntCC :: UnsignedGreaterThan , ret, max) ;
208+ fx. bcx . ins ( ) . select ( has_overflow, max_val, ret)
209+ } ;
210+ fx. bcx . ins ( ) . ireduce ( to_ty, val)
211+ } ;
212+
213+ if let Some ( false ) = fx. tcx . sess . opts . unstable_opts . saturating_float_casts {
214+ return val;
215+ }
216+
217+ let is_not_nan = fcmp ( fx, FloatCC :: Equal , from, from) ;
218+ let zero = type_zero_value ( & mut fx. bcx , to_ty) ;
219+ fx. bcx . ins ( ) . select ( is_not_nan, val, zero)
220+ } else {
221+ unreachable ! ( "{from_ty:?} -> {to_ty:?}" ) ;
222+ }
223+ }
224+
102225pub ( crate ) fn fmin_f128 ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > , a : Value , b : Value ) -> Value {
103226 fx. lib_call (
104227 "fminimumf128" ,
0 commit comments