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

Float round and roundf are slow #154

@raphlinus

Description

@raphlinus

This bug is branched from rust-lang/rust#55107.

The code for round (adapted from musl libm) is correct (I verified it for all f32 values) but slow, containing many branches.

The following code is also correct and should be much faster (this is the f32 version, which is the main thing I'm testing now, since I'm looking at simd). This is the positive branch.

    ((x + (0.25 - 0.5 * f32::EPSILON)) + (0.25 + 0.5 * f32::EPSILON)).floor()

There are two ways to make this work for signed values: one is to branch on the sign of the argument, the other is to do copysign(... x.abs() ..., x). The copysign intrinsic is not currently exposed in the Rust std lib though, for which I'll file an issue. One of the nice things about copysign is that there's a wasm intrinsic. That should get this function down to around 9 asm instructions from the current ~92.

It would also be really nice to expose the nearest wasm instruction as well; the problem is that there's no llvm intrinsic with the exactly correct semantics. I'm going to be exploring that as well.

Activity

tgross35

tgross35 commented on Feb 5, 2025

@tgross35
Contributor

Looks like this was done in #253, this was just never closed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @raphlinus@tgross35

        Issue actions

          Float round and roundf are slow · Issue #154 · rust-lang/libm