Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation failure for no_std & alloc feature #1088

Closed
michael-p opened this issue Jan 23, 2021 · 5 comments · Fixed by #1089
Closed

Compilation failure for no_std & alloc feature #1088

michael-p opened this issue Jan 23, 2021 · 5 comments · Fixed by #1089

Comments

@michael-p
Copy link

Dear all,

It looks like the rand crate does not compile for no_std targets when the alloc feature is enabled:

$ cargo build --no-default-features --features "alloc"
   Compiling rand_core v0.6.1 (/Users/michaelp/Desktop/rand/rand_core)
   Compiling rand v0.8.2 (/Users/michaelp/Desktop/rand)
error[E0599]: no method named `powf` found for type `f64` in the current scope
   --> src/seq/index.rs:393:40
    |
393 |             let key = rng.gen::<f64>().powf(1.0 / weight);
    |                                        ^^^^ method not found in `f64`

The reason is that f64::powf() and friends are not available in a no_std context.
The two options I see are to either make the functions sample_weighted and sample_efraimidis_spirakis available only when std feature is enabled, or alternatively use the pow function from the libm crate (this is what most other no_std crates like nalgebra use for their math needs).

Thank you!

@dhardy
Copy link
Member

dhardy commented Jan 23, 2021

You're right. That should have been covered by the tests.

I'll open a PR to fix the tests. As to fixing the code, probably best to disable these features? rand_distr depends on num-traits (and thus libm), but rand always tries to minimise dependencies.

dhardy added a commit that referenced this issue Jan 26, 2021
Fix #1088: no-std + alloc build; prepare 0.8.3
@jiayihu
Copy link

jiayihu commented Mar 4, 2021

I'm having the same issue but with rand_distr, but my understanding is that since it depends on num-traits it's intended to forcely require these f64 methods to work.

Example of error:

error[E0599]: no method named `powi` found for type `f64` in the current scope
   --> /Users/jiayihu/.cargo/registry/src/github.com-1ecc6299db9ec823/rand_distr-0.4.0/src/binomial.rs:115:27
    |
115 |             let mut r = q.powi(self.n as i32);
    |                           ^^^^ method not found in `f64`
    |
    = help: items from traits can only be used if the trait is in scope
    = note: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
            candidate #1: `use num_traits::float::FloatCore;`
            candidate #2: `use num_traits::Float;`
            candidate #3: `use num_traits::real::Real;`

@vks
Copy link
Collaborator

vks commented Mar 5, 2021

@jiayihu Indeed, rand_distr only works on no_std with num_traits and libm. (I just realized that the readme still claims that no_std is not supported for rand_distr, we should fix that.)

The reason for this is that no_std only supports elementary floating point operations (addition, multiplication etc.), but no exponentials or powers and similar, which require a C standard library in rustc's implementation.

@vks
Copy link
Collaborator

vks commented Mar 5, 2021

I just realized that the readme still claims that no_std is not supported for rand_distr, we should fix that.

I opened #1100 to address this.

@jiayihu
Copy link

jiayihu commented Mar 5, 2021

but no exponentials or powers and similar, which require a C standard library in rustc's implementation

Yes that happens often with embedded in my experience. If I'm correct, you're supposed to provide these implementations. Just as an example, I'm currently having these two functions implemented myself so that the linker can find them when needed:

#[no_mangle]
pub extern "C" fn fminf(a: f32, b: f32) -> f32 {
    if a < b {
        a
    } else {
        b
    }
}

#[no_mangle]
pub extern "C" fn fmaxf(a: f32, b: f32) -> f32 {
    if a > b {
        a
    } else {
        b
    }
}

So in the case of rand_distr, you are supposed to provide the impl of the f64 yourself. Ideally these are provided by libm, but it's still quite incomplete. Also I believe these are called Rust core intrinsics, correct me otherwise, in case anyone needs good keywords while searching Google :)

bors bot referenced this issue in comit-network/comit-rs Mar 17, 2021
3538: Bump rand from 0.8.1 to 0.8.3 r=mergify[bot] a=dependabot[bot]

