Skip to content

Commit 84277fe

Browse files
authored
[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)
Fixed point extension standard: https://standards.iso.org/ittf/PubliclyAvailableStandards/c051126_ISO_IEC_TR_18037_2008.zip
1 parent fd3a0c1 commit 84277fe

File tree

11 files changed

+539
-3
lines changed

11 files changed

+539
-3
lines changed

libc/cmake/modules/CheckCompilerFeatures.cmake

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
# Compiler features definition and flags
33
# ------------------------------------------------------------------------------
44

5-
# Initialize ALL_COMPILER_FEATURES as empty list.
6-
set(ALL_COMPILER_FEATURES "float128")
5+
set(ALL_COMPILER_FEATURES "float128" "fixed_point")
76

87
# Making sure ALL_COMPILER_FEATURES is sorted.
98
list(SORT ALL_COMPILER_FEATURES)
@@ -42,16 +41,23 @@ set(AVAILABLE_COMPILER_FEATURES "")
4241
# Try compile a C file to check if flag is supported.
4342
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
4443
foreach(feature IN LISTS ALL_COMPILER_FEATURES)
44+
set(compile_options ${LIBC_COMPILE_OPTIONS_NATIVE})
45+
if(${feature} STREQUAL "fixed_point")
46+
list(APPEND compile_options "-ffixed-point")
47+
endif()
48+
4549
try_compile(
4650
has_feature
4751
${CMAKE_CURRENT_BINARY_DIR}/compiler_features
4852
SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/compiler_features/check_${feature}.cpp
49-
COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${LIBC_COMPILE_OPTIONS_NATIVE}
53+
COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${compile_options}
5054
)
5155
if(has_feature)
5256
list(APPEND AVAILABLE_COMPILER_FEATURES ${feature})
5357
if(${feature} STREQUAL "float128")
5458
set(LIBC_COMPILER_HAS_FLOAT128 TRUE)
59+
elseif(${feature} STREQUAL "fixed_point")
60+
set(LIBC_COMPILER_HAS_FIXED_POINT TRUE)
5561
endif()
5662
endif()
5763
endforeach()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "include/llvm-libc-macross/stdfix_macros.h"
2+
3+
#ifndef LIBC_COMPILER_HAS_FIXED_POINT
4+
#error unsupported
5+
#endif

libc/config/linux/api.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ include "spec/linux.td"
66
include "spec/gnu_ext.td"
77
include "spec/bsd_ext.td"
88
include "spec/llvm_libc_ext.td"
9+
include "spec/stdc_ext.td"
910

