Skip to content

Commit bd16aa0

Browse files
committed
Auto merge of #38310 - frewsxcv:ctlz-cttz, r=pnkfelix
Clarify zero-value behavior of `ctlz`/`cttz` intrinsics. Fixes #34381.
2 parents 7ef1a69 + a99f70b commit bd16aa0

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

src/libcore/intrinsics.rs

+50-2
Original file line numberDiff line numberDiff line change
@@ -1156,10 +1156,58 @@ extern "rust-intrinsic" {
11561156
/// Returns the number of bits set in an integer type `T`
11571157
pub fn ctpop<T>(x: T) -> T;
11581158

1159-
/// Returns the number of leading bits unset in an integer type `T`
1159+
/// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1160+
///
1161+
/// # Examples
1162+
///
1163+
/// ```
1164+
/// #![feature(core_intrinsics)]
1165+
///
1166+
/// use std::intrinsics::ctlz;
1167+
///
1168+
/// let x = 0b0001_1100_u8;
1169+
/// let num_leading = unsafe { ctlz(x) };
1170+
/// assert_eq!(num_leading, 3);
1171+
/// ```
1172+
///
1173+
/// An `x` with value `0` will return the bit width of `T`.
1174+
///
1175+
/// ```
1176+
/// #![feature(core_intrinsics)]
1177+
///
1178+
/// use std::intrinsics::ctlz;
1179+
///
1180+
/// let x = 0u16;
1181+
/// let num_leading = unsafe { ctlz(x) };
1182+
/// assert_eq!(num_leading, 16);
1183+
/// ```
11601184
pub fn ctlz<T>(x: T) -> T;
11611185

1162-
/// Returns the number of trailing bits unset in an integer type `T`
1186+
/// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1187+
///
1188+
/// # Examples
1189+
///
1190+
/// ```
1191+
/// #![feature(core_intrinsics)]
1192+
///
1193+
/// use std::intrinsics::cttz;
1194+
///
1195+
/// let x = 0b0011_1000_u8;
1196+
/// let num_trailing = unsafe { cttz(x) };
1197+
/// assert_eq!(num_trailing, 3);
1198+
/// ```
1199+
///
1200+
/// An `x` with value `0` will return the bit width of `T`:
1201+
///
1202+
/// ```
1203+
/// #![feature(core_intrinsics)]
1204+
///
1205+
/// use std::intrinsics::cttz;
1206+
///
1207+
/// let x = 0u16;
1208+
/// let num_trailing = unsafe { cttz(x) };
1209+
/// assert_eq!(num_trailing, 16);
1210+
/// ```
11631211
pub fn cttz<T>(x: T) -> T;
11641212

11651213
/// Reverses the bytes in an integer type `T`.

0 commit comments

Comments
 (0)