I’m working on an embedded project, and in searching for some help in manipulating 24-bit audio data, came across this SO page with this answer:
Working with anything smaller than an integer (32 or 64 bit depending on your architecture) is not ideal. All CPU operations of the smaller data types (short, etc) are done using integer arithmetic. Conversion to and from the CPU has to be done, slowing your application down (even if it is just a tad).
Is this true for most compilers? I’m compiling for esp32, and I had figured I should use small datatypes when I can to save memory; i.e., using a uint8_t when iterating 0 to 10. The esp32 has quite a lot of memory for embedded (for my uses), so would it actually be faster to just use int, even for small numbers? Or is there a way I can find out?
Well, it does depend on on the architecture, as stated. On x86 the CPU merely uses the instructions appropriate for the data size and uses only parts of the registers it needs. That’s not really some sort of hackerry or inefficiency, it’s by design even.
When the move to 64bit happened we just got a register size doubling and new instructions that operate on those extended sizes. All that was added and CPU’s are still backwards compatible with 32bit binaries because of that (there’s more to it though), because all those old instructions still exist. So it’s inefficient in a way that you’re not using the full width of the registers, but that’s about it.
Definitely will be different esp, you should check. You can always do a couple of versions of your code and disassemble.
I believe it depends on the particular CPU that you use.
It is true that the CPU is going to do math in its native bit size. I am fairly certain that a x86_64 CPU will do 64 bit math to add two 8 bit values together. It’s more work to make an exception than to just run the full 64-bit addition with zeroes.
Tiny CPUs have some weird things. Some of them may have to load a 32-bit word and do an AND mask + shift in order to get a single 8 bit value. Others can load 8 bits but then need an extra instruction to sign-extend it to the full 32 bit register size.
What I would do is examine your compiler’s optimized machine code to see if this is actually a problem for your code. If it is then determine if it is more important to save bytes in size or a few cycles in CPU.