Dayuxiaoshui commented on issue #18580:
URL: https://github.com/apache/tvm/issues/18580#issuecomment-3644733984

   Hi @dutZ1855 @tlopex ,
   
   Thanks for reporting this! I've reproduced the issue and confirmed it's a 
real bug in the ACOS implementation for the LLVM target.
   
   **What's happening:**
   
   The ACOS operation is implemented as `acos(x) = π/2 - asin(x)` in the LLVM 
codegen (`src/target/llvm/intrin_rule_llvm.cc`). The problem is that ASIN uses 
a 6-term Taylor series expansion, which converges very slowly near the boundary 
values `x=±1.0`, causing significant precision errors.
   
   I verified this with a test script. For `x=1.0`:
   - The Taylor series computes `asin(1.0) ≈ 1.215922` 
   - The correct value should be `π/2 ≈ 1.570796`
   - This gives an error of `0.354874` (about 22.6% relative error)
   - So: `acos(1.0) = π/2 - 1.215922 = 0.354874` ❌ (should be `0.0`)
   
   The same issue occurs for `x=-1.0`, where `acos(-1.0) ≈ 2.786719` instead of 
`π ≈ 3.141593`.
   
   **Affected code locations:**
   - `src/target/llvm/intrin_rule_llvm.cc:167-189` - ASIN Taylor series (6 
terms)
   - `src/target/llvm/intrin_rule_llvm.cc:191-200` - ACOS that depends on ASIN
   
   **Impact:**
   The precision degrades as values approach the boundaries:
   - `|x| = 0.9`: ~6.3% error
   - `|x| = 0.99`: ~16.2% error  
   - `|x| = 1.0`: ~22.6% error
   
   **Potential fix:**
   The most straightforward solution would be to use the system library 
function (like `acosf`) for values near `±1.0` (e.g., `|x| > 0.9`), while 
keeping the Taylor series for values in the middle range where it's accurate. 
This is similar to how other targets handle trigonometric functions.
   
   I can work on a patch if that sounds good. Let me know if you'd like me to 
proceed!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to