Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
cbb321f
feat: implement template meta `countls`
krishna2803 Feb 1, 2025
cddf53e
chore: add `limits.h` header to CmakeLists.txt
krishna2803 Feb 1, 2025
bcccd52
add: `countlsfx` functions
krishna2803 Feb 1, 2025
647a16c
chore: add `padding_on_unsigned_fixed_point`
krishna2803 Feb 1, 2025
ca4d532
chore: add entrypoints
krishna2803 Feb 1, 2025
5c2894f
add: unit tests for `countlsfx` functions
krishna2803 Feb 1, 2025
519ae5d
chore: documentation update
krishna2803 Feb 1, 2025
e0acc36
chore: remove -Xclang flags and tests with padding
krishna2803 Feb 4, 2025
c31e539
nit: share loop for countlsfx and roundfx
krishna2803 Feb 4, 2025
9b499bc
fix: use `FXRep:TOTAL_LEN` to incoporate for `SIGN_LEN`
krishna2803 Feb 5, 2025
632a6d8
remove: `padding_on_unsigned_fixed_point.cpp`
krishna2803 Feb 5, 2025
475945d
chore: change `count leading zeros` to `count leading sign bits`
krishna2803 Feb 5, 2025
b146648
fix: ensure newline at end
krishna2803 Feb 5, 2025
659170d
style: clang-format
krishna2803 Feb 5, 2025
780fd3a
fix: buildbot failures
krishna2803 Feb 7, 2025
88e9b63
Merge branch 'main' into implement-countlsfx
krishna2803 Feb 10, 2025
c3c1c13
style: clang-format
krishna2803 Feb 10, 2025
ac10f17
Merge branch 'main' into implement-countlsfx
krishna2803 Feb 10, 2025
8790b2c
style: llvm coding standard
krishna2803 Feb 11, 2025
7a34ec1
chore: remove optimization options from maths functions (ref #126315)
krishna2803 Feb 11, 2025
958d51b
Merge branch 'main' into implement-countlsfx
krishna2803 Feb 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion libc/src/__support/fixed_point/fx_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ countls(T f) {
using BitType = typename FXRep::StorageType;
using FXBits = FXBits<T>;

if constexpr (FXRep::SIGN_LEN > 0)
if constexpr (FXRep::SIGN_LEN > 0) {
if (f < 0)
f = bit_not(f);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, you had this correct before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry if im missing something, but the doc says:

// Use braces on the outer `if` to avoid a potential dangling `else`
// situation.
if (isa<VarDecl>(D)) {
  if (shouldProcessAttr(A))
    handleAttr(A);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, that's news to me. I guess because of constexpr we cant make it if (FXRep::SIGN_LEN > 0 && f < 0)... or could we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll just test it and see if it works!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it seems it doesn't..

// clang++ -std=c++20 -Wall hello.cpp -o hello
#include <iostream>

constexpr int x = 1;

int main() {
  int y;
  std::cin >> y;

  if constexpr (x > 0 && y > 4)
    std::cout << "foo bar";
}
hello.cpp:10:17: error: constexpr if condition is not a constant expression
  if constexpr (x > 0 && y > 4)
                ^~~~~~~~~~~~~~
hello.cpp:10:26: note: read of non-const variable 'y' is not allowed in a constant expression
  if constexpr (x > 0 && y > 4)
                         ^
hello.cpp:7:7: note: declared here
  int y;
      ^
1 error generated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here:

In file included from /home/krishna/OpenSource/llvm-project/libc/src/stdfix/countlsr.cpp:11:
/home/krishna/OpenSource/llvm-project/libc/src/__support/fixed_point/fx_bits.h:188:17: error: constexpr if condition is not a constant expression
  188 |   if constexpr (FXRep::SIGN_LEN > 0 && f < 0)
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/krishna/OpenSource/llvm-project/libc/src/stdfix/countlsr.cpp:16:68: note: in instantiation of function template specialization '__llvm_libc_21_0_0_git::fixed_point::countls<_Fract>' requested here
   16 | LLVM_LIBC_FUNCTION(int, countlsr, (fract f)) { return fixed_point::countls(f); }
      |                                                                    ^
/home/krishna/OpenSource/llvm-project/libc/src/__support/fixed_point/fx_bits.h:188:40: note: function parameter 'f' with unknown value cannot be used in a constant expression
  188 |   if constexpr (FXRep::SIGN_LEN > 0 && f < 0)
      |                                        ^
/home/krishna/OpenSource/llvm-project/libc/src/__support/fixed_point/fx_bits.h:183:11: note: declared here
  183 | countls(T f) {
      |           ^
1 error generated.


BitType value_bits = FXBits(f).get_bits();
return cpp::countl_zero(value_bits) - FXRep::SIGN_LEN;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdfix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
SRCS
countls${suffix}_test.cpp
COMPILE_OPTIONS
-O3
${libc_opt_high_flag}
DEPENDS
libc.src.stdfix.countls${suffix}
libc.src.__support.fixed_point.fx_rep
Expand Down
9 changes: 3 additions & 6 deletions libc/test/src/stdfix/CountlsTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,18 @@ template <typename T> class CountlsTest : public LIBC_NAMESPACE::testing::Test {
// bits according to ISO/IEC TR 18037.
EXPECT_EQ(is_signed ? 0 : FXRep::VALUE_LEN, func(min));

if (10 <= static_cast<int>(max)) {
if (10 <= static_cast<int>(max))
EXPECT_EQ(FXRep::INTEGRAL_LEN - 4, func(10));
}

if (static_cast<int>(min) <= -10) {
if (static_cast<int>(min) <= -10)
EXPECT_EQ(FXRep::INTEGRAL_LEN - 4, func(-10));
}

if constexpr (is_signed) {
EXPECT_EQ(FXRep::VALUE_LEN, func(-zero));
EXPECT_EQ(FXRep::VALUE_LEN, func(-eps));
EXPECT_EQ(FXRep::INTEGRAL_LEN + 1, func(-one_half));
if (FXRep::FRACTION_LEN >= 2) {
if (FXRep::FRACTION_LEN >= 2)
EXPECT_EQ(FXRep::INTEGRAL_LEN + 2, func(-one_fourth));
}
}
}
};
Expand Down