CNL  2.0.2 (development)
Compositional Numeric Library
overloads.h
1 
2 // Copyright John McFarlane 2019.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file ../LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #if !defined(CNL_IMPL_ELASTIC_TAG_OVERLOADS_H)
8 #define CNL_IMPL_ELASTIC_TAG_OVERLOADS_H
9 
10 #include "../num_traits/set_width.h"
11 #include "../num_traits/width.h"
12 #include "../numbers/set_signedness.h"
13 #include "../numbers/signedness.h"
14 #include "definition.h"
15 #include "policy.h"
16 
17 #include <algorithm>
18 
20 namespace cnl {
21  namespace _impl {
22  template<
23  binary_arithmetic_op Operator, int LhsDigits, typename LhsNarrowest, int RhsDigits,
24  typename RhsNarrowest>
25  struct elastic_tag_overload_params {
26  using policy = typename _impl::policy<
27  Operator, LhsDigits, numbers::signedness_v<LhsNarrowest>, RhsDigits,
28  numbers::signedness_v<RhsNarrowest>>;
29  using narrowest = _impl::set_width_t<
30  numbers::set_signedness_t<
31  _impl::op_result<Operator, LhsNarrowest, RhsNarrowest>,
32  policy::is_signed>,
33  std::max(
34  _impl::width<LhsNarrowest>, _impl::width<RhsNarrowest>)>;
35 
36  using type = elastic_tag<policy::digits, narrowest>;
37  };
38  }
39 
40  template<int LhsDigits, typename LhsNarrowest, int RhsDigits, typename RhsNarrowest>
41  [[nodiscard]] constexpr auto operator+(
42  elastic_tag<LhsDigits, LhsNarrowest>, elastic_tag<RhsDigits, RhsNarrowest>) ->
43  typename _impl::elastic_tag_overload_params<
44  _impl::add_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type
45  {
46  return typename _impl::elastic_tag_overload_params<
47  _impl::add_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type{};
48  }
49 
50  template<int LhsDigits, typename LhsNarrowest, int RhsDigits, typename RhsNarrowest>
51  [[nodiscard]] constexpr auto operator-(
52  elastic_tag<LhsDigits, LhsNarrowest>, elastic_tag<RhsDigits, RhsNarrowest>) ->
53  typename _impl::elastic_tag_overload_params<
54  _impl::subtract_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type
55  {
56  return typename _impl::elastic_tag_overload_params<
57  _impl::subtract_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type{};
58  }
59 
60  template<int LhsDigits, typename LhsNarrowest, int RhsDigits, typename RhsNarrowest>
61  [[nodiscard]] constexpr auto operator*(
62  elastic_tag<LhsDigits, LhsNarrowest>, elastic_tag<RhsDigits, RhsNarrowest>) ->
63  typename _impl::elastic_tag_overload_params<
64  _impl::multiply_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type
65  {
66  return typename _impl::elastic_tag_overload_params<
67  _impl::multiply_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type{};
68  }
69 
70  template<int LhsDigits, typename LhsNarrowest, int RhsDigits, typename RhsNarrowest>
71  [[nodiscard]] constexpr auto operator/(
72  elastic_tag<LhsDigits, LhsNarrowest>, elastic_tag<RhsDigits, RhsNarrowest>) ->
73  typename _impl::elastic_tag_overload_params<
74  _impl::divide_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type
75  {
76  return typename _impl::elastic_tag_overload_params<
77  _impl::divide_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type{};
78  }
79 
80  template<int LhsDigits, typename LhsNarrowest, int RhsDigits, typename RhsNarrowest>
81  [[nodiscard]] constexpr auto operator%(
82  elastic_tag<LhsDigits, LhsNarrowest>, elastic_tag<RhsDigits, RhsNarrowest>) ->
83  typename _impl::elastic_tag_overload_params<
84  _impl::modulo_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type
85  {
86  return typename _impl::elastic_tag_overload_params<
87  _impl::modulo_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type{};
88  }
89 
90  template<int LhsDigits, typename LhsNarrowest, int RhsDigits, typename RhsNarrowest>
91  [[nodiscard]] constexpr auto operator&(
92  elastic_tag<LhsDigits, LhsNarrowest>, elastic_tag<RhsDigits, RhsNarrowest>) ->
93  typename _impl::elastic_tag_overload_params<
94  _impl::bitwise_and_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type
95  {
96  return typename _impl::elastic_tag_overload_params<
97  _impl::bitwise_and_op, LhsDigits, LhsNarrowest, RhsDigits, RhsNarrowest>::type{};
98  }
99 }
100 
101 #endif // CNL_IMPL_ELASTIC_TAG_OVERLOADS_H
cnl
compositional numeric library
Definition: abort.h:15
std::max
T max(T... args)