[libc-commits] [libc] [libc][stdfix] Implement sqrtfx for remaining fixed point types (PR #214741)

via libc-commits libc-commits at lists.llvm.org
Fri Aug 7 07:43:39 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: sohail (sohail103)

<details>
<summary>Changes</summary>

This PR implements sqrtfx for the remaining fixed point types (unsigned long accum and all signed variants).

```c
unsigned short fract sqrthr(short fract x);
unsigned fract sqrtr(fract x);
unsigned long fract sqrtlr(long fract x);
unsigned short accum sqrthk(short accum x);
unsigned accum sqrtk(accum x);
unsigned long accum sqrtlk(long accum x);
unsigned long accum sqrtulk(unsigned long accum x);
```

---

Patch is 38.82 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/214741.diff


37 Files Affected:

- (modified) libc/config/baremetal/arm/entrypoints.txt (+9-3) 
- (modified) libc/config/baremetal/riscv/entrypoints.txt (+9-3) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+9-3) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+9-3) 
- (modified) libc/docs/headers/stdfix.rst (+1-1) 
- (modified) libc/include/stdfix.yaml (+59-10) 
- (modified) libc/src/__support/fixed_point/CMakeLists.txt (+1) 
- (modified) libc/src/__support/fixed_point/sqrt.h (+84-29) 
- (modified) libc/src/stdfix/CMakeLists.txt (+1-1) 
- (added) libc/src/stdfix/sqrthk.cpp (+20) 
- (added) libc/src/stdfix/sqrthk.h (+21) 
- (added) libc/src/stdfix/sqrthr.cpp (+20) 
- (added) libc/src/stdfix/sqrthr.h (+21) 
- (added) libc/src/stdfix/sqrtk.cpp (+20) 
- (added) libc/src/stdfix/sqrtk.h (+21) 
- (added) libc/src/stdfix/sqrtlk.cpp (+20) 
- (added) libc/src/stdfix/sqrtlk.h (+21) 
- (added) libc/src/stdfix/sqrtlr.cpp (+20) 
- (added) libc/src/stdfix/sqrtlr.h (+21) 
- (added) libc/src/stdfix/sqrtr.cpp (+20) 
- (added) libc/src/stdfix/sqrtr.h (+21) 
- (added) libc/src/stdfix/sqrtulk.cpp (+20) 
- (added) libc/src/stdfix/sqrtulk.h (+21) 
- (modified) libc/test/src/stdfix/CMakeLists.txt (+1-1) 
- (modified) libc/test/src/stdfix/SqrtTest.h (+48-26) 
- (added) libc/test/src/stdfix/sqrthk_test.cpp (+13) 
- (added) libc/test/src/stdfix/sqrthr_test.cpp (+13) 
- (added) libc/test/src/stdfix/sqrtk_test.cpp (+13) 
- (added) libc/test/src/stdfix/sqrtlk_test.cpp (+13) 
- (added) libc/test/src/stdfix/sqrtlr_test.cpp (+13) 
- (added) libc/test/src/stdfix/sqrtr_test.cpp (+13) 
- (modified) libc/test/src/stdfix/sqrtuhk_test.cpp (+2-1) 
- (modified) libc/test/src/stdfix/sqrtuhr_test.cpp (+2-1) 
- (modified) libc/test/src/stdfix/sqrtuk_test.cpp (+1-1) 
- (added) libc/test/src/stdfix/sqrtulk_test.cpp (+14) 
- (modified) libc/test/src/stdfix/sqrtulr_test.cpp (+2-1) 
- (modified) libc/test/src/stdfix/sqrtur_test.cpp (+1-1) 


``````````diff
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 71ecbe438bf7b..d6e30c8ee5e16 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -923,12 +923,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.roundulk
     libc.src.stdfix.roundulr
     libc.src.stdfix.roundur
-    libc.src.stdfix.sqrtuhk
     libc.src.stdfix.sqrtuhr
-    libc.src.stdfix.sqrtuk
     libc.src.stdfix.sqrtur
-    # libc.src.stdfix.sqrtulk
     libc.src.stdfix.sqrtulr
+    libc.src.stdfix.sqrtuhk
+    libc.src.stdfix.sqrtuk
+    libc.src.stdfix.sqrtulk
+    libc.src.stdfix.sqrthr
+    libc.src.stdfix.sqrtr
+    libc.src.stdfix.sqrtlr
+    libc.src.stdfix.sqrthk
+    libc.src.stdfix.sqrtk
+    libc.src.stdfix.sqrtlk
     libc.src.stdfix.uhksqrtus
     libc.src.stdfix.uksqrtui
     libc.src.stdfix.hrbits
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index b38f60ab9d5f5..3aab854f028e7 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -920,12 +920,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.roundulk
     libc.src.stdfix.roundulr
     libc.src.stdfix.roundur
