mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-14 07:56:47 +00:00
new: 统计前导0
This commit is contained in:
parent
77633e2f19
commit
bf4226f6b9
55
kernel/arch/x86_64/math/bitcount.h
Normal file
55
kernel/arch/x86_64/math/bitcount.h
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#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;
|
||||||
|
}
|
@ -1,3 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "stddef.h"
|
#include "stddef.h"
|
||||||
|
#include <arch/arch.h>
|
||||||
|
#if ARCH(I386) || ARCH(X86_64)
|
||||||
|
#include <arch/x86_64/math/bitcount.h>
|
||||||
|
#else
|
||||||
|
#error Arch not supported.
|
||||||
|
#endif
|
||||||
|
|
||||||
int64_t pow(int64_t x, int y);
|
int64_t pow(int64_t x, int y);
|
Loading…
x
Reference in New Issue
Block a user