From B.Rowlingson@lancaster.ac.uk  Mon Nov 18 17:57:47 2002
From: B.Rowlingson@lancaster.ac.uk (Barry Rowlingson)
Date: Mon, 18 Nov 2002 17:57:47 +0000
Subject: [Rd] i386 floating point tweaking
Message-ID: <3DD92A1B.1020707@lancaster.ac.uk>

Just been trying to get the polygon triangulation code from this 
package: http://www-2.cs.cmu.edu/~quake/triangle.html to dyn.load into 
R. Uh oh. Floating point exceptions.

Track it down to some FPU diddling that the author deems is necessary. 
Here's my minimal code that breaks:

flipme.c:

#include <fpu_control.h>
void flipme(){
   int cword;
   cword=4210;
   _FPU_SETCW(cword);
   printf("%f\n",sqrt(2.0));
}

  Compile with "R SHLIB flipme.c", then dyn.load and run:

 > dyn.load("flipme.so")
 > .C("flipme")

Process R floating point exception at Mon Nov 18 17:41:50 2002

  The debugger tells me its the printf line that kills it.

  Anyone have an idea what's going on here? I've tried a few things:

* using _FPU_GETCW I've determined that R hasn't changed the control 
word from the system default.

* I wrote a short main program that used dlopen() to call flipme - and 
it worked fine, so the problem isn't intrinsic to dynamically loaded code.

* the author of the triangle package has put this in the code that 
manipulates the FPU control word:

/*  Don't change this routine unless you fully understand it. 
      */

  - I dont understand it, I changed it, the code broke.

  Anyone with deeper understanding of floating point, iee754, and 
assorted other Recipes For Chaos (RFCs) care to comment before we go 
back to using a file or a pipe to communicate between R and the code?

Baz



[Rd] i386 floating point tweaking
Barry Rowlingson