-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[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
Changes from 7 commits
cbb321f
cddf53e
bcccd52
647a16c
ca4d532
5c2894f
519ae5d
e0acc36
c31e539
9b499bc
632a6d8
475945d
b146648
659170d
d7bfd7c
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ set( | |
| "fixed_point" | ||
| "cfloat16" | ||
| "cfloat128" | ||
| "padding_on_unsigned_fixed_point" | ||
| ) | ||
|
|
||
| # Making sure ALL_COMPILER_FEATURES is sorted. | ||
|
|
@@ -64,6 +65,8 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES) | |
| set(link_options "") | ||
| if(${feature} STREQUAL "fixed_point") | ||
| list(APPEND compile_options "-ffixed-point") | ||
| elseif(${feature} STREQUAL "padding_on_unsigned_fixed_point") | ||
| list(APPEND compile_options "-ffixed-point -Xclang=-fpadding-on-unsigned-fixed-point") | ||
|
||
| elseif(${feature} MATCHES "^builtin_" OR | ||
| ${feature} STREQUAL "float16_conversion") | ||
| set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT}) | ||
|
|
@@ -112,6 +115,8 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES) | |
| set(LIBC_TYPES_HAS_FLOAT128 TRUE) | ||
| elseif(${feature} STREQUAL "fixed_point") | ||
| set(LIBC_COMPILER_HAS_FIXED_POINT TRUE) | ||
| elseif(${feature} STREQUAL "padding_on_unsigned_fixed_point") | ||
| set(LIBC_COMPILER_HAS_PADDING_ON_UNSIGNED_FIXED_POINT TRUE) | ||
| elseif(${feature} STREQUAL "cfloat16") | ||
| set(LIBC_TYPES_HAS_CFLOAT16 TRUE) | ||
| elseif(${feature} STREQUAL "cfloat128") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #include "include/llvm-libc-macros/stdfix-macros.h" | ||
|
|
||
| #ifndef LIBC_COMPILER_HAS_PADDING_ON_UNSIGNED_FIXED_POINT | ||
| #error unsupported | ||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,8 +12,9 @@ | |
| #include "include/llvm-libc-macros/stdfix-macros.h" | ||
| #include "src/__support/CPP/bit.h" | ||
| #include "src/__support/CPP/type_traits.h" | ||
| #include "src/__support/CPP/limits.h" // numeric_limits | ||
| #include "src/__support/macros/attributes.h" // LIBC_INLINE | ||
| #include "src/__support/macros/config.h" | ||
| #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 zeros | ||
|
||
| template <typename T> | ||
| LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, int> | ||
| countls(T f) { | ||
|
Member
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. guessing |
||
| 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::INTEGRAL_LEN + FXRep::FRACTION_LEN); | ||
|
||
|
|
||
| if constexpr (FXRep::SIGN_LEN != 0) { | ||
| if (x < 0) | ||
|
Member
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 | ||
|
|
||
|
|
||
| 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 |
| 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 |
| 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 |
| 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 |
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.
Can we remove changes on this file and
check_padding_on_unsigned_fixed_point.cppand leave them for other PR? They won't work as intended without changes ininclude/llvm-libc-macros/stdfix-macros.h.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.
Resolved in 632a6d8b