Skip to content

Commit 6084dad

Browse files
bors[bot]japaric
andcommitted
72: move a chunk of the README into CONTRIBUTING.md r=japaric a=japaric Co-authored-by: Jorge Aparicio <[email protected]>
2 parents 7cdd8c2 + b685b18 commit 6084dad

File tree

5 files changed

+105
-52
lines changed

5 files changed

+105
-52
lines changed

CONTRIBUTING.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# How to contribute
2+
3+
- Pick your favorite math function from the [issue tracker].
4+
- Look for the C implementation of the function in the [MUSL source code][src].
5+
- Copy paste the C code into a Rust file in the `src/math` directory and adjust `src/math/mod.rs`
6+
accordingly. Also, uncomment the corresponding trait method in `src/lib.rs`.
7+
- Run `cargo watch check` and fix the compiler errors.
8+
- Tweak the bottom of `test-generator/src/main.rs` to add your function to the test suite.
9+
- If you can, run the full test suite locally (see the [testing](#testing) section below). If you
10+
can't, no problem! Your PR will be fully tested automatically. Though you may still want to add
11+
and run some unit tests. See the bottom of [`src/math/truncf.rs`] for an example of such tests;
12+
you can run unit tests with the `cargo test --lib` command.
13+
- Send us a pull request!
14+
- :tada:
15+
16+
[issue tracker]: https://github.com/japaric/libm/issues
17+
[src]: https://git.musl-libc.org/cgit/musl/tree/src/math
18+
[`src/math/truncf.rs`]: https://github.com/japaric/libm/blob/master/src/math/truncf.rs
19+
20+
Check [PR #65] for an example.
21+
22+
[PR #65]: https://github.com/japaric/libm/pull/65
23+
24+
## Tips and tricks
25+
26+
- *IMPORTANT* The code in this crate will end up being used in the `core` crate so it can **not**
27+
have any external dependencies (other than `core` itself).
28+
29+
- Only use relative imports within the `math` directory / module, e.g. `use self::fabs::fabs` or
30+
`use super::isnanf`. Absolute imports from core are OK, e.g. `use core::u64`.
31+
32+
- To reinterpret a float as an integer use the `to_bits` method. The MUSL code uses the
33+
`GET_FLOAT_WORD` macro, or a union, to do this operation.
34+
35+
- To reinterpret an integer as a float use the `f32::from_bits` constructor. The MUSL code uses the
36+
`SET_FLOAT_WORD` macro, or a union, to do this operation.
37+
38+
- You may encounter weird literals like `0x1p127f` in the MUSL code. These are hexadecimal floating
39+
point literals. Rust (the language) doesn't support these kind of literals. The best way I have
40+
found to deal with these literals is to turn them into their integer representation using the
41+
[`hexf!`] macro and then turn them back into floats. See below:
42+
43+
[`hexf!`]: https://crates.io/crates/hexf
44+
45+
``` rust
46+
// Step 1: write a program to convert the float into its integer representation
47+
#[macro_use]
48+
extern crate hexf;
49+
50+
fn main() {
51+
println!("{:#x}", hexf32!("0x1.0p127").to_bits());
52+
}
53+
```
54+
55+
``` console
56+
$ # Step 2: run the program
57+
$ cargo run
58+
0x7f000000
59+
```
60+
61+
``` rust
62+
// Step 3: copy paste the output into libm
63+
let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 12
64+
```
65+
66+
- Rust code panics on arithmetic overflows when not optimized. You may need to use the [`Wrapping`]
67+
newtype to avoid this problem.
68+
69+
[`Wrapping`]: https://doc.rust-lang.org/std/num/struct.Wrapping.html
70+
71+
## Testing
72+
73+
The test suite of this crate can only be run on x86_64 Linux systems using the following commands:
74+
75+
``` console
76+
$ # The test suite depends on the `cross` tool so install it if you don't have it
77+
$ cargo install cross
78+
79+
$ # and the `cross` tool requires docker to be running
80+
$ systemctl start docker
81+
82+
$ # execute the test suite for the x86_64 target
83+
$ TARGET=x86_64-unknown-linux-gnu bash ci/script.sh
84+
85+
$ # execute the test suite for the ARMv7 target
86+
$ TARGET=armv7-unknown-linux-gnueabihf bash ci/script.sh
87+
```

README.md

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,9 @@ A port of [MUSL]'s libm to Rust.
99
The short term goal of this library is to enable math support (e.g. `sin`, `atan2`) for the
1010
`wasm32-unknown-unknown` target. The longer term goal is to enable math support in the `core` crate.
1111

12-
## Testing
13-
14-
The test suite of this crate can only be run on x86_64 Linux systems.
15-
16-
```
17-
$ # The test suite depends on the `cross` tool so install it if you don't have it
18-
$ cargo install cross
19-
20-
$ # and the `cross` tool requires docker to be running
21-
$ systemctl start docker
22-
23-
$ # execute the test suite for the x86_64 target
24-
$ TARGET=x86_64-unknown-linux-gnu bash ci/script.sh
25-
26-
$ # execute the test suite for the ARMv7 target
27-
$ TARGET=armv7-unknown-linux-gnueabihf bash ci/script.sh
28-
```
29-
3012
## Contributing
3113

32-
- Pick your favorite math function from the [issue tracker].
33-
- Look for the C implementation of the function in the [MUSL source code][src].
34-
- Copy paste the C code into a Rust file in the `src/math` directory and adjust `src/math/mod.rs`
35-
accordingly. Also, uncomment the corresponding trait method in `src/lib.rs`.
36-
- Run `cargo watch check` and fix the compiler errors.
37-
- Tweak the bottom of `test-generator/src/main.rs` to add your function to the test suite.
38-
- If you can, run the test suite locally. If you can't, no problem! Your PR will be tested
39-
automatically.
40-
- Send us a pull request!
41-
- :tada:
42-
43-
[issue tracker]: https://github.com/japaric/libm/issues
44-
[src]: https://git.musl-libc.org/cgit/musl/tree/src/math
45-
46-
Check [PR #2] for an example.
47-
48-
[PR #2]: https://github.com/japaric/libm/pull/2
49-
50-
### Notes
51-
52-
- Only use relative imports within the `math` directory / module, e.g. `use self::fabs::fabs` or
53-
`use super::isnanf`. Absolute imports from core are OK, e.g. `use core::u64`.
54-
55-
- To reinterpret a float as an integer use the `to_bits` method. The MUSL code uses the
56-
`GET_FLOAT_WORD` macro, or a union, to do this operation.
57-
58-
- To reinterpret an integer as a float use the `f32::from_bits` constructor. The MUSL code uses the
59-
`SET_FLOAT_WORD` macro, or a union, to do this operation.
60-
61-
- Rust code panics on arithmetic overflows when not optimized. You may need to use the [`Wrapping`]
62-
newtype to avoid this problem.
63-
64-
[`Wrapping`]: https://doc.rust-lang.org/std/num/struct.Wrapping.html
14+
Please check [CONTRIBUTING.md](CONTRIBUTING.md)
6515

6616
## License
6717

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ impl F32Ext for f32 {
355355
}
356356
}
357357

358-
/// Math support for `f32`
358+
/// Math support for `f64`
359359
///
360360
/// This trait is sealed and cannot be implemented outside of `libm`.
361361
pub trait F64Ext: private::Sealed {

src/math/trunc.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ pub fn trunc(x: f64) -> f64 {
2222
i &= !m;
2323
f64::from_bits(i)
2424
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
#[test]
29+
fn sanity_check() {
30+
assert_eq!(super::trunc(1.1), 1.0);
31+
}
32+
}

src/math/truncf.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ pub fn truncf(x: f32) -> f32 {
2222
i &= !m;
2323
f32::from_bits(i)
2424
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
#[test]
29+
fn sanity_check() {
30+
assert_eq!(super::truncf(1.1), 1.0);
31+
}
32+
}

0 commit comments

Comments
 (0)