Skip to content

Commit a676ecd

Browse files
authored
[libc][math] Add POSIX math constants to math.h header. (#149150)
https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/math.h.html
1 parent ac7ceb3 commit a676ecd

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

libc/include/llvm-libc-macros/math-macros.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,105 @@
5050
#define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
5151
#endif
5252

53+
// POSIX math constants
54+
// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/math.h.html
55+
#define M_E (__extension__ 0x1.5bf0a8b145769p1)
56+
#define M_EGAMMA (__extension__ 0x1.2788cfc6fb619p-1)
57+
#define M_LOG2E (__extension__ 0x1.71547652b82fep0)
58+
#define M_LOG10E (__extension__ 0x1.bcb7b1526e50ep-2)
59+
#define M_LN2 (__extension__ 0x1.62e42fefa39efp-1)
60+
#define M_LN10 (__extension__ 0x1.26bb1bbb55516p1)
61+
#define M_PHI (__extension__ 0x1.9e3779b97f4a8p0)
62+
#define M_PI (__extension__ 0x1.921fb54442d18p1)
63+
#define M_PI_2 (__extension__ 0x1.921fb54442d18p0)
64+
#define M_PI_4 (__extension__ 0x1.921fb54442d18p-1)
65+
#define M_1_PI (__extension__ 0x1.45f306dc9c883p-2)
66+
#define M_1_SQRTPI (__extension__ 0x1.20dd750429b6dp-1)
67+
#define M_2_PI (__extension__ 0x1.45f306dc9c883p-1)
68+
#define M_2_SQRTPI (__extension__ 0x1.20dd750429b6dp0)
69+
#define M_SQRT2 (__extension__ 0x1.6a09e667f3bcdp0)
70+
#define M_SQRT3 (__extension__ 0x1.bb67ae8584caap0)
71+
#define M_SQRT1_2 (__extension__ 0x1.6a09e667f3bcdp-1)
72+
#define M_SQRT1_3 (__extension__ 0x1.279a74590331cp-1)
73+
74+
#define M_Ef (__extension__ 0x1.5bf0a8p1f)
75+
#define M_EGAMMAf (__extension__ 0x1.2788dp-1f)
76+
#define M_LOG2Ef (__extension__ 0x1.715476p0f)
77+
#define M_LOG10Ef (__extension__ 0x1.bcb7b2p-2f)
78+
#define M_LN2f (__extension__ 0x1.62e43p-1f)
79+
#define M_LN10f (__extension__ 0x1.26bb1cp1f)
80+
#define M_PHIf (__extension__ 0x1.9e377ap0f)
81+
#define M_PIf (__extension__ 0x1.921fb6p1f)
82+
#define M_PI_2f (__extension__ 0x1.921fb6p0f)
83+
#define M_PI_4f (__extension__ 0x1.921fb6p-1f)
84+
#define M_1_PIf (__extension__ 0x1.45f306p-2f)
85+
#define M_1_SQRTPIf (__extension__ 0x1.20dd76p-1f)
86+
#define M_2_PIf (__extension__ 0x1.45f306p-1f)
87+
#define M_2_SQRTPIf (__extension__ 0x1.20dd76p0f)
88+
#define M_SQRT2f (__extension__ 0x1.6a09e6p0f)
89+
#define M_SQRT3f (__extension__ 0x1.bb67aep0f)
90+
#define M_SQRT1_2f (__extension__ 0x1.6a09e6p-1f)
91+
#define M_SQRT1_3f (__extension__ 0x1.279a74p-1f)
92+
93+
#define M_El (__extension__ 0x1.5bf0a8b1457695355fb8ac404e7ap1L)
94+
#define M_EGAMMAl (__extension__ 0x1.2788cfc6fb618f49a37c7f0202a6p-1L)
95+
#define M_LOG2El (__extension__ 0x1.71547652b82fe1777d0ffda0d23ap0L)
96+
#define M_LOG10El (__extension__ 0x1.bcb7b1526e50e32a6ab7555f5a68p-2L)
97+
#define M_LN2l (__extension__ 0x1.62e42fefa39ef35793c7673007e6p-1L)
98+
#define M_LN10l (__extension__ 0x1.26bb1bbb5551582dd4adac5705a6p1L)
99+
#define M_PHIl (__extension__ 0x1.9e3779b97f4a7c15f39cc0605ceep0L)
100+
#define M_PIl (__extension__ 0x1.921fb54442d18469898cc51701b8p1L)
101+
#define M_PI_2l (__extension__ 0x1.921fb54442d18469898cc51701b8p0L)
102+
#define M_PI_4l (__extension__ 0x1.921fb54442d18469898cc51701b8p-1L)
103+
#define M_1_PIl (__extension__ 0x1.45f306dc9c882a53f84eafa3ea6ap-2L)
104+
#define M_1_SQRTPIl (__extension__ 0x1.20dd750429b6d11ae3a914fed7fep-1L)
105+
#define M_2_PIl (__extension__ 0x1.45f306dc9c882a53f84eafa3ea6ap-1L)
106+
#define M_2_SQRTPIl (__extension__ 0x1.20dd750429b6d11ae3a914fed7fep0L)
107+
#define M_SQRT2l (__extension__ 0x1.6a09e667f3bcc908b2fb1366ea95p0L)
108+
#define M_SQRT3l (__extension__ 0x1.bb67ae8584caa73b25742d7078b8p0L)
109+
#define M_SQRT1_2l (__extension__ 0x1.6a09e667f3bcc908b2fb1366ea95p-1L)
110+
#define M_SQRT1_3l (__extension__ 0x1.279a74590331c4d218f81e4afb25p-1L)
111+
112+
#ifdef __FLT16_MANT_DIG__
113+
#define M_Ef16 (__extension__ 0x1.5cp1f16)
114+
#define M_EGAMMAf16 (__extension__ 0x1.278p-1f16)
115+
#define M_LOG2Ef16 (__extension__ 0x1.714f16)
116+
#define M_LOG10Ef16 (__extension__ 0x1.bccp-2f16)
117+
#define M_LN2f16 (__extension__ 0x1.63p-1f16)
118+
#define M_LN10f16 (__extension__ 0x1.26cp1f16)
119+
#define M_PHIf16 (__extension__ 0x1.9e4p0f16)
120+
#define M_PIf16 (__extension__ 0x1.92p1f16)
121+
#define M_PI_2f16 (__extension__ 0x1.92p0f16)
122+
#define M_PI_4f16 (__extension__ 0x1.92p-1f16)
123+
#define M_1_PIf16 (__extension__ 0x1.46p-2f16)
124+
#define M_1_SQRTPIf16 (__extension__ 0x1.20cp-1f16)
125+
#define M_2_PIf16 (__extension__ 0x1.46p-1f16)
126+
#define M_2_SQRTPIf16 (__extension__ 0x1.20cp0f16)
127+
#define M_SQRT2f16 (__extension__ 0x1.6ap0f16)
128+
#define M_SQRT3f16 (__extension__ 0x1.bb8p0f16)
129+
#define M_SQRT1_2f16 (__extension__ 0x1.6ap-1f16)
130+
#define M_SQRT1_3f16 (__extension__ 0x1.278p-1f16)
131+
#endif // __FLT16_MANT_DIG__
132+
133+
#ifdef __SIZEOF_FLOAT128__
134+
#define M_Ef128 (__extension__ 0x1.5bf0a8b1457695355fb8ac404e7ap1q)
135+
#define M_EGAMMAf128 (__extension__ 0x1.2788cfc6fb618f49a37c7f0202a6p-1q)
136+
#define M_LOG2Ef128 (__extension__ 0x1.71547652b82fe1777d0ffda0d23ap0q)
137+
#define M_LOG10Ef128 (__extension__ 0x1.bcb7b1526e50e32a6ab7555f5a68p-2q)
138+
#define M_LN2f128 (__extension__ 0x1.62e42fefa39ef35793c7673007e6p-1q)
139+
#define M_LN10f128 (__extension__ 0x1.26bb1bbb5551582dd4adac5705a6p1q)
140+
#define M_PHIf128 (__extension__ 0x1.9e3779b97f4a7c15f39cc0605ceep0q)
141+
#define M_PIf128 (__extension__ 0x1.921fb54442d18469898cc51701b8p1q)
142+
#define M_PI_2f128 (__extension__ 0x1.921fb54442d18469898cc51701b8p0q)
143+
#define M_PI_4f128 (__extension__ 0x1.921fb54442d18469898cc51701b8p-1q)
144+
#define M_1_PIf128 (__extension__ 0x1.45f306dc9c882a53f84eafa3ea6ap-2q)
145+
#define M_1_SQRTPIf128 (__extension__ 0x1.20dd750429b6d11ae3a914fed7fep-1q)
146+
#define M_2_PIf128 (__extension__ 0x1.45f306dc9c882a53f84eafa3ea6ap-1q)
147+
#define M_2_SQRTPIf128 (__extension__ 0x1.20dd750429b6d11ae3a914fed7fep0q)
148+
#define M_SQRT2f128 (__extension__ 0x1.6a09e667f3bcc908b2fb1366ea95p0q)
149+
#define M_SQRT3f128 (__extension__ 0x1.bb67ae8584caa73b25742d7078b8p0q)
150+
#define M_SQRT1_2f128 (__extension__ 0x1.6a09e667f3bcc908b2fb1366ea95p-1q)
151+
#define M_SQRT1_3f128 (__extension__ 0x1.279a74590331c4d218f81e4afb25p-1q)
152+
#endif // __SIZEOF_FLOAT128__
153+
53154
#endif // LLVM_LIBC_MACROS_MATH_MACROS_H

libc/test/include/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,21 @@ add_libc_test(
484484
libc.include.llvm-libc-macros.math_function_macros
485485
)
486486

487+
add_libc_test(
488+
math_constants_c_test
489+
C_TEST
490+
UNIT_TEST_ONLY
491+
SUITE
492+
libc_include_tests
493+
SRCS
494+
math_constants_test.c
495+
COMPILE_OPTIONS
496+
-Wall
497+
-Werror
498+
DEPENDS
499+
libc.include.llvm-libc-macros.math_macros
500+
)
501+
487502
# Test `#include <...>` of each header in each available language mode.
488503
# This is gated on -DLLVM_LIBC_BUILD_HEADER_TESTS=ON until all the bugs
489504
# in headers are fixed so the tests all compile.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Unittests for math constants --------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#include "include/llvm-libc-macros/math-macros.h"
9+
10+
#define IS_DOUBLE(X) _Generic((X), double: 1, default: 0)
11+
12+
#define IS_FLOAT(X) _Generic((X), float: 1, default: 0)
13+
14+
// check if macro is defined
15+
#ifndef M_PI
16+
#error "M_PI macro is not defined"
17+
#else
18+
int main(void) {
19+
_Static_assert(IS_DOUBLE(M_PI), "M_PI is not of double type.");
20+
_Static_assert(IS_FLOAT(M_PIf), "M_PIf is not of float type.");
21+
return 0;
22+
}
23+
#endif

0 commit comments

Comments
 (0)