Index: libcxx/include/CMakeLists.txt =================================================================== --- libcxx/include/CMakeLists.txt +++ libcxx/include/CMakeLists.txt @@ -118,6 +118,7 @@ mutex new numeric + numbers optional ostream queue Index: libcxx/include/module.modulemap =================================================================== --- libcxx/include/module.modulemap +++ libcxx/include/module.modulemap @@ -382,6 +382,10 @@ header "numeric" export * } + module numbers { + header "numbers" + export * + } module optional { header "optional" export * Index: libcxx/include/numbers =================================================================== --- /dev/null +++ libcxx/include/numbers @@ -0,0 +1,181 @@ +// -*- C++ -*- +//===---------------------------- numbers ---------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_NUMBERS +#define _LIBCPP_NUMBERS + +/* + numbers synopsis + +namespace std::numbers +{ + template inline constexpr T e_v = unspecified; + template inline constexpr T log2e_v = unspecified; + template inline constexpr T log10e_v = unspecified; + template inline constexpr T pi_v = unspecified; + template inline constexpr T inv_pi_v = unspecified; + template inline constexpr T inv_sqrtpi_v = unspecified; + template inline constexpr T ln2_v = unspecified; + template inline constexpr T ln10_v = unspecified; + template inline constexpr T sqrt2_v = unspecified; + template inline constexpr T sqrt3_v = unspecified; + template inline constexpr T inv_sqrt3_v = unspecified; + template inline constexpr T egamma_v = unspecified; + template inline constexpr T phi_v = unspecified; + + template inline constexpr T e_v = see below; + template inline constexpr T log2e_v = see below; + template inline constexpr T log10e_v = see below; + template inline constexpr T pi_v = see below; + template inline constexpr T inv_pi_v = see below; + template inline constexpr T inv_sqrtpi_v = see below; + template inline constexpr T ln2_v = see below; + template inline constexpr T ln10_v = see below; + template inline constexpr T sqrt2_v = see below; + template inline constexpr T sqrt3_v = see below; + template inline constexpr T inv_sqrt3_v = see below; + template inline constexpr T egamma_v = see below; + template inline constexpr T phi_v = see below; + + inline constexpr double e = e_v; + inline constexpr double log2e = log2e_v; + inline constexpr double log10e = log10e_v; + inline constexpr double pi = pi_v; + inline constexpr double inv_pi = inv_pi_v; + inline constexpr double inv_sqrtpi = inv_sqrtpi_v; + inline constexpr double ln2 = ln2_v; + inline constexpr double ln10 = ln10_v; + inline constexpr double sqrt2 = sqrt2_v; + inline constexpr double sqrt3 = sqrt3_v; + inline constexpr double inv_sqrt3 = inv_sqrt3_v; + inline constexpr double egamma = egamma_v; + inline constexpr double phi = phi_v; +} // std::numbers + +*/ + +#include <__config> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER > 17 + +namespace numbers { +template +using _IsFloat = _EnableIf, T>; + +// e +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T e_v = _IsFloat(2.718281828459045235360287471352663L); + +// log2(e) +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T log2e_v = _IsFloat(1.442695040888963407359924681001892L); + +// log10(e) +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T log10e_v = _IsFloat(0.4342944819032518276511289189166051L); + +// pi +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T pi_v = _IsFloat(3.141592653589793238462643383279503L); + +// 1/pi +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T inv_pi_v = _IsFloat(0.3183098861837906715377675267450287L); + +// 1/sqrt(pi) +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T inv_sqrtpi_v = _IsFloat(0.5641895835477562869480794515607726L); + +// log_e(2) +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T ln2_v = _IsFloat(0.6931471805599453094172321214581766L); + +// log_e(10) +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T ln10_v = _IsFloat(2.302585092994045684017991454684364L); + +// sqrt(2) +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T sqrt2_v = _IsFloat(1.414213562373095048801688724209698L); + +// sqrt(3) +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T sqrt3_v = _IsFloat(1.732050807568877293527446341505872L); + +// 1/sqrt(3) +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T inv_sqrt3_v = _IsFloat(0.5773502691896257645091487805019575L); + +// Euler-Mascheroni gama constant +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T egamma_v = _IsFloat(0.5772156649015328606065120900824024L); + +// Golden ratio phi constant +template +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR +T phi_v = _IsFloat(1.618033988749894848204586834365638L); + + +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double e = e_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double log2e = log2e_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double log10e = log10e_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double pi = pi_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double inv_pi = inv_pi_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double inv_sqrtpi = inv_sqrtpi_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double ln2 = ln2_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double ln10 = ln10_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double sqrt2 = sqrt2_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double sqrt3 = sqrt3_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double inv_sqrt3 = inv_sqrt3_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double egamma = egamma_v; +_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR + double phi = phi_v; +} //namespace numbers + +#endif //_LIBCPP_STD_VER > 17 + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif //_LIBCPP_NUMBERS Index: libcxx/test/libcxx/double_include.sh.cpp =================================================================== --- libcxx/test/libcxx/double_include.sh.cpp +++ libcxx/test/libcxx/double_include.sh.cpp @@ -99,6 +99,7 @@ #endif #include #include +#include #include #include #include Index: libcxx/test/std/numerics/numbers/numbers.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/numerics/numbers/numbers.pass.cpp @@ -0,0 +1,81 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + + +#include +#include "test_macros.h" +struct S1 { +}; + +int main() { + + // float + constexpr float f = std::numbers::e_v; + constexpr float f1 = std::numbers::log2e_v; + constexpr float f2 = std::numbers::log10e_v; + constexpr float f3 = std::numbers::pi_v; + constexpr float f4 = std::numbers::inv_pi_v; + constexpr float f5 = std::numbers::inv_sqrtpi_v; + constexpr float f6 = std::numbers::ln2_v; + constexpr float f7 = std::numbers::ln10_v; + constexpr float f8 = std::numbers::sqrt2_v; + constexpr float f9 = std::numbers::sqrt3_v; + constexpr float f10 = std::numbers::inv_sqrt3_v; + constexpr float f11 = std::numbers::egamma_v; + constexpr float f12 = std::numbers::phi_v; + + // double + constexpr double d = std::numbers::e_v; + constexpr double d1 = std::numbers::log2e_v; + constexpr double d2 = std::numbers::log10e_v; + constexpr double d3 = std::numbers::pi_v; + constexpr double d4 = std::numbers::inv_pi_v; + constexpr double d5 = std::numbers::inv_sqrtpi_v; + constexpr double d6 = std::numbers::ln2_v; + constexpr double d7 = std::numbers::ln10_v; + constexpr double d8 = std::numbers::sqrt2_v; + constexpr double d9 = std::numbers::sqrt3_v; + constexpr double d10 = std::numbers::inv_sqrt3_v; + constexpr double d11 = std::numbers::egamma_v; + constexpr double d12 = std::numbers::phi_v; + // static assert checks + static_assert(d == std::numbers::e, "d != std::numbers::e "); + static_assert(d1 == std::numbers::log2e, "d1 != std::numbers::log2e"); + static_assert(d2 == std::numbers::log10e, "d2 != std::numbers::log10e"); + static_assert(d3 == std::numbers::pi, "d3 != std::numbers::pi"); + static_assert(d4 == std::numbers::inv_pi, "d4 != std::numbers::inv_pi"); + static_assert(d5 == std::numbers::inv_sqrtpi, "d5 != std::numbers::inv_sqrtpi"); + static_assert(d6 == std::numbers::ln2, "d6 != std::numbers::ln2"); + static_assert(d7 == std::numbers::ln10, "d7 != std::numbers::ln10"); + static_assert(d8 == std::numbers::sqrt2, "d8 != std::numbers::sqrt2"); + static_assert(d9 == std::numbers::sqrt3, "d9 != std::numbers::sqrt3"); + static_assert(d10 == std::numbers::inv_sqrt3, "d10 != std::numbers::inv_sqrt3"); + static_assert(d11 == std::numbers::egamma, "d11 != std::numbers::egamma"); + static_assert(d12 == std::numbers::phi, "d12 != std::numbers::phi"); + // long oduble + constexpr long double ld = std::numbers::e_v; + constexpr long double ld1 = std::numbers::log2e_v; + constexpr long double ld2 = std::numbers::log10e_v; + constexpr long double ld3 = std::numbers::pi_v; + constexpr long double ld4 = std::numbers::inv_pi_v; + constexpr long double ld5 = std::numbers::inv_sqrtpi_v; + constexpr long double ld6 = std::numbers::ln2_v; + constexpr long double ld7 = std::numbers::ln10_v; + constexpr long double ld8 = std::numbers::sqrt2_v; + constexpr long double ld9 = std::numbers::sqrt3_v; + constexpr long double ld10 = std::numbers::inv_sqrt3_v; + constexpr long double ld11 = std::numbers::egamma_v; + constexpr long double ld12 = std::numbers::phi_v; + + return 0; + +} + +