Skip to content

Conversation

@lntue
Copy link
Contributor

@lntue lntue commented Jul 16, 2025

@llvmbot llvmbot added the libc label Jul 16, 2025
@lntue lntue requested review from jhuber6 and overmighty July 16, 2025 17:48
@llvmbot
Copy link
Member

llvmbot commented Jul 16, 2025

@llvm/pr-subscribers-libc

Author: None (lntue)

Changes

https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/math.h.html


Full diff: https://github.com/llvm/llvm-project/pull/149150.diff

1 Files Affected:

  • (modified) libc/include/llvm-libc-macros/math-macros.h (+21)
diff --git a/libc/include/llvm-libc-macros/math-macros.h b/libc/include/llvm-libc-macros/math-macros.h
index 2f05d7544666e..5195292c7db58 100644
--- a/libc/include/llvm-libc-macros/math-macros.h
+++ b/libc/include/llvm-libc-macros/math-macros.h
@@ -50,4 +50,25 @@
 #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
 #endif
 
+// POSIX math constants
+// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/math.h.html
+#define M_E (__extension__ 0x1.5bf0a8b145769p1)
+#define M_EGAMMA (__extension__ 0x1.2788cfc6fb619p-1)
+#define M_LOG2E (__extension__ 0x1.71547652b82fep0)
+#define M_LOG10E (__extension__ 0x1.bcb7b1526e50ep-2)
+#define M_LN2 (__extension__ 0x1.62e42fefa39efp-1)
+#define M_LN10 (__extension__ 0x1.26bb1bbb55516p1)
+#define M_PHI (__extension__ 0x1.9e3779b97f4a8p0)
+#define M_PI (__extension__ 0x1.921fb54442d18p1)
+#define M_PI_2 (__extension__ 0x1.921fb54442d18p0)
+#define M_PI_4 (__extension__ 0x1.921fb54442d18p-1)
+#define M_1_PI (__extension__ 0x1.45f306dc9c883p-2)
+#define M_1_SQRTPI (__extension__ 0x1.20dd750429b6dp-1)
+#define M_2_PI (__extension__ 0x1.45f306dc9c883p-1)
+#define M_2_SQRTPI (__extension__ 0x1.20dd750429b6dp0)
+#define M_SQRT2 (__extension__ 0x1.6a09e667f3bcdp0)
+#define M_SQRT3 (__extension__ 0x1.bb67ae8584caap0)
+#define M_SQRT1_2 (__extension__ 0x1.6a09e667f3bcdp-1)
+#define M_SQRT1_3 (__extension__ 0x1.279a74590331cp-1)
+
 #endif // LLVM_LIBC_MACROS_MATH_MACROS_H

@lntue lntue requested a review from frobtech July 16, 2025 17:49
@github-actions
Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

@frobtech
Copy link
Contributor

What about M_*l and M*_f versions? glibc also defines f32 and f16 suffixed versions when the types are available.

@lntue
Copy link
Contributor Author

lntue commented Jul 16, 2025

What about M_*l and M*_f versions? glibc also defines f32 and f16 suffixed versions when the types are available.

I've added f, l, f16, and f128 variants of the constants. And we haven't investigated the situation with _Float32 and _Float64 type support yet.

Copy link
Contributor

@frobtech frobtech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Eventually I want to get all the macros properly listed in math.yaml and for ones like these their values could easily be defined directly there. But we can do that as a later refactor.

It would be good to have at least nominal tests for these too. All the tests really need to do is verify the type of each expression, e.g.

static_cast(std::is_same_v<double, decltype(M_PI)>);

etc. I don't know if it's worthwhile doing value checks with e.g. comparing some trig function's value where the input constant is something trivial like 0,1,-1.

@lntue
Copy link
Contributor Author

lntue commented Jul 18, 2025

lgtm. Eventually I want to get all the macros properly listed in math.yaml and for ones like these their values could easily be defined directly there. But we can do that as a later refactor.

It would be good to have at least nominal tests for these too. All the tests really need to do is verify the type of each expression, e.g.

static_cast(std::is_same_v<double, decltype(M_PI)>);

etc. I don't know if it's worthwhile doing value checks with e.g. comparing some trig function's value where the input constant is something trivial like 0,1,-1.

