Skip to content

Commit 17f1c40

Browse files
committed
Cuberoot: Apply first iteration explicitly (mom-ocean#11)
Applying the first iteration explicitly appears to speed up the cuberoot function by a bit over 20%: Before: Halley Final: 0.14174999999999999 After: Halley Final: 0.11080000000000001 There is an assumption that compilers will precompute the constants like `0.7 * (0.7)**3`, and that all will do so in the same manner.
1 parent 4813b17 commit 17f1c40

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

src/framework/MOM_intrinsic_functions.F90

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ elemental function cuberoot(x) result(root)
6464
! and it is therefore more computationally efficient.
6565

6666
! This first estimate gives the same magnitude of errors for 0.125 and 1.0 after two iterations.
67-
num = 0.707106 ; den = 1.0
68-
do itt=1,3
67+
! The first iteration is applied explicitly.
68+
num = 0.707106 * (0.707106**3 + 2.0 * asx)
69+
den = 2.0 * (0.707106**3) + asx
70+
71+
do itt=1,2
6972
! Halley's method iterates estimates as Root = Root * (Root**3 + 2.*asx) / (2.*Root**3 + asx).
7073
num_prev = num ; den_prev = den
7174
num = num_prev * (num_prev**3 + 2.0 * asx * (den_prev**3))

0 commit comments

Comments
 (0)