@@ -3,7 +3,7 @@ use std::fmt;
3
3
use either:: { Either , Left , Right } ;
4
4
5
5
use rustc_apfloat:: {
6
- ieee:: { Double , Single } ,
6
+ ieee:: { Double , Half , Quad , Single } ,
7
7
Float ,
8
8
} ;
9
9
use rustc_macros:: HashStable ;
@@ -159,6 +159,13 @@ impl<Prov: Provenance> fmt::LowerHex for Scalar<Prov> {
159
159
}
160
160
}
161
161
162
+ impl < Prov > From < Half > for Scalar < Prov > {
163
+ #[ inline( always) ]
164
+ fn from ( f : Half ) -> Self {
165
+ Scalar :: from_f16 ( f)
166
+ }
167
+ }
168
+
162
169
impl < Prov > From < Single > for Scalar < Prov > {
163
170
#[ inline( always) ]
164
171
fn from ( f : Single ) -> Self {
@@ -173,6 +180,13 @@ impl<Prov> From<Double> for Scalar<Prov> {
173
180
}
174
181
}
175
182
183
+ impl < Prov > From < Quad > for Scalar < Prov > {
184
+ #[ inline( always) ]
185
+ fn from ( f : Quad ) -> Self {
186
+ Scalar :: from_f128 ( f)
187
+ }
188
+ }
189
+
176
190
impl < Prov > From < ScalarInt > for Scalar < Prov > {
177
191
#[ inline( always) ]
178
192
fn from ( ptr : ScalarInt ) -> Self {
@@ -281,6 +295,11 @@ impl<Prov> Scalar<Prov> {
281
295
Self :: from_int ( i, cx. data_layout ( ) . pointer_size )
282
296
}
283
297
298
+ #[ inline]
299
+ pub fn from_f16 ( f : Half ) -> Self {
300
+ Scalar :: Int ( f. into ( ) )
301
+ }
302
+
284
303
#[ inline]
285
304
pub fn from_f32 ( f : Single ) -> Self {
286
305
Scalar :: Int ( f. into ( ) )
@@ -291,6 +310,11 @@ impl<Prov> Scalar<Prov> {
291
310
Scalar :: Int ( f. into ( ) )
292
311
}
293
312
313
+ #[ inline]
314
+ pub fn from_f128 ( f : Quad ) -> Self {
315
+ Scalar :: Int ( f. into ( ) )
316
+ }
317
+
294
318
/// This is almost certainly not the method you want! You should dispatch on the type
295
319
/// and use `to_{u8,u16,...}`/`scalar_to_ptr` to perform ptr-to-int / int-to-ptr casts as needed.
296
320
///
@@ -493,6 +517,12 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
493
517
Ok ( i64:: try_from ( b) . unwrap ( ) )
494
518
}
495
519
520
+ #[ inline]
521
+ pub fn to_f16 ( self ) -> InterpResult < ' tcx , Half > {
522
+ // Going through `u16` to check size and truncation.
523
+ Ok ( Half :: from_bits ( self . to_u16 ( ) ?. into ( ) ) )
524
+ }
525
+
496
526
#[ inline]
497
527
pub fn to_f32 ( self ) -> InterpResult < ' tcx , Single > {
498
528
// Going through `u32` to check size and truncation.
@@ -504,6 +534,12 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
504
534
// Going through `u64` to check size and truncation.
505
535
Ok ( Double :: from_bits ( self . to_u64 ( ) ?. into ( ) ) )
506
536
}
537
+
538
+ #[ inline]
539
+ pub fn to_f128 ( self ) -> InterpResult < ' tcx , Quad > {
540
+ // Going through `u128` to check size and truncation.
541
+ Ok ( Quad :: from_bits ( self . to_u128 ( ) ?. into ( ) ) )
542
+ }
507
543
}
508
544
509
545
/// Gets the bytes of a constant slice value.
0 commit comments