-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][stdfix] Implement fixed point countlsfx
functions in llvm-libc
#125356
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 all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cbb321f
feat: implement template meta `countls`
krishna2803 cddf53e
chore: add `limits.h` header to CmakeLists.txt
krishna2803 bcccd52
add: `countlsfx` functions
krishna2803 647a16c
chore: add `padding_on_unsigned_fixed_point`
krishna2803 ca4d532
chore: add entrypoints
krishna2803 5c2894f
add: unit tests for `countlsfx` functions
krishna2803 519ae5d
chore: documentation update
krishna2803 e0acc36
chore: remove -Xclang flags and tests with padding
krishna2803 c31e539
nit: share loop for countlsfx and roundfx
krishna2803 9b499bc
fix: use `FXRep:TOTAL_LEN` to incoporate for `SIGN_LEN`
krishna2803 632a6d8
remove: `padding_on_unsigned_fixed_point.cpp`
krishna2803 475945d
chore: change `count leading zeros` to `count leading sign bits`
krishna2803 b146648
fix: ensure newline at end
krishna2803 659170d
style: clang-format
krishna2803 d7bfd7c
Merge branch 'main' into implement-countlsfx
krishna2803 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,10 @@ | |
|
||
#include "include/llvm-libc-macros/stdfix-macros.h" | ||
#include "src/__support/CPP/bit.h" | ||
#include "src/__support/CPP/limits.h" // numeric_limits | ||
#include "src/__support/CPP/type_traits.h" | ||
#include "src/__support/macros/attributes.h" // LIBC_INLINE | ||
#include "src/__support/macros/config.h" | ||
#include "src/__support/macros/attributes.h" // LIBC_INLINE | ||
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY | ||
#include "src/__support/math_extras.h" | ||
|
||
|
@@ -50,6 +51,12 @@ template <typename T> struct FXBits { | |
static constexpr StorageType SIGN_MASK = | ||
(fx_rep::SIGN_LEN == 0 ? 0 : StorageType(1) << SIGN_OFFSET); | ||
|
||
// mask for <integral | fraction> | ||
static constexpr StorageType VALUE_MASK = INTEGRAL_MASK | FRACTION_MASK; | ||
|
||
// mask for <sign | integral | fraction> | ||
static constexpr StorageType TOTAL_MASK = SIGN_MASK | VALUE_MASK; | ||
|
||
public: | ||
LIBC_INLINE constexpr FXBits() = default; | ||
|
||
|
@@ -74,6 +81,12 @@ template <typename T> struct FXBits { | |
return (value & INTEGRAL_MASK) >> INTEGRAL_OFFSET; | ||
} | ||
|
||
// returns complete bitstring representation the fixed point number | ||
// the bitstring is of the form: padding | sign | integral | fraction | ||
LIBC_INLINE constexpr StorageType get_bits() { | ||
return (value & TOTAL_MASK) >> FRACTION_OFFSET; | ||
} | ||
|
||
// TODO: replace bool with Sign | ||
LIBC_INLINE constexpr bool get_sign() { | ||
return static_cast<bool>((value & SIGN_MASK) >> SIGN_OFFSET); | ||
|
@@ -163,6 +176,26 @@ template <typename T> LIBC_INLINE constexpr T round(T x, int n) { | |
return bit_and((x + round_bit), rounding_mask); | ||
} | ||
|
||
// count leading sign bits | ||
template <typename T> | ||
LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, int> | ||
countls(T f) { | ||
using FXRep = FXRep<T>; | ||
using BitType = typename FXRep::StorageType; | ||
using FXBits = FXBits<T>; | ||
|
||
constexpr int CONTAIN_LEN = cpp::numeric_limits<BitType>::digits; | ||
constexpr int PADDING_LEN = CONTAIN_LEN - FXRep::TOTAL_LEN; | ||
|
||
if constexpr (FXRep::SIGN_LEN != 0) { | ||
if (x < 0) | ||
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. From the buildbot failures:
|
||
x = bit_not(x); | ||
} | ||
|
||
BitType value_bits = FXBits(x)::get_bits(); | ||
return cpp::countl_zero(value_bits) - PADDING_LEN; | ||
} | ||
|
||
} // 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,20 @@ | ||
//===-- Implementation for countlshk 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 "countlshk.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/fixed_point/fx_bits.h" | ||
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(int, countlshk, (short accum f)) { | ||
return fixed_point::countls(f); | ||
} | ||
|
||
} // 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 countlshk function ------------*- 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_COUNTLSHK_H | ||
#define LLVM_LIBC_SRC_STDFIX_COUNTLSHK_H | ||
|
||
#include "include/llvm-libc-macros/stdfix-macros.h" | ||
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int countlshk(short accum f); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_STDFIX_COUNTLSHK_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,20 @@ | ||
//===-- Implementation for countlshr 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 "countlshr.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/fixed_point/fx_bits.h" | ||
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(int, countlshr, (short fract f)) { | ||
return fixed_point::countls(f); | ||
} | ||
|
||
} // 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 countlshr function ------------*- 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_COUNTLSHR_H | ||
#define LLVM_LIBC_SRC_STDFIX_COUNTLSHR_H | ||
|
||
#include "include/llvm-libc-macros/stdfix-macros.h" | ||
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int countlshr(short fract f); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_STDFIX_COUNTLSHR_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,18 @@ | ||
//===-- Implementation for countlsk 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 "countlsk.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/fixed_point/fx_bits.h" | ||
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(int, countlsk, (accum f)) { return fixed_point::countls(f); } | ||
|
||
} // 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 countlsk function -------------*- 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_COUNTLSK_H | ||
#define LLVM_LIBC_SRC_STDFIX_COUNTLSK_H | ||
|
||
#include "include/llvm-libc-macros/stdfix-macros.h" | ||
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int countlsk(accum f); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_STDFIX_COUNTLSK_H |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guessing
x
below was supposed to bef
?