Embed/microCLib

优化性能
JiuHuan authored at 2026-08-14 14:00:33
72debd6
Tree
1 Parent(s) 206fe5e
Summary: 3 changed files with 34 additions and 15 deletions.
Modified +25 -9
Modified +7 -4
Modified +2 -2
Modified +25 -9
diff --git a/Core/BitHelper.c b/Core/BitHelper.c
index 6a4e025..7041c98 100644
--- a/Core/BitHelper.c
+++ b/Core/BitHelper.c
@@ -50,7 +50,7 @@ __inline void SetBit(byte* data, int idx, bool value)
 }
 
 /**
- * @brief 设置多bit
+ * @brief bit copy
  * @param dst 目标
  * @param dst_offset 目标偏移量
  * @param src 源
@@ -58,18 +58,20 @@ __inline void SetBit(byte* data, int idx, bool value)
  * @param bits 需要set的数量
  * @return 是否完成了
  */
-bool SetBits(byte* dst, int dst_offset, byte* src, int src_offset, int bits)
+bool BitCopy(byte* dst, int dst_offset, byte* src, int src_offset, int bits)
 {
 	if (dst == NULL)return false;
 	if (src == NULL)return false;
+	if (bits < 1)return true;
 
 	for (int i = 0; i < bits; i++)
 	{
-		bool value = GetBit(src, src_offset);
-		SetBit(dst, dst_offset, value);
+		int sIdx = src_offset + i;
+		int dIdx = dst_offset + i;
 
-		src_offset++;
-		dst_offset++;
+		bool value = (src[sIdx >> 3] & (0x01 << (sIdx & 0x07))) != 0;
+		if (value) { dst[dIdx >> 3] |= (byte) (0x01 << (dIdx & 0x07)); }
+		else	   { dst[dIdx >> 3] &= (byte)~(0x01 << (dIdx & 0x07)); }
 	}
 
 	return true;
@@ -78,16 +80,21 @@ bool SetBits(byte* dst, int dst_offset, byte* src, int src_offset, int bits)
 /** @brief 字节bit倒序 */
 byte BitReverse(byte data)
 {
+	/*
 	byte res = 0;
 	for (int i = 0; i < 8; i++)
 	{
 		byte mask = 0x01 << i;
 		byte mask2 = 0x80 >> i;
-
 		if ((data & mask) != 0)res |= mask2;
 	}
-
 	return res;
+	*/
+
+	data = (byte)(((data & 0x0F) << 4) | ((data & 0xF0) >> 4));   // 交换半字节
+	data = (byte)(((data & 0x33) << 2) | ((data & 0xCC) >> 2));   // 交换2位一组
+	data = (byte)(((data & 0x55) << 1) | ((data & 0xAA) >> 1));   // 交换相邻位
+	return data;
 }
 
 /** @brief 统计数组内所有bit 为1 的总数 */
@@ -108,12 +115,21 @@ int BitCount(byte* data, int len)
 			continue;
 		}
 
+		/*
 		for (int j = 0; j < 8; j++)
 		{
 			byte mask = 0x01 << j;
-
 			if ((dat & mask) != 0)cnt++;
 		}
+		*/
+
+		// Kernighan 算法
+		// 有几个1,循环几次
+		while (dat)
+		{
+			dat = (byte)(dat & (dat - 1));
+			cnt++;
+		}
 	}
 
 	return cnt;
Modified +7 -4
diff --git a/Core/BitHelper.h b/Core/BitHelper.h
index 3d89c38..42d54b1 100644
--- a/Core/BitHelper.h
+++ b/Core/BitHelper.h
@@ -18,7 +18,7 @@ bool GetBit(byte* data, int idx);
 void SetBit(byte* data, int idx, bool value);
 
 /**
- * @brief 设置多bit
+ * @brief bit copy
  * @param dst 目标
  * @param dst_offset 目标偏移量
  * @param src 源
@@ -26,7 +26,7 @@ void SetBit(byte* data, int idx, bool value);
  * @param bits 需要set的数量
  * @return 是否完成了
  */
-bool SetBits(byte* dst, int dst_offset, byte* src, int src_offset, int bits);
+bool BitCopy(byte* dst, int dst_offset, byte* src, int src_offset, int bits);
 
 /** @brief 字节bit倒序 */
 byte BitReverse(byte data);
@@ -35,6 +35,9 @@ byte BitReverse(byte data);
 int BitCount(byte* data, int len);
 
 /*
-不用额外写 bool GetBits(byte* dst, int dst_offset, byte* src, int src_offset, int bits);
-直接用  SetBits(byte* dst, int dst_offset, byte* src, int src_offset, int bits);  即可。
+bool GetBits(byte* dst, int dst_offset, byte* src, int src_offset, int bits);
+bool SetBits(byte* dst, int dst_offset, byte* src, int src_offset, int bits); 
+统一成 
+bool BitCopy(byte* dst, int dst_offset, byte* src, int src_offset, int bits);
 */
+
Modified +2 -2
diff --git a/Core/VirtualArea.c b/Core/VirtualArea.c
index 2643974..24dd824 100644
--- a/Core/VirtualArea.c
+++ b/Core/VirtualArea.c
@@ -62,7 +62,7 @@ int AreaBitCopy(Area_t* dst, Area_t* src)
 
 		int cplen = min(dst->Size - dst_offset, src->Size);
 
-		SetBits(dst->Buff, dst_offset, src->Buff, 0, cplen);
+		BitCopy(dst->Buff, dst_offset, src->Buff, 0, cplen);
 
 		return cplen;
 	}
@@ -72,7 +72,7 @@ int AreaBitCopy(Area_t* dst, Area_t* src)
 
 		int cplen = min(src->Size - src_offset, dst->Size);
 
-		SetBits(dst->Buff, 0, src->Buff, src_offset, cplen);
+		BitCopy(dst->Buff, 0, src->Buff, src_offset, cplen);
 
 		return cplen;
 	}