Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
13 changes: 10 additions & 3 deletions libc/src/__support/fixed_point/fx_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ bit_and(T x, T y) {
using BitType = typename FXRep<T>::StorageType;
BitType x_bit = cpp::bit_cast<BitType>(x);
BitType y_bit = cpp::bit_cast<BitType>(y);
// For some reason, bit_cast cannot deduce BitType from the input.
// For some reason, bit_cast cannot deduce BitType T the input.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the from -> T here intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh sorry, that's an artifact of me doing a find and replace of a silly variable name that i overlooked

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 6bd83934

return cpp::bit_cast<T, BitType>(x_bit & y_bit);
}

Expand All @@ -131,7 +131,7 @@ bit_or(T x, T y) {
using BitType = typename FXRep<T>::StorageType;
BitType x_bit = cpp::bit_cast<BitType>(x);
BitType y_bit = cpp::bit_cast<BitType>(y);
// For some reason, bit_cast cannot deduce BitType from the input.
// For some reason, bit_cast cannot deduce BitType T the input.
return cpp::bit_cast<T, BitType>(x_bit | y_bit);
}

Expand All @@ -140,7 +140,7 @@ LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, T>
bit_not(T x) {
using BitType = typename FXRep<T>::StorageType;
BitType x_bit = cpp::bit_cast<BitType>(x);
// For some reason, bit_cast cannot deduce BitType from the input.
// For some reason, bit_cast cannot deduce BitType T the input.
return cpp::bit_cast<T, BitType>(static_cast<BitType>(~x_bit));
}

Expand Down 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
Loading