Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / atomic / detail / storage_type.hpp
1 /*
2  * Distributed under the Boost Software License, Version 1.0.
3  * (See accompanying file LICENSE_1_0.txt or copy at
4  * http://www.boost.org/LICENSE_1_0.txt)
5  *
6  * Copyright (c) 2009 Helge Bahmann
7  * Copyright (c) 2012 Tim Blechmann
8  * Copyright (c) 2013 - 2014 Andrey Semashev
9  */
10 /*!
11  * \file   atomic/detail/storage_type.hpp
12  *
13  * This header defines underlying types used as storage
14  */
15
16 #ifndef BOOST_ATOMIC_DETAIL_STORAGE_TYPE_HPP_INCLUDED_
17 #define BOOST_ATOMIC_DETAIL_STORAGE_TYPE_HPP_INCLUDED_
18
19 #include <cstring>
20 #include <boost/cstdint.hpp>
21 #include <boost/atomic/detail/config.hpp>
22
23 #ifdef BOOST_HAS_PRAGMA_ONCE
24 #pragma once
25 #endif
26
27 namespace boost {
28 namespace atomics {
29 namespace detail {
30
31 template< unsigned int Size >
32 struct buffer_storage
33 {
34     unsigned char data[Size];
35
36     BOOST_FORCEINLINE bool operator! () const BOOST_NOEXCEPT
37     {
38         bool result = true;
39         for (unsigned int i = 0; i < Size && result; ++i)
40         {
41             result &= data[i] == 0;
42         }
43         return result;
44     }
45
46     BOOST_FORCEINLINE bool operator== (buffer_storage const& that) const BOOST_NOEXCEPT
47     {
48         return std::memcmp(data, that.data, Size) == 0;
49     }
50
51     BOOST_FORCEINLINE bool operator!= (buffer_storage const& that) const BOOST_NOEXCEPT
52     {
53         return std::memcmp(data, that.data, Size) != 0;
54     }
55 };
56
57 template< unsigned int Size, bool Signed >
58 struct make_storage_type
59 {
60     typedef buffer_storage< Size > type;
61 };
62
63 template< >
64 struct make_storage_type< 1u, false >
65 {
66     typedef boost::uint8_t type;
67 };
68
69 template< >
70 struct make_storage_type< 1u, true >
71 {
72     typedef boost::int8_t type;
73 };
74
75 template< >
76 struct make_storage_type< 2u, false >
77 {
78     typedef boost::uint16_t type;
79 };
80
81 template< >
82 struct make_storage_type< 2u, true >
83 {
84     typedef boost::int16_t type;
85 };
86
87 template< >
88 struct make_storage_type< 4u, false >
89 {
90     typedef boost::uint32_t type;
91 };
92
93 template< >
94 struct make_storage_type< 4u, true >
95 {
96     typedef boost::int32_t type;
97 };
98
99 template< >
100 struct make_storage_type< 8u, false >
101 {
102     typedef boost::uint64_t type;
103 };
104
105 template< >
106 struct make_storage_type< 8u, true >
107 {
108     typedef boost::int64_t type;
109 };
110
111 #if defined(BOOST_HAS_INT128)
112
113 template< >
114 struct make_storage_type< 16u, false >
115 {
116     typedef boost::uint128_type type;
117 };
118
119 template< >
120 struct make_storage_type< 16u, true >
121 {
122     typedef boost::int128_type type;
123 };
124
125 #elif !defined(BOOST_NO_ALIGNMENT)
126
127 struct BOOST_ALIGNMENT(16) storage128_t
128 {
129     boost::uint64_t data[2];
130
131     BOOST_FORCEINLINE bool operator! () const BOOST_NOEXCEPT
132     {
133         return data[0] == 0 && data[1] == 0;
134     }
135 };
136
137 BOOST_FORCEINLINE bool operator== (storage128_t const& left, storage128_t const& right) BOOST_NOEXCEPT
138 {
139     return left.data[0] == right.data[0] && left.data[1] == right.data[1];
140 }
141 BOOST_FORCEINLINE bool operator!= (storage128_t const& left, storage128_t const& right) BOOST_NOEXCEPT
142 {
143     return !(left == right);
144 }
145
146 template< bool Signed >
147 struct make_storage_type< 16u, Signed >
148 {
149     typedef storage128_t type;
150 };
151
152 #endif
153
154 template< typename T >
155 struct storage_size_of
156 {
157     enum _
158     {
159         size = sizeof(T),
160         value = (size == 3 ? 4 : (size >= 5 && size <= 7 ? 8 : (size >= 9 && size <= 15 ? 16 : size)))
161     };
162 };
163
164 } // namespace detail
165 } // namespace atomics
166 } // namespace boost
167
168 #endif // BOOST_ATOMIC_DETAIL_STORAGE_TYPE_HPP_INCLUDED_