Skip to content

Commit

Permalink
Rename namespace port to pstd
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch committed Jun 17, 2024
1 parent 49818d9 commit 5aacc82
Show file tree
Hide file tree
Showing 35 changed files with 133 additions and 129 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Changes in primecount-7.14, 2024-06-17

* int128_t.hpp: Rename namespace port to pstd (portable std namespace).

Changes in primecount-7.13, 2024-04-15

* Add preliminary MSVC 128-bit support.
Expand Down
4 changes: 2 additions & 2 deletions include/FactorTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class FactorTable : public BaseFactorTable
throw primecount_error("y must be <= FactorTable::max()");

y = std::max<int64_t>(1, y);
T T_MAX = port::numeric_limits<T>::max();
T T_MAX = pstd::numeric_limits<T>::max();
factor_.resize(to_index(y) + 1);

// mu(1) = 1.
Expand Down Expand Up @@ -189,7 +189,7 @@ class FactorTable : public BaseFactorTable

static maxint_t max()
{
maxint_t T_MAX = port::numeric_limits<T>::max();
maxint_t T_MAX = pstd::numeric_limits<T>::max();
return ipow<2>(T_MAX - 1) - 1;
}

Expand Down
4 changes: 2 additions & 2 deletions include/FactorTableD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class FactorTableD : public BaseFactorTable
throw primecount_error("z must be <= FactorTable::max()");

z = std::max<int64_t>(1, z);
T T_MAX = port::numeric_limits<T>::max();
T T_MAX = pstd::numeric_limits<T>::max();
factor_.resize(to_index(z) + 1);

// mu(1) = 1.
Expand Down Expand Up @@ -224,7 +224,7 @@ class FactorTableD : public BaseFactorTable

static maxint_t max()
{
maxint_t T_MAX = port::numeric_limits<T>::max();
maxint_t T_MAX = pstd::numeric_limits<T>::max();
return ipow<2>(T_MAX - 1) - 1;
}

Expand Down
4 changes: 2 additions & 2 deletions include/PhiTiny.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PhiTiny : public BitSieve240
// Unsigned integer division is usually
// faster than signed integer division,
// especially for int128_t.
using UT = typename port::make_unsigned<T>::type;
using UT = typename pstd::make_unsigned<T>::type;

if (a < max_a())
return phi((UT) x, a);
Expand Down Expand Up @@ -182,7 +182,7 @@ phi_tiny(T x, uint64_t a)
{
// If possible use smaller integer type
// to speed up integer division.
if (x <= port::numeric_limits<uint64_t>::max())
if (x <= pstd::numeric_limits<uint64_t>::max())
return phiTiny.phi_recursive((uint64_t) x, a);
else
return phiTiny.phi_recursive(x, a);
Expand Down
20 changes: 10 additions & 10 deletions include/fast_div.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ fast_div(X x, Y y)

#if defined(ENABLE_DIV32)

using UX = typename port::make_unsigned<X>::type;
using UY = typename port::make_unsigned<Y>::type;
using UX = typename pstd::make_unsigned<X>::type;
using UY = typename pstd::make_unsigned<Y>::type;

if (x <= port::numeric_limits<uint32_t>::max())
if (x <= pstd::numeric_limits<uint32_t>::max())
return uint32_t(x) / UY(y);
else
return UX(x) / UY(y);
#else
// Unsigned integer division is usually
// faster than signed integer division.
using UX = typename port::make_unsigned<X>::type;
using UY = typename port::make_unsigned<Y>::type;
using UX = typename pstd::make_unsigned<X>::type;
using UY = typename pstd::make_unsigned<Y>::type;
return UX(x) / UY(y);
#endif
}
Expand All @@ -72,8 +72,8 @@ fast_div(X x, Y y)

// Unsigned integer division is usually
// faster than signed integer division.
using UX = typename port::make_unsigned<X>::type;
using UY = typename port::make_unsigned<Y>::type;
using UX = typename pstd::make_unsigned<X>::type;
using UY = typename pstd::make_unsigned<Y>::type;
return UX(x) / UY(y);
}

