3
3
use crate :: source:: { get_source_text, walk_span_to_context} ;
4
4
use crate :: { clip, is_direct_expn_of, sext, unsext} ;
5
5
6
+ use rustc_apfloat:: ieee:: { Half , Quad } ;
7
+ use rustc_apfloat:: Float ;
6
8
use rustc_ast:: ast:: { self , LitFloatType , LitKind } ;
7
9
use rustc_data_structures:: sync:: Lrc ;
8
10
use rustc_hir:: def:: { DefKind , Res } ;
@@ -33,10 +35,14 @@ pub enum Constant<'tcx> {
33
35
Char ( char ) ,
34
36
/// An integer's bit representation.
35
37
Int ( u128 ) ,
38
+ /// An `f16`.
39
+ F16 ( f16 ) ,
36
40
/// An `f32`.
37
41
F32 ( f32 ) ,
38
42
/// An `f64`.
39
43
F64 ( f64 ) ,
44
+ /// An `f128`.
45
+ F128 ( f128 ) ,
40
46
/// `true` or `false`.
41
47
Bool ( bool ) ,
42
48
/// An array of constants.
@@ -161,12 +167,18 @@ impl<'tcx> Hash for Constant<'tcx> {
161
167
Self :: Int ( i) => {
162
168
i. hash ( state) ;
163
169
} ,
170
+ Self :: F16 ( f) => {
171
+ f64:: from ( f) . to_bits ( ) . hash ( state) ;
172
+ } ,
164
173
Self :: F32 ( f) => {
165
174
f64:: from ( f) . to_bits ( ) . hash ( state) ;
166
175
} ,
167
176
Self :: F64 ( f) => {
168
177
f. to_bits ( ) . hash ( state) ;
169
178
} ,
179
+ Self :: F128 ( f) => {
180
+ f. to_bits ( ) . hash ( state) ;
181
+ } ,
170
182
Self :: Bool ( b) => {
171
183
b. hash ( state) ;
172
184
} ,
@@ -268,6 +280,16 @@ impl<'tcx> Constant<'tcx> {
268
280
}
269
281
self
270
282
}
283
+
284
+ fn parse_f16 ( s : & str ) -> Self {
285
+ let f: Half = s. parse ( ) . unwrap ( ) ;
286
+ Self :: F16 ( f16:: from_bits ( f. to_bits ( ) . try_into ( ) . unwrap ( ) ) )
287
+ }
288
+
289
+ fn parse_f128 ( s : & str ) -> Self {
290
+ let f: Quad = s. parse ( ) . unwrap ( ) ;
291
+ Self :: F128 ( f128:: from_bits ( f. to_bits ( ) ) )
292
+ }
271
293
}
272
294
273
295
/// Parses a `LitKind` to a `Constant`.
@@ -279,16 +301,17 @@ pub fn lit_to_mir_constant<'tcx>(lit: &LitKind, ty: Option<Ty<'tcx>>) -> Constan
279
301
LitKind :: Char ( c) => Constant :: Char ( c) ,
280
302
LitKind :: Int ( n, _) => Constant :: Int ( n. get ( ) ) ,
281
303
LitKind :: Float ( ref is, LitFloatType :: Suffixed ( fty) ) => match fty {
282
- ast:: FloatTy :: F16 => unimplemented ! ( "f16_f128" ) ,
304
+ // FIXME(f16_f128): just use `parse()` directly when available for `f16`/`f128`
305
+ ast:: FloatTy :: F16 => Constant :: parse_f16 ( is. as_str ( ) ) ,
283
306
ast:: FloatTy :: F32 => Constant :: F32 ( is. as_str ( ) . parse ( ) . unwrap ( ) ) ,
284
307
ast:: FloatTy :: F64 => Constant :: F64 ( is. as_str ( ) . parse ( ) . unwrap ( ) ) ,
285
- ast:: FloatTy :: F128 => unimplemented ! ( "f16_f128" ) ,
308
+ ast:: FloatTy :: F128 => Constant :: parse_f128 ( is . as_str ( ) ) ,
286
309
} ,
287
310
LitKind :: Float ( ref is, LitFloatType :: Unsuffixed ) => match ty. expect ( "type of float is known" ) . kind ( ) {
288
- ty:: Float ( FloatTy :: F16 ) => unimplemented ! ( "f16_f128" ) ,
311
+ ty:: Float ( FloatTy :: F16 ) => Constant :: parse_f16 ( is . as_str ( ) ) ,
289
312
ty:: Float ( FloatTy :: F32 ) => Constant :: F32 ( is. as_str ( ) . parse ( ) . unwrap ( ) ) ,
290
313
ty:: Float ( FloatTy :: F64 ) => Constant :: F64 ( is. as_str ( ) . parse ( ) . unwrap ( ) ) ,
291
- ty:: Float ( FloatTy :: F128 ) => unimplemented ! ( "f16_f128" ) ,
314
+ ty:: Float ( FloatTy :: F128 ) => Constant :: parse_f128 ( is . as_str ( ) ) ,
292
315
_ => bug ! ( ) ,
293
316
} ,
294
317
LitKind :: Bool ( b) => Constant :: Bool ( b) ,
@@ -835,10 +858,10 @@ pub fn mir_to_const<'tcx>(lcx: &LateContext<'tcx>, result: mir::Const<'tcx>) ->
835
858
let range = alloc_range ( offset + size * idx, size) ;
836
859
let val = alloc. read_scalar ( & lcx. tcx , range, /* read_provenance */ false ) . ok ( ) ?;
837
860
res. push ( match flt {
838
- FloatTy :: F16 => unimplemented ! ( "f16_f128" ) ,
861
+ FloatTy :: F16 => Constant :: F16 ( f16 :: from_bits ( val . to_u16 ( ) . ok ( ) ? ) ) ,
839
862
FloatTy :: F32 => Constant :: F32 ( f32:: from_bits ( val. to_u32 ( ) . ok ( ) ?) ) ,
840
863
FloatTy :: F64 => Constant :: F64 ( f64:: from_bits ( val. to_u64 ( ) . ok ( ) ?) ) ,
841
- FloatTy :: F128 => unimplemented ! ( "f16_f128" ) ,
864
+ FloatTy :: F128 => Constant :: F128 ( f128 :: from_bits ( val . to_u128 ( ) . ok ( ) ? ) ) ,
842
865
} ) ;
843
866
}
844
867
Some ( Constant :: Vec ( res) )
0 commit comments