Skip to content

Commit cba0fe0

Browse files
committed
Implement rbits and add a test fixture
1 parent 072b6f2 commit cba0fe0

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

libc/src/stdfix/rbits.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "rbits.h"
2+
3+
#include "src/__support/common.h"
4+
#include "src/__support/fixed_point/fx_bits.h"
5+
#include "src/__support/macros/config.h"
6+
7+
namespace LIBC_NAMESPACE_DECL {
8+
9+
LLVM_LIBC_FUNCTION(fract, rbits, (int_r_t x)) {
10+
return fixed_point::fxbits<fract, int_r_t>(x);
11+
}
12+
13+
}

libc/src/stdfix/rbits.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef LLVM_LIBC_SRC_STDFIX_HRBITS_H
2+
#define LLVM_LIBC_SRC_STDFIX_HRBITS_H
3+
4+
#include "include/llvm-libc-macros/stdfix-macros.h"
5+
#include "src/__support/macros/config.h"
6+
namespace LIBC_NAMESPACE_DECL {
7+
8+
fract rbits(int_r_t x);
9+
10+
}
11+
12+
#endif

libc/test/src/stdfix/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
5959
)
6060
endforeach()
6161

62+
foreach(prefix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
63+
add_libc_test(
64+
${prefix}bits_test
65+
SUITE
66+
libc-stdfix-tests
67+
HDRS
68+
FxbitsTest.h
69+
SRCS
70+
${prefix}bits_test.cpp
71+
COMPILE_OPTIONS
72+
-O3
73+
DEPENDS
74+
libc.src.stdfix.${prefix}bits
75+
libc.src.__support.fixed_point.fx_bits
76+
)
77+
endforeach()
78+
6279
add_libc_test(
6380
uhksqrtus_test
6481
SUITE

libc/test/src/stdfix/FxbitsTest.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Utility class to test fxbits -----------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "test/UnitTest/Test.h"
10+
#include "src/__support/fixed_point/fx_rep.h"
11+
12+
template <typename T, typename XType> class FxbitsTest : public LIBC_NAMESPACE::testing::Test {
13+
using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>;
14+
static constexpr T zero = FXRep::ZERO();
15+
static constexpr T min = FXRep::MIN();
16+
static constexpr T max = FXRep::MAX();
17+
static constexpr T half = static_cast<T>(0.5);
18+
static constexpr T neg_half = static_cast<T>(-0.5);
19+
static constexpr T one =
20+
(FXRep::INTEGRAL_LEN > 0) ? static_cast<T>(1) : FXRep::MAX();
21+
static constexpr T neg_one = static_cast<T>(-1);
22+
static constexpr T eps = FXRep::EPS();
23+
24+
public:
25+
typedef T (*FxbitsFunc)(XType);
26+
27+
void testSpecialNumbers(FxbitsFunc func) {
28+
EXPECT_EQ(zero, func(0));
29+
EXPECT_EQ(half, func((XType) (0b1 << (FXRep::FRACTION_LEN - 1)))); // 0.1000...b
30+
}
31+
};
32+
33+
34+
#define LIST_FXBITS_TEST(T, XType, func) \
35+
using LlvmLibcFxbitsTest = FxbitsTest<T, XType>; \
36+
TEST_F(LlvmLibcFxbitsTest, SpecialNumbers) { testSpecialNumbers(&func); } \
37+
static_assert(true, "Require semicolon.")

libc/test/src/stdfix/rbits_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
#include "FxbitsTest.h"
3+
4+
#include "src/stdfix/rbits.h"
5+
6+
LIST_FXBITS_TEST(fract, int_r_t, LIBC_NAMESPACE::rbits);
7+

0 commit comments

Comments
 (0)