gsl_ieee-utils__read.c 4.9 KB

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