sfp-exceptions.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (C) 2016-2022 Free Software Foundation, Inc.
  2. This file is free software; you can redistribute it and/or modify it
  3. under the terms of the GNU General Public License as published by the
  4. Free Software Foundation; either version 3, or (at your option) any
  5. later version.
  6. This file is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. General Public License for more details.
  10. Under Section 7 of GPL version 3, you are granted additional
  11. permissions described in the GCC Runtime Library Exception, version
  12. 3.1, as published by the Free Software Foundation.
  13. You should have received a copy of the GNU General Public License and
  14. a copy of the GCC Runtime Library Exception along with this program;
  15. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "sfp-machine.h"
  18. /* Only provide exception support if we have hardware floating point and we can
  19. execute the mtfsf instruction. This would only be true if we are using the
  20. emulation routines for IEEE 128-bit floating point on pre-ISA 3.0 machines
  21. without the IEEE 128-bit floating point support. */
  22. #ifndef __NO_FPRS__
  23. void
  24. __sfp_handle_exceptions (int _fex)
  25. {
  26. const double fp_max = __DBL_MAX__;
  27. const double fp_min = __DBL_MIN__;
  28. const double fp_zero = (double) 0.0;
  29. const double fp_one = 1.0;
  30. double tmp;
  31. if (_fex & FP_EX_INVALID)
  32. {
  33. __asm__ __volatile__ ("fdiv %0, %1, %1"
  34. : "=f" (tmp)
  35. : "f" (fp_zero));
  36. }
  37. if (_fex & FP_EX_DIVZERO)
  38. {
  39. __asm__ __volatile__ ("fdiv %0, %1, %2"
  40. : "=f" (tmp)
  41. : "f" (fp_one), "f" (fp_zero));
  42. }
  43. if (_fex & FP_EX_OVERFLOW)
  44. {
  45. __asm__ __volatile__ ("fadd %0, %1, %1"
  46. : "=f" (tmp)
  47. : "f" (fp_max));
  48. }
  49. if (_fex & FP_EX_UNDERFLOW)
  50. {
  51. __asm__ __volatile__ ("fmul %0, %1, %1"
  52. : "=f" (tmp)
  53. : "f" (fp_min));
  54. }
  55. if (_fex & FP_EX_INEXACT)
  56. {
  57. __asm__ __volatile__ ("fsub %0, %1, %2"
  58. : "=f" (tmp)
  59. : "f" (fp_max), "f" (fp_one));
  60. }
  61. }
  62. #endif /* !__NO_FPRS__ */