He clearly has a more modern compiler than you. The point is when gcc will decide to inline and when not to. It's obviously moot, -O3 doesn't mean squat here theres nothing to optimize.Halifax wrote:Jim e: Yes that may be right.
But also just so you know, I can compile it with -O3 and it inlines it.
Good compilers, generating code for speed, will try to inline functions like that. Trick is how are you going to use that number. Both of you printed as if it were a 1 or a 0. But its bool, it just needs to be true or false if all you want is to branch you code at that point. At which case theres a pretty good chance that gcc will realize that bit 31 is the sign bit.
Here's my example:
Code: Select all
#include <stdio.h>
int is_negative(float x) {
unsigned int *ui = (unsigned int *)&x;
return (*ui >> 31);
}
int main(int argc, char **argv) {
float i;
for(i = -10.4f; i<10;i+=1.2f) {
if (is_negative(i)) {
puts("Negative");
} else {
puts("Positive");
}
}
system("Pause");
return 0;
}
Code: Select all
.file "main.c"
.text
.p2align 4,,15
.globl _is_negative
.def _is_negative; .scl 2; .type 32; .endef
_is_negative:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
popl %ebp
shrl $31, %eax
ret
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC2:
.ascii "Negative\0"
LC3:
.ascii "Positive\0"
LC5:
.ascii "Pause\0"
.align 4
LC1:
.long 1092616192
.align 4
LC4:
.long 1067030938
.text
.p2align 4,,15
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl $16, %eax
movl %esp, %ebp
pushl %ebx
subl $20, %esp
andl $-16, %esp
call __alloca
call ___main
movl $0xc1266666, %ebx
jmp L10
.p2align 4,,7
L18:
movl $LC2, (%esp)
L15:
call _puts
movl %ebx, -8(%ebp)
flds -8(%ebp)
fadds LC4
flds LC1
fucomp %st(1)
fnstsw %ax
fstps -8(%ebp)
sahf
movl -8(%ebp), %ebx
jbe L17
L10:
testl %ebx, %ebx
js L18
movl $LC3, (%esp)
jmp L15
.p2align 4,,7
L17:
movl $LC5, (%esp)
call _system
movl -4(%ebp), %ebx
xorl %eax, %eax
leave
ret
.def _system; .scl 3; .type 32; .endef
.def _puts; .scl 3; .type 32; .endef
Like I said before, You shouldn't try to out think the compiler, chances are your just gonna F' things up and make your code slower.
But I'm still guessing, treating it as a float is faster. I'm not certain how slow it is to constantly reload the value for FPU use.