File: bit_pack.c

package info (click to toggle)
xtide 2.6.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,996 kB
  • ctags: 2,617
  • sloc: cpp: 26,266; ansic: 8,105; makefile: 152; yacc: 113; sh: 54; lex: 54
file content (328 lines) | stat: -rw-r--r-- 14,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* $Id: bit_pack.c,v 1.3 2002/10/03 14:10:41 flaterco Exp $ */

#include <math.h>
#include <stdio.h>

#include "nvtypes.h"


static NV_U_BYTE        mask[8] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 
                        0xfe}, notmask[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 
                        0x07, 0x03, 0x01}; 


/*****************************************************************************\

                            DISTRIBUTION STATEMENT

    This source file is unclassified, distribution unlimited, public
    domain.  It is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

\*****************************************************************************/



/***************************************************************************\ 
*                                                                           * 
*   Function        calculate_bits - Computes the number of bits needed for *
*                   a specified integer range.                              * 
*                   buffer.                                                 * 
*                                                                           * 
*   Synopsis        calculate_bits (range);                                 * 
*                                                                           * 
*                   NV_U_INT32 range        integer data range              * 
*                                                                           * 
*   Returns         NV_U_INT32              number of bits needed to store  *
*                                           data in this range              *
*                                                                           * 
*   Author          Jan C. Depner                                           * 
*                                                                           * 
\***************************************************************************/ 

NV_U_INT32 calculate_bits (NV_U_INT32 range) 
{
    NV_FLOAT64            result;
    NV_U_INT32            bits;


    result = (log10 ((NV_FLOAT64) (range)) / log10 (2.0));
    bits = (NV_U_INT32)result;

    if (fmod (result, 1.0) != 0.0) bits++;

    return (bits);
}



/***************************************************************************\ 
*                                                                           * 
*   Function        bit_pack - Packs a long value into consecutive bits in  * 
*                   buffer.                                                 * 
*                                                                           * 
*   Synopsis        bit_pack (buffer, start, numbits, value);               * 
*                                                                           * 
*                   NV_U_BYTE buffer[]      address of buffer to use        * 
*                   NV_U_INT32 start        start bit position in buffer    * 
*                   NV_U_INT32 numbits      number of bits to store         * 
*                   NV_INT32 value          value to store                  * 
*                                                                           * 
*   Description     Packs the value 'value' into 'numbits' bits in 'buffer' * 
*                   starting at bit position 'start'.  The majority of      * 
*                   this code is based on Appendix C of Naval Ocean         * 
*                   Research and Development Activity Report #236, 'Data    * 
*                   Base Structure to Support the Production of the Digital * 
*                   Bathymetric Data Base', Nov. 1989, James E. Braud,      * 
*                   John L. Breckenridge, James E. Current, Jerry L.        * 
*                   Landrum.                                                * 
*                                                                           * 
*   Returns         void                                                    * 
*                                                                           * 
*   Author          Jan C. Depner                                           * 
*                                                                           * 
\***************************************************************************/ 

void bit_pack (NV_U_BYTE buffer[], NV_U_INT32 start, NV_U_INT32 numbits, 
NV_INT32 value) 
{ 
    NV_INT32                start_byte, end_byte, start_bit, end_bit, i; 
 
 
    i = start + numbits; 
 

    /*  Right shift the start and end by 3 bits, this is the same as        */ 
    /*  dividing by 8 but is faster.  This is computing the start and end   */ 
    /*  bytes for the field.                                                */ 
     
    start_byte = start >> 3; 
    end_byte = i >> 3; 
 

    /*  AND the start and end bit positions with 7, this is the same as     */ 
    /*  doing a mod with 8 but is faster.  Here we are computing the start  */ 
    /*  and end bits within the start and end bytes for the field.          */ 
     
    start_bit = start & 7; 
    end_bit = i & 7; 
 

    /*  Compute the number of bytes covered.                                */ 
     
    i = end_byte - start_byte - 1; 
 

    /*  If the value is to be stored in one byte, store it.                 */ 
     
    if (start_byte == end_byte) 
    { 
        /*  Rather tricky.  We are masking out anything prior to the start  */ 
        /*  bit and after the end bit in order to not corrupt data that has */ 
        /*  already been stored there.                                      */ 
         
        buffer[start_byte] &= mask[start_bit] | notmask[end_bit]; 
 

        /*  Now we mask out anything in the value that is prior to the      */ 
        /*  start bit and after the end bit.  This is, of course, after we  */ 
        /*  have shifted the value left past the end bit.                   */ 
         
        buffer[start_byte] |= (value << (8 - end_bit)) & 
            (notmask[start_bit] & mask[end_bit]); 
    } 
 

    /*  If the value covers more than 1 byte, store it.                     */ 
     
    else 
    { 
        /*  Here we mask out data prior to the start bit of the first byte. */ 
         
        buffer[start_byte] &= mask[start_bit]; 
 

        /*  Get the upper bits of the value and mask out anything prior to  */ 
        /*  the start bit.  As an example of what's happening here, if we   */ 
        /*  wanted to store a 14 bit field and the start bit for the first  */ 
        /*  byte is 3, we would be storing the upper 5 bits of the value in */ 
        /*  the first byte.                                                 */ 

        buffer[start_byte++] |= (value >> (numbits - (8 - start_bit))) & 
            notmask[start_bit]; 
 

        /*  Loop while decrementing the byte counter.                       */ 
         
        while (i--) 
        { 
            /*  Clear the entire byte.                                      */ 
             
       	    buffer[start_byte] &= 0; 
 

       	    /*  Get the next 8 bits from the value.                         */ 
       	     
       	    buffer[start_byte++] |= (value >> ((i << 3) + end_bit)) & 255; 
       	} 
 

       	/*  For the last byte we mask out anything after the end bit.       */ 
       	 
        buffer[start_byte] &= notmask[end_bit]; 
 

        /*  Get the last part of the value and stuff it in the end byte.    */ 
        /*  The left shift effectively erases anything above 8 - end_bit    */ 
        /*  bits in the value so that it will fit in the last byte.         */ 
         
        buffer[start_byte] |= (value << (8 - end_bit)); 
    } 
} 
 
 
 
