x2y2m1q.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Compute x^2 + y^2 - 1, without large cancellation error.
  2. Copyright (C) 2012 Free Software Foundation, Inc.
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "quadmath-imp.h"
  16. #include <stdlib.h>
  17. /* Calculate X + Y exactly and store the result in *HI + *LO. It is
  18. given that |X| >= |Y| and the values are small enough that no
  19. overflow occurs. */
  20. static inline void
  21. add_split (__float128 *hi, __float128 *lo, __float128 x, __float128 y)
  22. {
  23. /* Apply Dekker's algorithm. */
  24. *hi = x + y;
  25. *lo = (x - *hi) + y;
  26. }
  27. /* Calculate X * Y exactly and store the result in *HI + *LO. It is
  28. given that the values are small enough that no overflow occurs and
  29. large enough (or zero) that no underflow occurs. */
  30. static inline void
  31. mul_split (__float128 *hi, __float128 *lo, __float128 x, __float128 y)
  32. {
  33. /* Fast built-in fused multiply-add. */
  34. *hi = x * y;
  35. *lo = fmaq (x, y, -*hi);
  36. }
  37. /* Compare absolute values of floating-point values pointed to by P
  38. and Q for qsort. */
  39. static int
  40. compare (const void *p, const void *q)
  41. {
  42. __float128 pld = fabsq (*(const __float128 *) p);
  43. __float128 qld = fabsq (*(const __float128 *) q);
  44. if (pld < qld)
  45. return -1;
  46. else if (pld == qld)
  47. return 0;
  48. else
  49. return 1;
  50. }
  51. /* Return X^2 + Y^2 - 1, computed without large cancellation error.
  52. It is given that 1 > X >= Y >= epsilon / 2, and that either X >=
  53. 0.75 or Y >= 0.5. */
  54. __float128
  55. __quadmath_x2y2m1q (__float128 x, __float128 y)
  56. {
  57. __float128 vals[4];
  58. size_t i;
  59. /* FIXME: SET_RESTORE_ROUNDL (FE_TONEAREST); */
  60. mul_split (&vals[1], &vals[0], x, x);
  61. mul_split (&vals[3], &vals[2], y, y);
  62. if (x >= 0.75Q)
  63. vals[1] -= 1.0Q;
  64. else
  65. {
  66. vals[1] -= 0.5Q;
  67. vals[3] -= 0.5Q;
  68. }
  69. qsort (vals, 4, sizeof (__float128), compare);
  70. /* Add up the values so that each element of VALS has absolute value
  71. at most equal to the last set bit of the next nonzero
  72. element. */
  73. for (i = 0; i <= 2; i++)
  74. {
  75. add_split (&vals[i + 1], &vals[i], vals[i + 1], vals[i]);
  76. qsort (vals + i + 1, 3 - i, sizeof (__float128), compare);
  77. }
  78. /* Now any error from this addition will be small. */
  79. return vals[3] + vals[2] + vals[1] + vals[0];
  80. }