Skip to content

Commit a4d5f8b

Browse files
kazutakahiratagithub-actions[bot]
authored andcommitted
Automerge: [ADT, Support] Move llvm::numbers to STLForwardCompat.h (NFC) (#164053)
This patch moves llvm::numbers to STLForwardCompat.h for those constants defined in C++20 to facilitate the migration to C++20. "float" constants like ef and pif are not part of C++20, so they stay in MathExtras.h but are reimplemented in terms of e_v, pi_v, etc. sqrtpi and sqrtpif are not part of C++20, so they also stay in MathExtras.h, but they are redefined in terms of sqrtpi_v in the same style as other constants. I've verified the new C++17-style implementation to be bit-for-bit identical to the original definitions using static_assert: static_assert(e == llvm::numbers_new::e_v<double>); static_assert(ef == llvm::numbers_new::e_v<float>); before replacing the original definitions of e and ef in MathExtras.h, where llvm::numbers_new was a namesapce I used in STLForwardCompat.h for testing purposes.
2 parents 308735a + 4914926 commit a4d5f8b

File tree

2 files changed

+70
-31
lines changed

2 files changed

+70
-31
lines changed

llvm/include/llvm/ADT/STLForwardCompat.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,54 @@ namespace llvm {
2626
// Features from C++20
2727
//===----------------------------------------------------------------------===//
2828

29+
namespace numbers {
30+
// clang-format off
31+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
32+
inline constexpr T e_v = T(0x1.5bf0a8b145769P+1); // (2.7182818284590452354) https://oeis.org/A001113
33+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
34+
inline constexpr T egamma_v = T(0x1.2788cfc6fb619P-1); // (.57721566490153286061) https://oeis.org/A001620
35+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
36+
inline constexpr T ln2_v = T(0x1.62e42fefa39efP-1); // (.69314718055994530942) https://oeis.org/A002162
37+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
38+
inline constexpr T ln10_v = T(0x1.26bb1bbb55516P+1); // (2.3025850929940456840) https://oeis.org/A002392
39+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
40+
inline constexpr T log2e_v = T(0x1.71547652b82feP+0); // (1.4426950408889634074)
41+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
42+
inline constexpr T log10e_v = T(0x1.bcb7b1526e50eP-2); // (.43429448190325182765)
43+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
44+
inline constexpr T pi_v = T(0x1.921fb54442d18P+1); // (3.1415926535897932385) https://oeis.org/A000796
45+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
46+
inline constexpr T inv_pi_v = T(0x1.45f306dc9c883P-2); // (.31830988618379067154) https://oeis.org/A049541
47+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
48+
inline constexpr T inv_sqrtpi_v = T(0x1.20dd750429b6dP-1); // (.56418958354775628695) https://oeis.org/A087197
49+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
50+
inline constexpr T sqrt2_v = T(0x1.6a09e667f3bcdP+0); // (1.4142135623730950488) https://oeis.org/A00219
51+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
52+
inline constexpr T inv_sqrt2_v = T(0x1.6a09e667f3bcdP-1); // (.70710678118654752440)
53+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
54+
inline constexpr T sqrt3_v = T(0x1.bb67ae8584caaP+0); // (1.7320508075688772935) https://oeis.org/A002194
55+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
56+
inline constexpr T inv_sqrt3_v = T(0x1.279a74590331cP-1); // (.57735026918962576451)
57+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
58+
inline constexpr T phi_v = T(0x1.9e3779b97f4a8P+0); // (1.6180339887498948482) https://oeis.org/A001622
59+
60+
inline constexpr double e = e_v<double>;
61+
inline constexpr double egamma = egamma_v<double>;
62+
inline constexpr double ln2 = ln2_v<double>;
63+
inline constexpr double ln10 = ln10_v<double>;
64+
inline constexpr double log2e = log2e_v<double>;
65+
inline constexpr double log10e = log10e_v<double>;
66+
inline constexpr double pi = pi_v<double>;
67+
inline constexpr double inv_pi = inv_pi_v<double>;
68+
inline constexpr double inv_sqrtpi = inv_sqrtpi_v<double>;
69+
inline constexpr double sqrt2 = sqrt2_v<double>;
70+
inline constexpr double inv_sqrt2 = inv_sqrt2_v<double>;
71+
inline constexpr double sqrt3 = sqrt3_v<double>;
72+
inline constexpr double inv_sqrt3 = inv_sqrt3_v<double>;
73+
inline constexpr double phi = phi_v<double>;
74+
// clang-format on
75+
} // namespace numbers
76+
2977
template <typename T>
3078
struct remove_cvref // NOLINT(readability-identifier-naming)
3179
{

llvm/include/llvm/Support/MathExtras.h

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_SUPPORT_MATHEXTRAS_H
1414
#define LLVM_SUPPORT_MATHEXTRAS_H
1515

16+
#include "llvm/ADT/STLForwardCompat.h"
1617
#include "llvm/ADT/bit.h"
1718
#include "llvm/Support/Compiler.h"
1819
#include <cassert>
@@ -42,38 +43,28 @@ using common_sint =
4243

4344
/// Mathematical constants.
4445
namespace numbers {
45-
// TODO: Track C++20 std::numbers.
4646
// clang-format off
47-
constexpr double e = 0x1.5bf0a8b145769P+1, // (2.7182818284590452354) https://oeis.org/A001113
48-
egamma = 0x1.2788cfc6fb619P-1, // (.57721566490153286061) https://oeis.org/A001620
49-
ln2 = 0x1.62e42fefa39efP-1, // (.69314718055994530942) https://oeis.org/A002162
50-
ln10 = 0x1.26bb1bbb55516P+1, // (2.3025850929940456840) https://oeis.org/A002392
51-
log2e = 0x1.71547652b82feP+0, // (1.4426950408889634074)
52-
log10e = 0x1.bcb7b1526e50eP-2, // (.43429448190325182765)
53-
pi = 0x1.921fb54442d18P+1, // (3.1415926535897932385) https://oeis.org/A000796
54-
inv_pi = 0x1.45f306dc9c883P-2, // (.31830988618379067154) https://oeis.org/A049541
55-
sqrtpi = 0x1.c5bf891b4ef6bP+0, // (1.7724538509055160273) https://oeis.org/A002161
56-
inv_sqrtpi = 0x1.20dd750429b6dP-1, // (.56418958354775628695) https://oeis.org/A087197
57-
sqrt2 = 0x1.6a09e667f3bcdP+0, // (1.4142135623730950488) https://oeis.org/A00219
58-
inv_sqrt2 = 0x1.6a09e667f3bcdP-1, // (.70710678118654752440)
59-
sqrt3 = 0x1.bb67ae8584caaP+0, // (1.7320508075688772935) https://oeis.org/A002194
60-
inv_sqrt3 = 0x1.279a74590331cP-1, // (.57735026918962576451)
61-
phi = 0x1.9e3779b97f4a8P+0; // (1.6180339887498948482) https://oeis.org/A001622
62-
constexpr float ef = 0x1.5bf0a8P+1F, // (2.71828183) https://oeis.org/A001113
63-
egammaf = 0x1.2788d0P-1F, // (.577215665) https://oeis.org/A001620
64-
ln2f = 0x1.62e430P-1F, // (.693147181) https://oeis.org/A002162
65-
ln10f = 0x1.26bb1cP+1F, // (2.30258509) https://oeis.org/A002392
66-
log2ef = 0x1.715476P+0F, // (1.44269504)
67-
log10ef = 0x1.bcb7b2P-2F, // (.434294482)
68-
pif = 0x1.921fb6P+1F, // (3.14159265) https://oeis.org/A000796
69-
inv_pif = 0x1.45f306P-2F, // (.318309886) https://oeis.org/A049541
70-
sqrtpif = 0x1.c5bf8aP+0F, // (1.77245385) https://oeis.org/A002161
71-
inv_sqrtpif = 0x1.20dd76P-1F, // (.564189584) https://oeis.org/A087197
72-
sqrt2f = 0x1.6a09e6P+0F, // (1.41421356) https://oeis.org/A002193
73-
inv_sqrt2f = 0x1.6a09e6P-1F, // (.707106781)
74-
sqrt3f = 0x1.bb67aeP+0F, // (1.73205081) https://oeis.org/A002194
75-
inv_sqrt3f = 0x1.279a74P-1F, // (.577350269)
76-
phif = 0x1.9e377aP+0F; // (1.61803399) https://oeis.org/A001622
47+
inline constexpr float ef = e_v<float>;
48+
inline constexpr float egammaf = egamma_v<float>;
49+
inline constexpr float ln2f = ln2_v<float>;
50+
inline constexpr float ln10f = ln10_v<float>;
51+
inline constexpr float log2ef = log2e_v<float>;
52+
inline constexpr float log10ef = log10e_v<float>;
53+
inline constexpr float pif = pi_v<float>;
54+
inline constexpr float inv_pif = inv_pi_v<float>;
55+
inline constexpr float inv_sqrtpif = inv_sqrtpi_v<float>;
56+
inline constexpr float sqrt2f = sqrt2_v<float>;
57+
inline constexpr float inv_sqrt2f = inv_sqrt2_v<float>;
58+
inline constexpr float sqrt3f = sqrt3_v<float>;
59+
inline constexpr float inv_sqrt3f = inv_sqrt3_v<float>;
60+
inline constexpr float phif = phi_v<float>;
61+
62+
// sqrtpi is not in C++20 std::numbers.
63+
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
64+
inline constexpr T sqrtpi_v = T(0x1.c5bf891b4ef6bP+0); // (1.7724538509055160273) https://oeis.org/A002161
65+
inline constexpr double sqrtpi = sqrtpi_v<double>;
66+
inline constexpr float sqrtpif = sqrtpi_v<float>;
67+
7768
// These string literals are taken from below:
7869
// https://github.com/bminor/glibc/blob/8543577b04ded6d979ffcc5a818930e4d74d0645/math/math.h#L1215-L1229
7970
constexpr const char *pis = "3.141592653589793238462643383279502884",

0 commit comments

Comments
 (0)