fraiseexcpt.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Raise given exceptions. OpenRISC version.
  2. Copyright (C) 2024-2025 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <fenv.h>
  15. #include <fpu_control.h>
  16. #include <float.h>
  17. #include <math.h>
  18. int
  19. feraiseexcept (int excepts)
  20. {
  21. if (excepts == 0)
  22. return 0;
  23. /* Raise exceptions represented by EXPECTS. */
  24. if (excepts & FE_INEXACT)
  25. {
  26. float d = 1.0, x = 3.0;
  27. __asm__ volatile ("lf.div.s %0, %0, %1" : "+r" (d) : "r" (x));
  28. }
  29. if (excepts & FE_UNDERFLOW)
  30. {
  31. float d = FLT_MIN;
  32. __asm__ volatile ("lf.mul.s %0, %0, %0" : "+r" (d));
  33. }
  34. if (excepts & FE_OVERFLOW)
  35. {
  36. float d = FLT_MAX;
  37. __asm__ volatile ("lf.mul.s %0, %0, %0" : "+r" (d) : "r" (d));
  38. }
  39. if (excepts & FE_DIVBYZERO)
  40. {
  41. float d = 1.0, x = 0.0;
  42. __asm__ volatile ("lf.div.s %0, %0, %1" : "+r" (d) : "r" (x));
  43. }
  44. if (excepts & FE_INVALID)
  45. {
  46. float d = HUGE_VAL, x = 0.0;
  47. __asm__ volatile ("lf.mul.s %0, %1, %0" : "+r" (d) : "r" (x));
  48. }
  49. /* Success. */
  50. return 0;
  51. }