STL/stl/inc/ratio

287 строки
11 KiB
C++
Исходник Обычный вид История

2019-09-05 01:57:56 +03:00
// ratio standard header (core)
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#ifndef _RATIO_
#define _RATIO_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <cstdint>
2019-09-05 01:57:56 +03:00
#include <type_traits>
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
template <intmax_t _Val>
2020-05-15 01:58:54 +03:00
struct _Abs : integral_constant<intmax_t, (_Val < 0 ? -_Val : _Val)> {}; // computes absolute value of _Val
2019-09-05 01:57:56 +03:00
template <intmax_t _Ax, intmax_t _Bx, bool _Sfinae = false,
bool _Good = (_Abs<_Ax>::value <= INTMAX_MAX / (_Bx == 0 ? 1 : _Abs<_Bx>::value))>
2020-05-15 01:58:54 +03:00
struct _Safe_mult : integral_constant<intmax_t, _Ax * _Bx> {}; // computes _Ax * _Bx without overflow
2019-09-05 01:57:56 +03:00
template <intmax_t _Ax, intmax_t _Bx, bool _Sfinae>
struct _Safe_mult<_Ax, _Bx, _Sfinae, false> { // _Ax * _Bx would overflow
static_assert(_Sfinae, "integer arithmetic overflow");
};
template <intmax_t _Val>
2020-05-15 01:58:54 +03:00
struct _Sign_of : integral_constant<intmax_t, (_Val < 0 ? -1 : 1)> {}; // computes sign of _Val
2019-09-05 01:57:56 +03:00
template <intmax_t _Ax, intmax_t _Bx, bool _Good, bool _Also_good>
2020-05-15 01:58:54 +03:00
struct _Safe_addX : integral_constant<intmax_t, _Ax + _Bx> {}; // computes _Ax + _Bx without overflow
2019-09-05 01:57:56 +03:00
template <intmax_t _Ax, intmax_t _Bx>
struct _Safe_addX<_Ax, _Bx, false, false> { // _Ax + _Bx would overflow
static_assert(_Always_false<_Safe_addX>, "integer arithmetic overflow");
};
template <intmax_t _Ax, intmax_t _Bx>
2020-05-15 01:58:54 +03:00
struct _Safe_add : _Safe_addX<_Ax, _Bx, _Sign_of<_Ax>::value != _Sign_of<_Bx>::value,
(_Abs<_Ax>::value <= INTMAX_MAX - _Abs<_Bx>::value)>::type {
// computes _Ax + _Bx, forbids overflow
2019-09-05 01:57:56 +03:00
};
template <intmax_t _Ax, intmax_t _Bx>
2020-05-15 01:58:54 +03:00
struct _GcdX : _GcdX<_Bx, _Ax % _Bx>::type {}; // computes GCD of _Ax and _Bx
2019-09-05 01:57:56 +03:00
template <intmax_t _Ax>
2020-05-15 01:58:54 +03:00
struct _GcdX<_Ax, 0> : integral_constant<intmax_t, _Ax> {}; // computes GCD of _Ax and 0
2019-09-05 01:57:56 +03:00
template <intmax_t _Ax, intmax_t _Bx>
2020-05-15 01:58:54 +03:00
struct _Gcd : _GcdX<_Abs<_Ax>::value, _Abs<_Bx>::value>::type {}; // computes GCD of abs(_Ax) and abs(_Bx)
2019-09-05 01:57:56 +03:00
template <>
2020-05-15 01:58:54 +03:00
struct _Gcd<0, 0> : integral_constant<intmax_t, 1> {
// contrary to mathematical convention; avoids division by 0 in ratio_less
2019-09-05 01:57:56 +03:00
};
template <intmax_t _Nx, intmax_t _Dx = 1>
struct ratio { // holds the ratio of _Nx to _Dx
static_assert(_Dx != 0, "zero denominator");
static_assert(-INTMAX_MAX <= _Nx, "numerator too negative");
static_assert(-INTMAX_MAX <= _Dx, "denominator too negative");
static constexpr intmax_t num =
_Sign_of<_Nx>::value * _Sign_of<_Dx>::value * _Abs<_Nx>::value / _Gcd<_Nx, _Dx>::value;
static constexpr intmax_t den = _Abs<_Dx>::value / _Gcd<_Nx, _Dx>::value;
using type = ratio<num, den>;
};
template <class _Ty>
_INLINE_VAR constexpr bool _Is_ratio_v = false; // test for ratio type
template <intmax_t _Rx1, intmax_t _Rx2>
_INLINE_VAR constexpr bool _Is_ratio_v<ratio<_Rx1, _Rx2>> = true;
2019-09-05 01:57:56 +03:00
template <class _Rx1, class _Rx2>
2019-09-05 01:57:56 +03:00
struct _Ratio_add { // add two ratios
static_assert(_Is_ratio_v<_Rx1> && _Is_ratio_v<_Rx2>, "ratio_add<R1, R2> requires R1 and R2 to be ratio<>s.");
2019-09-05 01:57:56 +03:00
static constexpr intmax_t _Nx1 = _Rx1::num;
static constexpr intmax_t _Dx1 = _Rx1::den;
static constexpr intmax_t _Nx2 = _Rx2::num;
static constexpr intmax_t _Dx2 = _Rx2::den;
2019-09-05 01:57:56 +03:00
static constexpr intmax_t _Gx = _Gcd<_Dx1, _Dx2>::value;
2019-09-05 01:57:56 +03:00
// typename ratio<>::type is necessary here
using type =
typename ratio<_Safe_add<_Safe_mult<_Nx1, _Dx2 / _Gx>::value, _Safe_mult<_Nx2, _Dx1 / _Gx>::value>::value,
_Safe_mult<_Dx1, _Dx2 / _Gx>::value>::type;
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2>
using ratio_add = typename _Ratio_add<_Rx1, _Rx2>::type;
2019-09-05 01:57:56 +03:00
template <class _Rx1, class _Rx2>
2019-09-05 01:57:56 +03:00
struct _Ratio_subtract { // subtract two ratios
static_assert(_Is_ratio_v<_Rx1> && _Is_ratio_v<_Rx2>, "ratio_subtract<R1, R2> requires R1 and R2 to be ratio<>s.");
2019-09-05 01:57:56 +03:00
static constexpr intmax_t _Nx2 = _Rx2::num;
static constexpr intmax_t _Dx2 = _Rx2::den;
2019-09-05 01:57:56 +03:00
using type = ratio_add<_Rx1, ratio<-_Nx2, _Dx2>>;
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2>
using ratio_subtract = typename _Ratio_subtract<_Rx1, _Rx2>::type;
2019-09-05 01:57:56 +03:00
template <class _Rx1, class _Rx2>
2019-09-05 01:57:56 +03:00
struct _Ratio_multiply { // multiply two ratios
static_assert(_Is_ratio_v<_Rx1> && _Is_ratio_v<_Rx2>, "ratio_multiply<R1, R2> requires R1 and R2 to be ratio<>s.");
2019-09-05 01:57:56 +03:00
static constexpr intmax_t _Nx1 = _Rx1::num;
static constexpr intmax_t _Dx1 = _Rx1::den;
static constexpr intmax_t _Nx2 = _Rx2::num;
static constexpr intmax_t _Dx2 = _Rx2::den;
2019-09-05 01:57:56 +03:00
static constexpr intmax_t _Gx = _Gcd<_Nx1, _Dx2>::value;
static constexpr intmax_t _Gy = _Gcd<_Nx2, _Dx1>::value;
2019-09-05 01:57:56 +03:00
using _Num = _Safe_mult<_Nx1 / _Gx, _Nx2 / _Gy, true>;
using _Den = _Safe_mult<_Dx1 / _Gy, _Dx2 / _Gx, true>;
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2, bool _Sfinae = true, class = void>
2019-09-05 01:57:56 +03:00
struct _Ratio_multiply_sfinae { // detect overflow during multiplication
static_assert(_Sfinae, "integer arithmetic overflow");
};
template <class _Rx1, class _Rx2, bool _Sfinae>
struct _Ratio_multiply_sfinae<_Rx1, _Rx2, _Sfinae,
void_t<typename _Ratio_multiply<_Rx1, _Rx2>::_Num::type,
typename _Ratio_multiply<_Rx1, _Rx2>::_Den::type>> { // typename ratio<>::type is unnecessary here
using type = ratio<_Ratio_multiply<_Rx1, _Rx2>::_Num::value, _Ratio_multiply<_Rx1, _Rx2>::_Den::value>;
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2>
using ratio_multiply = typename _Ratio_multiply_sfinae<_Rx1, _Rx2, false>::type;
2019-09-05 01:57:56 +03:00
template <class _Rx1, class _Rx2>
2019-09-05 01:57:56 +03:00
struct _Ratio_divide { // divide two ratios
static_assert(_Is_ratio_v<_Rx1> && _Is_ratio_v<_Rx2>, "ratio_divide<R1, R2> requires R1 and R2 to be ratio<>s.");
2019-09-05 01:57:56 +03:00
static constexpr intmax_t _Nx2 = _Rx2::num;
static constexpr intmax_t _Dx2 = _Rx2::den;
2019-09-05 01:57:56 +03:00
using _Rx2_inverse = ratio<_Dx2, _Nx2>;
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2, bool _Sfinae = true>
2019-09-05 01:57:56 +03:00
using _Ratio_divide_sfinae =
typename _Ratio_multiply_sfinae<_Rx1, typename _Ratio_divide<_Rx1, _Rx2>::_Rx2_inverse, _Sfinae>::type;
2019-09-05 01:57:56 +03:00
template <class _Rx1, class _Rx2>
using ratio_divide = _Ratio_divide_sfinae<_Rx1, _Rx2, false>;
2019-09-05 01:57:56 +03:00
template <class _Rx1, class _Rx2>
struct ratio_equal : bool_constant<_Rx1::num == _Rx2::num && _Rx1::den == _Rx2::den> { // tests if ratio == ratio
static_assert(_Is_ratio_v<_Rx1> && _Is_ratio_v<_Rx2>, "ratio_equal<R1, R2> requires R1 and R2 to be ratio<>s.");
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2>
_INLINE_VAR constexpr bool ratio_equal_v = ratio_equal<_Rx1, _Rx2>::value;
2019-09-05 01:57:56 +03:00
template <class _Rx1, class _Rx2>
struct ratio_not_equal : bool_constant<!ratio_equal_v<_Rx1, _Rx2>> { // tests if ratio != ratio
static_assert(_Is_ratio_v<_Rx1> && _Is_ratio_v<_Rx2>, "ratio_not_equal<R1, R2> requires R1 and R2 to be ratio<>s.");
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2>
_INLINE_VAR constexpr bool ratio_not_equal_v = ratio_not_equal<_Rx1, _Rx2>::value;
2019-09-05 01:57:56 +03:00
struct _Big_uint128 {
uint64_t _Upper;
uint64_t _Lower;
constexpr bool operator<(const _Big_uint128 _Rhs) const noexcept {
if (_Upper != _Rhs._Upper) {
return _Upper < _Rhs._Upper;
}
return _Lower < _Rhs._Lower;
}
};
constexpr _Big_uint128 _Big_multiply(const uint64_t _Lfactor,
const uint64_t _Rfactor) noexcept { // multiply two 64-bit integers into a 128-bit integer, Knuth's algorithm M
const uint64_t _Llow = _Lfactor & 0xFFFF'FFFFULL;
const uint64_t _Lhigh = _Lfactor >> 32;
const uint64_t _Rlow = _Rfactor & 0xFFFF'FFFFULL;
const uint64_t _Rhigh = _Rfactor >> 32;
uint64_t _Temp = _Llow * _Rlow;
const uint64_t _Lower32 = _Temp & 0xFFFF'FFFFULL;
uint64_t _Carry = _Temp >> 32;
_Temp = _Llow * _Rhigh + _Carry;
const uint64_t _Mid_lower = _Temp & 0xFFFF'FFFFULL;
const uint64_t _Mid_upper = _Temp >> 32;
_Temp = _Lhigh * _Rlow + _Mid_lower;
_Carry = _Temp >> 32;
return {_Lhigh * _Rhigh + _Mid_upper + _Carry, (_Temp << 32) + _Lower32};
}
constexpr bool _Ratio_less(const int64_t _Nx1, const int64_t _Dx1, const int64_t _Nx2, const int64_t _Dx2) noexcept {
if (_Nx1 >= 0 && _Nx2 >= 0) {
return _Big_multiply(static_cast<uint64_t>(_Nx1), static_cast<uint64_t>(_Dx2))
< _Big_multiply(static_cast<uint64_t>(_Nx2), static_cast<uint64_t>(_Dx1));
2019-09-05 01:57:56 +03:00
}
if (_Nx1 < 0 && _Nx2 < 0) {
return _Big_multiply(static_cast<uint64_t>(-_Nx2), static_cast<uint64_t>(_Dx1))
< _Big_multiply(static_cast<uint64_t>(-_Nx1), static_cast<uint64_t>(_Dx2));
2019-09-05 01:57:56 +03:00
}
return _Nx1 < _Nx2;
2019-09-05 01:57:56 +03:00
}
template <class _Rx1, class _Rx2>
struct ratio_less : bool_constant<_Ratio_less(_Rx1::num, _Rx1::den, _Rx2::num, _Rx2::den)> { // tests if ratio < ratio
static_assert(_Is_ratio_v<_Rx1> && _Is_ratio_v<_Rx2>, "ratio_less<R1, R2> requires R1 and R2 to be ratio<>s.");
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2>
_INLINE_VAR constexpr bool ratio_less_v = ratio_less<_Rx1, _Rx2>::value;
2019-09-05 01:57:56 +03:00
template <class _Rx1, class _Rx2>
struct ratio_less_equal : bool_constant<!ratio_less_v<_Rx2, _Rx1>> { // tests if ratio <= ratio
static_assert(
_Is_ratio_v<_Rx1> && _Is_ratio_v<_Rx2>, "ratio_less_equal<R1, R2> requires R1 and R2 to be ratio<>s.");
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2>
_INLINE_VAR constexpr bool ratio_less_equal_v = ratio_less_equal<_Rx1, _Rx2>::value;
2019-09-05 01:57:56 +03:00
template <class _Rx1, class _Rx2>
struct ratio_greater : ratio_less<_Rx2, _Rx1>::type { // tests if ratio > ratio
static_assert(_Is_ratio_v<_Rx1> && _Is_ratio_v<_Rx2>, "ratio_greater<R1, R2> requires R1 and R2 to be ratio<>s.");
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2>
_INLINE_VAR constexpr bool ratio_greater_v = ratio_greater<_Rx1, _Rx2>::value;
2019-09-05 01:57:56 +03:00
template <class _Rx1, class _Rx2>
struct ratio_greater_equal : bool_constant<!ratio_less_v<_Rx1, _Rx2>> { // tests if ratio >= ratio
2019-09-05 01:57:56 +03:00
static_assert(
_Is_ratio_v<_Rx1> && _Is_ratio_v<_Rx2>, "ratio_greater_equal<R1, R2> requires R1 and R2 to be ratio<>s.");
2019-09-05 01:57:56 +03:00
};
template <class _Rx1, class _Rx2>
_INLINE_VAR constexpr bool ratio_greater_equal_v = ratio_greater_equal<_Rx1, _Rx2>::value;
2019-09-05 01:57:56 +03:00
using atto = ratio<1, 1000000000000000000LL>;
using femto = ratio<1, 1000000000000000LL>;
using pico = ratio<1, 1000000000000LL>;
using nano = ratio<1, 1000000000>;
using micro = ratio<1, 1000000>;
using milli = ratio<1, 1000>;
using centi = ratio<1, 100>;
using deci = ratio<1, 10>;
using deca = ratio<10, 1>;
using hecto = ratio<100, 1>;
using kilo = ratio<1000, 1>;
using mega = ratio<1000000, 1>;
using giga = ratio<1000000000, 1>;
using tera = ratio<1000000000000LL, 1>;
using peta = ratio<1000000000000000LL, 1>;
using exa = ratio<1000000000000000000LL, 1>;
_STD_END
#pragma pop_macro("new")
_STL_RESTORE_CLANG_WARNINGS
#pragma warning(pop)
#pragma pack(pop)
#endif // _STL_COMPILER_PREPROCESSOR
#endif // _RATIO_