Skip to content

mul_add feature + macos CI #4

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

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
os: [
ubuntu-latest,
windows-latest,
# macos-latest # disabled due to incompatibility. See issue #1
macos-latest,
]
rust: [stable]
steps:
Expand All @@ -38,6 +38,8 @@ jobs:
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Run tests on Release
run: cargo test --release --verbose
if: matrix.os != 'macos-latest'
run: cargo test --verbose && cargo test --release --verbose
- name: Run tests with FMA (macOS)
if: matrix.os == 'macos-latest'
run: cargo test --verbose --features mul_add && cargo test --release --verbose --features mul_add
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ name = "pymath"
version = "0.1.0"
edition = "2024"

[features]
# Turning on this feature on aarch64-apple-darwin helps bit representation compatibility
# See also: https://github.com/python/cpython/issues/132763
mul_add = []

[dev-dependencies]
proptest = "1.6.0"
pyo3 = { version = "0.24", features = ["abi3"] }
15 changes: 12 additions & 3 deletions src/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ const LANCZOS_DEN_COEFFS: [f64; LANCZOS_N] = [
1.0,
];

fn mul_add(a: f64, b: f64, c: f64) -> f64 {
if cfg!(feature = "mul_add") {
a.mul_add(b, c)
} else {
a * b + c
}
}

fn lanczos_sum(x: f64) -> f64 {
let mut num = 0.0;
let mut den = 0.0;
Expand All @@ -50,8 +58,8 @@ fn lanczos_sum(x: f64) -> f64 {
// this resulted in lower accuracy.
if x < 5.0 {
for i in (0..LANCZOS_N).rev() {
num = num * x + LANCZOS_NUM_COEFFS[i];
den = den * x + LANCZOS_DEN_COEFFS[i];
num = mul_add(num, x, LANCZOS_NUM_COEFFS[i]);
den = mul_add(den, x, LANCZOS_DEN_COEFFS[i]);
}
} else {
for i in 0..LANCZOS_N {
Expand Down Expand Up @@ -237,7 +245,8 @@ pub fn lgamma(x: f64) -> Result<f64, Error> {
// absorbed the exp(-lanczos_g) term, and throwing out the lanczos_g
// subtraction below; it's probably not worth it.
let mut r = lanczos_sum(absx).ln() - LANCZOS_G;
r += (absx - 0.5) * ((absx + LANCZOS_G - 0.5).ln() - 1.0);
let t = absx - 0.5;
r = mul_add(t, (absx + LANCZOS_G - 0.5).ln() - 1.0, r);

if x < 0.0 {
// Use reflection formula to get value for negative x
Expand Down
Loading