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