2

I'm learning about arithmetic of floating point number. And I wrote following code. But floating point exception does not occur. My environment is Cent OS 6.4 (x86_64).

Please teach me this reason.

#include <stdio.h>

int
main(void)
{
  double a,b,c;
  unsigned short int fctr;

  a=3.0;
  b=0.0;
  c=1.0;

  asm volatile(
    "fstcw %w0" // get FPU control word
    :"=m"(fctr):);
  printf("FPU control word= %X\n", fctr);

  fctr = fctr ^ 0x4;
  printf("FPU control word= %X\n", fctr);

  asm volatile(
    "fldcw %w0"  // set operand to FPU control word
    : :"m"(fctr));

  asm volatile(
    "fstcw %w0" // get FPU control word
    :"=m"(fctr):);
  printf("FPU control word= %X\n", fctr);

  c = a/b;

  return 0;
}
kuni255
  • 83
  • 8

1 Answers1

5

Probably because the x86_64 architecture by default does floating point with SSE2 not x87. (the statusword belongs to x87)

Compile with -S and check if the generated assembler really is x87.

Search for MXCSR in this link

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • 1
    That's correct. `feenableexcept` should be used instead of the inline asm, that will also work with the SSE implementation. – Jester May 29 '14 at 15:07
  • I confirmed generated instruction of division by gcc. Following is it. `divsd -0x10(%rbp),%xmm0` divsd is instruction of SSE2. I got that's why floating point exception does not occur?. Thank you for teaching me. – kuni255 May 31 '14 at 09:45
  • I don't know much about SSE exception support. I appended the only link I had to the original post, it seems to suggest setting a bit in mxcsr (what I assume the named glibc feenabledexcept does for you, but I don't use glibc since I'm generally a FreeBSD user). The article warns for performance issues though, like always with floating point, this is a tradeoff. – Marco van de Voort May 31 '14 at 14:24