You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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
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.
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. 🤷
Activity
lrhn commentedon Aug 4, 2025
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.99999994fand8388609.0fryanheise commentedon Aug 5, 2025
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.
Do you mean WASM? I don't think this would be a JS thing, although I think WASM supports round to nearest.
lrhn commentedon Aug 6, 2025
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.
ryanheise commentedon Aug 6, 2025
I would personally be fine with this approximation on ARMv7, and
vcvtnq_s32_f32on ARMv8 (which directly does rounding), since I'm not expecting all architectures to offer the same rounding modes exactly.I'm ignorant there, but I wouldn't have expected "any" of the methods in
Float32x4to actually be vector operations under the hood on JS. 🤷