fixed_point (deprecated)  rev.2
Binary Fixed-Point Arithmetic Library in C++
fixed_point_common_type.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 
9 
10 #if !defined(SG14_FIXED_POINT_COMMON_TYPE_H)
11 #define SG14_FIXED_POINT_COMMON_TYPE_H 1
12 
13 #include "fixed_point_type.h"
14 
16 namespace sg14 {
17 
19  // implementation-specific definitions
20 
21  namespace _impl {
22  namespace fp {
23  namespace ct {
24 
26  // sg14::_impl::fp::ct::common_type_mixed
27 
28  template<class Lhs, class Rhs, class _Enable = void>
29  struct common_type_mixed;
30 
31  // given a fixed-point and an integer type,
32  // generates a fixed-point type that is as big as both of them (or as close as possible)
33  template<class LhsRep, int LhsExponent, class RhsInteger>
34  struct common_type_mixed<fixed_point
35  <LhsRep, LhsExponent>, RhsInteger, _impl::enable_if_t<std::numeric_limits<RhsInteger>::is_integer>> : std::common_type<
36  fixed_point<LhsRep, LhsExponent>, fixed_point<RhsInteger, 0>> {
37  };
38 
39  // given a fixed-point and a floating-point type,
40  // generates a floating-point type that is as big as both of them (or as close as possible)
41  template<class LhsRep, int LhsExponent, class Float>
42  struct common_type_mixed<
43  fixed_point<LhsRep, LhsExponent>, Float,
44  _impl::enable_if_t<std::is_floating_point<Float>::value>>
45  : std::common_type<_impl::fp::float_of_same_size<LhsRep>, Float> {
46  };
47  }
48  }
49  }
50 }
51 
52 namespace std {
54  // std::common_type<> specializations related to sg14::sg14::fixed_point<>
55 
56  // std::common_type<fixed_point<>>
57  template<class Rep, int Exponent>
58  struct common_type<sg14::fixed_point<Rep, Exponent>> {
59  using type = sg14::fixed_point<
60  typename std::common_type<Rep>::type,
61  Exponent>;
62  };
63 
64  // std::common_type<fixed_point<>, not-fixed-point>
65  template<class LhsRep, int LhsExponent, class Rhs>
66  struct common_type<sg14::fixed_point<LhsRep, LhsExponent>, Rhs> {
67  static_assert(!sg14::_impl::is_fixed_point<Rhs>::value, "fixed-point Rhs type");
68  using type = typename sg14::_impl::fp::ct::common_type_mixed<sg14::fixed_point<LhsRep, LhsExponent>, Rhs>::type;
69  };
70 
71  // std::common_type<not-fixed-point, fixed_point<>>
72  template<class Lhs, class RhsRep, int RhsExponent>
73  struct common_type<Lhs, sg14::fixed_point<RhsRep, RhsExponent>> {
74  static_assert(!sg14::_impl::is_fixed_point<Lhs>::value, "fixed-point Lhs type");
75  using type = typename sg14::_impl::fp::ct::common_type_mixed<sg14::fixed_point<RhsRep, RhsExponent>, Lhs>::type;
76  };
77 
78  // std::common_type<fixed_point<>, fixed_point<>>
79  template<class LhsRep, int LhsExponent, class RhsRep, int RhsExponent>
80  struct common_type<sg14::fixed_point<LhsRep, LhsExponent>, sg14::fixed_point<RhsRep, RhsExponent>> {
81  using type = sg14::fixed_point<sg14::_impl::common_type_t<LhsRep, RhsRep>, sg14::_impl::min(LhsExponent, RhsExponent)>;
82  };
83 }
84 
85 #endif // SG14_FIXED_POINT_COMMON_TYPE_H
STL namespace.
literal real number approximation that uses fixed-point arithmetic
Definition: fixed_point_type.h:20
study group 14 of the C++ working group
Definition: const_integer.h:22