Need Help! Why does a printf() statement cause an SSE instruction to fail?

Negative_3

n00b
Joined
Apr 14, 2008
Messages
45
Hi. I have a problem. I am writing a C program in Visual Studio 2005 Pro and when I comment out a printf() statement, the whole program crashes. It doesn't make any sense to me, but hopefully some one here knows what the problem is.

Here is a simple program with some SSE instructions.

SourceCode1.jpg


That prefetcht0 just puts f3 in the top level of CPU cache (which is where it would already be as f3 was just initialized). Then the program moves the four floating point numbers in f1 into SSE register xmm0. It moves f2 into SSE register xmm1. Then it adds the four floats in xmm0 with the corresponding floats in xmm1 and stores the results in xmm1. Then it square roots each of the 4 floats in xmm1 and stores the results in xmm1. Then it writes those results from xmm1 into f3. Then it displays the results as expected:

Output1.jpg


Now lets comment out a printf() statement:

SourceCode2.jpg


And lets see what happens:

Output2.jpg


Wow, how does removing a printf() statement make the program crash?

I tried debugging it and I get this mess:

WTF.jpg


I hit the break button and it takes me here:

ASMProblem.jpg


The problem is the movaps instruction??

How could removing a printf() statement cause that to fail?

Here is the source code in a non picture format:

Code:
#include <stdio.h>

void main(void)
{
    int i;
    float f1[4]={1,2,3,4},f2[4]={3,7,13,21},f3[4]={0,0,0,0};

    __asm
    {
        prefetchT0    f3

        movaps	xmm0,f1
        movaps	xmm1,f2
        addps	xmm1,xmm0
        sqrtps	xmm1,xmm1
        movaps	f3,xmm1
    }


    for(i=0;i<4;i++)
    {
        printf("%f, ",f3[i]);
    }

    printf("\n\n");
}
 
Does it work with optimization disabled? Since the optimizer is unaware of how you're using any C memory in the asm block, it may well be postponing the initialization and allocation of f3, meaning the pointer is still null at that point, hence the exception. You may need to make one or both of the asm block and data arrays volatile to tell the compiler to leave them as written.

It's been a long time since I've used inline asm in anything but a microcontroller though, so I could well be way off.
 
Can you paste the decompiled asm from both the optimized and non optimized version?
 
Finally have a small amount of free time. In VS2010 that code crashes with or without the printf statement. I'll look at it...
 
Your data is not aligned to a 16 bit boundary (as is required by the "a" in movaps)

Since you're using VC++, try declaring like this:
__declspec(align(16)) float f1[4]={1,2,3,4},f2[4]={3,7,13,21},f3[4]={0,0,0,0};
 
I could also use movups for unaligned data, but movaps is faster.

Here is a quote from http://www.cortstratton.org/articles/OptimizingForSSE.php:

Okay, next lesson: when looking through the SSE instruction reference, did you notice that there are two different instructions for data movement: movups (unaligned 16-byte MOV) and movaps (aligned 16-byte MOV)? If you tried using movaps, you probably ended up with a "privileged instruction" runtime error, which is why you settled on movups. As it turns out, movaps is much faster than movups, but it requires that its arguments be aligned on 16-byte boundaries when moving to/from memory (otherwise, the instruction throws an exception). You have to do a little extra work in order to get your matrices and vectors to be 16-byte aligned, but it's worth it.

The first thing I tried in Visual C++ was to go into Project -> Settings -> C/C++ -> Code Generation, and change the "struct member alignment" field from "8 bytes" to "16 bytes". Problem solved? No, apparently not. Changing this field doesn't appear to do anything useful (if anybody knows why, please explain it to me!). Instead, I had to resort to making changes in the code itself, using the C/C++ __declspec(align(16)) specifier. You can put this specifier in front of the definition of any variable, data member, struct or class in order to force the object to be 16-byte aligned (in the case of structs or classes declared as __declspec(align(16)), all instances of that struct/class will be properly aligned).

It's important to note that apparently this method doesn't work for objects (or arrays of objects) which are dynamically allocated (with malloc() or new). Fortunately, the MSVC Processor Pack Upgrade includes an _mm_malloc() function (defined in malloc.h) which should do the trick. Otherwise, it may be necessary to write your own memory management routines to make sure your objects are properly aligned.
 
Back
Top