Optimize the if statement style such as in the floor function
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 1.2k
- Forks
- 271
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 173
Description
Original issue: https://gitlab.com/lfortran/lfortran/-/issues/697
This is a very common pattern:
elemental integer(i32) function dfloor_i32(x) result(r)
real(dp), intent(in) :: x
if (x >= 0) then
r = x
else
r = x-1
end if
end function
The if statement can be replaced with:
r = x - (1-sign(x))/2
Where the expression (1-sign(x))/2 is equal to 0 for x>0 and to 1 for x<0 (the x=0 case must also be handled, I am skipping it here). This can be obtained from the sign bit, so I think (1-sign(x))/2 = (x >> 63), so the above if statement can be replaced by just:
r = x - (x >> 63)
The x in x >> 63 might be needed to cast to integer first, and then back to float to subtract.
This is a general approach, that should work for cases like:
if (x >= 0) then
r = x
else
r = x-10
end if
where r is a real number (r = x - 10*(x >> 63)).
In the case above, it is also cast to integer (r is an integer) and I think there might be LLVM or assembly instructions for that directly, so the optimizer should also recognize the above special case as a floor operation and just replace it with a floor intrinsic function, just like we do with fma or flip_sign (all these the user can't enter directly, they are inserted by the optimizer).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the optimizer transformations mentioned for fma and flip_sign, then trace how the dfloor_i32 pattern is represented and tested. Define the supported sign-based cases, including x=0 and the required integer/real casts, and verify that recognized cases lower to the intended floor or equivalent operation without changing semantics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fortran
- Domain
- compilers, performance
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100