refactor: Move the libm crate to a subdirectory

In preparation for switching to a virtual manifest, move the `libm`
crate into a subdirectory and update paths to match.

Updating `Cargo.toml` is done in the next commit so git tracks the moved
file correctly.
This commit is contained in:
Trevor Gross 2025-04-09 01:21:33 +00:00 committed by Trevor Gross
parent 88dcaf20b5
commit 7077daa6ad
183 changed files with 334 additions and 23 deletions

View file

@ -213,7 +213,7 @@ jobs:
steps:
- uses: actions/checkout@master
- run: |
msrv="$(perl -ne 'print if s/rust-version\s*=\s*"(.*)"/\1/g' Cargo.toml)"
msrv="$(perl -ne 'print if s/rust-version\s*=\s*"(.*)"/\1/g' libm/Cargo.toml)"
echo "MSRV: $msrv"
echo "MSRV=$msrv" >> "$GITHUB_ENV"
- name: Install Rust

View file

@ -63,9 +63,9 @@ MANY_EXTENSIVE_THRESHOLD = 20
# Don't run exhaustive tests if these files change, even if they contaiin a function
# definition.
IGNORE_FILES = [
"src/math/support/",
"src/libm_helper.rs",
"src/math/arch/intrinsics.rs",
"libm/src/math/support/",
"libm/src/libm_helper.rs",
"libm/src/math/arch/intrinsics.rs",
]
TYPES = ["f16", "f32", "f64", "f128"]

View file

@ -1,8 +1,8 @@
#[path = "../../configure.rs"]
#[path = "../../libm/configure.rs"]
mod configure;
fn main() {
println!("cargo:rerun-if-changed=../../configure.rs");
println!("cargo:rerun-if-changed=../../libm/configure.rs");
let cfg = configure::Config::from_env();
configure::emit_libm_config(&cfg);
}

View file

@ -3,7 +3,7 @@ use core::ffi::c_int;
#[allow(dead_code)]
#[allow(clippy::all)] // We don't get `libm`'s list of `allow`s, so just ignore Clippy.
#[allow(unused_imports)]
#[path = "../../../src/math/mod.rs"]
#[path = "../../../libm/src/math/mod.rs"]
pub mod libm;
/// Mark functions `#[no_mangle]` and with the C ABI.

View file

@ -32,7 +32,7 @@ anyhow = "1.0.97"
gmp-mpfr-sys = { version = "1.6.4", optional = true, default-features = false }
iai-callgrind = { version = "0.14.0", optional = true }
indicatif = { version = "0.17.11", default-features = false }
libm = { path = "../..", features = ["unstable-public-internals"] }
libm = { path = "../../libm", features = ["unstable-public-internals"] }
libm-macros = { path = "../libm-macros" }
musl-math-sys = { path = "../musl-math-sys", optional = true }
paste = "1.0.15"

View file

@ -1,9 +1,9 @@
#[path = "../../configure.rs"]
#[path = "../../libm/configure.rs"]
mod configure;
use configure::Config;
fn main() {
println!("cargo:rerun-if-changed=../../configure.rs");
println!("cargo:rerun-if-changed=../../libm/configure.rs");
let cfg = Config::from_env();
configure::emit_test_config(&cfg);
}

View file

@ -7,7 +7,7 @@ publish = false
[dependencies]
[dev-dependencies]
libm = { path = "../../" }
libm = { path = "../../libm" }
[build-dependencies]
cc = "1.2.16"

View file

@ -11,7 +11,7 @@ build-mpfr = ["libm-test/build-mpfr", "dep:rug"]
unstable-float = ["libm/unstable-float", "libm-test/unstable-float", "rug?/nightly-float"]
[dependencies]
libm = { path = "../..", default-features = false }
libm = { path = "../../libm", default-features = false }
libm-macros = { path = "../libm-macros" }
libm-test = { path = "../libm-test", default-features = false }
musl-math-sys = { path = "../musl-math-sys", optional = true }

View file

@ -1,10 +1,10 @@
#![allow(unexpected_cfgs)]
#[path = "../../configure.rs"]
#[path = "../../libm/configure.rs"]
mod configure;
fn main() {
println!("cargo:rerun-if-changed=../../configure.rs");
println!("cargo:rerun-if-changed=../../libm/configure.rs");
let cfg = configure::Config::from_env();
configure::emit_libm_config(&cfg);
}

View file

