yu.dong | c33b307 | 2024-08-21 23:14:49 -0700 | [diff] [blame^] | 1 | /***************************************************************************** |
| 2 | * Copyright Statement: |
| 3 | * -------------------- |
| 4 | * This software is protected by Copyright and the information contained |
| 5 | * herein is confidential. The software may not be copied and the information |
| 6 | * contained herein may not be used or disclosed except with the written |
| 7 | * permission of MediaTek Inc. (C) 2005 |
| 8 | * |
| 9 | * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| 10 | * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") |
| 11 | * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON |
| 12 | * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| 13 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| 15 | * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| 16 | * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| 17 | * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH |
| 18 | * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO |
| 19 | * NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S |
| 20 | * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM. |
| 21 | * |
| 22 | * BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE |
| 23 | * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| 24 | * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| 25 | * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO |
| 26 | * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| 27 | * |
| 28 | * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE |
| 29 | * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF |
| 30 | * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND |
| 31 | * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER |
| 32 | * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC). |
| 33 | * |
| 34 | *****************************************************************************/ |
| 35 | |
| 36 | /***************************************************************************** |
| 37 | * |
| 38 | * Filename: |
| 39 | * --------- |
| 40 | * bitop_macros.h |
| 41 | * |
| 42 | * Project: |
| 43 | * -------- |
| 44 | * Maui_Software |
| 45 | * |
| 46 | * Description: |
| 47 | * ------------ |
| 48 | * Preprocessor macros for putting and getting bit fields into |
| 49 | * byte arrays. |
| 50 | * |
| 51 | * Author: |
| 52 | * ------- |
| 53 | * ------- |
| 54 | * |
| 55 | *============================================================================ |
| 56 | * HISTORY |
| 57 | * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!! |
| 58 | *------------------------------------------------------------------------------ |
| 59 | * removed! |
| 60 | * removed! |
| 61 | * removed! |
| 62 | * |
| 63 | * removed! |
| 64 | * removed! |
| 65 | * removed! |
| 66 | * |
| 67 | * removed! |
| 68 | * removed! |
| 69 | * removed! |
| 70 | * |
| 71 | * removed! |
| 72 | * removed! |
| 73 | * removed! |
| 74 | * removed! |
| 75 | * removed! |
| 76 | * removed! |
| 77 | * removed! |
| 78 | * removed! |
| 79 | * removed! |
| 80 | * removed! |
| 81 | * removed! |
| 82 | * removed! |
| 83 | * removed! |
| 84 | *------------------------------------------------------------------------------ |
| 85 | * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!! |
| 86 | *============================================================================ |
| 87 | ****************************************************************************/ |
| 88 | |
| 89 | /********************************************************************* |
| 90 | (C) _____ (year of first publication) Sasken communication |
| 91 | Technologies Limited, All rights reserved. |
| 92 | * This file provides a template for .c files. This space |
| 93 | * should be used to describe the file contents |
| 94 | * Component-specific prefix : xxxx |
| 95 | *********************************************************************/ |
| 96 | |
| 97 | #ifndef _BITOP_MACROS_H |
| 98 | #define _BITOP_MACROS_H |
| 99 | |
| 100 | /*************************************************************************** |
| 101 | * Macros With Parameters |
| 102 | ***************************************************************************/ |
| 103 | |
| 104 | #define MASK(_w) ( ((_w)>31) ? 0xffffffff : (0x1u << (_w)) - 1 ) |
| 105 | |
| 106 | |
| 107 | /* _uc is the pointer to the byte stream from which bits to be read */ |
| 108 | /* _s is the offset in bits from MSB of the byte stream _uc */ |
| 109 | /* _w is the number of bits to be read */ |
| 110 | |
| 111 | #define GET_BITS_1_8(_uc, _s, _w) ( \ |
| 112 | (((_s)+(_w)>8) ? *(_uc) << ((_s)+(_w)-8) | \ |
| 113 | *((_uc)+1) >> (16-(_s)-(_w)) : \ |
| 114 | *(_uc) << (_s) >> (8-(_w))) \ |
| 115 | & MASK(_w) ) |
| 116 | |
| 117 | #define GET_BITS_9_16(_uc, _s, _w) ( \ |
| 118 | ((_s+_w>16) ? *_uc << (_s+_w-8) | *(_uc+1) << (_s+_w-16) | \ |
| 119 | *(_uc+2) >> (24-_s-_w) : \ |
| 120 | *_uc << (_s+_w-8) | *(_uc+1) >> (16-_s-_w)) \ |
| 121 | & MASK(_w)) |
| 122 | |
| 123 | #define GET_BITS_17_24(_uc, _s, _w) ( \ |
| 124 | (((_s)+(_w)>24) ? *(_uc) << ((_s)+(_w)-8) | \ |
| 125 | *((_uc)+1) << ((_s)+(_w)-16) | \ |
| 126 | *((_uc)+2) << ((_s)+(_w)-24) | *((_uc)+3) >> (32-(_s)-(_w)) : \ |
| 127 | *(_uc) << ((_s)+(_w)-8) | *((_uc)+1) << ((_s)+(_w)-16) | \ |
| 128 | *((_uc)+2) >> (24-(_s)-(_w))) \ |
| 129 | & MASK(_w)) |
| 130 | |
| 131 | #define GET_BITS_25_32(_uc, _s, _w) ( \ |
| 132 | (((_s)+(_w)>32) ? *(_uc) << ((_s)+(_w)-8) | \ |
| 133 | *((_uc)+1) << ((_s)+(_w)-16) | \ |
| 134 | *((_uc)+2) << ((_s)+(_w)-24) | *((_uc)+3) << ((_s)+(_w)-32) | \ |
| 135 | *((_uc)+4) >> (40-(_s)-(_w)) : *(_uc) << ((_s)+(_w)-8) | \ |
| 136 | *((_uc)+1) << ((_s)+(_w)-16) | *((_uc)+2) << ((_s)+(_w)-24) | \ |
| 137 | *((_uc)+3) >> (32-(_s)-(_w))) & MASK(_w)) |
| 138 | |
| 139 | #define GET_BITS(_uc, _s, _w) ( \ |
| 140 | ((_w)>8) ? (((_w)>16) ? (((_w)>24) ? \ |
| 141 | GET_BITS_25_32((_uc), (_s), (_w)) : \ |
| 142 | GET_BITS_17_24((_uc), (_s), (_w))) : \ |
| 143 | GET_BITS_9_16((_uc), (_s), (_w))) : \ |
| 144 | GET_BITS_1_8((_uc), (_s), (_w)) \ |
| 145 | ) |
| 146 | |
| 147 | /* _uc is the pointer to the byte stream to which bits to be written */ |
| 148 | /* _s is the offset in bits from MSB of the byte stream _uc */ |
| 149 | /* _w is the number of bits to be written */ |
| 150 | /* _i is the value to be written */ |
| 151 | |
| 152 | #define PUT_BITS_1_8(_uc, _s, _w, _i) \ |
| 153 | if ((_s)+(_w)>8) { \ |
| 154 | *(_uc) &= ~ MASK(8-(_s)); \ |
| 155 | *(_uc) |= ((_i) & MASK((_w))) >> ((_s)+(_w)- 8); \ |
| 156 | *((_uc)+1) = (*((_uc)+1) & MASK(16-(_s)-(_w))) | \ |
| 157 | (_i) << (16-(_s)-(_w)); \ |
| 158 | } else { \ |
| 159 | *(_uc) &= ~ (MASK((_w)) << ( 8-(_w)-(_s))); \ |
| 160 | *(_uc) |= ((_i) & MASK((_w))) << ( 8-(_w)-(_s)); \ |
| 161 | } |
| 162 | |
| 163 | #define PUT_BITS_9_16(_uc, _s, _w, _i) \ |
| 164 | *(_uc) &= ~ MASK(8-(_s)); \ |
| 165 | if ((_s)+(_w)>16) { \ |
| 166 | *(_uc) |= ((_i) & MASK((_w))) >> ((_s)+(_w)- 8); \ |
| 167 | *((_uc)+1) = (_i) >> ((_s)+(_w)-16); \ |
| 168 | *((_uc)+2) = (*((_uc)+2) & MASK(24-(_s)-(_w))) | \ |
| 169 | (_i) << (24-(_s)-(_w)); \ |
| 170 | } else { \ |
| 171 | *(_uc) |= ((_i) & MASK((_w))) >> ((_s)+(_w)- 8); \ |
| 172 | *((_uc)+1) = (*((_uc)+1) & MASK(16-(_s)-(_w))) | \ |
| 173 | (_i) << (16-(_s)-(_w)); \ |
| 174 | } |
| 175 | |
| 176 | #define PUT_BITS_17_24(_uc, _s, _w, _i) \ |
| 177 | *(_uc) &= ~ MASK(8-(_s)); \ |
| 178 | if ((_s)+(_w)>24) { \ |
| 179 | *(_uc) |= ((_i) & MASK((_w))) >> ((_s)+(_w)- 8); \ |
| 180 | *((_uc)+1) = (_i) >> ((_s)+(_w)-16); \ |
| 181 | *((_uc)+2) = (_i) >> ((_s)+(_w)-24); \ |
| 182 | *((_uc)+3) = (*((_uc)+3) & MASK(32-(_s)-(_w))) | \ |
| 183 | (_i) << (32-(_s)-(_w)); \ |
| 184 | } else { \ |
| 185 | *(_uc) |= ((_i) & MASK((_w))) >> ((_s)+(_w)- 8); \ |
| 186 | *((_uc)+1) = (_i) >> ((_s)+(_w)-16); \ |
| 187 | *((_uc)+2) = (*((_uc)+2) & MASK(24-(_s)-(_w))) | \ |
| 188 | (_i) << (24-(_s)-(_w)); \ |
| 189 | } |
| 190 | |
| 191 | #define PUT_BITS_25_32(_uc, _s, _w, _i) \ |
| 192 | *(_uc) &= ~MASK(8-(_s)); \ |
| 193 | if ((_s)+(_w)>32) { \ |
| 194 | *(_uc) |= ((_i) & MASK((_w))) >> ((_s)+(_w)- 8); \ |
| 195 | *((_uc)+1) = (_i) >> ((_s)+(_w)-16); \ |
| 196 | *((_uc)+2) = (_i) >> ((_s)+(_w)-24); \ |
| 197 | *((_uc)+3) = (_i) >> ((_s)+(_w)-32); \ |
| 198 | *((_uc)+4) = (*((_uc)+4) & MASK(40-(_s)-(_w))) | \ |
| 199 | (_i) << (40-(_s)-(_w)); \ |
| 200 | } else { \ |
| 201 | *(_uc) |= ((_i) & MASK((_w))) >> ((_s)+(_w)- 8); \ |
| 202 | *((_uc)+1) = (_i) >> ((_s)+(_w)-16); \ |
| 203 | *((_uc)+2) = (_i) >> ((_s)+(_w)-24); \ |
| 204 | *((_uc)+3) = (*((_uc)+3) & MASK(32-(_s)-(_w))) | \ |
| 205 | (_i) << (32-(_s)-(_w)); \ |
| 206 | } |
| 207 | |
| 208 | #define PUT_BITS(_uc, _s, _w, _i) \ |
| 209 | if ((_w)<9) { \ |
| 210 | PUT_BITS_1_8(_uc, _s, _w, _i) \ |
| 211 | } else if ((_w)<17) { \ |
| 212 | PUT_BITS_9_16(_uc, _s, _w, _i) \ |
| 213 | } else if ((_w)<25) { \ |
| 214 | PUT_BITS_17_24(_uc, _s, _w, _i) \ |
| 215 | } else { \ |
| 216 | PUT_BITS_25_32(_uc, _s, _w, _i) \ |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /********************************************************************/ |
| 221 | /* This macro insert a given number of bits to a bit position |
| 222 | * by shifting the existing bits down. */ |
| 223 | |
| 224 | /* ptr is the pointer to the byte stream |
| 225 | * to which bits to be inserted. */ |
| 226 | /* _offset is the offset in bytes from MSB of byte stream ptr. */ |
| 227 | /* length is the number of bits to be inserted. */ |
| 228 | /* _size is the number of bits in the bit stream, after the point |
| 229 | * of insertion ,which is to be shifted. */ |
| 230 | /* _value is the value to be inserted. */ |
| 231 | |
| 232 | |
| 233 | #define INSERT_BITS(ptr,_offset,length,_size,_value) \ |
| 234 | { \ |
| 235 | unsigned int temp,value =_value; \ |
| 236 | unsigned short int offset=_offset,size=_size; \ |
| 237 | while(length <= size) { \ |
| 238 | temp = GET_BITS(ptr,offset,length); \ |
| 239 | PUT_BITS(ptr,offset,length,value); \ |
| 240 | ptr = ptr +((offset+length)/8); \ |
| 241 | offset = (offset+length)%8; \ |
| 242 | size -=length; \ |
| 243 | value = temp; \ |
| 244 | } \ |
| 245 | temp = GET_BITS(ptr,offset,size); \ |
| 246 | PUT_BITS(ptr,offset,length,value); \ |
| 247 | ptr = ptr +(offset+length)/8; \ |
| 248 | offset = (offset+length)%8; \ |
| 249 | PUT_BITS(ptr,offset,size,temp); \ |
| 250 | } |
| 251 | |
| 252 | /* Conversions for length fields */ |
| 253 | /* BITS_TO_BYTES_INC converts a length in bits, to a length in bytes, */ |
| 254 | /* inclusive of remainder (i.e. rounding up) */ |
| 255 | /* BITS_TO_BYTES_EXC converts a length in bits, to a length in bytes, */ |
| 256 | /* exclusive of remainder (i.e. rounding down) */ |
| 257 | /* These macros work for all types of integer */ |
| 258 | /* but beware sufficient headroom on BYTES_TO_BITS */ |
| 259 | #define BITS_TO_BYTES_INC(bit_length) (((bit_length)+7)/8) |
| 260 | #define BITS_TO_BYTES_EXC(bit_length) ((bit_length)/8) |
| 261 | #define BYTES_TO_BITS(length) ((length)*8) |
| 262 | |
| 263 | /* Similar to round up to fit U32s */ |
| 264 | /* for buffer size allocation etc. */ |
| 265 | #define BITS_TO_U32S_INC(bit_length) (((bit_length)+31)/32) |
| 266 | #define BYTES_TO_U32S_INC(length) (((length)+3)/4) |
| 267 | |
| 268 | #endif /* _BITOP_MACROS_H */ |
| 269 | |
| 270 | |