mirror of
git://git.musl-libc.org/musl
synced 2025-01-12 01:29:31 +00:00
add a_clz_64 helper function
counts leading zero bits of a 64bit int, undefined on zero input. (has nothing to do with atomics, added to atomic.h so target specific helper functions are together.) there is a logarithmic generic implementation and another in terms of a 32bit a_clz_32 on targets where that's available.
This commit is contained in:
parent
3356177979
commit
06fbefd100
@ -73,3 +73,10 @@ static inline int a_ctz_64(uint64_t x)
|
||||
: "=r"(x) : "r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#define a_clz_64 a_clz_64
|
||||
static inline int a_clz_64(uint64_t x)
|
||||
{
|
||||
__asm__("clz %0, %1" : "=r"(x) : "r"(x));
|
||||
return x;
|
||||
}
|
||||
|
@ -81,3 +81,10 @@ static inline void a_crash()
|
||||
#endif
|
||||
: : : "memory");
|
||||
}
|
||||
|
||||
#define a_clz_32 a_clz_32
|
||||
static inline int a_clz_32(uint32_t x)
|
||||
{
|
||||
__asm__ ("clz %0, %1" : "=r"(x) : "r"(x));
|
||||
return x;
|
||||
}
|
||||
|
@ -99,3 +99,10 @@ static inline int a_ctz_l(unsigned long x)
|
||||
__asm__( "bsf %1,%0" : "=r"(r) : "r"(x) );
|
||||
return r;
|
||||
}
|
||||
|
||||
#define a_clz_32 a_clz_32
|
||||
static inline int a_clz_32(uint32_t x)
|
||||
{
|
||||
__asm__( "bsr %1,%0 ; xor $31,%0" : "=r"(x) : "r"(x) );
|
||||
return x;
|
||||
}
|
||||
|
@ -37,3 +37,10 @@ static inline void a_store(volatile int *p, int v)
|
||||
*p = v;
|
||||
a_post_llsc();
|
||||
}
|
||||
|
||||
#define a_clz_32 a_clz_32
|
||||
static inline int a_clz_32(uint32_t x)
|
||||
{
|
||||
__asm__ ("cntlzw %0, %1" : "=r"(x) : "r"(x));
|
||||
return x;
|
||||
}
|
||||
|
@ -61,3 +61,10 @@ static inline void a_crash()
|
||||
{
|
||||
__asm__ __volatile__ (".long 0");
|
||||
}
|
||||
|
||||
#define a_clz_64 a_clz_64
|
||||
static inline int a_clz_64(uint64_t x)
|
||||
{
|
||||
__asm__ ("cntlzd %0, %1" : "=r"(x) : "r"(x));
|
||||
return x;
|
||||
}
|
||||
|
@ -112,3 +112,10 @@ static inline int a_ctz_l(unsigned long x)
|
||||
__asm__( "bsf %1,%0" : "=r"(x) : "r"(x) );
|
||||
return x;
|
||||
}
|
||||
|
||||
#define a_clz_64 a_clz_64
|
||||
static inline int a_clz_64(uint64_t x)
|
||||
{
|
||||
__asm__( "bsr %1,%0 ; xor $63,%0" : "=r"(x) : "r"(x) );
|
||||
return x;
|
||||
}
|
||||
|
@ -114,3 +114,10 @@ static inline int a_ctz_64(uint64_t x)
|
||||
__asm__( "bsf %1,%0" : "=r"(x) : "r"(x) );
|
||||
return x;
|
||||
}
|
||||
|
||||
#define a_clz_64 a_clz_64
|
||||
static inline int a_clz_64(uint64_t x)
|
||||
{
|
||||
__asm__( "bsr %1,%0 ; xor $63,%0" : "=r"(x) : "r"(x) );
|
||||
return x;
|
||||
}
|
||||
|
@ -277,6 +277,27 @@ static inline int a_ctz_64(uint64_t x)
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef a_clz_64
|
||||
#define a_clz_64 a_clz_64
|
||||
static inline int a_clz_64(uint64_t x)
|
||||
{
|
||||
#ifdef a_clz_32
|
||||
if (x>>32)
|
||||
return a_clz_32(x>>32);
|
||||
return a_clz_32(x) + 32;
|
||||
#else
|
||||
uint32_t y;
|
||||
int r;
|
||||
if (x>>32) y=x>>32, r=0; else y=x, r=32;
|
||||
if (y>>16) y>>=16; else r |= 16;
|
||||
if (y>>8) y>>=8; else r |= 8;
|
||||
if (y>>4) y>>=4; else r |= 4;
|
||||
if (y>>2) y>>=2; else r |= 2;
|
||||
return r | !(y>>1);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef a_ctz_l
|
||||
#define a_ctz_l a_ctz_l
|
||||
static inline int a_ctz_l(unsigned long x)
|
||||
|
Loading…
Reference in New Issue
Block a user