Remix.run Logo
losvedir a day ago

This is an interesting article. I don't really work with low level enough languages for this to matter (unless - does this ever show up in Javascript somehow?).

I guess I don't understand the "reduce" step. It seems like you have to be careful not to "undo" all the benefit from SIMD. Sure, it can compare 8 values in parallel, but then if you have to look at each of the 8 answers in turn you're back to where you began. Is the `@reduce()` function in the example a special Vector one that tells you if all the values are true or not in one "step"?

exDM69 a day ago | parent | next [-]

> Is the `@reduce()` function in the example a special...

Yes, it is. In SIMD you can do a reduce for a commutative function in O(log N) steps. Sum, product, min, max, all, any, etc.

Although in this case it might be better if you just take the mask (vector of booleans), convert it to a bitmask and check if it is (non-) zero.

If the language provides it, you might also use .all() or .any() for a mask.

wrs a day ago | parent | prev | next [-]

Yes. "`@reduce(.And, ...)` combines every boolean using `and` and returns a single boolean."

If it's true (the common case here) then you proceed to look at the next 8 bytes.

If it's false, you apply a @bitcast (turn the booleans into bits) and @ctz (find the first 0) to get the index of where it was false.

tapirl a day ago | parent [-]

Maybe I'm wrong, but should it be @clz instead?

wrs a day ago | parent [-]

No, the diagram looks like a binary literal but it's actually backwards from that. Lane 0 becomes the LSB, but that's on the left of the diagram.

tapirl a day ago | parent [-]

Aha, you are right. Smaller indexes are for least-significant bits.

saagarjha a day ago | parent | prev [-]

It can show up in JavaScript if you write your code in a way that lets the browser engine lower it to SIMD under the hood

mb2100 a day ago | parent [-]

how do I do that? :-)