llvm / llvm/llvm-project

[KnownFPClass] Assume `cos(x)` cannot return zero or subnormal

Open
#219,735 0 comments 0 reactions 0 assignees View on GitHub
floating-point llvm:support
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

There are 4 special deductions that we can perform on `sin`, `cos`, and `tan`:
1. `cos(x)` is never zero or subnormal
2. `sin(x)` is never zero or subnormal if `|x| > 0.1`
3. `tan(x)` is never zero or subnormal if `|x| > 0.1`
4. `tan(x)` is never infinity (we already make this deduction, but not the others)

Related to https://github.com/llvm/llvm-project/issues/219714

`cos(x)` is only zero if `x` is of the form `pi/2 + pi * n` where `n` is an integer. It is possible that there is no floating point value close enough to `pi/2 + pi * n` to cause `cos(x)` to return a value that is zero or subnormal. Therefore we may be able to unconditionally rule those floating point classes out.

Similarly, `sin(x)` and `tan(x)` are only zero if `x` is a number of the form `pi * n` where `n` is an integer. If `x` is sufficiently close to zero, then the result will be zero or subnormal, but if `x` is large enough (say `|x| > 0.1`), then it is possible that `x` will never get close enough to a multiple of `pi` to permit `sin(x)` or `tan(x)` to become zero or subnormal.

To summarize, all of these deductions rely on the fact that a floating point number can never approximate a multiple of `pi/2` too closely. Therefore, for consistency purposes, we should apply all 4 deductions, or apply none of the 4 deductions.

Contributor guide

Open the contributing guide

Research direction

No source file, test, or entry point is named. Start by locating the KnownFPClass handling for sin, cos, and tan, then review related issue 219714 and the floating-point reasoning in this report. Done means consistently applying all four stated deductions or applying none of them, with tests covering the resulting classifications.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.