[Numpy-discussion] Changeset 6557
Charles R Harris
charlesr.harris@gmail....
Sat Mar 7 13:34:18 CST 2009
On Sat, Mar 7, 2009 at 11:57 AM, David Cournapeau <cournape@gmail.com>wrote:
> On Sun, Mar 8, 2009 at 3:20 AM, Charles R Harris
> <charlesr.harris@gmail.com> wrote:
>
> >
> > The macro is ugly, unneeded, and obfuscating. Why construct a number from
> > characters and shifts when you can just *write it down*?
>
> The idea was to replace the 'ABCD' multi-byte constant. If you think
> that writing down the corresponding integer is cleaner, so be it - I
> don't care either way. I am not sure I see a difference between 'A' <<
> 24 .... and 103...., though.
>
>
> > True, it is initialized here:
> >
> > movl $16909060, -8(%ebp)
> >
>
> The generated assembly is exactly the same wether the constant is
> initialized through the macro or the integer (the actual integer is in
> the assembly). But in the following case:
>
> const union {
> npy_uint32 i;
> char c[4];
> } bint = {some constant};
>
> switch (bint.c[0]) {
> case 'A':
> etc....
> }
>
> The compiler did not remove the conditionals corresponding to the
> switch - const or not.
>
I got curious to see just how it would all go together. Here is the C:
#include <stdio.h>
enum {little_endian, big_endian, unknown};
static int order(void)
{
const union {
int i;
char c[sizeof(int)];
} test = {0x01020304};
if (test.c[0] == 1) {
return big_endian;
}
else if (test.c[0] == 4) {
return little_endian;
}
else {
return unknown;
}
}
int main(int argc, char **argv)
{
printf("%d\n", order());
return 0;
}
And here is the gcc -S -O2 compiled assembly:
.file "order.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%d\n"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $20, %esp
movl $0, 4(%esp) <<<<<<<<<
movl $.LC0, (%esp)
call printf
addl $20, %esp
xorl %eax, %eax
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.3.0 20080428 (Red Hat 4.3.0-8)"
.section .note.GNU-stack,"",@progbits
The order function has been inlined and the return value, 0, is loaded for
printing at the marked line. That line is the only place where anything
remains of the order function. The compiler knows the return value and puts
it on the stack for the printf call, nothing is computed at run time.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090307/e7bd4783/attachment-0001.html
More information about the Numpy-discussion
mailing list