[llvm-branch-commits] [libc] [llvm] [libc][math] Implement double precision cosh correctly rounded to all rounding modes. (PR #235181)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Oct 27 23:04:17 PDT 2026


================
@@ -0,0 +1,94 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Double-precision implementation of cosh.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSH_DOUBLE_EVAL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COSH_DOUBLE_EVAL_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/math/sinhcosh_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+namespace double_eval {
+
+// Handle tiny (|x| <= 2^-27), overflow (|x| >= 0x1.633ce8fb9f87ep+9), inf, and
+// nan for cosh(x):
+LIBC_NOINLINE LIBC_INLINE double cosh_exceptional(double x) {
+  using FPBits = fputil::FPBits<double>;
+  FPBits xbits(x);
+
+  // Tiny and subnormal range: |x| <= 2^-27.
+  // For 0 < |x| <= 2^-27:
+  //   0 < cosh(x) - 1 ~ x^2 / 2 < 2^-55 < 0.5 * ulp(1.0),
+  // so cosh(x) rounds to 1.0 + 2^-52 when rounding mode is FE_UPWARD and to
+  // 1.0 otherwise, with FE_INEXACT raised for all x != 0.
+  if (xbits.abs().uintval() <= 0x3e40'0000'0000'0000ULL) {
----------------
lntue wrote:

Done.

https://github.com/llvm/llvm-project/pull/235181


More information about the llvm-branch-commits mailing list