Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / endian / arithmetic.hpp
1 //  boost/endian/arithmetic.hpp  -------------------------------------------------------//
2
3 //  (C) Copyright Darin Adler 2000
4 //  (C) Copyright Beman Dawes 2006, 2009, 2014
5 //  (C) Copyright Peter Dimov 2019
6
7 //  Distributed under the Boost Software License, Version 1.0.
8 //  See http://www.boost.org/LICENSE_1_0.txt
9
10 //  See library home page at http://www.boost.org/libs/endian
11
12 //--------------------------------------------------------------------------------------//
13
14 //  Original design developed by Darin Adler based on classes developed by Mark
15 //  Borgerding. Four original class templates were combined into a single endian
16 //  class template by Beman Dawes, who also added the unrolled_byte_loops sign
17 //  partial specialization to correctly extend the sign when cover integer size
18 //  differs from endian representation size.
19
20 // TODO: When a compiler supporting constexpr becomes available, try possible uses.
21
22 #ifndef BOOST_ENDIAN_ARITHMETIC_HPP
23 #define BOOST_ENDIAN_ARITHMETIC_HPP
24
25 #if defined(_MSC_VER)
26 # pragma warning(push)
27 # pragma warning(disable:4365)  // conversion ... signed/unsigned mismatch
28 #endif
29
30 #include <boost/endian/buffers.hpp>
31 #include <boost/core/scoped_enum.hpp>
32 #include <boost/predef/other/endian.h>
33 #include <boost/static_assert.hpp>
34 #include <boost/cstdint.hpp>
35 #include <boost/config.hpp>
36 #include <boost/config/workaround.hpp>
37 #include <iosfwd>
38 #include <climits>
39
40 #if defined(__BORLANDC__) || defined( __CODEGEARC__)
41 # pragma pack(push, 1)
42 #endif
43
44 # if CHAR_BIT != 8
45 #   error Platforms with CHAR_BIT != 8 are not supported
46 # endif
47
48 # ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
49 #   define BOOST_ENDIAN_DEFAULT_CONSTRUCT {}          // C++03
50 # else
51 #   define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default;  // C++0x
52 # endif
53
54 // g++ pre-4.6 does not support unrestricted unions, but we have no Config macro for that
55 # if (defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || BOOST_WORKAROUND(BOOST_GCC, < 40600)) && defined(BOOST_ENDIAN_FORCE_PODNESS)
56 #   define BOOST_ENDIAN_NO_CTORS
57 # endif
58
59 # ifndef BOOST_ENDIAN_EXPLICIT_CTORS
60 #   define BOOST_ENDIAN_EXPLICIT_OPT
61 # else
62 #   define BOOST_ENDIAN_EXPLICIT_OPT explicit
63 # endif
64
65 //----------------------------------  synopsis  ----------------------------------------//
66
67 namespace boost
68 {
69 namespace endian
70 {
71
72   template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
73     BOOST_SCOPED_ENUM(align) Align = align::no>
74       class endian_arithmetic;
75
76   // big endian signed integer aligned types
77   typedef endian_arithmetic<order::big, int8_t, 8, align::yes>        big_int8_at;
78   typedef endian_arithmetic<order::big, int16_t, 16, align::yes>      big_int16_at;
79   typedef endian_arithmetic<order::big, int32_t, 32, align::yes>      big_int32_at;
80   typedef endian_arithmetic<order::big, int64_t, 64, align::yes>      big_int64_at;
81
82   // big endian unsigned integer aligned types
83   typedef endian_arithmetic<order::big, uint8_t, 8, align::yes>       big_uint8_at;
84   typedef endian_arithmetic<order::big, uint16_t, 16, align::yes>     big_uint16_at;
85   typedef endian_arithmetic<order::big, uint32_t, 32, align::yes>     big_uint32_at;
86   typedef endian_arithmetic<order::big, uint64_t, 64, align::yes>     big_uint64_at;
87
88   // little endian signed integer aligned types
89   typedef endian_arithmetic<order::little, int8_t, 8, align::yes>     little_int8_at;
90   typedef endian_arithmetic<order::little, int16_t, 16, align::yes>   little_int16_at;
91   typedef endian_arithmetic<order::little, int32_t, 32, align::yes>   little_int32_at;
92   typedef endian_arithmetic<order::little, int64_t, 64, align::yes>   little_int64_at;
93
94   // little endian unsigned integer aligned types
95   typedef endian_arithmetic<order::little, uint8_t, 8, align::yes>    little_uint8_at;
96   typedef endian_arithmetic<order::little, uint16_t, 16, align::yes>  little_uint16_at;
97   typedef endian_arithmetic<order::little, uint32_t, 32, align::yes>  little_uint32_at;
98   typedef endian_arithmetic<order::little, uint64_t, 64, align::yes>  little_uint64_at;
99
100   // aligned floating point types
101   typedef endian_arithmetic<order::big, float, 32, align::yes>        big_float32_at;
102   typedef endian_arithmetic<order::big, double, 64, align::yes>       big_float64_at;
103   typedef endian_arithmetic<order::little, float, 32, align::yes>     little_float32_at;
104   typedef endian_arithmetic<order::little, double, 64, align::yes>    little_float64_at;
105
106   // aligned native endian typedefs are not provided because
107   // <cstdint> types are superior for this use case
108
109   // big endian signed integer unaligned types
110   typedef endian_arithmetic<order::big, int_least8_t, 8>        big_int8_t;
111   typedef endian_arithmetic<order::big, int_least16_t, 16>      big_int16_t;
112   typedef endian_arithmetic<order::big, int_least32_t, 24>      big_int24_t;
113   typedef endian_arithmetic<order::big, int_least32_t, 32>      big_int32_t;
114   typedef endian_arithmetic<order::big, int_least64_t, 40>      big_int40_t;
115   typedef endian_arithmetic<order::big, int_least64_t, 48>      big_int48_t;
116   typedef endian_arithmetic<order::big, int_least64_t, 56>      big_int56_t;
117   typedef endian_arithmetic<order::big, int_least64_t, 64>      big_int64_t;
118
119   // big endian unsigned integer unaligned types
120   typedef endian_arithmetic<order::big, uint_least8_t, 8>       big_uint8_t;
121   typedef endian_arithmetic<order::big, uint_least16_t, 16>     big_uint16_t;
122   typedef endian_arithmetic<order::big, uint_least32_t, 24>     big_uint24_t;
123   typedef endian_arithmetic<order::big, uint_least32_t, 32>     big_uint32_t;
124   typedef endian_arithmetic<order::big, uint_least64_t, 40>     big_uint40_t;
125   typedef endian_arithmetic<order::big, uint_least64_t, 48>     big_uint48_t;
126   typedef endian_arithmetic<order::big, uint_least64_t, 56>     big_uint56_t;
127   typedef endian_arithmetic<order::big, uint_least64_t, 64>     big_uint64_t;
128
129   // little endian signed integer unaligned types
130   typedef endian_arithmetic<order::little, int_least8_t, 8>     little_int8_t;
131   typedef endian_arithmetic<order::little, int_least16_t, 16>   little_int16_t;
132   typedef endian_arithmetic<order::little, int_least32_t, 24>   little_int24_t;
133   typedef endian_arithmetic<order::little, int_least32_t, 32>   little_int32_t;
134   typedef endian_arithmetic<order::little, int_least64_t, 40>   little_int40_t;
135   typedef endian_arithmetic<order::little, int_least64_t, 48>   little_int48_t;
136   typedef endian_arithmetic<order::little, int_least64_t, 56>   little_int56_t;
137   typedef endian_arithmetic<order::little, int_least64_t, 64>   little_int64_t;
138
139   // little endian unsigned integer unaligned types
140   typedef endian_arithmetic<order::little, uint_least8_t, 8>    little_uint8_t;
141   typedef endian_arithmetic<order::little, uint_least16_t, 16>  little_uint16_t;
142   typedef endian_arithmetic<order::little, uint_least32_t, 24>  little_uint24_t;
143   typedef endian_arithmetic<order::little, uint_least32_t, 32>  little_uint32_t;
144   typedef endian_arithmetic<order::little, uint_least64_t, 40>  little_uint40_t;
145   typedef endian_arithmetic<order::little, uint_least64_t, 48>  little_uint48_t;
146   typedef endian_arithmetic<order::little, uint_least64_t, 56>  little_uint56_t;
147   typedef endian_arithmetic<order::little, uint_least64_t, 64>  little_uint64_t;
148
149 # if BOOST_ENDIAN_BIG_BYTE
150   // native endian signed integer unaligned types
151   typedef big_int8_t   native_int8_t;
152   typedef big_int16_t  native_int16_t;
153   typedef big_int24_t  native_int24_t;
154   typedef big_int32_t  native_int32_t;
155   typedef big_int40_t  native_int40_t;
156   typedef big_int48_t  native_int48_t;
157   typedef big_int56_t  native_int56_t;
158   typedef big_int64_t  native_int64_t;
159
160   // native endian unsigned integer unaligned types
161   typedef big_uint8_t   native_uint8_t;
162   typedef big_uint16_t  native_uint16_t;
163   typedef big_uint24_t  native_uint24_t;
164   typedef big_uint32_t  native_uint32_t;
165   typedef big_uint40_t  native_uint40_t;
166   typedef big_uint48_t  native_uint48_t;
167   typedef big_uint56_t  native_uint56_t;
168   typedef big_uint64_t  native_uint64_t;
169 # else
170   // native endian signed integer unaligned types
171   typedef little_int8_t   native_int8_t;
172   typedef little_int16_t  native_int16_t;
173   typedef little_int24_t  native_int24_t;
174   typedef little_int32_t  native_int32_t;
175   typedef little_int40_t  native_int40_t;
176   typedef little_int48_t  native_int48_t;
177   typedef little_int56_t  native_int56_t;
178   typedef little_int64_t  native_int64_t;
179
180   // native endian unsigned integer unaligned types
181   typedef little_uint8_t   native_uint8_t;
182   typedef little_uint16_t  native_uint16_t;
183   typedef little_uint24_t  native_uint24_t;
184   typedef little_uint32_t  native_uint32_t;
185   typedef little_uint40_t  native_uint40_t;
186   typedef little_uint48_t  native_uint48_t;
187   typedef little_uint56_t  native_uint56_t;
188   typedef little_uint64_t  native_uint64_t;
189 # endif
190
191   // unaligned floating point types
192   typedef endian_arithmetic<order::big, float, 32, align::no>        big_float32_t;
193   typedef endian_arithmetic<order::big, double, 64, align::no>       big_float64_t;
194   typedef endian_arithmetic<order::little, float, 32, align::no>     little_float32_t;
195   typedef endian_arithmetic<order::little, double, 64, align::no>    little_float64_t;
196   typedef endian_arithmetic<order::native, float, 32, align::no>     native_float32_t;
197   typedef endian_arithmetic<order::native, double, 64, align::no>    native_float64_t;
198
199 //----------------------------------  end synopsis  ------------------------------------//
200
201 template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
202     BOOST_SCOPED_ENUM(align) Align>
203 class endian_arithmetic:
204     public endian_buffer<Order, T, n_bits, Align>
205 {
206 private:
207
208     typedef endian_buffer<Order, T, n_bits, Align> inherited;
209
210 public:
211
212     typedef T value_type;
213
214 #ifndef BOOST_ENDIAN_NO_CTORS
215
216     endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
217
218     BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic( T val ) BOOST_NOEXCEPT: inherited( val )
219     {
220     }
221
222 #endif
223
224     endian_arithmetic& operator=( T val ) BOOST_NOEXCEPT
225     {
226         inherited::operator=( val );
227         return *this;
228     }
229
230     operator value_type() const BOOST_NOEXCEPT
231     {
232         return this->value();
233     }
234
235     // operators
236
237     T operator+() const BOOST_NOEXCEPT
238     {
239         return this->value();
240     }
241
242     endian_arithmetic& operator+=( T y ) BOOST_NOEXCEPT
243     {
244         *this = static_cast<T>( this->value() + y );
245         return *this;
246     }
247
248     endian_arithmetic& operator-=( T y ) BOOST_NOEXCEPT
249     {
250         *this = static_cast<T>( this->value() - y );
251         return *this;
252     }
253
254     endian_arithmetic& operator*=( T y ) BOOST_NOEXCEPT
255     {
256         *this = static_cast<T>( this->value() * y );
257         return *this;
258     }
259
260     endian_arithmetic& operator/=( T y ) BOOST_NOEXCEPT
261     {
262         *this = static_cast<T>( this->value() / y );
263         return *this;
264     }
265
266     endian_arithmetic& operator%=( T y ) BOOST_NOEXCEPT
267     {
268         *this = static_cast<T>( this->value() % y );
269         return *this;
270     }
271
272     endian_arithmetic& operator&=( T y ) BOOST_NOEXCEPT
273     {
274         *this = static_cast<T>( this->value() & y );
275         return *this;
276     }
277
278     endian_arithmetic& operator|=( T y ) BOOST_NOEXCEPT
279     {
280         *this = static_cast<T>( this->value() | y );
281         return *this;
282     }
283
284     endian_arithmetic& operator^=( T y ) BOOST_NOEXCEPT
285     {
286         *this = static_cast<T>( this->value() ^ y );
287         return *this;
288     }
289
290     endian_arithmetic& operator<<=( T y ) BOOST_NOEXCEPT
291     {
292         *this = static_cast<T>( this->value() << y );
293         return *this;
294     }
295
296     endian_arithmetic& operator>>=( T y ) BOOST_NOEXCEPT
297     {
298         *this = static_cast<T>( this->value() >> y );
299         return *this;
300     }
301
302     endian_arithmetic& operator++() BOOST_NOEXCEPT
303     {
304         *this += 1;
305         return *this;
306     }
307
308     endian_arithmetic& operator--() BOOST_NOEXCEPT
309     {
310         *this -= 1;
311         return *this;
312     }
313
314     endian_arithmetic operator++(int) BOOST_NOEXCEPT
315     {
316         endian_arithmetic tmp( *this );
317         *this += 1;
318         return tmp;
319     }
320
321     endian_arithmetic operator--(int) BOOST_NOEXCEPT
322     {
323         endian_arithmetic tmp( *this );
324         *this -= 1;
325         return tmp;
326     }
327
328     template<class Ch, class Tr>
329     friend std::basic_ostream<Ch, Tr>&
330     operator<<( std::basic_ostream<Ch, Tr>& os, endian_arithmetic const& x )
331     {
332         return os << x.value();
333     }
334
335     template<class Ch, class Tr>
336     friend std::basic_istream<Ch, Tr>&
337     operator>>( std::basic_istream<Ch, Tr>& is, endian_arithmetic& x )
338     {
339         T i;
340
341         if( is >> i )
342         {
343             x = i;
344         }
345
346         return is;
347     }
348 };
349
350 } // namespace endian
351 } // namespace boost
352
353 #if defined(__BORLANDC__) || defined( __CODEGEARC__)
354 # pragma pack(pop)
355 #endif
356
357 #if defined(_MSC_VER)
358 # pragma warning(pop)
359 #endif
360
361 #endif // BOOST_ENDIAN_ARITHMETIC_HPP