Bit reversal

Should we add a method to reverse the bit-order in a FixedWidthInteger?

1 Like

don't all the integer methods correspond to common machine instructions? && i don’t remember there being an x86 instruction for this

It'd be a pretty rarely used facility and indeed there isn't an instruction for this, so there's no benefit to having a standard library method. When I run into something like this, I tend to look to:

https://graphics.stanford.edu/~seander/bithacks.html

Wonderful resource.

3 Likes

Looking at "Reversing Bits in C," although x86 (i.e. Macs) doesn't have it, ARM (i.e. iDevices) does, so there's a case if you insist on Integer methods needing a corresponding intrinsic.

I need it for saving bits in (bit-level) big endian, while allowing the initializer take a Sequence of words in either big- or little-endian. I think indexing for Fast Fourier Transforms and tree traversal also use bit reversals.

Have you seen Foundation's byte order utilities? It is true that that API is not the most ergonomic one to use from Swift, but an evolution of such API maybe belongs to Foundation.

1 Like

That's at the byte-level for multi-byte objects, with the bits within a byte being a black box. I'm talking about the bit-level.

1 Like

Oh I see what you mean, sorry for the confusion! I give you my +1 on this, giving that there is an instruction for doing this on ARM and on non-ARM it's not trivial either.