83ba5dfa4ed4c20e76de495c0b150c2aba4267c0
[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   public int hgetbits(@LOC("THIS,BitReserve.BIT") int N) {\r
89 \r
90     totbit += N;\r
91 \r
92     @LOC("OUT") int val = 0;\r
93 \r
94     @LOC("THIS,BitReserve.BIT") int pos = buf_byte_idx;\r
95     if (pos + N < BUFSIZE) {\r
96       TERMINATE: while (N-- > 0) {\r
97         val <<= 1;\r
98         val |= ((buf[pos++] != 0) ? 1 : 0);\r
99       }\r
100     } else {\r
101       TERMINATE: while (N-- > 0) {\r
102         val <<= 1;\r
103         val |= ((buf[pos] != 0) ? 1 : 0);\r
104         pos = (pos + 1) & BUFSIZE_MASK;\r
105       }\r
106     }\r
107 \r
108     buf_byte_idx = pos;\r
109 \r
110     return val;\r
111 \r
112   }\r
113 \r
114   /**\r
115    * Returns next bit from reserve.\r
116    * \r
117    * @returns 0 if next bit is reset, or 1 if next bit is set.\r
118    */\r
119   @RETURNLOC("THIS,BitReserve.BIT")\r
120   public int hget1bit() {\r
121     totbit++;\r
122     @LOC("THIS,BitReserve.BIT") int val = buf[buf_byte_idx];\r
123     buf_byte_idx = (buf_byte_idx + 1) & BUFSIZE_MASK;\r
124     return val;\r
125   }\r
126 \r
127   /**\r
128    * Write 8 bits into the bit stream.\r
129    */\r
130   @LATTICE("OUT<THIS,THIS<IN,THISLOC=THIS,GLOBALLOC=IN")\r
131   public void hputbuf(@LOC("IN") int val) {\r
132     @LOC("THIS,BitReserve.OFF") int ofs = offset;\r
133     buf[ofs++] = val & 0x80;\r
134     buf[ofs++] = val & 0x40;\r
135     buf[ofs++] = val & 0x20;\r
136     buf[ofs++] = val & 0x10;\r
137     buf[ofs++] = val & 0x08;\r
138     buf[ofs++] = val & 0x04;\r
139     buf[ofs++] = val & 0x02;\r
140     buf[ofs++] = val & 0x01;\r
141 \r
142     if (ofs == BUFSIZE)\r
143       offset = 0;\r
144     else\r
145       offset = ofs;\r
146 \r
147   }\r
148 \r
149   /**\r
150    * Rewind N bits in Stream.\r
151    */\r
152   public void rewindNbits(@LOC("THIS,BitReserve.BIT") int N) {\r
153     totbit -= N;\r
154     buf_byte_idx -= N;\r
155     if (buf_byte_idx < 0)\r
156       buf_byte_idx += BUFSIZE;\r
157   }\r
158 \r
159   /**\r
160    * Rewind N bytes in Stream.\r
161    */\r
162   @LATTICE("THIS<BIT,BIT<N,THISLOC=THIS,GLOBALLOC=N")\r
163   public void rewindNbytes(@LOC("N") int N) {\r
164     @LOC("BIT") int bits = (N << 3);\r
165     totbit -= bits;\r
166     buf_byte_idx -= bits;\r
167     if (buf_byte_idx < 0)\r
168       buf_byte_idx += BUFSIZE;\r
169   }\r
170 }\r