lcd-m6sp.c: remove \r
[kugel-rb.git] / apps / codecs / libtremor / bitwise.c
blobdabba468b92c5969f93f117cad86f160f3759c66
1 /********************************************************************
2 * *
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
4 * *
5 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
6 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
7 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * *
9 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
10 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
11 * *
12 ********************************************************************
14 function: packing variable sized words into an octet stream
16 ********************************************************************/
18 /* We're 'LSb' endian; if we write a word but read individual bits,
19 then we'll read the lsb first */
21 #include "config-tremor.h"
22 #include <string.h>
23 #include "ogg.h"
25 const unsigned long oggpack_mask[] ICONST_ATTR =
26 {0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
27 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
28 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
29 0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
30 0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
31 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
32 0x3fffffff,0x7fffffff,0xffffffff };
34 void oggpack_readinit(oggpack_buffer *b,ogg_reference *r){
35 memset(b,0,sizeof(*b));
37 b->tail=b->head=r;
38 b->count=0;
39 b->headptr=b->head->buffer->data+b->head->begin;
40 b->headend=b->head->length;
41 _span(b);
44 #define _lookspan() while(!end){\
45 head=head->next;\
46 if(!head) return -1;\
47 ptr=head->buffer->data + head->begin;\
48 end=head->length;\
51 /* Read in bits without advancing the bitptr; bits <= 32 */
52 long oggpack_look_full(oggpack_buffer *b,int bits) ICODE_ATTR_TREMOR_NOT_MDCT;
53 long oggpack_look_full(oggpack_buffer *b,int bits){
54 unsigned long m=oggpack_mask[bits];
55 unsigned long ret=-1;
57 bits+=b->headbit;
59 if(bits >= b->headend<<3){
60 int end=b->headend;
61 unsigned char *ptr=b->headptr;
62 ogg_reference *head=b->head;
64 if(end<0)return -1;
66 if(bits){
67 _lookspan();
68 ret=*ptr++>>b->headbit;
69 if(bits>8){
70 --end;
71 _lookspan();
72 ret|=*ptr++<<(8-b->headbit);
73 if(bits>16){
74 --end;
75 _lookspan();
76 ret|=*ptr++<<(16-b->headbit);
77 if(bits>24){
78 --end;
79 _lookspan();
80 ret|=*ptr++<<(24-b->headbit);
81 if(bits>32 && b->headbit){
82 --end;
83 _lookspan();
84 ret|=*ptr<<(32-b->headbit);
91 }else{
93 /* make this a switch jump-table */
94 ret=b->headptr[0]>>b->headbit;
95 if(bits>8){
96 ret|=b->headptr[1]<<(8-b->headbit);
97 if(bits>16){
98 ret|=b->headptr[2]<<(16-b->headbit);
99 if(bits>24){
100 ret|=b->headptr[3]<<(24-b->headbit);
101 if(bits>32 && b->headbit)
102 ret|=b->headptr[4]<<(32-b->headbit);
108 ret&=m;
109 return ret;
112 /* spans forward and finds next byte. Never halts */
113 static void _span_one(oggpack_buffer *b){
114 while(b->headend<1){
115 if(b->head->next){
116 b->count+=b->head->length;
117 b->head=b->head->next;
118 b->headptr=b->head->buffer->data+b->head->begin;
119 b->headend=b->head->length;
120 }else
121 break;
125 static int _halt_one(oggpack_buffer *b){
126 if(b->headend<1){
127 _adv_halt(b);
128 return -1;
130 return 0;
133 /* bits <= 32 */
134 long oggpack_read(oggpack_buffer *b,register int bits) ICODE_ATTR_TREMOR_NOT_MDCT;
135 long oggpack_read(oggpack_buffer *b,register int bits){
136 unsigned long m=oggpack_mask[bits];
137 ogg_uint32_t ret=-1;
139 bits+=b->headbit;
141 if(bits >= b->headend<<3){
143 if(b->headend<0)return -1;
145 if(bits){
146 if (_halt_one(b)) return -1;
147 ret=*b->headptr>>b->headbit;
149 if(bits>=8){
150 ++b->headptr;
151 --b->headend;
152 _span_one(b);
153 if(bits>8){
154 if (_halt_one(b)) return -1;
155 ret|=*b->headptr<<(8-b->headbit);
157 if(bits>=16){
158 ++b->headptr;
159 --b->headend;
160 _span_one(b);
161 if(bits>16){
162 if (_halt_one(b)) return -1;
163 ret|=*b->headptr<<(16-b->headbit);
165 if(bits>=24){
166 ++b->headptr;
167 --b->headend;
168 _span_one(b);
169 if(bits>24){
170 if (_halt_one(b)) return -1;
171 ret|=*b->headptr<<(24-b->headbit);
173 if(bits>=32){
174 ++b->headptr;
175 --b->headend;
176 _span_one(b);
177 if(bits>32){
178 if (_halt_one(b)) return -1;
179 if(b->headbit)ret|=*b->headptr<<(32-b->headbit);
190 }else{
192 ret=b->headptr[0]>>b->headbit;
193 if(bits>8){
194 ret|=b->headptr[1]<<(8-b->headbit);
195 if(bits>16){
196 ret|=b->headptr[2]<<(16-b->headbit);
197 if(bits>24){
198 ret|=b->headptr[3]<<(24-b->headbit);
199 if(bits>32 && b->headbit){
200 ret|=b->headptr[4]<<(32-b->headbit);
206 b->headptr+=((unsigned)bits)/8;
207 b->headend-=((unsigned)bits)/8;
210 ret&=m;
211 b->headbit=bits&7;
212 return ret;