Code: Select all
bool is_negative(float x) {
unsigned int *ui = (unsigned int *)&x;
return (*ui >> 31);
}
Is it faster than "return (x < 0.0f);" anyway?
Moderator: MaxCoderz Staff
Code: Select all
bool is_negative(float x) {
unsigned int *ui = (unsigned int *)&x;
return (*ui >> 31);
}
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) {
printf("%d\n", is_negative(strtod(argv[1], NULL)));
return 0;
}
Code: Select all
.file "main.c"
.text
.p2align 4,,15
.globl is_negative
.type is_negative, @function
is_negative:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
popl %ebp
shrl $31, %eax
ret
.size is_negative, .-is_negative
.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 $36, %esp
movl 4(%ecx), %eax
movl $0, 4(%esp)
movl 4(%eax), %eax
movl %eax, (%esp)
call strtod
pushl %eax
movl -8(%ebp), %eax
fildl (%esp)
addl $4, %esp
fstps -8(%ebp)
movl $.LC0, (%esp)
shrl $31, %eax
movl %eax, 4(%esp)
call printf
addl $36, %esp
xorl %eax, %eax
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
.section .note.GNU-stack,"",@progbits
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) {
printf("%d\n", is_negative(strtod(argv[1], NULL)));
return 0;
}
Code: Select all
.text
.globl _is_negative
.def _is_negative; .scl 2; .type 32; .endef
_is_negative:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
leal 8(%ebp), %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
movl (%eax), %eax
shrl $31, %eax
leave
ret
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "%d\12\0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
call __alloca
call ___main
movl $0, 4(%esp)
movl 12(%ebp), %eax
addl $4, %eax
movl (%eax), %eax
movl %eax, (%esp)
call _strtod
pushl %eax
fildl (%esp)
leal 4(%esp), %esp
fstps (%esp)
call _is_negative
movl %eax, 4(%esp)
movl $LC0, (%esp)
call _printf
movl $0, %eax
leave
ret
.def _strtod; .scl 3; .type 32; .endef
.def _printf; .scl 3; .type 32; .endef
Halifax wrote:Assembler compiled with 'gcc' (3.4.2)
At any rate thats a bad example. If your using floats, you need to have some real float math going on. Goplat is probably right, even if it is inlined if its in the FPU its probably faster to do the comparison. You'd just be guessing where that number is stored at the moment.coelurus wrote:Code: Select all
.ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
Jim e: Yes that may be right.Jim e wrote:Halifax wrote:Assembler compiled with 'gcc' (3.4.2)At any rate thats a bad example. If your using floats, you need to have some real float math going on. Goplat is probably right, even if it is inlined if its in the FPU its probably faster to do the comparison. You'd just be guessing where that number is stored at the moment.coelurus wrote:Code: Select all
.ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"