[libc-commits] [libc] [libc][math][c23] Add f16divf C23 math function (PR #96131)

via libc-commits libc-commits at lists.llvm.org
Thu Jun 20 08:59:16 PDT 2024


================
@@ -0,0 +1,180 @@
+//===-- Division of IEEE 754 floating-point 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_GENERIC_DIV_H
+#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_DIV_H
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/CPP/type_traits.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+
+namespace LIBC_NAMESPACE::fputil::generic {
+
+template <typename OutType, typename InType>
+cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
+                     cpp::is_floating_point_v<InType> &&
+                     sizeof(OutType) <= sizeof(InType),
+                 OutType>
+div(InType x, InType y) {
+  using OutFPBits = FPBits<OutType>;
+  using OutStorageType = typename OutFPBits::StorageType;
+  using InFPBits = FPBits<InType>;
+  using InStorageType = typename InFPBits::StorageType;
+  using DyadicFloat =
+      DyadicFloat<cpp::bit_ceil(static_cast<size_t>(InFPBits::FRACTION_LEN))>;
+  using DyadicMantissaType = typename DyadicFloat::MantissaType;
+
+  // +1 for the implicit bit.
+  constexpr int DYADIC_EXTRA_MANTISSA_LEN =
+      DyadicMantissaType::BITS - (InFPBits::FRACTION_LEN + 1);
+  // +1 for the extra fractional bit in q.
+  constexpr int Q_EXTRA_FRACTION_LEN =
+      InFPBits::FRACTION_LEN + 1 - OutFPBits::FRACTION_LEN;
+
+  InFPBits x_bits(x);
+  InFPBits y_bits(y);
+
+  if (x_bits.is_nan() || y_bits.is_nan()) {
+    if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan())
----------------
overmighty wrote:

I pushed the change to `if (LIBC_UNLIKELY(...))`. It is indeed faster:

```
 Performance tests with inputs in denormal range:
-- My function --
     Total time      : 590356165 ns 
     Average runtime : 29.5178 ns/op 
     Ops per second  : 33877887 op/s 
-- Other function --
     Total time      : 612659035 ns 
     Average runtime : 30.6329 ns/op 
     Ops per second  : 32644617 op/s 
-- Average runtime ratio --
     Mine / Other's  : 0.963597 

 Performance tests with inputs in normal range:
-- My function --
     Total time      : 145477558 ns 
     Average runtime : 7.27387 ns/op 
     Ops per second  : 137478386 op/s 
-- Other function --
     Total time      : 172587200 ns 
     Average runtime : 8.62935 ns/op 
     Ops per second  : 115883564 op/s 
-- Average runtime ratio --
     Mine / Other's  : 0.842922 

 Performance tests with inputs in normal range with exponents close to each other:
-- My function --
     Total time      : 522964890 ns 
     Average runtime : 26.1482 ns/op 
     Ops per second  : 38243523 op/s 
-- Other function --
     Total time      : 550152260 ns 
     Average runtime : 27.5076 ns/op 
     Ops per second  : 36353608 op/s 
-- Average runtime ratio --
     Mine / Other's  : 0.950582 
```

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


More information about the libc-commits mailing list