19
19
#include < stdexcept>
20
20
#include < type_traits>
21
21
22
-
23
22
namespace sg14 {
24
23
namespace _impl {
25
24
template <class ... T>
@@ -772,17 +771,17 @@ namespace sg14 {
772
771
class Operator , class Lhs , class RhsDerived , class RhsRep ,
773
772
enable_if_t <precedes<Lhs, RhsDerived>::value, std::nullptr_t > = nullptr >
774
773
constexpr auto operate (const Lhs& lhs, const number_base<RhsDerived, RhsRep>& rhs, Operator op)
775
- -> decltype(op(lhs, to_rep (static_cast <const RhsDerived&>(rhs))))
774
+ -> decltype(op(lhs, static_cast <Lhs> (static_cast <const RhsDerived&>(rhs))))
776
775
{
777
- return op (lhs, to_rep (static_cast <const RhsDerived&>(rhs)));
776
+ return op (lhs, static_cast <Lhs> (static_cast <const RhsDerived&>(rhs)));
778
777
}
779
778
template <
780
779
class Operator , class LhsDerived , class LhsRep , class Rhs ,
781
780
enable_if_t <precedes<Rhs, LhsDerived>::value, std::nullptr_t > = nullptr >
782
781
constexpr auto operate (const number_base<LhsDerived, LhsRep>& lhs, const Rhs& rhs, Operator op)
783
- -> decltype(op(to_rep (static_cast <const LhsDerived&>(lhs)), rhs))
782
+ -> decltype(op(static_cast <Rhs> (static_cast <const LhsDerived&>(lhs)), rhs))
784
783
{
785
- return op (to_rep (static_cast <const LhsDerived&>(lhs)), rhs);
784
+ return op (static_cast <Rhs> (static_cast <const LhsDerived&>(lhs)), rhs);
786
785
}
787
786
template <
788
787
class Operator , class Lhs , class RhsDerived , class RhsRep ,
@@ -1268,6 +1267,13 @@ namespace sg14 {
1268
1267
using result_type = elastic_integer<RhsDigits, typename make_signed<RhsNarrowest>::type>;
1269
1268
return result_type::from_data (-static_cast <result_type>(rhs).data ());
1270
1269
}
1270
+ template <int RhsDigits, class RhsNarrowest >
1271
+ constexpr auto operator +(const elastic_integer<RhsDigits, RhsNarrowest>& rhs)
1272
+ -> elastic_integer<RhsDigits, typename make_signed<RhsNarrowest>::type>
1273
+ {
1274
+ using result_type = elastic_integer<RhsDigits, typename make_signed<RhsNarrowest>::type>;
1275
+ return result_type::from_data (static_cast <result_type>(rhs).data ());
1276
+ }
1271
1277
}
1272
1278
namespace std {
1273
1279
template <int LhsDigits, class LhsNarrowest , int RhsDigits, class RhsNarrowest >
@@ -1831,15 +1837,7 @@ namespace std {
1831
1837
};
1832
1838
template <class LhsRep , int LhsExponent, class RhsRep , int RhsExponent>
1833
1839
struct common_type <sg14::fixed_point<LhsRep, LhsExponent>, sg14::fixed_point<RhsRep, RhsExponent>> {
1834
- using _result_rep = typename std::common_type<LhsRep, RhsRep>::type;
1835
- static constexpr int _capacity = std::numeric_limits<_result_rep>::digits;
1836
- static constexpr int _ideal_max_top = sg14::_impl::max(
1837
- sg14::fixed_point<LhsRep, LhsExponent>::integer_digits,
1838
- sg14::fixed_point<RhsRep, RhsExponent>::integer_digits);
1839
- static constexpr int _ideal_exponent = sg14::_impl::min(LhsExponent, RhsExponent);
1840
- static constexpr int _exponent = ((_ideal_max_top-_ideal_exponent)<=_capacity) ? _ideal_exponent :
1841
- _ideal_max_top-_capacity;
1842
- using type = sg14::fixed_point<_result_rep, _exponent>;
1840
+ using type = sg14::fixed_point<sg14::_impl::common_type_t <LhsRep, RhsRep>, sg14::_impl::min(LhsExponent, RhsExponent)>;
1843
1841
};
1844
1842
}
1845
1843
namespace sg14 {
@@ -1888,54 +1886,6 @@ namespace sg14 {
1888
1886
{
1889
1887
return _impl::fp::operate<_impl::fp::division_arithmetic_operator_tag>(lhs, rhs, _impl::divide_tag);
1890
1888
}
1891
- template <class Rep , int Exponent>
1892
- constexpr auto operator ==(
1893
- const fixed_point <Rep, Exponent>& lhs,
1894
- const fixed_point <Rep, Exponent>& rhs)
1895
- -> decltype (lhs.data()==rhs.data())
1896
- {
1897
- return lhs.data ()==rhs.data ();
1898
- }
1899
- template <class Rep , int Exponent>
1900
- constexpr auto operator !=(
1901
- const fixed_point <Rep, Exponent>& lhs,
1902
- const fixed_point <Rep, Exponent>& rhs)
1903
- -> decltype (lhs.data()!=rhs.data())
1904
- {
1905
- return lhs.data ()!=rhs.data ();
1906
- }
1907
- template <class Rep , int Exponent>
1908
- constexpr auto operator >(
1909
- const fixed_point <Rep, Exponent>& lhs,
1910
- const fixed_point <Rep, Exponent>& rhs)
1911
- -> decltype (lhs.data()>rhs.data())
1912
- {
1913
- return lhs.data ()>rhs.data ();
1914
- }
1915
- template <class Rep , int Exponent>
1916
- constexpr auto operator <(
1917
- const fixed_point <Rep, Exponent>& lhs,
1918
- const fixed_point <Rep, Exponent>& rhs)
1919
- -> decltype (lhs.data()<rhs.data())
1920
- {
1921
- return lhs.data ()<rhs.data ();
1922
- }
1923
- template <class Rep , int Exponent>
1924
- constexpr auto operator >=(
1925
- const fixed_point <Rep, Exponent>& lhs,
1926
- const fixed_point <Rep, Exponent>& rhs)
1927
- -> decltype (lhs.data()>=rhs.data())
1928
- {
1929
- return lhs.data ()>=rhs.data ();
1930
- }
1931
- template <class Rep , int Exponent>
1932
- constexpr auto operator <=(
1933
- const fixed_point <Rep, Exponent>& lhs,
1934
- const fixed_point <Rep, Exponent>& rhs)
1935
- -> decltype (lhs.data()<=rhs.data())
1936
- {
1937
- return lhs.data ()<=rhs.data ();
1938
- }
1939
1889
namespace _fixed_point_operators_impl {
1940
1890
template <class Lhs , class Rhs >
1941
1891
constexpr bool is_heterogeneous () {
@@ -1952,6 +1902,12 @@ namespace sg14 {
1952
1902
{
1953
1903
return op (static_cast <_impl::common_type_t <Lhs, Rhs>>(lhs), static_cast <_impl::common_type_t <Lhs, Rhs>>(rhs));
1954
1904
};
1905
+ template <class Operator , class Rep , int Exponent, class = _impl::enable_if_t <Operator::is_comparison>>
1906
+ constexpr auto operate (const fixed_point<Rep, Exponent>& lhs, const fixed_point<Rep, Exponent>& rhs, Operator op)
1907
+ -> decltype(op(lhs.data(), rhs.data()))
1908
+ {
1909
+ return op (lhs.data (), rhs.data ());
1910
+ };
1955
1911
}
1956
1912
template <
1957
1913
class LhsRep , int LhsExponent,
@@ -2127,7 +2083,6 @@ namespace sg14 {
2127
2083
return fixed_point<LhsRep, LhsExponent-RhsValue>::from_data (lhs.data ());
2128
2084
}
2129
2085
}
2130
-
2131
2086
namespace sg14 {
2132
2087
namespace _impl {
2133
2088
template <class Rep , int Exponent>
@@ -2281,7 +2236,7 @@ namespace std {
2281
2236
}
2282
2237
static constexpr _value_type round_error () noexcept
2283
2238
{
2284
- return static_cast < _value_type>(. 5 );
2239
+ return _value_type::from_data (_rep{ 0 } );
2285
2240
}
2286
2241
static constexpr _value_type infinity () noexcept
2287
2242
{
@@ -2815,7 +2770,7 @@ namespace sg14 {
2815
2770
}
2816
2771
namespace _impl {
2817
2772
template <class OverflowTag , class OperatorTag , class LhsRep , class RhsRep , class = enable_if_t <OperatorTag::is_arithmetic>>
2818
- constexpr auto operate_common_policy (
2773
+ constexpr auto operate_common_tag (
2819
2774
OverflowTag,
2820
2775
OperatorTag,
2821
2776
const safe_integer<LhsRep, OverflowTag>& lhs,
@@ -2825,7 +2780,7 @@ namespace sg14 {
2825
2780
return make_safe_integer<OverflowTag>(_overflow_impl::operate<OverflowTag, OperatorTag>()(lhs.data (), rhs.data ()));
2826
2781
}
2827
2782
template <class OverflowTag , class OperatorTag , class LhsRep , class RhsRep , class = enable_if_t <OperatorTag::is_comparison>>
2828
- constexpr auto operate_common_policy (
2783
+ constexpr auto operate_common_tag (
2829
2784
OverflowTag,
2830
2785
OperatorTag,
2831
2786
const safe_integer<LhsRep, OverflowTag>& lhs,
@@ -2834,14 +2789,14 @@ namespace sg14 {
2834
2789
{
2835
2790
return _overflow_impl::operate<OverflowTag, OperatorTag>()(lhs.data (), rhs.data ());
2836
2791
}
2837
- template <class OperatorTag , class LhsRep , class LhsPolicy , class RhsRep , class RhsPolicy >
2792
+ template <class OperatorTag , class LhsRep , class LhsTag , class RhsRep , class RhsTag >
2838
2793
constexpr auto operate (
2839
- const safe_integer<LhsRep, LhsPolicy >& lhs,
2840
- const safe_integer<RhsRep, RhsPolicy >& rhs,
2794
+ const safe_integer<LhsRep, LhsTag >& lhs,
2795
+ const safe_integer<RhsRep, RhsTag >& rhs,
2841
2796
OperatorTag operator_tag)
2842
- -> decltype(operate_common_policy (common_type_t <LhsPolicy, RhsPolicy >{}, operator_tag, lhs, rhs))
2797
+ -> decltype(operate_common_tag (common_type_t <LhsTag, RhsTag >{}, operator_tag, lhs, rhs))
2843
2798
{
2844
- return operate_common_policy (common_type_t <LhsPolicy, RhsPolicy >{}, operator_tag, lhs, rhs);
2799
+ return operate_common_tag (common_type_t <LhsTag, RhsTag >{}, operator_tag, lhs, rhs);
2845
2800
}
2846
2801
}
2847
2802
template <class LhsRep , class LhsOverflowTag , class RhsRep , class RhsOverflowTag > constexpr auto operator >> (const safe_integer<LhsRep, LhsOverflowTag>& lhs, const safe_integer<RhsRep, RhsOverflowTag>& rhs) -> safe_integer<LhsRep, LhsOverflowTag> { return lhs.data () >> rhs.data (); } template <class Lhs , class RhsRep , class RhsOverflowTag , _impl::enable_if_t <std::is_fundamental<Lhs>::value, int > dummy = 0 > constexpr auto operator >> (const Lhs& lhs, const safe_integer<RhsRep, RhsOverflowTag>& rhs) -> Lhs { return lhs >> rhs.data (); } template <class LhsRep , class LhsOverflowTag , class Rhs , _impl::enable_if_t <std::is_fundamental<Rhs>::value, int > dummy = 0 > constexpr auto operator >> (const safe_integer<LhsRep, LhsOverflowTag>& lhs, const Rhs& rhs) -> safe_integer<LhsRep, LhsOverflowTag> { return safe_integer<LhsRep, LhsOverflowTag>(lhs.data () >> rhs); };
0 commit comments