Add f16 and f128 support

This commit is contained in:
beetrees 2024-07-10 09:38:47 +01:00
parent da27b89ca5
commit d5db933f9d
No known key found for this signature in database
GPG key ID: 8791BD754191EBD6
28 changed files with 384 additions and 73 deletions

View file

@ -30,8 +30,10 @@ pub enum BuiltinUint {
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum BuiltinFloat {
F16,
F32,
F64,
F128,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -65,8 +67,10 @@ impl BuiltinType {
(name![u64], BuiltinType::Uint(BuiltinUint::U64)),
(name![u128], BuiltinType::Uint(BuiltinUint::U128)),
(name![f16], BuiltinType::Float(BuiltinFloat::F16)),
(name![f32], BuiltinType::Float(BuiltinFloat::F32)),
(name![f64], BuiltinType::Float(BuiltinFloat::F64)),
(name![f128], BuiltinType::Float(BuiltinFloat::F128)),
];
pub fn by_name(name: &Name) -> Option<Self> {
@ -97,8 +101,10 @@ impl AsName for BuiltinType {
BuiltinUint::U128 => name![u128],
},
BuiltinType::Float(it) => match it {
BuiltinFloat::F16 => name![f16],
BuiltinFloat::F32 => name![f32],
BuiltinFloat::F64 => name![f64],
BuiltinFloat::F128 => name![f128],
},
}
}
@ -155,8 +161,10 @@ impl BuiltinUint {
impl BuiltinFloat {
pub fn from_suffix(suffix: &str) -> Option<BuiltinFloat> {
let res = match suffix {
"f16" => BuiltinFloat::F16,
"f32" => BuiltinFloat::F32,
"f64" => BuiltinFloat::F64,
"f128" => BuiltinFloat::F128,
_ => return None,
};
Some(res)
@ -192,8 +200,10 @@ impl fmt::Display for BuiltinUint {
impl fmt::Display for BuiltinFloat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
BuiltinFloat::F16 => "f16",
BuiltinFloat::F32 => "f32",
BuiltinFloat::F64 => "f64",
BuiltinFloat::F128 => "f128",
})
}
}

View file

@ -20,6 +20,7 @@ use std::fmt;
use hir_expand::name::Name;
use intern::Interned;
use la_arena::{Idx, RawIdx};
use rustc_apfloat::ieee::{Half as f16, Quad as f128};
use smallvec::SmallVec;
use syntax::ast;
@ -62,11 +63,16 @@ pub type LabelId = Idx<Label>;
#[derive(Default, Debug, Clone, Eq, PartialEq)]
pub struct FloatTypeWrapper(Box<str>);
// FIXME(#17451): Use builtin types once stabilised.
impl FloatTypeWrapper {
pub fn new(value: String) -> Self {
Self(value.into())
}
pub fn to_f128(&self) -> f128 {
self.0.parse().unwrap_or_default()
}
pub fn to_f64(&self) -> f64 {
self.0.parse().unwrap_or_default()
}
@ -74,6 +80,10 @@ impl FloatTypeWrapper {
pub fn to_f32(&self) -> f32 {
self.0.parse().unwrap_or_default()
}
pub fn to_f16(&self) -> f16 {
self.0.parse().unwrap_or_default()
}
}
impl fmt::Display for FloatTypeWrapper {
@ -91,7 +101,7 @@ pub enum Literal {
Bool(bool),
Int(i128, Option<BuiltinInt>),
Uint(u128, Option<BuiltinUint>),
// Here we are using a wrapper around float because f32 and f64 do not implement Eq, so they
// Here we are using a wrapper around float because float primitives do not implement Eq, so they
// could not be used directly here, to understand how the wrapper works go to definition of
// FloatTypeWrapper
Float(FloatTypeWrapper, Option<BuiltinFloat>),

View file

@ -36,6 +36,7 @@ macro_rules! m {
let _ = 'c';
let _ = 1000;
let _ = 12E+99_f64;
let _ = 45E+1234_f128;
let _ = "rust1";
let _ = -92;
}
@ -50,6 +51,7 @@ macro_rules! m {
let _ = 'c';
let _ = 1000;
let _ = 12E+99_f64;
let _ = 45E+1234_f128;
let _ = "rust1";
let _ = -92;
}
@ -58,6 +60,7 @@ fn f() {
let _ = 'c';
let _ = 1000;
let _ = 12E+99_f64;
let _ = 45E+1234_f128;
let _ = "rust1";
let _ = -92;
}