-    libc.src.stdfix.sqrtuhk
     libc.src.stdfix.sqrtuhr
-    libc.src.stdfix.sqrtuk
     libc.src.stdfix.sqrtur
-    # libc.src.stdfix.sqrtulk
     libc.src.stdfix.sqrtulr
+    libc.src.stdfix.sqrtuhk
+    libc.src.stdfix.sqrtuk
+    libc.src.stdfix.sqrtulk
+    libc.src.stdfix.sqrthr
+    libc.src.stdfix.sqrtr
+    libc.src.stdfix.sqrtlr
+    libc.src.stdfix.sqrthk
+    libc.src.stdfix.sqrtk
+    libc.src.stdfix.sqrtlk
     libc.src.stdfix.uhksqrtus
     libc.src.stdfix.uksqrtui
     libc.src.stdfix.hrbits
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index c6a4b431f33de..7396b30274315 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1137,12 +1137,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.roundulk
     libc.src.stdfix.roundulr
     libc.src.stdfix.roundur
-    libc.src.stdfix.sqrtuhk
     libc.src.stdfix.sqrtuhr
-    libc.src.stdfix.sqrtuk
     libc.src.stdfix.sqrtur
-    # libc.src.stdfix.sqrtulk
     libc.src.stdfix.sqrtulr
+    libc.src.stdfix.sqrtuhk
+    libc.src.stdfix.sqrtuk
+    libc.src.stdfix.sqrtulk
+    libc.src.stdfix.sqrthr
+    libc.src.stdfix.sqrtr
+    libc.src.stdfix.sqrtlr
+    libc.src.stdfix.sqrthk
+    libc.src.stdfix.sqrtk
+    libc.src.stdfix.sqrtlk
     libc.src.stdfix.uhksqrtus
     libc.src.stdfix.uksqrtui
     libc.src.stdfix.hrbits
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index fb0886e2e650a..25dfa84483ec2 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1142,12 +1142,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.roundulk
     libc.src.stdfix.roundulr
     libc.src.stdfix.roundur
-    libc.src.stdfix.sqrtuhk
     libc.src.stdfix.sqrtuhr
-    libc.src.stdfix.sqrtuk
     libc.src.stdfix.sqrtur
-    # libc.src.stdfix.sqrtulk
     libc.src.stdfix.sqrtulr
+    libc.src.stdfix.sqrtuhk
+    libc.src.stdfix.sqrtuk
+    libc.src.stdfix.sqrtulk
+    libc.src.stdfix.sqrthr
+    libc.src.stdfix.sqrtr
+    libc.src.stdfix.sqrtlr
+    libc.src.stdfix.sqrthk
+    libc.src.stdfix.sqrtk
+    libc.src.stdfix.sqrtlk
     libc.src.stdfix.uhksqrtus
     libc.src.stdfix.uksqrtui
     libc.src.stdfix.hrbits
diff --git a/libc/docs/headers/stdfix.rst b/libc/docs/headers/stdfix.rst
index 91dad81c595b0..049ed4846e28b 100644
--- a/libc/docs/headers/stdfix.rst
+++ b/libc/docs/headers/stdfix.rst
@@ -114,7 +114,7 @@ floating point types, but are not part of the ISO/IEC TR 18037:2008 spec.
 +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
 | sin           |                |             |               |            |                |             |                |             |               |            |                |             |
 +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
-| sqrt          | |check|        |             | |check|       |            | |check|        |             | |check|        |             | |check|       |            |                |             |
+| sqrt          | |check|        | |check|     | |check|       | |check|    | |check|        | |check|     | |check|        | |check|     | |check|       | |check|    | |check|        | |check|     |
 +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
 | tan           |                |             |               |            |                |             |                |             |               |            |                |             |
 +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
diff --git a/libc/include/stdfix.yaml b/libc/include/stdfix.yaml
index f91e4ea620e3e..eecfa86262f96 100644
--- a/libc/include/stdfix.yaml
+++ b/libc/include/stdfix.yaml
@@ -410,21 +410,37 @@ functions:
       - type: unsigned fract
       - type: int
     guard: LIBC_COMPILER_HAS_FIXED_POINT
-  - name: sqrtuhk
+  - name: sqrtuhr
     standards:
       - stdc_ext
       - llvm_libc_stdfix_ext
-    return_type: unsigned short accum
+    return_type: unsigned short fract
     arguments:
