Skip to content

Commit dfaca93

Browse files
committed
Auto merge of rust-lang#14950 - HKalbasi:render-const, r=HKalbasi
Support floating point intrinsics in const eval
2 parents 7738ff4 + a6a27a7 commit dfaca93

File tree

4 files changed

+294
-107
lines changed

4 files changed

+294
-107
lines changed

crates/hir-ty/src/consteval/tests/intrinsics.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,40 @@ fn likely() {
174174
);
175175
}
176176

177+
#[test]
178+
fn floating_point() {
179+
check_number(
180+
r#"
181+
extern "rust-intrinsic" {
182+
pub fn sqrtf32(x: f32) -> f32;
183+
pub fn powf32(a: f32, x: f32) -> f32;
184+
pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
185+
}
186+
187+
const GOAL: f32 = sqrtf32(1.2) + powf32(3.4, 5.6) + fmaf32(-7.8, 1.3, 2.4);
188+
"#,
189+
i128::from_le_bytes(pad16(
190+
&f32::to_le_bytes(1.2f32.sqrt() + 3.4f32.powf(5.6) + (-7.8f32).mul_add(1.3, 2.4)),
191+
true,
192+
)),
193+
);
194+
check_number(
195+
r#"
196+
extern "rust-intrinsic" {
197+
pub fn powif64(a: f64, x: i32) -> f64;
198+
pub fn sinf64(x: f64) -> f64;
199+
pub fn minnumf64(x: f64, y: f64) -> f64;
200+
}
201+
202+
const GOAL: f64 = powif64(1.2, 5) + sinf64(3.4) + minnumf64(-7.8, 1.3);
203+
"#,
204+
i128::from_le_bytes(pad16(
205+
&f64::to_le_bytes(1.2f64.powi(5) + 3.4f64.sin() + (-7.8f64).min(1.3)),
206+
true,
207+
)),
208+
);
209+
}
210+
177211
#[test]
178212
fn atomic() {
179213
check_number(

crates/hir-ty/src/mir/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ impl Evaluator<'_> {
870870
Owned(c.to_le_bytes().into())
871871
}
872872
chalk_ir::FloatTy::F64 => {
873-
let c = -from_bytes!(f32, c);
873+
let c = -from_bytes!(f64, c);
874874
Owned(c.to_le_bytes().into())
875875
}
876876
}

0 commit comments

Comments
 (0)