20 changes: 20 additions & 0 deletions libc/src/stdfix/sqrtur.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for sqrtur ------------------------*- 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_SQRTUR_H
#define LLVM_LIBC_SRC_STDFIX_SQRTUR_H

#include "include/llvm-libc-macros/stdfix-macros.h"

namespace LIBC_NAMESPACE {

unsigned fract sqrtur(unsigned fract x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDFIX_SQRTUR_H
22 changes: 22 additions & 0 deletions libc/test/src/stdfix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ foreach(suffix IN ITEMS hr r lr hk k lk)
)
endforeach()

foreach(suffix IN ITEMS uhr ur ulr uhk uk)
add_libc_test(
sqrt${suffix}_test
SUITE
libc-stdfix-tests
HDRS
SqrtTest.h
SRCS
sqrt${suffix}_test.cpp
COMPILE_OPTIONS
-O3
-ffixed-point
DEPENDS
libc.src.stdfix.sqrt${suffix}
libc.src.__support.CPP.bit
libc.src.__support.fixed_point.fx_rep
libc.src.__support.fixed_point.sqrt
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.sqrt
)
endforeach()

foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
add_libc_test(
round${suffix}_test
Expand Down
66 changes: 66 additions & 0 deletions libc/test/src/stdfix/SqrtTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//===-- Utility class to test fixed-point sqrt ------------------*- 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
//
//===----------------------------------------------------------------------===//

#include "test/UnitTest/Test.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/FPUtil/BasicOperations.h"
#include "src/__support/FPUtil/sqrt.h"
#include "src/__support/fixed_point/fx_rep.h"
#include "src/__support/fixed_point/sqrt.h"

template <typename T> class SqrtTest : public LIBC_NAMESPACE::testing::Test {

using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>;
static constexpr T zero = FXRep::ZERO();
static constexpr T min = FXRep::MIN();
static constexpr T max = FXRep::MAX();
static constexpr T half = static_cast<T>(0.5);
static constexpr T quarter = static_cast<T>(0.25);
static constexpr T one =
(FXRep::INTEGRAL_LEN > 0) ? static_cast<T>(1) : FXRep::MAX();
static constexpr T eps = FXRep::EPS();

public:
typedef T (*SqrtFunc)(T);

void testSpecialNumbers(SqrtFunc func) {
EXPECT_EQ(zero, func(zero));
EXPECT_EQ(half, func(quarter));

if constexpr (FXRep::INTEGRAL_LEN) {
EXPECT_EQ(one, func(one));
EXPECT_EQ(static_cast<T>(2.0), func(static_cast<T>(4.0)));
}

using StorageType = typename FXRep::StorageType;

constexpr size_t COUNT = 255;
constexpr StorageType STEP =
~StorageType(0) / static_cast<StorageType>(COUNT);
constexpr double ERR = 3.0 * static_cast<double>(eps);
StorageType x = 0;
for (size_t i = 0; i < COUNT; ++i, x += STEP) {
T v = LIBC_NAMESPACE::cpp::bit_cast<T>(x);
double v_d = static_cast<double>(v);
double errors = LIBC_NAMESPACE::fputil::abs(
static_cast<double>(func(v)) - LIBC_NAMESPACE::fputil::sqrt(v_d));
if (errors > ERR) {
// Print out the failure input and output.
EXPECT_EQ(v, zero);
EXPECT_EQ(func(v), zero);
}
ASSERT_TRUE(errors <= ERR);
}
}
};

#define LIST_SQRT_TESTS(T, func) \
using LlvmLibcSqrtTest = SqrtTest<T>; \
TEST_F(LlvmLibcSqrtTest, SpecialNumbers) { testSpecialNumbers(&func); } \
static_assert(true, "Require semicolon.")
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/sqrtuhk_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for sqrtuhk ---------------------------------------------===//
//
// 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 "SqrtTest.h"

#include "src/stdfix/sqrtuhk.h"

LIST_SQRT_TESTS(unsigned short accum, LIBC_NAMESPACE::sqrtuhk);
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/sqrtuhr_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for sqrtuhr ---------------------------------------------===//
//
// 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 "SqrtTest.h"

#include "src/stdfix/sqrtuhr.h"

LIST_SQRT_TESTS(unsigned short fract, LIBC_NAMESPACE::sqrtuhr);
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/sqrtuk_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for sqrtuk ----------------------------------------------===//
//
// 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 "SqrtTest.h"

#include "src/stdfix/sqrtuk.h"

LIST_SQRT_TESTS(unsigned accum, LIBC_NAMESPACE::sqrtuk);
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/sqrtulr_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for sqrtulr ---------------------------------------------===//
//
// 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 "SqrtTest.h"

#include "src/stdfix/sqrtulr.h"

LIST_SQRT_TESTS(unsigned long fract, LIBC_NAMESPACE::sqrtulr);
13 changes: 13 additions & 0 deletions libc/test/src/stdfix/sqrtur_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for sqrtur ----------------------------------------------===//
//
// 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 "SqrtTest.h"

#include "src/stdfix/sqrtur.h"

LIST_SQRT_TESTS(unsigned fract, LIBC_NAMESPACE::sqrtur);