q_util.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Software floating-point emulation.
  2. Helper routine for _Q_* routines.
  3. Simulate exceptions using double arithmetics.
  4. Copyright (C) 1999 Free Software Foundation, Inc.
  5. This file is part of the GNU C Library.
  6. Contributed by Jakub Jelinek (jj@ultra.linux.cz).
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library; if not, write to the Free
  17. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA. */
  19. #include "soft-fp.h"
  20. unsigned long long ___Q_numbers [] = {
  21. 0x0000000000000000ULL, /* Zero */
  22. 0x0010100000000000ULL, /* Very tiny number */
  23. 0x0010000000000000ULL, /* Minimum normalized number */
  24. 0x7fef000000000000ULL, /* A huge double number */
  25. };
  26. double ___Q_simulate_exceptions(int exceptions)
  27. {
  28. double d, *p = (double *)___Q_numbers;
  29. if (exceptions & FP_EX_INVALID)
  30. d = p[0]/p[0];
  31. if (exceptions & FP_EX_OVERFLOW)
  32. {
  33. d = p[3] + p[3];
  34. exceptions &= ~FP_EX_INEXACT;
  35. }
  36. if (exceptions & FP_EX_UNDERFLOW)
  37. {
  38. if (exceptions & FP_EX_INEXACT)
  39. {
  40. d = p[2] * p[2];
  41. exceptions &= ~FP_EX_INEXACT;
  42. }
  43. else
  44. d = p[1] - p[2];
  45. }
  46. if (exceptions & FP_EX_DIVZERO)
  47. d = 1.0/p[0];
  48. if (exceptions & FP_EX_INEXACT)
  49. d = p[3] - p[2];
  50. return d;
  51. }