mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-07-01 18:39:16 +00:00
fix(kernel/std): 🎨 implementation for <chrono>, <ratio> & <thread> were broken
This commit is contained in:
@ -17,40 +17,173 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
#include <type_traits>
|
||||
|
||||
namespace std
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template <intmax_t A, intmax_t B>
|
||||
struct GCD
|
||||
{
|
||||
static constexpr intmax_t value = GCD<B, A % B>::value;
|
||||
};
|
||||
|
||||
template <intmax_t A>
|
||||
struct GCD<A, 0>
|
||||
{
|
||||
static constexpr intmax_t value = A;
|
||||
};
|
||||
}
|
||||
|
||||
template <intmax_t Num, intmax_t Denom = 1>
|
||||
class ratio
|
||||
{
|
||||
private:
|
||||
static_assert(Denom != 0, "Denominator cannot be zero");
|
||||
static_assert(Num >= -__INTMAX_MAX__ && Denom >= -__INTMAX_MAX__, "Overflow");
|
||||
|
||||
private:
|
||||
static constexpr intmax_t __first = Num < 0 ? -Num : Num;
|
||||
static constexpr intmax_t __second = Denom < 0 ? -Denom : Denom;
|
||||
static constexpr intmax_t __gcd = detail::GCD<__first, __second>::value;
|
||||
|
||||
public:
|
||||
typedef ratio<Num, Denom> type;
|
||||
static constexpr intmax_t num = Num;
|
||||
static constexpr intmax_t denom = Denom;
|
||||
static constexpr intmax_t num = Denom < 0 ? -Num / __gcd : Num / __gcd;
|
||||
static constexpr intmax_t den = Denom < 0 ? -Denom / __gcd : Denom / __gcd;
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename _Tp>
|
||||
constexpr bool __ta_is_ratio = false;
|
||||
|
||||
template <intmax_t _Num, intmax_t _Den>
|
||||
constexpr bool __ta_is_ratio<ratio<_Num, _Den>> = true;
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool __is_equal()
|
||||
{
|
||||
return R1::num == R2::num && R1::den == R2::den;
|
||||
}
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool __is_less()
|
||||
{
|
||||
return R1::num * R2::den < R2::num * R1::den;
|
||||
}
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool __is_less_or_equal()
|
||||
{
|
||||
return R1::num * R2::den <= R2::num * R1::den;
|
||||
}
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool __is_greater()
|
||||
{
|
||||
return R1::num * R2::den > R2::num * R1::den;
|
||||
}
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool __is_greater_or_equal()
|
||||
{
|
||||
return R1::num * R2::den >= R2::num * R1::den;
|
||||
}
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
using ratio_add = ratio<R1::num * R2::denom + R2::num * R1::denom, R1::denom * R2::denom>;
|
||||
template <class R1, class R2>
|
||||
using ratio_subtract = ratio<R1::num * R2::denom - R2::num * R1::denom, R1::denom * R2::denom>;
|
||||
template <class R1, class R2>
|
||||
using ratio_multiply = ratio<R1::num * R2::num, R1::denom * R2::denom>;
|
||||
template <class R1, class R2>
|
||||
using ratio_divide = ratio<R1::num * R2::denom, R1::denom * R2::num>;
|
||||
struct ratio_add
|
||||
{
|
||||
static_assert(detail::__ta_is_ratio<R1> && detail::__ta_is_ratio<R2>, "Both template arguments must be std::ratio");
|
||||
|
||||
static constexpr intmax_t num = R1::num * R2::den + R2::num * R1::den;
|
||||
static constexpr intmax_t den = R1::den * R2::den;
|
||||
|
||||
using type = ratio<num, den>;
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
struct ratio_equal;
|
||||
struct ratio_subtract
|
||||
{
|
||||
static_assert(detail::__ta_is_ratio<R1> && detail::__ta_is_ratio<R2>, "Both template arguments must be std::ratio");
|
||||
|
||||
static constexpr intmax_t num = R1::num * R2::den - R2::num * R1::den;
|
||||
static constexpr intmax_t den = R1::den * R2::den;
|
||||
|
||||
using type = ratio<num, den>;
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
struct ratio_not_equal;
|
||||
struct ratio_multiply
|
||||
{
|
||||
static_assert(detail::__ta_is_ratio<R1> && detail::__ta_is_ratio<R2>, "Both templates must be ratios");
|
||||
|
||||
static constexpr intmax_t num = R1::num * R2::num;
|
||||
static constexpr intmax_t den = R1::den * R2::den;
|
||||
|
||||
using type = ratio<num, den>;
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
struct ratio_less;
|
||||
struct ratio_divide
|
||||
{
|
||||
static_assert(R2::num != 0, "Divide by zero");
|
||||
|
||||
static constexpr intmax_t num = R1::num * R2::den;
|
||||
static constexpr intmax_t den = R1::den * R2::num;
|
||||
|
||||
using type = ratio<num, den>;
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
struct ratio_less_equal;
|
||||
struct ratio_equal : public std::integral_constant<bool, detail::__is_equal<R1, R2>()>
|
||||
{
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
struct ratio_greater;
|
||||
struct ratio_not_equal : public std::integral_constant<bool, !detail::__is_equal<R1, R2>()>
|
||||
{
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
struct ratio_greater_equal;
|
||||
struct ratio_less : public std::integral_constant<bool, detail::__is_less<R1, R2>()>
|
||||
{
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
struct ratio_less_equal : public std::integral_constant<bool, detail::__is_less_or_equal<R1, R2>()>
|
||||
{
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
struct ratio_greater : public std::integral_constant<bool, detail::__is_greater<R1, R2>()>
|
||||
{
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
struct ratio_greater_equal : public std::integral_constant<bool, detail::__is_greater_or_equal<R1, R2>()>
|
||||
{
|
||||
};
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool ratio_equal_v = ratio_equal<R1, R2>::value;
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool ratio_not_equal_v = ratio_not_equal<R1, R2>::value;
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool ratio_less_v = ratio_less<R1, R2>::value;
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool ratio_less_equal_v = ratio_less_equal<R1, R2>::value;
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool ratio_greater_v = ratio_greater<R1, R2>::value;
|
||||
|
||||
template <class R1, class R2>
|
||||
constexpr bool ratio_greater_equal_v = ratio_greater_equal<R1, R2>::value;
|
||||
|
||||
// typedef ratio<1, 1000000000000000000000000000000> quecto;
|
||||
// typedef ratio<1, 1000000000000000000000000000> ronto;
|
||||
|
Reference in New Issue
Block a user