mul_split.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Compute full X * Y for double type.
  2. Copyright (C) 2013-2019 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. #ifndef _MUL_SPLIT_H
  16. #define _MUL_SPLIT_H
  17. #include <float.h>
  18. /* Calculate X * Y exactly and store the result in *HI + *LO. It is
  19. given that the values are small enough that no overflow occurs and
  20. large enough (or zero) that no underflow occurs. */
  21. static void
  22. mul_split (double *hi, double *lo, double x, double y)
  23. {
  24. #ifdef __FP_FAST_FMA
  25. /* Fast built-in fused multiply-add. */
  26. *hi = x * y;
  27. *lo = __builtin_fma (x, y, -*hi);
  28. #else
  29. /* Apply Dekker's algorithm. */
  30. *hi = x * y;
  31. # define C ((1 << (DBL_MANT_DIG + 1) / 2) + 1)
  32. double x1 = x * C;
  33. double y1 = y * C;
  34. # undef C
  35. x1 = (x - x1) + x1;
  36. y1 = (y - y1) + y1;
  37. double x2 = x - x1;
  38. double y2 = y - y1;
  39. *lo = (((x1 * y1 - *hi) + x1 * y2) + x2 * y1) + x2 * y2;
  40. #endif
  41. }
  42. #endif /* _MUL_SPLIT_H */