From Jason Turner

[bit]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpo0bbyz2a/{from.md → to.md} +252 -0
tmp/tmpo0bbyz2a/{from.md → to.md} RENAMED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Bit manipulation <a id="bit">[[bit]]</a>
2
+
3
+ ### General <a id="bit.general">[[bit.general]]</a>
4
+
5
+ The header `<bit>` provides components to access, manipulate and process
6
+ both individual bits and bit sequences.
7
+
8
+ ### Header `<bit>` synopsis <a id="bit.syn">[[bit.syn]]</a>
9
+
10
+ ``` cpp
11
+ namespace std {
12
+ // [bit.cast], bit_cast
13
+ template<class To, class From>
14
+ constexpr To bit_cast(const From& from) noexcept;
15
+
16
+ // [bit.pow.two], integral powers of 2
17
+ template<class T>
18
+ constexpr bool has_single_bit(T x) noexcept;
19
+ template<class T>
20
+ constexpr T bit_ceil(T x);
21
+ template<class T>
22
+ constexpr T bit_floor(T x) noexcept;
23
+ template<class T>
24
+ constexpr T bit_width(T x) noexcept;
25
+
26
+ // [bit.rotate], rotating
27
+ template<class T>
28
+ [[nodiscard]] constexpr T rotl(T x, int s) noexcept;
29
+ template<class T>
30
+ [[nodiscard]] constexpr T rotr(T x, int s) noexcept;
31
+
32
+ // [bit.count], counting
33
+ template<class T>
34
+ constexpr int countl_zero(T x) noexcept;
35
+ template<class T>
36
+ constexpr int countl_one(T x) noexcept;
37
+ template<class T>
38
+ constexpr int countr_zero(T x) noexcept;
39
+ template<class T>
40
+ constexpr int countr_one(T x) noexcept;
41
+ template<class T>
42
+ constexpr int popcount(T x) noexcept;
43
+
44
+ // [bit.endian], endian
45
+ enum class endian {
46
+ little = see below,
47
+ big = see below,
48
+ native = see below
49
+ };
50
+ }
51
+ ```
52
+
53
+ ### Function template `bit_cast` <a id="bit.cast">[[bit.cast]]</a>
54
+
55
+ ``` cpp
56
+ template<class To, class From>
57
+ constexpr To bit_cast(const From& from) noexcept;
58
+ ```
59
+
60
+ *Constraints:*
61
+
62
+ - `sizeof(To) == sizeof(From)` is `true`;
63
+ - `is_trivially_copyable_v<To>` is `true`; and
64
+ - `is_trivially_copyable_v<From>` is `true`.
65
+
66
+ *Returns:* An object of type `To`. Implicitly creates objects nested
67
+ within the result [[intro.object]]. Each bit of the value representation
68
+ of the result is equal to the corresponding bit in the object
69
+ representation of `from`. Padding bits of the result are unspecified.
70
+ For the result and each object created within it, if there is no value
71
+ of the object’s type corresponding to the value representation produced,
72
+ the behavior is undefined. If there are multiple such values, which
73
+ value is produced is unspecified.
74
+
75
+ *Remarks:* This function is `constexpr` if and only if `To`, `From`, and
76
+ the types of all subobjects of `To` and `From` are types `T` such that:
77
+
78
+ - `is_union_v<T>` is `false`;
79
+ - `is_pointer_v<T>` is `false`;
80
+ - `is_member_pointer_v<T>` is `false`;
81
+ - `is_volatile_v<T>` is `false`; and
82
+ - `T` has no non-static data members of reference type.
83
+
84
+ ### Integral powers of 2 <a id="bit.pow.two">[[bit.pow.two]]</a>
85
+
86
+ ``` cpp
87
+ template<class T>
88
+ constexpr bool has_single_bit(T x) noexcept;
89
+ ```
90
+
91
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
92
+
93
+ *Returns:* `true` if `x` is an integral power of two; `false` otherwise.
94
+
95
+ ``` cpp
96
+ template<class T>
97
+ constexpr T bit_ceil(T x);
98
+ ```
99
+
100
+ Let N be the smallest power of 2 greater than or equal to `x`.
101
+
102
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
103
+
104
+ *Preconditions:* N is representable as a value of type `T`.
105
+
106
+ *Returns:* N.
107
+
108
+ *Throws:* Nothing.
109
+
110
+ *Remarks:* A function call expression that violates the precondition in
111
+ the *Preconditions:* element is not a core constant
112
+ expression [[expr.const]].
113
+
114
+ ``` cpp
115
+ template<class T>
116
+ constexpr T bit_floor(T x) noexcept;
117
+ ```
118
+
119
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
120
+
121
+ *Returns:* If `x == 0`, `0`; otherwise the maximal value `y` such that
122
+ `has_single_bit(y)` is `true` and `y <= x`.
123
+
124
+ ``` cpp
125
+ template<class T>
126
+ constexpr T bit_width(T x) noexcept;
127
+ ```
128
+
129
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
130
+
131
+ *Returns:* If `x == 0`, `0`; otherwise one plus the base-2 logarithm of
132
+ `x`, with any fractional part discarded.
133
+
134
+ ### Rotating <a id="bit.rotate">[[bit.rotate]]</a>
135
+
136
+ In the following descriptions, let `N` denote
137
+ `numeric_limits<T>::digits`.
138
+
139
+ ``` cpp
140
+ template<class T>
141
+ [[nodiscard]] constexpr T rotl(T x, int s) noexcept;
142
+ ```
143
+
144
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
145
+
146
+ Let `r` be `s % N`.
147
+
148
+ *Returns:* If `r` is `0`, `x`; if `r` is positive,
149
+ `(x << r) | (x >> (N - r))`; if `r` is negative, `rotr(x, -r)`.
150
+
151
+ ``` cpp
152
+ template<class T>
153
+ [[nodiscard]] constexpr T rotr(T x, int s) noexcept;
154
+ ```
155
+
156
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
157
+
158
+ Let `r` be `s % N`.
159
+
160
+ *Returns:* If `r` is `0`, `x`; if `r` is positive,
161
+ `(x >> r) | (x << (N - r))`; if `r` is negative, `rotl(x, -r)`.
162
+
163
+ ### Counting <a id="bit.count">[[bit.count]]</a>
164
+
165
+ In the following descriptions, let `N` denote
166
+ `numeric_limits<T>::digits`.
167
+
168
+ ``` cpp
169
+ template<class T>
170
+ constexpr int countl_zero(T x) noexcept;
171
+ ```
172
+
173
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
174
+
175
+ *Returns:* The number of consecutive `0` bits in the value of `x`,
176
+ starting from the most significant bit.
177
+
178
+ [*Note 1*: Returns `N` if `x == 0`. — *end note*]
179
+
180
+ ``` cpp
181
+ template<class T>
182
+ constexpr int countl_one(T x) noexcept;
183
+ ```
184
+
185
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
186
+
187
+ *Returns:* The number of consecutive `1` bits in the value of `x`,
188
+ starting from the most significant bit.
189
+
190
+ [*Note 2*: Returns `N` if
191
+ `x == numeric_limits<T>::max()`. — *end note*]
192
+
193
+ ``` cpp
194
+ template<class T>
195
+ constexpr int countr_zero(T x) noexcept;
196
+ ```
197
+
198
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
199
+
200
+ *Returns:* The number of consecutive `0` bits in the value of `x`,
201
+ starting from the least significant bit.
202
+
203
+ [*Note 3*: Returns `N` if `x == 0`. — *end note*]
204
+
205
+ ``` cpp
206
+ template<class T>
207
+ constexpr int countr_one(T x) noexcept;
208
+ ```
209
+
210
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
211
+
212
+ *Returns:* The number of consecutive `1` bits in the value of `x`,
213
+ starting from the least significant bit.
214
+
215
+ [*Note 4*: Returns `N` if
216
+ `x == numeric_limits<T>::max()`. — *end note*]
217
+
218
+ ``` cpp
219
+ template<class T>
220
+ constexpr int popcount(T x) noexcept;
221
+ ```
222
+
223
+ *Constraints:* `T` is an unsigned integer type [[basic.fundamental]].
224
+
225
+ *Returns:* The number of `1` bits in the value of `x`.
226
+
227
+ ### Endian <a id="bit.endian">[[bit.endian]]</a>
228
+
229
+ Two common methods of byte ordering in multibyte scalar types are
230
+ big-endian and little-endian in the execution environment. Big-endian is
231
+ a format for storage of binary data in which the most significant byte
232
+ is placed first, with the rest in descending order. Little-endian is a
233
+ format for storage of binary data in which the least significant byte is
234
+ placed first, with the rest in ascending order. This subclause describes
235
+ the endianness of the scalar types of the execution environment.
236
+
237
+ ``` cpp
238
+ enum class endian {
239
+ little = see below,
240
+ big = see below,
241
+ native = see below
242
+ };
243
+ ```
244
+
245
+ If all scalar types have size 1 byte, then all of `endian::little`,
246
+ `endian::big`, and `endian::native` have the same value. Otherwise,
247
+ `endian::little` is not equal to `endian::big`. If all scalar types are
248
+ big-endian, `endian::native` is equal to `endian::big`. If all scalar
249
+ types are little-endian, `endian::native` is equal to `endian::little`.
250
+ Otherwise, `endian::native` is not equal to either `endian::big` or
251
+ `endian::little`.
252
+