(git:27c966b)
Loading...
Searching...
No Matches
double_double.F
Go to the documentation of this file.
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! **************************************************************************************************
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
35 PUBLIC :: dd_add, dd_sub, dd_mul, dd_div
37
38! **************************************************************************************************
39!> \brief Double-double number hi + lo with |lo| <= ulp(hi)/2
40! **************************************************************************************************
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! **************************************************************************************************
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
56CONTAINS
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 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 s = a + b
72 bb = s - a
73 e = (a - (s - bb)) + (b - bb)
74
75 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 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 s = a + b
89 e = b - (s - a)
90
91 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 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 p = a*b
107 a_hi = transfer(iand(transfer(a, 0_int_8), dd_split_mask), 0.0_dp)
108 a_lo = a - a_hi
109 b_hi = transfer(iand(transfer(b, 0_int_8), dd_split_mask), 0.0_dp)
110 b_lo = b - b_hi
111 e = ((a_hi*b_hi - p) + a_hi*b_lo + a_lo*b_hi) + a_lo*b_lo
112
113 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 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 CALL two_sum(a%hi, b%hi, s, e)
128 CALL two_sum(a%lo, b%lo, t, f)
129 e = e + t
130 CALL quick_two_sum(s, e, s2, e2)
131 e2 = e2 + f
132 CALL quick_two_sum(s2, e2, c%hi, c%lo)
133
134 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 ELEMENTAL FUNCTION dd_sub(a, b) RESULT(c)
143 TYPE(dd_type), INTENT(IN) :: a, b
144 TYPE(dd_type) :: c
145
146 c = dd_add(a, dd_type(-b%hi, -b%lo))
147
148 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 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 CALL two_prod(a%hi, b%hi, p, e)
163 e = e + (a%hi*b%lo + a%lo*b%hi)
164 CALL quick_two_sum(p, e, c%hi, c%lo)
165
166 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 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 q1 = a%hi/b%hi
182 r = dd_sub(a, dd_mul(dd_type(q1, 0.0_dp), b))
183 q2 = r%hi/b%hi
184 r = dd_sub(r, dd_mul(dd_type(q2, 0.0_dp), b))
185 q3 = r%hi/b%hi
186 CALL quick_two_sum(q1, q2, q%hi, q%lo)
187 c = dd_add(q, dd_type(q3, 0.0_dp))
188
189 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 ELEMENTAL FUNCTION zdd(z) RESULT(c)
197 COMPLEX(KIND=dp), INTENT(IN) :: z
198 TYPE(zdd_type) :: c
199
200 c%re = dd_type(real(z, kind=dp), 0.0_dp)
201 c%im = dd_type(aimag(z), 0.0_dp)
202
203 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 ELEMENTAL FUNCTION zdd_to_dp(a) RESULT(z)
211 TYPE(zdd_type), INTENT(IN) :: a
212 COMPLEX(KIND=dp) :: z
213
214 z = cmplx(a%re%hi + a%re%lo, a%im%hi + a%im%lo, kind=dp)
215
216 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 ELEMENTAL FUNCTION zdd_add(a, b) RESULT(c)
225 TYPE(zdd_type), INTENT(IN) :: a, b
226 TYPE(zdd_type) :: c
227
228 c%re = dd_add(a%re, b%re)
229 c%im = dd_add(a%im, b%im)
230
231 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 ELEMENTAL FUNCTION zdd_sub(a, b) RESULT(c)
240 TYPE(zdd_type), INTENT(IN) :: a, b
241 TYPE(zdd_type) :: c
242
243 c%re = dd_sub(a%re, b%re)
244 c%im = dd_sub(a%im, b%im)
245
246 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 ELEMENTAL FUNCTION zdd_mul(a, b) RESULT(c)
255 TYPE(zdd_type), INTENT(IN) :: a, b
256 TYPE(zdd_type) :: c
257
258 c%re = dd_sub(dd_mul(a%re, b%re), dd_mul(a%im, b%im))
259 c%im = dd_add(dd_mul(a%re, b%im), dd_mul(a%im, b%re))
260
261 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 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 d = dd_add(dd_mul(b%re, b%re), dd_mul(b%im, b%im))
276 c%re = dd_div(dd_add(dd_mul(a%re, b%re), dd_mul(a%im, b%im)), d)
277 c%im = dd_div(dd_sub(dd_mul(a%im, b%re), dd_mul(a%re, b%im)), d)
278
279 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 ELEMENTAL FUNCTION zdd_conjg(a) RESULT(c)
287 TYPE(zdd_type), INTENT(IN) :: a
288 TYPE(zdd_type) :: c
289
290 c%re = a%re
291 c%im = dd_type(-a%im%hi, -a%im%lo)
292
293 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 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 s = dd_add(dd_mul(a%re, a%re), dd_mul(a%im, a%im))
307 r = s%hi + s%lo
308
309 END FUNCTION zdd_abs2
310
311END MODULE double_double
Double-double arithmetic: real and complex numbers stored as unevaluated sums hi + lo of two double-p...
elemental type(zdd_type) function, public zdd_mul(a, b)
Product of two complex double-double numbers.
elemental type(dd_type) function, public dd_div(a, b)
Quotient of two double-double numbers, by three steps of long division.
elemental type(dd_type) function, public dd_sub(a, b)
Difference of two double-double numbers.
type(zdd_type), parameter, public zdd_zero
elemental type(zdd_type) function, public zdd(z)
Complex double-double number from a double-precision one.
elemental type(dd_type) function, public dd_add(a, b)
Sum of two double-double numbers.
elemental type(dd_type) function, public dd_mul(a, b)
Product of two double-double numbers.
type(zdd_type), parameter, public zdd_i
type(zdd_type), parameter, public zdd_one
elemental type(zdd_type) function, public zdd_conjg(a)
Complex conjugate of a complex double-double number.
elemental type(zdd_type) function, public zdd_add(a, b)
Sum of two complex double-double numbers.
elemental type(zdd_type) function, public zdd_div(a, b)
Quotient of two complex double-double numbers.
elemental real(kind=dp) function, public zdd_abs2(a)
Squared modulus of a complex double-double number, rounded to double precision.
elemental type(zdd_type) function, public zdd_sub(a, b)
Difference of two complex double-double numbers.
elemental complex(kind=dp) function, public zdd_to_dp(a)
Rounds a complex double-double number to double precision.
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
Double-double number hi + lo with |lo| <= ulp(hi)/2.
Complex double-double number.