I added a unit test for M_PI in the test/include folder.

@lntue lntue merged commit a676ecd into llvm:main Jul 18, 2025
18 of 19 checks passed
@lntue lntue deleted the mpi branch July 18, 2025 15:28
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 18, 2025

LLVM Buildbot has detected a new failure on builder premerge-monolithic-linux running on premerge-linux-1 while building libc at step 7 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/153/builds/38600

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-all) failure: test (failure)
...
PASS: lld :: COFF/associative-comdat-mingw.s (98607 of 101693)
PASS: lld :: COFF/arm64ec-patchable-thunks.test (98608 of 101693)
PASS: lld :: COFF/arm64ec-import.test (98609 of 101693)
PASS: lld :: COFF/associative-comdat-mingw-i386.s (98610 of 101693)
PASS: lit :: shtest-recursive-substitution.py (98611 of 101693)
PASS: UBSan-ThreadSanitizer-x86_64 :: TestCases/ImplicitConversion/signed-integer-truncation-ignorelist.c (98612 of 101693)
PASS: lld :: COFF/autoimport-list-ptrs.s (98613 of 101693)
PASS: lld :: COFF/arm64x-sameaddress.test (98614 of 101693)
PASS: lld :: COFF/associative-comdat.s (98615 of 101693)
TIMEOUT: MLIR :: Examples/standalone/test.toy (98616 of 101693)
******************** TEST 'MLIR :: Examples/standalone/test.toy' FAILED ********************
Exit Code: 1
Timeout: Reached timeout of 60 seconds

Command Output (stdout):
--
# RUN: at line 1
"/etc/cmake/bin/cmake" "/build/buildbot/premerge-monolithic-linux/llvm-project/mlir/examples/standalone" -G "Ninja"  -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_C_COMPILER=/usr/bin/clang  -DLLVM_ENABLE_LIBCXX=OFF -DMLIR_DIR=/build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir  -DLLVM_USE_LINKER=lld  -DPython3_EXECUTABLE="/usr/bin/python3.10"
# executed command: /etc/cmake/bin/cmake /build/buildbot/premerge-monolithic-linux/llvm-project/mlir/examples/standalone -G Ninja -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_C_COMPILER=/usr/bin/clang -DLLVM_ENABLE_LIBCXX=OFF -DMLIR_DIR=/build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir -DLLVM_USE_LINKER=lld -DPython3_EXECUTABLE=/usr/bin/python3.10
# .---command stdout------------
# | -- The CXX compiler identification is Clang 16.0.6
# | -- The C compiler identification is Clang 16.0.6
# | -- Detecting CXX compiler ABI info
# | -- Detecting CXX compiler ABI info - done
# | -- Check for working CXX compiler: /usr/bin/clang++ - skipped
# | -- Detecting CXX compile features
# | -- Detecting CXX compile features - done
# | -- Detecting C compiler ABI info
# | -- Detecting C compiler ABI info - done
# | -- Check for working C compiler: /usr/bin/clang - skipped
# | -- Detecting C compile features
# | -- Detecting C compile features - done
# | -- Looking for histedit.h
# | -- Looking for histedit.h - found
# | -- Found LibEdit: /usr/include (found version "2.11") 
# | -- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11") 
# | -- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.13") 
# | -- Using MLIRConfig.cmake in: /build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir
# | -- Using LLVMConfig.cmake in: /build/buildbot/premerge-monolithic-linux/build/lib/cmake/llvm
# | -- Linker detection: unknown
# | -- Performing Test LLVM_LIBSTDCXX_MIN
# | -- Performing Test LLVM_LIBSTDCXX_MIN - Success
# | -- Performing Test LLVM_LIBSTDCXX_SOFT_ERROR
# | -- Performing Test LLVM_LIBSTDCXX_SOFT_ERROR - Success
# | -- Performing Test CXX_SUPPORTS_CUSTOM_LINKER
# | -- Performing Test CXX_SUPPORTS_CUSTOM_LINKER - Success
# | -- Performing Test C_SUPPORTS_FPIC
# | -- Performing Test C_SUPPORTS_FPIC - Success
# | -- Performing Test CXX_SUPPORTS_FPIC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants