Skip to content

Add Float32x4.round() #61240

Open
Open

Description

@ryanheise

I would like to request Float32x4.round() to be added. This would offer cross-platform rounding without being able to control the rounding mode (i.e. you get whatever the platform supports).

You could use _mm_cvtps_epi32 and vcvtq_s32_f32 (add 0.5).

Activity

  1. lrhn commented on Aug 4, 2025

    @lrhn
    Member

    Is there a way to implement this efficiently in JS?

    For the record, adding 0.5 and rounding down is not the same as rounding to nearest.
    There are 32-bit floats where it gives the wrong result, like 0.99999994f and 8388609.0f

  2. ryanheise commented on Aug 5, 2025

    @ryanheise
    Author

    There are 32-bit floats where it gives the wrong result, like 0.99999994f and 8388609.0f

    Doesn't 0.99999994f work? Seems to at least be applicable for floats less than 8388609.0f. There's potentially also vcvtnq_s32_f32 if available.

    Is there a way to implement this efficiently in JS?

    Do you mean WASM? I don't think this would be a JS thing, although I think WASM supports round to nearest.

  3. added
    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
    on Aug 5, 2025
  4. lrhn commented on Aug 6, 2025

    @lrhn
    Member

    I mean JS. If we add this to dart:typed_data, I would want it to work when compiled to JS too. The library is available, and it's sad if it doesn't actually work.

    Or maybe we can say it doesn't work, like we do for Int64List.

    Good catch on the bad-rounding number. It is actually 0.49999997f, ~half of the one I wrote. If you add 0.5f to that, it becomes 1.0f, which doesn't round down to zero, even if the original started out below 0.5.

  5. ryanheise commented on Aug 6, 2025

    @ryanheise
    Author

    I would personally be fine with this approximation on ARMv7, and vcvtnq_s32_f32 on ARMv8 (which directly does rounding), since I'm not expecting all architectures to offer the same rounding modes exactly.

    I mean JS. If we add this to dart:typed_data, I would want it to work when compiled to JS too. The library is available, and it's sad if it doesn't actually work.

    Or maybe we can say it doesn't work, like we do for Int64List.

    I'm ignorant there, but I wouldn't have expected "any" of the methods in Float32x4 to actually be vector operations under the hood on JS. 🤷

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

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-corelibrary-typed-datatype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions