www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - converting unsigned long to double

↑ ↓ ← Christof Meerwald <cmeerw web.de> writes:
I just wondered why DM was so much slower (about 10 times) than other
compilers (on a "silly" test program) and noticed that the culprit was a
conversion from unsigned long to double.

Let's have a look at the generated code:

double f(long l)
{
  return l;
}

generated code:
  fild    dword ptr 4[ESP]
  ret


But:

double g(unsigned long l)
{
  return l;
}

generated code:
  push    EAX
  push    EAX
  mov     EAX,0Ch[ESP]
  call    near ptr __ULNGDBL 
  mov     [ESP],EAX
  mov     4[ESP],EDX
  fld     qword ptr [ESP]
  add     ESP,8
  ret

Most other compilers seem to convert the function g to something like:

double g(unsigned long l)
{
  return (long long) l;
}

which usually is much faster (because it doesn't need to call a helper
function):
  push    EAX
  xor     ECX,ECX
  push    EAX
  mov     EAX,0Ch[ESP]
  mov     [ESP],EAX
  mov     4[ESP],ECX
  fild    long64 ptr [ESP]
  add     ESP,8
  ret

(all compiled with "-cpp -o+all -ff")


bye, Christof

-- 
http://cmeerw.org                                 JID: cmeerw jabber.at
mailto cmeerw at web.de

...and what have you contributed to the Net?
Feb 03 2003
↑ ↓ → "Walter" <walter digitalmars.com> writes:
Yes, that's better code. -Walter

"Christof Meerwald" <cmeerw web.de> wrote in message
news:b1mofb$9i7$1 digitaldaemon.com...
 I just wondered why DM was so much slower (about 10 times) than other
 compilers (on a "silly" test program) and noticed that the culprit was a
 conversion from unsigned long to double.

 Let's have a look at the generated code:

 double f(long l)
 {
   return l;
 }

 generated code:
   fild    dword ptr 4[ESP]
   ret


 But:

 double g(unsigned long l)
 {
   return l;
 }

 generated code:
   push    EAX
   push    EAX
   mov     EAX,0Ch[ESP]
   call    near ptr __ULNGDBL 
   mov     [ESP],EAX
   mov     4[ESP],EDX
   fld     qword ptr [ESP]
   add     ESP,8
   ret

 Most other compilers seem to convert the function g to something like:

 double g(unsigned long l)
 {
   return (long long) l;
 }

 which usually is much faster (because it doesn't need to call a helper
 function):
   push    EAX
   xor     ECX,ECX
   push    EAX
   mov     EAX,0Ch[ESP]
   mov     [ESP],EAX
   mov     4[ESP],ECX
   fild    long64 ptr [ESP]
   add     ESP,8
   ret

 (all compiled with "-cpp -o+all -ff")


 bye, Christof

 --
 http://cmeerw.org                                 JID: cmeerw jabber.at
 mailto cmeerw at web.de

 ...and what have you contributed to the Net?

Feb 03 2003