#shift u32 by 32

1 messages · Page 1 of 1 (latest)

median sparrow
#

why zig doesn't allow shifting u32 by 32?
and how can I do it efficiently (ideally the code generates a single instruction if possible)

glass bronze
#

Shifting a u32 by 32 is just clearing it, so x ^ x

fluid sluice
#

Or just set it to zero lol

#

x = 0

median sparrow
#

yeah I know but I don't want to do add a branch just to check for this condition

#

cuz CPUs do support shifting by 32 nativaly

#

I dont' want to do this:

#
if (shift == 32)
  result = 0
else
  result = data >> shift;
fluid sluice
#

LLVM will likely be good enough to optimize this out

#

I'd check compiler explorer first though

fair spruce
#

can also raise the shiftable value then lower it: x = @intCast(@as(u64, x) >> shift)

fair spruce
glass bronze
#

Ah nvm

#

I thought we were shifting up

#

In which case we would need truncate

hidden latch
#

surely ranged ints will fix this...

unreal dome
#

I dont think ranged ints should fix this, expanding an integer while bit fiddling should be explicit imo

hidden latch
#

I just meant x >> n where x: u32 allowing n to be 0-32 rather than 0-31. It comes up more often than I would have expected when I’m doing bit stuff and writing the branch feels really dumb

scarlet lantern
#

Consider raising this in the zsf zulip so that Andrew sees it, ultimately he decides.

rare prawn
hidden latch
#

wow. ok. The point about cpu design, where only the bottom 5 bits have to be checked, is maybe convincing enough, but DAMN would it be nice if the bit shift shifted bits instead of whatever the hell that is

#

TIL I guess