changes: now Inference engine works fine with the EyeTracking benchmark.
[IRC.git] / Robust / src / Benchmarks / SSJava / MP3DecoderInfer / 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 \r
41 final class BitReserve {\r
42   /**\r
43    * Size of the internal buffer to store the reserved bits. Must be a power of\r
44    * 2. And x8, as each bit is stored as a single entry.\r
45    */\r
46   private static final int BUFSIZE = 4096 * 8;\r
47 \r
48   /**\r
49    * Mask that can be used to quickly implement the modulus operation on\r
50    * BUFSIZE.\r
51    */\r
52   private static final int BUFSIZE_MASK = BUFSIZE - 1;\r
53 \r
54   private int offset;\r
55 \r
56   public int totbit;\r
57 \r
58   public int buf_byte_idx;\r
59 \r
60   private final int[] buf;\r
61 \r
62   BitReserve() {\r
63     offset = 0;\r
64     totbit = 0;\r
65     buf_byte_idx = 0;\r
66     buf = new int[BUFSIZE];\r
67   }\r
68 \r
69   /**\r
70    * Return totbit Field.\r
71    */\r
72 \r
73   public int hsstell() {\r
74     return (totbit);\r
75   }\r
76 \r
77   /**\r
78    * Read a number bits from the bit stream.\r
79    * \r
80    * @param N\r
81    *          the number of\r
82    */\r
83   public int hgetbits(int N) {\r
84 \r
85     totbit += N;\r
86 \r
87     int val = 0;\r
88 \r
89     int pos = buf_byte_idx;\r
90     if (pos + N < BUFSIZE) {\r
91       TERMINATE: while (N-- > 0) {\r
92         val <<= 1;\r
93         val |= ((buf[pos++] != 0) ? 1 : 0);\r
94       }\r
95     } else {\r
96       TERMINATE: while (N-- > 0) {\r
97         val <<= 1;\r
98         val |= ((buf[pos] != 0) ? 1 : 0);\r
99         pos = (pos + 1) & BUFSIZE_MASK;\r
100       }\r
101     }\r
102 \r
103     buf_byte_idx = pos;\r
104 \r
105     return val;\r
106 \r
107   }\r
108 \r
109   /**\r
110    * Returns next bit from reserve.\r
111    * \r
112    * @returns 0 if next bit is reset, or 1 if next bit is set.\r
113    */\r
114 \r
115   public int hget1bit() {\r
116     totbit++;\r
117     int val = buf[buf_byte_idx];\r
118     buf_byte_idx = (buf_byte_idx + 1) & BUFSIZE_MASK;\r
119     return val;\r
120   }\r
121 \r
122   /**\r
123    * Write 8 bits into the bit stream.\r
124    */\r
125 \r
126   public void hputbuf(int val) {\r
127     int ofs = offset;\r
128     buf[ofs++] = val & 0x80;\r
129     buf[ofs++] = val & 0x40;\r
130     buf[ofs++] = val & 0x20;\r
131     buf[ofs++] = val & 0x10;\r
132     buf[ofs++] = val & 0x08;\r
133     buf[ofs++] = val & 0x04;\r
134     buf[ofs++] = val & 0x02;\r
135     buf[ofs++] = val & 0x01;\r
136 \r
137     if (ofs == BUFSIZE)\r
138       offset = 0;\r
139     else\r
140       offset = ofs;\r
141 \r
142   }\r
143 \r
144   /**\r
145    * Rewind N bits in Stream.\r
146    */\r
147   public void rewindNbits(int N) {\r
148     totbit -= N;\r
149     buf_byte_idx -= N;\r
150     if (buf_byte_idx < 0)\r
151       buf_byte_idx += BUFSIZE;\r
152   }\r
153 \r
154   /**\r
155    * Rewind N bytes in Stream.\r
156    */\r
157 \r
158   public void rewindNbytes(int N) {\r
159     int bits = (N << 3);\r
160     totbit -= bits;\r
161     buf_byte_idx -= bits;\r
162     if (buf_byte_idx < 0)\r
163       buf_byte_idx += BUFSIZE;\r
164   }\r
165 }\r