fraiseexcpt.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Raise given exceptions.
  2. Copyright (C) 2013 Imagination Technologies Ltd.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <fenv.h>
  17. #include <math.h>
  18. libm_hidden_proto(feraiseexcept)
  19. int
  20. feraiseexcept (int excepts)
  21. {
  22. /* Raise exceptions represented by EXPECTS. But we must raise only
  23. one signal at a time. It is important that if the overflow/underflow
  24. exception and the inexact exception are given at the same time,
  25. the overflow/underflow exception follows the inexact exception. */
  26. /* First: invalid exception. */
  27. if ((FE_INVALID & excepts) != 0)
  28. {
  29. /* Reciprocal square root of a negative number is invalid. */
  30. __asm__ volatile(
  31. "F MOV FX.0,#0xc000 ! -2\n"
  32. "F RSQ FX.1,FX.0\n"
  33. );
  34. }
  35. /* Next: division by zero. */
  36. if ((FE_DIVBYZERO & excepts) != 0)
  37. {
  38. __asm__ volatile(
  39. "F MOV FX.0,#0\n"
  40. "F RCP FX.1,FX.0\n"
  41. );
  42. }
  43. /* Next: overflow. */
  44. if ((FE_OVERFLOW & excepts) != 0)
  45. {
  46. /* Adding a large number in single precision can cause overflow. */
  47. __asm__ volatile(
  48. " MOVT D0.0,#0x7f7f\n"
  49. " ADD D0.0,D0.0,#0xffff\n"
  50. "F MOV FX.0,D0.0\n"
  51. "F ADD FX.1,FX.0,FX.0\n"
  52. );
  53. }
  54. /* Next: underflow. */
  55. if ((FE_UNDERFLOW & excepts) != 0)
  56. {
  57. /* Multiplying a small value by 0.5 will cause an underflow. */
  58. __asm__ volatile(
  59. " MOV D0.0,#1\n"
  60. "F MOV FX.0,D0.0\n"
  61. " MOVT D0.0,#0x3f00\n"
  62. "F MOV FX.1,D0.0\n"
  63. "F MUL FX.2,FX.1,FX.0\n"
  64. );
  65. }
  66. /* Last: inexact. */
  67. if ((FE_INEXACT & excepts) != 0)
  68. {
  69. /* Converting a small single precision value to half precision
  70. can cause an inexact exception. */
  71. __asm__ volatile(
  72. " MOV D0.0,#0x0001\n"
  73. "F MOV FX.0,D0.0\n"
  74. "F FTOH FX.1,FX.0\n"
  75. );
  76. }
  77. /* Success. */
  78. return 0;
  79. }
  80. libm_hidden_def(feraiseexcept)