From 6471eb050e5c2129953b09e58179d6c79b8d7455 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 1 Nov 2023 01:16:12 +0000 Subject: [PATCH] Depend on `libm` in `no-std` for `powf`(64) In the next commits we'll need `f64`'s `powf`, which is only available in `std`. For `no-std`, here we depend on `libm` (a `rust-lang` org project), which we can use for `powf`. --- lightning/Cargo.toml | 3 ++- lightning/src/lib.rs | 1 + lightning/src/routing/scoring.rs | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lightning/Cargo.toml b/lightning/Cargo.toml index 3c3912e2b..bb4a3ae1f 100644 --- a/lightning/Cargo.toml +++ b/lightning/Cargo.toml @@ -31,7 +31,7 @@ unsafe_revoked_tx_signing = [] # Override signing to not include randomness when generating signatures for test vectors. _test_vectors = [] -no-std = ["hashbrown", "bitcoin/no-std", "core2/alloc"] +no-std = ["hashbrown", "bitcoin/no-std", "core2/alloc", "libm"] std = ["bitcoin/std"] # Generates low-r bitcoin signatures, which saves 1 byte in 50% of the cases @@ -48,6 +48,7 @@ regex = { version = "1.5.6", optional = true } backtrace = { version = "0.3", optional = true } core2 = { version = "0.3.0", optional = true, default-features = false } +libm = { version = "0.2", optional = true, default-features = false } [dev-dependencies] regex = "1.5.6" diff --git a/lightning/src/lib.rs b/lightning/src/lib.rs index c42f66da5..6eefb3983 100644 --- a/lightning/src/lib.rs +++ b/lightning/src/lib.rs @@ -69,6 +69,7 @@ extern crate hex; #[cfg(any(test, feature = "_test_utils"))] extern crate regex; #[cfg(not(feature = "std"))] extern crate core2; +#[cfg(not(feature = "std"))] extern crate libm; #[cfg(ldk_bench)] extern crate criterion; diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 337fd8eec..3124b21b2 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -1469,6 +1469,16 @@ impl>, L: Deref, T: Time> ScoreUpdate for Prob impl>, L: Deref, T: Time> Score for ProbabilisticScorerUsingTime where L::Target: Logger {} +#[cfg(feature = "std")] +#[inline] +fn powf64(n: f64, exp: f64) -> f64 { + n.powf(exp) +} +#[cfg(not(feature = "std"))] +fn powf64(n: f64, exp: f64) -> f64 { + libm::powf(n as f32, exp as f32) as f64 +} + mod approx { const BITS: u32 = 64; const HIGHEST_BIT: u32 = BITS - 1; -- 2.39.5