gsl_ieee-utils__fp-gnux86.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* ieee-utils/fp-gnux86.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 <stdio.h>
  20. #include <fpu_control.h>
  21. #include "gsl_errno.h"
  22. #include "gsl_ieee_utils.h"
  23. /* Handle libc5, where _FPU_SETCW is not available, suggested by
  24. OKUJI Yoshinori <okuji@gnu.org> and Evgeny Stambulchik
  25. <fnevgeny@plasma-gate.weizmann.ac.il> */
  26. #ifndef _FPU_SETCW
  27. #include <i386/fpu_control.h>
  28. #define _FPU_SETCW(cw) __setfpucw(cw)
  29. #endif
  30. int
  31. gsl_ieee_set_mode (int precision, int rounding, int exception_mask)
  32. {
  33. unsigned short mode = 0 ;
  34. switch (precision)
  35. {
  36. case GSL_IEEE_SINGLE_PRECISION:
  37. mode |= _FPU_SINGLE ;
  38. break ;
  39. case GSL_IEEE_DOUBLE_PRECISION:
  40. mode |= _FPU_DOUBLE ;
  41. break ;
  42. case GSL_IEEE_EXTENDED_PRECISION:
  43. mode |= _FPU_EXTENDED ;
  44. break ;
  45. default:
  46. mode |= _FPU_EXTENDED ;
  47. }
  48. switch (rounding)
  49. {
  50. case GSL_IEEE_ROUND_TO_NEAREST:
  51. mode |= _FPU_RC_NEAREST ;
  52. break ;
  53. case GSL_IEEE_ROUND_DOWN:
  54. mode |= _FPU_RC_DOWN ;
  55. break ;
  56. case GSL_IEEE_ROUND_UP:
  57. mode |= _FPU_RC_UP ;
  58. break ;
  59. case GSL_IEEE_ROUND_TO_ZERO:
  60. mode |= _FPU_RC_ZERO ;
  61. break ;
  62. default:
  63. mode |= _FPU_RC_NEAREST ;
  64. }
  65. if (exception_mask & GSL_IEEE_MASK_INVALID)
  66. mode |= _FPU_MASK_IM ;
  67. if (exception_mask & GSL_IEEE_MASK_DENORMALIZED)
  68. mode |= _FPU_MASK_DM ;
  69. if (exception_mask & GSL_IEEE_MASK_DIVISION_BY_ZERO)
  70. mode |= _FPU_MASK_ZM ;
  71. if (exception_mask & GSL_IEEE_MASK_OVERFLOW)
  72. mode |= _FPU_MASK_OM ;
  73. if (exception_mask & GSL_IEEE_MASK_UNDERFLOW)
  74. mode |= _FPU_MASK_UM ;
  75. if (exception_mask & GSL_IEEE_TRAP_INEXACT)
  76. {
  77. mode &= ~ _FPU_MASK_PM ;
  78. }
  79. else
  80. {
  81. mode |= _FPU_MASK_PM ;
  82. }
  83. _FPU_SETCW(mode) ;
  84. #if HAVE_FPU_X86_SSE
  85. #define _FPU_SETMXCSR(cw_sse) asm volatile ("ldmxcsr %0" : : "m" (*&cw_sse))
  86. {
  87. unsigned int mode_sse = 0;
  88. mode_sse |= (mode & 0x3f)<<7; /* exception masks */
  89. mode_sse |= (mode & 0xc00)<<3; /* rounding control */
  90. _FPU_SETMXCSR(mode_sse);
  91. }
  92. #endif
  93. return GSL_SUCCESS ;
  94. }