| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2006-2007, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: biobits.h $ |
|---|
| 11 | * $brcm_Revision: 3 $ |
|---|
| 12 | * $brcm_Date: 7/9/07 3:53p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Bit stream utility |
|---|
| 17 | * |
|---|
| 18 | * Revision History: |
|---|
| 19 | * |
|---|
| 20 | * $brcm_Log: /BSEAV/lib/utils/biobits.h $ |
|---|
| 21 | * |
|---|
| 22 | * 3 7/9/07 3:53p vsilyaev |
|---|
| 23 | * PR 32846: Improved documentation |
|---|
| 24 | * |
|---|
| 25 | * 2 10/10/06 12:16p vsilyaev |
|---|
| 26 | * PR 24844: Added bit extraction routines |
|---|
| 27 | * |
|---|
| 28 | * 1 7/28/06 4:09p vsilyaev |
|---|
| 29 | * PR 22578: Bitstream utility |
|---|
| 30 | * |
|---|
| 31 | * |
|---|
| 32 | *******************************************************************************/ |
|---|
| 33 | #ifndef _BIOBITS_H__ |
|---|
| 34 | #define _BIOBITS_H__ |
|---|
| 35 | |
|---|
| 36 | #include "biovec.h" |
|---|
| 37 | |
|---|
| 38 | #ifdef __cplusplus |
|---|
| 39 | extern "C" |
|---|
| 40 | { |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | /* low level bit manipulation */ |
|---|
| 44 | /* |
|---|
| 45 | Summary: |
|---|
| 46 | Returns bit 'b' from word 'w', |
|---|
| 47 | |
|---|
| 48 | Example: |
|---|
| 49 | B_GET_BIT(0xDE,1)==0 |
|---|
| 50 | B_GET_BIT(0xDE,7)!=0 |
|---|
| 51 | */ |
|---|
| 52 | #define B_GET_BIT(w,b) ((w)&(1<<(b))) |
|---|
| 53 | |
|---|
| 54 | /* |
|---|
| 55 | Summary: |
|---|
| 56 | Returns bits 'e'..'b' from word 'w', |
|---|
| 57 | |
|---|
| 58 | Example: |
|---|
| 59 | B_GET_BITS(0xDE,7,4)==0x0D |
|---|
| 60 | B_GET_BITS(0xDE,3,0)=0x0E |
|---|
| 61 | */ |
|---|
| 62 | #define B_GET_BITS(w,e,b) (((w)>>(b))&(((unsigned)(-1))>>((sizeof(unsigned))*8-(e+1-b)))) |
|---|
| 63 | |
|---|
| 64 | /* |
|---|
| 65 | Summary: |
|---|
| 66 | Sets bit 'b 'to value of 'v', 'name' is ignored (used as inline comment). |
|---|
| 67 | 'v' is converted to bool (0/1) before setting bit, e.g. v with values of 1 or 1000 would generate the same result |
|---|
| 68 | |
|---|
| 69 | Example: |
|---|
| 70 | B_SET_BIT("example 1",1,0)==1 |
|---|
| 71 | B_SET_BIT("example 2",1,2)==4 |
|---|
| 72 | B_SET_BIT("example 3",100,2)==4 |
|---|
| 73 | */ |
|---|
| 74 | |
|---|
| 75 | #define B_SET_BIT(name,v,b) (((unsigned)((v)!=0)<<(b))) |
|---|
| 76 | |
|---|
| 77 | /* |
|---|
| 78 | Summary: |
|---|
| 79 | Sets bits 'e'..'b 'to value of 'v', 'name' is ignored (used as inline comment). |
|---|
| 80 | 'v' is not truncated to 'e'-'b', e.g. if v has more than 'e'-'b'+1 bits result is undefined |
|---|
| 81 | |
|---|
| 82 | */ |
|---|
| 83 | #define B_SET_BITS(name,v,e,b) (((unsigned)(v))<<(b)) |
|---|
| 84 | |
|---|
| 85 | /* |
|---|
| 86 | Summary: |
|---|
| 87 | Sets bit 'b'...'e' to value of 'v', name is ignored (used as inline comment). |
|---|
| 88 | 'v' is not truncated, e.g. it's the user responsibilty that it would feet into the given number of bits |
|---|
| 89 | |
|---|
| 90 | Example: |
|---|
| 91 | B_SET_BITS("example 1",1,0,0)==1 |
|---|
| 92 | B_SET_BITS("example 2",1,1,0)==1 |
|---|
| 93 | B_SET_BITS("example 3",0x55,7,1)==0xAA |
|---|
| 94 | */ |
|---|
| 95 | |
|---|
| 96 | #define B_SET_BITS(name,v,e,b) (((unsigned)(v))<<(b)) |
|---|
| 97 | |
|---|
| 98 | /* this type is used to define bitstrem */ |
|---|
| 99 | typedef struct bio_bitstream { |
|---|
| 100 | bio_cursor *cursor; |
|---|
| 101 | uint32_t cache; |
|---|
| 102 | int cache_pos; |
|---|
| 103 | } bio_bitstream; |
|---|
| 104 | |
|---|
| 105 | void bio_bitstream_init(bio_bitstream *bs, bio_cursor *cursor); |
|---|
| 106 | void bio_bitstream_dump(bio_bitstream *bs); |
|---|
| 107 | bool bio_bitstream_eof(bio_bitstream *bs); |
|---|
| 108 | int bio_bitstream_show(bio_bitstream *bs); |
|---|
| 109 | int bio_bitstream_bit(bio_bitstream *bs); |
|---|
| 110 | int bio_bitstream_drop(bio_bitstream *bs); |
|---|
| 111 | unsigned bio_bitstream_bits(bio_bitstream *bs, unsigned nbits); |
|---|
| 112 | void bio_bitstream_drop_bits(bio_bitstream *bs, unsigned nbits); |
|---|
| 113 | |
|---|
| 114 | #ifdef __cplusplus |
|---|
| 115 | } |
|---|
| 116 | #endif |
|---|
| 117 | |
|---|
| 118 | #endif /* _BIOBITS_H__ */ |
|---|
| 119 | |
|---|