Skip to content

Commit

Permalink
Merge pull request #327 from tgross35/config-rustfmt
Browse files Browse the repository at this point in the history
Add a rustfmt.toml file matching rust-lang/rust
  • Loading branch information
tgross35 authored Oct 27, 2024
2 parents f4e5b38 + 719b487 commit 3b8a7de
Show file tree
Hide file tree
Showing 60 changed files with 142 additions and 394 deletions.
5 changes: 5 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Use `git config blame.ignorerevsfile .git-blame-ignore-revs` to make
# `git blame` ignore the following commits.

# Reformat with a new `.rustfmt.toml`
5882cabb83c30bf7c36023f9a55a80583636b0e8
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: Install Rust
run: rustup update stable && rustup default stable && rustup component add rustfmt
run: rustup update nightly && rustup default nightly && rustup component add rustfmt
- run: cargo fmt -- --check

wasm:
Expand Down
5 changes: 5 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This matches rustc
style_edition = "2024"
use_small_heuristics = "Max"
group_imports = "StdExternalCrate"
imports_granularity = "Module"
43 changes: 11 additions & 32 deletions crates/libm-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ fn main() {

#[cfg(feature = "test-musl-serialized")]
mod musl_reference_tests {
use rand::seq::SliceRandom;
use rand::Rng;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use std::{env, fs};

use rand::Rng;
use rand::seq::SliceRandom;

// Number of tests to generate for each function
const NTESTS: usize = 500;
Expand Down Expand Up @@ -60,10 +60,7 @@ mod musl_reference_tests {
return;
}

let files = fs::read_dir(math_src)
.unwrap()
.map(|f| f.unwrap().path())
.collect::<Vec<_>>();
let files = fs::read_dir(math_src).unwrap().map(|f| f.unwrap().path()).collect::<Vec<_>>();

let mut math = Vec::new();
for file in files {
Expand Down Expand Up @@ -112,12 +109,7 @@ mod musl_reference_tests {
let tail = eat(tail, " -> ");
let ret = parse_retty(tail.replace("{", "").trim());

return Function {
name: name.to_string(),
args,
ret,
tests: Vec::new(),
};
return Function { name: name.to_string(), args, ret, tests: Vec::new() };

fn parse_ty(s: &str) -> Ty {
match s {
Expand Down Expand Up @@ -156,11 +148,7 @@ mod musl_reference_tests {
}

fn generate_test<R: Rng>(function: &Function, rng: &mut R) -> Test {
let mut inputs = function
.args
.iter()
.map(|ty| ty.gen_i64(rng))
.collect::<Vec<_>>();
let mut inputs = function.args.iter().map(|ty| ty.gen_i64(rng)).collect::<Vec<_>>();

// First argument to this function appears to be a number of
// iterations, so passing in massive random numbers causes it to
Expand All @@ -180,25 +168,20 @@ mod musl_reference_tests {

impl Ty {
fn gen_i64<R: Rng>(&self, r: &mut R) -> i64 {
use std::f32;
use std::f64;
use std::{f32, f64};

return match self {
Ty::F32 => {
if r.gen_range(0..20) < 1 {
let i = *[f32::NAN, f32::INFINITY, f32::NEG_INFINITY]
.choose(r)
.unwrap();
let i = *[f32::NAN, f32::INFINITY, f32::NEG_INFINITY].choose(r).unwrap();
i.to_bits().into()
} else {
r.gen::<f32>().to_bits().into()
}
}
Ty::F64 => {
if r.gen_range(0..20) < 1 {
let i = *[f64::NAN, f64::INFINITY, f64::NEG_INFINITY]
.choose(r)
.unwrap();
let i = *[f64::NAN, f64::INFINITY, f64::NEG_INFINITY].choose(r).unwrap();
i.to_bits() as i64
} else {
r.gen::<f64>().to_bits() as i64
Expand Down Expand Up @@ -424,11 +407,7 @@ mod musl_reference_tests {
src.push_str(");");

for (i, ret) in function.ret.iter().enumerate() {
let get = if function.ret.len() == 1 {
String::new()
} else {
format!(".{}", i)
};
let get = if function.ret.len() == 1 { String::new() } else { format!(".{}", i) };
src.push_str(&(match ret {
Ty::F32 => format!("if libm::_eqf(output{}, f32::from_bits(expected[{}] as u32)).is_ok() {{ continue }}", get, i),
Ty::F64 => format!("if libm::_eq(output{}, f64::from_bits(expected[{}] as u64)).is_ok() {{ continue }}", get, i),
Expand Down
15 changes: 4 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ mod math;

use core::{f32, f64};

pub use self::math::*;
pub use libm_helper::*;

pub use self::math::*;

/// Approximate equality with 1 ULP of tolerance
#[doc(hidden)]
#[inline]
Expand All @@ -29,11 +30,7 @@ pub fn _eqf(a: f32, b: f32) -> Result<(), u32> {
} else {
let err = (a.to_bits() as i32).wrapping_sub(b.to_bits() as i32).abs();

if err <= 1 {
Ok(())
} else {
Err(err as u32)
}
if err <= 1 { Ok(()) } else { Err(err as u32) }
}
}

Expand All @@ -45,10 +42,6 @@ pub fn _eq(a: f64, b: f64) -> Result<(), u64> {
} else {
let err = (a.to_bits() as i64).wrapping_sub(b.to_bits() as i64).abs();

if err <= 1 {
Ok(())
} else {
Err(err as u64)
}
if err <= 1 { Ok(()) } else { Err(err as u64) }
}
}
6 changes: 1 addition & 5 deletions src/math/asin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,5 @@ pub fn asin(mut x: f64) -> f64 {
c = (z - f * f) / (s + f);
x = 0.5 * PIO2_HI - (2.0 * s * r - (PIO2_LO - 2.0 * c) - (0.5 * PIO2_HI - 2.0 * f));
}
if hx >> 31 != 0 {
-x
} else {
x
}
if hx >> 31 != 0 { -x } else { x }
}
6 changes: 1 addition & 5 deletions src/math/asinf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,5 @@ pub fn asinf(mut x: f32) -> f32 {
let z = (1. - fabsf(x)) * 0.5;
let s = sqrt(z as f64);
x = (PIO2 - 2. * (s + s * (r(z) as f64))) as f32;
if (hx >> 31) != 0 {
-x
} else {
x
}
if (hx >> 31) != 0 { -x } else { x }
}
6 changes: 1 addition & 5 deletions src/math/asinh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,5 @@ pub fn asinh(mut x: f64) -> f64 {
force_eval!(x + x1p120);
}

if sign {
-x
} else {
x
}
if sign { -x } else { x }
}
6 changes: 1 addition & 5 deletions src/math/asinhf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,5 @@ pub fn asinhf(mut x: f32) -> f32 {
force_eval!(x + x1p120);
}

if sign {
-x
} else {
x
}
if sign { -x } else { x }
}
12 changes: 5 additions & 7 deletions src/math/atan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
* to produce the hexadecimal values shown.
*/

use super::fabs;
use core::f64;

use super::fabs;

const ATANHI: [f64; 4] = [
4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */
7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */
Expand Down Expand Up @@ -128,18 +129,15 @@ pub fn atan(x: f64) -> f64 {

let z = i!(ATANHI, id as usize) - (x * (s1 + s2) - i!(ATANLO, id as usize) - x);

if sign != 0 {
-z
} else {
z
}
if sign != 0 { -z } else { z }
}

#[cfg(test)]
mod tests {
use super::atan;
use core::f64;

use super::atan;

#[test]
fn sanity_check() {
for (input, answer) in [
Expand Down
3 changes: 1 addition & 2 deletions src/math/atan2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
* to produce the hexadecimal values shown.
*/

use super::atan;
use super::fabs;
use super::{atan, fabs};

const PI: f64 = 3.1415926535897931160E+00; /* 0x400921FB, 0x54442D18 */
const PI_LO: f64 = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
Expand Down
3 changes: 1 addition & 2 deletions src/math/atan2f.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
* ====================================================
*/

use super::atanf;
use super::fabsf;
use super::{atanf, fabsf};

const PI: f32 = 3.1415927410e+00; /* 0x40490fdb */
const PI_LO: f32 = -8.7422776573e-08; /* 0xb3bbbd2e */
Expand Down
15 changes: 3 additions & 12 deletions src/math/atanf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ const ATAN_LO: [f32; 4] = [
7.5497894159e-08, /* atan(inf)lo 0x33a22168 */
];

const A_T: [f32; 5] = [
3.3333328366e-01,
-1.9999158382e-01,
1.4253635705e-01,
-1.0648017377e-01,
6.1687607318e-02,
];
const A_T: [f32; 5] =
[3.3333328366e-01, -1.9999158382e-01, 1.4253635705e-01, -1.0648017377e-01, 6.1687607318e-02];

/// Arctangent (f32)
///
Expand Down Expand Up @@ -104,9 +99,5 @@ pub fn atanf(mut x: f32) -> f32 {
}
let id = id as usize;
let z = i!(ATAN_HI, id) - ((x * (s1 + s2) - i!(ATAN_LO, id)) - x);
if sign {
-z
} else {
z
}
if sign { -z } else { z }
}
6 changes: 1 addition & 5 deletions src/math/atanh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,5 @@ pub fn atanh(x: f64) -> f64 {
y = 0.5 * log1p(2.0 * (y / (1.0 - y)));
}

if sign {
-y
} else {
y
}
if sign { -y } else { y }
}
6 changes: 1 addition & 5 deletions src/math/atanhf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,5 @@ pub fn atanhf(mut x: f32) -> f32 {
x = 0.5 * log1pf(2.0 * (x / (1.0 - x)));
}

if sign {
-x
} else {
x
}
if sign { -x } else { x }
}
15 changes: 4 additions & 11 deletions src/math/ceil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,21 @@ pub fn ceil(x: f64) -> f64 {
return x;
}
// y = int(x) - x, where int(x) is an integer neighbor of x
y = if (u >> 63) != 0 {
x - TOINT + TOINT - x
} else {
x + TOINT - TOINT - x
};
y = if (u >> 63) != 0 { x - TOINT + TOINT - x } else { x + TOINT - TOINT - x };
// special case because of non-nearest rounding modes
if e < 0x3ff {
force_eval!(y);
return if (u >> 63) != 0 { -0. } else { 1. };
}
if y < 0. {
x + y + 1.
} else {
x + y
}
if y < 0. { x + y + 1. } else { x + y }
}

#[cfg(test)]
mod tests {
use super::*;
use core::f64::*;

use super::*;

#[test]
fn sanity_check() {
assert_eq!(ceil(1.1), 2.0);
Expand Down
3 changes: 2 additions & 1 deletion src/math/ceilf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ pub fn ceilf(x: f32) -> f32 {
#[cfg(not(target_arch = "powerpc64"))]
#[cfg(test)]
mod tests {
use super::*;
use core::f32::*;

use super::*;

#[test]
fn sanity_check() {
assert_eq!(ceilf(1.1), 2.0);
Expand Down
4 changes: 2 additions & 2 deletions src/math/cosf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* ====================================================
*/

use super::{k_cosf, k_sinf, rem_pio2f};

use core::f64::consts::FRAC_PI_2;

use super::{k_cosf, k_sinf, rem_pio2f};

/* Small multiples of pi/2 rounded to double precision. */
const C1_PIO2: f64 = 1. * FRAC_PI_2; /* 0x3FF921FB, 0x54442D18 */
const C2_PIO2: f64 = 2. * FRAC_PI_2; /* 0x400921FB, 0x54442D18 */
Expand Down
4 changes: 1 addition & 3 deletions src/math/cosh.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::exp;
use super::expm1;
use super::k_expo2;
use super::{exp, expm1, k_expo2};

/// Hyperbolic cosine (f64)
///
Expand Down
4 changes: 1 addition & 3 deletions src/math/coshf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::expf;
use super::expm1f;
use super::k_expo2f;
use super::{expf, expm1f, k_expo2f};

/// Hyperbolic cosine (f64)
///
Expand Down
Loading

0 comments on commit 3b8a7de

Please sign in to comment.