Different results when using different cl.exe compiler options

2

I'm on Windows 7 64bit, using VS2013 Express for Desktop.

Compile as default (using SSE)

D:\Work>cl printf-test.c
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86

D:\Work>printf-test.exe
FPU control word 0x0000037f
0. FPU status 0x00000000
1. 0x43380000 0x00000000
1. FPU status 0x00000000
2. 0x43380000 0x00000001
2. FPU status 0x00000000
3. 0x43380000 0x00000001          //@@1
3. FPU status 0x00000000

Compile using x87 only.

D:\Work>cl /arch:IA32 printf-test.c
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86

D:\Work>printf-test.exe
FPU control word 0x0000037f
0. FPU status 0x00000000
1. 0x43380000 0x00000000
1. FPU status 0x00000020
2. 0x43380000 0x00000001   
2. FPU status 0x00000220
3. 0x43380000 0x00000000         //@@1 this is the difference. why?
3. FPU status 0x00000020

Test code as below

#include <stdio.h>

void print_double_hex( double d , int idx)
{
    union UD
    {
        double d;
        int i[2];
    } c;
    c.d = d + 6755399441055744.0;
    printf("%d. 0x%08x 0x%08x\n", idx, c.i[1], c.i[0]);
}

void PrintControlWord()
{
    int a;
    int c;
    __asm {
        fstcw [esp - 4]
        mov eax, [esp - 4]
        and eax, 0xFFFF
        mov c, eax
    }
    printf("FPU control word 0x%08x\n", c);
}

void PrintStatus(int idx)
{
    int a;
    int c;
    __asm {
        fstsw [esp - 4]
        mov eax, [esp - 4]
        and eax, 0xFFFF
        mov c, eax
    }
    printf("%d. FPU status 0x%08x\n", idx, c);
}

int main()
{
    double a = .5;
    double b = 0.501;
    double c = 0.5001;
    int fpu = 0;

    __asm {                 // @@2 Remove it, see result below
        finit
    }

    PrintControlWord();
    PrintStatus(0); 

    print_double_hex(a, 1);
    PrintStatus(1); 

    print_double_hex(b, 2);
    PrintStatus(2);

    print_double_hex(c, 3);
    PrintStatus(3);

    return 0;
}

Note that I initialize the FPU manually, if I remove it. The result will change!

After removing finit, Compile as default

D:\Work>cl printf-test.c
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86

D:\Work>printf-test.exe
FPU control word 0x0000027f      //@@2 FPU control word changed, why?
0. FPU status 0x00000000
1. 0x43380000 0x00000000
1. FPU status 0x00000000
2. 0x43380000 0x00000001
2. FPU status 0x00000000
3. 0x43380000 0x00000001
3. FPU status 0x00000000

Compile using x87 only

D:\Work>cl /arch:IA32 printf-test.c
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86

D:\Work>printf-test.exe
FPU control word 0x0000027f
0. FPU status 0x00000000
1. 0x43380000 0x00000000
1. FPU status 0x00000020
2. 0x43380000 0x00000001
2. FPU status 0x00000020
3. 0x43380000 0x00000001         //@@3, why?
3. FPU status 0x00000020

So my questions is:

  1. For @@1, why there are different? Is it a bug?

  2. For @@3, why the value changed?

visual-c++
sse
inline-assembly
fpu
x87
asked on Stack Overflow Jun 30, 2014 by Sonny • edited Aug 30, 2019 by Michael Petch

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0