]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/lz4/LZ4UnsafeUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / lz4 / LZ4UnsafeUtils.java
1 package net.jpountz.lz4;
2
3 /*
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 import static net.jpountz.lz4.LZ4Constants.COPY_LENGTH;
18 import static net.jpountz.lz4.LZ4Constants.LAST_LITERALS;
19 import static net.jpountz.lz4.LZ4Constants.ML_BITS;
20 import static net.jpountz.lz4.LZ4Constants.ML_MASK;
21 import static net.jpountz.lz4.LZ4Constants.RUN_MASK;
22 import static net.jpountz.util.UnsafeUtils.readByte;
23 import static net.jpountz.util.UnsafeUtils.readInt;
24 import static net.jpountz.util.UnsafeUtils.readLong;
25 import static net.jpountz.util.UnsafeUtils.readShort;
26 import static net.jpountz.util.UnsafeUtils.writeByte;
27 import static net.jpountz.util.UnsafeUtils.writeInt;
28 import static net.jpountz.util.UnsafeUtils.writeLong;
29 import static net.jpountz.util.UnsafeUtils.writeShort;
30 import static net.jpountz.util.Utils.NATIVE_BYTE_ORDER;
31
32 import java.nio.ByteOrder;
33
34 enum LZ4UnsafeUtils {
35   ;
36
37   static void safeArraycopy(byte[] src, int srcOff, byte[] dest, int destOff, int len) {
38     final int fastLen = len & 0xFFFFFFF8;
39     wildArraycopy(src, srcOff, dest, destOff, fastLen);
40     for (int i = 0, slowLen = len & 0x7; i < slowLen; i += 1) {
41       writeByte(dest, destOff + fastLen + i, readByte(src, srcOff + fastLen + i));
42     }
43   }
44
45   static void wildArraycopy(byte[] src, int srcOff, byte[] dest, int destOff, int len) {
46     for (int i = 0; i < len; i += 8) {
47       writeLong(dest, destOff + i, readLong(src, srcOff + i));
48     }
49   }
50
51   static void wildIncrementalCopy(byte[] dest, int matchOff, int dOff, int matchCopyEnd) {
52     if (dOff - matchOff < 4) {
53       for (int i = 0; i < 4; ++i) {
54         writeByte(dest, dOff+i, readByte(dest, matchOff+i));
55       }
56       dOff += 4;
57       matchOff += 4;
58       int dec = 0;
59       assert dOff >= matchOff && dOff - matchOff < 8;
60       switch (dOff - matchOff) {
61       case 1:
62         matchOff -= 3;
63         break;
64       case 2:
65         matchOff -= 2;
66         break;
67       case 3:
68         matchOff -= 3;
69         dec = -1;
70         break;
71       case 5:
72         dec = 1;
73         break;
74       case 6:
75         dec = 2;
76         break;
77       case 7:
78         dec = 3;
79         break;
80       default:
81         break;
82       }
83       writeInt(dest, dOff, readInt(dest, matchOff));
84       dOff += 4;
85       matchOff -= dec;
86     } else if (dOff - matchOff < COPY_LENGTH) {
87       writeLong(dest, dOff, readLong(dest, matchOff));
88       dOff += dOff - matchOff;
89     }
90     while (dOff < matchCopyEnd) {
91       writeLong(dest, dOff, readLong(dest, matchOff));
92       dOff += 8;
93       matchOff += 8;
94     }
95   }
96
97   static void safeIncrementalCopy(byte[] dest, int matchOff, int dOff, int matchLen) {
98     for (int i = 0; i < matchLen; ++i) {
99       dest[dOff + i] = dest[matchOff + i];
100       writeByte(dest, dOff + i, readByte(dest, matchOff + i));
101     }
102   }
103
104   static int readShortLittleEndian(byte[] src, int srcOff) {
105     short s = readShort(src, srcOff);
106     if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
107       s = Short.reverseBytes(s);
108     }
109     return s & 0xFFFF;
110   }
111
112   static void writeShortLittleEndian(byte[] dest, int destOff, int value) {
113     short s = (short) value;
114     if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
115       s = Short.reverseBytes(s);
116     }
117     writeShort(dest, destOff, s);
118   }
119
120   static boolean readIntEquals(byte[] src, int ref, int sOff) {
121     return readInt(src, ref) == readInt(src, sOff);
122   }
123
124   static int commonBytes(byte[] src, int ref, int sOff, int srcLimit) {
125     int matchLen = 0;
126     while (sOff <= srcLimit - 8) {
127       if (readLong(src, sOff) == readLong(src, ref)) {
128         matchLen += 8;
129         ref += 8;
130         sOff += 8;
131       } else {
132         final int zeroBits;
133         if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
134           zeroBits = Long.numberOfLeadingZeros(readLong(src, sOff) ^ readLong(src, ref));
135         } else {
136           zeroBits = Long.numberOfTrailingZeros(readLong(src, sOff) ^ readLong(src, ref));
137         }
138         return matchLen + (zeroBits >>> 3);
139       }
140     }
141     while (sOff < srcLimit && readByte(src, ref++) == readByte(src, sOff++)) {
142       ++matchLen;
143     }
144     return matchLen;
145   }
146
147   static int writeLen(int len, byte[] dest, int dOff) {
148     while (len >= 0xFF) {
149       writeByte(dest, dOff++, 0xFF);
150       len -= 0xFF;
151     }
152     writeByte(dest, dOff++, len);
153     return dOff;
154   }
155
156   static int encodeSequence(byte[] src, int anchor, int matchOff, int matchRef, int matchLen, byte[] dest, int dOff, int destEnd) {
157     final int runLen = matchOff - anchor;
158     final int tokenOff = dOff++;
159     int token;
160
161     if (runLen >= RUN_MASK) {
162       token = (byte) (RUN_MASK << ML_BITS);
163       dOff = writeLen(runLen - RUN_MASK, dest, dOff);
164     } else {
165       token = runLen << ML_BITS;
166     }
167
168     // copy literals
169     wildArraycopy(src, anchor, dest, dOff, runLen);
170     dOff += runLen;
171
172     // encode offset
173     final int matchDec = matchOff - matchRef;
174     dest[dOff++] = (byte) matchDec;
175     dest[dOff++] = (byte) (matchDec >>> 8);
176
177     // encode match len
178     matchLen -= 4;
179     if (dOff + (1 + LAST_LITERALS) + (matchLen >>> 8) > destEnd) {
180       throw new LZ4Exception("maxDestLen is too small");
181     }
182     if (matchLen >= ML_MASK) {
183       token |= ML_MASK;
184       dOff = writeLen(matchLen - RUN_MASK, dest, dOff);
185     } else {
186       token |= matchLen;
187     }
188
189     dest[tokenOff] = (byte) token;
190
191     return dOff;
192   }
193
194   static int commonBytesBackward(byte[] b, int o1, int o2, int l1, int l2) {
195     int count = 0;
196     while (o1 > l1 && o2 > l2 && readByte(b, --o1) == readByte(b, --o2)) {
197       ++count;
198     }
199     return count;
200   }
201
202   static int lastLiterals(byte[] src, int sOff, int srcLen, byte[] dest, int dOff, int destEnd) {
203     return LZ4SafeUtils.lastLiterals(src, sOff, srcLen, dest, dOff, destEnd);
204   }
205
206 }