The function below is used in my program the most.
CONTAINS
SUBROUTINE Delta4(R,Del)
REAL(DP), DIMENSION(:), INTENT(IN) :: R
REAL(DP), DIMENSION(:), INTENT(OUT) :: Del
INTEGER(I4B) :: i, j, r_n, c_n
REAL(DP) :: ar
!r_n=size(R,1);
Del=0.0_dp
do i=1,4; ar=ABS(R(i))
if (ar <= 1.0_dp) then
Del(i)=(3.0_dp-2.0_dp*ar+ &
sqrt(1.0_dp+4.0_dp*ar-4.0_dp*ar*ar))*0.125_dp
else !! if (1.0_dp < ar .and. ar <= 2.0_dp) then
Del(i)=(5.0_dp-2.0_dp*ar- &
sqrt(-7.0_dp+12.0_dp*ar-4.0_dp*ar*ar))*0.125_dp
end if
end do
R, Del : length 4 vector
So I want to improve the speed of this function.
I as far as know, the if-else branch is slow. Furthermore, it is in a do loop.
How can I optimize it?