Back to index

glibc  2.9
setfpucw.c
Go to the documentation of this file.
00001 /* Set FP exception mask and rounding mode.
00002    Copyright (C) 1996, 1997, 1998, 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <fpu_control.h>
00021 #include <asm/fpu.h>
00022 
00023 extern void          __ieee_set_fp_control (unsigned long);
00024 libc_hidden_proto(__ieee_set_fp_control)
00025 
00026 extern unsigned long __ieee_get_fp_control (void);
00027 libc_hidden_proto(__ieee_get_fp_control)
00028 
00029 static inline unsigned long
00030 rdfpcr (void)
00031 {
00032   unsigned long fpcr;
00033   asm ("excb; mf_fpcr %0" : "=f"(fpcr));
00034   return fpcr;
00035 }
00036 
00037 
00038 static inline void
00039 wrfpcr (unsigned long fpcr)
00040 {
00041   asm volatile ("mt_fpcr %0; excb" : : "f"(fpcr));
00042 }
00043 
00044 
00045 void
00046 __setfpucw (fpu_control_t fpu_control)
00047 {
00048   unsigned long fpcr = 0, fpcw = 0;
00049 
00050   if (!fpu_control)
00051     fpu_control = _FPU_DEFAULT;
00052 
00053   /* first, set dynamic rounding mode: */
00054 
00055   fpcr = rdfpcr();
00056   fpcr &= ~FPCR_DYN_MASK;
00057   switch (fpu_control & 0xc00)
00058     {
00059     case _FPU_RC_NEAREST:   fpcr |= FPCR_DYN_NORMAL; break;
00060     case _FPU_RC_DOWN:             fpcr |= FPCR_DYN_MINUS; break;
00061     case _FPU_RC_UP:        fpcr |= FPCR_DYN_PLUS; break;
00062     case _FPU_RC_ZERO:             fpcr |= FPCR_DYN_CHOPPED; break;
00063     }
00064   wrfpcr(fpcr);
00065 
00066   /* now tell kernel about traps that we like to hear about: */
00067 
00068   fpcw = __ieee_get_fp_control();
00069   fpcw &= ~IEEE_TRAP_ENABLE_MASK;
00070 
00071   if (!(fpu_control & _FPU_MASK_IM)) fpcw |= IEEE_TRAP_ENABLE_INV;
00072   if (!(fpu_control & _FPU_MASK_DM)) fpcw |= IEEE_TRAP_ENABLE_UNF;
00073   if (!(fpu_control & _FPU_MASK_ZM)) fpcw |= IEEE_TRAP_ENABLE_DZE;
00074   if (!(fpu_control & _FPU_MASK_OM)) fpcw |= IEEE_TRAP_ENABLE_OVF;
00075   if (!(fpu_control & _FPU_MASK_PM)) fpcw |= IEEE_TRAP_ENABLE_INE;
00076 
00077   __fpu_control = fpu_control;     /* update global copy */
00078 
00079   __ieee_set_fp_control(fpcw);
00080 }