Blame ieee-utils/fp-gnux86.c

Packit 67cb25
/* ieee-utils/fp-gnux86.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
#include <stdio.h>
Packit 67cb25
#include <fpu_control.h>
Packit 67cb25
#include <gsl/gsl_errno.h>
Packit 67cb25
#include <gsl/gsl_ieee_utils.h>
Packit 67cb25
Packit 67cb25
  /* Handle libc5, where _FPU_SETCW is not available, suggested by
Packit 67cb25
     OKUJI Yoshinori <okuji@gnu.org> and Evgeny Stambulchik
Packit 67cb25
     <fnevgeny@plasma-gate.weizmann.ac.il> */
Packit 67cb25
Packit 67cb25
#ifndef _FPU_SETCW
Packit 67cb25
#include <i386/fpu_control.h>
Packit 67cb25
#define _FPU_SETCW(cw) __setfpucw(cw)
Packit 67cb25
#endif
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_ieee_set_mode (int precision, int rounding, int exception_mask)
Packit 67cb25
{
Packit 67cb25
  unsigned short mode = 0 ;
Packit 67cb25
Packit 67cb25
  switch (precision)
Packit 67cb25
    {
Packit 67cb25
    case GSL_IEEE_SINGLE_PRECISION:
Packit 67cb25
      mode |= _FPU_SINGLE ;
Packit 67cb25
      break ;
Packit 67cb25
    case GSL_IEEE_DOUBLE_PRECISION:
Packit 67cb25
      mode |= _FPU_DOUBLE ;
Packit 67cb25
      break ;
Packit 67cb25
    case GSL_IEEE_EXTENDED_PRECISION:
Packit 67cb25
      mode |= _FPU_EXTENDED ;
Packit 67cb25
      break ;
Packit 67cb25
    default:
Packit 67cb25
      mode |= _FPU_EXTENDED ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  switch (rounding)
Packit 67cb25
    {
Packit 67cb25
    case GSL_IEEE_ROUND_TO_NEAREST:
Packit 67cb25
      mode |= _FPU_RC_NEAREST ;
Packit 67cb25
      break ;
Packit 67cb25
    case GSL_IEEE_ROUND_DOWN:
Packit 67cb25
      mode |= _FPU_RC_DOWN ;
Packit 67cb25
      break ;
Packit 67cb25
    case GSL_IEEE_ROUND_UP:
Packit 67cb25
      mode |= _FPU_RC_UP ;
Packit 67cb25
      break ;
Packit 67cb25
    case GSL_IEEE_ROUND_TO_ZERO:
Packit 67cb25
      mode |= _FPU_RC_ZERO ;
Packit 67cb25
      break ;
Packit 67cb25
    default:
Packit 67cb25
      mode |= _FPU_RC_NEAREST ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  if (exception_mask & GSL_IEEE_MASK_INVALID)
Packit 67cb25
    mode |= _FPU_MASK_IM ;
Packit 67cb25
Packit 67cb25
  if (exception_mask & GSL_IEEE_MASK_DENORMALIZED)
Packit 67cb25
    mode |= _FPU_MASK_DM ;
Packit 67cb25
Packit 67cb25
  if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO)
Packit 67cb25
    mode |= _FPU_MASK_ZM ;
Packit 67cb25
Packit 67cb25
  if (exception_mask & GSL_IEEE_MASK_OVERFLOW)
Packit 67cb25
    mode |= _FPU_MASK_OM ;
Packit 67cb25
Packit 67cb25
  if (exception_mask & GSL_IEEE_MASK_UNDERFLOW)
Packit 67cb25
    mode |= _FPU_MASK_UM ;
Packit 67cb25
Packit 67cb25
  if (exception_mask & GSL_IEEE_TRAP_INEXACT)
Packit 67cb25
    {
Packit 67cb25
      mode &= ~ _FPU_MASK_PM ;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      mode |= _FPU_MASK_PM ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  _FPU_SETCW(mode) ;
Packit 67cb25
Packit 67cb25
#if HAVE_FPU_X86_SSE
Packit 67cb25
#define _FPU_SETMXCSR(cw_sse) asm volatile ("ldmxcsr %0" : : "m" (*&cw_sse))
Packit 67cb25
  {
Packit 67cb25
    unsigned int mode_sse = 0;
Packit 67cb25
Packit 67cb25
    mode_sse |= (mode & 0x3f)<<7;  /* exception masks */
Packit 67cb25
    mode_sse |= (mode & 0xc00)<<3;    /* rounding control */
Packit 67cb25
    
Packit 67cb25
    _FPU_SETMXCSR(mode_sse);
Packit 67cb25
  }
Packit 67cb25
#endif
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS ;
Packit 67cb25
}