r/ProgrammerHumor • • 11d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

Show parent comment

-5

u/yjlom 11d ago

Bits are addressable. Not in a single instruction on most modern processors, which is mainly because of the C and C++ standard, and not without affecting surrounding bits (but then that bytes are threadsafely addressable is mostly done to accomodate the C and C++ standard), but they are addressable and not any noticeably slower than bytes at that.

If hardware didn't have to contort to fit C, we'd probably have bit-precise addressing and 512bit-precise threadsafe addressing.

7

u/unknown_alt_acc 11d ago

No, bits plainly aren’t addressable outside of maybe some obscure or specialized architectures. You can manipulate individual bits just fine, but they don’t have their own address in memory space.

And while I’m no computer engineer, I’m curious why you think that ISAs would move towards individually addressable bits when by your own admission you can get the same effect at minimal runtime cost through bitwise operations. Bit-level addressing sounds like it would just complicate a processor’s circuitry for a very marginal benefit. And especially when you often want to align your data to a 4 or 8 byte boundaries anyway to benefit from vectorization, bit-level addressing seems like it runs counter to the general trends in processor design.

1

u/yjlom 11d ago

  I’m curious why you think that ISAs would move towards individually addressable bits when by your own admission you can get the same effect at minimal runtime cost through bitwise operations. 

For example, modern x86 typically loads and writes 512 bits at a time and byte-level addressing is already (cheaply) achieved through shift-and-mask. The illusion that concurrent writes to adjacent bytes are independent on the other hand is a major source of complexity for memory controllers.

You get to use the same vector instructions for multiple objectif of smaller sizes: for free for bitwise ops, with 1 bit of padding for addition and subtraction, with 100% padding for shifts. If your type fits within 3 bits and you don't need to multiply it, you gain a lot by aligning it to 4 bits instead of 32; if it's booleans, you can perform 512 logical operations in per cycle instead of 64 by keeping them to a single bit. Even if you need multiplication, 2 clobbered registers, a few rots, and a few ands may come out cheaper than the loads you save, though I haven't measured it and I'm guessing ymmv on that.

And yes, bits are addressable on pretty much all mainstream architectures, in software, as long as you spice up your pointers a little.

1

u/unknown_alt_acc 10d ago

And yes, bits are addressable on pretty much all mainstream architectures, in software, as long as you spice up your pointers a little.

It sounds like you're abusing the word "addressable" to mean that you can simulate bit-level addressing with bitwise operations rather than ISAs featuring it at the hardware level.

The illusion that concurrent writes to adjacent bytes are independent on the other hand is a major source of complexity for memory controllers.

Having 8x more writable addresses per cache line sounds like it would make synchronization more difficult, not less.

You get to use the same vector instructions for multiple objectif of smaller sizes: for free for bitwise ops, with 1 bit of padding for addition and subtraction, with 100% padding for shifts. If your type fits within 3 bits and you don't need to multiply it, you gain a lot by aligning it to 4 bits instead of 32

That either adds a lot of complexity by necessitating a whole lot of instructions for weird data sizes, or destroys performance by doing everything bit-by-bit.

if it's booleans, you can perform 512 logical operations in per cycle instead of 64 by keeping them to a single bit

Modern architectures already include a plethora of instructions for this. Bit-level addressing, again, seems like an unnecessary complication to add into the mix for most cases.