public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Strange behavior with movq
@ 2011-05-25 17:27 Olivier Maury
  2011-05-25 17:48 ` Axel Freyn
  2011-05-25 18:31 ` Ian Lance Taylor
  0 siblings, 2 replies; 4+ messages in thread
From: Olivier Maury @ 2011-05-25 17:27 UTC (permalink / raw)
  To: gcc-help

Hi,

I've a problem with the movq instruction on a 32 bits OS : when that 
instruction is executed it has a bad effect on the floating stack.

The gcc version I tried : 4.5.1 and 4.1.2.

The OS : Red Hat Enterprise Linux Client release 5.5 (Tikanga)
kernel : 2.6.18-194.el5PAE

Please note that the following code works fine if I use an older but 64 
bits version of RHEL:
Red Hat Enterprise Linux WS release 4 (Nahant Update 5)
kernel :  2.6.9-55.ELsmp

I wrote a very simple example to test some movntq builtin function:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
   double *out;
   double in = 2.0;
   long long unsigned int *vin = (long long unsigned int *)&in;

   out = (double*)malloc(sizeof(double));

   out[0] = 0.0;

   printf("%E\n", out[0]);
   printf("%E\n", *((double*)vin));

   __builtin_ia32_movntq((long long unsigned int*)(out), *vin);

   printf("%E\n", *out);
}

Since I'm compiling on a 32 bits OS I use the following compilation 
command: gcc movntq.c -mfpmath=sse -msse3

If I have a look at the assembly code I have the following code between 
the two last printf :

         call    printf
         movl    -12(%ebp), %eax
         movl    (%eax), %edx
         movl    4(%eax), %ecx
         movl    -16(%ebp), %eax
         movl    %edx, -32(%ebp)
         movl    %ecx, -28(%ebp)
         movq    -32(%ebp), %mm0
         movntq  %mm0, (%eax)
         movl    -16(%ebp), %eax
         movsd   (%eax), %xmm0
         movsd   %xmm0, 4(%esp)
         movl    $.LC2, (%esp)
         call    printf

The problem is around the movq instruction... When I go over that 
instruction using the stepi of gdb, the floating stack gets very strange:

(gdb) info float
   R7: Valid   0x403d8000000000000000 +4611686018427387904
   R6: Zero    0x00000000000000000000 +0
   R5: Zero    0x00000000000000000000 +0
   R4: Zero    0x00000000000000000000 +0
   R3: Zero    0x00000000000000000000 +0
   R2: Zero    0x00000000000000000000 +0
   R1: Zero    0x00000000000000000000 +0
=>R0: Special 0xffff4000000000000000 Unsupported

Status Word:         0x0000
                        TOP: 0
Control Word:        0x037f   IM DM ZM OM UM PM
                        PC: Extended Precision (64-bits)
                        RC: Round to nearest
Tag Word:            0x1556
Instruction Pointer: 0x73:0x007f6c4e
Operand Pointer:     0x7b:0xbfffda64
Opcode:              0xdd5c

If the value stored in out[0] is ok after the movntq instruction (p 
out[0] returns 2 in gdb) the last printf returns a NAN...

Does anyone have an idea about what's going on here ? Is it a bug ?

Best Regards

Olivier

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

* Re: Strange behavior with movq
  2011-05-25 17:27 Strange behavior with movq Olivier Maury
@ 2011-05-25 17:48 ` Axel Freyn
  2011-05-25 18:31 ` Ian Lance Taylor
  1 sibling, 0 replies; 4+ messages in thread
From: Axel Freyn @ 2011-05-25 17:48 UTC (permalink / raw)
  To: gcc-help

Hi Olivier,
On Wed, May 25, 2011 at 06:13:28PM +0200, Olivier Maury wrote:
> [...]
> I wrote a very simple example to test some movntq builtin function:
>
> #include <stdlib.h>
> #include <stdio.h>
>
> int main(int argc, char **argv)
> {
>   double *out;
>   double in = 2.0;
>   long long unsigned int *vin = (long long unsigned int *)&in;
>
>   out = (double*)malloc(sizeof(double));
>
>   out[0] = 0.0;
>
>   printf("%E\n", out[0]);
>   printf("%E\n", *((double*)vin));
>
>   __builtin_ia32_movntq((long long unsigned int*)(out), *vin);
>
>   printf("%E\n", *out);
> }
>
> Since I'm compiling on a 32 bits OS I use the following compilation  
> command: gcc movntq.c -mfpmath=sse -msse3
>
> If I have a look at the assembly code I have the following code between  
> the two last printf :
>
>         call    printf
>         movl    -12(%ebp), %eax
>         movl    (%eax), %edx
>         movl    4(%eax), %ecx
>         movl    -16(%ebp), %eax
>         movl    %edx, -32(%ebp)
>         movl    %ecx, -28(%ebp)
>         movq    -32(%ebp), %mm0
>         movntq  %mm0, (%eax)
>         movl    -16(%ebp), %eax
>         movsd   (%eax), %xmm0
>         movsd   %xmm0, 4(%esp)
>         movl    $.LC2, (%esp)
>         call    printf
>
> [...]
>
> If the value stored in out[0] is ok after the movntq instruction (p  
> out[0] returns 2 in gdb) the last printf returns a NAN...
>
> Does anyone have an idea about what's going on here ? Is it a bug ?
Is think it's a bug in your code / the documentation.
See the old bug report http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28331

calling __builtin_ia32_emms(); after movntq solves the problem on my 
machine.

Axel

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

* Re: Strange behavior with movq
  2011-05-25 17:27 Strange behavior with movq Olivier Maury
  2011-05-25 17:48 ` Axel Freyn
@ 2011-05-25 18:31 ` Ian Lance Taylor
       [not found]   ` <ABFB4F15FE95AC429F394F3C1D58BBE50EA85E@EU1-MAIL.mgc.mentorg.com>
  1 sibling, 1 reply; 4+ messages in thread
From: Ian Lance Taylor @ 2011-05-25 18:31 UTC (permalink / raw)
  To: Olivier Maury; +Cc: gcc-help

Olivier Maury <Olivier_maury@mentor.com> writes:

> I've a problem with the movq instruction on a 32 bits OS : when that
> instruction is executed it has a bad effect on the floating stack.

When you are using SSE registers you shouldn't be using the floating
point stack, and vice-versa.  Read up on the EMMS instruction.

Ian

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

* Re: RE : Strange behavior with movq
       [not found]   ` <ABFB4F15FE95AC429F394F3C1D58BBE50EA85E@EU1-MAIL.mgc.mentorg.com>
@ 2011-05-26 12:05     ` Ian Lance Taylor
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2011-05-26 12:05 UTC (permalink / raw)
  To: Maury, Olivier; +Cc: gcc-help

"Maury, Olivier" <Olivier_Maury@mentor.com> writes:

> I suppose the problem is, as I use a 32 bits OS, the printf function
> was compiled using the FP stack and not the sse/mmx registers !
> Otherwise I should have the same pb on 64 bits which is not the
> case. Am I correct ?

Yes.

Ian

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

end of thread, other threads:[~2011-05-25 22:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-25 17:27 Strange behavior with movq Olivier Maury
2011-05-25 17:48 ` Axel Freyn
2011-05-25 18:31 ` Ian Lance Taylor
     [not found]   ` <ABFB4F15FE95AC429F394F3C1D58BBE50EA85E@EU1-MAIL.mgc.mentorg.com>
2011-05-26 12:05     ` RE : " Ian Lance Taylor

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).