Depend on `libm` in `no-std` for `powf`(64)
authorMatt Corallo <git@bluematt.me>
Wed, 1 Nov 2023 01:16:12 +0000 (01:16 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 13 Dec 2023 18:36:40 +0000 (18:36 +0000)
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
lightning/src/lib.rs
lightning/src/routing/scoring.rs

index 3c3912e2b32fc29a130b9d78d504a6826b17cb54..bb4a3ae1ffd7d6506d7ee529ccf355c97c98daa3 100644 (file)
@@ -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"
index c42f66da5764ec95c656a9b72e6fe10c6c66d771..6eefb3983cca95f1bb0351a43bfddf39d2be58db 100644 (file)
@@ -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;
 
index 337fd8eec7a895ce5198edb3a9774267048de03d..3124b21b2b4fc5f425f87a3cc0f9cf009ce226c8 100644 (file)
@@ -1469,6 +1469,16 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Score for ProbabilisticScorerUsingTime<G, L, T>
 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;