/***************************************************************************\ 
*                                                                           * 
*   Function        bit_unpack - Unpacks a long value from consecutive bits * 
*                   in buffer.                                              * 
*                                                                           * 
*   Synopsis        bit_unpack (buffer, start, numbits);                    * 
*                                                                           * 
*                   NV_U_BYTE buffer[]      address of buffer to use        * 
*                   NV_U_INT32 start        start bit position in buffer    * 
*                   NV_U_INT32 numbits      number of bits to retrieve      * 
*                                                                           * 
*   Description     Unpacks the value from 'numbits' bits in 'buffer'       * 
*                   starting at bit position 'start'.  The value is assumed * 
*                   to be unsigned.  The majority of this code is based on  * 
*                   Appendix C of Naval Ocean Research and Development      * 
*                   Activity Report #236, 'Data Base Structure to Support   * 
*                   the Production of the Digital Bathymetric Data Base',   * 
*                   Nov. 1989, James E. Braud, John L. Breckenridge, James  * 
*                   E. Current, Jerry L. Landrum.                           * 
*                                                                           * 
*   Returns         NV_U_INT32              value retrieved from buffer     * 
*                                                                           * 
*   Author          Jan C. Depner                                           * 
*                                                                           * 
\***************************************************************************/ 

NV_U_INT32 bit_unpack (NV_U_BYTE buffer[], NV_U_INT32 start, NV_U_INT32 numbits) 
{ 
    NV_INT32                start_byte, end_byte, start_bit, end_bit, i; 
    NV_U_INT32              value; 
 
 
    i = start + numbits; 
 

    /*  Right shift the start and end by 3 bits, this is the same as        */ 
    /*  dividing by 8 but is faster.  This is computing the start and end   */ 
    /*  bytes for the field.                                                */ 
     
    start_byte = start >> 3; 
    end_byte = i >> 3; 
 

    /*  AND the start and end bit positions with 7, this is the same as     */ 
    /*  doing a mod with 8 but is faster.  Here we are computing the start  */ 
    /*  and end bits within the start and end bytes for the field.          */ 
     
    start_bit = start & 7; 
    end_bit = i & 7; 
 

    /*  Compute the number of bytes covered.                                */ 
     
    i = end_byte - start_byte - 1; 
 

    /*  If the value is stored in one byte, retrieve it.                    */ 
     
    if (start_byte == end_byte) 
    { 
        /*  Mask out anything prior to the start bit and after the end bit. */ 
         
        value = (NV_U_INT32) buffer[start_byte] & (notmask[start_bit] &
            mask[end_bit]); 
 

        /*  Now we shift the value to the right.                            */ 
         
        value >>= (8 - end_bit); 
    } 
 

    /*  If the value covers more than 1 byte, retrieve it.                  */ 
     
    else 
    { 
        /*  Here we mask out data prior to the start bit of the first byte  */ 
        /*  and shift to the left the necessary amount.                     */ 
         
        value = (NV_U_INT32) (buffer[start_byte++] & notmask[start_bit]) <<
            (numbits - (8 - start_bit)); 
 

        /*  Loop while decrementing the byte counter.                       */ 
         
        while (i--) 
        { 
       	    /*  Get the next 8 bits from the buffer.                        */ 
       	     
       	    value += (NV_U_INT32) buffer[start_byte++] << ((i << 3) + end_bit);
       	} 
 

       	/*  For the last byte we mask out anything after the end bit and    */ 
       	/*  then shift to the right (8 - end_bit) bits.                     */ 
       	 
        value += (NV_U_INT32) (buffer[start_byte] & mask[end_bit]) >> 
            (8 - end_bit); 
    } 

    return (value); 
} 



/***************************************************************************\ 
*                                                                           * 
*   Function        signed_bit_unpack - Unpacks a signed long value from    *
*                   consecutive bits in buffer.                             * 
*                                                                           * 
*   Synopsis        signed_bit_unpack (buffer, start, numbits);             * 
*                                                                           * 
*                   NV_U_BYTE buffer[]      address of buffer to use        * 
*                   NV_U_INT32 start        start bit position in buffer    * 
*                   NV_U_INT32 numbits      number of bits to retrieve      * 
*                                                                           * 
*   Description     Unpacks the value from 'numbits' bits in 'buffer'       * 
*                   starting at bit position 'start'.  The value is assumed * 
*                   to be signed.  The majority of this code is based on    * 
*                   Appendix C of Naval Ocean Research and Development      * 
*                   Activity Report #236, 'Data Base Structure to Support   * 
*                   the Production of the Digital Bathymetric Data Base',   * 
*                   Nov. 1989, James E. Braud, John L. Breckenridge, James  * 
*                   E. Current, Jerry L. Landrum.                           * 
*                                                                           * 
*   Returns         NV_INT32              value retrieved from buffer       * 
*                                                                           * 
*   Author          Jan C. Depner                                           * 
*                                                                           * 
\***************************************************************************/ 

NV_INT32 signed_bit_unpack (NV_U_BYTE buffer[], NV_U_INT32 start, 
NV_U_INT32 numbits) 
{
    static NV_INT32              extend_mask = 0x7fffffff;
    NV_INT32                     value;

    value = bit_unpack (buffer, start, numbits);

    if (value & (1 << (numbits - 1))) value |= (extend_mask << numbits);

    return (value);
}