fixed_point (deprecated)  rev.2
Binary Fixed-Point Arithmetic Library in C++
multiprecision.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_MULTIPRECISION_H)
11 #define SG14_MULTIPRECISION_H 1
12 
13 #include <sg14/num_traits.h>
14 
15 #include <boost/multiprecision/cpp_int.hpp>
16 
18 namespace sg14 {
19  namespace _bmp = boost::multiprecision;
20 
23  // type trait specializations of boost::multiprecision types
24  //
25  // These are the definitions needed to use any custom integer type with
26  // sg14::fixed_point
27 
28  template<unsigned NumBits, _bmp::cpp_integer_type SignType, _bmp::cpp_int_check_type Checked, class Allocator>
29  struct make_signed<_bmp::cpp_int_backend<NumBits, NumBits, SignType, Checked, Allocator>> {
30  using type = _bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude, Checked, Allocator>;
31  };
32 
33  template<unsigned NumBits, _bmp::cpp_integer_type SignType, _bmp::cpp_int_check_type Checked, class Allocator>
34  struct make_unsigned<_bmp::cpp_int_backend<NumBits, NumBits, SignType, Checked, Allocator>> {
35  using type = _bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude, Checked, Allocator>;
36  };
37 
38  template<unsigned NumBits, _bmp::cpp_integer_type SignType, _bmp::cpp_int_check_type Checked, class Allocator>
39  struct digits<_bmp::cpp_int_backend<NumBits, NumBits, SignType, Checked, Allocator>>
40  : std::integral_constant<_digits_type, NumBits> {
41  };
42 
43  template<unsigned NumBits, _bmp::cpp_integer_type SignType, _bmp::cpp_int_check_type Checked, class Allocator, _digits_type MinNumDigits>
44  struct set_digits<_bmp::cpp_int_backend<NumBits, NumBits, SignType, Checked, Allocator>, MinNumDigits> {
45  static constexpr unsigned width = MinNumDigits + (SignType == _bmp::signed_magnitude);
46  using type = _bmp::cpp_int_backend<width, width, SignType, Checked, Allocator>;
47  };
48 
49  template<class Backend, _bmp::expression_template_option ExpressionTemplates>
50  struct make_signed<_bmp::number<Backend, ExpressionTemplates>> {
51  using type = _bmp::number<make_signed_t<Backend>, ExpressionTemplates>;
52  };
53 
54  template<class Backend, _bmp::expression_template_option ExpressionTemplates>
55  struct make_unsigned<_bmp::number<Backend, ExpressionTemplates>> {
56  using type = _bmp::number<make_unsigned_t<Backend>, ExpressionTemplates>;
57  };
58 
59  template<class Backend, _bmp::expression_template_option ExpressionTemplates>
60  struct digits<_bmp::number<Backend, ExpressionTemplates>>
61  : digits<Backend> {
62  };
63 
64  template<class Backend, _bmp::expression_template_option ExpressionTemplates, _digits_type MinNumDigits>
65  struct set_digits<_bmp::number<Backend, ExpressionTemplates>, MinNumDigits> {
66  using type = _bmp::number<set_digits_t<Backend, MinNumDigits>, ExpressionTemplates>;
67  };
68 
71  // aliases of _bmp types
72 
73  namespace _sized_integer_impl {
74  template<unsigned NumBits, _bmp::cpp_integer_type SignType>
75  using backend = _bmp::cpp_int_backend<
76  NumBits, NumBits, SignType, _bmp::unchecked, void>;
77  }
78 
79  // sg14::signed_multiprecision - a signed integer of arbitrary size
80  template<unsigned NumDigits = digits<int>::value>
81  using signed_multiprecision = _bmp::number<_sized_integer_impl::backend<NumDigits+1, _bmp::signed_magnitude>, _bmp::et_off>;
82 
83  // sg14::unsigned_multiprecision - an unsigned integer of arbitrary size
84  template<unsigned NumDigits = digits<unsigned>::value>
85  using unsigned_multiprecision = _bmp::number<_sized_integer_impl::backend<NumDigits, _bmp::unsigned_magnitude>, _bmp::et_off>;
86 
87  // sg14::unsigned_multiprecision - an integer of arbitrary size
88  template<unsigned NumDigits = digits<int>::value>
89  using multiprecision = signed_multiprecision<NumDigits+1>;
90 }
91 
92 #endif // SG14_MULTIPRECISION_H
study group 14 of the C++ working group
Definition: const_integer.h:22