Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 8c7418e

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]: #533 [2]: https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/Dependencies.20affecting.20codegen/with/513079387
1 parent 1c64b16 commit 8c7418e

21 files changed

+27
-2
lines changed

src/math/fma.rs

+1
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,

src/math/fma_wide.rs

+1
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>,

src/math/generic/ceil.rs

+2
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

src/math/generic/copysign.rs

+1
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();

src/math/generic/fabs.rs

+1
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)

src/math/generic/fdim.rs

+1
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
}

src/math/generic/floor.rs

+2
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

src/math/generic/fmax.rs

+1-1
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

src/math/generic/fmaximum.rs

+1
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

src/math/generic/fmaximum_num.rs

+1
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()) {

src/math/generic/fmin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
use super::super::Float;
1818

19+
#[inline]
1920
pub fn fmin<F: Float>(x: F, y: F) -> F {
2021
let res = if y.is_nan() || x < y { x } else { y };
2122
// Canonicalize

src/math/generic/fminimum.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
use super::super::Float;
1313

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

src/math/generic/fminimum_num.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
use super::super::Float;
1515

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

src/math/generic/fmod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use super::super::{CastFrom, Float, Int, MinInt};
55

6-
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
6+
#[inline]
77
pub fn fmod<F: Float>(x: F, y: F) -> F {
88
let zero = F::Int::ZERO;
99
let one = F::Int::ONE;

src/math/generic/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Note: generic functions are marked `#[inline]` because, even though generic functions are
2+
// typically inlined, this does not seem to always be the case.
3+
14
mod ceil;
25
mod copysign;
36
mod fabs;

src/math/generic/rint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::super::support::{FpResult, Round};
66

77
/// IEEE 754-2019 `roundToIntegralExact`, which respects rounding mode and raises inexact if
88
/// applicable.
9+
#[inline]
910
pub fn rint_round<F: Float>(x: F, _round: Round) -> FpResult<F> {
1011
let toint = F::ONE / F::EPSILON;
1112
let e = x.ex();

src/math/generic/round.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::super::{Float, MinInt};
22
use super::{copysign, trunc};
33

4+
#[inline]
45
pub fn round<F: Float>(x: F) -> F {
56
let f0p5 = F::from_parts(false, F::EXP_BIAS - 1, F::Int::ZERO); // 0.5
67
let f0p25 = F::from_parts(false, F::EXP_BIAS - 2, F::Int::ZERO); // 0.25

src/math/generic/scalbn.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use super::super::{CastFrom, CastInto, Float, IntTy, MinInt};
1616
/// >
1717
/// > If the calculation does not overflow or underflow, the returned value is exact and
1818
/// > independent of the current rounding direction mode.
19+
#[inline]
1920
pub fn scalbn<F: Float>(mut x: F, mut n: i32) -> F
2021
where
2122
u32: CastInto<F::Int>,

src/math/generic/sqrt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use super::super::support::{FpResult, IntTy, Round, Status, cold_path};
4545
use super::super::{CastFrom, CastInto, DInt, Float, HInt, Int, MinInt};
4646

47+
#[inline]
4748
pub fn sqrt<F>(x: F) -> F
4849
where
4950
F: Float + SqrtHelper,
@@ -57,6 +58,7 @@ where
5758
sqrt_round(x, Round::Nearest).val
5859
}
5960

61+
#[inline]
6062
pub fn sqrt_round<F>(x: F, _round: Round) -> FpResult<F>
6163
where
6264
F: Float + SqrtHelper,

src/math/generic/trunc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
use super::super::support::{FpResult, Status};
55
use super::super::{Float, Int, IntTy, MinInt};
66

7+
#[inline]
78
pub fn trunc<F: Float>(x: F) -> F {
89
trunc_status(x).val
910
}
1011

12+
#[inline]
1113
pub fn trunc_status<F: Float>(x: F) -> FpResult<F> {
1214
let mut xi: F::Int = x.to_bits();
1315
let e: i32 = x.exp_unbiased();

src/math/roundeven.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub fn roundevenf128(x: f128) -> f128 {
3030
roundeven_impl(x)
3131
}
3232

33+
#[inline]
3334
pub fn roundeven_impl<F: Float>(x: F) -> F {
3435
super::generic::rint_round(x, Round::Nearest).val
3536
}

0 commit comments

Comments
 (0)