core/stdarch/crates/core_arch/src/x86_64/abm.rs
1//! Advanced Bit Manipulation (ABM) instructions
2//!
3//! The POPCNT and LZCNT have their own CPUID bits to indicate support.
4//!
5//! The references are:
6//!
7//! - [Intel 64 and IA-32 Architectures Software Developer's Manual Volume 2:
8//! Instruction Set Reference, A-Z][intel64_ref].
9//! - [AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and
10//! System Instructions][amd64_ref].
11//!
12//! [Wikipedia][wikipedia_bmi] provides a quick overview of the instructions
13//! available.
14//!
15//! [intel64_ref]: https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf
16//! [amd64_ref]: https://docs.amd.com/v/u/en-US/24594_3.37
17//! [wikipedia_bmi]:
18//! https://en.wikipedia.org/wiki/Bit_Manipulation_Instruction_Sets#ABM_.28Advanced_Bit_Manipulation.29
19
20#[cfg(test)]
21use stdarch_test::assert_instr;
22
23/// Counts the leading most significant zero bits.
24///
25/// When the operand is zero, it returns its size in bits.
26///
27/// [Intel's documentation](https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_lzcnt_u64)
28#[inline]
29#[target_feature(enable = "lzcnt")]
30#[cfg_attr(test, assert_instr(lzcnt))]
31#[stable(feature = "simd_x86", since = "1.27.0")]
32#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
33pub const fn _lzcnt_u64(x: u64) -> u64 {
34 x.leading_zeros() as u64
35}
36
37/// Counts the bits that are set.
38///
39/// [Intel's documentation](https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_popcnt64)
40#[inline]
41#[target_feature(enable = "popcnt")]
42#[cfg_attr(test, assert_instr(popcnt))]
43#[stable(feature = "simd_x86", since = "1.27.0")]
44#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
45pub const fn _popcnt64(x: i64) -> i32 {
46 x.count_ones() as i32
47}
48
49#[cfg(test)]
50mod tests {
51 use crate::core_arch::assert_eq_const as assert_eq;
52 use stdarch_test::simd_test;
53
54 use crate::core_arch::arch::x86_64::*;
55
56 #[simd_test(enable = "lzcnt")]
57 const fn test_lzcnt_u64() {
58 assert_eq!(_lzcnt_u64(0b0101_1010), 57);
59 }
60
61 #[simd_test(enable = "popcnt")]
62 const fn test_popcnt64() {
63 assert_eq!(_popcnt64(0b0101_1010), 4);
64 }
65}