Blame ieee-utils/read.c

Packit 67cb25
/* ieee-utils/read.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 <config.h>
Packit 67cb25
#include <stdlib.h>
Packit 67cb25
#include <string.h>
Packit 67cb25
#include <gsl/gsl_errno.h>
Packit 67cb25
#include <gsl/gsl_ieee_utils.h>
Packit 67cb25
Packit 67cb25
static int 
Packit 67cb25
lookup_string (const char * p, int * precision, int * rounding, 
Packit 67cb25
               int * exception_mask) ;
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_ieee_read_mode_string (const char * description, 
Packit 67cb25
                           int * precision, 
Packit 67cb25
                           int * rounding, 
Packit 67cb25
                           int * exception_mask)
Packit 67cb25
{
Packit 67cb25
  char * start ;
Packit 67cb25
  char * end;
Packit 67cb25
  char * p;
Packit 67cb25
Packit 67cb25
  int precision_count = 0 ;
Packit 67cb25
  int rounding_count = 0 ;
Packit 67cb25
  int exception_count = 0 ;
Packit 67cb25
Packit 67cb25
  start = (char *) malloc(strlen(description) + 1) ;
Packit 67cb25
Packit 67cb25
  if (start == 0) 
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR ("no memory to parse mode string", GSL_ENOMEM) ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  strcpy (start, description) ;
Packit 67cb25
Packit 67cb25
  p = start ;
Packit 67cb25
Packit 67cb25
  *precision = 0 ;
Packit 67cb25
  *rounding = 0 ;
Packit 67cb25
  *exception_mask = 0 ;
Packit 67cb25
Packit 67cb25
  do {
Packit 67cb25
    int status ;
Packit 67cb25
    int new_precision, new_rounding, new_exception ;
Packit 67cb25
Packit 67cb25
    end = strchr (p,',') ;
Packit 67cb25
Packit 67cb25
    if (end) 
Packit 67cb25
      {
Packit 67cb25
        *end = '\0' ;
Packit 67cb25
        do 
Packit 67cb25
          {
Packit 67cb25
            end++ ;  /* skip over trailing whitespace */
Packit 67cb25
          } 
Packit 67cb25
        while (*end == ' ' || *end == ',') ;
Packit 67cb25
      }
Packit 67cb25
        
Packit 67cb25
    new_precision = 0 ; 
Packit 67cb25
    new_rounding = 0 ; 
Packit 67cb25
    new_exception = 0 ;
Packit 67cb25
Packit 67cb25
    status = lookup_string (p, &new_precision, &new_rounding, &new_exception) ;
Packit 67cb25
Packit 67cb25
    if (status)
Packit 67cb25
      {
Packit 67cb25
        free(start);
Packit 67cb25
        GSL_ERROR ("unrecognized GSL_IEEE_MODE string.\nValid settings are:\n\n" 
Packit 67cb25
                   "  single-precision double-precision extended-precision\n"
Packit 67cb25
                   "  round-to-nearest round-down round-up round-to-zero\n"
Packit 67cb25
                   "  mask-invalid mask-denormalized mask-division-by-zero\n"
Packit 67cb25
                   "  mask-overflow mask-underflow mask-all\n"
Packit 67cb25
                   "  trap-common trap-inexact\n"
Packit 67cb25
                   "\n"
Packit 67cb25
                   "separated by commas. "
Packit 67cb25
                   "(e.g. GSL_IEEE_MODE=\"round-down,mask-underflow\")",
Packit 67cb25
                   GSL_EINVAL) ;
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
    if (new_precision) 
Packit 67cb25
      {
Packit 67cb25
        *precision = new_precision ;
Packit 67cb25
        precision_count ++ ;
Packit 67cb25
        if (precision_count > 1)
Packit 67cb25
          {
Packit 67cb25
            free(start);
Packit 67cb25
            GSL_ERROR ("attempted to set IEEE precision twice", GSL_EINVAL) ;
Packit 67cb25
          }
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
    if (new_rounding) 
Packit 67cb25
      {
Packit 67cb25
        *rounding = new_rounding ;
Packit 67cb25
        rounding_count ++ ;
Packit 67cb25
        if (rounding_count > 1)
Packit 67cb25
          {
Packit 67cb25
            free(start);
Packit 67cb25
            GSL_ERROR ("attempted to set IEEE rounding mode twice", GSL_EINVAL) ;
Packit 67cb25
          }
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
    if (new_exception) 
Packit 67cb25
      {
Packit 67cb25
        *exception_mask |= new_exception ;
Packit 67cb25
        exception_count ++ ;
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
    p = end ; 
Packit 67cb25
Packit 67cb25
  } while (end && *p != '\0') ;
Packit 67cb25
Packit 67cb25
  free(start) ;
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS ;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static int 
Packit 67cb25
lookup_string (const char * p, int * precision, int * rounding, 
Packit 67cb25
               int * exception_mask)
Packit 67cb25
{
Packit 67cb25
  if (strcmp(p,"single-precision") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *precision = GSL_IEEE_SINGLE_PRECISION ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"double-precision") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *precision = GSL_IEEE_DOUBLE_PRECISION ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"extended-precision") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *precision = GSL_IEEE_EXTENDED_PRECISION ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"round-to-nearest") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *rounding = GSL_IEEE_ROUND_TO_NEAREST ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"round-down") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *rounding = GSL_IEEE_ROUND_DOWN ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"round-up") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *rounding = GSL_IEEE_ROUND_UP ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"round-to-zero") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *rounding = GSL_IEEE_ROUND_TO_ZERO ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"mask-all") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *exception_mask = GSL_IEEE_MASK_ALL ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"mask-invalid") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *exception_mask = GSL_IEEE_MASK_INVALID ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"mask-denormalized") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *exception_mask = GSL_IEEE_MASK_DENORMALIZED ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"mask-division-by-zero") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *exception_mask = GSL_IEEE_MASK_DIVISION_BY_ZERO ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"mask-overflow") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *exception_mask = GSL_IEEE_MASK_OVERFLOW ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"mask-underflow") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *exception_mask = GSL_IEEE_MASK_UNDERFLOW ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"trap-inexact") == 0) 
Packit 67cb25
    {
Packit 67cb25
      *exception_mask = GSL_IEEE_TRAP_INEXACT ;
Packit 67cb25
    }
Packit 67cb25
  else if (strcmp(p,"trap-common") == 0) 
Packit 67cb25
    {
Packit 67cb25
      return 0 ;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      return 1 ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return 0 ;
Packit 67cb25
}