From 4da8682523ed8527046790f288ee55dd394be52a Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Fri, 22 Apr 2022 18:39:25 -0700 Subject: [PATCH] Remove unnecessary const-time x87-related checks --- library/core/src/num/f32.rs | 24 ++++++++---------------- library/core/src/num/f64.rs | 24 ++++++++---------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/library/core/src/num/f32.rs b/library/core/src/num/f32.rs index 5359f02851b..e1a46086af0 100644 --- a/library/core/src/num/f32.rs +++ b/library/core/src/num/f32.rs @@ -928,19 +928,9 @@ const fn ct_f32_to_u32(ct: f32) -> u32 { FpCategory::Subnormal => { panic!("const-eval error: cannot use f32::to_bits on a subnormal number") } - FpCategory::Infinite => - // SAFETY: Infinity per se is fine - unsafe { mem::transmute::(ct) }, - FpCategory::Zero | FpCategory::Normal => { + FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => { // SAFETY: We have a normal floating point number. Now we transmute, i.e. do a bitcopy. - let bits: u32 = unsafe { mem::transmute::(ct) }; - // Let's doublecheck to make sure it wasn't a weird float by truncating it. - if bits >> 23 & 0xFF == 0xFF { - panic!( - "const-eval error: an unusually large x87 floating point value should not leak into const eval" - ) - }; - bits + unsafe { mem::transmute::(ct) } } } } @@ -1021,13 +1011,15 @@ pub const fn from_bits(v: u32) -> Self { const fn ct_u32_to_f32(ct: u32) -> f32 { match f32::classify_bits(ct) { FpCategory::Subnormal => { - panic!("const-eval error: cannot use f32::from_bits on a subnormal number"); + panic!("const-eval error: cannot use f32::from_bits on a subnormal number") } FpCategory::Nan => { - panic!("const-eval error: cannot use f32::from_bits on NaN"); + panic!("const-eval error: cannot use f32::from_bits on NaN") + } + FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => { + // SAFETY: It's not a frumious number + unsafe { mem::transmute::(ct) } } - // SAFETY: It's not a frumious number - _ => unsafe { mem::transmute::(ct) }, } } // SAFETY: `u32` is a plain old datatype so we can always... uh... diff --git a/library/core/src/num/f64.rs b/library/core/src/num/f64.rs index 61b040cf0fe..b07f201ca4a 100644 --- a/library/core/src/num/f64.rs +++ b/library/core/src/num/f64.rs @@ -921,19 +921,9 @@ const fn ct_f64_to_u64(ct: f64) -> u64 { FpCategory::Subnormal => { panic!("const-eval error: cannot use f64::to_bits on a subnormal number") } - FpCategory::Infinite => - // SAFETY: Infinity per se is fine - unsafe { mem::transmute::(ct) }, - FpCategory::Zero | FpCategory::Normal => { + FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => { // SAFETY: We have a normal floating point number. Now we transmute, i.e. do a bitcopy. - let bits: u64 = unsafe { mem::transmute::(ct) }; - // Let's doublecheck to make sure it wasn't a weird float by truncating it. - if (bits >> 52) & 0x7FF == 0x7FF { - panic!( - "const-eval error: an unusually large x87 floating point value should not leak into const eval" - ) - }; - bits + unsafe { mem::transmute::(ct) } } } } @@ -1019,13 +1009,15 @@ pub const fn from_bits(v: u64) -> Self { const fn ct_u64_to_f64(ct: u64) -> f64 { match f64::classify_bits(ct) { FpCategory::Subnormal => { - panic!("const-eval error: cannot use f64::from_bits on a subnormal number"); + panic!("const-eval error: cannot use f64::from_bits on a subnormal number") } FpCategory::Nan => { - panic!("const-eval error: cannot use f64::from_bits on NaN"); + panic!("const-eval error: cannot use f64::from_bits on NaN") + } + FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => { + // SAFETY: It's not a frumious number + unsafe { mem::transmute::(ct) } } - // SAFETY: It's not a frumious number - _ => unsafe { mem::transmute::(ct) }, } } // SAFETY: `u64` is a plain old datatype so we can always... uh...