Skip to content

[libc][stdfix] Generate stdfix.h header with fixed point precision macros according to ISO/IEC TR 18037:2008 standard, and add fixed point type support detection. #81255

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 8 commits into from
Feb 13, 2024
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: 9 additions & 3 deletions libc/cmake/modules/CheckCompilerFeatures.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# Compiler features definition and flags
# ------------------------------------------------------------------------------

# Initialize ALL_COMPILER_FEATURES as empty list.
set(ALL_COMPILER_FEATURES "float128")
set(ALL_COMPILER_FEATURES "float128" "fixed_point")

# Making sure ALL_COMPILER_FEATURES is sorted.
list(SORT ALL_COMPILER_FEATURES)
Expand Down Expand Up @@ -42,16 +41,23 @@ set(AVAILABLE_COMPILER_FEATURES "")
# Try compile a C file to check if flag is supported.
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
foreach(feature IN LISTS ALL_COMPILER_FEATURES)
set(compile_options ${LIBC_COMPILE_OPTIONS_NATIVE})
if(${feature} STREQUAL "fixed_point")
list(APPEND compile_options "-ffixed-point")
endif()

try_compile(
has_feature
${CMAKE_CURRENT_BINARY_DIR}/compiler_features
SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/compiler_features/check_${feature}.cpp
COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${LIBC_COMPILE_OPTIONS_NATIVE}
COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${compile_options}
)
if(has_feature)
list(APPEND AVAILABLE_COMPILER_FEATURES ${feature})
if(${feature} STREQUAL "float128")
set(LIBC_COMPILER_HAS_FLOAT128 TRUE)
elseif(${feature} STREQUAL "fixed_point")
set(LIBC_COMPILER_HAS_FIXED_POINT TRUE)
endif()
endif()
endforeach()
Expand Down
5 changes: 5 additions & 0 deletions libc/cmake/modules/compiler_features/check_fixed_point.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "include/llvm-libc-macross/stdfix_macros.h"

#ifndef LIBC_COMPILER_HAS_FIXED_POINT
#error unsupported
#endif
1 change: 1 addition & 0 deletions libc/config/linux/api.td
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ include "spec/linux.td"
include "spec/gnu_ext.td"
include "spec/bsd_ext.td"
include "spec/llvm_libc_ext.td"
include "spec/stdc_ext.td"

def AssertMacro : MacroDef<"assert"> {
let Defn = [{
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.spawn
libc.include.setjmp
libc.include.stdbit
libc.include.stdfix
libc.include.stdio
libc.include.stdlib
libc.include.string
Expand Down
6 changes: 6 additions & 0 deletions libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Math Functions
:hidden:

log.rst
stdfix.rst


.. contents:: Table of Contents
Expand Down Expand Up @@ -607,6 +608,11 @@ Algorithms + Implementation Details

* :doc:`log`

Fixed-point Arithmetics
=======================

* :doc:`stdfix`

References
==========

Expand Down
136 changes: 136 additions & 0 deletions libc/docs/math/stdfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
================
StdFix Functions
================

.. include:: ../check.rst

Standards
---------

- stdfix.h is specified in the `ISO/IEC TR 18037:2008 <https://www.iso.org/standard/51126.html>`_,
C extensions to support embedded processors .

- Its `specifications <https://standards.iso.org/ittf/PubliclyAvailableStandards/c051126_ISO_IEC_TR_18037_2008.zip>`_.

---------------
Source location
---------------

- The main source for fixed-point functions is located at:
``libc/src/stdfix`` with subdirectories for internal implementations.

---------------------
Implementation Status
---------------------

Requirements
============

- In order to build LLVM libc to support fixed-point arithmetics, we need the
compiler to support the basic fixed-point types `_Fract` and `_Accum` in
C++.

- For the users to be able to use the generated headers, their compiler needs
to support `_Fract` and `_Accum` types in C or C++.

- This compiler support is checked at the beginning of
`libc/include/llvm-libc-macros/stdfix-macros.h <https://github.com/llvm/llvm-project/tree/main/libc/include/llvm-libc-macros/stdfix-macros.h>`_.



Predefined Macros
=================

- We use the macro `LIBC_COMPILER_HAS_FIXED_POINT` to specify whether the
compiler support the fixed-point types.

- Other predefined precision macros specified in section 7.18a.3 are defined
in `libc/include/llvm-libc-macros/stdfix-macros.h <https://github.com/llvm/llvm-project/tree/main/libc/include/llvm-libc-macros/stdfix-macros.h>`_
using the default configuration of `typical desktop processor` in section
A.3.


Fixed-point Arithmetics
=======================

+---------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+
| Function Name | _Fract (r) | _Accum (k) |
| +------------------------------+----------------------------+------------------------------+------------------------------+----------------------------+------------------------------+
| | short (hr) | _ (r) | long (lr) | short (hk) | _ (k) | long (lk) |
| +----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| | unsigned (uhr) | signed (hr) | unsigned (ur) | signed (r) | unsigned (ulr) | signed (lr) | unsigned (uhk) | signed (hk) | unsigned (uk) | signed (k) | unsigned (ulk) | signed (lk) |
+===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
| abs | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| bits\* | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| \*bits | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| countls | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| divi | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| idivi | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| muli | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| rdivi | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| round | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| sqrt | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+

================== =========
Type Generic Macro Available
================== =========
absfx
countlsfx
roundfx
================== =========


Higher math functions
=====================

+---------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+
| Function Name | _Fract (r) | _Accum (k) |
| +------------------------------+----------------------------+------------------------------+------------------------------+----------------------------+------------------------------+
| | short (hr) | _ (r) | long (lr) | short (hk) | _ (k) | long (lk) |
| +----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| | unsigned (uhr) | signed (hr) | unsigned (ur) | signed (r) | unsigned (ulr) | signed (lr) | unsigned (uhk) | signed (hk) | unsigned (uk) | signed (k) | unsigned (ulk) | signed (lk) |
+===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
| cos | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| exp | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| log | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| sin | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| tan | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+


Conversion Functions
====================

+---------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+
| Function Name | _Fract (r) | _Accum (k) |
| +------------------------------+----------------------------+------------------------------+------------------------------+----------------------------+------------------------------+
| | short (hr) | _ (r) | long (lr) | short (hk) | _ (k) | long (lk) |
| +----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| | unsigned (uhr) | signed (hr) | unsigned (ur) | signed (r) | unsigned (ulr) | signed (lr) | unsigned (uhk) | signed (hk) | unsigned (uk) | signed (k) | unsigned (ulk) | signed (lk) |
+===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
| fprintf | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| fscanf | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
| strtofx | | | | | | | | | | | | |
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+


Warnings
========

This is currently a work-in-progress, its headers, macros, and ABI are still unstable, and might be modified.
8 changes: 8 additions & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ add_gen_header(
.llvm-libc-types.float128
)

add_gen_header(
stdfix
DEF_FILE stdfix.h.def
GEN_HDR stdfix.h
DEPENDS
.llvm-libc-macros.stdfix_macros
)

# TODO: This should be conditional on POSIX networking being included.
file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/arpa)

Expand Down
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,9 @@ add_macro_header(
HDR
inttypes-macros.h
)

add_macro_header(
stdfix_macros
HDR
stdfix-macros.h
)
Loading