2022-08-18 23:29:51 +08:00

55 lines
1.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <common/stddef.h>
/**
* @brief 统计二进制数的前导0
*
* @param x 待统计的数
* @return int 结果
*/
static __always_inline int __clz(uint32_t x)
{
asm volatile("bsr %%eax, %%eax\n\t"
"xor $0x1f, %%eax\n\t"
: "=a"(x)
: "a"(x)
: "memory");
return x;
}
/**
* @brief 统计二进制数的前导0 (宽度为unsigned long)
*
* @param x 待统计的数
* @return int 结果
*/
static __always_inline int __clzl(unsigned long x)
{
int res = 0;
asm volatile("cltq\n\t"
"bsr %%rax, %%rax\n\t"
"xor $0x3f, %%rax\n\t"
"mov %%eax,%0\n\t"
: "=m"(res)
: "a"(x)
: "memory");
return res;
}
/**
* @brief 统计二进制数的前导0宽度为unsigned long long
*
* @param x 待统计的数
* @return int 结果
*/
static __always_inline int __clzll(unsigned long long x)
{
int res = 0;
asm volatile("cltq\n\t"
"bsr %%rax, %%rax\n\t"
"xor $0x3f, %%rax\n\t"
"mov %%eax,%0\n\t"
: "=m"(res)
: "a"(x)
: "memory");
return res;
}