-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f54208e
[libc][stdfix] Implement fxdivi functions
bojle 040e833
[libc][stdfix] add a couple more tests and error info for divi
bojle 71d283b
[libc] add a few tests to handles edge cases with FRACT_{MIN/MAX}
bojle cfe5122
[libc] fix formatting for rdivi.cpp
bojle c614888
[libc] comments, remove extra iterations, and misc changes
bojle 40cf04a
[libc] formatting
bojle 744463c
[libc] INT_MIN handling
bojle f5ff751
[libc] formatting
bojle 31bbc8b
[libc] multiply and divide by 2 when INT_MIN
bojle b631411
[libc] specially handle po2 cases
bojle 73a93c3
[libc] add special handling for po2
bojle 303dc94
[libc] use abs from algorithm.h
bojle 07e1de8
[libc] add doc, fix func name case
bojle c2b4b99
[libc] TotalBits -> TOTAL_BITS
bojle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,6 +224,80 @@ idiv(T x, T y) { | |
return static_cast<XType>(result); | ||
} | ||
|
||
LIBC_INLINE long accum nrstep(long accum d, long accum x0) { | ||
auto v = x0 * (2.lk - (d * x0)); | ||
return v; | ||
} | ||
|
||
/* Divide the two integers and return a fixed_point value | ||
* | ||
* For reference, see: | ||
* https://en.wikipedia.org/wiki/Division_algorithm#Newton%E2%80%93Raphson_division | ||
* https://stackoverflow.com/a/9231996 | ||
*/ | ||
template <typename XType> LIBC_INLINE constexpr XType divi(int n, int d) { | ||
// If the value of the second operand of the / operator is zero, the | ||
// behavior is undefined. Ref: ISO/IEC TR 18037:2008(E) p.g. 16 | ||
LIBC_CRASH_ON_VALUE(d, 0); | ||
|
||
bojle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (LIBC_UNLIKELY(n == 0)) { | ||
return FXRep<XType>::ZERO(); | ||
} | ||
bool result_is_negative = (n < 0) ^ (d < 0); | ||
bojle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
unsigned int nv = static_cast<unsigned int>(n < 0 ? -n : n); | ||
unsigned int dv = static_cast<unsigned int>(d < 0 ? -d : d); | ||
unsigned int clz = cpp::countl_zero<unsigned int>(dv) - 1; | ||
bojle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
unsigned long int scaled_val = dv << clz; | ||
/* Scale denominator to be in the range of [0.5,1] */ | ||
bojle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
FXBits<long accum> d_scaled{scaled_val}; | ||
unsigned long int scaled_val_n = nv << clz; | ||
/* Scale the numerator as much as the denominator to maintain correctness of | ||
* the original equation | ||
*/ | ||
FXBits<long accum> n_scaled{scaled_val_n}; | ||
long accum n_scaled_val = n_scaled.get_val(); | ||
long accum d_scaled_val = d_scaled.get_val(); | ||
/* x0 = (48/17) - (32/17) * d_n */ | ||
long accum a = 2.8235lk; /* 48/17 */ | ||
long accum b = 1.8823lk; /* 32/17 */ | ||
|
||
/* Error of the initial approximation, as derived | ||
* from the wikipedia article is | ||
* E0 = 1/17 = 0.059 (5.9%) | ||
*/ | ||
long accum initial_approx = a - (b * d_scaled_val); | ||
bojle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/* Each newton-raphson iteration will square the error, due | ||
* to quadratic convergence. So, | ||
* E1 = (0.059)^2 = 0.0034 | ||
*/ | ||
long accum val = nrstep(d_scaled_val, initial_approx); | ||
/* E2 = 0.0000121 */ | ||
val = nrstep(d_scaled_val, val); | ||
/* E3 = 1.468e−10 */ | ||
bojle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
val = nrstep(d_scaled_val, val); | ||
/* E4 = 2.155e−20 */ | ||
val = nrstep(d_scaled_val, val); | ||
|
||
long accum res = n_scaled_val * val; | ||
|
||
if (result_is_negative) { | ||
res *= static_cast<long accum>(-1); | ||
} | ||
|
||
// Check for overflow before returning | ||
long accum max_val = static_cast<long accum>(FXRep<XType>::MAX()); | ||
long accum min_val = static_cast<long accum>(FXRep<XType>::MIN()); | ||
|
||
/* Per clause 7.18a.6.1, saturate values on overflow */ | ||
if (res > max_val) { | ||
return FXRep<XType>::MAX(); | ||
} else if (res < min_val) { | ||
return FXRep<XType>::MIN(); | ||
} else { | ||
return static_cast<XType>(res); | ||
} | ||
} | ||
|
||
} // namespace fixed_point | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "include/llvm-libc-macros/stdfix-macros.h" // fract | ||
#include "rdivi.h" | ||
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//===-- 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" | ||
|
||
#include <stdio.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.
Outdated
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
|
||
XType epsilon = get_epsilon<XType>(); | ||
EXPECT_EQ(func(0, 10), 0.r); | ||
EXPECT_EQ(func(0, -10), 0.r); | ||
EXPECT_EQ(func(-(1 << FRACT_FBIT), 1 << FRACT_FBIT), FRACT_MIN); | ||
EXPECT_EQ(func((1 << FRACT_FBIT) - 1, 1 << FRACT_FBIT), FRACT_MAX); | ||
/* From Section 7.18a.6.1, functions returning a fixed-point value, the | ||
* return value is saturated on overflow. */ | ||
EXPECT_EQ(func(INT_MAX, INT_MAX), FRACT_MAX); | ||
EXPECT_LT(func(INT_MAX - 1, INT_MAX) - 0.99999999r, epsilon); | ||
EXPECT_EQ(func(INT_MIN, INT_MAX), FRACT_MIN); | ||
/* Expecting 0 here as fract is not precise enough to | ||
* handle 1/INT_MAX | ||
*/ | ||
EXPECT_LT(func(1, INT_MAX) - 0.r, epsilon); | ||
/* This results in 1.1739, which should be saturated to FRACT_MAX */ | ||
EXPECT_EQ(func(27, 23), FRACT_MAX); | ||
} | ||
}; | ||
|
||
#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.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.