ddcoremath.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Double-double common routines used in correctly rounded implementations.
  2. Copyright (c) 2023-2025 Alexei Sibidanov.
  3. The original version of this file was copied from the CORE-MATH
  4. project (file src/binary64/acosh/acosh.c, revision 1bd85b89).
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE. */
  20. #ifndef _DD_COREMATH_H
  21. #define _DD_COREMATH_H
  22. /* References:
  23. [1] Tight and rigourous error bounds for basic building blocks of
  24. double-word arithmetic, by Mioara Joldeş, Jean-Michel Muller,
  25. and Valentina Popescu, ACM Transactions on Mathematical Software,
  26. 44(2), 2017.
  27. */
  28. static inline double
  29. fasttwosum (double x, double y, double *e)
  30. {
  31. double s = x + y, z = s - x;
  32. *e = y - z;
  33. return s;
  34. }
  35. static inline double
  36. fasttwosub (double x, double y, double *e)
  37. {
  38. double s = x - y, z = x - s;
  39. *e = z - y;
  40. return s;
  41. }
  42. static inline double
  43. twosum (double x, double y, double *e)
  44. {
  45. if (__glibc_likely (fabs (x) > fabs (y)))
  46. return fasttwosum (x, y, e);
  47. else
  48. return fasttwosum (y, x, e);
  49. }
  50. static inline double
  51. fastsum (double xh, double xl, double yh, double yl, double *e)
  52. {
  53. double sl, sh = fasttwosum (xh, yh, &sl);
  54. *e = (xl + yl) + sl;
  55. return sh;
  56. }
  57. static inline double
  58. sumdd (double xh, double xl, double yh, double yl, double *e)
  59. {
  60. double sl, sh;
  61. if (__glibc_likely (fabs (xh) > fabs (yh)))
  62. sh = fasttwosum (xh, yh, &sl);
  63. else
  64. sh = fasttwosum (yh, xh, &sl);
  65. *e = (xl + yl) + sl;
  66. return sh;
  67. }
  68. static inline double
  69. adddd (double xh, double xl, double ch, double cl, double *l)
  70. {
  71. double s = xh + ch, d = s - xh;
  72. *l = ((ch - d) + (xh + (d - s))) + (xl + cl);
  73. return s;
  74. }
  75. /* This function implements Algorithm 10 (DWTimesDW1) from [1]
  76. Its relative error (for round-to-nearest ties-to-even) is bounded by 5u^2
  77. (Theorem 2.6 of [2]), where u = 2^-53 for double precision,
  78. assuming xh = RN(xh + xl), which implies |xl| <= 1/2 ulp(xh),
  79. and similarly for ch, cl. */
  80. static inline double
  81. muldd_acc (double xh, double xl, double ch, double cl, double *l)
  82. {
  83. double ahlh = ch * xl, alhh = cl * xh, ahhh = ch * xh,
  84. ahhl = fma (ch, xh, -ahhh);
  85. ahhl += alhh + ahlh;
  86. ch = ahhh + ahhl;
  87. *l = (ahhh - ch) + ahhl;
  88. return ch;
  89. }
  90. /* Note: in revision 085972b, we replaced the last two lines
  91. ch = ahhh + ahhl and *l = (ahhh - ch) + ahhl by fasttwosum (ahhh, ahhl, l).
  92. Indeed, these last two lines did emulate a FastTwoSum.
  93. However, they did emulate another variant of fasttwosum, with
  94. z = x - s and e = z + y.
  95. Note that these two variants differ when the fasttwosum condition
  96. |x| >= |y| is not satisfied.
  97. Take for example with precision 3 and rounding upwards, x=-7 and
  98. y=28. Then s = RU(x + y) = 24. With the first variant, z = RU(s-x) = 32
  99. and e = RU(y - z) = -4, thus s + e = 20. With the second variant,
  100. z = RU(x-s) = -28 and e = RU(z + y) = 0, thus s + e = 24. In this case,
  101. the first variant is closer to the sum x + y = 21.
  102. Still with precision 3 and rounding upwards, now take x=7 and
  103. y=-28. Then s = RU(x + y) = -20. With the first variant, z = RU(s-x) = -24
  104. and e = RU(y - z) = -4, thus s + e = -28. With the second variant,
  105. z = RU(x-s) = 28 and e = RU(z + y) = 0, thus s + e = -20. In this case,
  106. the second variant is closer to the sum x + y = -21.
  107. */
  108. static inline double
  109. muldd_acc2 (double xh, double xl, double ch, double cl, double *l)
  110. {
  111. double ahlh = ch * xl, alhh = cl * xh, ahhh = ch * xh,
  112. ahhl = fma (ch, xh, -ahhh);
  113. ahhl += alhh + ahlh;
  114. return fasttwosum (ahhh, ahhl, l);
  115. }
  116. static inline double
  117. muldd2 (double xh, double xl, double ch, double cl, double *l)
  118. {
  119. double ahhh = ch * xh;
  120. *l = (ch * xl + cl * xh) + fma (ch, xh, -ahhh);
  121. return ahhh;
  122. }
  123. static inline double
  124. muldd3 (double xh, double xl, double yh, double yl, double *l)
  125. {
  126. double ch = xh * yh, cl1 = fma (xh, yh, -ch);
  127. double tl0 = xl * yl;
  128. double tl1 = tl0 + xh * yl;
  129. double cl2 = tl1 + xl * yh;
  130. double cl3 = cl1 + cl2;
  131. return fasttwosum (ch, cl3, l);
  132. }
  133. static inline double
  134. mulddd (double xh, double xl, double ch, double *l)
  135. {
  136. double ahlh = ch * xl, ahhh = ch * xh, ahhl = fma (ch, xh, -ahhh);
  137. ahhl += ahlh;
  138. ch = ahhh + ahhl;
  139. *l = (ahhh - ch) + ahhl;
  140. return ch;
  141. }
  142. static inline double
  143. mulddd2 (double x, double ch, double cl, double *l)
  144. {
  145. double ahhh = ch * x;
  146. *l = cl * x + fma (ch, x, -ahhh);
  147. return ahhh;
  148. }
  149. static inline double mulddd3 (double xh, double xl, double ch, double *l)
  150. {
  151. double hh = xh * ch;
  152. *l = fma (ch, xh, -hh) + xl * ch;
  153. return hh;
  154. }
  155. static inline double
  156. polydd (double xh, double xl, int n, const double c[][2], double *l)
  157. {
  158. int i = n - 1;
  159. double ch, cl;
  160. ch = fasttwosum (c[i][0], *l, &cl);
  161. cl += c[i][1];
  162. while (--i >= 0)
  163. {
  164. ch = muldd_acc (xh, xl, ch, cl, &cl);
  165. double th = ch + c[i][0], tl = (c[i][0] - th) + ch;
  166. ch = th;
  167. cl += tl + c[i][1];
  168. }
  169. *l = cl;
  170. return ch;
  171. }
  172. static inline double
  173. polydd2 (double xh, double xl, int n, const double c[][2], double *l)
  174. {
  175. int i = n - 1;
  176. double cl, ch = fasttwosum (c[i][0], *l, &cl);
  177. cl += c[i][1];
  178. while (--i >= 0)
  179. {
  180. ch = muldd2 (xh, xl, ch, cl, &cl);
  181. ch = fastsum (c[i][0], c[i][1], ch, cl, &cl);
  182. }
  183. *l = cl;
  184. return ch;
  185. }
  186. static inline double
  187. polydd3 (double xh, double xl, int n, const double c[][2], double *l)
  188. {
  189. int i = n - 1;
  190. double ch, cl;
  191. ch = fasttwosum (c[i][0], *l, &cl);
  192. cl += c[i][1];
  193. while(--i>=0){
  194. ch = muldd_acc2 (xh, xl, ch, cl, &cl);
  195. double th, tl;
  196. th = fasttwosum (c[i][0], ch, &tl);
  197. ch = th;
  198. cl += tl + c[i][1];
  199. }
  200. *l = cl;
  201. return ch;
  202. }
  203. static inline double
  204. polyddd (double x, int n, const double c[][2], double *l)
  205. {
  206. int i = n - 1;
  207. double cl, ch = fasttwosum (c[i][0], *l, &cl);
  208. cl += c[i][1];
  209. while (--i >= 0)
  210. {
  211. ch = mulddd2 (x, ch, cl, &cl);
  212. ch = sumdd (c[i][0], c[i][1], ch, cl, &cl);
  213. }
  214. *l = cl;
  215. return ch;
  216. }
  217. static inline double
  218. polydddfst (double x, int n, const double c[][2], double *l)
  219. {
  220. int i = n - 1;
  221. double cl, ch = fasttwosum (c[i][0], *l, &cl);
  222. cl += c[i][1];
  223. while (--i >= 0)
  224. {
  225. ch = mulddd2 (x, ch, cl, &cl);
  226. ch = fastsum (c[i][0], c[i][1], ch, cl, &cl);
  227. }
  228. *l = cl;
  229. return ch;
  230. }
  231. static inline double
  232. polyd (double x, int n, const double c[][2])
  233. {
  234. int i = n - 1;
  235. double ch = c[i][0];
  236. while (--i >= 0)
  237. ch = c[i][0] + x * ch;
  238. return ch;
  239. }
  240. #endif