Bumps [rand](https://github.com/rust-random/rand) from 0.8.1 to 0.8.3.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/rust-random/rand/blob/master/CHANGELOG.md">rand's changelog</a>.</em></p>
<blockquote>
<h2>[0.8.3] - 2021-01-25</h2>
<h3>Fixes</h3>
<ul>
<li>Fix <code>no-std</code> + <code>alloc</code> build by gating <code>choose_multiple_weighted</code> on <code>std</code> (<a href="https://github.com/rust-random/rand/issues/1088">#1088</a>)</li>
</ul>
<h2>[0.8.2] - 2021-01-12</h2>
<h3>Fixes</h3>
<ul>
<li>Fix panic in <code>UniformInt::sample_single_inclusive</code> and <code>Rng::gen_range</code> when
providing a full integer range (eg <code>0..=MAX</code>) (<a href="https://github.com/rust-random/rand/issues/1087">#1087</a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/rust-random/rand/commit/6ecbe2626b2cc6110a25c97b1702b347574febc7"><code>6ecbe26</code></a> Merge pull request <a href="https://github.com/rust-random/rand/issues/1089">#1089</a> from dhardy/work</li>
<li><a href="https://github.com/rust-random/rand/commit/8821743325303e4deb6c4a6c934f0e2eb9680205"><code>8821743</code></a> Prepare 0.8.3</li>
<li><a href="https://github.com/rust-random/rand/commit/fa615efd91f83fe8e56799cfa9217d67f7625fb7"><code>fa615ef</code></a> Feature gate choose_multiple_weighted on std</li>
<li><a href="https://github.com/rust-random/rand/commit/22dec87aac43e0e92eb15fa85b2de6da5b3c95b7"><code>22dec87</code></a> CI: more accurate no-default-feature and nightly test targets</li>
<li><a href="https://github.com/rust-random/rand/commit/6a6b9fd06dbf54538d250dfbc4c918f79daa9299"><code>6a6b9fd</code></a> Merge pull request <a href="https://github.com/rust-random/rand/issues/1087">#1087</a> from GautierMinster/fix_uniform_int_panic_on_full_in...</li>
<li><a href="https://github.com/rust-random/rand/commit/2c9085a2de8864ee327af0311f6a8e5747cf25b7"><code>2c9085a</code></a> Bump to 0.8.2 and update changelog</li>
<li><a href="https://github.com/rust-random/rand/commit/4e8c7a4ca2963797d0ec414d04b6239ece905165"><code>4e8c7a4</code></a> distributions/uniform: fix panic in gen_range(0..=MAX)</li>
<li>See full diff in <a href="https://github.com/rust-random/rand/compare/0.8.1...0.8.3">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=rand&package-manager=cargo&previous-version=0.8.1&new-version=0.8.3)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually


</details>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
bors bot referenced this issue in comit-network/comit-rs Mar 17, 2021
3538: Bump rand from 0.8.1 to 0.8.3 r=mergify[bot] a=dependabot[bot]

Bumps [rand](https://github.com/rust-random/rand) from 0.8.1 to 0.8.3.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/rust-random/rand/blob/master/CHANGELOG.md">rand's changelog</a>.</em></p>
<blockquote>
<h2>[0.8.3] - 2021-01-25</h2>
<h3>Fixes</h3>
<ul>
<li>Fix <code>no-std</code> + <code>alloc</code> build by gating <code>choose_multiple_weighted</code> on <code>std</code> (<a href="https://github.com/rust-random/rand/issues/1088">#1088</a>)</li>
</ul>
<h2>[0.8.2] - 2021-01-12</h2>
<h3>Fixes</h3>
<ul>
<li>Fix panic in <code>UniformInt::sample_single_inclusive</code> and <code>Rng::gen_range</code> when
providing a full integer range (eg <code>0..=MAX</code>) (<a href="https://github.com/rust-random/rand/issues/1087">#1087</a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/rust-random/rand/commit/6ecbe2626b2cc6110a25c97b1702b347574febc7"><code>6ecbe26</code></a> Merge pull request <a href="https://github.com/rust-random/rand/issues/1089">#1089</a> from dhardy/work</li>
<li><a href="https://github.com/rust-random/rand/commit/8821743325303e4deb6c4a6c934f0e2eb9680205"><code>8821743</code></a> Prepare 0.8.3</li>
<li><a href="https://github.com/rust-random/rand/commit/fa615efd91f83fe8e56799cfa9217d67f7625fb7"><code>fa615ef</code></a> Feature gate choose_multiple_weighted on std</li>
<li><a href="https://github.com/rust-random/rand/commit/22dec87aac43e0e92eb15fa85b2de6da5b3c95b7"><code>22dec87</code></a> CI: more accurate no-default-feature and nightly test targets</li>
<li><a href="https://github.com/rust-random/rand/commit/6a6b9fd06dbf54538d250dfbc4c918f79daa9299"><code>6a6b9fd</code></a> Merge pull request <a href="https://github.com/rust-random/rand/issues/1087">#1087</a> from GautierMinster/fix_uniform_int_panic_on_full_in...</li>
<li><a href="https://github.com/rust-random/rand/commit/2c9085a2de8864ee327af0311f6a8e5747cf25b7"><code>2c9085a</code></a> Bump to 0.8.2 and update changelog</li>
<li><a href="https://github.com/rust-random/rand/commit/4e8c7a4ca2963797d0ec414d04b6239ece905165"><code>4e8c7a4</code></a> distributions/uniform: fix panic in gen_range(0..=MAX)</li>
<li>See full diff in <a href="https://github.com/rust-random/rand/compare/0.8.1...0.8.3">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=rand&package-manager=cargo&previous-version=0.8.1&new-version=0.8.3)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually


</details>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants