I tried to calculate hashes for constant C-strings in compile-time using macros. That is my example code:
#include <stddef.h>
#include <stdint.h>
typedef uint32_t hash_t;
#define hash_cstr(s) ({ \
typeof(sizeof(s)) i = 0; \
hash_t h = 5381; \
for (; i < sizeof(s) - 1; ) \
h = h * 33 + s[i++]; \
h; \
})
/* tests */
#include <stdio.h>
int main() {
#define test(s) printf("The djb2 hash of " #s " is a %u\n", hash_cstr(#s))
test(POST);
test(/path/to/file);
test(Content-Length);
}
Now I run GCC to show listing:
arm-none-eabi-gcc-4.8 -S -O2 -funroll-loops -o hash_test.S hash_test.c
And the result is as expected: all strings was eliminated and replaced by its hashes. But generally I use -Os to compile code of embedded apps. When I try to do it, I have hashes only for strings with less than four characters. I also tried to set parameter max-unroll-times
and use GCC 4.9:
arm-none-eabi-gcc-4.9 -S -Os -funroll-loops \
--param max-unroll-times=128 -o hash_test.S hash_test.c
I can't understand the reason of that behavior and how I can extend this restriction of four chars.