Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions llvm/include/llvm/ADT/STLForwardCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,54 @@ namespace llvm {
// Features from C++20
//===----------------------------------------------------------------------===//

namespace numbers {
// clang-format off
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T e_v = T(0x1.5bf0a8b145769P+1); // (2.7182818284590452354) https://oeis.org/A001113
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T egamma_v = T(0x1.2788cfc6fb619P-1); // (.57721566490153286061) https://oeis.org/A001620
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T ln2_v = T(0x1.62e42fefa39efP-1); // (.69314718055994530942) https://oeis.org/A002162
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T ln10_v = T(0x1.26bb1bbb55516P+1); // (2.3025850929940456840) https://oeis.org/A002392
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T log2e_v = T(0x1.71547652b82feP+0); // (1.4426950408889634074)
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T log10e_v = T(0x1.bcb7b1526e50eP-2); // (.43429448190325182765)
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T pi_v = T(0x1.921fb54442d18P+1); // (3.1415926535897932385) https://oeis.org/A000796
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T inv_pi_v = T(0x1.45f306dc9c883P-2); // (.31830988618379067154) https://oeis.org/A049541
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T inv_sqrtpi_v = T(0x1.20dd750429b6dP-1); // (.56418958354775628695) https://oeis.org/A087197
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T sqrt2_v = T(0x1.6a09e667f3bcdP+0); // (1.4142135623730950488) https://oeis.org/A00219
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T inv_sqrt2_v = T(0x1.6a09e667f3bcdP-1); // (.70710678118654752440)
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T sqrt3_v = T(0x1.bb67ae8584caaP+0); // (1.7320508075688772935) https://oeis.org/A002194
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T inv_sqrt3_v = T(0x1.279a74590331cP-1); // (.57735026918962576451)
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T phi_v = T(0x1.9e3779b97f4a8P+0); // (1.6180339887498948482) https://oeis.org/A001622

inline constexpr double e = e_v<double>;
inline constexpr double egamma = egamma_v<double>;
inline constexpr double ln2 = ln2_v<double>;
inline constexpr double ln10 = ln10_v<double>;
inline constexpr double log2e = log2e_v<double>;
inline constexpr double log10e = log10e_v<double>;
inline constexpr double pi = pi_v<double>;
inline constexpr double inv_pi = inv_pi_v<double>;
inline constexpr double inv_sqrtpi = inv_sqrtpi_v<double>;
inline constexpr double sqrt2 = sqrt2_v<double>;
inline constexpr double inv_sqrt2 = inv_sqrt2_v<double>;
inline constexpr double sqrt3 = sqrt3_v<double>;
inline constexpr double inv_sqrt3 = inv_sqrt3_v<double>;
inline constexpr double phi = phi_v<double>;
// clang-format on
} // namespace numbers

template <typename T>
struct remove_cvref // NOLINT(readability-identifier-naming)
{
Expand Down
53 changes: 22 additions & 31 deletions llvm/include/llvm/Support/MathExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_SUPPORT_MATHEXTRAS_H
#define LLVM_SUPPORT_MATHEXTRAS_H

#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/ADT/bit.h"
#include "llvm/Support/Compiler.h"
#include <cassert>
Expand Down Expand Up @@ -42,38 +43,28 @@ using common_sint =

/// Mathematical constants.
namespace numbers {
// TODO: Track C++20 std::numbers.
// clang-format off
constexpr double e = 0x1.5bf0a8b145769P+1, // (2.7182818284590452354) https://oeis.org/A001113
egamma = 0x1.2788cfc6fb619P-1, // (.57721566490153286061) https://oeis.org/A001620
ln2 = 0x1.62e42fefa39efP-1, // (.69314718055994530942) https://oeis.org/A002162
ln10 = 0x1.26bb1bbb55516P+1, // (2.3025850929940456840) https://oeis.org/A002392
log2e = 0x1.71547652b82feP+0, // (1.4426950408889634074)
log10e = 0x1.bcb7b1526e50eP-2, // (.43429448190325182765)
pi = 0x1.921fb54442d18P+1, // (3.1415926535897932385) https://oeis.org/A000796
inv_pi = 0x1.45f306dc9c883P-2, // (.31830988618379067154) https://oeis.org/A049541
sqrtpi = 0x1.c5bf891b4ef6bP+0, // (1.7724538509055160273) https://oeis.org/A002161
inv_sqrtpi = 0x1.20dd750429b6dP-1, // (.56418958354775628695) https://oeis.org/A087197
sqrt2 = 0x1.6a09e667f3bcdP+0, // (1.4142135623730950488) https://oeis.org/A00219
inv_sqrt2 = 0x1.6a09e667f3bcdP-1, // (.70710678118654752440)
sqrt3 = 0x1.bb67ae8584caaP+0, // (1.7320508075688772935) https://oeis.org/A002194
inv_sqrt3 = 0x1.279a74590331cP-1, // (.57735026918962576451)
phi = 0x1.9e3779b97f4a8P+0; // (1.6180339887498948482) https://oeis.org/A001622
constexpr float ef = 0x1.5bf0a8P+1F, // (2.71828183) https://oeis.org/A001113
egammaf = 0x1.2788d0P-1F, // (.577215665) https://oeis.org/A001620
ln2f = 0x1.62e430P-1F, // (.693147181) https://oeis.org/A002162
ln10f = 0x1.26bb1cP+1F, // (2.30258509) https://oeis.org/A002392
log2ef = 0x1.715476P+0F, // (1.44269504)
log10ef = 0x1.bcb7b2P-2F, // (.434294482)
pif = 0x1.921fb6P+1F, // (3.14159265) https://oeis.org/A000796
inv_pif = 0x1.45f306P-2F, // (.318309886) https://oeis.org/A049541
sqrtpif = 0x1.c5bf8aP+0F, // (1.77245385) https://oeis.org/A002161
inv_sqrtpif = 0x1.20dd76P-1F, // (.564189584) https://oeis.org/A087197
sqrt2f = 0x1.6a09e6P+0F, // (1.41421356) https://oeis.org/A002193
inv_sqrt2f = 0x1.6a09e6P-1F, // (.707106781)
sqrt3f = 0x1.bb67aeP+0F, // (1.73205081) https://oeis.org/A002194
inv_sqrt3f = 0x1.279a74P-1F, // (.577350269)
phif = 0x1.9e377aP+0F; // (1.61803399) https://oeis.org/A001622
inline constexpr float ef = e_v<float>;
inline constexpr float egammaf = egamma_v<float>;
inline constexpr float ln2f = ln2_v<float>;
inline constexpr float ln10f = ln10_v<float>;
inline constexpr float log2ef = log2e_v<float>;
inline constexpr float log10ef = log10e_v<float>;
inline constexpr float pif = pi_v<float>;
inline constexpr float inv_pif = inv_pi_v<float>;
inline constexpr float inv_sqrtpif = inv_sqrtpi_v<float>;
inline constexpr float sqrt2f = sqrt2_v<float>;
inline constexpr float inv_sqrt2f = inv_sqrt2_v<float>;
inline constexpr float sqrt3f = sqrt3_v<float>;
inline constexpr float inv_sqrt3f = inv_sqrt3_v<float>;
inline constexpr float phif = phi_v<float>;

// sqrtpi is not in C++20 std::numbers.
template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
inline constexpr T sqrtpi_v = T(0x1.c5bf891b4ef6bP+0); // (1.7724538509055160273) https://oeis.org/A002161
inline constexpr double sqrtpi = sqrtpi_v<double>;
inline constexpr float sqrtpif = sqrtpi_v<float>;

// These string literals are taken from below:
// https://github.com/bminor/glibc/blob/8543577b04ded6d979ffcc5a818930e4d74d0645/math/math.h#L1215-L1229
constexpr const char *pis = "3.141592653589793238462643383279502884",
Expand Down