-      - type: unsigned short accum
+      - type: unsigned short fract
     guard: LIBC_COMPILER_HAS_FIXED_POINT
-  - name: sqrtuhr
+  - name: sqrtur
     standards:
       - stdc_ext
       - llvm_libc_stdfix_ext
-    return_type: unsigned short fract
+    return_type: unsigned fract
     arguments:
-      - type: unsigned short fract
+      - type: unsigned fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: sqrtulr
+    standards:
+      - stdc_ext
+      - llvm_libc_stdfix_ext
+    return_type: unsigned long fract
+    arguments:
+      - type: unsigned long fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: sqrtuhk
+    standards:
+      - stdc_ext
+      - llvm_libc_stdfix_ext
+    return_type: unsigned short accum
+    arguments:
+      - type: unsigned short accum
     guard: LIBC_COMPILER_HAS_FIXED_POINT
   - name: sqrtuk
     standards:
@@ -436,12 +452,29 @@ functions:
     guard: LIBC_COMPILER_HAS_FIXED_POINT
   - name: sqrtulk
     standards:
+      - stdc_ext
       - llvm_libc_stdfix_ext
     return_type: unsigned long accum
     arguments:
       - type: unsigned long accum
     guard: LIBC_COMPILER_HAS_FIXED_POINT
-  - name: sqrtulr
+  - name: sqrthr
+    standards:
+      - stdc_ext
+      - llvm_libc_stdfix_ext
+    return_type: unsigned short fract
+    arguments:
+      - type: short fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: sqrtr
+    standards:
+      - stdc_ext
+      - llvm_libc_stdfix_ext
+    return_type: unsigned fract
+    arguments:
+      - type: fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: sqrtlr
     standards:
       - stdc_ext
       - llvm_libc_stdfix_ext
@@ -449,13 +482,29 @@ functions:
     arguments:
       - type: unsigned long fract
     guard: LIBC_COMPILER_HAS_FIXED_POINT
-  - name: sqrtur
+  - name: sqrthk
     standards:
       - stdc_ext
       - llvm_libc_stdfix_ext
-    return_type: unsigned fract
+    return_type: unsigned short accum
     arguments:
-      - type: unsigned fract
+      - type: short accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: sqrtk
+    standards:
+      - stdc_ext
+      - llvm_libc_stdfix_ext
+    return_type: unsigned accum
+    arguments:
+      - type: accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: sqrtlk
+    standards:
+      - stdc_ext
+      - llvm_libc_stdfix_ext
+    return_type: unsigned long accum
+    arguments:
+      - type: long accum
     guard: LIBC_COMPILER_HAS_FIXED_POINT
   - name: uhksqrtus
     standards:
diff --git a/libc/src/__support/fixed_point/CMakeLists.txt b/libc/src/__support/fixed_point/CMakeLists.txt
index c1b0ab30e0307..7e9d542683499 100644
--- a/libc/src/__support/fixed_point/CMakeLists.txt
+++ b/libc/src/__support/fixed_point/CMakeLists.txt
@@ -38,4 +38,5 @@ add_header_library(
     libc.src.__support.CPP.bit
     libc.src.__support.CPP.limits
     libc.src.__support.CPP.type_traits
+    libc.src.__support.uint128
 )
diff --git a/libc/src/__support/fixed_point/sqrt.h b/libc/src/__support/fixed_point/sqrt.h
index b77319debeeaf..6ab520a00408a 100644
--- a/libc/src/__support/fixed_point/sqrt.h
+++ b/libc/src/__support/fixed_point/sqrt.h
@@ -13,11 +13,13 @@
 #include "src/__support/CPP/bit.h"
 #include "src/__support/CPP/limits.h" // CHAR_BIT
 #include "src/__support/CPP/type_traits.h"
-#include "src/__support/macros/attributes.h"   // LIBC_INLINE
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"   // LIBC_CRASH_ON_VALUE
 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
 
 #include "fx_rep.h"
+#include "src/__support/uint128.h"
 
 #ifdef LIBC_COMPILER_HAS_FIXED_POINT
 
@@ -131,8 +133,25 @@ template <> struct SqrtConfig<unsigned int> {
   using HalfType = unsigned short;
 };
 
-// TODO: unsigned long accum type is 64-bit, and will need 64-bit fract type.
-// Probably we will use DyadicFloat<64> for intermediate computations instead.
+template <typename T> struct FXUnsigned;
+template <> struct FXUnsigned<short fract> {
+  using Type = unsigned short fract;
+};
+template <> struct FXUnsigned<fract> {
+  using Type = unsigned fract;
+};
+template <> struct FXUnsigned<long fract> {
+  using Type = unsigned long fract;
+};
+template <> struct FXUnsigned<short accum> {
+  using Type = unsigned short accum;
+};
+template <> struct FXUnsigned<accum> {
+  using Type = unsigned accum;
+};
+template <> struct FXUnsigned<long accum> {
+  using Type = unsigned long accum;
+};
 
 } // namespace internal
 
