Skip to content

Use a macro to reduce duplication in core/src/num/f{16,32,64,128}.rs (part 1) - #163410

Open
beetrees wants to merge 2 commits into
rust-lang:mainfrom
beetrees:float-macro-1
Open

beetrees wants to merge 2 commits into
rust-lang:mainfrom
beetrees:float-macro-1

Conversation

@beetrees

@beetrees beetrees commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Now there are 4 primitive floating point types, the code for each method/constant is duplicated 4 times, once on each type. This PR begins reducing the duplication by moving the code in impl f{16,32,64,128} in core into a float_impl! macro, similar to the existing int_impl! and uint_impl! macros (which are used for 6 primitive types each). To reduce merge conflicts and make this easier to review, I'm going to move the items in chunks: this PR just moves the associated constants.

The PR is split into two commits, with the first commit having the macro duplicated in all 4 individual float files so that the diff can compare the macro with the original implementation.

@beetrees beetrees added the A-floating-point Area: Floating point numbers and arithmetic label Sep 27, 2026
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 27, 2026
@rustbot

rustbot commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

r? @Darksonn

rustbot has assigned @Darksonn.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from Darksonn, JohnTitor, Mark-Simulacrum, clarfonthey, jhpratt

@beetrees beetrees changed the title Use a macro to reduce duplication in `core/src/num/f{16,32,64,128}.rs (part 1) Use a macro to reduce duplication in core/src/num/f{16,32,64,128}.rs (part 1) Sep 27, 2026

@bushrat011899 bushrat011899 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good idea, but I have a suggestion which might make the diff easier to review, and possibly be neater too.

View changes since this review

Comment thread library/core/src/num/float_macros.rs Outdated
@@ -0,0 +1,326 @@
macro_rules! float_impl {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's inconsistent with how the integer macros are defined, but I think it makes more sense to use a declarative macro since there's exactly one rule. For one thing, you won't need to #[macro_use] this module and can instead just import it by name. Plus less whitespace, which is nice.

Something like:

pub macro float_impl(
    Self = $SelfT:ty,
    // ...
) {
    /// The radix or base of the internal representation of
    #[doc = concat!("`", stringify!($SelfT), "`.")]
    #[$assoc_int_consts]
    pub const RADIX: u32 = 2;

    // ...
}

Ideally all 3 would be declarative macros (or use num-traits cough), but I think this would be a good place to start? You could even split this commit into two stages:

  1. Replace an impl fN { ... } with a pub macro fN_impl(...) { ... } in-place. Since indentation matches the diff should be very clean.
  2. Merge the various fN_impl macros into the single float_impl macro.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've switched to using a declarative macro, and split out a first commit which has the macro duplicated in all the float files so that the diff can easily be compared.

…` (part 1)

The macro is duplicated in each file to allow the diff to compare changes with the previous implementation.
@Darksonn

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned JohnTitor and unassigned Darksonn Sep 28, 2026
@tgross35

tgross35 commented Oct 1, 2026

Copy link
Copy Markdown
Member

I've been iffy about doing this because it comes with quite a few downsides. But one idea I thought was nice was to use macro_attr instead:

macro_rules! float_method {
    attr(to_bits, $fty:ty, bits_12p5 = $onebits: ) ($func:item) => {
        /// Raw transmutation to u32.
        /// ...
        /// ```
        /// assert_eq!((12.5f32).to_bits(), $bits_12p5); // bit of pseudocode, needs concat
        /// ```
        #[must_use = "..."]
        $func
    };
    attr(sqrt, $fty:ty) ($func:item) => {
        /// Returns the square root of a number.
        /// 
        /// Returns NaN if self is a negative number other than -0.0.
        // ...
    };
}

impl f32 {
    #[float_method(to_bits, f32, bits_12p5 = 0x41480000)]
    #[stable(feature = "float_bits_conv", since = "1.20.0")]
    fn to_bits(...) { ... }

    #[float_method(sqrt, f32)]
    fn sqrt(...) { ... }
}

impl f64 {
    #[float_method(to_bits, f64, bits_12p5 = 0x4029000000000000)]
    #[stable(feature = "float_bits_conv", since = "1.20.0")]
    fn to_bits(...) { ... }

    #[float_method(sqrt, f64)]
    fn sqrt(...) { ... }
}

That way we can reuse the docs but the code stays much more readable. E.g. the rustdoc "source" links still point to the right place, we don't need to mess with attributes quite as much, and it's fine when an implementation occasionally needs to deviate.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-floating-point Area: Floating point numbers and arithmetic S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants