Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2632261

Browse files
authoredJul 24, 2024
Rollup merge of rust-lang#127252 - fitzgen:edge-cases-for-bitwise-operations, r=m-ou-se
Add edge-case examples to `{count,leading,trailing}_{ones,zeros}` methods Some architectures (i386) do not define a "count leading zeros" instruction, they define a "find first set bit" instruction (`bsf`) whose result is undefined when given zero (ie none of the bits are set). Of this family of bitwise operations, I always forget which of these things is potentially undefined for zero, and I'm also not 100% sure that Rust provides a hard guarantee for the results of these methods when given zero. So I figured there are others who have these same uncertainties, and it would be good to resolve them and answer the question via extending these doc examples/tests. See https://en.wikipedia.org/wiki/Find_first_set#Hardware_support for more info on i386 and `bsf` on zero.
2 parents b0d7414 + 538fe81 commit 2632261

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed
 

‎core/src/num/uint_macros.rs

+35-6
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,13 @@ macro_rules! uint_impl {
6565
///
6666
/// ```
6767
#[doc = concat!("let n = 0b01001100", stringify!($SelfT), ";")]
68-
///
6968
/// assert_eq!(n.count_ones(), 3);
69+
///
70+
#[doc = concat!("let max = ", stringify!($SelfT),"::MAX;")]
71+
#[doc = concat!("assert_eq!(max.count_ones(), ", stringify!($BITS), ");")]
72+
///
73+
#[doc = concat!("let zero = 0", stringify!($SelfT), ";")]
74+
/// assert_eq!(zero.count_ones(), 0);
7075
/// ```
7176
#[stable(feature = "rust1", since = "1.0.0")]
7277
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
@@ -86,7 +91,11 @@ macro_rules! uint_impl {
8691
/// Basic usage:
8792
///
8893
/// ```
89-
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 0);")]
94+
#[doc = concat!("let zero = 0", stringify!($SelfT), ";")]
95+
#[doc = concat!("assert_eq!(zero.count_zeros(), ", stringify!($BITS), ");")]
96+
///
97+
#[doc = concat!("let max = ", stringify!($SelfT),"::MAX;")]
98+
/// assert_eq!(max.count_zeros(), 0);
9099
/// ```
91100
#[stable(feature = "rust1", since = "1.0.0")]
92101
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
@@ -108,8 +117,13 @@ macro_rules! uint_impl {
108117
///
109118
/// ```
110119
#[doc = concat!("let n = ", stringify!($SelfT), "::MAX >> 2;")]
111-
///
112120
/// assert_eq!(n.leading_zeros(), 2);
121+
///
122+
#[doc = concat!("let zero = 0", stringify!($SelfT), ";")]
123+
#[doc = concat!("assert_eq!(zero.leading_zeros(), ", stringify!($BITS), ");")]
124+
///
125+
#[doc = concat!("let max = ", stringify!($SelfT),"::MAX;")]
126+
/// assert_eq!(max.leading_zeros(), 0);
113127
/// ```
114128
#[doc = concat!("[`ilog2`]: ", stringify!($SelfT), "::ilog2")]
115129
#[stable(feature = "rust1", since = "1.0.0")]
@@ -130,8 +144,13 @@ macro_rules! uint_impl {
130144
///
131145
/// ```
132146
#[doc = concat!("let n = 0b0101000", stringify!($SelfT), ";")]
133-
///
134147
/// assert_eq!(n.trailing_zeros(), 3);
148+
///
149+
#[doc = concat!("let zero = 0", stringify!($SelfT), ";")]
150+
#[doc = concat!("assert_eq!(zero.trailing_zeros(), ", stringify!($BITS), ");")]
151+
///
152+
#[doc = concat!("let max = ", stringify!($SelfT),"::MAX;")]
153+
#[doc = concat!("assert_eq!(max.trailing_zeros(), 0);")]
135154
/// ```
136155
#[stable(feature = "rust1", since = "1.0.0")]
137156
#[rustc_const_stable(feature = "const_math", since = "1.32.0")]
@@ -150,8 +169,13 @@ macro_rules! uint_impl {
150169
///
151170
/// ```
152171
#[doc = concat!("let n = !(", stringify!($SelfT), "::MAX >> 2);")]
153-
///
154172
/// assert_eq!(n.leading_ones(), 2);
173+
///
174+
#[doc = concat!("let zero = 0", stringify!($SelfT), ";")]
175+
/// assert_eq!(zero.leading_ones(), 0);
176+
///
177+
#[doc = concat!("let max = ", stringify!($SelfT),"::MAX;")]
178+
#[doc = concat!("assert_eq!(max.leading_ones(), ", stringify!($BITS), ");")]
155179
/// ```
156180
#[stable(feature = "leading_trailing_ones", since = "1.46.0")]
157181
#[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
@@ -171,8 +195,13 @@ macro_rules! uint_impl {
171195
///
172196
/// ```
173197
#[doc = concat!("let n = 0b1010111", stringify!($SelfT), ";")]
174-
///
175198
/// assert_eq!(n.trailing_ones(), 3);
199+
///
200+
#[doc = concat!("let zero = 0", stringify!($SelfT), ";")]
201+
/// assert_eq!(zero.trailing_ones(), 0);
202+
///
203+
#[doc = concat!("let max = ", stringify!($SelfT),"::MAX;")]
204+
#[doc = concat!("assert_eq!(max.trailing_ones(), ", stringify!($BITS), ");")]
176205
/// ```
177206
#[stable(feature = "leading_trailing_ones", since = "1.46.0")]
178207
#[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]

0 commit comments

Comments
 (0)
Failed to load comments.