-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[libc][stdfix] Implement fxdivi functions (rdivi) #154914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//===-- Implementation of rdivi 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 "rdivi.h" | ||
#include "include/llvm-libc-macros/stdfix-macros.h" // fract | ||
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION | ||
#include "src/__support/fixed_point/fx_bits.h" // fixed_point | ||
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(fract, rdivi, (int a, int b)) { | ||
return fixed_point::divi<fract>(a,b); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//===-- Implementation header for rdivi ------------------------*- 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_RDIVI_H | ||
#define LLVM_LIBC_SRC_STDFIX_RDIVI_H | ||
|
||
#include "include/llvm-libc-macros/stdfix-macros.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
fract rdivi(int a, int b); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_STDFIX_RDIVI_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//===-- Utility class to test fxdivi functions ------------------*- 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 "src/__support/CPP/type_traits.h" | ||
#include "src/__support/fixed_point/fx_bits.h" | ||
#include "src/__support/fixed_point/fx_rep.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
template <typename XType> XType get_epsilon() = delete; | ||
template <> fract get_epsilon() { return FRACT_EPSILON; } | ||
template <> unsigned fract get_epsilon() { return UFRACT_EPSILON; } | ||
template <> long fract get_epsilon() { return LFRACT_EPSILON; } | ||
|
||
template <typename XType> | ||
class DivITest : public LIBC_NAMESPACE::testing::Test { | ||
using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<XType>; | ||
using FXBits = LIBC_NAMESPACE::fixed_point::FXBits<XType>; | ||
|
||
public: | ||
typedef XType (*DivIFunc)(int, int); | ||
|
||
void testBasic(DivIFunc func) { | ||
bojle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
XType epsilon = get_epsilon<XType>(); | ||
EXPECT_LT((func(2, 3) - 0.666656494140625r), epsilon); | ||
EXPECT_LT((func(3, 4) - 0.75r), epsilon); | ||
EXPECT_LT((func(1043, 2764) - 0.3773516643r), epsilon); | ||
EXPECT_LT((func(60000, 720293) - 0.08329943509r), epsilon); | ||
|
||
EXPECT_EQ(func(128, 256), 0.5r); | ||
EXPECT_EQ(func(1, 2), 0.5r); | ||
EXPECT_EQ(func(1, 4), 0.25r); | ||
EXPECT_EQ(func(1, 8), 0.125r); | ||
EXPECT_EQ(func(1, 16), 0.0625r); | ||
|
||
EXPECT_EQ(func(-1, 2), -0.5r); | ||
EXPECT_EQ(func(1, -4), -0.25r); | ||
EXPECT_EQ(func(-1, 8), -0.125r); | ||
EXPECT_EQ(func(1, -16), -0.0625r); | ||
} | ||
|
||
void testSpecial(DivIFunc func) { | ||
bojle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
EXPECT_EQ(func(0,10), 0.r); | ||
EXPECT_EQ(func(0,-10), 0.r); | ||
EXPECT_EQ(func(-32768,32768), FRACT_MIN); | ||
EXPECT_EQ(func(32767,32768), FRACT_MAX); | ||
Comment on lines
+49
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will only be true if the number of fractional bits for a
|
||
EXPECT_EQ(func(INT_MAX,INT_MAX), 1.0r); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct but it had me second guessing a little because of some nuances with the spec I had to look up. Per
so
so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tangentially, it also might be good to add some tests which would trigger the saturated overflow case. So maybe some integral values which would resolve to either less than zero or greater than or equal to one which would saturate to zero and |
||
EXPECT_EQ(func(INT_MAX-1,INT_MAX), 0.99999999r); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
EXPECT_EQ(func(INT_MIN,INT_MAX), FRACT_MIN); | ||
/* Expecting 0 here as fract is not precise enough to | ||
* handle 1/INT_MAX | ||
*/ | ||
EXPECT_EQ(func(1, INT_MAX), 0.r); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to before, rounding can be either up or down, so I think we also want to use the epsilon approach here. |
||
} | ||
}; | ||
|
||
#define LIST_DIVI_TESTS(Name, XType, func) \ | ||
using LlvmLibc##Name##diviTest = DivITest<XType>; \ | ||
TEST_F(LlvmLibc##Name##diviTest, Basic) { testBasic(&func); } \ | ||
TEST_F(LlvmLibc##Name##diviTest, Special) { testSpecial(&func); } \ | ||
static_assert(true, "Require semicolon.") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//===-- Unittests for rdivi -----------------------------------------------===// | ||
// | ||
// 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 "DivITest.h" | ||
|
||
#include "llvm-libc-macros/stdfix-macros.h" // fract | ||
#include "src/stdfix/rdivi.h" | ||
|
||
LIST_DIVI_TESTS(r, fract, LIBC_NAMESPACE::rdivi); |
Uh oh!
There was an error while loading. Please reload this page.