[libc-commits] [libc] [libc][math][C23] float16 emulation (PR #242947)

via libc-commits libc-commits at lists.llvm.org
Thu Nov 26 15:22:17 PST 2026


https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/242947

>From bb665c67d6af34e55aa36fe1d4804456fce1946c Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Tue, 3 Mar 2026 02:42:07 +0200
Subject: [PATCH 1/7] [libc][math][C23] add software float16 support

---
 libc/config/linux/arm/entrypoints.txt         |   5 +
 .../include/llvm-libc-macros/float16-macros.h |  14 +-
 libc/src/__support/FPUtil/BasicOperations.h   |   4 +
 libc/src/__support/FPUtil/CMakeLists.txt      |  18 +++
 libc/src/__support/FPUtil/Float16.h           | 127 ++++++++++++++++++
 libc/src/__support/macros/properties/types.h  |  12 ++
 libc/src/__support/math/CMakeLists.txt        |   1 +
 libc/src/__support/math/fabsf16.h             |   4 +-
 libc/test/src/math/exhaustive/CMakeLists.txt  |  17 +++
 .../src/math/exhaustive/exhaustive_test.h     |   1 +
 .../test/src/math/exhaustive/float16_test.cpp |  67 +++++++++
 libc/test/src/math/smoke/CMakeLists.txt       |   1 +
 libc/test/src/math/smoke/fabsf16_test.cpp     |   2 +-
 libc/utils/MPFRWrapper/CMakeLists.txt         |   2 +
 libc/utils/MPFRWrapper/MPCommon.cpp           |   7 +-
 libc/utils/MPFRWrapper/MPFRUtils.cpp          |   1 +
 16 files changed, 277 insertions(+), 6 deletions(-)
 create mode 100644 libc/src/__support/FPUtil/Float16.h
 create mode 100644 libc/test/src/math/exhaustive/float16_test.cpp

diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 7a9e287839f9a7..14bf6bb86e3d1e 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -712,6 +712,11 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.ufromfpxl
 )
 
+list(APPEND TARGET_LIBM_ENTRYPOINTS
+  # float16 entrypoints
+  libc.src.math.fabsf16
+)
+
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
diff --git a/libc/include/llvm-libc-macros/float16-macros.h b/libc/include/llvm-libc-macros/float16-macros.h
index 1416b0a658ac8b..04d696c83ce0c4 100644
--- a/libc/include/llvm-libc-macros/float16-macros.h
+++ b/libc/include/llvm-libc-macros/float16-macros.h
@@ -11,11 +11,19 @@
 
 #include "../llvm-libc-types/float128.h"
 
-#if defined(__FLT16_MANT_DIG__) &&                                             \
+#if defined(__arm__) && defined(_M_ARM)
+#define LIBC_USE_SOFT_FLOAT16
+#endif
+
+#ifdef LIBC_USE_SOFT_FLOAT16
+#define LIBC_TYPES_HAS_FLOAT16
+#endif
+
+#if !defined(LIBC_TYPES_HAS_FLOAT16) && defined(__FLT16_MANT_DIG__) &&         \
     (!defined(__GNUC__) || __GNUC__ >= 13 ||                                   \
      (defined(__clang__) && __clang_major__ >= 12)) &&                         \
