Skip to content

[libc] Add exceptional values for sinhf16/coshf16 #133002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025

Conversation

michaelrj-google
Copy link
Contributor

The rounding of the result when using an FMA instruction for hyperbolic
sin/cos on float16 was off by 1 bit for a few cases. This patch adds
extra exceptional cases to handle these.

The rounding of the result when using an FMA instruction for hyperbolic
sin/cos on float16 was off by 1 bit for a few cases. This patch adds
extra exceptional cases to handle these.
@llvmbot
Copy link
Member

llvmbot commented Mar 25, 2025

@llvm/pr-subscribers-libc

Author: Michael Jones (michaelrj-google)

Changes

The rounding of the result when using an FMA instruction for hyperbolic
sin/cos on float16 was off by 1 bit for a few cases. This patch adds
extra exceptional cases to handle these.


Full diff: https://github.com/llvm/llvm-project/pull/133002.diff

2 Files Affected:

  • (modified) libc/src/math/generic/coshf16.cpp (+5-1)
  • (modified) libc/src/math/generic/sinhf16.cpp (+6-2)
diff --git a/libc/src/math/generic/coshf16.cpp b/libc/src/math/generic/coshf16.cpp
index 6668e77000f0c..689d16a55260f 100644
--- a/libc/src/math/generic/coshf16.cpp
+++ b/libc/src/math/generic/coshf16.cpp
@@ -42,7 +42,7 @@ static constexpr fputil::ExceptValues<float16, 9> COSHF16_EXCEPTS_POS = {{
     {0x497cU, 0x7715U, 1U, 0U, 1U},
 }};
 
-static constexpr fputil::ExceptValues<float16, 4> COSHF16_EXCEPTS_NEG = {{
+static constexpr fputil::ExceptValues<float16, 6> COSHF16_EXCEPTS_NEG = {{
     // x = -0x1.6ap-5, coshf16(x) = 0x1p+0 (RZ)
     {0xa9a8U, 0x3c00U, 1U, 0U, 1U},
     // x = -0x1.b6p+0, coshf16(x) = 0x1.6d8p+1 (RZ)
@@ -51,6 +51,10 @@ static constexpr fputil::ExceptValues<float16, 4> COSHF16_EXCEPTS_NEG = {{
     {0xc4a2U, 0x526dU, 1U, 0U, 0U},
     // x = -0x1.5fp+3, coshf16(x) = 0x1.c54p+14 (RZ)
     {0xc97cU, 0x7715U, 1U, 0U, 1U},
+    // x = -0x1.8c4p+0, coshf16(x) = 0x1.3a8p+1 (RZ)
+    {0xbe31U, 0x40eaU, 1U, 0U, 0U},
+    // x = -0x1.994p+0, coshf16(x) = 0x1.498p+1 (RZ)
+    {0xbe65U, 0x4126U, 1U, 0U, 0U},
 }};
 #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
 
diff --git a/libc/src/math/generic/sinhf16.cpp b/libc/src/math/generic/sinhf16.cpp
index 680e1cc49a1d8..b426ea78b98c0 100644
--- a/libc/src/math/generic/sinhf16.cpp
+++ b/libc/src/math/generic/sinhf16.cpp
@@ -21,7 +21,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-static constexpr fputil::ExceptValues<float16, 16> SINHF16_EXCEPTS_POS = {{
+static constexpr fputil::ExceptValues<float16, 17> SINHF16_EXCEPTS_POS = {{
     // x = 0x1.714p-5, sinhf16(x) = 0x1.714p-5 (RZ)
     {0x29c5U, 0x29c5U, 1U, 0U, 1U},
     // x = 0x1.25p-4, sinhf16(x) = 0x1.25p-4 (RZ)
@@ -54,9 +54,11 @@ static constexpr fputil::ExceptValues<float16, 16> SINHF16_EXCEPTS_POS = {{
     {0x4629U, 0x5b65U, 1U, 0U, 1U},
     // x = 0x1.5fp+3, sinhf16(x) = 0x1.c54p+14 (RZ)
     {0x497cU, 0x7715U, 1U, 0U, 1U},
+    // x = 0x1.3c8p+1, sinhf16(x) = 0x1.78ap+2 (RZ)
+    {0x40f2U, 0x45e2U, 1U, 0U, 1U},
 }};
 
-static constexpr fputil::ExceptValues<float16, 12> SINHF16_EXCEPTS_NEG = {{
+static constexpr fputil::ExceptValues<float16, 13> SINHF16_EXCEPTS_NEG = {{
     // x = -0x1.714p-5, sinhf16(x) = -0x1.714p-5 (RZ)
     {0xa9c5U, 0xa9c5U, 0U, 1U, 1U},
     // x = -0x1.25p-4, sinhf16(x) = -0x1.25p-4 (RZ)
@@ -81,6 +83,8 @@ static constexpr fputil::ExceptValues<float16, 12> SINHF16_EXCEPTS_NEG = {{
     {0xc629U, 0xdb65U, 0U, 1U, 1U},
     // x = -0x1.5fp+3, sinhf16(x) = -0x1.c54p+14 (RZ)
     {0xc97cU, 0xf715U, 0U, 1U, 1U},
+    // x = -0x1.3c8p+1, sinhf16(x) = -0x1.78ap+2 (RZ)
+    {0xc0f2U, 0xc5e2U, 0U, 1U, 1U},
 }};
 #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
 

@michaelrj-google michaelrj-google merged commit c995db3 into llvm:main Mar 25, 2025
18 checks passed
@michaelrj-google michaelrj-google deleted the libcExceptionalTrig16 branch March 25, 2025 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants