Skip to content

Commit 161036a

Browse files
committed
Mark generic functions #[inline]
Benchmarks for [1] seemed to indicate that repository organization for some reason had an effect on performance, even though the exact same rustc commands were running (though some with a different order). After investigating more, it appears that dependencies may have an affect on inlining thresholds for generic functions. It is surprising that this happens, we more or less expect that public functions will be standalone but everything they call will be inlined. To help ensure this, mark all generic functions `#[inline]` if they should be merged into the public function. Zulip discussion at [2]. [1]: rust-lang/libm#533 [2]: https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/Dependencies.20affecting.20codegen/with/513079387
1 parent c3c363f commit 161036a

21 files changed

+27
-2
lines changed

libm/src/math/fma.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn fmaf128(x: f128, y: f128, z: f128) -> f128 {
2929

3030
/// Fused multiply-add that works when there is not a larger float size available. Computes
3131
/// `(x * y) + z`.
32+
#[inline]
3233
pub fn fma_round<F>(x: F, y: F, z: F, _round: Round) -> FpResult<F>
3334
where
3435
F: Float,

libm/src/math/fma_wide.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn fmaf(x: f32, y: f32, z: f32) -> f32 {
2828

2929
/// Fma implementation when a hardware-backed larger float type is available. For `f32` and `f64`,
3030
/// `f64` has enough precision to represent the `f32` in its entirety, except for double rounding.
31+
#[inline]
3132
pub fn fma_wide_round<F, B>(x: F, y: F, z: F, round: Round) -> FpResult<F>
3233
where
3334
F: Float + HFloat<D = B>,

libm/src/math/generic/ceil.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use super::super::support::{FpResult, Status};
1111
use super::super::{Float, Int, IntTy, MinInt};
1212

13+
#[inline]
1314
pub fn ceil<F: Float>(x: F) -> F {
1415
ceil_status(x).val
1516
}
1617

18+
#[inline]
1719
pub fn ceil_status<F: Float>(x: F) -> FpResult<F> {
1820
let zero = IntTy::<F>::ZERO;
1921

libm/src/math/generic/copysign.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::super::Float;
22

33
/// Copy the sign of `y` to `x`.
4+
#[inline]
45
pub fn copysign<F: Float>(x: F, y: F) -> F {
56
let mut ux = x.to_bits();
67
let uy = y.to_bits();

libm/src/math/generic/fabs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::super::Float;
22

33
/// Absolute value.
4+
#[inline]
45
pub fn fabs<F: Float>(x: F) -> F {
56
let abs_mask = !F::SIGN_MASK;
67
F::from_bits(x.to_bits() & abs_mask)

libm/src/math/generic/fdim.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::super::Float;
22

3+
#[inline]
34
pub fn fdim<F: Float>(x: F, y: F) -> F {
45
if x <= y { F::ZERO } else { x - y }
56
}

libm/src/math/generic/floor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use super::super::support::{FpResult, Status};
1111
use super::super::{Float, Int, IntTy, MinInt};
1212

13+
#[inline]
1314
pub fn floor<F: Float>(x: F) -> F {
1415
floor_status(x).val
1516
}
1617

18+
#[inline]
1719
pub fn floor_status<F: Float>(x: F) -> FpResult<F> {
1820
let zero = IntTy::<F>::ZERO;
1921

libm/src/math/generic/fmax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
use super::super::Float;
1818

19-
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
19+
#[inline]
2020
pub fn fmax<F: Float>(x: F, y: F) -> F {
2121
let res = if x.is_nan() || x < y { y } else { x };
2222
// Canonicalize

libm/src/math/generic/fmaximum.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
use super::super::Float;
1313

14+
#[inline]
1415
pub fn fmaximum<F: Float>(x: F, y: F) -> F {
1516
let res = if x.is_nan() {
1617
x

libm/src/math/generic/fmaximum_num.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
use super::super::Float;
1515

16+
#[inline]
1617
pub fn fmaximum_num<F: Float>(x: F, y: F) -> F {
1718
let res =
1819
if x.is_nan() || x < y || (x.to_bits() == F::NEG_ZERO.to_bits() && y.is_sign_positive()) {

0 commit comments

Comments
 (0)