I'm trying to use SharedArrayBuffer
in rust. I want to accumulated output from n workers in part of sab. As i found out i'm constrained to int array types, but my data stored as floats. I'm using Atomics::add(&rc2, idx as u32, k)
where k is my float transformed into i32. I'm receiving some garbage in each of the described approaches (when im trying to read through float32 view from filled sab). What i've tried: 1) use to_ne_bytes + i32::from_be_bytes
on my float and then reassemble it as i32 2) use std::mem::transmute::<f32, i32>(v);
3) use to_bits / cast to u32 / from_bits / cast to i32 4) look up implementation in js sys github repo (here https://github.com/rustwasm/wasm-bindgen/blob/master/crates/js-sys/src/lib.rs), but there is seems to be only some bindings to external code, i dont understand where this code located so i would be able to check why Atomic add wants i32 and what it does with it. Thanks!
Asked
Active
Viewed 88 times
0

Anatoly Strashkevich
- 1,834
- 4
- 16
- 32
-
The [`Atomics.add()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/add) method in JavaScript for which this is but a binding only works for integer types. Since float values are stored and add in different way compared to ints no kind of `transmute()` trickery will help you. You need to accumulate the output without using atomics. – HHK Mar 12 '21 at 16:47
-
Thank you for your comment @HHK , what is puzzling me is there multiple kinds of views available, which are supported for atomic operations (like u8, u16) but all Atomic functions in js sys docs require i32, i thought there is some internal magic going on which will allow to perform add on arbitrary types – Anatoly Strashkevich Mar 12 '21 at 17:00