Expand All @@ -89,10 +89,10 @@ fast_div(X x, Y y)

// Unsigned integer division is usually
// faster than signed integer division.
using UX = typename port::make_unsigned<X>::type;
using UY = typename port::make_unsigned<Y>::type;
using UX = typename pstd::make_unsigned<X>::type;
using UY = typename pstd::make_unsigned<Y>::type;

if (x <= port::numeric_limits<uint64_t>::max())
if (x <= pstd::numeric_limits<uint64_t>::max())
return uint64_t(x) / UY(y);
else
return UX(x) / UY(y);
Expand Down
16 changes: 8 additions & 8 deletions include/imath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ template <typename T>
inline T next_power_of_2(T x)
{
#if __cplusplus >= 202002L
using UT = typename port::make_unsigned<T>::type;
using UT = typename pstd::make_unsigned<T>::type;
return std::bit_ceil((UT) x);

#elif __has_builtin(__builtin_clzll)
if (x == 0 || x == 1)
return 1;

static_assert(sizeof(T) <= sizeof(unsigned long long), "Unsupported type, wider than long long!");
auto bits = port::numeric_limits<unsigned long long>::digits;
auto bits = pstd::numeric_limits<unsigned long long>::digits;
auto shift = bits - __builtin_clzll(x - 1);
return (T) (1ull << shift);

Expand All @@ -60,8 +60,8 @@ inline T next_power_of_2(T x)
return 1;

x--;
using UT = typename port::make_unsigned<T>::type;
T bits = port::numeric_limits<UT>::digits;
using UT = typename pstd::make_unsigned<T>::type;
T bits = pstd::numeric_limits<UT>::digits;

for (T i = 1; i < bits; i += i)
x |= (x >> i);
Expand All @@ -80,23 +80,23 @@ template <typename T>
inline T ilog2(T x)
{
#if __cplusplus >= 202002L
using UT = typename port::make_unsigned<T>::type;
using UT = typename pstd::make_unsigned<T>::type;
auto ux = (UT) x;
ux = (ux > 0) ? ux : 1;
return std::bit_width(ux) - 1;

#elif __has_builtin(__builtin_clzll)
static_assert(sizeof(T) <= sizeof(unsigned long long), "Unsupported type, wider than long long!");
auto bits = port::numeric_limits<unsigned long long>::digits;
auto bits = pstd::numeric_limits<unsigned long long>::digits;

// Workaround to avoid undefined behavior,
// __builtin_clz(0) is undefined.
x = (x > 0) ? x : 1;
return (T) ((bits - 1) - __builtin_clzll(x));

#else
using UT = typename port::make_unsigned<T>::type;
T bits = port::numeric_limits<UT>::digits;
using UT = typename pstd::make_unsigned<T>::type;
T bits = pstd::numeric_limits<UT>::digits;
T log2 = 0;

for (T i = bits / 2; i > 0; i /= 2)
Expand Down
30 changes: 15 additions & 15 deletions include/int128_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,18 @@ using std::to_string;
// This is required for GCC/Clang if the user compiles with -std=c++*
// instead of -std=gnu++* and also for LLVM/Clang on Windows.
namespace {
namespace port {
namespace pstd {

using namespace primecount;

// port::is_same
// pstd::is_same
template<class T, class U>
struct is_same : std::false_type {};

template<class T>
struct is_same<T, T> : std::true_type {};

// port::conditional
// pstd::conditional
template <bool Cond, class T, class F>
struct conditional {
using type = T;
Expand All @@ -125,7 +125,7 @@ struct conditional<false, T, F> {
using type = F;
};

// port::is_integral
// pstd::is_integral
template<typename T> struct is_integral {
static constexpr bool value = std::is_integral<T>::value;
};
Expand All @@ -135,7 +135,7 @@ template<typename T> struct is_integral {
template<> struct is_integral<uint128_t> : std::true_type {};
#endif

// port::is_floating_point
// pstd::is_floating_point
template<typename T> struct is_floating_point {
static constexpr bool value = std::is_floating_point<T>::value;
};
Expand All @@ -145,7 +145,7 @@ template<typename T> struct is_floating_point {
template<> struct is_floating_point<uint128_t> : std::false_type {};
#endif

// port::is_signed
// pstd::is_signed
template<typename T> struct is_signed {
static constexpr bool value = std::is_signed<T>::value;
};
Expand All @@ -155,7 +155,7 @@ template<typename T> struct is_signed {
template<> struct is_signed<uint128_t> : std::false_type {};
#endif

// port::is_unsigned
// pstd::is_unsigned
template<typename T> struct is_unsigned {
static constexpr bool value = std::is_unsigned<T>::value;
};
Expand All @@ -165,7 +165,7 @@ template<typename T> struct is_unsigned {
template<> struct is_unsigned<uint128_t> : std::true_type {};
#endif

// port::make_unsigned
// pstd::make_unsigned
template<typename T> struct make_unsigned {
using type = typename std::make_unsigned<T>::type;
};
Expand All @@ -175,13 +175,13 @@ template<typename T> struct make_unsigned {
template<> struct make_unsigned<uint128_t> { using type = uint128_t; };
#endif

// port::numeric_limits
// pstd::numeric_limits
template<typename T> struct numeric_limits {
static constexpr T min() { return std::numeric_limits<T>::min(); }
static constexpr T max() { return std::numeric_limits<T>::max(); }
static constexpr T infinity() { return std::numeric_limits<T>::infinity(); }
static constexpr T epsilon() { return std::numeric_limits<T>::epsilon(); }
static constexpr int digits = std::numeric_limits<T>::digits;
static constexpr T min() { return std::numeric_limits<T>::min(); }
static constexpr T max() { return std::numeric_limits<T>::max(); }
static constexpr T infinity() { return std::numeric_limits<T>::infinity(); }
static constexpr T epsilon() { return std::numeric_limits<T>::epsilon(); }
static constexpr int digits = std::numeric_limits<T>::digits;
};

#if defined(HAVE_INT128_T)
Expand All @@ -200,7 +200,7 @@ template<typename T> struct numeric_limits {
};
#endif

} // namespace port
} // namespace pstd
} // namespace

#if defined(HAVE_INT128_T) && \
Expand Down
4 changes: 2 additions & 2 deletions include/isqrt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ ALWAYS_INLINE T isqrt(T x)
// time in all cases i.e. even if sqrt_max were declared
// without constexpr.
//
constexpr T sqrt_max = ct_sqrt(port::numeric_limits<T>::max());
constexpr T sqrt_max = ct_sqrt(pstd::numeric_limits<T>::max());

// For 128-bit integers we use uint64_t as the
// result type. For all other types we use the
// same result type as the input type.
using R = typename port::conditional<sizeof(T) / 2 == sizeof(uint64_t), uint64_t, T>::type;
using R = typename pstd::conditional<sizeof(T) / 2 == sizeof(uint64_t), uint64_t, T>::type;
R r = (R) std::min(s, sqrt_max);

// In my tests the first corrections were needed above
Expand Down
12 changes: 6 additions & 6 deletions include/min.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ template <typename A, typename B>
struct is_comparable
{
enum {
value = port::is_same<A, B>::value || (
port::is_integral<A>::value &&
port::is_integral<B>::value &&
value = pstd::is_same<A, B>::value || (
pstd::is_integral<A>::value &&
pstd::is_integral<B>::value &&
sizeof(A) >= sizeof(B))
};
};
Expand All @@ -33,9 +33,9 @@ template <typename A, typename B, typename C>
struct is_comparable_3
{
enum {
value = port::is_integral<A>::value &&
port::is_integral<B>::value &&
port::is_integral<C>::value &&
value = pstd::is_integral<A>::value &&
pstd::is_integral<B>::value &&
pstd::is_integral<C>::value &&
sizeof(A) >= sizeof(B) &&
sizeof(B) >= sizeof(C)
};
Expand Down
8 changes: 4 additions & 4 deletions src/LogarithmicIntegral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ T li(T x)
sum += (p / q) * inner_sum;

// Not converging anymore
if (std::abs(sum - old_sum) <= port::numeric_limits<T>::epsilon())
if (std::abs(sum - old_sum) <= pstd::numeric_limits<T>::epsilon())
break;
}

Expand Down Expand Up @@ -129,7 +129,7 @@ T Li_inverse(T x)
return 0;

T t = initialNthPrimeApprox(x);
T old_term = port::numeric_limits<T>::infinity();
T old_term = pstd::numeric_limits<T>::infinity();

// The condition i < ITERS is required in case the computation
// does not converge. This happened on Linux i386 where
Expand Down Expand Up @@ -290,8 +290,8 @@ T Li_inverse_overflow_check(T x)
FLOAT res = Li_inverse((FLOAT) x);

// Prevent integer overflow
if (res > (FLOAT) port::numeric_limits<T>::max())
return port::numeric_limits<T>::max();
if (res > (FLOAT) pstd::numeric_limits<T>::max())
return pstd::numeric_limits<T>::max();
else
return (T) res;
}
Expand Down
2 changes: 1 addition & 1 deletion src/P2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ T P2_OpenMP(T x,

// \sum_{i=a+1}^{b} -(i - 1)
T sum = (a - 2) * (a + 1) / 2 - (b - 2) * (b + 1) / 2;
static_assert(port::is_signed<T>::value, "T must be signed integer type");
static_assert(pstd::is_signed<T>::value, "T must be signed integer type");

int64_t xy = (int64_t)(x / max(y, 1));
LoadBalancerP2 loadBalancer(x, xy, threads, is_print);
Expand Down
2 changes: 1 addition & 1 deletion src/PhiTiny.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ PhiTiny::PhiTiny()
for (uint64_t x = 1; x < pp; x++)
{
uint64_t phi_xa = phi(x, a - 1) - phi(x / primes[a], a - 1);
ASSERT(phi_xa <= port::numeric_limits<uint8_t>::max());
ASSERT(phi_xa <= pstd::numeric_limits<uint8_t>::max());
phi_[a][x] = (uint8_t) phi_xa;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/RiemannR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace {
const primecount::Array<long double, 128> zeta =
{
-0.500000000000000000000000000000000000000L,
port::numeric_limits<long double>::infinity(),
pstd::numeric_limits<long double>::infinity(),
1.644934066848226436472415166646025189219L,
1.202056903159594285399738161511449990765L,
1.082323233711138191516003696541167902775L,
Expand Down Expand Up @@ -214,7 +214,7 @@ T RiemannR(T x)
if (x < T(1e-5))
return 0;

T epsilon = port::numeric_limits<T>::epsilon();
T epsilon = pstd::numeric_limits<T>::epsilon();
T sum = 1;
T term = 1;
T logx = std::log(x);
Expand Down Expand Up @@ -254,7 +254,7 @@ T RiemannR_inverse(T x)
return 0;

T t = initialNthPrimeApprox(x);
T old_term = port::numeric_limits<T>::infinity();
T old_term = pstd::numeric_limits<T>::infinity();

// The condition i < ITERS is required in case the computation
// does not converge. This happened on Linux i386 where
Expand Down Expand Up @@ -524,8 +524,8 @@ T RiemannR_inverse_overflow_check(T x)
FLOAT res = RiemannR_inverse((FLOAT) x);

// Prevent integer overflow
if (res > (FLOAT) port::numeric_limits<T>::max())
return port::numeric_limits<T>::max();
if (res > (FLOAT) pstd::numeric_limits<T>::max())
return pstd::numeric_limits<T>::max();
else
return (T) res;
}
Expand Down
2 changes: 1 addition & 1 deletion src/S1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ int128_t S1(int128_t x,
int128_t s1;

// uses less memory
if (y <= port::numeric_limits<uint32_t>::max())
if (y <= pstd::numeric_limits<uint32_t>::max())
s1 = S1_OpenMP(x, (uint32_t) y, c, threads);
else
s1 = S1_OpenMP(x, y, c, threads);
Expand Down
Loading

0 comments on commit 5aacc82

Please sign in to comment.