remove unnecessary annotations to calculate evalution numbers.
[IRC.git] / Robust / src / Benchmarks / SSJava / MP3Decoder / BitReserve.java
1 /*
2  * 11/19/04                     1.0 moved to LGPL.
3  * 
4  * 12/12/99 0.0.7       Implementation stores single bits 
5  *                                      as ints for better performance. mdm@techie.com.
6  *
7  * 02/28/99 0.0     Java Conversion by E.B, javalayer@javazoom.net
8  *
9  *                  Adapted from the public c code by Jeff Tsay.
10  *
11  *-----------------------------------------------------------------------
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU Library General Public License as published
14  *   by the Free Software Foundation; either version 2 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU Library General Public License for more details.
21  *
22  *   You should have received a copy of the GNU Library General Public
23  *   License along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *----------------------------------------------------------------------
26  */
27
28 /**
29  * Implementation of Bit Reservoir for Layer III.
30  * <p>
31  * The implementation stores single bits as a word in the buffer. If a bit is
32  * set, the corresponding word in the buffer will be non-zero. If a bit is
33  * clear, the corresponding word is zero. Although this may seem waseful, this
34  * can be a factor of two quicker than packing 8 bits to a byte and extracting.
35  * <p>
36  */
37
38 // REVIEW: there is no range checking, so buffer underflow or overflow
39 // can silently occur.
40 @LATTICE("BUF<OFF,BUF<BIT,BIT*,OFF*")
41 @METHODDEFAULT("OUT<THIS,THIS<IN,IN*,OUT*,THISLOC=THIS,RETURNLOC=OUT")
42 final class BitReserve {
43   /**
44    * Size of the internal buffer to store the reserved bits. Must be a power of
45    * 2. And x8, as each bit is stored as a single entry.
46    */
47   private static final int BUFSIZE = 4096 * 8;
48
49   /**
50    * Mask that can be used to quickly implement the modulus operation on
51    * BUFSIZE.
52    */
53   private static final int BUFSIZE_MASK = BUFSIZE - 1;
54
55   @LOC("BIT")
56   private int offset;
57
58   @LOC("BIT")
59   public int totbit;
60
61   @LOC("BIT")
62   public int buf_byte_idx;
63
64   @LOC("BIT")
65   private final int[] buf;
66
67   BitReserve() {
68     offset = 0;
69     totbit = 0;
70     buf_byte_idx = 0;
71     buf = new int[BUFSIZE];
72   }
73
74   /**
75    * Return totbit Field.
76    */
77   @RETURNLOC("THIS,BitReserve.BIT")
78   public int hsstell() {
79     return (totbit);
80   }
81
82   /**
83    * Read a number bits from the bit stream.
84    * 
85    * @param N
86    *          the number of
87    */
88   @PCLOC("THIS,BitReserve.BIT")
89   public int hgetbits(@LOC("THIS,BitReserve.BIT") int N) {
90
91     totbit += N;
92
93     @LOC("OUT") int val = 0;
94
95     @LOC("THIS,BitReserve.BIT") int pos = buf_byte_idx;
96     if (pos + N < BUFSIZE) {
97       TERMINATE: while (N-- > 0) {
98         val <<= 1;
99         val |= ((buf[pos++] != 0) ? 1 : 0);
100       }
101     } else {
102       TERMINATE: while (N-- > 0) {
103         val <<= 1;
104         val |= ((buf[pos] != 0) ? 1 : 0);
105         pos = (pos + 1) & BUFSIZE_MASK;
106       }
107     }
108
109     buf_byte_idx = pos;
110
111     return val;
112
113   }
114
115   /**
116    * Returns next bit from reserve.
117    * 
118    * @returns 0 if next bit is reset, or 1 if next bit is set.
119    */
120   @RETURNLOC("THIS,BitReserve.BIT")
121   public int hget1bit() {
122     totbit++;
123     @LOC("THIS,BitReserve.BIT") int val = buf[buf_byte_idx];
124     buf_byte_idx = (buf_byte_idx + 1) & BUFSIZE_MASK;
125     return val;
126   }
127
128   /**
129    * Write 8 bits into the bit stream.
130    */
131   @LATTICE("OUT<THIS,THIS<IN,THISLOC=THIS,GLOBALLOC=IN")
132   public void hputbuf(@LOC("IN") int val) {
133     @LOC("THIS,BitReserve.OFF") int ofs = offset;
134     buf[ofs++] = val & 0x80;
135     buf[ofs++] = val & 0x40;
136     buf[ofs++] = val & 0x20;
137     buf[ofs++] = val & 0x10;
138     buf[ofs++] = val & 0x08;
139     buf[ofs++] = val & 0x04;
140     buf[ofs++] = val & 0x02;
141     buf[ofs++] = val & 0x01;
142
143     if (ofs == BUFSIZE)
144       offset = 0;
145     else
146       offset = ofs;
147
148   }
149
150   /**
151    * Rewind N bits in Stream.
152    */
153   public void rewindNbits(@LOC("THIS,BitReserve.BIT") int N) {
154     totbit -= N;
155     buf_byte_idx -= N;
156     if (buf_byte_idx < 0)
157       buf_byte_idx += BUFSIZE;
158   }
159
160   /**
161    * Rewind N bytes in Stream.
162    */
163   @LATTICE("THIS<BIT,BIT<N,THISLOC=THIS,GLOBALLOC=N")
164   public void rewindNbytes(@LOC("N") int N) {
165     @LOC("BIT") int bits = (N << 3);
166     totbit -= bits;
167     buf_byte_idx -= bits;
168     if (buf_byte_idx < 0)
169       buf_byte_idx += BUFSIZE;
170   }
171 }