[cfe-dev] Status regarding support for fixed point numbers (and Embedded-C) in Clang/LLVM
Björn Pettersson A via cfe-dev
cfe-dev at lists.llvm.org
Mon Mar 15 05:06:33 PDT 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 , accompanied by discussions about how to support the types in LLVM
. 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 .
A major part of chapter 4 in  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
ur: unsigned _Fract
lr: long _Fract
ulr: unsigned long _Fract
hk: short _Accum
uhk: unsigned short _Accum
uk: unsigned _Accum
lk: long _Accum
ulk: unsigned long _Accum
- Support for operations involving fixed-point types:
Unary (++, --, +, -, !)
Binary (+, -, *, /, <<, >>)
Comparison (<, <=, >=, >, ==, !=)
Assignment / Compound Assignment (=, +=, -=, *=, /=, <<=, >>=)
- 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
- 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 .)
- The Embedded-C draft  mentions a stdfix.h library.
(This has not been implemented. Also lacking in gcc according to .)
- 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 ).
 - https://lists.llvm.org/pipermail/cfe-dev/2018-April/057756.html
 - https://lists.llvm.org/pipermail/llvm-dev/2018-August/125433.html
 - http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf
 - https://gcc.gnu.org/wiki/FixedPointArithmetic
 - https://github.com/itanium-cxx-abi/cxx-abi/issues/56
More information about the cfe-dev