Back to index

glibc  2.9
setfpucw.c
Go to the documentation of this file.
00001 /* Set FP exception mask and rounding mode.  Mach/Alpha version.
00002    Copyright (C) 2002 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 
00022 
00023 #define FPCR_DYN_SHIFT      58            /* first dynamic rounding mode bit */
00024 #define FPCR_DYN_CHOPPED (0x0UL << FPCR_DYN_SHIFT)      /* towards 0 */
00025 #define FPCR_DYN_MINUS       (0x1UL << FPCR_DYN_SHIFT)  /* towards -INF */
00026 #define FPCR_DYN_NORMAL      (0x2UL << FPCR_DYN_SHIFT)  /* towards nearest */
00027 #define FPCR_DYN_PLUS        (0x3UL << FPCR_DYN_SHIFT)  /* towards +INF */
00028 #define FPCR_DYN_MASK        (0x3UL << FPCR_DYN_SHIFT)
00029 
00030 static inline unsigned long
00031 rdfpcr (void)
00032 {
00033   unsigned long fpcr;
00034   asm ("excb; mf_fpcr %0" : "=f"(fpcr));
00035   return fpcr;
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;
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   /* XXX trap bits? */
00067 
00068   __fpu_control = fpu_control;     /* update global copy */
00069 }