Skip to content

Commit 97db7e2

Browse files
committed
fix floating issues with libm
1 parent 273766f commit 97db7e2

File tree

6 files changed

+17
-7
lines changed

6 files changed

+17
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cranelift/codegen/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ serde = { workspace = true, optional = true }
3737
serde_derive = { workspace = true, optional = true }
3838
pulley-interpreter = { workspace = true, optional = true }
3939
postcard = { workspace = true, optional = true }
40-
gimli = { workspace = true, features = ["write", "std"], optional = true }
40+
gimli = { workspace = true, features = ["write"], optional = true }
4141
smallvec = { workspace = true }
4242
regalloc2 = { workspace = true, features = ["checker"] }
4343
souper-ir = { version = "2.1.0", optional = true }
4444
sha2 = { version = "0.10.2", optional = true }
4545
rustc-hash = { workspace = true }
4646
wasmtime-math = { workspace = true }
47+
libm = { workspace = true, features = ["unstable-public-internals"] }
4748
# It is a goal of the cranelift-codegen crate to have minimal external dependencies.
4849
# Please don't add any unless they are essential to the task of creating binary
4950
# machine code. Integration tests that need external dependencies can be
@@ -65,7 +66,7 @@ default = ["std", "unwind", "host-arch", "timing"]
6566
# The "std" feature enables use of libstd. The "core" feature enables use
6667
# of some minimal std-like replacement libraries. At least one of these two
6768
# features need to be enabled.
68-
std = ["serde?/std", "rustc-hash/std", "cranelift-control/fuzz"]
69+
std = ["serde?/std", "rustc-hash/std", "gimli/std", "cranelift-control/fuzz"]
6970

7071
# The "core" feature used to enable a hashmap workaround, but is now
7172
# deprecated (we (i) always use hashbrown, and (ii) don't support a

cranelift/codegen/src/ir/immediates.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use core::fmt::{self, Display, Formatter};
1111
use core::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Sub};
1212
use core::str::FromStr;
1313
use core::{i32, u32};
14+
use libm::Libm;
1415
#[cfg(feature = "enable-serde")]
1516
use serde_derive::{Deserialize, Serialize};
1617

@@ -683,28 +684,28 @@ macro_rules! ieee_float {
683684
$(
684685
/// Returns the square root of `self`.
685686
pub fn sqrt(self) -> Self {
686-
Self::with_float(self.$as_float().sqrt())
687+
Self::with_float(Libm::<$float_ty>::sqrt(self.$as_float()))
687688
}
688689

689690
/// Returns the smallest integer greater than or equal to `self`.
690691
pub fn ceil(self) -> Self {
691-
Self::with_float(self.$as_float().ceil())
692+
Self::with_float(Libm::<$float_ty>::ceil(self.$as_float()))
692693
}
693694

694695
/// Returns the largest integer less than or equal to `self`.
695696
pub fn floor(self) -> Self {
696-
Self::with_float(self.$as_float().floor())
697+
Self::with_float(Libm::<$float_ty>::floor(self.$as_float()))
697698
}
698699

699700
/// Returns the integer part of `self`. This means that non-integer numbers are always truncated towards zero.
700701
pub fn trunc(self) -> Self {
701-
Self::with_float(self.$as_float().trunc())
702+
Self::with_float(Libm::<$float_ty>::trunc(self.$as_float()))
702703
}
703704

704705
/// Returns the nearest integer to `self`. Rounds half-way cases to the number
705706
/// with an even least significant digit.
706707
pub fn round_ties_even(self) -> Self {
707-
Self::with_float(self.$as_float().round_ties_even())
708+
Self::with_float(libm::generic::rint_round(self.$as_float(), libm::support::Round::Nearest).val)
708709
}
709710
)?
710711
}

cranelift/codegen/src/isa/unwind.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod winx64;
1414
#[cfg(feature = "unwind")]
1515
pub mod winarm64;
1616

17+
#[cfg(feature = "unwind")]
1718
/// CFA-based unwind information used on SystemV.
1819
pub type CfaUnwindInfo = systemv::UnwindInfo;
1920

cranelift/codegen/src/isa/x64/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod lower;
2929
mod pcc;
3030
pub mod settings;
3131

32+
#[cfg(feature = "unwind")]
3233
pub use inst::unwind::systemv::create_cie;
3334

3435
/// An X64 backend.
@@ -218,6 +219,7 @@ pub fn emit_unwind_info(
218219
) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {
219220
use crate::isa::unwind::{UnwindInfo, UnwindInfoKind};
220221
Ok(match kind {
222+
#[cfg(feature = "unwind")]
221223
UnwindInfoKind::SystemV => {
222224
let mapper = self::inst::unwind::systemv::RegisterMapper;
223225
Some(UnwindInfo::SystemV(
@@ -228,6 +230,7 @@ pub fn emit_unwind_info(
228230
)?,
229231
))
230232
}
233+
#[cfg(feature = "unwind")]
231234
UnwindInfoKind::Windows => Some(UnwindInfo::WindowsX64(
232235
crate::isa::unwind::winx64::create_unwind_info_from_insts::<
233236
self::inst::unwind::winx64::RegisterMapper,

cranelift/codegen/src/machinst/vcode.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,9 @@ impl<I: VCodeInst> VCode<I> {
12781278
let slot = alloc.as_stack().unwrap();
12791279
let slot_offset = self.abi.get_spillslot_offset(slot);
12801280
let slot_base_to_caller_sp_offset = self.abi.slot_base_to_caller_sp_offset();
1281+
#[cfg(not(feature = "unwind"))]
1282+
let caller_sp_to_cfa_offset = 0;
1283+
#[cfg(feature = "unwind")]
12811284
let caller_sp_to_cfa_offset =
12821285
crate::isa::unwind::systemv::caller_sp_to_cfa_offset();
12831286
// NOTE: this is a negative offset because it's relative to the caller's SP

0 commit comments

Comments
 (0)