> 1.5 << 2 4 Javascript truncates the number to an integer, and then applies the shift. There's no reason for low-level, C mask hacks for bitshifts.
-
-
Replying to @KardOnIce @saleemrash1d and
All of JavaScript's bitwise operators first coerce the value to a signed 32 bit int, then convert it back. This makes it possible to optimize chains of these operations; most js engines even have an int32 array specialization.
2 replies 0 retweets 0 likes -
Replying to @widdr @KardOnIce and
This is how webassembly was first done; you can `|0` every value after every operation (like addition) to keep them in int32 form, and Mozilla made programs of that form optimize aggressively to show off. None of this to say JS isn't bad, mind.
1 reply 0 retweets 0 likes -
Replying to @widdr @saleemrash1d and
My point is to say: float-to-int conversions => expensive bitmask for shift => cheap logical check => not that expensive
2 replies 0 retweets 0 likes -
Replying to @KardOnIce @saleemrash1d and
float to int is a lot cheaper than you might think! like, really very cheap indeed: on the semi-recent Haswell arch, cvtsi2sd has about 1-per-clock throughput and 4-clock latency. you can nitpick exactly how expensive it is in real terms, but in the worst case it is NEGLIGIBLE.
1 reply 2 retweets 1 like -
Replying to @widdr @KardOnIce and
logical checks are usually much more expensive than this on average, as they can lead to mispredictions. as the double -> int conversion is hardware optimized it's almost guaranteed to look pretty lousy by comparison.
1 reply 0 retweets 0 likes -
-
Replying to @widdr @saleemrash1d and
It's true on newer architectures, but there's ways of making the logical check a bit cheaper. Also, ~7 latency for older architectures is about what I know.
1 reply 0 retweets 0 likes -
Replying to @KardOnIce @widdr and
Anyway, something like this magically runs faster on my PC than just the straight SHR (it has worse codegen though, so there are damned lies and then benchmarks): inline int32_t shr(int32_t x, int32_t n) { if (likely(n < 32)) { return x >> n; } else { return 0; } }
2 replies 0 retweets 0 likes -
Replying to @KardOnIce @saleemrash1d and
yeah that sounds right. I forget if JS bitshifts sign-extend, though...
2 replies 0 retweets 1 like
Depends on wether you use >> or >>>
-
-
Always fun: > -1 >>> 3 536870911
1 reply 0 retweets 1 like -
Replying to @KardOnIce @jedisct1 and
oh man i forgot js has java-style bitshift ops
0 replies 0 retweets 1 like
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.