-
Notifications
You must be signed in to change notification settings - Fork 62
/
boost.multiprecision.h
346 lines (291 loc) · 12.2 KB
/
boost.multiprecision.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright John McFarlane 2015 - 2016.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file ../../LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
/// \file
/// \brief definitions and specializations that adapt Boost.Multiprecision for use with \ref cnl::scaled_integer
#if !defined(CNL_BOOST_MULTIPRECISION_H)
#define CNL_BOOST_MULTIPRECISION_H
#include "../_impl/numbers/set_signedness.h"
#include "../_impl/numbers/signedness.h"
#include "../_impl/scaled/power.h"
#include "../constant.h"
#include "../num_traits.h"
#include "../rounding_integer.h"
#include <boost/multiprecision/cpp_int.hpp>
#include <type_traits>
/// compositional numeric library
namespace cnl {
namespace _bmp = boost::multiprecision;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// generic operators of boost::multiprecision::number
namespace _impl {
template<typename T>
struct is_bmp_number : std::false_type {
};
template<class Backend>
struct is_bmp_number<_bmp::number<Backend>> : std::true_type {
};
}
template<class Backend>
struct is_integer<_bmp::number<Backend>> : std::true_type {
};
template<
unsigned MinBits,
unsigned MaxBits,
_bmp::cpp_integer_type SignType,
_bmp::cpp_int_check_type Checked,
class Allocator>
struct is_integer<_bmp::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>>
: std::true_type {
};
template<
int DestExponent, int SrcExponent, int Radix, unsigned NumBits,
_bmp::cpp_integer_type SignType>
struct custom_operator<
_impl::convert_op,
op_value<_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, SignType>>, cnl::power<SrcExponent, Radix>>,
op_value<_bmp::cpp_int_backend<NumBits, NumBits, SignType>, cnl::power<DestExponent, Radix>>> {
[[nodiscard]] constexpr auto operator()(
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, SignType>> const& input) const
{
// when converting *from* scaled_integer
return input.backend();
}
};
/// \cond
template<
int DestExponent, int SrcExponent, int Radix, unsigned NumBits,
_bmp::cpp_integer_type SignType, typename Input>
requires(!_impl::is_bmp_number<Input>::value) struct custom_operator<
_impl::convert_op,
op_value<Input, cnl::power<SrcExponent, Radix>>,
op_value<_bmp::cpp_int_backend<NumBits, NumBits, SignType>, cnl::power<DestExponent, Radix>>> {
[[nodiscard]] constexpr auto operator()(Input const& input) const
-> _bmp::cpp_int_backend<NumBits, NumBits, SignType>
{
return input;
}
};
/// \endcond
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// type trait specializations of boost::multiprecision::number
namespace _impl {
template<typename T>
struct is_boost_multiprecision : std::false_type {
};
template<typename Backend>
struct is_boost_multiprecision<_bmp::number<Backend>> : std::true_type {
};
}
}
/// compositional numeric library, numbers header/namespace
namespace cnl::numbers {
template<unsigned NumBits>
struct signedness<_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude>>>
: std::true_type {
};
template<unsigned NumBits>
struct signedness<
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude>>>
: std::false_type {
};
template<unsigned NumBits>
struct set_signedness<
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude>>,
true> {
using type = _bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude>>;
};
template<unsigned NumBits>
struct set_signedness<
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude>>,
true> {
using type = _bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude>>;
};
template<unsigned NumBits>
struct set_signedness<
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude>>,
false> {
using type =
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude>>;
};
template<unsigned NumBits>
struct set_signedness<
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude>>,
false> {
using type =
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude>>;
};
}
namespace cnl {
template<unsigned NumBits>
inline constexpr int
digits_v<_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude>>> = NumBits - 1;
template<unsigned NumBits>
inline constexpr int
digits_v<_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude>>> = NumBits;
template<unsigned NumBits, int MinNumDigits>
struct set_digits<
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude>>,
MinNumDigits> {
using type = _bmp::number<
_bmp::cpp_int_backend<MinNumDigits, MinNumDigits, _bmp::unsigned_magnitude>>;
};
template<unsigned NumBits, int MinNumDigits>
struct set_digits<
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude>>,
MinNumDigits> {
using type = _bmp::number<
_bmp::cpp_int_backend<MinNumDigits + 1, MinNumDigits + 1, _bmp::signed_magnitude>>;
};
template<unsigned NumBits>
struct to_rep<_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude>>> {
private:
using number_type =
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude>>;
public:
[[nodiscard]] constexpr auto& operator()(number_type& n) const
{
return n;
};
[[nodiscard]] constexpr auto const& operator()(number_type const& n) const
{
return n;
};
[[nodiscard]] constexpr auto operator()(number_type&& n) const
{
return n;
};
};
template<unsigned NumBits>
struct to_rep<_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude>>> {
private:
using number_type =
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude>>;
public:
[[nodiscard]] constexpr auto& operator()(number_type& n) const
{
return n;
};
[[nodiscard]] constexpr auto const& operator()(number_type const& n) const
{
return n;
};
[[nodiscard]] constexpr auto operator()(number_type&& n) const
{
return std::move(n);
};
};
/// \cond
template<unsigned NumBits, class Value>
requires(!_impl::is_boost_multiprecision<Value>::value) struct from_value<
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::signed_magnitude>>,
Value> {
private:
static constexpr auto _bits = digits_v<Value> + 1;
public:
[[nodiscard]] constexpr auto operator()(Value const& value) const
-> _bmp::number<_bmp::cpp_int_backend<_bits, _bits, _bmp::signed_magnitude>>
{
return value;
}
};
template<unsigned NumBits, class Value>
requires(!_impl::is_boost_multiprecision<Value>::value) struct from_value<
_bmp::number<_bmp::cpp_int_backend<NumBits, NumBits, _bmp::unsigned_magnitude>>,
Value> {
private:
static constexpr auto _bits = digits_v<Value>;
public:
[[nodiscard]] constexpr auto operator()(Value const& value) const
-> _bmp::number<_bmp::cpp_int_backend<_bits, _bits, _bmp::unsigned_magnitude>>
{
return value;
}
};
/// \endcond
template<class LhsBackend, class RhsBackend>
struct from_value<_bmp::number<LhsBackend>, _bmp::number<RhsBackend>> {
[[nodiscard]] constexpr auto operator()(_bmp::number<RhsBackend> const& value) const
-> _bmp::number<RhsBackend>
{
return value;
}
};
template<int Digits, int Radix, class Backend>
struct scale<Digits, Radix, _bmp::number<Backend>>
: _impl::default_scale<Digits, Radix, _bmp::number<Backend>> {
};
template<class Backend>
struct rounding<_bmp::number<Backend>> : std::type_identity<native_rounding_tag> {
};
template<class Backend>
struct rep_of<_bmp::number<Backend>> : std::type_identity<Backend> {
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// aliases of boost::multiprecision types
namespace _sized_integer_impl {
template<unsigned NumBits, _bmp::cpp_integer_type SignType>
using backend = _bmp::cpp_int_backend<NumBits, NumBits, SignType>;
template<unsigned NumBits, _bmp::cpp_integer_type SignType>
using number = _bmp::number<_sized_integer_impl::backend<NumBits, SignType>, _bmp::et_off>;
}
/// \brief alias to a signed, fixed-size Boost.Multiprecision type
/// suitable for specializing \ref scaled_integer
///
/// \tparam NumDigits number of digits constituting the integer value (excluding sign bit)
/// \sa unsigned_multiprecision, multiprecision
template<unsigned NumDigits = digits_v<int>>
using signed_multiprecision =
_sized_integer_impl::number<NumDigits + 1, _bmp::signed_magnitude>;
/// \brief alias to an unsigned, fixed-size Boost.Multiprecision type
/// suitable for specializing \ref scaled_integer
///
/// \tparam NumDigits number of digits constituting the integer value (excluding sign bit)
/// \sa signed_multiprecision, multiprecision
template<unsigned NumDigits = digits_v<unsigned>>
using unsigned_multiprecision =
_sized_integer_impl::number<NumDigits, _bmp::unsigned_magnitude>;
/// \brief alias to a signed, fixed-size Boost.Multiprecision type
/// suitable for specializing \ref scaled_integer
///
/// \tparam NumDigits number of digits constituting the integer value (excluding sign bit)
/// \sa signed_multiprecision, unsigned_multiprecision
template<unsigned NumDigits = digits_v<int>>
using multiprecision = signed_multiprecision<NumDigits>;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// boost::multiprecision bitwise shift operators
template<unsigned NumBits, CNL_IMPL_CONSTANT_VALUE_TYPE Value>
[[nodiscard]] constexpr auto operator<<(
_sized_integer_impl::number<NumBits, _bmp::signed_magnitude> const& lhs,
constant<Value>)
{
return lhs << Value;
}
template<unsigned NumBits, CNL_IMPL_CONSTANT_VALUE_TYPE Value>
[[nodiscard]] constexpr auto operator<<(
_sized_integer_impl::number<NumBits, _bmp::unsigned_magnitude> const& lhs,
constant<Value>)
{
return lhs << Value;
}
template<unsigned NumBits, CNL_IMPL_CONSTANT_VALUE_TYPE Value>
[[nodiscard]] constexpr auto operator>>(
_sized_integer_impl::number<NumBits, _bmp::unsigned_magnitude> const& lhs,
constant<Value>)
{
return lhs >> Value;
}
template<unsigned NumBits, CNL_IMPL_CONSTANT_VALUE_TYPE Value>
[[nodiscard]] constexpr auto operator>>(
_sized_integer_impl::number<NumBits, _bmp::signed_magnitude> const& lhs,
constant<Value>)
{
return lhs >> Value;
}
}
#endif // CNL_BOOST_MULTIPRECISION_H