smallint usage guidelines

Bernhard Fischer rep.dot.nop at gmail.com
Tue Apr 10 18:48:31 UTC 2007


On Sun, Apr 08, 2007 at 06:01:18PM +0200, Denis Vlasenko wrote:
>Hi Bernhart,
>
>       smalluint i = index_in_str_array(params, name) + 1;
>       if (i == 0)
>               return 0;
>       if (!(i == 4 || i == 5))
>               i |= 0x80;
>
>       return i;
>
>I think that this optimization is wrong.
>index_in_str_array returns int. At best, compiler will use it as-is.
>At worst, compiler will try to make sure that it is properly casted into 
>byte,
>which probably results in "n = n & 0xff" on many architectures.
>
>You save nothing on space here because i is not stored on-stack,
>gcc will keep it in register. And even it is *is* stored,
>it is *stack* storage, which is cheap (unlike data/bss).

Let me point you at ip.c, r18387 or later.
There, using a smallint (or that smalluint hack for that matter)
generates smaller code (for me) than an int.

We should rather fix index_in_{sub,}str_array to return an unsigned, i.e.
0 on failure then, no?
>
>small[u]ints are useful _mostly_ for:
>(a) flag variables
>   (a1) global flag variables - make data/bss smaller
>   (a2) local flag variables - "a = 5", "a |= 0x40" are smaller
>        for bytes than for full integers.
>           Example:
>           on i386, there is no widening constant store instruction
>           for some types of address modes, thus
>           movl $0x0,(%eax) is "c7 00 00 00 00 00"
>           movb $0x0,(%eax) is "c6 00 00"
>(b) small integer structure members, when you have many such
>structures allocated,
>   or when these are global objects of this structure type
>
>small[u]ints are *NOT* useful for:
>(a) function parameters and return values -
>   they are pushed on-stack or stored in registers, bytes here are *harder*
>   to deal with than ints

Is there a nice tool that estimates the pure cycles usage of insn (or
patterns thereof)?

I have this one which operates at runtime only:
http://busybox.net/lists/busybox/2005-September/016309.html
http://www.busybox.net/lists/busybox/2006-September/024612.html
(BTW, someone on IRC ment that it was useful, what do you think?)

>(b) "computational" variables - "a++", "a = b*3 + 7" may take more code to 
>do
>   on bytes than on ints on some architectires.
>
>Comments?

Not really, except that i just use i386 myself, which is an
inappropriate comment AFAIC.



More information about the busybox mailing list