fixed_point (deprecated)  rev.2
Binary Fixed-Point Arithmetic Library in C++
common.h
1 
2 // Copyright John McFarlane 2015 - 2016.
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 // definitions that are directly required by more than one header of the API
8 
9 #if !defined(SG14_COMMON_H)
10 #define SG14_COMMON_H 1
11 
12 #include <utility>
13 
14 namespace sg14 {
15  namespace _impl {
17  // sg14::_impl::max
18 
19  template<class T>
20  constexpr T max(T a, T b)
21  {
22  return (a<b) ? b : a;
23  }
24 
26  // sg14::_impl::min
27 
28  template<class T>
29  constexpr T min(T a, T b)
30  {
31  return (a<b) ? a : b;
32  }
33 
36  // operator helpers
37 
39  // operation tags
40 
41  struct arithmetic_op {
42  static constexpr bool is_arithmetic = true;
43  };
44 
45  struct comparison_op {
46  static constexpr bool is_comparison = true;
47  };
48 
49  struct minus_op : arithmetic_op {
50  template<class Rhs>
51  constexpr auto operator()(const Rhs& rhs) const -> decltype(-rhs)
52  {
53  return -rhs;
54  }
55  };
56 
57  struct plus_op : arithmetic_op {
58  template<class Rhs>
59  constexpr auto operator()(const Rhs& rhs) const -> decltype(+rhs)
60  {
61  return +rhs;
62  }
63  };
64 
65  struct add_op : arithmetic_op {
66  template<class Lhs, class Rhs>
67  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs+rhs)
68  {
69  return lhs+rhs;
70  }
71  };
72 
73  struct subtract_op : arithmetic_op {
74  template<class Lhs, class Rhs>
75  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs-rhs)
76  {
77  return lhs-rhs;
78  }
79  };
80 
81  struct multiply_op : arithmetic_op {
82  template<class Lhs, class Rhs>
83  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs*rhs)
84  {
85  return lhs*rhs;
86  }
87  };
88 
89  struct divide_op : arithmetic_op {
90  template<class Lhs, class Rhs>
91  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs/rhs)
92  {
93  return lhs/rhs;
94  }
95  };
96 
97  struct bitwise_or_op : arithmetic_op {
98  template<class Lhs, class Rhs>
99  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs|rhs)
100  {
101  return lhs|rhs;
102  }
103  };
104 
105  struct bitwise_and_op : arithmetic_op {
106  template<class Lhs, class Rhs>
107  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs&rhs)
108  {
109  return lhs&rhs;
110  }
111  };
112 
113  struct bitwise_xor_op : arithmetic_op {
114  template<class Lhs, class Rhs>
115  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs^rhs)
116  {
117  return lhs^rhs;
118  }
119  };
120 
121  struct equal_op : comparison_op {
122  template<class Lhs, class Rhs>
123  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs==rhs)
124  {
125  return lhs==rhs;
126  }
127  };
128 
129  struct not_equal_op : comparison_op {
130  template<class Lhs, class Rhs>
131  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs!=rhs)
132  {
133  return lhs!=rhs;
134  }
135  };
136 
137  struct less_than_op : comparison_op {
138  template<class Lhs, class Rhs>
139  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs<rhs)
140  {
141  return lhs<rhs;
142  }
143  };
144 
145  struct greater_than_op : comparison_op {
146  template<class Lhs, class Rhs>
147  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs>rhs)
148  {
149  return lhs>rhs;
150  }
151  };
152 
153  struct less_than_or_equal_op : comparison_op {
154  template<class Lhs, class Rhs>
155  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs<=rhs)
156  {
157  return lhs<=rhs;
158  }
159  };
160 
161  struct greater_than_or_equal_op : comparison_op {
162  template<class Lhs, class Rhs>
163  constexpr auto operator()(const Lhs& lhs, const Rhs& rhs) const -> decltype(lhs>=rhs)
164  {
165  return lhs>=rhs;
166  }
167  };
168 
169  static constexpr plus_op plus_tag {};
170  static constexpr minus_op minus_tag {};
171 
172  static constexpr add_op add_tag {};
173  static constexpr subtract_op subtract_tag {};
174  static constexpr multiply_op multiply_tag {};
175  static constexpr divide_op divide_tag {};
176 
177  static constexpr bitwise_or_op bitwise_or_tag {};
178  static constexpr bitwise_and_op bitwise_and_tag {};
179  static constexpr bitwise_xor_op bitwise_xor_tag {};
180 
181  static constexpr equal_op equal_tag {};
182  static constexpr not_equal_op not_equal_tag {};
183  static constexpr less_than_op less_than_tag {};
184  static constexpr greater_than_op greater_than_tag {};
185  static constexpr less_than_or_equal_op less_than_or_equal_tag {};
186  static constexpr greater_than_or_equal_op greater_than_or_equal_tag {};
187 
189  // sg14::_impl::rep_op_result
190 
191  template<class Operator, class Lhs, class Rhs>
192  using op_result = decltype(Operator()(std::declval<Lhs>(), std::declval<Rhs>()));
193  }
194 }
195 
196 #endif // SG14_COMMON_H
study group 14 of the C++ working group
Definition: const_integer.h:22