Abstract: This article leads us to analyze the LiteOS bit operation module source code, the code is very simple, refer to the official example program code, the actual compilation run, deepen understanding.

This article is from huawei cloud community “LiteOS kernel source code analysis series five LiteOS kernel source code analysis — bit operation module”, original author: zhushy.

Before further analysis, this article will familiarize us with the auxiliary function module – bit operation provided by LiteOS, which is used for bit operation in modules such as mutex. Bitwise operations are operations on bits of binary numbers. The program can set a variable as a status word, and each bit (flag bit) in the status word can have a custom meaning. The source code mentioned in this article can be found on LiteOS open source site gitee.com/LiteOS/Lite… To obtain. Bit operation module source code, development documents are as follows:

  • LiteOS kernel bit operation source code

The function of bit-operation module is relatively simple, including the bit-operation header file kernel\include\los_bitmap.h, C source file kernel\base\los_bitmap.c.

  • Development guide bit operation documentation

Online documentation gitee.com/LiteOS/Lite… .

Let’s first look at the bit-manipulation concept core usage scenarios. For detailed introduction and related examples, please refer to the Bit-manipulation documentation of the LiteOS Development Guide.

LiteOS bit manipulation module provides 32-bit unsigned integer values with bits ranging from 0 to 31, starting from 0, left to right, 0 bit, 1 bit… 31st and so on. The macro OS_BITMAP_MASK is defined as follows, in decimal 31. If the incoming bit is greater than 31, pos& OS_BITMAP_MASK is truncated by logic and operation (pos& OS_BITMAP_MASK). The lower 5 bits are selected to ensure that the value is not greater than 31 to avoid overflow.

#define OS_BITMAP_MASK 0x1FU
Copy the code

HuaweiLiteOS bit operation module Provides 1 and 0 operations to change the content of the flag bit. It also obtains the highest and lowest bit of the status word whose flag bit is 1. Users can also perform bitwise operations on system registers.

Below, we analyze the source code of the lower bit operation.

Bit operation common functions

Bit operation provides four apis for setting 1, clearing 0, and obtaining the highest and lowest value of 1, as follows:

LOS_BitmapSet() sets a flag bit of the status word to 1

Sets one of the flag bits of the status word to 1. The state of the content to be changed is described as follows: UINT32 *bitmap; UINT16 pos

The code is simple, first performing basic validation and returning if the status word is empty. Then calculate pos & OS_BITMAP_MASK, taking only the lower 5 bits of binary and the maximum value of 31 bits to avoid overflow when moving to the left. 1U << (pos & OS_BITMAP_MASK) Indicates the status bit that needs to be changed. Set the status word to UINT32 * Bitmap with the specified bit set to 1.

VOID LOS_BitmapSet(UINT32 *bitmap, UINT16 pos){    if (bitmap == NULL) {        return;    }    *bitmap |= 1U << (pos & OS_BITMAP_MASK);}
Copy the code

LOS_BitmapClr() clears a flag bit of the status word

~(1U<< (pos & OS_BITMAP_MASK)) indicates that the bits of the status word to be changed are 0 and the rest bits are 1. Then, the status word is set to 0 based on the bit-and-operation UINT32 * Bitmap.

VOID LOS_BitmapClr(UINT32 *bitmap, UINT16 pos){    if (bitmap == NULL) {        return;    }    *bitmap &= ~(1U << (pos & OS_BITMAP_MASK));}
Copy the code

LOS_HighBitGet() gets the highest bit of the 1 in the status word

CLZ(bitmap) is a macro that expands to (__builtin_clz(bitmap)), which is a library function of the compiler’s built-in efficient bit operation. CLZ is short for count leading Zeros, which is the number of zeros at the beginning of the high level of the binary value. Subtracting this value using OS_BITMAP_MASK results in the highest bit of 1 in the status word.

UINT16 LOS_HighBitGet(UINT32 bitmap){    if (bitmap == 0) {        return LOS_INVALID_BIT_INDEX;    }    return (OS_BITMAP_MASK - CLZ(bitmap));}
Copy the code

LOS_LowBitGet() gets the lowest value of 1 in the status word

CLZ(bitmap) is a macro, expanded to (__builtin_ctz(value)), which is a library function of the compiler’s built-in efficient bit operation. CTZ is short for count trailing zeros, which counts the number of zeros ending in the lower part of a binary value. The result is the least significant 1 in the status word.

UINT16 LOS_LowBitGet(UINT32 bitmap){    if (bitmap == 0) {        return LOS_INVALID_BIT_INDEX;    }    return CTZ(bitmap);}
Copy the code

summary

This article led us to analyze the LiteOS bit operation module source code, the code is very simple, refer to the official example program code, the actual compilation run, deepen understanding.

Click to follow, the first time to learn about Huawei cloud fresh technology ~