[Openmcl-devel] Floating point exceptions on Windows

Martin martin.lisp at gmail.com
Wed May 6 00:20:50 PDT 2009


(I had sent this reply earlier, but it seems to have been copied only to 
R. Matthew Emerson. Apologies if it appears multiple times in the list.)

R. Matthew Emerson wrote:
> On May 3, 2009, at 11:18 AM, Martin wrote:
> 
>> Hi!
>>
>> I am using CCL 1.3-r11949 on Windows XP SP2 and have a problem with
>> floating point exception propagation.
>>
>> Now, I have a file app1.c with a function with the following code  
>> extract:
>>
>>
>> __declspec(dllexport) double fn1 ()
>> {
>>   double zero = 0;
>>   return 1.0/zero;
>> }
>>
>> I compiled it into a DLL (Using MS Visual C++ 2005 Express Edition)  
>> with
>> the following command:
>>
>> cl /LD app1.c
>>
>> to yield app1.dll.
>>
>> Using it in CCL:
>>
>> Welcome to Clozure Common Lisp Version 1.3-r11949M  (WindowsX8632)!
>> ? (open-shared-library "D:/studies/floats/app1/app1.dll")
>> #<SHLIB app1.dll #x89DED5E>
>> ? (ff-call (%reference-external-entry-point (external  
>> "fn1")) :double-float)
>> 1.0000000000000002D0
>> ? (log 10)
>>> Error: DIVISION-BY-ZERO detected
>>>       performing LOG on (10.0)
>>> While executing: CCL::%FP-ERROR-FROM-STATUS, in process listener(1).
>>> Type :POP to abort, :R for a list of available restarts.
>>> Type :? for other options.
>> 1 >
> 
> This is kind of a long shot, but...if you disassemble fn1(), does it  
> use x87 or SSE2 instructions?
> 

fn1() seems to be using x87 instructions.

(I don't know much assembly myself---actually trying to learn at least 
for to be able to read compiler sources.)

Regardless:

Compiling app1.c with the assembly output option, _fn1() compiles to:


_fn1    PROC
     push    ebp
     mov    ebp, esp
     sub    esp, 8
     fldz
     fstp    QWORD PTR _zero$[ebp]
     fld1
     fdiv    QWORD PTR _zero$[ebp]
     mov    esp, ebp
     pop    ebp
     ret    0
_fn1    ENDP

(A disassembly of fn1() in the compiled DLL shows the same code
sequence.)

(
Compiling with the /arch:SSE2 flag added yields:

_fn1    PROC
     push    ebp
     mov    ebp, esp
     sub    esp, 8
     movsd    xmm0, QWORD PTR __real at 0000000000000000
     movsd    QWORD PTR _zero$[ebp], xmm0
     fld1
     fdiv    QWORD PTR _zero$[ebp]
     mov    esp, ebp
     pop    ebp
     ret    0
_fn1    ENDP

So, I suspect my conclusion is correct.
)

> On non-Darwin x8632 systems (including Windows), we assume that  
> foreign code will use the x87 for floating-point operations.  After  
> the ff-call, we therefore will look at the x87 state to see if the  
> foreign code signaled any fp exceptions.   If the foreign code uses  
> SS2E, we won't notice any exceptiopns (because we're looking for them  
> in the wrong place: the x87 state, and not the mxcsr).
> 
>>  2. How can I disable floating point exceptions from within CCL?
> 
> The manual documents SET-FPU-MODE and GET-FPU-MODE, which will let you  
> do this.
> 
> http://ccl.clozure.com/ccl-documentation.html#floating-point


Thanks. I hadn't thought to search the manual and/or sources. Just tried 
can now disable signaling.

Regards,
Martin.




More information about the Openmcl-devel mailing list