Skip to content

[libc][stdfix] Implement fixed point bitsfx functions in llvm libc #128413

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 19 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
libc.src.stdfix.ukbits
libc.src.stdfix.lkbits
libc.src.stdfix.ulkbits
libc.src.stdfix.bitshr
libc.src.stdfix.bitsr
libc.src.stdfix.bitslr
libc.src.stdfix.bitshk
libc.src.stdfix.bitsk
libc.src.stdfix.bitslk
libc.src.stdfix.bitsuhr
libc.src.stdfix.bitsur
libc.src.stdfix.bitsulr
libc.src.stdfix.bitsuhk
libc.src.stdfix.bitsuk
libc.src.stdfix.bitsulk
libc.src.stdfix.countlshr
libc.src.stdfix.countlsr
libc.src.stdfix.countlslr
Expand Down
12 changes: 12 additions & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
libc.src.stdfix.ukbits
libc.src.stdfix.lkbits
libc.src.stdfix.ulkbits
libc.src.stdfix.bitshr
libc.src.stdfix.bitsr
libc.src.stdfix.bitslr
libc.src.stdfix.bitshk
libc.src.stdfix.bitsk
libc.src.stdfix.bitslk
libc.src.stdfix.bitshr
libc.src.stdfix.bitsur
libc.src.stdfix.bitsulr
libc.src.stdfix.bitsuhk
libc.src.stdfix.bitsuk
libc.src.stdfix.bitsulk
libc.src.stdfix.countlshr
libc.src.stdfix.countlsr
libc.src.stdfix.countlslr
Expand Down
12 changes: 12 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
# TODO: https://github.com/llvm/llvm-project/issues/115778
libc.src.stdfix.lkbits
libc.src.stdfix.ulkbits
libc.src.stdfix.bitshr
libc.src.stdfix.bitsr
libc.src.stdfix.bitslr
libc.src.stdfix.bitshk
libc.src.stdfix.bitsk
libc.src.stdfix.bitslk
libc.src.stdfix.bitsuhr
libc.src.stdfix.bitsur
libc.src.stdfix.bitsulr
libc.src.stdfix.bitsuhk
libc.src.stdfix.bitsuk
libc.src.stdfix.bitsulk
libc.src.stdfix.countlshr
libc.src.stdfix.countlsr
libc.src.stdfix.countlslr
Expand Down
12 changes: 12 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
libc.src.stdfix.ukbits
libc.src.stdfix.lkbits
libc.src.stdfix.ulkbits
libc.src.stdfix.bitshr
libc.src.stdfix.bitsr
libc.src.stdfix.bitslr
libc.src.stdfix.bitshk
libc.src.stdfix.bitsk
libc.src.stdfix.bitslk
libc.src.stdfix.bitsuhr
libc.src.stdfix.bitsur
libc.src.stdfix.bitsulr
libc.src.stdfix.bitsuhk
libc.src.stdfix.bitsuk
libc.src.stdfix.bitsulk
libc.src.stdfix.countlshr
libc.src.stdfix.countlsr
libc.src.stdfix.countlslr
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/headers/math/stdfix.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ The following functions are included in the ISO/IEC TR 18037:2008 standard.
+===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
| abs | | |check| | | |check| | | |check| | | |check| | | |check| | | |check| |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| bits\* | | | | | | | | | | | | |
| bits\* | |check| | |check| | |check| | |check| | |check| | |check| | |check| | |check| | |check| | |check| | |check| | |check| |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| \*bits | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
Expand Down
84 changes: 84 additions & 0 deletions libc/include/stdfix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,90 @@ functions:
arguments:
- type: uint_ulk_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitshr
standards:
- stdc_ext
return_type: int_hr_t
arguments:
- type: short fract
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitsuhr
standards:
- stdc_ext
return_type: uint_uhr_t
arguments:
- type: unsigned short fract
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitsr
standards:
- stdc_ext
return_type: int_r_t
arguments:
- type: fract
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitsur
standards:
- stdc_ext
return_type: uint_ur_t
arguments:
- type: unsigned fract
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitslr
standards:
- stdc_ext
return_type: int_lr_t
arguments:
- type: long fract
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitsulr
standards:
- stdc_ext
return_type: uint_ulr_t
arguments:
- type: unsigned long fract
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitshk
standards:
- stdc_ext
return_type: int_hk_t
arguments:
- type: short accum
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitsuhk
standards:
- stdc_ext
return_type: uint_uhk_t
arguments:
- type: unsigned short accum
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitsk
standards:
- stdc_ext
return_type: int_k_t
arguments:
- type: accum
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitsuk
standards:
- stdc_ext
return_type: uint_uk_t
arguments:
- type: unsigned accum
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitslk
standards:
- stdc_ext
return_type: int_lk_t
arguments:
- type: long accum
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: bitsulk
standards:
- stdc_ext
return_type: uint_ulk_t
arguments:
- type: unsigned long accum
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: roundhk
standards:
- stdc_ext
Expand Down
7 changes: 7 additions & 0 deletions libc/src/__support/fixed_point/fx_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ countls(T f) {
return cpp::countl_zero(value_bits) - FXRep::SIGN_LEN;
}

