Author Topic: Divide and Multiply LONGS  (Read 4789 times)

0 Members and 1 Guest are viewing this topic.

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 964
    • it-berater
Divide and Multiply LONGS
« on: December 23, 2009, 06:52:34 AM »
Hi Hutch, take a look at these.
It's quite a while that i did these, are they still state of the art?
Or would you suggest a change somewhere to get sweeze some additional cycles out?

Code: [Select]
'------------------------------------------------------------------------------------------------
' Unsigned Multipliziert P1=P2*P3
' Uses Flags, EAX,EDX. Ergebnis ist am Ende auch in EAX
' P1,P2,P3 sind Variablen oder Registernamen
MACRO A_MUL(P1,P2,P3)
! MOV EAX,P2
! MUL P3
! MOV P1,EAX
END MACRO
'------------------------------------------------------------------------------------------------
' Sgned Multipliziert P1=P2*P3
' Uses Flags, EAX,EDX. Ergebnis ist am Ende auch in EAX
' P1,P2,P3 sind Variablen oder Registernamen
MACRO A_SMUL(P1,P2,P3)
! MOV EAX,P2
! CDQ
! IMUL P3
! MOV P1,EAX
END MACRO
'------------------------------------------------------------------------------------------------
' Unsigned-Dividiert P1=P2/P3
' Uses Flags, EAX,EDX. Ergebnis ist am Ende auch in EAX
' P1,P2,P3 sind Variablen oder Registernamen
MACRO A_DIV(P1,P2,P3)
! MOV EAX,P2
! CDQ
! DIV P3
! MOV P1,EAX
END MACRO
'------------------------------------------------------------------------------------------------
' Signed Dividiert P1=P2/P3
' Uses Flags, EAX,EDX. Ergebnis ist am Ende auch in EAX
' P1,P2,P3 sind Variablen oder Registernamen
MACRO A_SDIV(P1,P2,P3)
! MOV EAX,P2
! CDQ
! IDIV P3
! MOV P1,EAX
END MACRO
« Last Edit: December 23, 2009, 07:19:32 AM by Josť Roca »

Offline Steve Hutchesson

  • Moderator
  • Jr. Member
  • *****
  • Posts: 83
    • The MASM Forum
Re: Divide and Multiply LONGS
« Reply #1 on: December 23, 2009, 07:47:59 AM »
Theo,

Just taking a quick look at the macros, I doubt there are any real gains as the speed limit will be the respective DIV and MUL instructions which have alwas been slow operations. If you were going to try a different technique you could try the FP unit but from memory its no faster. An option I have not looked at is some of the very late SSE instructions which may be faster but they are not general purtpose like the macros you have posted.

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 964
    • it-berater
Re: Divide and Multiply LONGS
« Reply #2 on: December 24, 2009, 06:47:33 AM »
Sometimes you just need an Integer Divide, you don't need anything after the Point.
Do you expect the FP-Unit to be faster then these Integer Operations?

Even if they can be made in parallel to some extent on modern CPU's?
Thats a point i am not sure about.

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 698
    • Charles Pegge
Re: Divide and Multiply LONGS
« Reply #3 on: December 24, 2009, 12:20:00 PM »

On recent CPUs, MUL is as fast as ADD but DIV is the really slow one. usually 20-40 times slower because it uses microcoded successive approximation . This is true of both the CPU and FPU. So to get the best execution times, avoid division wherever possible. In many situations, where you would normally divide by a constant, you can multiply by the reciprocal instead.

Offline Steve Hutchesson

  • Moderator
  • Jr. Member
  • *****
  • Posts: 83
    • The MASM Forum
Re: Divide and Multiply LONGS
« Reply #4 on: December 26, 2009, 11:42:08 PM »
Charles,

I remember from my school days the technique you mentioned and while I know how to do this with fractions on paper, I have not seen a simple way to do this with bare integers. Do you have flooating around or remember how to do this with integer values ?

Offline Frederick J. Harris

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 942
    • Frederick J. Harris
Re: Divide and Multiply LONGS
« Reply #5 on: January 11, 2010, 03:07:07 AM »
Quote
...where you would normally divide by a constant, you can multiply by the reciprocal instead.

To get the reciprocal to perform the multiplication would involve a division, not so?  (Oops!  Divide the constant yourself first dummy!)
« Last Edit: January 11, 2010, 05:26:48 PM by Frederick J. Harris »

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 698
    • Charles Pegge
Re: Divide and Multiply LONGS
« Reply #6 on: January 11, 2010, 10:38:13 PM »
Yes Frederick, you only get the performance benefit if the reciprocal is a constant that can be used repeatedly.

Steve,
I would do all the arithmetic in the FPU - the performance hit is minimal. You could devise a fixed-point reciprocal system for the CPU - using EAX for the fraction & pulling the integer result from EDX but I think this would be too restrictive to be safe for general use.