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