www.digitalmars.com         C & C++   DMDScript  

c++ - converting unsigned long to double

reply 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
parent "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