new: 统计前导0

This commit is contained in:
fslongjin 2022-08-18 23:29:51 +08:00
parent 77633e2f19
commit bf4226f6b9
2 changed files with 63 additions and 1 deletions

View 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 0unsigned 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;
}

View File

@ -1,3 +1,10 @@
#pragma once
#include "stddef.h"
int64_t pow(int64_t x, int y);
#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);