Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Double-double arithmetic: real and complex numbers stored as unevaluated sums hi + lo of
10 : !> two double-precision numbers, about 31 significant digits, following Y. Hida, X. S. Li,
11 : !> D. H. Bailey, Proc. 15th IEEE Symposium on Computer Arithmetic, 155 (2001).
12 : !> \note
13 : !> The error-free transformations rely on IEEE double-precision rounding of every single
14 : !> operation. Contraction to fused multiply-adds (e.g. gfortran with -march=native) leaves
15 : !> the results correct but reduces the precision of the product to about 77 bits;
16 : !> -ffp-contract=off restores the full precision.
17 : !> \par History
18 : !> 11.2026 moved out of gw_nevanlinna
19 : ! **************************************************************************************************
20 : MODULE double_double
21 : USE kinds, ONLY: dp,&
22 : int_8
23 :
24 : IMPLICIT NONE
25 :
26 : PRIVATE
27 :
28 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'double_double'
29 :
30 : ! clears the lowest 27 of the 52 stored mantissa bits; splitting by bit masking instead of
31 : ! Dekker's multiplication keeps the split exact when the compiler contracts to FMA
32 : INTEGER(KIND=int_8), PARAMETER :: dd_split_mask = -134217728_int_8
33 :
34 : PUBLIC :: dd_type, zdd_type, zdd_zero, zdd_one, zdd_i
35 : PUBLIC :: dd_add, dd_sub, dd_mul, dd_div
36 : PUBLIC :: zdd, zdd_to_dp, zdd_add, zdd_sub, zdd_mul, zdd_div, zdd_conjg, zdd_abs2
37 :
38 : ! **************************************************************************************************
39 : !> \brief Double-double number hi + lo with |lo| <= ulp(hi)/2
40 : ! **************************************************************************************************
41 : TYPE dd_type
42 : REAL(KIND=dp) :: hi = 0.0_dp, lo = 0.0_dp
43 : END TYPE dd_type
44 :
45 : ! **************************************************************************************************
46 : !> \brief Complex double-double number
47 : ! **************************************************************************************************
48 : TYPE zdd_type
49 : TYPE(dd_type) :: re = dd_type(), im = dd_type()
50 : END TYPE zdd_type
51 :
52 : TYPE(zdd_type), PARAMETER :: zdd_zero = zdd_type(dd_type(0.0_dp, 0.0_dp), dd_type(0.0_dp, 0.0_dp)), &
53 : zdd_one = zdd_type(dd_type(1.0_dp, 0.0_dp), dd_type(0.0_dp, 0.0_dp)), &
54 : zdd_i = zdd_type(dd_type(0.0_dp, 0.0_dp), dd_type(1.0_dp, 0.0_dp))
55 :
56 : CONTAINS
57 :
58 : ! **************************************************************************************************
59 : !> \brief Error-free sum, a + b = s + e exactly
60 : !> \param a summand
61 : !> \param b summand
62 : !> \param s rounded sum
63 : !> \param e rounding error
64 : ! **************************************************************************************************
65 160462472 : ELEMENTAL SUBROUTINE two_sum(a, b, s, e)
66 : REAL(KIND=dp), INTENT(IN) :: a, b
67 : REAL(KIND=dp), INTENT(OUT) :: s, e
68 :
69 : REAL(KIND=dp) :: bb
70 :
71 160462472 : s = a + b
72 160462472 : bb = s - a
73 160462472 : e = (a - (s - bb)) + (b - bb)
74 :
75 160462472 : END SUBROUTINE two_sum
76 :
77 : ! **************************************************************************************************
78 : !> \brief Error-free sum for |a| >= |b|
79 : !> \param a larger summand
80 : !> \param b smaller summand
81 : !> \param s rounded sum
82 : !> \param e rounding error
83 : ! **************************************************************************************************
84 253103162 : ELEMENTAL SUBROUTINE quick_two_sum(a, b, s, e)
85 : REAL(KIND=dp), INTENT(IN) :: a, b
86 : REAL(KIND=dp), INTENT(OUT) :: s, e
87 :
88 253103162 : s = a + b
89 253103162 : e = b - (s - a)
90 :
91 253103162 : END SUBROUTINE quick_two_sum
92 :
93 : ! **************************************************************************************************
94 : !> \brief Error-free product, a*b = p + e to within a relative 2**(-106)
95 : !> \param a factor
96 : !> \param b factor
97 : !> \param p rounded product
98 : !> \param e rounding error
99 : ! **************************************************************************************************
100 81194736 : ELEMENTAL SUBROUTINE two_prod(a, b, p, e)
101 : REAL(KIND=dp), INTENT(IN) :: a, b
102 : REAL(KIND=dp), INTENT(OUT) :: p, e
103 :
104 : REAL(KIND=dp) :: a_hi, a_lo, b_hi, b_lo
105 :
106 81194736 : p = a*b
107 81194736 : a_hi = TRANSFER(IAND(TRANSFER(a, 0_int_8), dd_split_mask), 0.0_dp)
108 81194736 : a_lo = a - a_hi
109 81194736 : b_hi = TRANSFER(IAND(TRANSFER(b, 0_int_8), dd_split_mask), 0.0_dp)
110 81194736 : b_lo = b - b_hi
111 81194736 : e = ((a_hi*b_hi - p) + a_hi*b_lo + a_lo*b_hi) + a_lo*b_lo
112 :
113 81194736 : END SUBROUTINE two_prod
114 :
115 : ! **************************************************************************************************
116 : !> \brief Sum of two double-double numbers
117 : !> \param a summand
118 : !> \param b summand
119 : !> \return a + b
120 : ! **************************************************************************************************
121 80231236 : ELEMENTAL FUNCTION dd_add(a, b) RESULT(c)
122 : TYPE(dd_type), INTENT(IN) :: a, b
123 : TYPE(dd_type) :: c
124 :
125 : REAL(KIND=dp) :: e, e2, f, s, s2, t
126 :
127 80231236 : CALL two_sum(a%hi, b%hi, s, e)
128 80231236 : CALL two_sum(a%lo, b%lo, t, f)
129 80231236 : e = e + t
130 80231236 : CALL quick_two_sum(s, e, s2, e2)
131 80231236 : e2 = e2 + f
132 80231236 : CALL quick_two_sum(s2, e2, c%hi, c%lo)
133 :
134 80231236 : END FUNCTION dd_add
135 :
136 : ! **************************************************************************************************
137 : !> \brief Difference of two double-double numbers
138 : !> \param a minuend
139 : !> \param b subtrahend
140 : !> \return a - b
141 : ! **************************************************************************************************
142 47607176 : ELEMENTAL FUNCTION dd_sub(a, b) RESULT(c)
143 : TYPE(dd_type), INTENT(IN) :: a, b
144 : TYPE(dd_type) :: c
145 :
146 47607176 : c = dd_add(a, dd_type(-b%hi, -b%lo))
147 :
148 47607176 : END FUNCTION dd_sub
149 :
150 : ! **************************************************************************************************
151 : !> \brief Product of two double-double numbers
152 : !> \param a factor
153 : !> \param b factor
154 : !> \return a*b
155 : ! **************************************************************************************************
156 81194736 : ELEMENTAL FUNCTION dd_mul(a, b) RESULT(c)
157 : TYPE(dd_type), INTENT(IN) :: a, b
158 : TYPE(dd_type) :: c
159 :
160 : REAL(KIND=dp) :: e, p
161 :
162 81194736 : CALL two_prod(a%hi, b%hi, p, e)
163 81194736 : e = e + (a%hi*b%lo + a%lo*b%hi)
164 81194736 : CALL quick_two_sum(p, e, c%hi, c%lo)
165 :
166 81194736 : END FUNCTION dd_mul
167 :
168 : ! **************************************************************************************************
169 : !> \brief Quotient of two double-double numbers, by three steps of long division
170 : !> \param a dividend
171 : !> \param b divisor
172 : !> \return a/b
173 : ! **************************************************************************************************
174 11445954 : ELEMENTAL FUNCTION dd_div(a, b) RESULT(c)
175 : TYPE(dd_type), INTENT(IN) :: a, b
176 : TYPE(dd_type) :: c
177 :
178 : REAL(KIND=dp) :: q1, q2, q3
179 : TYPE(dd_type) :: q, r
180 :
181 11445954 : q1 = a%hi/b%hi
182 11445954 : r = dd_sub(a, dd_mul(dd_type(q1, 0.0_dp), b))
183 11445954 : q2 = r%hi/b%hi
184 11445954 : r = dd_sub(r, dd_mul(dd_type(q2, 0.0_dp), b))
185 11445954 : q3 = r%hi/b%hi
186 11445954 : CALL quick_two_sum(q1, q2, q%hi, q%lo)
187 11445954 : c = dd_add(q, dd_type(q3, 0.0_dp))
188 :
189 11445954 : END FUNCTION dd_div
190 :
191 : ! **************************************************************************************************
192 : !> \brief Complex double-double number from a double-precision one
193 : !> \param z complex number
194 : !> \return z
195 : ! **************************************************************************************************
196 1004023 : ELEMENTAL FUNCTION zdd(z) RESULT(c)
197 : COMPLEX(KIND=dp), INTENT(IN) :: z
198 : TYPE(zdd_type) :: c
199 :
200 1004023 : c%re = dd_type(REAL(z, KIND=dp), 0.0_dp)
201 1004023 : c%im = dd_type(AIMAG(z), 0.0_dp)
202 :
203 1004023 : END FUNCTION zdd
204 :
205 : ! **************************************************************************************************
206 : !> \brief Rounds a complex double-double number to double precision
207 : !> \param a complex double-double number
208 : !> \return a in double precision
209 : ! **************************************************************************************************
210 1171428 : ELEMENTAL FUNCTION zdd_to_dp(a) RESULT(z)
211 : TYPE(zdd_type), INTENT(IN) :: a
212 : COMPLEX(KIND=dp) :: z
213 :
214 1171428 : z = CMPLX(a%re%hi + a%re%lo, a%im%hi + a%im%lo, KIND=dp)
215 :
216 1171428 : END FUNCTION zdd_to_dp
217 :
218 : ! **************************************************************************************************
219 : !> \brief Sum of two complex double-double numbers
220 : !> \param a summand
221 : !> \param b summand
222 : !> \return a + b
223 : ! **************************************************************************************************
224 1004023 : ELEMENTAL FUNCTION zdd_add(a, b) RESULT(c)
225 : TYPE(zdd_type), INTENT(IN) :: a, b
226 : TYPE(zdd_type) :: c
227 :
228 1004023 : c%re = dd_add(a%re, b%re)
229 1004023 : c%im = dd_add(a%im, b%im)
230 :
231 1004023 : END FUNCTION zdd_add
232 :
233 : ! **************************************************************************************************
234 : !> \brief Difference of two complex double-double numbers
235 : !> \param a minuend
236 : !> \param b subtrahend
237 : !> \return a - b
238 : ! **************************************************************************************************
239 7366957 : ELEMENTAL FUNCTION zdd_sub(a, b) RESULT(c)
240 : TYPE(zdd_type), INTENT(IN) :: a, b
241 : TYPE(zdd_type) :: c
242 :
243 7366957 : c%re = dd_sub(a%re, b%re)
244 7366957 : c%im = dd_sub(a%im, b%im)
245 :
246 7366957 : END FUNCTION zdd_sub
247 :
248 : ! **************************************************************************************************
249 : !> \brief Product of two complex double-double numbers
250 : !> \param a factor
251 : !> \param b factor
252 : !> \return a*b
253 : ! **************************************************************************************************
254 4258377 : ELEMENTAL FUNCTION zdd_mul(a, b) RESULT(c)
255 : TYPE(zdd_type), INTENT(IN) :: a, b
256 : TYPE(zdd_type) :: c
257 :
258 4258377 : c%re = dd_sub(dd_mul(a%re, b%re), dd_mul(a%im, b%im))
259 4258377 : c%im = dd_add(dd_mul(a%re, b%im), dd_mul(a%im, b%re))
260 :
261 4258377 : END FUNCTION zdd_mul
262 :
263 : ! **************************************************************************************************
264 : !> \brief Quotient of two complex double-double numbers
265 : !> \param a dividend
266 : !> \param b divisor
267 : !> \return a/b
268 : ! **************************************************************************************************
269 5722977 : ELEMENTAL FUNCTION zdd_div(a, b) RESULT(c)
270 : TYPE(zdd_type), INTENT(IN) :: a, b
271 : TYPE(zdd_type) :: c
272 :
273 : TYPE(dd_type) :: d
274 :
275 5722977 : d = dd_add(dd_mul(b%re, b%re), dd_mul(b%im, b%im))
276 5722977 : c%re = dd_div(dd_add(dd_mul(a%re, b%re), dd_mul(a%im, b%im)), d)
277 5722977 : c%im = dd_div(dd_sub(dd_mul(a%im, b%re), dd_mul(a%re, b%im)), d)
278 :
279 5722977 : END FUNCTION zdd_div
280 :
281 : ! **************************************************************************************************
282 : !> \brief Complex conjugate of a complex double-double number
283 : !> \param a complex double-double number
284 : !> \return conjugate of a
285 : ! **************************************************************************************************
286 4258377 : ELEMENTAL FUNCTION zdd_conjg(a) RESULT(c)
287 : TYPE(zdd_type), INTENT(IN) :: a
288 : TYPE(zdd_type) :: c
289 :
290 4258377 : c%re = a%re
291 4258377 : c%im = dd_type(-a%im%hi, -a%im%lo)
292 :
293 4258377 : END FUNCTION zdd_conjg
294 :
295 : ! **************************************************************************************************
296 : !> \brief Squared modulus of a complex double-double number, rounded to double precision
297 : !> \param a complex double-double number
298 : !> \return |a|**2
299 : ! **************************************************************************************************
300 3465729 : ELEMENTAL FUNCTION zdd_abs2(a) RESULT(r)
301 : TYPE(zdd_type), INTENT(IN) :: a
302 : REAL(KIND=dp) :: r
303 :
304 : TYPE(dd_type) :: s
305 :
306 3465729 : s = dd_add(dd_mul(a%re, a%re), dd_mul(a%im, a%im))
307 3465729 : r = s%hi + s%lo
308 :
309 3465729 : END FUNCTION zdd_abs2
310 :
311 0 : END MODULE double_double
|