Author Topic: Does uint16_t in 32bit ARM saves memory?  (Read 2837 times)

0 Members and 1 Guest are viewing this topic.

Offline TNbTopic starter

  • Regular Contributor
  • *
  • Posts: 106
  • Country: fi
Does uint16_t in 32bit ARM saves memory?
« on: September 29, 2016, 07:41:53 pm »
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.
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: Does uint16_t in 32bit ARM saves memory?
« Reply #1 on: September 29, 2016, 07:55:33 pm »
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.
 
The following users thanked this post: rx8pilot

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5319
  • Country: gb
Re: Does uint16_t in 32bit ARM saves memory?
« Reply #2 on: September 29, 2016, 08:05:52 pm »
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.

Depending 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.
 

Offline TNbTopic starter

  • Regular Contributor
  • *
  • Posts: 106
  • Country: fi
Re: Does uint16_t in 32bit ARM saves memory?
« Reply #3 on: September 29, 2016, 08:20:19 pm »
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.
But 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.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5319
  • Country: gb
Re: Does uint16_t in 32bit ARM saves memory?
« Reply #4 on: September 29, 2016, 08:35:55 pm »
Adresses in ARM are byte-granular.
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4422
  • Country: dk
Re: Does uint16_t in 32bit ARM saves memory?
« Reply #5 on: September 29, 2016, 08:40:28 pm »
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.
But 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.

it 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 use

but 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







 

Offline TNbTopic starter

  • Regular Contributor
  • *
  • Posts: 106
  • Country: fi
Re: Does uint16_t in 32bit ARM saves memory?
« Reply #6 on: September 29, 2016, 08:54:04 pm »
OK, thank you all guys, now it is less of a mystery for me :)
 

Offline Kleinstein

  • Super Contributor
  • ***
  • Posts: 14192
  • Country: de
Re: Does uint16_t in 32bit ARM saves memory?
« Reply #7 on: September 30, 2016, 07:00:00 am »
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).
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Does uint16_t in 32bit ARM saves memory?
« Reply #8 on: September 30, 2016, 07:19:12 am »
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 using SIMD.
With 32 bits however, you'll also need more flash for the literals in operations.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf