News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Which Code Is Faster?

Started by cman, November 01, 2010, 11:07:05 PM

Previous topic - Next topic

cman

I'm not too familiar with the fpu , but is it better to perform an entire calculation that has both integer operations and floating point operations in the fpu or only use the fpu for the floating point calculations? I have two code fragments and don't know which is faster:



;################### Code option 1

;get the screen width
invoke GetSystemMetrics , SM_CXSCREEN
mov screenWidth , eax
   
;get the screen height
invoke GetSystemMetrics , SM_CYSCREEN
mov screenHeight , eax

;load fpu with 15% and screen height
;st(0) = screenHeight , st(1) = screenHeight
fld fifteenPercent
fld screenHeight
   
;st ( 0 ) = st ( 0 ) * st ( 1 )
;st = .15 * screenHeight
fmul st , st ( 1 )
   
;controlPanel = .15 * screenHeight
fstp controlPanelWidth

;st ( 0 ) = seventyFivePercent , st ( 1 ) = screenHeight
fld screenHeight
fld seventyFivePercent
   
;st ( 0 ) = st ( 0 ) * st ( 1 )
;st = screenHeight * .75
fmul st , st ( 1 )
   
   
;windowHeight = .75 * screenHeight
fstp windowHeight
fwait
   
;move the control panel width to eax
mov eax , controlPanelWidth
;windowHeight = windowHeight + controlPanelWidth
add windowHeight ,eax
   
;################## Code option 2

;get the screen width
invoke GetSystemMetrics , SM_CXSCREEN
mov screenWidth , eax
   
;get the screen height
invoke GetSystemMetrics , SM_CYSCREEN
mov screenHeight , eax

;load fpu with 15% and screen height
;st(0) = screenHeight , st(1) = screenHeight
fld fifteenPercent
fld screenHeight
   
;st ( 0 ) = st ( 0 ) * st ( 1 )
;st = .15 * screenHeight
fmul st , st ( 1 )
   
;st ( 0 ) = seventyFivePercent , st ( 1 ) = screenHeight
fld screenHeight
fld seventyFivePercent
   
;st ( 0 ) = st ( 0 ) * st ( 1 )
fmul st , st ( 1 )
fadd st , st ( 2 )
   
;windowHeight = .75 * screenHeight + controlPanel
fstp windowHeight

dedndave

well - the accuracy of these values isn't critical - you want to round to the nearest integer
it may be simpler not to use the FPU at all
that is because you are starting with integers, and want to end up with integer results
for example, to get 75% of a value, multiply it by 3, then divide it by 4 (using SHR)

if you use the FPU, you must convert types with the result - that slows you down   :bg

Farabi

fmul about 2-5 times slower on pentium, I heard not on AMD but dont know.
If you need integer result better not use fpu since it is slow.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

raymond

And, until you become fully familiar with the FPU instructions, I would strongly recommend you stay with the CPU integer instructions.

For a start, look at the description of fstp, specially what is between the ( ) summarizing the purpose of the instruction:
http://www.ray.masmcode.com/tutorial/fpuchap4.htm#fstp

and then think of what you will recover later in the EAX register.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

FORTRANS

Hi,

   Mixing FPU and CPU was slow in the past as the floating
point to integer conversion was slow for loads or stores.  So
you tried to keep all the code in either the CPU or the FPU
until the calculation was finished.  Now on newer CPU's you
would probably have to time it to see which of the three is
faster.  And you are not using FILD or FIST so if you want
integer values where are you doing the conversion?

Regards,

Steve N.