@@ -147,7 +166,7 @@ sqrt_core(typename Config::Type x_frac) {
   if (x_frac == FXRep::ONE_FOURTH())
     return FXRep::ONE_HALF();
 
-  // Use use Newton method to approximate sqrt(a):
+  // Use Newton method to approximate sqrt(a):
   //   x_{n + 1} = 1/2 (x_n + a / x_n)
   // For the initial values, we choose x_0
 
@@ -181,31 +200,67 @@ sqrt_core(typename Config::Type x_frac) {
 
 template <typename T>
 LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, T> sqrt(T x) {
-  using BitType = typename FXRep<T>::StorageType;
-  BitType x_bit = cpp::bit_cast<BitType>(x);
-
-  if (LIBC_UNLIKELY(x_bit == 0))
-    return FXRep<T>::ZERO();
-
-  int leading_zeros = cpp::countl_zero(x_bit);
-  constexpr int STORAGE_LENGTH = sizeof(BitType) * CHAR_BIT;
-  constexpr int EXP_ADJUSTMENT = STORAGE_LENGTH - FXRep<T>::FRACTION_LEN - 1;
-  // x_exp is the real exponent of the leading bit of x.
-  int x_exp = EXP_ADJUSTMENT - leading_zeros;
-  int shift = EXP_ADJUSTMENT - 1 - (x_exp & (~1));
-  // Normalize.
-  x_bit <<= shift;
-  using FracType = typename internal::SqrtConfig<T>::Type;
-  FracType x_frac = cpp::bit_cast<FracType>(x_bit);
-
-  // Compute sqrt(x_frac) using Newton-method.
-  FracType r = sqrt_core<internal::SqrtConfig<T>>(x_frac);
-
-  // Re-scaling
-  r >>= EXP_ADJUSTMENT - (x_exp >> 1);
-
-  // Return result.
-  return cpp::bit_cast<T>(r);
+  if constexpr (FXRep<T>::SIGN_LEN > 0) {
+    LIBC_CRASH_ON_VALUE(x < FXRep<T>::ZERO(), true);
+    return static_cast<T>(
+        sqrt(static_cast<typename internal::FXUnsigned<T>::Type>(x)));
+  } else {
+    using BitType = typename FXRep<T>::StorageType;
+    BitType x_bit = cpp::bit_cast<BitType>(x);
+
+    if (LIBC_UNLIKELY(x_bit == 0))
+      return FXRep<T>::ZERO();
+
+    int leading_zeros = cpp::countl_zero(x_bit);
+    constexpr int STORAGE_LENGTH = sizeof(BitType) * CHAR_BIT;
+    constexpr int EXP_ADJUSTMENT = STORAGE_LENGTH - FXRep<T>::FRACTION_LEN - 1;
+    // x_exp is the real exponent of the leading bit of x.
+    int x_exp = EXP_ADJUSTMENT - leading_zeros;
+    int shift = EXP_ADJUSTMENT - 1 - (x_exp & (~1));
+    // Normalize.
+    x_bit <<= shift;
+
+    if constexpr (STORAGE_LENGTH > 32) {
+      using SeedConfig = internal::SqrtConfig<unsigned long fract>;
+      using SeedType = SeedConfig::Type;
+      using SeedStorageType = typename FXRep<SeedType>::StorageType;
+      constexpr int SEED_LENGTH = sizeof(SeedStorageType) * CHAR_BIT;
+
+      static_assert(STORAGE_LENGTH <= 2 * SEED_LENGTH,
+                    "Output storage type has to be lesser than or equal to "
+                    "double the seed table's width for accurate results.");
+
+      // Seed from the widest available table using the top half of x, then
+      // widen to full precision. No native fract type exists at this storage
+      // width to build a table for directly.
+      SeedStorageType x_top_half =
+          static_cast<SeedStorageType>(x_bit >> SEED_LENGTH);
+      SeedType x_seed = cpp::bit_cast<SeedType>(x_top_half);
+      SeedType r_seed = sqrt_core<SeedConfig>(x_seed);
+
+      UInt128 r0 = static_cast<UInt128>(cpp::bit_cast<SeedStorageType>(r_seed))
+                   << SEED_LENGTH;
+
+      UInt128 x_wide = static_cast<UInt128>(x_bit) << STORAGE_LENGTH;
+      UInt128 r1 = (r0 + x_wide / r0) >> 1;
+
+      r1 >>= EXP_ADJUSTMENT - (x_exp >> 1);
+      BitType r_bits = static_cast<BitType>(r1);
+      return cpp::bit_cast<T>(r_bits);
+    } else {
+      using FracType = typename internal::SqrtConfig<T>::Type;
+      FracType x_frac = cpp::bit_cast<FracType>(x_bit);
+
+      // Compute sqrt(x_frac) using Newton-method.
+      FracType r = sqrt_core<internal::SqrtConfig<T>>(x_frac);
+
+      // Re-scaling
+      r >>= EXP_ADJUSTMENT - (x_exp >> 1);
+
+      // Return result.
+      return cpp::bit_cast<T>(r);
+    }
+  }
 }
 
 // Integer square root - Accurate version:
diff --git a/libc/src/stdfix/CMakeLists.txt b/libc/src/stdfix/CMakeLists.txt
index b7121407e0307..627b56fe35de0 100644
--- a/libc/src/stdfix/CMakeLists.txt
+++ b/libc/src/stdfix/CMakeLists.txt
@@ -14,7 +14,7 @@ foreach(suffix IN ITEMS hr r lr hk k lk)
   )
 endforeach()
 
-foreach(suffix IN ITEMS uhr ur ulr uhk uk)
+foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
   add_entrypoint_object(
     sqrt${suffix}
     HDRS
diff --git a/libc/src/stdfix/sqrthk.cpp b/libc/src/stdfix/sqrthk.cpp
new file mode 100644
index 0000000000000..200e2942131e2
--- /dev/null
+++ b/libc/src/stdfix/sqrthk.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of sqrthk function ---------------------------------===//
+//
+// 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 "sqrthk.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/sqrt.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(unsigned short accum, sqrthk, (short accum x)) {
+  return fixed_point::sqrt(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/sqrthk.h b/libc/src/stdfix/sqrthk.h
new file mode 100644
index 0000000000000..2924104c57e0a
--- /dev/null
+++ b/libc/src/stdfix/sqrthk.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for sqrthk ------------------------*- 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_STDFIX_SQRTHK_H
+#define LLVM_LIBC_SRC_STDFIX_SQRTHK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+unsigned short accum sqrthk(short accum x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_SQRTHK_H
diff --git a/libc/src/stdfix/sqrthr.cpp b/libc/src/stdfix/sqrthr.cpp
new file mode 100644
index 0000000000000..e3eb0904fb5f0
--- /dev/null
+++ b/libc/src/stdfix/sqrthr.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of sqrthr function ---------------------------------===//
+//
+// 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 "sqrthr.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/sqrt.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(unsigned short fract, sqrthr, (short fract x)) {
+  return fixed_point::sqrt(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/sqrthr.h b/libc/src/stdfix/sqrthr.h
new file mode 100644
index 0000000000000..352ca1d405af3
--- /dev/null
+++ b/libc/src/stdfix/sqrthr.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for sqrthr ------------------------*- 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_STDFIX_SQRTHR_H
+#define LLVM_LIBC_SRC_STDFIX_SQRTHR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+unsigned short fract sqrthr(short fract x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_SQRTHR_H
diff --git a/libc/src/stdfix/sqrtk.cpp b/libc/src/stdfix/sqrtk.cpp
new file mode 100644
index 0000000000000..66c17a37c264d
--- /dev/null
+++ b/libc/src/stdfix/sqrtk.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of sqrtk function ----------------------------------===//
+//
+// 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 "sqrtk.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/sqrt.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(unsigned accum, sqrtk, (accum x)) {
+  return fixed_point::sqrt(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/sqrtk.h b/libc/src/stdfix/sqrtk.h
new file mode 100644
index 0000000000000..f01e4a5da9ad0
--- /dev/null
+++ b/libc/src/stdfix/sqrtk.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for sqrtk -------------------------*- 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_STDFIX_SQRTK_H
+#define LLVM_LIBC_SRC_STDFIX_SQRTK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+unsigned accum sqrtk(accum x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_SQRTK_H
diff --git a/libc/src/stdfix/sqrtlk.cpp b/libc/src/stdfix/sqrtlk.cpp
new file mode 100644
index 0000000000000..a4c6bf1419157
--- /dev/null
+++ b/libc/src/stdfix/sqrtlk.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of sqrtlk function ---------------------------------===//
+//
+// Part of the L...
[truncated]

``````````

</details>


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


More information about the libc-commits mailing list