llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.29k stars 12.1k forks source link

Implement fixed point bitsfx functions in llvm-libc #113359

Open PiJoules opened 1 month ago

PiJoules commented 1 month ago

Some fixed point functions from ISO 18037 are implemented in llvm-libc, but not all of them are implemented. The various bitsfx functions should also be added. Copying from the extension:

4.1.7.4 The bitwise fixed-point to integer conversion functions

The bitwise fixed-point to integer conversion functions bitsfx, where fx stands for one of hr, r, lr, hk, k, lk, uhr, ur, ulr, uhk, uk or ulk, take one fixed-point type argument (corresponding to fx); the type of the function is an implementation-defined integer type int_fx_t (for signed fixed-point types) or uint_fx_t (for unsigned fixed-point types), defined in the <stdfix.h> headerfile, that is large enough to hold all the bits in the fixed-point type.

The bitwise fixed-point to integer conversion functions return an integer value equal to the fixedpoint value of the argument multiplied by 2^F, where F is the number of fractional bits in the fixed point type. The result type is an integer type big enough to hold all valid result values for the given fixed-point argument type. For example, if the fract type has 15 fractional bits, then after the declaration

  fract a = 0.5;

the value of bitsr(a) is 0.5 * 2^15 = 0x4000.

These can first be implemented as __builtin_* functions in clang then llvm-libc can provide the wrappers for each of the builtin functions.

llvmbot commented 1 month ago

@llvm/issue-subscribers-libc

Author: None (PiJoules)

Some fixed point functions from ISO 18037 are implemented in llvm-libc, but not all of them are implemented. The various bits`fx` functions should also be added. Copying from the extension: ``` 4.1.7.4 The bitwise fixed-point to integer conversion functions The bitwise fixed-point to integer conversion functions bitsfx, where fx stands for one of hr, r, lr, hk, k, lk, uhr, ur, ulr, uhk, uk or ulk, take one fixed-point type argument (corresponding to fx); the type of the function is an implementation-defined integer type int_fx_t (for signed fixed-point types) or uint_fx_t (for unsigned fixed-point types), defined in the <stdfix.h> headerfile, that is large enough to hold all the bits in the fixed-point type. The bitwise fixed-point to integer conversion functions return an integer value equal to the fixedpoint value of the argument multiplied by 2^F, where F is the number of fractional bits in the fixed point type. The result type is an integer type big enough to hold all valid result values for the given fixed-point argument type. For example, if the fract type has 15 fractional bits, then after the declaration fract a = 0.5; the value of bitsr(a) is 0.5 * 2^15 = 0x4000. ``` These can first be implemented as `__builtin_*` functions in clang then llvm-libc can provide the wrappers for each of the builtin functions.
llvmbot commented 1 month ago

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor has already been assigned to this issue. If you believe that no one is actually working on it despite an assignment, ping the person. After one week without a response, the assignee may be changed.
  2. In the comments of this issue, request for it to be assigned to you, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot commented 1 month ago

@llvm/issue-subscribers-good-first-issue

Author: None (PiJoules)

Some fixed point functions from ISO 18037 are implemented in llvm-libc, but not all of them are implemented. The various bits`fx` functions should also be added. Copying from the extension: ``` 4.1.7.4 The bitwise fixed-point to integer conversion functions The bitwise fixed-point to integer conversion functions bitsfx, where fx stands for one of hr, r, lr, hk, k, lk, uhr, ur, ulr, uhk, uk or ulk, take one fixed-point type argument (corresponding to fx); the type of the function is an implementation-defined integer type int_fx_t (for signed fixed-point types) or uint_fx_t (for unsigned fixed-point types), defined in the <stdfix.h> headerfile, that is large enough to hold all the bits in the fixed-point type. The bitwise fixed-point to integer conversion functions return an integer value equal to the fixedpoint value of the argument multiplied by 2^F, where F is the number of fractional bits in the fixed point type. The result type is an integer type big enough to hold all valid result values for the given fixed-point argument type. For example, if the fract type has 15 fractional bits, then after the declaration fract a = 0.5; the value of bitsr(a) is 0.5 * 2^15 = 0x4000. ``` These can first be implemented as `__builtin_*` functions in clang then llvm-libc can provide the wrappers for each of the builtin functions.
braw-lee commented 1 month ago

I can try this, assign me pls

braw-lee commented 1 month ago

also, are there any previously merged PR's that I can refer on how to implement libc functions? @PiJoules

lntue commented 4 weeks ago

also, are there any previously merged PR's that I can refer on how to implement libc functions? @PiJoules

Maybe something like https://github.com/llvm/llvm-project/pull/84391 (modulo the math part) to add new functions and tests to the stdfix.h header.

PiJoules commented 3 weeks ago

Initially I was thinking this could be a __builtin_ function that would be exposed through llvm-libc but I think from https://github.com/llvm/llvm-project/issues/113357#issuecomment-2443183341, it would also work to just have this function do a cpp::bit_cast and avoid having to do clang-side changes.

Thanks for taking this on also!

PiJoules commented 1 week ago

@braw-lee if you're still working on this, you might be able to leverage some of the stuff added in https://github.com/llvm/llvm-project/pull/114912

braw-lee commented 1 week ago

oh thanks, that will be helpful