更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_bitmap.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "los_bitmap.h"
33#include "los_printf.h"
34#include "los_toolchain.h" //GCC 编译器的内置函数
35
36/**
37 * @brief
38 * @verbatim
39 基本概念
40 位操作是指对二进制数的bit位进行操作。程序可以设置某一变量为状态字,状态字中的
41 每一bit位(标志位)可以具有自定义的含义。
42
43 使用场景
44 系统提供标志位的置1和清0操作,可以改变标志位的内容,同时还提供获取状态字中标志位
45 为1的最高位和最低位的功能。用户也可以对系统的寄存器进行位操作。
46
47 参考
48 https://www.geeksforgeeks.org/builtin-functions-gcc-compiler/
49 * @endverbatim
50 */
51#define OS_BITMAP_MASK 0x1FU //
52#define OS_BITMAP_WORD_MASK ~0UL
53
54/*! find first zero bit starting from LSB */
55STATIC INLINE UINT16 Ffz(UINTPTR x)
56{//__builtin_ffsl: 返回右起第一个1的位置,函数来自 glibc
57 return __builtin_ffsl(~x) - 1;//从LSB开始查找第一个零位 LSB(最低有效位) 对应 最高有效位(MSB)
58}
59///对状态字的某一标志位进行置1操作
60VOID LOS_BitmapSet(UINT32 *bitmap, UINT16 pos)
61{
62 if (bitmap == NULL) {
63 return;
64 }
65
66 *bitmap |= 1U << (pos & OS_BITMAP_MASK);//在对应位上置1
67}
68///对状态字的某一标志位进行清0操作
69VOID LOS_BitmapClr(UINT32 *bitmap, UINT16 pos)
70{
71 if (bitmap == NULL) {
72 return;
73 }
74
75 *bitmap &= ~(1U << (pos & OS_BITMAP_MASK));//在对应位上置0
76}
77
78/**
79 * @brief 获取参数位图中最高位为1的索引位 例如: 00110110 返回 5
80 * @verbatim
81 CLZ 用于计算操作数最高端0的个数,这条指令主要用于以下两个场合
82   1.计算操作数规范化(使其最高位为1)时需要左移的位数
83   2.确定一个优先级掩码中最高优先级
84 * @endverbatim
85 * @param bitmap
86 * @return UINT16
87 */
89{
90 if (bitmap == 0) {
91 return LOS_INVALID_BIT_INDEX;
92 }
93
94 return (OS_BITMAP_MASK - CLZ(bitmap));//CLZ = count leading zeros 用于计算整数的前导零
95}
96/// 获取参数位图中最低位为1的索引位, 例如: 00110110 返回 1
98{
99 if (bitmap == 0) {
100 return LOS_INVALID_BIT_INDEX;
101 }
102
103 return CTZ(bitmap);// CTZ = count trailing zeros 用于计算给定整数的尾随零
104}
105/// 从start位置开始设置numsSet个bit位 置1
106VOID LOS_BitmapSetNBits(UINTPTR *bitmap, UINT32 start, UINT32 numsSet)
107{
108 UINTPTR *p = bitmap + BITMAP_WORD(start);
109 const UINT32 size = start + numsSet;
110 UINT16 bitsToSet = BITMAP_BITS_PER_WORD - (start % BITMAP_BITS_PER_WORD);
111 UINTPTR maskToSet = BITMAP_FIRST_WORD_MASK(start);
112
113 while (numsSet > bitsToSet) {
114 *p |= maskToSet;
115 numsSet -= bitsToSet;
116 bitsToSet = BITMAP_BITS_PER_WORD;
117 maskToSet = OS_BITMAP_WORD_MASK;
118 p++;
119 }
120 if (numsSet) {
121 maskToSet &= BITMAP_LAST_WORD_MASK(size);
122 *p |= maskToSet;
123 }
124}
125///从start位置开始 清除numsSet个bit位置0 ,对状态字的连续标志位进行清0操作
126VOID LOS_BitmapClrNBits(UINTPTR *bitmap, UINT32 start, UINT32 numsClear)
127{
128 UINTPTR *p = bitmap + BITMAP_WORD(start);
129 const UINT32 size = start + numsClear;
130 UINT16 bitsToClear = BITMAP_BITS_PER_WORD - (start % BITMAP_BITS_PER_WORD);
131 UINTPTR maskToClear = BITMAP_FIRST_WORD_MASK(start);
132
133 while (numsClear >= bitsToClear) {
134 *p &= ~maskToClear;
135 numsClear -= bitsToClear;
136 bitsToClear = BITMAP_BITS_PER_WORD;
137 maskToClear = OS_BITMAP_WORD_MASK;
138 p++;
139 }
140 if (numsClear) {
141 maskToClear &= BITMAP_LAST_WORD_MASK(size);
142 *p &= ~maskToClear;
143 }
144}
145///从numBits位置开始找到第一个0位
147{
148 INT32 bit, i;
149
150 for (i = 0; i < BITMAP_NUM_WORDS(numBits); i++) {
151 if (bitmap[i] == OS_BITMAP_WORD_MASK) {
152 continue;
153 }
154 bit = i * BITMAP_BITS_PER_WORD + Ffz(bitmap[i]);
155 if (bit < numBits) {
156 return bit;
157 }
158 return -1;
159 }
160 return -1;
161}
162
VOID LOS_BitmapSet(UINT32 *bitmap, UINT16 pos)
对状态字的某一标志位进行置1操作
Definition: los_bitmap.c:60
VOID LOS_BitmapClr(UINT32 *bitmap, UINT16 pos)
对状态字的某一标志位进行清0操作
Definition: los_bitmap.c:69
UINT16 LOS_LowBitGet(UINT32 bitmap)
获取参数位图中最低位为1的索引位, 例如: 00110110 返回 1
Definition: los_bitmap.c:97
VOID LOS_BitmapClrNBits(UINTPTR *bitmap, UINT32 start, UINT32 numsClear)
从start位置开始 清除numsSet个bit位置0 ,对状态字的连续标志位进行清0操作
Definition: los_bitmap.c:126
UINT16 LOS_HighBitGet(UINT32 bitmap)
获取参数位图中最高位为1的索引位 例如: 00110110 返回 5
Definition: los_bitmap.c:88
INT32 LOS_BitmapFfz(UINTPTR *bitmap, UINT32 numBits)
从numBits位置开始找到第一个0位
Definition: los_bitmap.c:146
VOID LOS_BitmapSetNBits(UINTPTR *bitmap, UINT32 start, UINT32 numsSet)
从start位置开始设置numsSet个bit位 置1
Definition: los_bitmap.c:106
STATIC INLINE UINT16 Ffz(UINTPTR x)
Definition: los_bitmap.c:55
unsigned short UINT16
Definition: los_typedef.h:56
signed int INT32
Definition: los_typedef.h:60
unsigned long UINTPTR
Definition: los_typedef.h:68
unsigned int UINT32
Definition: los_typedef.h:57