diff options
| author | beetrees <b@beetr.ee> | 2024-05-06 13:27:40 +0100 |
|---|---|---|
| committer | beetrees <b@beetr.ee> | 2024-05-06 14:56:10 +0100 |
| commit | 3769fddba23985e9ab83828ccce672507e7dd891 (patch) | |
| tree | ec5cdc7c2024da713350fbccfa04ccb4cfd3b891 /compiler/rustc_abi/src/lib.rs | |
| parent | 8cef37dbb67e9c80702925f19cf298c4203991e4 (diff) | |
| download | rust-3769fddba23985e9ab83828ccce672507e7dd891.tar.gz rust-3769fddba23985e9ab83828ccce672507e7dd891.zip | |
Refactor float `Primitive`s to a separate `Float` type
Diffstat (limited to 'compiler/rustc_abi/src/lib.rs')
| -rw-r--r-- | compiler/rustc_abi/src/lib.rs | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs index 5e3f64540e4..bc2ad2394c8 100644 --- a/compiler/rustc_abi/src/lib.rs +++ b/compiler/rustc_abi/src/lib.rs @@ -926,6 +926,41 @@ impl Integer { } } +/// Floating-point types. +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[cfg_attr(feature = "nightly", derive(HashStable_Generic))] +pub enum Float { + F16, + F32, + F64, + F128, +} + +impl Float { + pub fn size(self) -> Size { + use Float::*; + + match self { + F16 => Size::from_bits(16), + F32 => Size::from_bits(32), + F64 => Size::from_bits(64), + F128 => Size::from_bits(128), + } + } + + pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign { + use Float::*; + let dl = cx.data_layout(); + + match self { + F16 => dl.f16_align, + F32 => dl.f32_align, + F64 => dl.f64_align, + F128 => dl.f128_align, + } + } +} + /// Fundamental unit of memory access and layout. #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[cfg_attr(feature = "nightly", derive(HashStable_Generic))] @@ -938,10 +973,7 @@ pub enum Primitive { /// a negative integer passed by zero-extension will appear positive in /// the callee, and most operations on it will produce the wrong values. Int(Integer, bool), - F16, - F32, - F64, - F128, + Float(Float), Pointer(AddressSpace), } @@ -952,10 +984,7 @@ impl Primitive { match self { Int(i, _) => i.size(), - F16 => Size::from_bits(16), - F32 => Size::from_bits(32), - F64 => Size::from_bits(64), - F128 => Size::from_bits(128), + Float(f) => f.size(), // FIXME(erikdesjardins): ignoring address space is technically wrong, pointers in // different address spaces can have different sizes // (but TargetDataLayout doesn't currently parse that part of the DL string) @@ -969,10 +998,7 @@ impl Primitive { match self { Int(i, _) => i.align(dl), - F16 => dl.f16_align, - F32 => dl.f32_align, - F64 => dl.f64_align, - F128 => dl.f128_align, + Float(f) => f.align(dl), // FIXME(erikdesjardins): ignoring address space is technically wrong, pointers in // different address spaces can have different alignments // (but TargetDataLayout doesn't currently parse that part of the DL string) |
