Compile 16-bit literals for ESP32S3

jeff.cav.edu
Posts: 1
Joined: Sun Sep 14, 2025 6:06 pm

Compile 16-bit literals for ESP32S3

Postby jeff.cav.edu » Sun Sep 14, 2025 6:55 pm

Hi, I have a function which implements a decision tree with if-else statements involving unsigned 16-bit variables and constants.
tree.png
tree.png (50.23 KiB) Viewed 358 times
However, when I disassemble the compiled file, each 16-bit constant seems to occupy 32 bits in ROM.
literals.png
literals.png (32.65 KiB) Viewed 358 times
Is my understanding correct (spending 32 bits for 16-bit constants)?
Is there a way to use only 16 bits for each literal to save space?


Thanks in advance.

Sprite
Espressif staff
Espressif staff
Posts: 10650
Joined: Thu Nov 26, 2015 4:08 am

Re: Compile 16-bit literals for ESP32S3

Postby Sprite » Mon Sep 15, 2025 1:00 am

That's probably an optimization for space and speed as iirc Xtensa doesn't have an instruction to load a literal smaller than 32 bit, so it'd have to string multiple instructions together to do the same thing if you saved them as actual 16-bit values. (And note that that'd mean the savings in data space would be smaller than the extra space the added instructions would take up.)

If you want to do this anyway, you could create a const array of 16-bit values and use the indices of that, rather than literals.

Who is online

Users browsing this forum: Amazon [Bot], Baidu [Spider], Bing [Bot], Qwantbot and 3 guests