You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.0 KiB
61 lines
1.0 KiB
4 months ago
|
/*
|
||
|
* Copyright (c) Hisilicon Technologies Co., Ltd. 2014-2020. All rights reserved.
|
||
|
* Description: bits.c
|
||
|
* Author: SmartMedia_BSP
|
||
|
* Create: 2014-06-04
|
||
|
*/
|
||
|
|
||
|
#include <stddef.h>
|
||
|
|
||
|
int bits_count(uint32 nn)
|
||
|
{
|
||
|
int count = 0;
|
||
|
while (nn) {
|
||
|
if (nn & 1) {
|
||
|
count++;
|
||
|
}
|
||
|
nn >>= 1;
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
|
|
||
|
int ffs(int nn)
|
||
|
{
|
||
|
int pos = 1;
|
||
|
unsigned int mask = 0xFFFF;
|
||
|
unsigned int shift = 16; /* 16:number */
|
||
|
|
||
|
if (!nn) {
|
||
|
return 0;
|
||
|
}
|
||
|
while (shift) {
|
||
|
if (!((unsigned int)nn & mask)) {
|
||
|
nn = (unsigned int)nn >> shift;
|
||
|
pos += shift;
|
||
|
}
|
||
|
shift >>= 1;
|
||
|
mask >>= shift;
|
||
|
}
|
||
|
return pos;
|
||
|
}
|
||
|
|
||
|
int ffs64(uint64 nn)
|
||
|
{
|
||
|
int pos = 1;
|
||
|
uint32 mask = 0xFFFFFFFF;
|
||
|
uint32 shift = 32; /* 32:number */
|
||
|
|
||
|
if (!nn) {
|
||
|
return 0;
|
||
|
}
|
||
|
while (shift) {
|
||
|
if (!(nn & mask)) {
|
||
|
nn >>= shift;
|
||
|
pos += shift;
|
||
|
}
|
||
|
shift >>= 1;
|
||
|
mask >>= shift;
|
||
|
}
|
||
|
return pos;
|
||
|
}
|