1011
def AssertMacro : MacroDef<"assert"> {
1112
let Defn = [{

libc/config/linux/x86_64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(TARGET_PUBLIC_HEADERS
1616
libc.include.spawn
1717
libc.include.setjmp
1818
libc.include.stdbit
19+
libc.include.stdfix
1920
libc.include.stdio
2021
libc.include.stdlib
2122
libc.include.string

libc/docs/math/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Math Functions
1616
:hidden:
1717

1818
log.rst
19+
stdfix.rst
1920

2021

2122
.. contents:: Table of Contents
@@ -611,6 +612,11 @@ Algorithms + Implementation Details
611612

612613
* :doc:`log`
613614

615+
Fixed-point Arithmetics
616+
=======================
617+
618+
* :doc:`stdfix`
619+
614620
References
615621
==========
616622

libc/docs/math/stdfix.rst

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
================
2+
StdFix Functions
3+
================
4+
5+
.. include:: ../check.rst
6+
7+
Standards
8+
---------
9+
10+
- stdfix.h is specified in the `ISO/IEC TR 18037:2008 <https://www.iso.org/standard/51126.html>`_,
11+
C extensions to support embedded processors .
12+
13+
- Its `specifications <https://standards.iso.org/ittf/PubliclyAvailableStandards/c051126_ISO_IEC_TR_18037_2008.zip>`_.
14+
15+
---------------
16+
Source location
17+
---------------
18+
19+
- The main source for fixed-point functions is located at:
20+
``libc/src/stdfix`` with subdirectories for internal implementations.
21+
22+
---------------------
23+
Implementation Status
24+
---------------------
25+
26+
Requirements
27+
============
28+
29+
- In order to build LLVM libc to support fixed-point arithmetics, we need the
30+
compiler to support the basic fixed-point types `_Fract` and `_Accum` in
31+
C++.
32+
33+
- For the users to be able to use the generated headers, their compiler needs
34+
to support `_Fract` and `_Accum` types in C or C++.
35+
36+
- This compiler support is checked at the beginning of
37+
`libc/include/llvm-libc-macros/stdfix-macros.h <https://github.com/llvm/llvm-project/tree/main/libc/include/llvm-libc-macros/stdfix-macros.h>`_.
38+
39+
40+
41+
Predefined Macros
42+
=================
43+
44+
- We use the macro `LIBC_COMPILER_HAS_FIXED_POINT` to specify whether the
45+
compiler support the fixed-point types.
46+
47+
- Other predefined precision macros specified in section 7.18a.3 are defined
48+
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>`_
49+
using the default configuration of `typical desktop processor` in section
50+
A.3.
51+
52+
53+
Fixed-point Arithmetics
54+
=======================
55+
56+
+---------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+
57+
| Function Name | _Fract (r) | _Accum (k) |
58+
| +------------------------------+----------------------------+------------------------------+------------------------------+----------------------------+------------------------------+
59+
| | short (hr) | _ (r) | long (lr) | short (hk) | _ (k) | long (lk) |
60+
| +----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
61+
| | unsigned (uhr) | signed (hr) | unsigned (ur) | signed (r) | unsigned (ulr) | signed (lr) | unsigned (uhk) | signed (hk) | unsigned (uk) | signed (k) | unsigned (ulk) | signed (lk) |
62+
+===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
63+
| abs | | | | | | | | | | | | |
64+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
65+
| bits\* | | | | | | | | | | | | |
66+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
67+
| \*bits | | | | | | | | | | | | |
68+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
69+
| countls | | | | | | | | | | | | |
70+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
71+
| divi | | | | | | | | | | | | |
72+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
73+
| idivi | | | | | | | | | | | | |
74+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
75+
| muli | | | | | | | | | | | | |
76+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
77+
| rdivi | | | | | | | | | | | | |
78+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
79+
| round | | | | | | | | | | | | |
80+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
81+
| sqrt | | | | | | | | | | | | |
82+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
83+
84+
================== =========
85+
Type Generic Macro Available
86+
================== =========
87+
absfx
88+
countlsfx
89+
roundfx
90+
================== =========
91+
92+
93+
Higher math functions
94+
=====================
95+
96+
+---------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+
97+
| Function Name | _Fract (r) | _Accum (k) |
98+
| +------------------------------+----------------------------+------------------------------+------------------------------+----------------------------+------------------------------+
99+
| | short (hr) | _ (r) | long (lr) | short (hk) | _ (k) | long (lk) |
100+
| +----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
101+
| | unsigned (uhr) | signed (hr) | unsigned (ur) | signed (r) | unsigned (ulr) | signed (lr) | unsigned (uhk) | signed (hk) | unsigned (uk) | signed (k) | unsigned (ulk) | signed (lk) |
102+
+===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
103+
| cos | | | | | | | | | | | | |
104+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
105+
| exp | | | | | | | | | | | | |
106+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
107+
| log | | | | | | | | | | | | |
108+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
109+
| sin | | | | | | | | | | | | |
110+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
111+
| tan | | | | | | | | | | | | |
112+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
113+
114+
115+
Conversion Functions
116+
====================
117+
118+
+---------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------+
119+
| Function Name | _Fract (r) | _Accum (k) |
120+
| +------------------------------+----------------------------+------------------------------+------------------------------+----------------------------+------------------------------+
121+
| | short (hr) | _ (r) | long (lr) | short (hk) | _ (k) | long (lk) |
122+
| +----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
123+
| | unsigned (uhr) | signed (hr) | unsigned (ur) | signed (r) | unsigned (ulr) | signed (lr) | unsigned (uhk) | signed (hk) | unsigned (uk) | signed (k) | unsigned (ulk) | signed (lk) |
124+
+===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
125+
| fprintf | | | | | | | | | | | | |
126+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
127+
| fscanf | | | | | | | | | | | | |
128+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
129+
| strtofx | | | | | | | | | | | | |
130+
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
131+
132+
133+
Warnings
134+
========
135+
136+
This is currently a work-in-progress, its headers, macros, and ABI are still unstable, and might be modified.

libc/include/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ add_gen_header(
104104
.llvm-libc-types.float128
105105
)
106106

107+
add_gen_header(
108+
stdfix
109+
DEF_FILE stdfix.h.def
110+
GEN_HDR stdfix.h
111+
DEPENDS
112+
.llvm-libc-macros.stdfix_macros
113+
)
114+
107115
# TODO: This should be conditional on POSIX networking being included.
108116
file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/arpa)
109117

libc/include/llvm-libc-macros/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,9 @@ add_macro_header(
227227
HDR
228228
inttypes-macros.h
229229
)
230+
231+
add_macro_header(
232+
stdfix_macros
233+
HDR
234+
stdfix-macros.h
235+
)

0 commit comments

Comments
 (0)