1 /****************************************************************************** 2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 3 * All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *****************************************************************************/ 18 #pragma once 19 20 #include "macro_trick.h" 21 22 #define BIT(n) (1 << (n)) 23 24 // BITSx are internal used macro, please use BITS instead 25 #define BITS1(a) BIT(a) 26 #define BITS2(a, b) (BIT(a) | BIT(b)) 27 #define BITS3(a, b, c) (BIT(a) | BIT(b) | BIT(c)) 28 #define BITS4(a, b, c, d) (BIT(a) | BIT(b) | BIT(c) | BIT(d)) 29 #define BITS5(a, b, c, d, e) (BIT(a) | BIT(b) | BIT(c) | BIT(d) | BIT(e)) 30 #define BITS6(a, b, c, d, e, f) (BIT(a) | BIT(b) | BIT(c) | BIT(d) | BIT(e) | BIT(f)) 31 #define BITS7(a, b, c, d, e, f, g) (BIT(a) | BIT(b) | BIT(c) | BIT(d) | BIT(e) | BIT(f) | BIT(g)) 32 #define BITS8(a, b, c, d, e, f, g, h) (BIT(a) | BIT(b) | BIT(c) | BIT(d) | BIT(e) | BIT(f) | BIT(g) | BIT(h)) 33 34 #define BITS(...) VARARG(BITS, __VA_ARGS__) 35 36 // bits range: BITS_RNG(4, 5) 0b000111110000, start from 4, length = 5 37 #define BIT_RNG(s, e) (BIT_MASK_LEN((e) - (s) + 1) << (s)) 38 39 #define BM_MASK_V(x, mask) ((x) | (mask)) 40 #define BM_CLR_MASK_V(x, mask) ((x) & ~(mask)) 41 42 #define BM_SET(x, mask) ((x) |= (mask)) 43 #define BM_CLR(x, mask) ((x) &= ~(mask)) 44 #define BM_IS_SET(x, mask) ((x) & (mask)) 45 #define BM_IS_CLR(x, mask) ((~(x)) & (mask)) 46 #define BM_FLIP(x, mask) ((x) ^= (mask)) 47 48 // !!!! v is already a masked value, no need to shift 49 #define BM_MASK_VAL(x, mask, v) (((x) & ~(mask)) | (v)) 50 #define BM_SET_MASK_VAL(x, mask, v) ((x) = BM_MASK_VAL(x, mask, v)) 51 52 #define BIT_SET(x, n) ((x) |= BIT(n)) 53 #define BIT_CLR(x, n) ((x) &= ~BIT(n)) 54 #define BIT_IS_SET(x, n) ((x)&BIT(n)) 55 #define BIT_FLIP(x, n) ((x) ^= BIT(n)) 56 #define BIT_SET_HIGH(x) ((x) |= BIT((sizeof((x)) * 8 - 1))) // set the highest bit 57 #define BIT_CLR_HIGH(x) ((x) &= ~BIT((sizeof((x)) * 8 - 1))) // clr the highest bit 58 #define BIT_IS_SET_HIGH(x) ((x)&BIT((sizeof((x)) * 8 - 1))) // check the higest bit 59 60 #define BIT_MASK_LEN(len) (BIT(len) - 1) 61 #define BIT_MASK(start, len) (BIT_MASK_LEN(len) << (start)) 62 63 // ! Prepare a bitmask for insertion or combining. 64 #define BIT_PREP(x, start, len) ((x)&BIT_MASK(start, len)) 65 66 // ! Extract a bitfield of length \a len starting at bit \a start from \a y. 67 #define BIT_GET(x, start, len) (((x) >> (start)) & BIT_MASK_LEN(len)) 68 #define BIT_GET_LOW(x, len) ((x)&BIT_MASK_LEN(len)) 69 70 // ! Insert a new bitfield value \a x into \a y. 71 #define BIT_MERGE(y, x, start, len) (y = ((y) & ~BIT_MASK(start, len)) | BIT_PREP(x, start, len)) 72 73 #define BIT_IS_EVEN(x) (((x)&1) == 0) 74 #define BIT_IS_ODD(x) (!BIT_IS_EVEN((x))) 75 #define BIT_IS_POW2(x) (!((x) & ((x)-1))) 76 #define BIT_TURNOFF_1(x) ((x) &= ((x)-1)) 77 #define BIT_ISOLATE_1(x) ((x) &= (-(x))) 78 #define BIT_PROPAGATE_1(x) ((x) |= ((x)-1)) 79 #define BIT_ISOLATE_0(x) ((x) = ~(x) & ((x) + 1)) 80 #define BIT_TURNON_0(x) ((x) |= ((x) + 1)) 81 #define CLAMP_TO_ONE(x) (!!(x)) // compiler defined, not stardard. 0 --> 0, 1 --> 0xffffffff 82 83 #define ONES(x) BIT_MASK_LEN(x) 84 #define ONES_32 0xffffffff 85 #define ALL_SET 0xffffffff 86 87 // Return the bit index of the lowest 1 in y. ex: 0b00110111000 --> 3 88 #define BIT_LOW_BIT(y) (((y) & BIT(0))?0:(((y) & BIT(1))?1:(((y) & BIT(2))?2:(((y) & BIT(3))?3: \ 89 (((y) & BIT(4))?4:(((y) & BIT(5))?5:(((y) & BIT(6))?6:(((y) & BIT(7))?7: \ 90 (((y) & BIT(8))?8:(((y) & BIT(9))?9:(((y) & BIT(10))?10:(((y) & BIT(11))?11: \ 91 (((y) & BIT(12))?12:(((y) & BIT(13))?13:(((y) & BIT(14))?14:(((y) & BIT(15))?15: \ 92 (((y) & BIT(16))?16:(((y) & BIT(17))?17:(((y) & BIT(18))?18:(((y) & BIT(19))?19: \ 93 (((y) & BIT(20))?20:(((y) & BIT(21))?21:(((y) & BIT(22))?22:(((y) & BIT(23))?23: \ 94 (((y) & BIT(24))?24:(((y) & BIT(25))?25:(((y) & BIT(26))?26:(((y) & BIT(27))?27: \ 95 (((y) & BIT(28))?28:(((y) & BIT(29))?29:(((y) & BIT(30))?30:(((y) & BIT(31))?31:32 \ 96 )))))))))))))))))))))))))))))))) 97 98 // Return the bit index of the highest 1 in (y). ex: 0b00110111000 --> 8 99 #define BIT_HIGH_BIT(y) (((y) & BIT(31))?31:(((y) & BIT(30))?30:(((y) & BIT(29))?29:(((y) & BIT(28))?28: \ 100 (((y) & BIT(27))?27:(((y) & BIT(26))?26:(((y) & BIT(25))?25:(((y) & BIT(24))?24: \ 101 (((y) & BIT(23))?23:(((y) & BIT(22))?22:(((y) & BIT(21))?21:(((y) & BIT(20))?20: \ 102 (((y) & BIT(19))?19:(((y) & BIT(18))?18:(((y) & BIT(17))?17:(((y) & BIT(16))?16: \ 103 (((y) & BIT(15))?15:(((y) & BIT(14))?14:(((y) & BIT(13))?13:(((y) & BIT(12))?12: \ 104 (((y) & BIT(11))?11:(((y) & BIT(10))?10:(((y) & BIT(9))?9:(((y) & BIT(8))?8: \ 105 (((y) & BIT(7))?7:(((y) & BIT(6))?6:(((y) & BIT(5))?5:(((y) & BIT(4))?4: \ 106 (((y) & BIT(3))?3:(((y) & BIT(2))?2:(((y) & BIT(1))?1:(((y) & BIT(0))?0:32 \ 107 )))))))))))))))))))))))))))))))) 108 109 #define BM_MASK_FLD(x, mask) (((x) & (mask)) >> BIT_LOW_BIT(mask)) 110 #define BM_SET_MASK_FLD(x, mask, v) ((x) = BM_MASK_VAL(x, mask, v)) 111 112 ////////////////////// 113 #define MV(m, v) (((v) << BIT_LOW_BIT(m)) & (m)) 114 115 // warning MASK_VALn are internal used macro, please use MASK_VAL instead 116 #define MASK_VAL2(m, v) (MV(m, v)) 117 #define MASK_VAL4(m1, v1, m2, v2) (MV(m1, v1) | MV(m2, v2)) 118 #define MASK_VAL6(m1, v1, m2, v2, m3, v3) (MV(m1, v1) | MV(m2, v2) | MV(m3, v3)) 119 #define MASK_VAL8(m1, v1, m2, v2, m3, v3, m4, v4) (MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4)) 120 #define MASK_VAL10(m1, v1, m2, v2, m3, v3, m4, v4, m5, v5) \ 121 (MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4) | MV(m5, v5)) 122 #define MASK_VAL12(m1, v1, m2, v2, m3, v3, m4, v4, m5, v5, m6, v6) \ 123 (MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4) | MV(m5, v5) | MV(m6, v6)) 124 #define MASK_VAL14(m1, v1, m2, v2, m3, v3, m4, v4, m5, v5, m6, v6, m7, v7) \ 125 (MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4) | MV(m5, v5) | MV(m6, v6) | MV(m7, v7)) 126 #define MASK_VAL16(m1, v1, m2, v2, m3, v3, m4, v4, m5, v5, m6, v6, m7, v7, m8, v8) \ 127 (MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4) | MV(m5, v5) | MV(m6, v6) | MV(m7, v7) | MV(m8, v8)) 128 129 #define MASK_VAL(...) VARARG(MASK_VAL, __VA_ARGS__) 130 131 #define FLD_MASK_VAL(x, mask, v) BM_MASK_VAL(x, mask, MV(mask, v)) 132 133 #define SET_FLD(x, mask) BM_SET(x, mask) 134 #define CLR_FLD(x, mask) BM_CLR(x, mask) 135 #define FLIP_FLD(x, mask) BM_FLIP(x, mask) 136 137 #define GET_FLD(x, mask) BM_MASK_FLD(x, mask) 138 139 #define SET_FLD_V(...) VARARG(SET_FLD_V, __VA_ARGS__) 140 141 #define SET_FLD_FULL_V3(x, m, v) ((x) = MASK_VAL2(m, v)) 142 #define SET_FLD_FULL_V5(x, m1, v1, m2, v2) ((x) = MASK_VAL4(m1, v1, m2, v2)) 143 #define SET_FLD_FULL_V7(x, m1, v1, m2, v2, m3, v3) ((x) = MASK_VAL6(m1, v1, m2, v2, m3, v3)) 144 #define SET_FLD_FULL_V9(x, m1, v1, m2, v2, m3, v3, m4, v4) ((x) = MASK_VAL8(m1, v1, m2, v2, m3, v3, m4, v4)) 145 #define SET_FLD_FULL_V11(x, m1, v1, m2, v2, m3, v3, m4, v4, m5, v5) \ 146 ((x) = MASK_VAL10(m1, v1, m2, v2, m3, v3, m4, v4, m5, v5)) 147 #define SET_FLD_FULL_V13(x, m1, v1, m2, v2, m3, v3, m4, v4, m5, v5, m6, v6) \ 148 ((x) = MASK_VAL12(m1, v1, m2, v2, m3, v3, m4, v4, m5, v5, m6, v6)) 149 #define SET_FLD_FULL_V(...) VARARG(SET_FLD_FULL_V, __VA_ARGS__) 150 151 //////////////////////////////////////////////////////////////////////// 152 #define BIT8_IFY(y) \ 153 ((((y) & 0x0000000FLU) ? 1 : 0) + (((y) & 0x000000F0LU) ? 2 : 0) + (((y) & 0x00000F00LU) ? 4 : 0) + \ 154 (((y) & 0x0000F000LU) ? 8 : 0) + (((y) & 0x000F0000LU) ? 16 : 0) + (((y) & 0x00F00000LU) ? 32 : 0) + \ 155 (((y) & 0x0F000000LU) ? 64 : 0) + (((y) & 0xF0000000LU) ? 128 : 0)) 156 157 #define HEX_X(i) (0x##i##LU) 158 159 #define BIT_8(j) ((unsigned char)BIT8_IFY(HEX_X(j))) 160 161 #ifndef WIN32 162 // warning SET_FLD_Vn are internal used macro, please use SET_FLD_V instead 163 #define SET_FLD_V3(x, m, v) BM_SET_MASK_FLD(x, m, MV(m, v)) 164 165 #define SET_FLD_V5(x, m1, v1, m2, v2) BM_SET_MASK_FLD(x, (m1) | (m2), MV(m1, v1) | MV(m2, v2)) 166 167 #define SET_FLD_V7(x, m1, v1, m2, v2, m3, v3) \ 168 BM_SET_MASK_FLD(x, (m1) | (m2) | (m3), MV(m1, v1) | MV(m2, v2) | MV(m3, v3)) 169 170 #define SET_FLD_V9(x, m1, v1, m2, v2, m3, v3, m4, v4) \ 171 BM_SET_MASK_FLD(x, (m1) | (m2) | (m3) | (m4), MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4)) 172 173 #define SET_FLD_V11(x, m1, v1, m2, v2, m3, v3, m4, v4, m5, v5) \ 174 BM_SET_MASK_FLD(x, (m1) | (m2) | (m3) | (m4) | (m5), \ 175 MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4) | MV(m5, v5)) 176 177 #define SET_FLD_V13(x, m1, v1, m2, v2, m3, v3, m4, v4, m5, v5, m6, v6) \ 178 BM_SET_MASK_FLD(x, (m1) | (m2) | (m3) | (m4) | (m5) | (m6), \ 179 MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4) | MV(m5, v5) | MV(m6, v6)) 180 #else 181 #define SET_FLD_V3(x, m, v) \ 182 __pragma(warning(push)) __pragma(warning(disable : 4244)) BM_SET_MASK_FLD(x, m, MV(m, v)) __pragma(warning(pop)) 183 184 #define SET_FLD_V5(x, m1, v1, m2, v2) \ 185 __pragma(warning(push)) __pragma(warning(disable : 4244)) \ 186 BM_SET_MASK_FLD(x, (m1) | (m2), MV(m1, v1) | MV(m2, v2)) __pragma(warning(pop)) 187 188 #define SET_FLD_V7(x, m1, v1, m2, v2, m3, v3) \ 189 __pragma(warning(push)) __pragma(warning(disable : 4244)) \ 190 BM_SET_MASK_FLD(x, (m1) | (m2) | (m3), MV(m1, v1) | MV(m2, v2) | MV(m3, v3)) __pragma(warning(pop)) 191 192 #define SET_FLD_V9(x, m1, v1, m2, v2, m3, v3, m4, v4) \ 193 __pragma(warning(push)) __pragma(warning(disable : 4244)) \ 194 BM_SET_MASK_FLD(x, (m1) | (m2) | (m3) | (m4), MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4)) \ 195 __pragma(warning(pop)) 196 197 #define SET_FLD_V11(x, m1, v1, m2, v2, m3, v3, m4, v4, m5, v5) \ 198 __pragma(warning(push)) __pragma(warning(disable : 4244)) \ 199 BM_SET_MASK_FLD(x, (m1) | (m2) | (m3) | (m4) | (m5), \ 200 MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4) | MV(m5, v5)) __pragma(warning(pop)) 201 202 #define SET_FLD_V13(x, m1, v1, m2, v2, m3, v3, m4, v4, m5, v5, m6, v6) \ 203 __pragma(warning(push)) __pragma(warning(disable : 4244)) \ 204 BM_SET_MASK_FLD(x, (m1) | (m2) | (m3) | (m4) | (m5) | (m6), \ 205 MV(m1, v1) | MV(m2, v2) | MV(m3, v3) | MV(m4, v4) | MV(m5, v5) | MV(m6, v6)) \ 206 __pragma(warning(pop)) 207 #endif