-    !defined(__arm__) && !defined(_M_ARM) && !defined(__riscv) &&              \
-    !defined(_WIN32) && (!defined(__i386__) || defined(__SSE2__))
+    !defined(__riscv) && !defined(_WIN32) &&                                   \
+    (!defined(__i386__) || defined(__SSE2__))
 #define LIBC_TYPES_HAS_FLOAT16
 
 // TODO: This would no longer be required if HdrGen let us guard function
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index eaf7706965efc4..0b885e75efeba6 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -53,6 +53,7 @@ max(T x, T y) {
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT16
+#if !defined(LIBC_USE_SOFT_FLOAT16)
 #if defined(__LIBC_USE_BUILTIN_FMAXF16_FMINF16)
 template <> LIBC_INLINE constexpr float16 max(float16 x, float16 y) {
   if (cpp::is_constant_evaluated())
@@ -69,6 +70,7 @@ template <> LIBC_INLINE constexpr float16 max(float16 x, float16 y) {
   return ((xi > yi) != (xi < 0 && yi < 0)) ? x : y;
 }
 #endif
+#endif // !LIBC_USE_SOFT_FLOAT16
 #endif // LIBC_TYPES_HAS_FLOAT16
 
 #if defined(__LIBC_USE_BUILTIN_FMAX_FMIN) && !defined(LIBC_TARGET_ARCH_IS_X86)
@@ -106,6 +108,7 @@ min(T x, T y) {
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT16
+#if !defined(LIBC_USE_SOFT_FLOAT16)
 #if defined(__LIBC_USE_BUILTIN_FMAXF16_FMINF16)
 template <> LIBC_INLINE constexpr float16 min(float16 x, float16 y) {
   if (cpp::is_constant_evaluated())
@@ -122,6 +125,7 @@ template <> LIBC_INLINE constexpr float16 min(float16 x, float16 y) {
   return ((xi < yi) != (xi < 0 && yi < 0)) ? x : y;
 }
 #endif
+#endif // !LIBC_USE_SOFT_FLOAT16
 #endif // LIBC_TYPES_HAS_FLOAT16
 
 #if defined(__LIBC_USE_BUILTIN_FMAX_FMIN) && !defined(LIBC_TARGET_ARCH_IS_X86)
diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index e6f2ca19af4174..c68470c4fbdaba 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -58,6 +58,24 @@ add_header_library(
     libc.src.__support.macros.properties.cpu_features
 )
 
+add_header_library(
+  Float16
+  HDRS
+    Float16.h
+  DEPENDS
+    .cast
+    .comparison_operations
+    .dyadic_float
+    libc.hdr.stdint_proxy
+    libc.src.__support.CPP.bit
+    libc.src.__support.CPP.type_traits
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.FPUtil.generic.div
+    libc.src.__support.FPUtil.generic.mul
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_header_library(
   fpbits_str
   HDRS
diff --git a/libc/src/__support/FPUtil/Float16.h b/libc/src/__support/FPUtil/Float16.h
new file mode 100644
index 00000000000000..5387e6a7c1c7a5
--- /dev/null
+++ b/libc/src/__support/FPUtil/Float16.h
@@ -0,0 +1,127 @@
+//===-- Definition of float16 data type. ------------------------*- 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_FLOAT16_H
+#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT16_H
+
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_USE_SOFT_FLOAT16
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/CPP/type_traits.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/comparison_operations.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/FPUtil/generic/div.h"
+#include "src/__support/FPUtil/generic/mul.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace fputil {
+
+struct Float16 {
+  uint16_t bits;
+
+  LIBC_INLINE Float16() = default;
+
+  template <typename T>
+  LIBC_INLINE constexpr explicit Float16(T value)
+      : bits(static_cast<uint16_t>(0U)) {
+    if constexpr (cpp::is_floating_point_v<T>) {
+      bits = fputil::cast<Float16>(value).bits;
+    } else if constexpr (cpp::is_integral_v<T>) {
+      Sign sign = Sign::POS;
+
+      if constexpr (cpp::is_signed_v<T>) {
+        if (value < 0) {
+          sign = Sign::NEG;
+          value = -value;
+        }
+      }
+
+      fputil::DyadicFloat<cpp::numeric_limits<cpp::make_unsigned_t<T>>::digits>
+          xd(sign, 0, value);
+      bits = xd.template as<Float16, /*ShouldSignalExceptions=*/true>().bits;
+
+    } else if constexpr (cpp::is_convertible_v<T, Float16>) {
+      bits = value.operator Float16().bits;
+    }
+  }
+
+  template <cpp::enable_if_t<fputil::get_fp_type<float>() ==
+                                 fputil::FPType::IEEE754_Binary32,
+                             int> = 0>
+  LIBC_INLINE constexpr operator float() const {
+    return fputil::cast<float>(*this);
+  }
+
+  template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
+  LIBC_INLINE constexpr explicit operator T() const {
+    return static_cast<T>(static_cast<float>(*this));
+  }
+
+  LIBC_INLINE bool operator==(Float16 other) const {
+    return fputil::equals(*this, other);
+  }
+
+  LIBC_INLINE bool operator!=(Float16 other) const {
+    return !fputil::equals(*this, other);
+  }
+
+  LIBC_INLINE bool operator<(Float16 other) const {
+    return fputil::less_than(*this, other);
+  }
+
+  LIBC_INLINE bool operator<=(Float16 other) const {
+    return fputil::less_than_or_equals(*this, other);
+  }
+
+  LIBC_INLINE bool operator>(Float16 other) const {
+    return fputil::greater_than(*this, other);
+  }
+
+  LIBC_INLINE bool operator>=(Float16 other) const {
+    return fputil::greater_than_or_equals(*this, other);
+  }
+
+  LIBC_INLINE constexpr Float16 operator-() const {
+    fputil::FPBits<float16> result(*this);
+    result.set_sign(result.is_pos() ? Sign::NEG : Sign::POS);
+    return result.get_val();
+  }
+
+  LIBC_INLINE Float16 operator+(Float16 other) const {
+    return fputil::generic::add<Float16>(*this, other);
+  }
+
+  LIBC_INLINE Float16 operator-(Float16 other) const {
+    return fputil::generic::sub<Float16>(*this, other);
+  }
+
+  LIBC_INLINE Float16 operator*(Float16 other) const {
+    return fputil::generic::mul<float16>(*this, other);
+  }
+
+  LIBC_INLINE Float16 operator/(Float16 other) const {
+    return fputil::generic::div<float16>(*this, other);
+  }
+
+  LIBC_INLINE Float16 &operator*=(const Float16 &other) {
+    *this = *this * other;
+    return *this;
+  }
+}; // struct Float16
+
+} // namespace fputil
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_USE_SOFT_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT16_H
diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index aad4c41005c454..80789791d3d973 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -70,8 +70,20 @@
 // LIBC_TYPES_HAS_FLOAT16 is provided by
 // "include/llvm-libc-macros/float16-macros.h"
 #ifdef LIBC_TYPES_HAS_FLOAT16
+
+#ifdef LIBC_USE_SOFT_FLOAT16
+namespace LIBC_NAMESPACE_DECL {
+namespace fputil {
+struct Float16;
+} // namespace fputil
+} // namespace LIBC_NAMESPACE_DECL
+
+using float16 = LIBC_NAMESPACE::fputil::Float16;
+#else
 // Type alias for internal use.
 using float16 = _Float16;
+#endif
+
 #endif // LIBC_TYPES_HAS_FLOAT16
 
 // -- float128 support --------------------------------------------------------
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c1db38835a508b..0f3118659dfef9 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3553,6 +3553,7 @@ add_header_library(
     fabsf16.h
   DEPENDS
     libc.include.llvm-libc-macros.float16_macros
+    libc.src.__support.FPUtil.Float16
     libc.src.__support.FPUtil.basic_operations
     libc.src.__support.macros.config
   FLAGS
diff --git a/libc/src/__support/math/fabsf16.h b/libc/src/__support/math/fabsf16.h
index 26634361adc730..ca568c680b7c2b 100644
--- a/libc/src/__support/math/fabsf16.h
+++ b/libc/src/__support/math/fabsf16.h
@@ -14,6 +14,7 @@
 #ifdef LIBC_TYPES_HAS_FLOAT16
 
 #include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/Float16.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
@@ -27,7 +28,8 @@ LIBC_INLINE constexpr float16 fabsf16(float16 x) {
   // For x86, GCC generates better code from the generic implementation.
   // https://godbolt.org/z/K9orM4hTa
 #if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) &&                                 \
-    !(defined(LIBC_TARGET_ARCH_IS_X86) && defined(LIBC_COMPILER_IS_GCC))
+    !(defined(LIBC_TARGET_ARCH_IS_X86) && defined(LIBC_COMPILER_IS_GCC)) &&    \
+    !defined(LIBC_USE_SOFT_FLOAT16)
   return __builtin_fabsf16(x);
 #else
   return fputil::abs(x);
diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index d85e5e908fbe5b..160157f156f9d1 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -701,6 +701,23 @@ add_fp_unittest(
     -lpthread
 )
 
+add_fp_unittest(
+  float16_test
+  NO_RUN_POSTBUILD
+  NEED_MPFR
+  SUITE
+    libc_math_exhaustive_tests
+  SRCS
+    float16_test.cpp
+  DEPENDS
+    .exhaustive_test
+    libc.src.__support.FPUtil.Float16
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.fp_bits
+  LINK_LIBRARIES
+    -lpthread
+)
+
 add_fp_unittest(
   bfloat16_test
   NEED_MPFR
diff --git a/libc/test/src/math/exhaustive/exhaustive_test.h b/libc/test/src/math/exhaustive/exhaustive_test.h
index 85c0e07e3417f0..e0179479ac9c57 100644
--- a/libc/test/src/math/exhaustive/exhaustive_test.h
+++ b/libc/test/src/math/exhaustive/exhaustive_test.h
@@ -16,6 +16,7 @@
 
 #include "src/__support/CPP/type_traits.h"
 #include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/Float16.h"
 #include "src/__support/macros/properties/types.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
diff --git a/libc/test/src/math/exhaustive/float16_test.cpp b/libc/test/src/math/exhaustive/float16_test.cpp
new file mode 100644
index 00000000000000..3d1ba1ab805a64
--- /dev/null
+++ b/libc/test/src/math/exhaustive/float16_test.cpp
@@ -0,0 +1,67 @@
+//===-- Exhaustive tests for float -> float16 conversion ------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "exhaustive_test.h"
+#include "src/__support/FPUtil/cast.h"
+#include "utils/MPFRWrapper/MPCommon.h"
+
+using namespace LIBC_NAMESPACE::fputil;
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+template <typename InType>
+struct Float16ConversionChecker : public virtual LIBC_NAMESPACE::testing::Test {
+  using FloatType = InType;
+  using FPBits = LIBC_NAMESPACE::fputil::FPBits<FloatType>;
+  using StorageType = typename FPBits::StorageType;
+
+  // Check in a range, return the number of failures.
+  uint64_t check(StorageType start, StorageType stop,
+                 mpfr::RoundingMode rounding) {
+    mpfr::ForceRoundingMode r(rounding);
+    if (!r.success)
+      return (stop > start);
+    StorageType bits = start;
+    uint64_t failed = 0;
+    do {
+      FPBits x_bits(bits);
+      FloatType x = x_bits.get_val();
+
+      const float16 libc_result = cast<float16>(x);
+      const float16 mpfr_result = mpfr::MPFRNumber(x).as<float16>();
+
+      const bool correct =
+          LIBC_NAMESPACE::testing::getMatcher<
+              LIBC_NAMESPACE::testing::TestCond::EQ>(mpfr_result)
+              .match(libc_result);
+
+      failed += (!correct);
+    } while (bits++ < stop);
+    return failed;
+  }
+};
+
+template <typename FloatType>
+using LlvmLibcFloat16ExhaustiveTest =
+    LlvmLibcExhaustiveMathTest<Float16ConversionChecker<FloatType>>;
+using LlvmLibcFloat16FromFloatTest = LlvmLibcFloat16ExhaustiveTest<float>;
+
+// Positive Range: [0, Inf];
+constexpr uint32_t POS_START = 0x0000'0000U;
+constexpr uint32_t POS_STOP = 0x7f80'0000U;
+
+// Negative Range: [-Inf, 0];
+constexpr uint32_t NEG_START = 0xb000'0000U;
+constexpr uint32_t NEG_STOP = 0xff80'0000U;
+
+TEST_F(LlvmLibcFloat16FromFloatTest, PostiveRange) {
+  test_full_range_all_roundings(POS_START, POS_STOP);
+}
+
+TEST_F(LlvmLibcFloat16FromFloatTest, NegativeRange) {
+  test_full_range_all_roundings(NEG_START, NEG_STOP);
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index fe398e4b1367aa..8b7c8de360fa63 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -253,6 +253,7 @@ add_fp_unittest(
     FAbsTest.h
   DEPENDS
     libc.src.math.fabsf16
+    libc.src.__support.FPUtil.Float16
 )
 
 add_fp_unittest(
diff --git a/libc/test/src/math/smoke/fabsf16_test.cpp b/libc/test/src/math/smoke/fabsf16_test.cpp
index 8402ce28dd383f..fb36af661f48ce 100644
--- a/libc/test/src/math/smoke/fabsf16_test.cpp
+++ b/libc/test/src/math/smoke/fabsf16_test.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "FAbsTest.h"
-
+#include "src/__support/FPUtil/Float16.h"
 #include "src/math/fabsf16.h"
 
 LIST_FABS_TESTS(Fabsf16, float16, LIBC_NAMESPACE::fabsf16)
diff --git a/libc/utils/MPFRWrapper/CMakeLists.txt b/libc/utils/MPFRWrapper/CMakeLists.txt
index 3c301180882a8e..5cdbe5a802cb44 100644
--- a/libc/utils/MPFRWrapper/CMakeLists.txt
+++ b/libc/utils/MPFRWrapper/CMakeLists.txt
@@ -14,6 +14,7 @@ if(LIBC_TESTS_CAN_USE_MPFR OR LIBC_TESTS_CAN_USE_MPC)
     libc.src.__support.CPP.string
     libc.src.__support.CPP.string_view
     libc.src.__support.CPP.type_traits
+    libc.src.__support.FPUtil.Float16
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.cast
     libc.src.__support.FPUtil.float80
@@ -48,6 +49,7 @@ if(LIBC_TESTS_CAN_USE_MPFR)
     libc.hdr.stdint_proxy
     libc.src.__support.CPP.array
     libc.src.__support.CPP.stringstream
+    libc.src.__support.FPUtil.Float16
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.fpbits_str
diff --git a/libc/utils/MPFRWrapper/MPCommon.cpp b/libc/utils/MPFRWrapper/MPCommon.cpp
index 2901bed3914160..5c8c8c0d161287 100644
--- a/libc/utils/MPFRWrapper/MPCommon.cpp
+++ b/libc/utils/MPFRWrapper/MPCommon.cpp
@@ -10,6 +10,11 @@
 
 #include "src/__support/CPP/string_view.h"
 #include "src/__support/FPUtil/bfloat16.h"
+
+#ifdef LIBC_USE_SOFT_FLOAT16
+#include "src/__support/FPUtil/Float16.h"
+#endif
+
 #include "src/__support/FPUtil/cast.h"
 #include "src/__support/FPUtil/float80.h"
 #include "src/__support/macros/config.h"
@@ -633,7 +638,7 @@ template <> long double MPFRNumber::as<long double>() const {
   return mpfr_get_ld(value, mpfr_rounding);
 }
 
-#ifdef LIBC_TYPES_HAS_FLOAT16
+#if defined(LIBC_TYPES_HAS_FLOAT16) || defined(LIBC_USE_SOFT_FLOAT16)
 template <> float16 MPFRNumber::as<float16>() const {
   // TODO: Either prove that this cast won't cause double-rounding errors, or
   // find a better way to get a float16.
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 11bb44abca988b..e19541d879bb42 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -11,6 +11,7 @@
 
 #include "src/__support/CPP/array.h"
 #include "src/__support/CPP/stringstream.h"
+#include "src/__support/FPUtil/Float16.h"
 #include "src/__support/FPUtil/bfloat16.h"
 #include "src/__support/FPUtil/fpbits_str.h"
 #include "src/__support/macros/config.h"

>From 346f785211f1ab34341daa9870a2d6521e3c695c Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Wed, 5 Aug 2026 03:40:11 +0300
Subject: [PATCH 2/7] address reviews

---
 .../include/llvm-libc-macros/float16-macros.h |   2 +-
 .../CPP/type_traits/is_floating_point.h       |   3 +-
 libc/src/__support/FPUtil/BasicOperations.h   |  12 +-
 libc/src/__support/FPUtil/CMakeLists.txt      |  37 +++---
 libc/src/__support/FPUtil/FPBits.h            |   2 +
 libc/src/__support/FPUtil/cast.h              |  22 +++-
 libc/src/__support/FPUtil/dyadic_float.h      |   2 +-
 .../__support/FPUtil/{Float16.h => float16.h} |  55 +++++---
 libc/src/__support/macros/properties/types.h  |   6 +-
 libc/src/__support/math/CMakeLists.txt        |   2 +-
 libc/src/__support/math/fabsf16.h             |   2 +-
 libc/test/src/__support/FPUtil/CMakeLists.txt |  11 ++
 .../src/__support/FPUtil/float16_test.cpp     | 124 ++++++++++++++++++
 libc/test/src/math/exhaustive/CMakeLists.txt  |   2 +-
 .../src/math/exhaustive/exhaustive_test.h     |   2 +-
 libc/test/src/math/smoke/CMakeLists.txt       |   2 +-
 libc/test/src/math/smoke/fabsf16_test.cpp     |   2 +-
 libc/utils/MPFRWrapper/CMakeLists.txt         |   4 +-
 libc/utils/MPFRWrapper/MPCommon.cpp           |  14 +-
 libc/utils/MPFRWrapper/MPCommon.h             |   9 ++
 libc/utils/MPFRWrapper/MPFRUtils.cpp          |   2 +-
 21 files changed, 242 insertions(+), 75 deletions(-)
 rename libc/src/__support/FPUtil/{Float16.h => float16.h} (68%)
 create mode 100644 libc/test/src/__support/FPUtil/float16_test.cpp

diff --git a/libc/include/llvm-libc-macros/float16-macros.h b/libc/include/llvm-libc-macros/float16-macros.h
index 04d696c83ce0c4..01da4baf884a6e 100644
--- a/libc/include/llvm-libc-macros/float16-macros.h
+++ b/libc/include/llvm-libc-macros/float16-macros.h
@@ -11,7 +11,7 @@
 
 #include "../llvm-libc-types/float128.h"
 
-#if defined(__arm__) && defined(_M_ARM)
+#if (defined(__arm__) && defined(_M_ARM)) || defined(__riscv)
 #define LIBC_USE_SOFT_FLOAT16
 #endif
 
diff --git a/libc/src/__support/CPP/type_traits/is_floating_point.h b/libc/src/__support/CPP/type_traits/is_floating_point.h
index 4a5984e38a8cf0..973fd9909b2440 100644
--- a/libc/src/__support/CPP/type_traits/is_floating_point.h
+++ b/libc/src/__support/CPP/type_traits/is_floating_point.h
@@ -42,7 +42,8 @@ template <typename T> struct is_floating_point {
 #endif
                               ,
                               bfloat16
-
+                              ,
+                              fputil::Float16
                               ,
                               fputil::Float128
 
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 0b885e75efeba6..f82ab4ea0a26a6 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -52,8 +52,7 @@ max(T x, T y) {
   return constexpr_max(x, y);
 }
 
-#ifdef LIBC_TYPES_HAS_FLOAT16
-#if !defined(LIBC_USE_SOFT_FLOAT16)
+#if defined(LIBC_TYPES_HAS_FLOAT16) && !defined(LIBC_USE_SOFT_FLOAT16)
 #if defined(__LIBC_USE_BUILTIN_FMAXF16_FMINF16)
 template <> LIBC_INLINE constexpr float16 max(float16 x, float16 y) {
   if (cpp::is_constant_evaluated())
@@ -70,8 +69,7 @@ template <> LIBC_INLINE constexpr float16 max(float16 x, float16 y) {
   return ((xi > yi) != (xi < 0 && yi < 0)) ? x : y;
 }
 #endif
-#endif // !LIBC_USE_SOFT_FLOAT16
-#endif // LIBC_TYPES_HAS_FLOAT16
+#endif // defined(LIBC_TYPES_HAS_FLOAT16) && !defined(LIBC_USE_SOFT_FLOAT16)
 
 #if defined(__LIBC_USE_BUILTIN_FMAX_FMIN) && !defined(LIBC_TARGET_ARCH_IS_X86)
 template <> LIBC_INLINE constexpr float max(float x, float y) {
@@ -107,8 +105,7 @@ min(T x, T y) {
   return constexpr_min(x, y);
 }
 
-#ifdef LIBC_TYPES_HAS_FLOAT16
-#if !defined(LIBC_USE_SOFT_FLOAT16)
+#if defined(LIBC_TYPES_HAS_FLOAT16) && !defined(LIBC_USE_SOFT_FLOAT16)
 #if defined(__LIBC_USE_BUILTIN_FMAXF16_FMINF16)
 template <> LIBC_INLINE constexpr float16 min(float16 x, float16 y) {
   if (cpp::is_constant_evaluated())
@@ -125,8 +122,7 @@ template <> LIBC_INLINE constexpr float16 min(float16 x, float16 y) {
   return ((xi < yi) != (xi < 0 && yi < 0)) ? x : y;
 }
 #endif
-#endif // !LIBC_USE_SOFT_FLOAT16
-#endif // LIBC_TYPES_HAS_FLOAT16
+#endif // defined(LIBC_TYPES_HAS_FLOAT16) && !defined(LIBC_USE_SOFT_FLOAT16)
 
 #if defined(__LIBC_USE_BUILTIN_FMAX_FMIN) && !defined(LIBC_TARGET_ARCH_IS_X86)
 template <> LIBC_INLINE constexpr float min(float x, float y) {
diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index c68470c4fbdaba..f887b0cb09e68e 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -58,24 +58,6 @@ add_header_library(
     libc.src.__support.macros.properties.cpu_features
 )
 
-add_header_library(
-  Float16
-  HDRS
-    Float16.h
-  DEPENDS
-    .cast
-    .comparison_operations
-    .dyadic_float
-    libc.hdr.stdint_proxy
-    libc.src.__support.CPP.bit
-    libc.src.__support.CPP.type_traits
-    libc.src.__support.FPUtil.generic.add_sub
-    libc.src.__support.FPUtil.generic.div
-    libc.src.__support.FPUtil.generic.mul
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
-)
-
 add_header_library(
   fpbits_str
   HDRS
@@ -347,6 +329,25 @@ add_header_library(
     libc.src.__support.macros.properties.types
 )
 
+add_header_library(
+  float16
+  HDRS
+    float16.h
+  DEPENDS
+    .cast
+    .comparison_operations
+    .dyadic_float
+    libc.hdr.stdint_proxy
+    libc.src.__support.CPP.bit
+    libc.src.__support.CPP.type_traits
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.FPUtil.generic.div
+    libc.src.__support.FPUtil.generic.mul
+    libc.src.__support.macros.attributes
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
 add_header_library(
   bfloat16
   HDRS
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 83dedeb45d4f3d..7267ccccfd3c9c 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -821,6 +821,8 @@ template <typename T> LIBC_INLINE static constexpr FPType get_fp_type() {
     return FPType::IEEE754_Binary128;
   else if constexpr (cpp::is_same_v<UnqualT, Float80>)
     return FPType::X86_Binary80;
+  else if constexpr (cpp::is_same_v<UnqualT, Float16>)
+    return FPType::IEEE754_Binary16;
   else
     static_assert(cpp::always_false<UnqualT>, "Unsupported type");
 }
diff --git a/libc/src/__support/FPUtil/cast.h b/libc/src/__support/FPUtil/cast.h
index 7c1c3114f70bfc..35c00df1ee344b 100644
--- a/libc/src/__support/FPUtil/cast.h
+++ b/libc/src/__support/FPUtil/cast.h
@@ -36,7 +36,9 @@ cast(InType x) {
                   cpp::is_same_v<OutType, Float128> ||
                   cpp::is_same_v<InType, Float128> ||
                   cpp::is_same_v<OutType, Float80> ||
-                  cpp::is_same_v<InType, Float80>
+                  cpp::is_same_v<InType, Float80> ||
+                  cpp::is_same_v<OutType, Float16> ||
+                  cpp::is_same_v<InType, Float16>
 #if defined(LIBC_TYPES_HAS_FLOAT16) && !defined(__LIBC_USE_FLOAT16_CONVERSION)
                   || cpp::is_same_v<OutType, float16> ||
                   cpp::is_same_v<InType, float16>
@@ -55,12 +57,18 @@ cast(InType x) {
           return OutFPBits::quiet_nan().get_val();
         }
 
-        InStorageType x_mant = x_bits.get_mantissa();
-        if constexpr (InFPBits::FRACTION_LEN > OutFPBits::FRACTION_LEN)
-          x_mant >>= InFPBits::FRACTION_LEN - OutFPBits::FRACTION_LEN;
-        return OutFPBits::quiet_nan(x_bits.sign(),
-                                    static_cast<OutStorageType>(x_mant))
-            .get_val();
+        OutStorageType out_mant = 0;
+        if constexpr (InFPBits::FRACTION_LEN > OutFPBits::FRACTION_LEN) {
+          InStorageType in_mant = x_bits.get_mantissa();
+          in_mant >>= InFPBits::FRACTION_LEN - OutFPBits::FRACTION_LEN;
+          out_mant = static_cast<OutStorageType>(in_mant);
+        } else if constexpr (InFPBits::FRACTION_LEN < OutFPBits::FRACTION_LEN) {
+          out_mant = static_cast<OutStorageType>(x_bits.get_mantissa());
+          out_mant <<= OutFPBits::FRACTION_LEN - InFPBits::FRACTION_LEN;
+        } else {
+          out_mant = static_cast<OutStorageType>(x_bits.get_mantissa());
+        }
+        return OutFPBits::quiet_nan(x_bits.sign(), out_mant).get_val();
       }
 
       if (x_bits.is_inf())
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index b61db90461e3a4..21944df379ba89 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -476,7 +476,7 @@ template <size_t Bits> struct DyadicFloat {
                                         void>>
   LIBC_INLINE LIBC_CONSTEXPR_DEFAULT T as() const {
     if constexpr (cpp::is_same_v<T, bfloat16> || cpp::is_same_v<T, Float128> ||
-                  cpp::is_same_v<T, Float80>
+                  cpp::is_same_v<T, Float80> || cpp::is_same_v<T, Float16>
 #if defined(LIBC_TYPES_HAS_FLOAT16) && !defined(__LIBC_USE_FLOAT16_CONVERSION)
                   || cpp::is_same_v<T, float16>
 #endif
diff --git a/libc/src/__support/FPUtil/Float16.h b/libc/src/__support/FPUtil/float16.h
similarity index 68%
rename from libc/src/__support/FPUtil/Float16.h
rename to libc/src/__support/FPUtil/float16.h
index 5387e6a7c1c7a5..d1c0981bc240e1 100644
--- a/libc/src/__support/FPUtil/Float16.h
+++ b/libc/src/__support/FPUtil/float16.h
@@ -9,10 +9,6 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT16_H
 #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT16_H
 
-#include "src/__support/macros/properties/types.h"
-
-#ifdef LIBC_USE_SOFT_FLOAT16
-
 #include "hdr/stdint_proxy.h"
 #include "src/__support/CPP/type_traits.h"
 #include "src/__support/FPUtil/cast.h"
@@ -21,7 +17,9 @@
 #include "src/__support/FPUtil/generic/add_sub.h"
 #include "src/__support/FPUtil/generic/div.h"
 #include "src/__support/FPUtil/generic/mul.h"
+#include "src/__support/macros/attributes.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
 
 namespace LIBC_NAMESPACE_DECL {
 namespace fputil {
@@ -52,6 +50,8 @@ struct Float16 {
 
     } else if constexpr (cpp::is_convertible_v<T, Float16>) {
       bits = value.operator Float16().bits;
+    } else {
+      bits = fputil::cast<Float16>(static_cast<float>(value)).bits;
     }
   }
 
@@ -67,61 +67,74 @@ struct Float16 {
     return static_cast<T>(static_cast<float>(*this));
   }
 
-  LIBC_INLINE bool operator==(Float16 other) const {
+  LIBC_INLINE constexpr bool operator==(Float16 other) const {
     return fputil::equals(*this, other);
   }
 
-  LIBC_INLINE bool operator!=(Float16 other) const {
+  LIBC_INLINE constexpr bool operator!=(Float16 other) const {
     return !fputil::equals(*this, other);
   }
 
-  LIBC_INLINE bool operator<(Float16 other) const {
+  LIBC_INLINE constexpr bool operator<(Float16 other) const {
     return fputil::less_than(*this, other);
   }
 
-  LIBC_INLINE bool operator<=(Float16 other) const {
+  LIBC_INLINE constexpr bool operator<=(Float16 other) const {
     return fputil::less_than_or_equals(*this, other);
   }
 
-  LIBC_INLINE bool operator>(Float16 other) const {
+  LIBC_INLINE constexpr bool operator>(Float16 other) const {
     return fputil::greater_than(*this, other);
   }
 
-  LIBC_INLINE bool operator>=(Float16 other) const {
+  LIBC_INLINE constexpr bool operator>=(Float16 other) const {
     return fputil::greater_than_or_equals(*this, other);
   }
 
-  LIBC_INLINE constexpr Float16 operator-() const {
-    fputil::FPBits<float16> result(*this);
+  LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR Float16 operator-() const {
+    fputil::FPBits<Float16> result(*this);
     result.set_sign(result.is_pos() ? Sign::NEG : Sign::POS);
     return result.get_val();
   }
 
-  LIBC_INLINE Float16 operator+(Float16 other) const {
+  LIBC_INLINE constexpr Float16 operator+(Float16 other) const {
     return fputil::generic::add<Float16>(*this, other);
   }
 
-  LIBC_INLINE Float16 operator-(Float16 other) const {
+  LIBC_INLINE constexpr Float16 operator-(Float16 other) const {
     return fputil::generic::sub<Float16>(*this, other);
   }
 
-  LIBC_INLINE Float16 operator*(Float16 other) const {
-    return fputil::generic::mul<float16>(*this, other);
+  LIBC_INLINE constexpr Float16 operator*(Float16 other) const {
+    return fputil::generic::mul<Float16>(*this, other);
+  }
+
+  LIBC_INLINE constexpr Float16 operator/(Float16 other) const {
+    return fputil::generic::div<Float16>(*this, other);
+  }
+
+  LIBC_INLINE constexpr Float16 &operator+=(Float16 other) {
+    *this = *this + other;
+    return *this;
   }
 
-  LIBC_INLINE Float16 operator/(Float16 other) const {
-    return fputil::generic::div<float16>(*this, other);
+  LIBC_INLINE constexpr Float16 &operator-=(Float16 other) {
+    *this = *this - other;
+    return *this;
   }
 
-  LIBC_INLINE Float16 &operator*=(const Float16 &other) {
+  LIBC_INLINE constexpr Float16 &operator*=(Float16 other) {
     *this = *this * other;
     return *this;
   }
+
+  LIBC_INLINE constexpr Float16 &operator/=(Float16 other) {
+    *this = *this / other;
+    return *this;
+  }
 }; // struct Float16
 
 } // namespace fputil
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif // LIBC_USE_SOFT_FLOAT16
-
 #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT16_H
diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index 80789791d3d973..86276cd9365dc0 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -69,15 +69,15 @@
 // -- float16 support ---------------------------------------------------------
 // LIBC_TYPES_HAS_FLOAT16 is provided by
 // "include/llvm-libc-macros/float16-macros.h"
-#ifdef LIBC_TYPES_HAS_FLOAT16
-
-#ifdef LIBC_USE_SOFT_FLOAT16
 namespace LIBC_NAMESPACE_DECL {
 namespace fputil {
 struct Float16;
 } // namespace fputil
 } // namespace LIBC_NAMESPACE_DECL
 
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#ifdef LIBC_USE_SOFT_FLOAT16
 using float16 = LIBC_NAMESPACE::fputil::Float16;
 #else
 // Type alias for internal use.
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 0f3118659dfef9..a4c3f99926fee5 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3553,7 +3553,7 @@ add_header_library(
     fabsf16.h
   DEPENDS
     libc.include.llvm-libc-macros.float16_macros
-    libc.src.__support.FPUtil.Float16
+    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.basic_operations
     libc.src.__support.macros.config
   FLAGS
diff --git a/libc/src/__support/math/fabsf16.h b/libc/src/__support/math/fabsf16.h
index ca568c680b7c2b..65b786fd773319 100644
--- a/libc/src/__support/math/fabsf16.h
+++ b/libc/src/__support/math/fabsf16.h
@@ -14,7 +14,7 @@
 #ifdef LIBC_TYPES_HAS_FLOAT16
 
 #include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/FPUtil/Float16.h"
+#include "src/__support/FPUtil/float16.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
diff --git a/libc/test/src/__support/FPUtil/CMakeLists.txt b/libc/test/src/__support/FPUtil/CMakeLists.txt
index c61b3465f9c57a..545c48fd107478 100644
--- a/libc/test/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/test/src/__support/FPUtil/CMakeLists.txt
@@ -144,6 +144,17 @@ if(LLVM_LIBC_FULL_BUILD)
   return()
 endif()
 
+add_fp_unittest(
+  float16_test
+  NEED_MPFR
+  SUITE
+    libc-fputil-tests
+  SRCS
+    float16_test.cpp
+  DEPENDS
+    libc.src.__support.FPUtil.float16
+)
+
 add_fp_unittest(
   bfloat16_test
   NEED_MPFR
diff --git a/libc/test/src/__support/FPUtil/float16_test.cpp b/libc/test/src/__support/FPUtil/float16_test.cpp
new file mode 100644
index 00000000000000..203e22c5806920
--- /dev/null
+++ b/libc/test/src/__support/FPUtil/float16_test.cpp
@@ -0,0 +1,124 @@
+//===-- Unit tests for float16 type ---------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/float16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPCommon.h"
+
+using Float16 = LIBC_NAMESPACE::fputil::Float16;
+using LlvmLibcFloat16ConversionTest =
+    LIBC_NAMESPACE::testing::FPTest<Float16>;
+
+// range: [0, inf]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7c00U;
+
+// range: [-0, -inf]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xfc00U;
+
+using MPFRNumber = LIBC_NAMESPACE::testing::mpfr::MPFRNumber;
+
+TEST_F(LlvmLibcFloat16ConversionTest, ToFloatPositiveRange) {
+  for (uint16_t bits = POS_START; bits <= POS_STOP; bits++) {
+    Float16 f16_num{bits};
+    MPFRNumber mpfr_num{f16_num};
+
+    // float16 to float
+    float mpfr_float = mpfr_num.as<float>();
+    EXPECT_FP_EQ_ALL_ROUNDING(mpfr_float, static_cast<float>(f16_num));
+
+    // float to float16
+    Float16 f16_from_float{mpfr_float};
+    MPFRNumber mpfr_num_2{mpfr_float};
+    Float16 mpfr_f16 = mpfr_num_2.as<Float16>();
+    EXPECT_FP_EQ_ALL_ROUNDING(mpfr_f16, f16_from_float);
+  }
+}
+
+TEST_F(LlvmLibcFloat16ConversionTest, ToFloatNegativeRange) {
+  for (uint16_t bits = NEG_START; bits <= NEG_STOP; bits++) {
+    Float16 f16_num{bits};
+    MPFRNumber mpfr_num{f16_num};
+
+    // float16 to float
+    float mpfr_float = mpfr_num.as<float>();
+    EXPECT_FP_EQ_ALL_ROUNDING(mpfr_float, static_cast<float>(f16_num));
+
+    // float to float16
+    Float16 f16_from_float{mpfr_float};
+    MPFRNumber mpfr_num_2{mpfr_float};
+    Float16 mpfr_f16 = mpfr_num_2.as<Float16>();
+    EXPECT_FP_EQ_ALL_ROUNDING(mpfr_f16, f16_from_float);
+  }
+}
+
+TEST_F(LlvmLibcFloat16ConversionTest, FromInteger) {
+  constexpr int RANGE = 1'234;
+  for (int i = -RANGE; i <= RANGE; i++) {
+    Float16 mpfr_f16 = MPFRNumber(i).as<Float16>();
+    Float16 libc_f16{i};
+    EXPECT_FP_EQ_ALL_ROUNDING(mpfr_f16, libc_f16);
+  }
+}
+
+TEST_F(LlvmLibcFloat16ConversionTest, CompoundAssignmentOperators) {
+  constexpr Float16 VAL[] = {zero,           neg_zero,        inf,
+                             neg_inf,        min_normal,      max_normal,
+                             Float16(1.0f),  Float16(-1.0f),  Float16(2.0f),
+                             Float16(3.0f)};
+  // *=
+  for (const Float16 &x : VAL) {
+    for (const Float16 &y : VAL) {
+      Float16 a = x, b = y;
+      MPFRNumber mpfr_a{a}, mpfr_b{b};
+      MPFRNumber mpfr_c = mpfr_a.mul(mpfr_b);
+      Float16 mpfr_f16 = mpfr_c.as<Float16>();
+      a *= b;
+      Float16 libc_f16 = a;
+      EXPECT_FP_EQ_ALL_ROUNDING(mpfr_f16, libc_f16);
+    }
+  }
+  // /=
+  for (const Float16 &x : VAL) {
+    for (const Float16 &y : VAL) {
+      Float16 a = x, b = y;
+      MPFRNumber mpfr_a{a}, mpfr_b{b};
+      MPFRNumber mpfr_c = mpfr_a.div(mpfr_b);
+      Float16 mpfr_f16 = mpfr_c.as<Float16>();
+      a /= b;
+      Float16 libc_f16 = a;
+      EXPECT_FP_EQ_ALL_ROUNDING(mpfr_f16, libc_f16);
+    }
+  }
+  // +=
+  for (const Float16 &x : VAL) {
+    for (const Float16 &y : VAL) {
+      Float16 a = x, b = y;
+      MPFRNumber mpfr_a{a}, mpfr_b{b};
+      MPFRNumber mpfr_c = mpfr_a.add(mpfr_b);
+      Float16 mpfr_f16 = mpfr_c.as<Float16>();
+      a += b;
+      Float16 libc_f16 = a;
+      EXPECT_FP_EQ_ALL_ROUNDING(mpfr_f16, libc_f16);
+    }
+  }
+  // -=
+  for (const Float16 &x : VAL) {
+    for (const Float16 &y : VAL) {
+      Float16 a = x, b = y;
+      MPFRNumber mpfr_a{a}, mpfr_b{b};
+      MPFRNumber mpfr_c = mpfr_a.sub(mpfr_b);
+      Float16 mpfr_f16 = mpfr_c.as<Float16>();
+      a -= b;
+      Float16 libc_f16 = a;
+      EXPECT_FP_EQ_ALL_ROUNDING(mpfr_f16, libc_f16);
+    }
+  }
+}
diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index 160157f156f9d1..3bbc5e27cf6b22 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -711,7 +711,7 @@ add_fp_unittest(
     float16_test.cpp
   DEPENDS
     .exhaustive_test
-    libc.src.__support.FPUtil.Float16
+    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.cast
     libc.src.__support.FPUtil.fp_bits
   LINK_LIBRARIES
diff --git a/libc/test/src/math/exhaustive/exhaustive_test.h b/libc/test/src/math/exhaustive/exhaustive_test.h
index e0179479ac9c57..9dea202ca972ff 100644
--- a/libc/test/src/math/exhaustive/exhaustive_test.h
+++ b/libc/test/src/math/exhaustive/exhaustive_test.h
@@ -16,7 +16,7 @@
 
 #include "src/__support/CPP/type_traits.h"
 #include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/Float16.h"
+#include "src/__support/FPUtil/float16.h"
 #include "src/__support/macros/properties/types.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 8b7c8de360fa63..419b4181229204 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -253,7 +253,7 @@ add_fp_unittest(
     FAbsTest.h
   DEPENDS
     libc.src.math.fabsf16
-    libc.src.__support.FPUtil.Float16
+    libc.src.__support.FPUtil.float16
 )
 
 add_fp_unittest(
diff --git a/libc/test/src/math/smoke/fabsf16_test.cpp b/libc/test/src/math/smoke/fabsf16_test.cpp
index fb36af661f48ce..dcac6db64f76db 100644
--- a/libc/test/src/math/smoke/fabsf16_test.cpp
+++ b/libc/test/src/math/smoke/fabsf16_test.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "FAbsTest.h"
-#include "src/__support/FPUtil/Float16.h"
+#include "src/__support/FPUtil/float16.h"
 #include "src/math/fabsf16.h"
 
 LIST_FABS_TESTS(Fabsf16, float16, LIBC_NAMESPACE::fabsf16)
diff --git a/libc/utils/MPFRWrapper/CMakeLists.txt b/libc/utils/MPFRWrapper/CMakeLists.txt
index 5cdbe5a802cb44..dd3832b2b04dff 100644
--- a/libc/utils/MPFRWrapper/CMakeLists.txt
+++ b/libc/utils/MPFRWrapper/CMakeLists.txt
@@ -14,7 +14,7 @@ if(LIBC_TESTS_CAN_USE_MPFR OR LIBC_TESTS_CAN_USE_MPC)
     libc.src.__support.CPP.string
     libc.src.__support.CPP.string_view
     libc.src.__support.CPP.type_traits
-    libc.src.__support.FPUtil.Float16
+    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.cast
     libc.src.__support.FPUtil.float80
@@ -49,7 +49,7 @@ if(LIBC_TESTS_CAN_USE_MPFR)
     libc.hdr.stdint_proxy
     libc.src.__support.CPP.array
     libc.src.__support.CPP.stringstream
-    libc.src.__support.FPUtil.Float16
+    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.fpbits_str
diff --git a/libc/utils/MPFRWrapper/MPCommon.cpp b/libc/utils/MPFRWrapper/MPCommon.cpp
index 5c8c8c0d161287..6a846459f96811 100644
--- a/libc/utils/MPFRWrapper/MPCommon.cpp
+++ b/libc/utils/MPFRWrapper/MPCommon.cpp
@@ -10,11 +10,7 @@
 
 #include "src/__support/CPP/string_view.h"
 #include "src/__support/FPUtil/bfloat16.h"
-
-#ifdef LIBC_USE_SOFT_FLOAT16
-#include "src/__support/FPUtil/Float16.h"
-#endif
-
+#include "src/__support/FPUtil/float16.h"
 #include "src/__support/FPUtil/cast.h"
 #include "src/__support/FPUtil/float80.h"
 #include "src/__support/macros/config.h"
@@ -638,7 +634,7 @@ template <> long double MPFRNumber::as<long double>() const {
   return mpfr_get_ld(value, mpfr_rounding);
 }
 
-#if defined(LIBC_TYPES_HAS_FLOAT16) || defined(LIBC_USE_SOFT_FLOAT16)
+#ifdef LIBC_TYPES_HAS_FLOAT16
 template <> float16 MPFRNumber::as<float16>() const {
   // TODO: Either prove that this cast won't cause double-rounding errors, or
   // find a better way to get a float16.
@@ -646,6 +642,12 @@ template <> float16 MPFRNumber::as<float16>() const {
 }
 #endif
 
+#if !defined(LIBC_USE_SOFT_FLOAT16)
+template <> fputil::Float16 MPFRNumber::as<fputil::Float16>() const {
+  return fputil::cast<fputil::Float16>(mpfr_get_d(value, mpfr_rounding));
+}
+#endif
+
 #ifdef LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
 template <> float128 MPFRNumber::as<float128>() const {
   return mpfr_get_float128(value, mpfr_rounding);
diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h
index 1deda67da64e6c..baa3f8c7e44c7a 100644
--- a/libc/utils/MPFRWrapper/MPCommon.h
+++ b/libc/utils/MPFRWrapper/MPCommon.h
@@ -71,6 +71,12 @@ template <> struct ExtraPrecision<float80> {
 };
 #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
 
+#if !defined(LIBC_USE_SOFT_FLOAT16)
+template <> struct ExtraPrecision<LIBC_NAMESPACE::fputil::Float16> {
+  static constexpr unsigned int VALUE = 128;
+};
+#endif
+
 template <> struct ExtraPrecision<bfloat16> {
   static constexpr unsigned int VALUE = 64;
 };
@@ -121,6 +127,9 @@ class MPFRNumber {
             cpp::enable_if_t<cpp::is_same_v<float, XType>
 #ifdef LIBC_TYPES_HAS_FLOAT16
                                  || cpp::is_same_v<float16, XType>
+#endif
+#if !defined(LIBC_USE_SOFT_FLOAT16)
+                                 || cpp::is_same_v<LIBC_NAMESPACE::fputil::Float16, XType>
 #endif
                                  || cpp::is_same_v<bfloat16, XType>,
                              int> = 0>
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index e19541d879bb42..079aa75775a49a 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -11,7 +11,7 @@
 
 #include "src/__support/CPP/array.h"
 #include "src/__support/CPP/stringstream.h"
-#include "src/__support/FPUtil/Float16.h"
+#include "src/__support/FPUtil/float16.h"
 #include "src/__support/FPUtil/bfloat16.h"
 #include "src/__support/FPUtil/fpbits_str.h"
 #include "src/__support/macros/config.h"

>From b981ec3529777a9c46212dd850b431d6d7ee8dbb Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Sat, 29 Aug 2026 05:52:21 +0300
Subject: [PATCH 3/7] Make soft float16 emulation the default behavior

---
 libc/config/baremetal/aarch64/entrypoints.txt |  2 +-
 libc/config/baremetal/arm/entrypoints.txt     |  2 +-
 libc/config/baremetal/riscv/entrypoints.txt   |  2 +-
 libc/config/darwin/aarch64/entrypoints.txt    |  2 +-
 libc/config/freebsd/x86_64/entrypoints.txt    |  2 +-
 libc/config/linux/aarch64/entrypoints.txt     |  2 +-
 libc/config/linux/arm/entrypoints.txt         |  6 +-----
 libc/config/linux/riscv/entrypoints.txt       |  2 +-
 libc/config/linux/x86_64/entrypoints.txt      |  2 +-
 libc/config/windows/entrypoints.txt           |  1 +
 .../include/llvm-libc-macros/float16-macros.h | 20 ++++++-------------
 .../CPP/type_traits/is_floating_point.h       |  6 +-----
 libc/src/__support/FPUtil/float16.h           |  5 +++++
 libc/src/__support/macros/properties/types.h  | 19 ++++++++----------
 libc/src/__support/math/fabsf16.h             | 14 ++++++-------
 libc/src/math/fabsf16.h                       |  5 +++++
 libc/test/src/__support/FPUtil/CMakeLists.txt |  2 ++
 .../FPUtil/comparison_operations_test.cpp     |  1 +
 .../__support/FPUtil/dyadic_float_test.cpp    |  1 +
 .../src/__support/FPUtil/float16_test.cpp     |  9 ++++-----
 .../test/src/math/exhaustive/float16_test.cpp |  4 ++--
 libc/test/src/math/smoke/fabsf16_test.cpp     |  4 ++++
 libc/utils/MPFRWrapper/MPCommon.cpp           |  4 +---
 libc/utils/MPFRWrapper/MPCommon.h             | 15 ++++++--------
 libc/utils/MPFRWrapper/MPFRUtils.cpp          |  2 +-
 25 files changed, 64 insertions(+), 70 deletions(-)

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index e3d5c8c0a68399..e72f99ea9b715d 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -415,6 +415,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     libc.src.math.fabsl
     libc.src.math.fadd
@@ -714,7 +715,6 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.f16sub
     libc.src.math.f16subf
     libc.src.math.f16subl
-    libc.src.math.fabsf16
     libc.src.math.fdimf16
     libc.src.math.floorf16
     libc.src.math.fmaf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index fc937dcc7670c0..a8c86abe0c1960 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -427,6 +427,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     libc.src.math.fabsl
     libc.src.math.fadd
@@ -726,7 +727,6 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.f16sub
     libc.src.math.f16subf
     libc.src.math.f16subl
-    libc.src.math.fabsf16
     libc.src.math.fdimf16
     libc.src.math.floorf16
     libc.src.math.fmaf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index ae01bdda48a8ed..a05b35ab5831c3 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -418,6 +418,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     libc.src.math.fabsl
     libc.src.math.fadd
@@ -717,7 +718,6 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.f16sub
     libc.src.math.f16subf
     libc.src.math.f16subl
-    libc.src.math.fabsf16
     libc.src.math.fdimf16
     libc.src.math.floorf16
     libc.src.math.fmaf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index b0fa44ccbd6df0..d9e54ae2b4a625 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -235,6 +235,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     libc.src.math.fabsl
     libc.src.math.fadd
@@ -535,7 +536,6 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.f16sub
     libc.src.math.f16subf
     libc.src.math.f16subl
-    libc.src.math.fabsf16
     libc.src.math.fdimf16
     libc.src.math.floorf16
     libc.src.math.fmaf16
diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 862a421c473d36..f95944134efcb1 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -164,6 +164,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     libc.src.math.fabsl
     libc.src.math.fadd
@@ -478,7 +479,6 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.f16sub
     libc.src.math.f16subf
     libc.src.math.f16subl
-    libc.src.math.fabsf16
     libc.src.math.fdimf16
     libc.src.math.floorf16
     libc.src.math.fmaf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 99678f528cda26..6d4289af4753bd 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -707,6 +707,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     libc.src.math.fabsl
     libc.src.math.fadd
@@ -996,7 +997,6 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.f16sub
     libc.src.math.f16subf
     # libc.src.math.f16subl
-    libc.src.math.fabsf16
     libc.src.math.fdimf16
     libc.src.math.fdiv
     libc.src.math.fdivl
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 14bf6bb86e3d1e..d697de35da93b9 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -478,6 +478,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     libc.src.math.fabsl
     libc.src.math.fadd
@@ -712,11 +713,6 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.ufromfpxl
 )
 
-list(APPEND TARGET_LIBM_ENTRYPOINTS
-  # float16 entrypoints
-  libc.src.math.fabsf16
-)
-
 list(APPEND TARGET_LIBM_ENTRYPOINTS
   # bfloat16 entrypoints
   libc.src.math.atanbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index d1232ae7d11013..6007fb255d3191 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -788,6 +788,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     libc.src.math.fabsl
     libc.src.math.fadd
@@ -1095,7 +1096,6 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.f16sub
     libc.src.math.f16subf
     libc.src.math.f16subl
-    libc.src.math.fabsf16
     libc.src.math.fdimf16
     libc.src.math.floorf16
     libc.src.math.fmaf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 28d2d4881dbefd..8b283a4636d434 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -788,6 +788,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     libc.src.math.fabsl
     libc.src.math.fadd
@@ -1095,7 +1096,6 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.f16sub
     libc.src.math.f16subf
     libc.src.math.f16subl
-    libc.src.math.fabsf16
     libc.src.math.fdimf16
     libc.src.math.floorf16
     libc.src.math.fmaf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 7046934c029380..02805f021e3084 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -196,6 +196,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     libc.src.math.fabsl
     libc.src.math.fadd
diff --git a/libc/include/llvm-libc-macros/float16-macros.h b/libc/include/llvm-libc-macros/float16-macros.h
index 01da4baf884a6e..5d9ff66d531ae5 100644
--- a/libc/include/llvm-libc-macros/float16-macros.h
+++ b/libc/include/llvm-libc-macros/float16-macros.h
@@ -11,26 +11,18 @@
 
 #include "../llvm-libc-types/float128.h"
 
-#if (defined(__arm__) && defined(_M_ARM)) || defined(__riscv)
-#define LIBC_USE_SOFT_FLOAT16
-#endif
-
-#ifdef LIBC_USE_SOFT_FLOAT16
-#define LIBC_TYPES_HAS_FLOAT16
-#endif
-
-#if !defined(LIBC_TYPES_HAS_FLOAT16) && defined(__FLT16_MANT_DIG__) &&         \
+#if defined(__FLT16_MANT_DIG__) &&                                             \
     (!defined(__GNUC__) || __GNUC__ >= 13 ||                                   \
      (defined(__clang__) && __clang_major__ >= 12)) &&                         \
-    !defined(__riscv) && !defined(_WIN32) &&                                   \
-    (!defined(__i386__) || defined(__SSE2__))
+    !defined(__arm__) && !defined(_M_ARM) && !defined(__riscv) &&              \
+    !defined(_WIN32) && (!defined(__i386__) || defined(__SSE2__))
 #define LIBC_TYPES_HAS_FLOAT16
+#endif
 
 // TODO: This would no longer be required if HdrGen let us guard function
 // declarations with multiple macros.
-#ifdef LIBC_TYPES_HAS_NATIVE_FLOAT128
+#if defined(LIBC_TYPES_HAS_FLOAT16) && defined(LIBC_TYPES_HAS_NATIVE_FLOAT128)
 #define LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128
-#endif // LIBC_TYPES_HAS_NATIVE_FLOAT128
-#endif
+#endif // LIBC_TYPES_HAS_FLOAT16 && LIBC_TYPES_HAS_NATIVE_FLOAT128
 
 #endif // LLVM_LIBC_MACROS_FLOAT16_MACROS_H
diff --git a/libc/src/__support/CPP/type_traits/is_floating_point.h b/libc/src/__support/CPP/type_traits/is_floating_point.h
index 973fd9909b2440..d64f8784db0d84 100644
--- a/libc/src/__support/CPP/type_traits/is_floating_point.h
+++ b/libc/src/__support/CPP/type_traits/is_floating_point.h
@@ -41,11 +41,7 @@ template <typename T> struct is_floating_point {
                               float128
 #endif
                               ,
-                              bfloat16
-                              ,
-                              fputil::Float16
-                              ,
-                              fputil::Float128
+                              bfloat16, fputil::Float16, fputil::Float128
 
                               ,
                               fputil::Float80>();
diff --git a/libc/src/__support/FPUtil/float16.h b/libc/src/__support/FPUtil/float16.h
index d1c0981bc240e1..58672b6f24e748 100644
--- a/libc/src/__support/FPUtil/float16.h
+++ b/libc/src/__support/FPUtil/float16.h
@@ -29,6 +29,11 @@ struct Float16 {
 
   LIBC_INLINE Float16() = default;
 
+  template <size_t Bits>
+  LIBC_INLINE constexpr explicit Float16(const DyadicFloat<Bits> &df)
+      : bits(df.template as<Float16, /*ShouldSignalExceptions=*/false>().bits) {
+  }
+
   template <typename T>
   LIBC_INLINE constexpr explicit Float16(T value)
       : bits(static_cast<uint16_t>(0U)) {
diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index 86276cd9365dc0..82aa6db59e89a5 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -69,23 +69,20 @@
 // -- float16 support ---------------------------------------------------------
 // LIBC_TYPES_HAS_FLOAT16 is provided by
 // "include/llvm-libc-macros/float16-macros.h"
+#ifdef LIBC_TYPES_HAS_FLOAT16
+// Type alias for internal use.
+using float16 = _Float16;
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+// -- Emulated float16 support ------------------------------------------------
+// Float16 is always available regardless of built-in float16 type support in
+// the compiler.
 namespace LIBC_NAMESPACE_DECL {
 namespace fputil {
 struct Float16;
 } // namespace fputil
 } // namespace LIBC_NAMESPACE_DECL
 
-#ifdef LIBC_TYPES_HAS_FLOAT16
-
-#ifdef LIBC_USE_SOFT_FLOAT16
-using float16 = LIBC_NAMESPACE::fputil::Float16;
-#else
-// Type alias for internal use.
-using float16 = _Float16;
-#endif
-
-#endif // LIBC_TYPES_HAS_FLOAT16
-
 // -- float128 support --------------------------------------------------------
 // LIBC_TYPES_HAS_NATIVE_FLOAT128 and 'float128' type are provided by
 // "include/llvm-libc-types/float128.h"
diff --git a/libc/src/__support/math/fabsf16.h b/libc/src/__support/math/fabsf16.h
index 65b786fd773319..9ff447d888da1b 100644
--- a/libc/src/__support/math/fabsf16.h
+++ b/libc/src/__support/math/fabsf16.h
@@ -9,17 +9,19 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_FABSF16_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_FABSF16_H
 
-#include "include/llvm-libc-macros/float16-macros.h"
-
-#ifdef LIBC_TYPES_HAS_FLOAT16
-
 #include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/FPUtil/float16.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
+#include "src/__support/macros/properties/types.h"
 
 namespace LIBC_NAMESPACE_DECL {
+
+#ifndef LIBC_TYPES_HAS_FLOAT16
+using float16 = LIBC_NAMESPACE::fputil::Float16;
+#endif // LIBC_TYPES_HAS_FLOAT16
+
 namespace math {
 
 LIBC_INLINE constexpr float16 fabsf16(float16 x) {
@@ -29,7 +31,7 @@ LIBC_INLINE constexpr float16 fabsf16(float16 x) {
   // https://godbolt.org/z/K9orM4hTa
 #if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) &&                                 \
     !(defined(LIBC_TARGET_ARCH_IS_X86) && defined(LIBC_COMPILER_IS_GCC)) &&    \
-    !defined(LIBC_USE_SOFT_FLOAT16)
+    defined(LIBC_TYPES_HAS_FLOAT16)
   return __builtin_fabsf16(x);
 #else
   return fputil::abs(x);
@@ -39,6 +41,4 @@ LIBC_INLINE constexpr float16 fabsf16(float16 x) {
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif // LIBC_TYPES_HAS_FLOAT16
-
 #endif // LLVM_LIBC_SRC___SUPPORT_MATH_FABSF16_H
diff --git a/libc/src/math/fabsf16.h b/libc/src/math/fabsf16.h
index f48a51f5ff58ec..5876f9c3a48f3d 100644
--- a/libc/src/math/fabsf16.h
+++ b/libc/src/math/fabsf16.h
@@ -9,11 +9,16 @@
 #ifndef LLVM_LIBC_SRC_MATH_FABSF16_H
 #define LLVM_LIBC_SRC_MATH_FABSF16_H
 
+#include "src/__support/FPUtil/float16.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/types.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
+#ifndef LIBC_TYPES_HAS_FLOAT16
+using float16 = LIBC_NAMESPACE::fputil::Float16;
+#endif // LIBC_TYPES_HAS_FLOAT16
+
 float16 fabsf16(float16 x);
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/__support/FPUtil/CMakeLists.txt b/libc/test/src/__support/FPUtil/CMakeLists.txt
index 545c48fd107478..6e39111f3d7ce6 100644
--- a/libc/test/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/test/src/__support/FPUtil/CMakeLists.txt
@@ -9,6 +9,7 @@ add_fp_unittest(
     dyadic_float_test.cpp
   DEPENDS
     libc.src.__support.FPUtil.dyadic_float
+    libc.src.__support.FPUtil.float16
     libc.src.__support.macros.properties.types
   COMPILE_OPTIONS
     # Prevent constant folding with a default rounding mode.
@@ -175,6 +176,7 @@ add_fp_unittest(
   DEPENDS
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.comparison_operations
+    libc.src.__support.FPUtil.float16
     libc.src.__support.macros.properties.types
 )
 
diff --git a/libc/test/src/__support/FPUtil/comparison_operations_test.cpp b/libc/test/src/__support/FPUtil/comparison_operations_test.cpp
index 5b56240222c62c..f8f2b4384d9e83 100644
--- a/libc/test/src/__support/FPUtil/comparison_operations_test.cpp
+++ b/libc/test/src/__support/FPUtil/comparison_operations_test.cpp
@@ -8,6 +8,7 @@
 
 #include "src/__support/FPUtil/bfloat16.h"
 #include "src/__support/FPUtil/comparison_operations.h"
+#include "src/__support/FPUtil/float16.h"
 #include "src/__support/macros/properties/types.h"
 #include "test/UnitTest/FEnvSafeTest.h"
 #include "test/UnitTest/FPMatcher.h"
diff --git a/libc/test/src/__support/FPUtil/dyadic_float_test.cpp b/libc/test/src/__support/FPUtil/dyadic_float_test.cpp
index 3575f9694ab7fe..7aeba365d83cbd 100644
--- a/libc/test/src/__support/FPUtil/dyadic_float_test.cpp
+++ b/libc/test/src/__support/FPUtil/dyadic_float_test.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/float16.h"
 #include "src/__support/big_int.h"
 #include "src/__support/macros/properties/types.h"
 #include "test/UnitTest/FPMatcher.h"
diff --git a/libc/test/src/__support/FPUtil/float16_test.cpp b/libc/test/src/__support/FPUtil/float16_test.cpp
index 203e22c5806920..674afd0fefd1cf 100644
--- a/libc/test/src/__support/FPUtil/float16_test.cpp
+++ b/libc/test/src/__support/FPUtil/float16_test.cpp
@@ -12,8 +12,7 @@
 #include "utils/MPFRWrapper/MPCommon.h"
 
 using Float16 = LIBC_NAMESPACE::fputil::Float16;
-using LlvmLibcFloat16ConversionTest =
-    LIBC_NAMESPACE::testing::FPTest<Float16>;
+using LlvmLibcFloat16ConversionTest = LIBC_NAMESPACE::testing::FPTest<Float16>;
 
 // range: [0, inf]
 static constexpr uint16_t POS_START = 0x0000U;
@@ -69,9 +68,9 @@ TEST_F(LlvmLibcFloat16ConversionTest, FromInteger) {
 }
 
 TEST_F(LlvmLibcFloat16ConversionTest, CompoundAssignmentOperators) {
-  constexpr Float16 VAL[] = {zero,           neg_zero,        inf,
-                             neg_inf,        min_normal,      max_normal,
-                             Float16(1.0f),  Float16(-1.0f),  Float16(2.0f),
+  constexpr Float16 VAL[] = {zero,          neg_zero,       inf,
+                             neg_inf,       min_normal,     max_normal,
+                             Float16(1.0f), Float16(-1.0f), Float16(2.0f),
                              Float16(3.0f)};
   // *=
   for (const Float16 &x : VAL) {
diff --git a/libc/test/src/math/exhaustive/float16_test.cpp b/libc/test/src/math/exhaustive/float16_test.cpp
index 3d1ba1ab805a64..ea0eb9547b001e 100644
--- a/libc/test/src/math/exhaustive/float16_test.cpp
+++ b/libc/test/src/math/exhaustive/float16_test.cpp
@@ -31,8 +31,8 @@ struct Float16ConversionChecker : public virtual LIBC_NAMESPACE::testing::Test {
       FPBits x_bits(bits);
       FloatType x = x_bits.get_val();
 
-      const float16 libc_result = cast<float16>(x);
-      const float16 mpfr_result = mpfr::MPFRNumber(x).as<float16>();
+      const Float16 libc_result = cast<Float16>(x);
+      const Float16 mpfr_result = mpfr::MPFRNumber(x).as<Float16>();
 
       const bool correct =
           LIBC_NAMESPACE::testing::getMatcher<
diff --git a/libc/test/src/math/smoke/fabsf16_test.cpp b/libc/test/src/math/smoke/fabsf16_test.cpp
index dcac6db64f76db..b1dc0f479b1ae0 100644
--- a/libc/test/src/math/smoke/fabsf16_test.cpp
+++ b/libc/test/src/math/smoke/fabsf16_test.cpp
@@ -10,4 +10,8 @@
 #include "src/__support/FPUtil/float16.h"
 #include "src/math/fabsf16.h"
 
+#ifndef LIBC_TYPES_HAS_FLOAT16
+using float16 = LIBC_NAMESPACE::fputil::Float16;
+#endif // LIBC_TYPES_HAS_FLOAT16
+
 LIST_FABS_TESTS(Fabsf16, float16, LIBC_NAMESPACE::fabsf16)
diff --git a/libc/utils/MPFRWrapper/MPCommon.cpp b/libc/utils/MPFRWrapper/MPCommon.cpp
index 6a846459f96811..5fc15e0ef8a641 100644
--- a/libc/utils/MPFRWrapper/MPCommon.cpp
+++ b/libc/utils/MPFRWrapper/MPCommon.cpp
@@ -10,8 +10,8 @@
 
 #include "src/__support/CPP/string_view.h"
 #include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/FPUtil/float16.h"
 #include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/float16.h"
 #include "src/__support/FPUtil/float80.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/types.h"
@@ -642,11 +642,9 @@ template <> float16 MPFRNumber::as<float16>() const {
 }
 #endif
 
-#if !defined(LIBC_USE_SOFT_FLOAT16)
 template <> fputil::Float16 MPFRNumber::as<fputil::Float16>() const {
   return fputil::cast<fputil::Float16>(mpfr_get_d(value, mpfr_rounding));
 }
-#endif
 
 #ifdef LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
 template <> float128 MPFRNumber::as<float128>() const {
diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h
index baa3f8c7e44c7a..e4f619891afb50 100644
--- a/libc/utils/MPFRWrapper/MPCommon.h
+++ b/libc/utils/MPFRWrapper/MPCommon.h
@@ -71,11 +71,9 @@ template <> struct ExtraPrecision<float80> {
 };
 #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
 
-#if !defined(LIBC_USE_SOFT_FLOAT16)
 template <> struct ExtraPrecision<LIBC_NAMESPACE::fputil::Float16> {
   static constexpr unsigned int VALUE = 128;
 };
-#endif
 
 template <> struct ExtraPrecision<bfloat16> {
   static constexpr unsigned int VALUE = 64;
@@ -124,15 +122,14 @@ class MPFRNumber {
   // to float, as the MPFR API does not support float16, thus requiring
   // conversion to a higher-precision format.
   template <typename XType,
-            cpp::enable_if_t<cpp::is_same_v<float, XType>
+            cpp::enable_if_t<
+                cpp::is_same_v<float, XType>
 #ifdef LIBC_TYPES_HAS_FLOAT16
-                                 || cpp::is_same_v<float16, XType>
-#endif
-#if !defined(LIBC_USE_SOFT_FLOAT16)
-                                 || cpp::is_same_v<LIBC_NAMESPACE::fputil::Float16, XType>
+                    || cpp::is_same_v<float16, XType>
 #endif
-                                 || cpp::is_same_v<bfloat16, XType>,
-                             int> = 0>
+                    || cpp::is_same_v<LIBC_NAMESPACE::fputil::Float16, XType> ||
+                    cpp::is_same_v<bfloat16, XType>,
+                int> = 0>
   explicit MPFRNumber(XType x,
                       unsigned int precision = ExtraPrecision<XType>::VALUE,
                       RoundingMode rounding = RoundingMode::Nearest)
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 079aa75775a49a..b0ae2f7aa1a342 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -11,8 +11,8 @@
 
 #include "src/__support/CPP/array.h"
 #include "src/__support/CPP/stringstream.h"
-#include "src/__support/FPUtil/float16.h"
 #include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/float16.h"
 #include "src/__support/FPUtil/fpbits_str.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/types.h"

>From 88166b64499d4418d2e6eb9e479c40a73e861f00 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Mon, 16 Nov 2026 03:56:16 +0200
Subject: [PATCH 4/7] Address review comments on soft Float16

---
 .../CPP/type_traits/is_floating_point.h       |  8 ++++++-
 libc/src/__support/FPUtil/BasicOperations.h   |  8 +++----
 libc/src/__support/FPUtil/float16.h           | 23 +++++++++++++++----
 libc/src/__support/math/CMakeLists.txt        |  2 +-
 .../FPUtil/comparison_operations_test.cpp     |  2 ++
 .../__support/FPUtil/dyadic_float_test.cpp    |  1 +
 .../src/__support/FPUtil/float16_test.cpp     | 13 ++++++++---
 libc/test/src/math/exhaustive/CMakeLists.txt  |  4 ++--
 .../src/math/exhaustive/exhaustive_test.h     |  1 -
 .../test/src/math/exhaustive/float16_test.cpp | 14 +++++++----
 libc/test/src/math/smoke/CMakeLists.txt       |  2 +-
 libc/test/src/math/smoke/fabsf16_test.cpp     |  1 +
 libc/utils/MPFRWrapper/CMakeLists.txt         |  4 ++--
 13 files changed, 60 insertions(+), 23 deletions(-)

diff --git a/libc/src/__support/CPP/type_traits/is_floating_point.h b/libc/src/__support/CPP/type_traits/is_floating_point.h
index d64f8784db0d84..8ae94e16c66332 100644
--- a/libc/src/__support/CPP/type_traits/is_floating_point.h
+++ b/libc/src/__support/CPP/type_traits/is_floating_point.h
@@ -41,7 +41,13 @@ template <typename T> struct is_floating_point {
                               float128
 #endif
                               ,
-                              bfloat16, fputil::Float16, fputil::Float128
+                              bfloat16
+
+                              ,
+                              fputil::Float16
+
+                              ,
+                              fputil::Float128
 
                               ,
                               fputil::Float80>();
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index f82ab4ea0a26a6..eaf7706965efc4 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -52,7 +52,7 @@ max(T x, T y) {
   return constexpr_max(x, y);
 }
 
-#if defined(LIBC_TYPES_HAS_FLOAT16) && !defined(LIBC_USE_SOFT_FLOAT16)
+#ifdef LIBC_TYPES_HAS_FLOAT16
 #if defined(__LIBC_USE_BUILTIN_FMAXF16_FMINF16)
 template <> LIBC_INLINE constexpr float16 max(float16 x, float16 y) {
   if (cpp::is_constant_evaluated())
@@ -69,7 +69,7 @@ template <> LIBC_INLINE constexpr float16 max(float16 x, float16 y) {
   return ((xi > yi) != (xi < 0 && yi < 0)) ? x : y;
 }
 #endif
-#endif // defined(LIBC_TYPES_HAS_FLOAT16) && !defined(LIBC_USE_SOFT_FLOAT16)
+#endif // LIBC_TYPES_HAS_FLOAT16
 
 #if defined(__LIBC_USE_BUILTIN_FMAX_FMIN) && !defined(LIBC_TARGET_ARCH_IS_X86)
 template <> LIBC_INLINE constexpr float max(float x, float y) {
@@ -105,7 +105,7 @@ min(T x, T y) {
   return constexpr_min(x, y);
 }
 
-#if defined(LIBC_TYPES_HAS_FLOAT16) && !defined(LIBC_USE_SOFT_FLOAT16)
+#ifdef LIBC_TYPES_HAS_FLOAT16
 #if defined(__LIBC_USE_BUILTIN_FMAXF16_FMINF16)
 template <> LIBC_INLINE constexpr float16 min(float16 x, float16 y) {
   if (cpp::is_constant_evaluated())
@@ -122,7 +122,7 @@ template <> LIBC_INLINE constexpr float16 min(float16 x, float16 y) {
   return ((xi < yi) != (xi < 0 && yi < 0)) ? x : y;
 }
 #endif
-#endif // defined(LIBC_TYPES_HAS_FLOAT16) && !defined(LIBC_USE_SOFT_FLOAT16)
+#endif // LIBC_TYPES_HAS_FLOAT16
 
 #if defined(__LIBC_USE_BUILTIN_FMAX_FMIN) && !defined(LIBC_TARGET_ARCH_IS_X86)
 template <> LIBC_INLINE constexpr float min(float x, float y) {
diff --git a/libc/src/__support/FPUtil/float16.h b/libc/src/__support/FPUtil/float16.h
index 58672b6f24e748..17abbe7fc4a6c5 100644
--- a/libc/src/__support/FPUtil/float16.h
+++ b/libc/src/__support/FPUtil/float16.h
@@ -1,10 +1,16 @@
-//===-- Definition of float16 data type. ------------------------*- 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of Float16, a software-emulated IEEE 754 binary16 type that is
+/// available regardless of compiler support for _Float16.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT16_H
 #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT16_H
@@ -28,6 +34,11 @@ struct Float16 {
   uint16_t bits;
 
   LIBC_INLINE Float16() = default;
+  LIBC_INLINE constexpr Float16(const Float16 &) = default;
+  LIBC_INLINE constexpr Float16(Float16 &&) = default;
+  LIBC_INLINE constexpr Float16 &operator=(const Float16 &) = default;
+  LIBC_INLINE constexpr Float16 &operator=(Float16 &&) = default;
+  LIBC_INLINE ~Float16() = default;
 
   template <size_t Bits>
   LIBC_INLINE constexpr explicit Float16(const DyadicFloat<Bits> &df)
@@ -40,17 +51,21 @@ struct Float16 {
     if constexpr (cpp::is_floating_point_v<T>) {
       bits = fputil::cast<Float16>(value).bits;
     } else if constexpr (cpp::is_integral_v<T>) {
+      using UnsignedT = cpp::make_unsigned_t<T>;
       Sign sign = Sign::POS;
+      auto unsigned_value = static_cast<UnsignedT>(value);
 
       if constexpr (cpp::is_signed_v<T>) {
         if (value < 0) {
           sign = Sign::NEG;
-          value = -value;
+          // Negate in the unsigned domain so that the minimum value of T does
+          // not overflow.
+          unsigned_value = static_cast<UnsignedT>(-unsigned_value);
         }
       }
 
-      fputil::DyadicFloat<cpp::numeric_limits<cpp::make_unsigned_t<T>>::digits>
-          xd(sign, 0, value);
+      fputil::DyadicFloat<cpp::numeric_limits<UnsignedT>::digits> xd(
+          sign, 0, unsigned_value);
       bits = xd.template as<Float16, /*ShouldSignalExceptions=*/true>().bits;
 
     } else if constexpr (cpp::is_convertible_v<T, Float16>) {
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a4c3f99926fee5..c3b8ac7b6864db 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -3553,8 +3553,8 @@ add_header_library(
     fabsf16.h
   DEPENDS
     libc.include.llvm-libc-macros.float16_macros
-    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.float16
     libc.src.__support.macros.config
   FLAGS
     MISC_MATH_BASIC_OPS_OPT
diff --git a/libc/test/src/__support/FPUtil/comparison_operations_test.cpp b/libc/test/src/__support/FPUtil/comparison_operations_test.cpp
index f8f2b4384d9e83..33ab6830b88f31 100644
--- a/libc/test/src/__support/FPUtil/comparison_operations_test.cpp
+++ b/libc/test/src/__support/FPUtil/comparison_operations_test.cpp
@@ -21,6 +21,7 @@ using LIBC_NAMESPACE::fputil::less_than;
 using LIBC_NAMESPACE::fputil::less_than_or_equals;
 
 using BFloat16 = LIBC_NAMESPACE::fputil::BFloat16;
+using SoftFloat16 = LIBC_NAMESPACE::fputil::Float16;
 
 template <typename T>
 class ComparisonOperationsTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
@@ -336,3 +337,4 @@ TEST_COMPARISON_OPS(DFloat128, float128)
 #endif // LIBC_TYPES_HAS_NATIVE_FLOAT128
 
 TEST_COMPARISON_OPS(BFloat16, BFloat16)
+TEST_COMPARISON_OPS(SoftFloat16, SoftFloat16)
diff --git a/libc/test/src/__support/FPUtil/dyadic_float_test.cpp b/libc/test/src/__support/FPUtil/dyadic_float_test.cpp
index 7aeba365d83cbd..a2a1ae0763f123 100644
--- a/libc/test/src/__support/FPUtil/dyadic_float_test.cpp
+++ b/libc/test/src/__support/FPUtil/dyadic_float_test.cpp
@@ -107,3 +107,4 @@ TEST_EDGE_RANGES(LongDouble, long double);
 #ifdef LIBC_TYPES_HAS_FLOAT16
 TEST_EDGE_RANGES(Float16, float16);
 #endif
+TEST_EDGE_RANGES(SoftFloat16, LIBC_NAMESPACE::fputil::Float16);
diff --git a/libc/test/src/__support/FPUtil/float16_test.cpp b/libc/test/src/__support/FPUtil/float16_test.cpp
index 674afd0fefd1cf..112f041957d9cc 100644
--- a/libc/test/src/__support/FPUtil/float16_test.cpp
+++ b/libc/test/src/__support/FPUtil/float16_test.cpp
@@ -1,17 +1,24 @@
-//===-- Unit tests for float16 type ---------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
+/// Unit tests for the Float16 type.
+///
+//===----------------------------------------------------------------------===//
 
+#include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/float16.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 #include "utils/MPFRWrapper/MPCommon.h"
 
 using Float16 = LIBC_NAMESPACE::fputil::Float16;
+using Float16Bits = LIBC_NAMESPACE::fputil::FPBits<Float16>;
 using LlvmLibcFloat16ConversionTest = LIBC_NAMESPACE::testing::FPTest<Float16>;
 
 // range: [0, inf]
@@ -26,7 +33,7 @@ using MPFRNumber = LIBC_NAMESPACE::testing::mpfr::MPFRNumber;
 
 TEST_F(LlvmLibcFloat16ConversionTest, ToFloatPositiveRange) {
   for (uint16_t bits = POS_START; bits <= POS_STOP; bits++) {
-    Float16 f16_num{bits};
+    Float16 f16_num = Float16Bits(bits).get_val();
     MPFRNumber mpfr_num{f16_num};
 
     // float16 to float
@@ -43,7 +50,7 @@ TEST_F(LlvmLibcFloat16ConversionTest, ToFloatPositiveRange) {
 
 TEST_F(LlvmLibcFloat16ConversionTest, ToFloatNegativeRange) {
   for (uint16_t bits = NEG_START; bits <= NEG_STOP; bits++) {
-    Float16 f16_num{bits};
+    Float16 f16_num = Float16Bits(bits).get_val();
     MPFRNumber mpfr_num{f16_num};
 
     // float16 to float
diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index 3bbc5e27cf6b22..36f76f58a02eed 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -706,13 +706,13 @@ add_fp_unittest(
   NO_RUN_POSTBUILD
   NEED_MPFR
   SUITE
-    libc_math_exhaustive_tests
+    libc-math-exhaustive-tests
   SRCS
     float16_test.cpp
   DEPENDS
     .exhaustive_test
-    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.fp_bits
   LINK_LIBRARIES
     -lpthread
diff --git a/libc/test/src/math/exhaustive/exhaustive_test.h b/libc/test/src/math/exhaustive/exhaustive_test.h
index 9dea202ca972ff..85c0e07e3417f0 100644
--- a/libc/test/src/math/exhaustive/exhaustive_test.h
+++ b/libc/test/src/math/exhaustive/exhaustive_test.h
@@ -16,7 +16,6 @@
 
 #include "src/__support/CPP/type_traits.h"
 #include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/float16.h"
 #include "src/__support/macros/properties/types.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
diff --git a/libc/test/src/math/exhaustive/float16_test.cpp b/libc/test/src/math/exhaustive/float16_test.cpp
index ea0eb9547b001e..04b1af703fc86c 100644
--- a/libc/test/src/math/exhaustive/float16_test.cpp
+++ b/libc/test/src/math/exhaustive/float16_test.cpp
@@ -1,13 +1,19 @@
-//===-- Exhaustive tests for float -> float16 conversion ------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
+/// Exhaustive tests for float -> Float16 conversion.
+///
+//===----------------------------------------------------------------------===//
 
 #include "exhaustive_test.h"
 #include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/float16.h"
 #include "utils/MPFRWrapper/MPCommon.h"
 
 using namespace LIBC_NAMESPACE::fputil;
@@ -54,11 +60,11 @@ using LlvmLibcFloat16FromFloatTest = LlvmLibcFloat16ExhaustiveTest<float>;
 constexpr uint32_t POS_START = 0x0000'0000U;
 constexpr uint32_t POS_STOP = 0x7f80'0000U;
 
-// Negative Range: [-Inf, 0];
-constexpr uint32_t NEG_START = 0xb000'0000U;
+// Negative Range: [-0, -Inf];
+constexpr uint32_t NEG_START = 0x8000'0000U;
 constexpr uint32_t NEG_STOP = 0xff80'0000U;
 
-TEST_F(LlvmLibcFloat16FromFloatTest, PostiveRange) {
+TEST_F(LlvmLibcFloat16FromFloatTest, PositiveRange) {
   test_full_range_all_roundings(POS_START, POS_STOP);
 }
 
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 419b4181229204..3ef3cb4d6472b4 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -252,8 +252,8 @@ add_fp_unittest(
   HDRS
     FAbsTest.h
   DEPENDS
-    libc.src.math.fabsf16
     libc.src.__support.FPUtil.float16
+    libc.src.math.fabsf16
 )
 
 add_fp_unittest(
diff --git a/libc/test/src/math/smoke/fabsf16_test.cpp b/libc/test/src/math/smoke/fabsf16_test.cpp
index b1dc0f479b1ae0..974d2109d964e4 100644
--- a/libc/test/src/math/smoke/fabsf16_test.cpp
+++ b/libc/test/src/math/smoke/fabsf16_test.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "FAbsTest.h"
+
 #include "src/__support/FPUtil/float16.h"
 #include "src/math/fabsf16.h"
 
diff --git a/libc/utils/MPFRWrapper/CMakeLists.txt b/libc/utils/MPFRWrapper/CMakeLists.txt
index dd3832b2b04dff..f15a450585d239 100644
--- a/libc/utils/MPFRWrapper/CMakeLists.txt
+++ b/libc/utils/MPFRWrapper/CMakeLists.txt
@@ -14,9 +14,9 @@ if(LIBC_TESTS_CAN_USE_MPFR OR LIBC_TESTS_CAN_USE_MPC)
     libc.src.__support.CPP.string
     libc.src.__support.CPP.string_view
     libc.src.__support.CPP.type_traits
-    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.float80
     libc.src.__support.FPUtil.fp_bits
     libc.test.UnitTest.FPTestHelpers
@@ -49,8 +49,8 @@ if(LIBC_TESTS_CAN_USE_MPFR)
     libc.hdr.stdint_proxy
     libc.src.__support.CPP.array
     libc.src.__support.CPP.stringstream
-    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.fpbits_str
     libc.test.UnitTest.FPTestHelpers

>From c8721d9bb9bfa3ca7f1e4fb802e579a3686f1cc7 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Mon, 16 Nov 2026 03:56:16 +0200
Subject: [PATCH 5/7] Fix fabsf16 MPFR test without native float16

---
 libc/test/src/math/CMakeLists.txt    | 3 ++-
 libc/test/src/math/fabsf16_test.cpp  | 5 +++++
 libc/utils/MPFRWrapper/MPFRUtils.cpp | 7 +++++++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 2a7ef8e0e186d8..384b465fa834ab 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -339,8 +339,9 @@ add_fp_unittest(
   HDRS
     FAbsTest.h
   DEPENDS
-    libc.src.math.fabsf16
+    libc.src.__support.FPUtil.float16
     libc.src.__support.FPUtil.fp_bits
+    libc.src.math.fabsf16
 )
 
 add_fp_unittest(
diff --git a/libc/test/src/math/fabsf16_test.cpp b/libc/test/src/math/fabsf16_test.cpp
index 2117172b89b5c8..1a9621321fc56b 100644
--- a/libc/test/src/math/fabsf16_test.cpp
+++ b/libc/test/src/math/fabsf16_test.cpp
@@ -13,6 +13,11 @@
 
 #include "FAbsTest.h"
 
+#include "src/__support/FPUtil/float16.h"
 #include "src/math/fabsf16.h"
 
+#ifndef LIBC_TYPES_HAS_FLOAT16
+using float16 = LIBC_NAMESPACE::fputil::Float16;
+#endif // LIBC_TYPES_HAS_FLOAT16
+
 LIST_FABS_TESTS(Fabsf16, float16, LIBC_NAMESPACE::fabsf16)
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index b0ae2f7aa1a342..065e89c0dce8c7 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -288,6 +288,10 @@ template void explain_unary_operation_single_output_error(Operation op,
 template void explain_unary_operation_single_output_error(Operation op,
                                                           bfloat16, bfloat16,
                                                           double, RoundingMode);
+template void explain_unary_operation_single_output_error(Operation op,
+                                                          fputil::Float16,
+                                                          fputil::Float16,
+                                                          double, RoundingMode);
 
 template <typename T>
 void explain_unary_operation_two_outputs_error(
@@ -606,6 +610,9 @@ template bool compare_unary_operation_single_output(Operation, float128,
 template bool compare_unary_operation_single_output(Operation, bfloat16,
                                                     bfloat16, double,
                                                     RoundingMode);
+template bool compare_unary_operation_single_output(Operation, fputil::Float16,
+                                                    fputil::Float16, double,
+                                                    RoundingMode);
 
 template <typename T>
 bool compare_unary_operation_two_outputs(Operation op, T input,

>From 4cd47d37d89fd2d8224591608dfc8576514e9416 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Mon, 16 Nov 2026 04:29:58 +0200
Subject: [PATCH 6/7] Test Float16 conversion from minimum integer values

---
 libc/test/src/__support/FPUtil/float16_test.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/libc/test/src/__support/FPUtil/float16_test.cpp b/libc/test/src/__support/FPUtil/float16_test.cpp
index 112f041957d9cc..265f74aa058158 100644
--- a/libc/test/src/__support/FPUtil/float16_test.cpp
+++ b/libc/test/src/__support/FPUtil/float16_test.cpp
@@ -11,6 +11,7 @@
 ///
 //===----------------------------------------------------------------------===//
 
+#include "src/__support/CPP/limits.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/float16.h"
 #include "test/UnitTest/FPMatcher.h"
@@ -74,6 +75,15 @@ TEST_F(LlvmLibcFloat16ConversionTest, FromInteger) {
   }
 }
 
+TEST_F(LlvmLibcFloat16ConversionTest, FromIntegerMinValue) {
+  EXPECT_FP_EQ(Float16(-32768.0f),
+               Float16(LIBC_NAMESPACE::cpp::numeric_limits<int16_t>::min()));
+  EXPECT_FP_EQ(neg_inf,
+               Float16(LIBC_NAMESPACE::cpp::numeric_limits<int32_t>::min()));
+  EXPECT_FP_EQ(neg_inf,
+               Float16(LIBC_NAMESPACE::cpp::numeric_limits<int64_t>::min()));
+}
+
 TEST_F(LlvmLibcFloat16ConversionTest, CompoundAssignmentOperators) {
   constexpr Float16 VAL[] = {zero,          neg_zero,       inf,
                              neg_inf,       min_normal,     max_normal,

>From 0ad8d31b6e9c708a11e9c90ee7f5e49676d7282a Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 27 Nov 2026 00:54:31 +0200
Subject: [PATCH 7/7] Enable fabsf16 on more targets, document float16 guards,
 add missing includes

---
 libc/config/darwin/x86_64/entrypoints.txt      | 1 +
 libc/config/gpu/spirv/entrypoints.txt          | 1 +
 libc/include/llvm-libc-macros/float16-macros.h | 7 +++++++
 libc/src/__support/FPUtil/CMakeLists.txt       | 3 +++
 libc/src/__support/FPUtil/float16.h            | 3 +++
 libc/test/src/math/exhaustive/float16_test.cpp | 2 +-
 6 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index a85ea90f123e79..6cd016653735b2 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -146,6 +146,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     #libc.src.math.fabs
     #libc.src.math.fabsf
     #libc.src.math.fabsf128
+    libc.src.math.fabsf16
     libc.src.math.fabsf80
     #libc.src.math.fabsl
     #libc.src.math.fdim
diff --git a/libc/config/gpu/spirv/entrypoints.txt b/libc/config/gpu/spirv/entrypoints.txt
index b57dd2dad31bcf..15bc60f1db01ef 100644
--- a/libc/config/gpu/spirv/entrypoints.txt
+++ b/libc/config/gpu/spirv/entrypoints.txt
@@ -98,6 +98,7 @@ set(TARGET_LIBC_ENTRYPOINTS
 
 set(TARGET_LIBM_ENTRYPOINTS
   # math.h entrypoints
+  libc.src.math.fabsf16
   libc.src.math.ldexp
   libc.src.math.ldexpf
   libc.src.math.logbf
diff --git a/libc/include/llvm-libc-macros/float16-macros.h b/libc/include/llvm-libc-macros/float16-macros.h
index 5d9ff66d531ae5..0c233f90b0a8e4 100644
--- a/libc/include/llvm-libc-macros/float16-macros.h
+++ b/libc/include/llvm-libc-macros/float16-macros.h
@@ -11,6 +11,11 @@
 
 #include "../llvm-libc-types/float128.h"
 
+// LIBC_TYPES_HAS_FLOAT16 is defined only when the compiler provides a usable
+// native _Float16: GCC 13+ or Clang 12+, and not on targets where _Float16 is
+// missing or has ABI problems (32-bit ARM, RISC-V, Windows, and i386 without
+// SSE2). When it is not defined, the internal software type fputil::Float16
+// is used instead.
 #if defined(__FLT16_MANT_DIG__) &&                                             \
     (!defined(__GNUC__) || __GNUC__ >= 13 ||                                   \
      (defined(__clang__) && __clang_major__ >= 12)) &&                         \
@@ -19,6 +24,8 @@
 #define LIBC_TYPES_HAS_FLOAT16
 #endif
 
+// Defined when both native _Float16 and native float128 are available, for
+// functions that take or return both types (e.g. f16sqrtf128).
 // TODO: This would no longer be required if HdrGen let us guard function
 // declarations with multiple macros.
 #if defined(LIBC_TYPES_HAS_FLOAT16) && defined(LIBC_TYPES_HAS_NATIVE_FLOAT128)
diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index f887b0cb09e68e..1f0f07a4d6acb4 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -337,8 +337,10 @@ add_header_library(
     .cast
     .comparison_operations
     .dyadic_float
+    .fp_bits
     libc.hdr.stdint_proxy
     libc.src.__support.CPP.bit
+    libc.src.__support.CPP.limits
     libc.src.__support.CPP.type_traits
     libc.src.__support.FPUtil.generic.add_sub
     libc.src.__support.FPUtil.generic.div
@@ -346,6 +348,7 @@ add_header_library(
     libc.src.__support.macros.attributes
     libc.src.__support.macros.config
     libc.src.__support.macros.properties.types
+    libc.src.__support.sign
 )
 
 add_header_library(
diff --git a/libc/src/__support/FPUtil/float16.h b/libc/src/__support/FPUtil/float16.h
index 17abbe7fc4a6c5..4226b49c062c4f 100644
--- a/libc/src/__support/FPUtil/float16.h
+++ b/libc/src/__support/FPUtil/float16.h
@@ -16,7 +16,9 @@
 #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FLOAT16_H
 
 #include "hdr/stdint_proxy.h"
+#include "src/__support/CPP/limits.h"
 #include "src/__support/CPP/type_traits.h"
+#include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/cast.h"
 #include "src/__support/FPUtil/comparison_operations.h"
 #include "src/__support/FPUtil/dyadic_float.h"
@@ -26,6 +28,7 @@
 #include "src/__support/macros/attributes.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/types.h"
+#include "src/__support/sign.h"
 
 namespace LIBC_NAMESPACE_DECL {
 namespace fputil {
diff --git a/libc/test/src/math/exhaustive/float16_test.cpp b/libc/test/src/math/exhaustive/float16_test.cpp
index 04b1af703fc86c..77563347ea8c42 100644
--- a/libc/test/src/math/exhaustive/float16_test.cpp
+++ b/libc/test/src/math/exhaustive/float16_test.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 ///
 /// \file
-/// Exhaustive tests for float -> Float16 conversion.
+/// Exhaustive tests for float to Float16 conversion.
 ///
 //===----------------------------------------------------------------------===//
 



More information about the libc-commits mailing list