Skip to content
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

Implement ISO/IEC JTC1 SC22 WG14 N1169 fixed-point types and arithmetic #43241

Open
bevin-hansson opened this issue Nov 4, 2019 · 1 comment
Labels
bugzilla Issues migrated from bugzilla clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@bevin-hansson
Copy link
Contributor

Bugzilla Link 43896
Version trunk
OS All
CC @bevin-hansson,@bjope,@PiJoules,@zygoloid

Extended Description

This is a ticket to track the remaining work required for supporting the ISO/IEC JTC1 SC22 WG14 N1169 fixed-point types and arithmetic.

Although this ticket is filed under Clang Frontend, the remaining work also includes patches to LLVM and other parts of Clang.

The currently estimated work that remains is:

LLVM:

  • Intrinsic for fixed-point [saturating] division
  • Possibly intrinsics for floating point/fixed-point conversion
  • Possibly intrinsic for saturating left shift

Clang:

  • Precision/limits macros (stdfix.h)
  • Constant evaluation
    ** Subtraction
    ** Multiplication
    ** Division
    ** Shift
    ** Floating point conversion
  • Compound assignment
  • Codegen
    ** Multiplication
    ** Division
    ** Shift
    ** Unary operations
    ** Floating point conversion
@bjope
Copy link
Collaborator

bjope commented Mar 16, 2021

Looking at the earlier "remaining work" list several things should have been implemented by now.

I sent out a status report to cfe-dev/llvm-dev on March 15, 2021:

Status regarding support for fixed point numbers (and Embedded-C) in Clang/LLVM

During the years there have been a number of questions on cfe-dev and llvm-dev
about support for fixed point arithmetic in LLVM.

In 2018 Leonard Chan wrote a proposal for adding fixed point number support in
clang [1], accompanied by discussions about how to support the types in LLVM
[2]. Since then a number of patches has been submitted and nowadays
Clang/LLVM has lots of support for fixed point arithmetic, based on the
Embedded-C draft [3].

A major part of chapter 4 in [3] should have been implemented by now.
Here is a list of features implemented:

  • Fixed point types can be enabled using the -ffixed-point clang option.

  • Variables can be declared using the _Fract and _Accum type specifiers (see
    for example clang/test/Frontend/fixed_point_declarations.c for examples).

  • Saturated fixed point types are available by using the _Sat specifier
    for the _Fract and _Accum types.

  • Support for conversion between signed/unsigned fixed-point types and
    integral or floating point types.

  • Support for literals, using the following fixed-point suffixes:
    hr: short _Fract
    uhr: unsigned short _Fract
    r: _Fract
    ur: unsigned _Fract
    lr: long _Fract
    ulr: unsigned long _Fract
    hk: short _Accum
    uhk: unsigned short _Accum
    k: _Accum
    uk: unsigned _Accum
    lk: long _Accum
    ulk: unsigned long _Accum

  • Support for operations involving fixed-point types:
    Unary (++, --, +, -, !)
    Binary (+, -, *, /, <<, >>)
    Comparison (<, <=, >=, >, ==, !=)
    Assignment / Compound Assignment (=, +=, -=, *=, /=, <<=, >>=)

Future improvements:

  • Improve user documentation (the comment that _Fract types aren't supported
    has been removed from the UsersManual, but we haven't really added anything
    describing that we now support fixed point types).

  • Support for format specifiers (r/R/k/K) for fixed-point arguments in I/O
    functions.

  • Support for pragmas such as FX_FRACT_OVERFLOW and FX_ACCUM_OVERFLOW.
    (This might be a difference compared to what they have implemented in
    gcc, see [4].)

  • The Embedded-C draft [3] mentions a stdfix.h library.
    (This has not been implemented. Also lacking in gcc according to [4].)

  • Implement sanitizer checks for fixed point shifts. The LLVM IR for shift has
    UB semantics when shift count is too large. So either we need to clamp shift
    count when lowering shift operators (if there are special rules for
    embedded-c), or if it is UB on c level it would be nice if the sanitizer
    catch such faults. There are some TODO:s left in the code (see for example
    ScalarExprEmitter::EmitShl) indicating that the fixed point lowering
    currently bypass the code for sanitizing the shift count.

  • Add some C++ compatibility. One thing that limits the fixed point support
    to C is lack of name mangling rules for fixed point types (at least according
    to the Itanium C++ ABI). One solution is to create a wrapper class, overriding
    various operators. Another idea is to add name mangling (there is a proposal
    for the Itanium C++ ABI here [5]).

References:

[1] - https://lists.llvm.org/pipermail/cfe-dev/2018-April/057756.html
[2] - https://lists.llvm.org/pipermail/llvm-dev/2018-August/125433.html
[3] - http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf
[4] - https://gcc.gnu.org/wiki/FixedPointArithmetic
[5] - itanium-cxx-abi/cxx-abi#56

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

2 participants