guanjinquan d53ddde95d
Patch add idr (#56)
* 增加了idr模块

* 增加了IDR模块,并尝试覆盖上一个错误版本.

* 增加了IDR模块

* 修改了test-idr.c文件

* 进一步完善函数注释

* 更新idr文档同时修改了test-idr的错误


* 将lz4库改为使用系统的clz函数

* idr和test-idr O1

* bugfix: 修复测试用例中的移位问题

* 修正问题

Signed-off-by: guanjinquan <1666320330@qq.com>
Co-authored-by: fslongjin <longjin@RinGoTek.cn>
2022-10-23 16:07:28 +08:00

69 lines
1.5 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;
}
static __always_inline int __ctz(uint32_t x)
{
asm volatile("tzcnt %%eax, %%eax":"=a"(x):"a"(x):"memory");
return x;
}
static __always_inline int __ctzl(unsigned long x)
{
asm volatile("tzcnt %%rax, %%rax":"=a"(x):"a"(x):"memory");
return x;
}
#define __ctzll __ctzl