@ -18,7 +18,7 @@ from typing import Any, Callable, TypeAlias
SELF_PATH = Path(__file__)
ETC_DIR = SELF_PATH.parent
ROOT_DIR = ETC_DIR.parent
LIBM_DIR = ETC_DIR.parent.joinpath("libm")
# These files do not trigger a retest.
IGNORED_SOURCES = ["src/libm_helper.rs", "src/math/support/float_traits.rs"]
@ -75,7 +75,7 @@ class Crate:
"-Zunstable-options",
"-o-",
],
cwd=ROOT_DIR,
cwd=LIBM_DIR,
text=True,
)
j = json.loads(j)
@ -121,8 +121,8 @@ class Crate:
# A lot of the `arch` module is often configured out so doesn't show up in docs. Use
# string matching as a fallback.
for fname in glob("src/math/arch/**.rs", root_dir=ROOT_DIR):
contents = (ROOT_DIR.joinpath(fname)).read_text()
for fname in glob("src/math/arch/**.rs", root_dir=LIBM_DIR):
contents = (LIBM_DIR.joinpath(fname)).read_text()
for name in self.public_functions:
if f"fn {name}" in contents:
@ -188,10 +188,10 @@ class Crate:
include all public API.
"""
flist = sp.check_output(["git", "ls-files"], cwd=ROOT_DIR, text=True)
flist = sp.check_output(["git", "ls-files"], cwd=LIBM_DIR, text=True)
for path in flist.splitlines():
fpath = ROOT_DIR.joinpath(path)
fpath = LIBM_DIR.joinpath(path)
if fpath.is_dir() or fpath == SELF_PATH:
continue
@ -229,7 +229,7 @@ class Crate:
if len(not_found) == 0:
return
relpath = fpath.relative_to(ROOT_DIR)
relpath = fpath.relative_to(LIBM_DIR)
eprint(f"functions not found at {relpath}:{line_num}: {not_found}")
exit(1)
@ -244,7 +244,7 @@ def validate_delimited_block(
"""Identify blocks of code wrapped within `start` and `end`, collect their contents
to a list of strings, and call `validate` for each of those lists.
"""
relpath = fpath.relative_to(ROOT_DIR)
relpath = fpath.relative_to(LIBM_DIR)
block_lines = []
block_start_line: None | int = None
for line_num, line in enumerate(lines):
@ -274,7 +274,7 @@ def validate_delimited_block(
def ensure_sorted(fpath: Path, block_start_line: int, lines: list[str]) -> None:
"""Ensure that a list of lines is sorted, otherwise print a diff and exit."""
relpath = fpath.relative_to(ROOT_DIR)
relpath = fpath.relative_to(LIBM_DIR)
diff_and_exit(
"\n".join(lines),
"\n".join(sorted(lines)),

View file

@ -6,6 +6,7 @@ fn main() {
let cfg = configure::Config::from_env();
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=configure.rs");
println!("cargo:rustc-check-cfg=cfg(assert_no_panic)");
// If set, enable `no-panic`. Requires LTO (`release-opt` profile).

View file

@ -0,0 +1,8 @@
/// Sign of Y, magnitude of X (f32)
///
/// Constructs a number with the magnitude (absolute value) of its
/// first argument, `x`, and the sign of its second argument, `y`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn copysignf(x: f32, y: f32) -> f32 {
super::generic::copysign(x, y)
}

View file

@ -0,0 +1,8 @@
/// Sign of Y, magnitude of X (f128)
///
/// Constructs a number with the magnitude (absolute value) of its
/// first argument, `x`, and the sign of its second argument, `y`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn copysignf128(x: f128, y: f128) -> f128 {
super::generic::copysign(x, y)
}

View file

@ -0,0 +1,8 @@
/// Sign of Y, magnitude of X (f16)
///
/// Constructs a number with the magnitude (absolute value) of its
/// first argument, `x`, and the sign of its second argument, `y`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn copysignf16(x: f16, y: f16) -> f16 {
super::generic::copysign(x, y)
}

View file

@ -0,0 +1,39 @@
/// Absolute value (magnitude) (f32)
///
/// Calculates the absolute value (magnitude) of the argument `x`,
/// by direct manipulation of the bit representation of `x`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fabsf(x: f32) -> f32 {
select_implementation! {
name: fabsf,
use_arch: all(target_arch = "wasm32", intrinsics_enabled),
args: x,
}
super::generic::fabs(x)
}
// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
#[cfg(not(target_arch = "powerpc64"))]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sanity_check() {
assert_eq!(fabsf(-1.0), 1.0);
assert_eq!(fabsf(2.8), 2.8);
}
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs
#[test]
fn spec_tests() {
assert!(fabsf(f32::NAN).is_nan());
for f in [0.0, -0.0].iter().copied() {
assert_eq!(fabsf(f), 0.0);
}
for f in [f32::INFINITY, f32::NEG_INFINITY].iter().copied() {
assert_eq!(fabsf(f), f32::INFINITY);
}
}
}

View file

@ -0,0 +1,31 @@
/// Absolute value (magnitude) (f128)
///
/// Calculates the absolute value (magnitude) of the argument `x`,
/// by direct manipulation of the bit representation of `x`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fabsf128(x: f128) -> f128 {
super::generic::fabs(x)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sanity_check() {
assert_eq!(fabsf128(-1.0), 1.0);
assert_eq!(fabsf128(2.8), 2.8);
}
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs
#[test]
fn spec_tests() {
assert!(fabsf128(f128::NAN).is_nan());
for f in [0.0, -0.0].iter().copied() {
assert_eq!(fabsf128(f), 0.0);
}
for f in [f128::INFINITY, f128::NEG_INFINITY].iter().copied() {
assert_eq!(fabsf128(f), f128::INFINITY);
}
}
}

View file

@ -0,0 +1,31 @@
/// Absolute value (magnitude) (f16)
///
/// Calculates the absolute value (magnitude) of the argument `x`,
/// by direct manipulation of the bit representation of `x`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fabsf16(x: f16) -> f16 {
super::generic::fabs(x)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sanity_check() {
assert_eq!(fabsf16(-1.0), 1.0);
assert_eq!(fabsf16(2.8), 2.8);
}
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs
#[test]
fn spec_tests() {
assert!(fabsf16(f16::NAN).is_nan());
for f in [0.0, -0.0].iter().copied() {
assert_eq!(fabsf16(f), 0.0);
}
for f in [f16::INFINITY, f16::NEG_INFINITY].iter().copied() {
assert_eq!(fabsf16(f), f16::INFINITY);
}
}
}

View file

@ -0,0 +1,12 @@
/// Positive difference (f32)
///
/// Determines the positive difference between arguments, returning:
/// * x - y if x > y, or
/// * +0 if x <= y, or
/// * NAN if either argument is NAN.
///
/// A range error may occur.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fdimf(x: f32, y: f32) -> f32 {
super::generic::fdim(x, y)
}

View file

@ -0,0 +1,12 @@
/// Positive difference (f128)
///
/// Determines the positive difference between arguments, returning:
/// * x - y if x > y, or
/// * +0 if x <= y, or
/// * NAN if either argument is NAN.
///
/// A range error may occur.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fdimf128(x: f128, y: f128) -> f128 {
super::generic::fdim(x, y)
}

View file

@ -0,0 +1,12 @@
/// Positive difference (f16)
///
/// Determines the positive difference between arguments, returning:
/// * x - y if x > y, or
/// * +0 if x <= y, or
/// * NAN if either argument is NAN.
///
/// A range error may occur.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fdimf16(x: f16, y: f16) -> f16 {
super::generic::fdim(x, y)
}

View file

@ -0,0 +1,13 @@
/// Floor (f32)
///
/// Finds the nearest integer less than or equal to `x`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn floorf(x: f32) -> f32 {
select_implementation! {
name: floorf,
use_arch: all(target_arch = "wasm32", intrinsics_enabled),
args: x,
}
return super::generic::floor(x);
}

View file

@ -0,0 +1,7 @@
/// Floor (f128)
///
/// Finds the nearest integer less than or equal to `x`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn floorf128(x: f128) -> f128 {
return super::generic::floor(x);
}

View file

@ -0,0 +1,7 @@
/// Floor (f16)
///
/// Finds the nearest integer less than or equal to `x`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn floorf16(x: f16) -> f16 {
return super::generic::floor(x);
}

View file

@ -0,0 +1,5 @@
/// Calculate the remainder of `x / y`, the precise result of `x - trunc(x / y) * y`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fmodf(x: f32, y: f32) -> f32 {
super::generic::fmod(x, y)
}

View file

@ -0,0 +1,5 @@
/// Calculate the remainder of `x / y`, the precise result of `x - trunc(x / y) * y`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fmodf128(x: f128, y: f128) -> f128 {
super::generic::fmod(x, y)
}

View file

@ -0,0 +1,5 @@
/// Calculate the remainder of `x / y`, the precise result of `x - trunc(x / y) * y`.
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fmodf16(x: f16, y: f16) -> f16 {
super::generic::fmod(x, y)
}

Some files were not shown because too many files have changed in this diff Show more