1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/* ieee-utils/read.c
*
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_ieee_utils.h>
static int
lookup_string (const char * p, int * precision, int * rounding,
int * exception_mask) ;
int
gsl_ieee_read_mode_string (const char * description,
int * precision,
int * rounding,
int * exception_mask)
{
char * start ;
char * end;
char * p;
int precision_count = 0 ;
int rounding_count = 0 ;
int exception_count = 0 ;
start = (char *) malloc(strlen(description) + 1) ;
if (start == 0)
{
GSL_ERROR ("no memory to parse mode string", GSL_ENOMEM) ;
}
strcpy (start, description) ;
p = start ;
*precision = 0 ;
*rounding = 0 ;
*exception_mask = 0 ;
do {
int status ;
int new_precision, new_rounding, new_exception ;
end = strchr (p,',') ;
if (end)
{
*end = '\0' ;
do
{
end++ ; /* skip over trailing whitespace */
}
while (*end == ' ' || *end == ',') ;
}
new_precision = 0 ;
new_rounding = 0 ;
new_exception = 0 ;
status = lookup_string (p, &new_precision, &new_rounding, &new_exception) ;
if (status)
{
free(start);
GSL_ERROR ("unrecognized GSL_IEEE_MODE string.\nValid settings are:\n\n"
" single-precision double-precision extended-precision\n"
" round-to-nearest round-down round-up round-to-zero\n"
" mask-invalid mask-denormalized mask-division-by-zero\n"
" mask-overflow mask-underflow mask-all\n"
" trap-common trap-inexact\n"
"\n"
"separated by commas. "
"(e.g. GSL_IEEE_MODE=\"round-down,mask-underflow\")",
GSL_EINVAL) ;
}
if (new_precision)
{
*precision = new_precision ;
precision_count ++ ;
if (precision_count > 1)
{
free(start);
GSL_ERROR ("attempted to set IEEE precision twice", GSL_EINVAL) ;
}
}
if (new_rounding)
{
*rounding = new_rounding ;
rounding_count ++ ;
if (rounding_count > 1)
{
free(start);
GSL_ERROR ("attempted to set IEEE rounding mode twice", GSL_EINVAL) ;
}
}
if (new_exception)
{
*exception_mask |= new_exception ;
exception_count ++ ;
}
p = end ;
} while (end && *p != '\0') ;
free(start) ;
return GSL_SUCCESS ;
}
static int
lookup_string (const char * p, int * precision, int * rounding,
int * exception_mask)
{
if (strcmp(p,"single-precision") == 0)
{
*precision = GSL_IEEE_SINGLE_PRECISION ;
}
else if (strcmp(p,"double-precision") == 0)
{
*precision = GSL_IEEE_DOUBLE_PRECISION ;
}
else if (strcmp(p,"extended-precision") == 0)
{
*precision = GSL_IEEE_EXTENDED_PRECISION ;
}
else if (strcmp(p,"round-to-nearest") == 0)
{
*rounding = GSL_IEEE_ROUND_TO_NEAREST ;
}
else if (strcmp(p,"round-down") == 0)
{
*rounding = GSL_IEEE_ROUND_DOWN ;
}
else if (strcmp(p,"round-up") == 0)
{
*rounding = GSL_IEEE_ROUND_UP ;
}
else if (strcmp(p,"round-to-zero") == 0)
{
*rounding = GSL_IEEE_ROUND_TO_ZERO ;
}
else if (strcmp(p,"mask-all") == 0)
{
*exception_mask = GSL_IEEE_MASK_ALL ;
}
else if (strcmp(p,"mask-invalid") == 0)
{
*exception_mask = GSL_IEEE_MASK_INVALID ;
}
else if (strcmp(p,"mask-denormalized") == 0)
{
*exception_mask = GSL_IEEE_MASK_DENORMALIZED ;
}
else if (strcmp(p,"mask-division-by-zero") == 0)
{
*exception_mask = GSL_IEEE_MASK_DIVISION_BY_ZERO ;
}
else if (strcmp(p,"mask-overflow") == 0)
{
*exception_mask = GSL_IEEE_MASK_OVERFLOW ;
}
else if (strcmp(p,"mask-underflow") == 0)
{
*exception_mask = GSL_IEEE_MASK_UNDERFLOW ;
}
else if (strcmp(p,"trap-inexact") == 0)
{
*exception_mask = GSL_IEEE_TRAP_INEXACT ;
}
else if (strcmp(p,"trap-common") == 0)
{
return 0 ;
}
else
{
return 1 ;
}
return 0 ;
}
|