sfp-exceptions.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. *
  4. * This file is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 3, or (at your option) any
  7. * later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * Under Section 7 of GPL version 3, you are granted additional
  15. * permissions described in the GCC Runtime Library Exception, version
  16. * 3.1, as published by the Free Software Foundation.
  17. *
  18. * You should have received a copy of the GNU General Public License and
  19. * a copy of the GCC Runtime Library Exception along with this program;
  20. * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. */
  23. #include "sfp-machine.h"
  24. void
  25. __sfp_handle_exceptions (int _fex)
  26. {
  27. const float fp_max = __FLT_MAX__;
  28. const float fp_min = __FLT_MIN__;
  29. const float fp_1e32 = 1.0e32f;
  30. const float fp_zero = 0.0;
  31. const float fp_one = 1.0;
  32. unsigned fpsr;
  33. if (_fex & FP_EX_INVALID)
  34. {
  35. __asm__ __volatile__ ("fdiv\ts0, %s0, %s0"
  36. :
  37. : "w" (fp_zero)
  38. : "s0");
  39. __asm__ __volatile__ ("mrs\t%0, fpsr" : "=r" (fpsr));
  40. }
  41. if (_fex & FP_EX_DIVZERO)
  42. {
  43. __asm__ __volatile__ ("fdiv\ts0, %s0, %s1"
  44. :
  45. : "w" (fp_one), "w" (fp_zero)
  46. : "s0");
  47. __asm__ __volatile__ ("mrs\t%0, fpsr" : "=r" (fpsr));
  48. }
  49. if (_fex & FP_EX_OVERFLOW)
  50. {
  51. __asm__ __volatile__ ("fadd\ts0, %s0, %s1"
  52. :
  53. : "w" (fp_max), "w" (fp_1e32)
  54. : "s0");
  55. __asm__ __volatile__ ("mrs\t%0, fpsr" : "=r" (fpsr));
  56. }
  57. if (_fex & FP_EX_UNDERFLOW)
  58. {
  59. __asm__ __volatile__ ("fmul\ts0, %s0, %s0"
  60. :
  61. : "w" (fp_min)
  62. : "s0");
  63. __asm__ __volatile__ ("mrs\t%0, fpsr" : "=r" (fpsr));
  64. }
  65. if (_fex & FP_EX_INEXACT)
  66. {
  67. __asm__ __volatile__ ("fsub\ts0, %s0, %s1"
  68. :
  69. : "w" (fp_max), "w" (fp_one)
  70. : "s0");
  71. __asm__ __volatile__ ("mrs\t%0, fpsr" : "=r" (fpsr));
  72. }
  73. }