// fixed-point to integer conversion
template <typename T, typename XType>
LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, XType>
bitsfx(T f) {
return cpp::bit_cast<XType, T>(f);
}

} // namespace fixed_point
} // namespace LIBC_NAMESPACE_DECL

Expand Down
14 changes: 14 additions & 0 deletions libc/src/stdfix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
libc.src.__support.fixed_point.fx_bits
)

add_entrypoint_object(
bits${suffix}
HDRS
bits${suffix}.h
SRCS
bits${suffix}.cpp
COMPILE_OPTIONS
${libc_opt_high_flag}
DEPENDS
libc.src.__support.fixed_point.fx_bits
libc.include.llvm-libc-types.stdfix-types
libc.include.llvm-libc-macros.stdfix_macros
)

add_entrypoint_object(
countls${suffix}
HDRS
Expand Down
22 changes: 22 additions & 0 deletions libc/src/stdfix/bitshk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation of bitshk 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 "bitshk.h"
#include "include/llvm-libc-macros/stdfix-macros.h" // short accum
#include "include/llvm-libc-types/stdfix-types.h" // int_hk_t
#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(int_hk_t, bitshk, (short accum f)) {
return fixed_point::bitsfx<short accum, int_hk_t>(f);
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/stdfix/bitshk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for bitshk 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_BITSHK_H
#define LLVM_LIBC_SRC_STDFIX_BITSHK_H

#include "include/llvm-libc-macros/stdfix-macros.h" // short accum
#include "include/llvm-libc-types/stdfix-types.h" // int_hk_t
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL

namespace LIBC_NAMESPACE_DECL {

int_hk_t bitshk(short accum f);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STDFIX_BITSHK_H
22 changes: 22 additions & 0 deletions libc/src/stdfix/bitshr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation of bitshr 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 "bitshr.h"
#include "include/llvm-libc-macros/stdfix-macros.h" // short fract
#include "include/llvm-libc-types/stdfix-types.h" // int_hr_t
#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(int_hr_t, bitshr, (short fract f)) {
return fixed_point::bitsfx<short fract, int_hr_t>(f);
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/stdfix/bitshr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for bitshr 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_BITSHR_H
#define LLVM_LIBC_SRC_STDFIX_BITSHR_H

#include "include/llvm-libc-macros/stdfix-macros.h" // short fract
#include "include/llvm-libc-types/stdfix-types.h" // int_hr_t
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL

namespace LIBC_NAMESPACE_DECL {

int_hr_t bitshr(short fract f);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STDFIX_BITSHR_H
22 changes: 22 additions & 0 deletions libc/src/stdfix/bitsk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation for bitsk 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 "bitsk.h"
#include "include/llvm-libc-macros/stdfix-macros.h" // accum
#include "include/llvm-libc-types/stdfix-types.h" // int_k_t
#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(int_k_t, bitsk, (accum f)) {
return fixed_point::bitsfx<accum, int_k_t>(f);
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/stdfix/bitsk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for bitsk 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_BITSK_H
#define LLVM_LIBC_SRC_STDFIX_BITSK_H

#include "include/llvm-libc-macros/stdfix-macros.h" // accum
#include "include/llvm-libc-types/stdfix-types.h" // int_k_t
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL

namespace LIBC_NAMESPACE_DECL {

int_k_t bitsk(accum f);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STDFIX_BITSK_H
22 changes: 22 additions & 0 deletions libc/src/stdfix/bitslk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation for bitslk 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 "bitslk.h"
#include "include/llvm-libc-macros/stdfix-macros.h" // long accum
#include "include/llvm-libc-types/stdfix-types.h" // int_lk_t
#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(int_lk_t, bitslk, (long accum f)) {
return fixed_point::bitsfx<long accum, int_lk_t>(f);
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/stdfix/bitslk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for bitslk 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_BITSLK_H
#define LLVM_LIBC_SRC_STDFIX_BITSLK_H

#include "include/llvm-libc-macros/stdfix-macros.h" // long accum
#include "include/llvm-libc-types/stdfix-types.h" // int_lk_t
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL

namespace LIBC_NAMESPACE_DECL {

int_lk_t bitslk(long accum f);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STDFIX_BITSLK_H
Loading