Add `logp1`/`expm1` instrinsics to LLVM
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
There are already LLVM intrinsics for `log/log2/log10` and `exp/exp2/exp10`, so I thought it would also be good to add `logp1`/`log1p` and `expm1` intrinsics:
- `logp1` computes `log(x + 1)` without losing precision when `x` is close to zero.
- `expm1` computes `exp(x) - 1` without losing precision when `x` is close to zero.
Proposed intrinsics:
- `logp1`: `log(x + 1)` (This is the C23 spelling)
- `log2p1`: `log2(x + 1)`
- `log10p1`: `log10(x + 1)`
- `expm1`: `exp(x) - 1`
- `exp2m1`: `exp2(x) - 1`
- `exp10m1`: `exp10(x) - 1`
It seems like `MLIR` already uses the C99 spelling `log1p`, although it does not provide a `log2p1` or `log10p1` intrinsic. So we would have to decide on which spelling of `logp1`/`log1p` `MLIR` and `LLVM` will use.
***
Some floating point units have a dedicated instruction for these intrinsics:
The x87 FPU provides the `FYL2XP1` and `F2XM1` instructions http://www.infophysics.net/x87.pdf file:///C:/Users/zerico/Downloads/253666-sdm-vol-2a.pdf:
- `FYL2XP1` calculates `y * log2(x + 1)` provided `-0.2928 < x < +0.2928` or `-(1 - sqrt(2)/2) < x < +(1 - sqrt(2)/2)` (although some sources say that it works provided `x >= -1.0` on newer x87 FPU's?)
- `F2XM1` on the x87 FPU calculates `2^x - 1` provided `-1.0 <= x <= +1.0`
The Motorola 68881/68882 FPU provides the `FLOGNP1` and `FETOXM1` instructions: https://www.manualslib.com/manual/2299719/Freescale-Semiconductor-Mc68881.html?page=61#manual https://media.digikey.com/PDF/Data%20Sheets/Freescale%20Semi/MC68882.pdf
- `FLOGNP1` calculates `log(x + 1)`
- `FETOXM1` calculates `exp(x) - 1`
***
A related function that could also be added is the C23 `compoundn` function which calculates `(x + 1)^n` without losing precision when `x` is close to zero. It can be implemented as `exp(n * logp1(x))`. It is quite similar to the `powi` LLVM intrinsic (`pown` in C23) function which calculates `x^n`.
Contributor guide
Research direction
Start by comparing the existing LLVM log/log2/log10 and exp/exp2/exp10 intrinsics with MLIR's existing log1p support. Resolve the logp1/log1p spelling and the proposed intrinsic scope across LLVM and MLIR, then confirm that the agreed set is consistently represented and documented.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100