Home | History | Annotate | Line # | Download | only in i386
sfp-exceptions.c revision 1.1.1.3.4.2
      1 /*
      2  * Copyright (C) 2012-2018 Free Software Foundation, Inc.
      3  *
      4  * This file is free software; you can redistribute it and/or modify it
      5  * under the terms of the GNU General Public License as published by the
      6  * Free Software Foundation; either version 3, or (at your option) any
      7  * later version.
      8  *
      9  * This file is distributed in the hope that it will be useful, but
     10  * WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * General Public License for more details.
     13  *
     14  * Under Section 7 of GPL version 3, you are granted additional
     15  * permissions described in the GCC Runtime Library Exception, version
     16  * 3.1, as published by the Free Software Foundation.
     17  *
     18  * You should have received a copy of the GNU General Public License and
     19  * a copy of the GCC Runtime Library Exception along with this program;
     20  * see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     21  * <http://www.gnu.org/licenses/>.
     22  */
     23 
     24 #ifndef _SOFT_FLOAT
     25 #include "sfp-machine.h"
     26 
     27 struct fenv
     28 {
     29   unsigned short int __control_word;
     30   unsigned short int __unused1;
     31   unsigned short int __status_word;
     32   unsigned short int __unused2;
     33   unsigned short int __tags;
     34   unsigned short int __unused3;
     35   unsigned int __eip;
     36   unsigned short int __cs_selector;
     37   unsigned int __opcode:11;
     38   unsigned int __unused4:5;
     39   unsigned int __data_offset;
     40   unsigned short int __data_selector;
     41   unsigned short int __unused5;
     42 };
     43 
     44 void
     45 __sfp_handle_exceptions (int _fex)
     46 {
     47   if (_fex & FP_EX_INVALID)
     48     {
     49       float f = 0.0f;
     50 #ifdef __SSE_MATH__
     51       volatile float r __attribute__ ((unused));
     52       asm volatile ("%vdivss\t{%0, %d0|%d0, %0}" : "+x" (f));
     53       r = f; /* Needed to trigger exception.   */
     54 #else
     55       asm volatile ("fdiv\t{%y0, %0|%0, %y0}" : "+t" (f));
     56       /* No need for fwait, exception is triggered by emitted fstp.  */
     57 #endif
     58     }
     59   if (_fex & FP_EX_DENORM)
     60     {
     61       struct fenv temp;
     62       asm volatile ("fnstenv\t%0" : "=m" (temp));
     63       temp.__status_word |= FP_EX_DENORM;
     64       asm volatile ("fldenv\t%0" : : "m" (temp));
     65       asm volatile ("fwait");
     66     }
     67   if (_fex & FP_EX_DIVZERO)
     68     {
     69       float f = 1.0f, g = 0.0f;
     70 #ifdef __SSE_MATH__
     71       volatile float r __attribute__ ((unused));
     72       asm volatile ("%vdivss\t{%1, %d0|%d0, %1}" : "+x" (f) : "xm" (g));
     73       r = f; /* Needed to trigger exception.   */
     74 #else
     75       asm volatile ("fdivs\t%1" : "+t" (f) : "m" (g));
     76       /* No need for fwait, exception is triggered by emitted fstp.  */
     77 #endif
     78     }
     79   if (_fex & FP_EX_OVERFLOW)
     80     {
     81       struct fenv temp;
     82       asm volatile ("fnstenv\t%0" : "=m" (temp));
     83       temp.__status_word |= FP_EX_OVERFLOW;
     84       asm volatile ("fldenv\t%0" : : "m" (temp));
     85       asm volatile ("fwait");
     86     }
     87   if (_fex & FP_EX_UNDERFLOW)
     88     {
     89       struct fenv temp;
     90       asm volatile ("fnstenv\t%0" : "=m" (temp));
     91       temp.__status_word |= FP_EX_UNDERFLOW;
     92       asm volatile ("fldenv\t%0" : : "m" (temp));
     93       asm volatile ("fwait");
     94     }
     95   if (_fex & FP_EX_INEXACT)
     96     {
     97       float f = 1.0f, g = 3.0f;
     98 #ifdef __SSE_MATH__
     99       volatile float r __attribute__ ((unused));
    100       asm volatile ("%vdivss\t{%1, %d0|%d0, %1}" : "+x" (f) : "xm" (g));
    101       r = f; /* Needed to trigger exception.   */
    102 #else
    103       asm volatile ("fdivs\t%1" : "+t" (f) : "m" (g));
    104       /* No need for fwait, exception is triggered by emitted fstp.  */
    105 #endif
    106     }
    107 };
    108 #endif
    109