Does uint16_t in 32bit ARM saves memory? - Page 1
Forum section: electronics / beginners

author: TNb
author_rank: Regular Contributor
timestamp: September 29, 2016, 07:41:53 pm
content: If I would declare variable to be uint16_t when using 32bit ARM microcontroller, would it actually mean anything? I am asking because as far as I know it would be stored in 32bit register anyway, so I don't save memory this way, isn't it? Correct me if I'm wrong.

author: AndyC_772
author_rank: Super Contributor
timestamp: September 29, 2016, 07:55:33 pm
content: You'll probably save memory. Even though the RAM is 32 bits wide, it can be accessed with a granularity of 1 byte, so you can store two uint16_t's in a single word.The effect is most obvious if you create not just a single word, but an array. Create an array of 1024 x uint16_t and it'll use up 2k of RAM. If you use uint32_t, it'll require 4k.

author: Howardlong
author_rank: Super Contributor
timestamp: September 29, 2016, 08:05:52 pm
content: Quote from: TNb on September 29, 2016, 07:41:53 pmDepending on the compiler, its switches, and any #pragmas in the code, it will save space in certain circumstances, and particularly in arrays and structures where they are often packed. Be aware that there can be performance impact though if accessing a variable across its natural byte or half-word boundary. The same goes for Thumb2 code, where a word instruction fetch straddling a word boundary can significantly impact performance.Maybe I'm old school, but unless there's a reason to use a larger sized variable than necessary, I don't, particularly in embedded systems.

author: TNb
author_rank: Regular Contributor
timestamp: September 29, 2016, 08:20:19 pm
content: Quote from: AndyC_772 on September 29, 2016, 07:55:33 pmBut how uC knows that? Suppose I have 16bit value, uC accesses it by the address, which points to 32bit register. So now uC has to read whole 32 bit value. I suppose that there is some mechanism that tell it to stop after 16 bit, but that instruction should be stored somewhere also, i.e. consume memory. Even weirder if the 16bit value is stored at the second half of 32bit register - now uC goes to the memory address and it needs to know beforehand that it has to skip first 16bits and start after that. Of course it can't know it just like that, some instruction has to be stored(and take some bits) to tell uC to do it(right?).So how does it work without making a mess inside uC? I tried to google some stuff and search on EE.SE, but couldn't find explanation.

author: Howardlong
author_rank: Super Contributor
timestamp: September 29, 2016, 08:35:55 pm
content: Adresses in ARM are byte-granular.

author: langwadt
author_rank: Super Contributor
timestamp: September 29, 2016, 08:40:28 pm
content: Quote from: TNb on September 29, 2016, 08:20:19 pmit depends on the cpu, usually a 32bit cpu will still use addresses that point to bytes and have instructions that can load or store, 8bit, 16bit or 32 bit so it is part the instruction and the address what bits to usebut you can have a cpu that doesn't have such instructions, so the compiler would have to generate a string of instructions that mask shift and possibly sign extends when you access anything but 32 bit

author: TNb
author_rank: Regular Contributor
timestamp: September 29, 2016, 08:54:04 pm
content: OK, thank you all guys, now it is less of a mystery for me

author: Kleinstein
author_rank: Super Contributor
timestamp: September 30, 2016, 07:00:00 am
content: As noted above it may save some RAM space, especially in arrays. However it usually needs extra code, so one will very likely need extra code space (flash).

author: Jeroen3
author_rank: Super Contributor
timestamp: September 30, 2016, 07:19:12 am
content: Operating on 16 bits shouldn't use more instructions. It uses less sram, which is good. But registers are limited to one operand, except when usingSIMD.With 32 bits however, you'll also need more flash for the literals in operations.