fld number1 fmul number2 frndint fstp number3
Yup, just set the bit 10 and bit 11 of the control word to zero, then FPU will round number to the nearest, or equidistance..datacword dw 0.code fstcw cword ; Get FPU control word fwait mov ax, cword ; Store it to AX and ax, 0fffff9ffh ; Clear bit 10 and bit 11 push eax fldcw [sp] ; Store modified control word ' do your rounding here.. Regards,-chris
fstcw cword fwait mov ax, cword and ax, 0fffff9ffh or ax, 0C00h push eax fldcw [sp] ; this line alwasy error fld float_buffer frndint fstp float_buffer ;restore some stuff fldcw cword pop eax
Sorry, that was my mistake. I'm using MASM version 8.0 which can handle 16-bit/32-bit register correction, while MASM version 6.14 (comes from MASM32v9) can't. In general, FLDCW instruction is expecting a DWORD pointer to a 16-bit data, so a more correct one is using ESP instead: fldcw [esp] ; ESP = 32-bit pointer to the stackor try to reuse cword variable: fstcw cword fwait mov ax, cword and ax, 0fffff9ffh or ax, 0C00h mov cword, ax ; cword = changed control word fldcw [cword] ; Update control wordRegards,-chris
modchip:I'm having a little problem again... Using the code we currently have, the result is always floored. Hmmmm... Is this what it should really do?Input: 5.96Output: 5Input: 5.2Output: 5Anyways, I'll try to read more on Ramond's site. Later!Regards,m0ds
00 - Means, it round floating point to nearest whole number. I'm not sure on how to make it equidistant, because I can't make it work that way, like 5.5 should remain 5.5 etc.01 - The rounded number is floor value., 5.9=510 - The rounded number is ceiling value., 5.2=611 - Truncates based on precisionWell, this takes a lot of trial and error, I haven't done much about it, because I prefer using Streaming SIMD Extension (SSE/SSE2 or SSE3) instructions over FPU. In FPU, you have to play with IC (Infinity Control) and PC (Precision Control), together with RC (Rounding Control) to be able to make rounding work. The initial settings of these controls are mostly for basic arithmetic.If you have much time, I suggest start learning Streaming SIMD Extension, since Windows Vista is now blocking any call to FPU: http://www.intel.com/support/processors/sb/cs-001650.htmYou can search the Intel website for more documents about SSE, or if you're an AMD user, try working with 3DNow, documents available in their respective sites.I also recomend this sites:http://www.cpuid.com/ (For MMX/3DNow/SSE/SSE2/SSE3 instruction sets)http://www.sandpile.org/ (For general CPU layout and design)Cheers,-chris
Whew, after 1 year, 2 months and 13 days, I finally got the solution for this. BTW, the solution is in my blog. Check it out here: http://devpinoy.org/blogs/modchip/archive/2007/08/13/round-n-round-we-go.aspx
Thanks!