Files
kunlun/plc/cvg/common/inc/cvg_bitmap.h
2024-09-28 14:24:04 +08:00

189 lines
5.9 KiB
C

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#ifndef CVG_BITMAP_H
#define CVG_BITMAP_H
/* os shim includes */
#include "os_types.h"
#include "os_mem.h"
/* common includes */
#include "iot_bitops.h"
#include "iot_bitmap_api.h"
/* public api includes */
#include "plc_protocol.h"
#ifdef __cplusplus
extern "C" {
#endif
/* bitmap index to tei conversion */
#define CVG_BM_TO_TEI(bm) ((tei_t)((bm) - 1))
/* tei to bitmap index conversion */
#define CVG_TEI_TO_BM(tei) ((tei) + 1)
/* tei map definition */
#define CVG_TEI_MAP_BYTE_LEN ((PLC_TEI_MAX_NUM + 8) / 8)
#pragma pack(push) /* save the pack status */
#pragma pack(1) /* 1 byte align */
typedef struct _cvg_tei_map {
uint8_t map[CVG_TEI_MAP_BYTE_LEN];
} cvg_tei_map_t;
#pragma pack(pop) /* restore the pack status */
#define cvg_tei_map_reset(bm) os_mem_set((bm), 0, sizeof(*(bm)))
#define cvg_tei_map_ffs(bm) iot_bitmap_ffs((bm)->map, sizeof(*(bm)))
#define cvg_tei_map_fls(bm) iot_bitmap_fls((bm)->map, sizeof(*(bm)))
#define cvg_tei_map_ffs_and_c(bm) iot_bitmap_ffs_and_c((bm)->map, sizeof(*(bm)))
#define cvg_tei_map_ffs_from(bm, sp) \
iot_bitmap_ffs_from((bm)->map, sizeof(*(bm)), (sp))
static inline uint32_t cvg_tei_map_ffz(cvg_tei_map_t *bm)
{
uint32_t ret;
/* always skip the first bit as it's invalid */
bm->map[0] |= 0x01;
ret = iot_bitmap_ffz(bm->map, sizeof(*bm));
bm->map[0] &= ~0x01;
return ret;
}
static inline uint32_t cvg_tei_map_ffz_from(cvg_tei_map_t *bm,
uint16_t bm_size, uint32_t sp)
{
uint32_t ret;
/* always skip the first bit as it's invalid */
bm->map[0] |= 0x01;
ret = iot_bitmap_ffz_from(bm->map, bm_size, sp);
bm->map[0] &= ~0x01;
return ret;
}
static inline uint32_t cvg_tei_map_ffz_and_s(cvg_tei_map_t *bm)
{
uint32_t ret;
/* always skip the first bit as it's invalid */
bm->map[0] |= 0x01;
ret = iot_bitmap_ffz_and_s(bm->map, sizeof(*bm));
bm->map[0] &= ~0x01;
return ret;
}
#define cvg_tei_map_set(bm, bit) \
iot_bitmap_set((bm)->map, sizeof(*(bm)), (bit))
#define cvg_tei_map_is_set(bm, bit) \
iot_bitmap_is_set((bm)->map, sizeof(*(bm)), (bit))
#define cvg_tei_map_clear(bm, bit) \
iot_bitmap_clear((bm)->map, sizeof(*(bm)), (bit))
#define cvg_tei_map_cbs(bm) iot_bitmap_cbs((bm)->map, sizeof(*(bm)))
static inline uint32_t cvg_tei_map_cbz(cvg_tei_map_t *bm)
{
uint32_t ret;
/* always skip the first bit as it's invalid */
bm->map[0] |= 0x01;
ret = iot_bitmap_cbz(bm->map, sizeof(*bm));
bm->map[0] &= ~0x01;
return ret;
}
/*
* cvg_tei_map_merge() - merge bm2 to bm1.
* @bm1: bitmap to be merged with bm2 and store the result
* @bm2: bitmap to be merged to bm1
*/
static inline void cvg_tei_map_merge(cvg_tei_map_t *bm1,
const cvg_tei_map_t *bm2)
{
for (uint16_t i = 0; i < CVG_TEI_MAP_BYTE_LEN; i++) {
bm1->map[i] |= bm2->map[i];
}
}
/*
* cvg_tei_map_exclude() - exclude bm2 from bm1.
* @bm1: bitmap to be excluded with bm2 and store the result
* @bm2: bitmap to be excluded from bm1
*/
static inline void cvg_tei_map_exclude(cvg_tei_map_t *bm1,
const cvg_tei_map_t *bm2)
{
for (uint16_t i = 0; i < CVG_TEI_MAP_BYTE_LEN; i++) {
bm1->map[i] &= ~(bm2->map[i]);
}
}
/*
* CVG_TEI_MAP_LOOP_BEGIN()
* CVG_TEI_MAP_LOOP_END()
*
* macro to go through the tei set to 1 in the bitmap.
* @bm: pointer to bitmap
* @tei: the tei_t variable to save the tei info of each loop
*/
#define CVG_TEI_MAP_LOOP_BEGIN(bm, tei) \
do { \
uint32_t __b; \
uint32_t __idx = cvg_tei_map_ffs(bm); \
if (__idx == 0) \
break; \
tei = CVG_BM_TO_TEI(__idx); \
__b = 1 << (tei & 0x07); \
__idx = tei >> 3; \
for (; __idx < CVG_TEI_MAP_BYTE_LEN; __idx++) { \
if ((bm)->map[__idx] == 0) { \
(tei) += 8; \
continue; \
} \
while (__b < 0x100) { \
if ((bm)->map[__idx] & __b) { \
#define CVG_TEI_MAP_LOOP_END(bm, tei) \
} \
(tei)++; \
__b <<= 1; \
} \
__b = 1; \
} \
} while (0);
#ifdef __cplusplus
}
#endif
#endif /* CVG_BITMAP_H */