Skip to content

Commit 84c47b8

Browse files
committed
Auto merge of rust-lang#108717 - TDecki:dec2flt-inline, r=thomcc
Add inlining annotations in `dec2flt`. Currently, the combination of `dec2flt` being generic and the `FromStr` implementaions containing inline anttributes causes massive amounts of assembly to be generated whenever these implementation are used. In addition, the assembly has calls to function which ought to be inlined, but they are not (even when using lto). This Pr fixes this.
2 parents 3ff4d56 + db2c6e0 commit 84c47b8

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

library/core/src/num/dec2flt/common.rs

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ pub struct BiasedFp {
192192
}
193193

194194
impl BiasedFp {
195+
#[inline]
195196
pub const fn zero_pow2(e: i32) -> Self {
196197
Self { f: 0, e }
197198
}

library/core/src/num/dec2flt/float.rs

+4
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ impl RawFloat for f32 {
118118
const SMALLEST_POWER_OF_TEN: i32 = -65;
119119
const LARGEST_POWER_OF_TEN: i32 = 38;
120120

121+
#[inline]
121122
fn from_u64(v: u64) -> Self {
122123
debug_assert!(v <= Self::MAX_MANTISSA_FAST_PATH);
123124
v as _
124125
}
125126

127+
#[inline]
126128
fn from_u64_bits(v: u64) -> Self {
127129
f32::from_bits((v & 0xFFFFFFFF) as u32)
128130
}
@@ -169,11 +171,13 @@ impl RawFloat for f64 {
169171
const SMALLEST_POWER_OF_TEN: i32 = -342;
170172
const LARGEST_POWER_OF_TEN: i32 = 308;
171173

174+
#[inline]
172175
fn from_u64(v: u64) -> Self {
173176
debug_assert!(v <= Self::MAX_MANTISSA_FAST_PATH);
174177
v as _
175178
}
176179

180+
#[inline]
177181
fn from_u64_bits(v: u64) -> Self {
178182
f64::from_bits(v)
179183
}

library/core/src/num/dec2flt/lemire.rs

+2
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,12 @@ pub fn compute_float<F: RawFloat>(q: i64, mut w: u64) -> BiasedFp {
118118
/// This uses a pre-computed integer approximation for
119119
/// log2(10), where 217706 / 2^16 is accurate for the
120120
/// entire range of non-finite decimal exponents.
121+
#[inline]
121122
fn power(q: i32) -> i32 {
122123
(q.wrapping_mul(152_170 + 65536) >> 16) + 63
123124
}
124125

126+
#[inline]
125127
fn full_multiplication(a: u64, b: u64) -> (u64, u64) {
126128
let r = (a as u128) * (b as u128);
127129
(r as u64, (r >> 64) as u64)

library/core/src/num/dec2flt/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,13 @@ macro_rules! from_str_float_impl {
147147
/// representable floating-point number to the number represented
148148
/// by `src` (following the same rules for rounding as for the
149149
/// results of primitive operations).
150-
#[inline]
150+
// We add the `#[inline(never)]` attribute, since its content will
151+
// be filled with that of `dec2flt`, which has #[inline(always)].
152+
// Since `dec2flt` is generic, a normal inline attribute on this function
153+
// with `dec2flt` having no attributes results in heavily repeated
154+
// generation of `dec2flt`, despite the fact only a maximum of 2
155+
// possible instances can ever exist. Adding #[inline(never)] avoids this.
156+
#[inline(never)]
151157
fn from_str(src: &str) -> Result<Self, ParseFloatError> {
152158
dec2flt(src)
153159
}
@@ -202,12 +208,14 @@ impl fmt::Display for ParseFloatError {
202208
}
203209
}
204210

211+
#[inline]
205212
pub(super) fn pfe_empty() -> ParseFloatError {
206213
ParseFloatError { kind: FloatErrorKind::Empty }
207214
}
208215

209216
// Used in unit tests, keep public.
210217
// This is much better than making FloatErrorKind and ParseFloatError::kind public.
218+
#[inline]
211219
pub fn pfe_invalid() -> ParseFloatError {
212220
ParseFloatError { kind: FloatErrorKind::Invalid }
213221
}
@@ -220,6 +228,7 @@ fn biased_fp_to_float<T: RawFloat>(x: BiasedFp) -> T {
220228
}
221229

222230
/// Converts a decimal string into a floating point number.
231+
#[inline(always)] // Will be inlined into a function with `#[inline(never)]`, see above
223232
pub fn dec2flt<F: RawFloat>(s: &str) -> Result<F, ParseFloatError> {
224233
let mut s = s.as_bytes();
225234
let c = if let Some(&c) = s.first() {

library/core/src/num/dec2flt/number.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct Number {
3333

3434
impl Number {
3535
/// Detect if the float can be accurately reconstructed from native floats.
36+
#[inline]
3637
fn is_fast_path<F: RawFloat>(&self) -> bool {
3738
F::MIN_EXPONENT_FAST_PATH <= self.exponent
3839
&& self.exponent <= F::MAX_EXPONENT_DISGUISED_FAST_PATH

0 commit comments

Comments
 (0)