public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Need help on FPU division on 64 bit RHEL
@ 2009-03-20  7:04 Halesh S
  2009-03-20  8:43 ` Andrew Haley
  2009-03-20 17:09 ` Bob Plantz
  0 siblings, 2 replies; 3+ messages in thread
From: Halesh S @ 2009-03-20  7:04 UTC (permalink / raw)
  To: gcc-help

Hi,

I am writing assembly level code for dividing two numbers.

The assembly function i used is

float  assm_div(float a1, float b1)
{
 float res;
 __asm__ ("fld 8(%%ebp)"
          :
          :"a"(a1)
         );
 __asm__ ("fld 12(%%ebp)"
         :
         :"b"(b1)
         );
 __asm__ ("fdiv %st(1), %st");
 __asm__ ("fstp -8(%ebp)"
         );
 __asm__ ("fstp -12(%ebp)"
         );
 return res;
}

It works fine in Red hat 32 bit Linux, for the same its not working
Red hat 64 bit Linux, on same hardware.

Is there any changes required for code to make work??

Thanks,
Halesh

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need help on FPU division on 64 bit RHEL
  2009-03-20  7:04 Need help on FPU division on 64 bit RHEL Halesh S
@ 2009-03-20  8:43 ` Andrew Haley
  2009-03-20 17:09 ` Bob Plantz
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Haley @ 2009-03-20  8:43 UTC (permalink / raw)
  To: Halesh S; +Cc: gcc-help

Halesh S wrote:

> I am writing assembly level code for dividing two numbers.
> 
> The assembly function i used is
> 
> float  assm_div(float a1, float b1)
> {
>  float res;
>  __asm__ ("fld 8(%%ebp)"
>           :
>           :"a"(a1)
>          );
>  __asm__ ("fld 12(%%ebp)"
>          :
>          :"b"(b1)
>          );
>  __asm__ ("fdiv %st(1), %st");
>  __asm__ ("fstp -8(%ebp)"
>          );
>  __asm__ ("fstp -12(%ebp)"
>          );
>  return res;
> }
> 
> It works fine in Red hat 32 bit Linux, for the same its not working
> Red hat 64 bit Linux, on same hardware.
> 
> Is there any changes required for code to make work??

It'll have to be rewritten for x86_64, which has a different instruction
set.  But there are several bugs in the i386 version anyway: it works
only by accident.  Why do you not do

  return a/b;

?

Andrew.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need help on FPU division on 64 bit RHEL
  2009-03-20  7:04 Need help on FPU division on 64 bit RHEL Halesh S
  2009-03-20  8:43 ` Andrew Haley
@ 2009-03-20 17:09 ` Bob Plantz
  1 sibling, 0 replies; 3+ messages in thread
From: Bob Plantz @ 2009-03-20 17:09 UTC (permalink / raw)
  To: Halesh S; +Cc: gcc-help

On Fri, 2009-03-20 at 12:33 +0530, Halesh S wrote:
> Hi,
> 
> I am writing assembly level code for dividing two numbers.
> 
> The assembly function i used is
> 
> float  assm_div(float a1, float b1)
> {
>  float res;
>  __asm__ ("fld 8(%%ebp)"
>           :
>           :"a"(a1)
>          );
>  __asm__ ("fld 12(%%ebp)"
>          :
>          :"b"(b1)
>          );
>  __asm__ ("fdiv %st(1), %st");
>  __asm__ ("fstp -8(%ebp)"
>          );
>  __asm__ ("fstp -12(%ebp)"
>          );
>  return res;
> }
> 
> It works fine in Red hat 32 bit Linux, for the same its not working
> Red hat 64 bit Linux, on same hardware.
> 
> Is there any changes required for code to make work??
> 
> Thanks,
> Halesh

The first thing to know is that the default for floating point is x87 in
32-bit and it's SSE2 in 64-bit.

When I use the command:
   gcc -O1 -S -fno-asynchronous-unwind-tables divide.c

for the C source file:

double asm_div(double a, double b) { 
  return a / b;
}

I get:

	.file	"divide.c"
	.text
.globl asm_div
	.type	asm_div, @function
asm_div:
	divsd	%xmm1, %xmm0
	ret
	.size	asm_div, .-asm_div
	.ident	"GCC: (Ubuntu 4.3.2-1ubuntu12) 4.3.2"
	.section	.note.GNU-stack,"",@progbits

Floating point arguments are passed in the xmm registers, not on the
stack, in 64-bit. And the return value is in the xmm0 register. So the
assembly language is only one instruction.

For more complicated C functions, try the command:
   gcc -O1 -c -g -Wa,-adhl -fno-asynchronous-unwind-tables divide.c >
divide.lst

As an aside, you should probably use "double" instead of "float" to get
better precision. If you use float, the instruction is divss instead of
divsd.


--Bob


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-03-20 17:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-20  7:04 Need help on FPU division on 64 bit RHEL Halesh S
2009-03-20  8:43 ` Andrew Haley
2009-03-20 17:09 ` Bob Plantz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).