Don Fisher wrote:

> For the alpha I see many compiler flags that control how floating point errors
> are handled.  Examples are
> 
> -mfp-trap-mode=TRAP MODE
> -mfp-rounding-mode=ROUNDING MODE
> -mtrap-precision=TRAP PRECISION
> 
> How are these functions implemented for the i386?  I would like to be able to
> ignore or trap to an exception handler things like floating underflow, division
> by zero etc.

AFAIK, you have to manipulate the FPU control word directly, e.g.

        #include <stdio.h>
        
        int main(void)
        {
                unsigned short cw = 0;
                double one = 1.0, zero = 0.0;
        
                /* print the CW */
                asm("fstcw %0" : "=m" (cw));
                printf("CW=%04x\n", cw);
        
                /* divide by zero: shouldn't generate SIGFPE */
                printf("1/0 = ");
                printf("%f\n", one/zero);
        
                /* clear the ZM (divide-by-zero mask) bit */
                cw &= ~0x0004;
                asm("fldcw %0" : "=m" (cw));
        
                /* print the CW */
                asm("fstcw %0" : "=m" (cw));
                printf("CW=%04x\n", cw);
        
                /* divide by zero: should generate SIGFPE */
                printf("1/0 = ");
                printf("%f\n", one/zero);
        
                return 0;
        }

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to