Skip to content

Commit 4da8682

Browse files
Remove unnecessary const-time x87-related checks
1 parent bb555b8 commit 4da8682

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

library/core/src/num/f32.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -928,19 +928,9 @@ impl f32 {
928928
FpCategory::Subnormal => {
929929
panic!("const-eval error: cannot use f32::to_bits on a subnormal number")
930930
}
931-
FpCategory::Infinite =>
932-
// SAFETY: Infinity per se is fine
933-
unsafe { mem::transmute::<f32, u32>(ct) },
934-
FpCategory::Zero | FpCategory::Normal => {
931+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => {
935932
// SAFETY: We have a normal floating point number. Now we transmute, i.e. do a bitcopy.
936-
let bits: u32 = unsafe { mem::transmute::<f32, u32>(ct) };
937-
// Let's doublecheck to make sure it wasn't a weird float by truncating it.
938-
if bits >> 23 & 0xFF == 0xFF {
939-
panic!(
940-
"const-eval error: an unusually large x87 floating point value should not leak into const eval"
941-
)
942-
};
943-
bits
933+
unsafe { mem::transmute::<f32, u32>(ct) }
944934
}
945935
}
946936
}
@@ -1021,13 +1011,15 @@ impl f32 {
10211011
const fn ct_u32_to_f32(ct: u32) -> f32 {
10221012
match f32::classify_bits(ct) {
10231013
FpCategory::Subnormal => {
1024-
panic!("const-eval error: cannot use f32::from_bits on a subnormal number");
1014+
panic!("const-eval error: cannot use f32::from_bits on a subnormal number")
10251015
}
10261016
FpCategory::Nan => {
1027-
panic!("const-eval error: cannot use f32::from_bits on NaN");
1017+
panic!("const-eval error: cannot use f32::from_bits on NaN")
1018+
}
1019+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => {
1020+
// SAFETY: It's not a frumious number
1021+
unsafe { mem::transmute::<u32, f32>(ct) }
10281022
}
1029-
// SAFETY: It's not a frumious number
1030-
_ => unsafe { mem::transmute::<u32, f32>(ct) },
10311023
}
10321024
}
10331025
// SAFETY: `u32` is a plain old datatype so we can always... uh...

library/core/src/num/f64.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -921,19 +921,9 @@ impl f64 {
921921
FpCategory::Subnormal => {
922922
panic!("const-eval error: cannot use f64::to_bits on a subnormal number")
923923
}
924-
FpCategory::Infinite =>
925-
// SAFETY: Infinity per se is fine
926-
unsafe { mem::transmute::<f64, u64>(ct) },
927-
FpCategory::Zero | FpCategory::Normal => {
924+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => {
928925
// SAFETY: We have a normal floating point number. Now we transmute, i.e. do a bitcopy.
929-
let bits: u64 = unsafe { mem::transmute::<f64, u64>(ct) };
930-
// Let's doublecheck to make sure it wasn't a weird float by truncating it.
931-
if (bits >> 52) & 0x7FF == 0x7FF {
932-
panic!(
933-
"const-eval error: an unusually large x87 floating point value should not leak into const eval"
934-
)
935-
};
936-
bits
926+
unsafe { mem::transmute::<f64, u64>(ct) }
937927
}
938928
}
939929
}
@@ -1019,13 +1009,15 @@ impl f64 {
10191009
const fn ct_u64_to_f64(ct: u64) -> f64 {
10201010
match f64::classify_bits(ct) {
10211011
FpCategory::Subnormal => {
1022-
panic!("const-eval error: cannot use f64::from_bits on a subnormal number");
1012+
panic!("const-eval error: cannot use f64::from_bits on a subnormal number")
10231013
}
10241014
FpCategory::Nan => {
1025-
panic!("const-eval error: cannot use f64::from_bits on NaN");
1015+
panic!("const-eval error: cannot use f64::from_bits on NaN")
1016+
}
1017+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => {
1018+
// SAFETY: It's not a frumious number
1019+
unsafe { mem::transmute::<u64, f64>(ct) }
10261020
}
1027-
// SAFETY: It's not a frumious number
1028-
_ => unsafe { mem::transmute::<u64, f64>(ct) },
10291021
}
10301022
}
10311023
// SAFETY: `u64` is a plain old datatype so we can always... uh...

0 commit comments

Comments
 (0)