[libc-commits] [libc] [libc][math][c23] Add nextupl and nextdownl functions (PR #85484)

via libc-commits libc-commits at lists.llvm.org
Fri Mar 15 20:09:04 PDT 2024


================
@@ -0,0 +1,56 @@
+//===-- nextupdown implementation for x86 long double numbers ---*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEXTUPDOWNLONGDOUBLE_H
+#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEXTUPDOWNLONGDOUBLE_H
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/properties/architectures.h"
+
+#if !defined(LIBC_TARGET_ARCH_IS_X86)
+#error "Invalid include"
+#endif
+
+namespace LIBC_NAMESPACE::fputil {
+
+template <bool IsDown>
+LIBC_INLINE constexpr long double nextupdown(long double x) {
+  constexpr Sign sign = IsDown ? Sign::NEG : Sign::POS;
+
+  using FPBits_t = FPBits<long double>;
+  FPBits_t xbits(x);
+  if (xbits.is_nan() || xbits == FPBits_t::max_normal(sign) ||
+      xbits == FPBits_t::inf(sign))
+    return x;
+
+  using StorageType = typename FPBits_t::StorageType;
+  if (x == 0.0l) {
+    xbits = FPBits_t::min_subnormal(sign);
----------------
lntue wrote:

Probably using early return can reduce the nested if-else a bit:
```
if (x == 0.0l)
  return FPBits_t::min_subnormal(sign).get_val();

if (xbits.sign() == sign) ...
```

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


More information about the libc-commits mailing list