I've been playing around with x87 FPU programming,and I just came across the following magic spell for converting from float (32-bit) to int (32-bit):
flds from # number to convert from
fadds magic1 # add magic number 1 (???)
fstps to # store in destination as single-precision binary32
movl to,%eax # load float result as an int
subl $magic2,%eax # subtract magic number 2 (???)
sarl $1,%eax # divide by 2
movl %eax,to # and look, here's the result!
.section .rodata
magic1: .float 6291456.0 # 3 * 2^21
.equ magic2, 0x4ac00000 # ???
.data
from: .float 31415.9265 # pick a number, any number...
to: .long 0 # result ends up here
(AT&T syntax with GAS directives)
I have tried this out, and it seems to work (rounding towards -infinity) but I have absolutely no idea why! Can anyone explain how it works?