-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc][math][c23] fmul correcly rounded to all rounding modes #91537
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
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
9f16ac2
start fmul template
92d66d8
format code
f00264c
make actual llvm impl.
beaea4e
format code
54dee21
move fmul function to generic/fmul.cpp
3bf640d
fix tests
0c290ce
clang format
c33af75
add more test cases
7f6ca5a
format code
4cb0426
add more test cases
0433ad0
format code
5b7da13
delete dmull and fmull stuff
4e3bc60
update
6eba323
format code
6970e8a
make the b formula
48b3460
format code
378e6be
make the tests pass
6d2b55d
format code
bc080c5
check for undefined behavior
94bd877
format code
bc958c2
fix bug and add tests
8af1658
format code
d557571
add special input test suite
f0cdabe
add tests
18fbdb6
format code
c3be569
refactor
9c3e7a7
format code
2c51d27
refactor
b483faa
format code
fa2d429
clean code
5acf04d
format code
c41adb2
refactor
ad6c2cf
format
a12fc67
update
a27e3aa
format code
a877230
refactor
eee370b
format code
d54dfd2
update math docs
3ded176
address review
c6f9ef9
format code
44a2778
address review
8a14205
format code
6dba1c9
address review
0e5f889
format code
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
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
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
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,18 @@ | ||
//===-- Implementation header for fmul --------------------------*- 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_MATH_FMUL_H | ||
#define LLVM_LIBC_SRC_MATH_FMUL_H | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
float fmul(double x, double y); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_MATH_FMUL_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,128 @@ | ||
//===-- Implementation of fmul 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 "src/math/fmul.h" | ||
#include "src/__support/CPP/bit.h" | ||
#include "src/__support/FPUtil/BasicOperations.h" | ||
#include "src/__support/FPUtil/FPBits.h" | ||
#include "src/__support/FPUtil/rounding_mode.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/uint128.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
LLVM_LIBC_FUNCTION(float, fmul, (double x, double y)) { | ||
auto x_bits = fputil::FPBits<double>(x); | ||
|
||
auto y_bits = fputil::FPBits<double>(y); | ||
|
||
auto output_sign = (x_bits.sign() != y_bits.sign()) ? Sign::NEG : Sign::POS; | ||
|
||
if (LIBC_UNLIKELY(x_bits.is_inf_or_nan() || y_bits.is_inf_or_nan() || | ||
x_bits.is_zero() || y_bits.is_zero())) { | ||
if (x_bits.is_nan()) | ||
return static_cast<float>(x); | ||
if (y_bits.is_nan()) | ||
return static_cast<float>(y); | ||
if (x_bits.is_inf()) | ||
return y_bits.is_zero() | ||
? fputil::FPBits<float>::quiet_nan().get_val() | ||
: fputil::FPBits<float>::inf(output_sign).get_val(); | ||
if (y_bits.is_inf()) | ||
return x_bits.is_zero() | ||
? fputil::FPBits<float>::quiet_nan().get_val() | ||
: fputil::FPBits<float>::inf(output_sign).get_val(); | ||
// Now either x or y is zero, and the other one is finite. | ||
return fputil::FPBits<float>::zero(output_sign).get_val(); | ||
} | ||
|
||
uint64_t mx, my; | ||
|
||
// Get mantissa and append the hidden bit if needed. | ||
mx = x_bits.get_explicit_mantissa(); | ||
my = y_bits.get_explicit_mantissa(); | ||
|
||
// Get the corresponding biased exponent. | ||
int ex = x_bits.get_explicit_exponent(); | ||
int ey = y_bits.get_explicit_exponent(); | ||
|
||
// Count the number of leading zeros of the explicit mantissas. | ||
int nx = cpp::countl_zero(mx); | ||
int ny = cpp::countl_zero(my); | ||
// Shift the leading 1 bit to the most significant bit. | ||
mx <<= nx; | ||
my <<= ny; | ||
|
||
// Adjust exponent accordingly: If x or y are normal, we will only need to | ||
// shift by (exponent length + sign bit = 11 bits. If x or y are denormal, we | ||
// will need to shift more than 11 bits. | ||
ex -= (nx - 11); | ||
ey -= (ny - 11); | ||
|
||
UInt128 product = static_cast<UInt128>(mx) * static_cast<UInt128>(my); | ||
int32_t dm1; | ||
uint64_t highs, lows; | ||
uint64_t g, hight, lowt; | ||
uint32_t m; | ||
uint32_t b; | ||
int c; | ||
|
||
highs = static_cast<uint64_t>(product >> 64); | ||
c = static_cast<int>(highs >= 0x8000000000000000); | ||
lows = static_cast<uint64_t>(product); | ||
|
||
lowt = (lows != 0); | ||
|
||
dm1 = ex + ey + c + fputil::FPBits<float>::EXP_BIAS; | ||
|
||
int round_mode = fputil::quick_get_round(); | ||
if (dm1 >= 255) { | ||
if ((round_mode == FE_TOWARDZERO) || | ||
(round_mode == FE_UPWARD && output_sign.is_neg()) || | ||
(round_mode == FE_DOWNWARD && output_sign.is_pos())) { | ||
return fputil::FPBits<float>::max_normal(output_sign).get_val(); | ||
} | ||
return fputil::FPBits<float>::inf().get_val(); | ||
} else if (dm1 <= 0) { | ||
|
||
int m_shift = 40 + c - dm1; | ||
int g_shift = m_shift - 1; | ||
int h_shift = 64 - g_shift; | ||
m = (m_shift >= 64) ? 0 : static_cast<uint32_t>(highs >> m_shift); | ||
|
||
g = g_shift >= 64 ? 0 : (highs >> g_shift) & 1; | ||
hight = h_shift >= 64 ? highs : (highs << h_shift) != 0; | ||
|
||
dm1 = 0; | ||
} else { | ||
m = static_cast<uint32_t>(highs >> (39 + c)); | ||
g = (highs >> (38 + c)) & 1; | ||
hight = (highs << (26 - c)) != 0; | ||
} | ||
|
||
if (round_mode == FE_TONEAREST) { | ||
b = g && ((hight && lowt) || ((m & 1) != 0)); | ||
} else if ((output_sign.is_neg() && round_mode == FE_DOWNWARD) || | ||
(output_sign.is_pos() && round_mode == FE_UPWARD)) { | ||
b = (g == 0 && (hight && lowt) == 0) ? 0 : 1; | ||
} else { | ||
b = 0; | ||
} | ||
|
||
uint32_t exp16 = (dm1 << 23); | ||
|
||
uint32_t m2 = m & fputil::FPBits<float>::FRACTION_MASK; | ||
|
||
uint32_t result = (exp16 + m2) + b; | ||
|
||
auto result_bits = fputil::FPBits<float>(result); | ||
result_bits.set_sign(output_sign); | ||
return result_bits.get_val(); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
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,104 @@ | ||
//===-- Utility class to test fmul[f|l] ---------------------*- 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_TEST_SRC_MATH_SMOKE_FMULTEST_H | ||
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMULTEST_H | ||
|
||
#include "test/UnitTest/FEnvSafeTest.h" | ||
#include "test/UnitTest/FPMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
template <typename T, typename R> | ||
class FmulTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { | ||
|
||
DECLARE_SPECIAL_CONSTANTS(T) | ||
|
||
public: | ||
typedef T (*FMulFunc)(R, R); | ||
|
||
void testMul(FMulFunc func) { | ||
|
||
EXPECT_FP_EQ_ALL_ROUNDING(T(15.0), func(3.0, 5.0)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(T(0x1.0p-130), func(0x1.0p1, 0x1.0p-131)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(T(0x1.0p-127), func(0x1.0p2, 0x1.0p-129)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(T(1.0), func(1.0, 1.0)); | ||
|
||
EXPECT_FP_EQ_ALL_ROUNDING(T(0.0), func(-0.0, -0.0)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.0), func(0.0, -0.0)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.0), func(-0.0, 0.0)); | ||
|
||
EXPECT_FP_EQ_ROUNDING_NEAREST(inf, func(0x1.0p100, 0x1.0p100)); | ||
EXPECT_FP_EQ_ROUNDING_UPWARD(inf, func(0x1.0p100, 0x1.0p100)); | ||
EXPECT_FP_EQ_ROUNDING_DOWNWARD(max_normal, func(0x1.0p100, 0x1.0p100)); | ||
EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(max_normal, func(0x1.0p100, 0x1.0p100)); | ||
|
||
EXPECT_FP_EQ_ROUNDING_NEAREST( | ||
0x1p0, func(1.0, 1.0 + 0x1.0p-128 + 0x1.0p-149 + 0x1.0p-150)); | ||
EXPECT_FP_EQ_ROUNDING_DOWNWARD( | ||
0x1p0, func(1.0, 1.0 + 0x1.0p-128 + 0x1.0p-149 + 0x1.0p-150)); | ||
EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO( | ||
0x1p0, func(1.0, 1.0 + 0x1.0p-128 + 0x1.0p-149 + 0x1.0p-150)); | ||
EXPECT_FP_EQ_ROUNDING_UPWARD( | ||
0x1p0, func(1.0, 1.0 + 0x1.0p-128 + 0x1.0p-149 + 0x1.0p-150)); | ||
|
||
EXPECT_FP_EQ_ROUNDING_NEAREST( | ||
0x1.0p-128f + 0x1.0p-148f, | ||
func(1.0, 0x1.0p-128 + 0x1.0p-149 + 0x1.0p-150)); | ||
EXPECT_FP_EQ_ROUNDING_UPWARD( | ||
0x1.0p-128f + 0x1.0p-148f, | ||
func(1.0, 0x1.0p-128 + 0x1.0p-149 + 0x1.0p-150)); | ||
EXPECT_FP_EQ_ROUNDING_DOWNWARD( | ||
0x1.0p-128f + 0x1.0p-149f, | ||
func(1.0, 0x1.0p-128 + 0x1.0p-149 + 0x1.0p-150)); | ||
EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO( | ||
0x1.0p-128f + 0x1.0p-149f, | ||
func(1.0, 0x1.0p-128 + 0x1.0p-149 + 0x1.0p-150)); | ||
} | ||
|
||
void testSpecialInputs(FMulFunc func) { | ||
EXPECT_FP_EQ_ALL_ROUNDING(inf, func(inf, 0x1.0p-129)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(inf, func(0x1.0p-129, inf)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(inf, func(inf, 2.0)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(inf, func(3.0, inf)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(0.0, func(0.0, 0.0)); | ||
|
||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(neg_inf, aNaN)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(aNaN, neg_inf)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(inf, func(neg_inf, neg_inf)); | ||
|
||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(0.0, neg_inf)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(neg_inf, 0.0)); | ||
|
||
EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, func(neg_inf, 1.0)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, func(1.0, neg_inf)); | ||
|
||
EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, func(neg_inf, 0x1.0p-129)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, func(0x1.0p-129, neg_inf)); | ||
|
||
EXPECT_FP_EQ_ALL_ROUNDING(0.0, func(0.0, 0x1.0p-129)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(inf, 0.0)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(0.0, inf)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(0.0, aNaN)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(2.0, aNaN)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(0x1.0p-129, aNaN)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(inf, aNaN)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(aNaN, aNaN)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(0.0, sNaN)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(2.0, sNaN)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(0x1.0p-129, sNaN)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(inf, sNaN)); | ||
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(sNaN, sNaN)); | ||
} | ||
}; | ||
|
||
#define LIST_FMUL_TESTS(T, R, func) \ | ||
using LlvmLibcFmulTest = FmulTest<T, R>; \ | ||
TEST_F(LlvmLibcFmulTest, Mul) { testMul(&func); } \ | ||
TEST_F(LlvmLibcFmulTest, NaNInf) { testSpecialInputs(&func); } | ||
|
||
#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMULTEST_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//===-- Unittests for fmul-------------------------------------------------===// | ||
// | ||
// 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 "FMulTest.h" | ||
|
||
#include "src/math/fmul.h" | ||
|
||
LIST_FMUL_TESTS(float, double, LIBC_NAMESPACE::fmul) |
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.