1. 优化Bit

This commit is contained in:
coffee 2026-03-28 08:42:10 +08:00
parent c70549293b
commit e63e2899ac

View File

@ -6,6 +6,10 @@
#define LogD(...) #define LogD(...)
#endif #endif
#define _HBIT_BYTE_INDEX(index) (index >> 3)
#define _HBIT_BIT_OFFSET(index) (index & 0x7)
#define _HBIT_ALIGNED_LEN(len) (len & (~(HBitLenType)0x7))
static void InitHBit(HBitType *data) { static void InitHBit(HBitType *data) {
if ((data[0] & kHBitInitFlag) == 0) { if ((data[0] & kHBitInitFlag) == 0) {
return; return;
@ -25,12 +29,12 @@ static HBitLenType GetBitLen(const HBitType *data) {
static void SetBit(HBitType *data, HBitIndexType index, HBitType value) { static void SetBit(HBitType *data, HBitIndexType index, HBitType value) {
_HBitBase *base = (_HBitBase *)data; _HBitBase *base = (_HBitBase *)data;
base->data[index / 8] = (base->data[index / 8] & ~(0x01 << (index % 8))) | ((!!value) << (index % 8)); base->data[_HBIT_BYTE_INDEX(index)] = (base->data[_HBIT_BYTE_INDEX(index)] & ~(0x01 << _HBIT_BIT_OFFSET(index))) | ((!!value) << _HBIT_BIT_OFFSET(index));
} }
static HBitType GetBit(const HBitType *data, HBitIndexType index) { static HBitType GetBit(const HBitType *data, HBitIndexType index) {
_HBitBase *base = (_HBitBase *)data; _HBitBase *base = (_HBitBase *)data;
return (base->data[index / 8] >> (index % 8)) & 0x01; return (base->data[_HBIT_BYTE_INDEX(index)] >> _HBIT_BIT_OFFSET(index)) & 0x01;
} }
void HBitSet(HBitType *data, HBitIndexType index, HBitType value) { void HBitSet(HBitType *data, HBitIndexType index, HBitType value) {
@ -56,11 +60,11 @@ HBitType HBitGet(const HBitType *data, HBitIndexType index) {
void HBitFill(HBitType *data, HBitType value) { void HBitFill(HBitType *data, HBitType value) {
InitHBit(data); InitHBit(data);
_HBitBase *base = (_HBitBase *)data; _HBitBase *base = (_HBitBase *)data;
for (int i = 0; i < base->len / 8; ++i) { for (int i = 0; i < _HBIT_BYTE_INDEX(base->len); ++i) {
base->data[i] = value ? 0xFF : 0x00; base->data[i] = value ? 0xFF : 0x00;
} }
for (int i = base->len / 8 * 8; i < base->len; ++i) { for (int i = _HBIT_ALIGNED_LEN(base->len); i < base->len; ++i) {
SetBit(data, i, value); SetBit(data, i, value);
} }
} }
@ -73,13 +77,13 @@ void HBitReverse(HBitType *data, HBitIndexType index) {
HBitType HBitAny(const HBitType *data) { HBitType HBitAny(const HBitType *data) {
const _HBitBase *base = (_HBitBase *)data; const _HBitBase *base = (_HBitBase *)data;
InitHBit((HBitType *)data); InitHBit((HBitType *)data);
for (int i = 0; i < base->len / 8; ++i) { for (HBitLenType i = 0; i < _HBIT_BYTE_INDEX(base->len); ++i) {
if (base->data[i] != 0x00) { if (base->data[i] != 0x00) {
return 1; return 1;
} }
} }
for (int i = base->len / 8 * 8; i < base->len; ++i) { for (HBitLenType i = _HBIT_ALIGNED_LEN(base->len); i < base->len; ++i) {
if (HBitGet(data, i)) { if (HBitGet(data, i)) {
return 1; return 1;
} }
@ -91,13 +95,13 @@ HBitType HBitAny(const HBitType *data) {
HBitType HBitAll(const HBitType *data) { HBitType HBitAll(const HBitType *data) {
const _HBitBase *base = (_HBitBase *)data; const _HBitBase *base = (_HBitBase *)data;
InitHBit((HBitType *)data); InitHBit((HBitType *)data);
for (int i = 0; i < base->len / 8; ++i) { for (HBitLenType i = 0; i < _HBIT_BYTE_INDEX(base->len); ++i) {
if (base->data[i] != 0xFF) { if (base->data[i] != 0xFF) {
return 0; return 0;
} }
} }
for (int i = base->len / 8 * 8; i < base->len; ++i) { for (HBitLenType i = _HBIT_ALIGNED_LEN(base->len); i < base->len; ++i) {
if (!HBitGet(data, i)) { if (!HBitGet(data, i)) {
return 0; return 0;
} }