Skip to content

Commit

Permalink
Update xxhash to r5.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpountz committed Dec 16, 2012
1 parent 599c6bd commit 54a51f9
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 73 deletions.
4 changes: 2 additions & 2 deletions CHANGES.txt
Expand Up @@ -4,6 +4,6 @@ Version 1.0
===========

First release.
Java ports and bindings of LZ4 and xxHash.
Java ports and bindings of LZ4 and xxhash.
LZ4 r85
xxhash r4
xxhash r5
12 changes: 12 additions & 0 deletions src/java/net/jpountz/util/UnsafeUtils.java
Expand Up @@ -17,7 +17,11 @@
* limitations under the License.
*/

import static java.lang.Integer.reverseBytes;
import static net.jpountz.util.Utils.NATIVE_BYTE_ORDER;

import java.lang.reflect.Field;
import java.nio.ByteOrder;

import sun.misc.Unsafe;

Expand Down Expand Up @@ -76,6 +80,14 @@ public static int readInt(byte[] src, int srcOff) {
return UNSAFE.getInt(src, BYTE_ARRAY_OFFSET + srcOff);
}

public static int readIntLE(byte[] src, int srcOff) {
int i = readInt(src, srcOff);
if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
i = reverseBytes(i);
}
return i;
}

public static void writeInt(byte[] dest, int destOff, int value) {
UNSAFE.putInt(dest, BYTE_ARRAY_OFFSET + destOff, value);
}
Expand Down
30 changes: 14 additions & 16 deletions src/java/net/jpountz/xxhash/StreamingXXHash32JNI.java
Expand Up @@ -24,33 +24,31 @@ final class StreamingXXHash32JNI extends StreamingXXHash32 {

StreamingXXHash32JNI(int seed) {
super(seed);
reset();
state = XXHashJNI.XXH32_init(seed);
}

private void checkState() {
if (state == 0) {
throw new AssertionError("Already finalized");
}
}

@Override
public void reset() {
if (state != 0) {
XXHashJNI.XXH32_result(state);
state = 0;
}
checkState();
XXHashJNI.XXH32_result(state);
state = XXHashJNI.XXH32_init(seed);
}

@Override
public int getValue() {
if (state == 0) {
throw new IllegalStateException("getValue has already been called");
}
final int result = XXHashJNI.XXH32_result(state);
state = 0;
return result;
checkState();
return XXHashJNI.XXH32_getIntermediateResult(state);
}

@Override
public void update(byte[] bytes, int off, int len) {
if (state == 0) {
throw new IllegalStateException("getValue has already been called");
}
checkState();
XXHashJNI.XXH32_feed(state, bytes, off, len);
}

Expand All @@ -59,9 +57,9 @@ protected void finalize() throws Throwable {
super.finalize();
// free memory
if (state != 0) {
getValue();
XXHashJNI.XXH32_result(state);
state = 0;
}
assert state == 0;
}

}
22 changes: 11 additions & 11 deletions src/java/net/jpountz/xxhash/StreamingXXHash32JavaSafe.java
Expand Up @@ -19,7 +19,7 @@

import static java.lang.Integer.rotateLeft;
import static net.jpountz.util.Utils.checkRange;
import static net.jpountz.util.Utils.readInt;
import static net.jpountz.util.Utils.readIntLE;
import static net.jpountz.xxhash.XXHashUtils.PRIME1;
import static net.jpountz.xxhash.XXHashUtils.PRIME2;
import static net.jpountz.xxhash.XXHashUtils.PRIME3;
Expand All @@ -41,11 +41,11 @@ public int getValue() {
h32 = seed + PRIME5;
}

h32 += (int) totalLen;
h32 += totalLen;

int off = 0;
while (off <= memSize - 4) {
h32 += readInt(memory, off) * PRIME3;
h32 += readIntLE(memory, off) * PRIME3;
h32 = rotateLeft(h32, 17) * PRIME4;
off += 4;
}
Expand Down Expand Up @@ -82,19 +82,19 @@ public void update(byte[] buf, int off, int len) {
if (memSize > 0) { // data left from previous update
System.arraycopy(buf, off, memory, memSize, 16 - memSize);

v1 += readInt(memory, 0) * PRIME2;
v1 += readIntLE(memory, 0) * PRIME2;
v1 = rotateLeft(v1, 13);
v1 *= PRIME1;

v2 += readInt(memory, 4) * PRIME2;
v2 += readIntLE(memory, 4) * PRIME2;
v2 = rotateLeft(v2, 13);
v2 *= PRIME1;

v3 += readInt(memory, 8) * PRIME2;
v3 += readIntLE(memory, 8) * PRIME2;
v3 = rotateLeft(v3, 13);
v3 *= PRIME1;

v4 += readInt(memory, 12) * PRIME2;
v4 += readIntLE(memory, 12) * PRIME2;
v4 = rotateLeft(v4, 13);
v4 *= PRIME1;

Expand All @@ -110,22 +110,22 @@ public void update(byte[] buf, int off, int len) {
int v4 = this.v4;

while (off <= limit) {
v1 += readInt(buf, off) * PRIME2;
v1 += readIntLE(buf, off) * PRIME2;
v1 = rotateLeft(v1, 13);
v1 *= PRIME1;
off += 4;

v2 += readInt(buf, off) * PRIME2;
v2 += readIntLE(buf, off) * PRIME2;
v2 = rotateLeft(v2, 13);
v2 *= PRIME1;
off += 4;

v3 += readInt(buf, off) * PRIME2;
v3 += readIntLE(buf, off) * PRIME2;
v3 = rotateLeft(v3, 13);
v3 *= PRIME1;
off += 4;

v4 += readInt(buf, off) * PRIME2;
v4 += readIntLE(buf, off) * PRIME2;
v4 = rotateLeft(v4, 13);
v4 *= PRIME1;
off += 4;
Expand Down
22 changes: 11 additions & 11 deletions src/java/net/jpountz/xxhash/StreamingXXHash32JavaUnsafe.java
Expand Up @@ -19,7 +19,7 @@

import static java.lang.Integer.rotateLeft;
import static net.jpountz.util.UnsafeUtils.readByte;
import static net.jpountz.util.UnsafeUtils.readInt;
import static net.jpountz.util.UnsafeUtils.readIntLE;
import static net.jpountz.util.Utils.checkRange;
import static net.jpountz.xxhash.XXHashUtils.PRIME1;
import static net.jpountz.xxhash.XXHashUtils.PRIME2;
Expand All @@ -42,11 +42,11 @@ public int getValue() {
h32 = seed + PRIME5;
}

h32 += (int) totalLen;
h32 += totalLen;

int off = 0;
while (off <= memSize - 4) {
h32 += readInt(memory, off) * PRIME3;
h32 += readIntLE(memory, off) * PRIME3;
h32 = rotateLeft(h32, 17) * PRIME4;
off += 4;
}
Expand Down Expand Up @@ -83,19 +83,19 @@ public void update(byte[] buf, int off, int len) {
if (memSize > 0) { // data left from previous update
System.arraycopy(buf, off, memory, memSize, 16 - memSize);

v1 += readInt(memory, 0) * PRIME2;
v1 += readIntLE(memory, 0) * PRIME2;
v1 = rotateLeft(v1, 13);
v1 *= PRIME1;

v2 += readInt(memory, 4) * PRIME2;
v2 += readIntLE(memory, 4) * PRIME2;
v2 = rotateLeft(v2, 13);
v2 *= PRIME1;

v3 += readInt(memory, 8) * PRIME2;
v3 += readIntLE(memory, 8) * PRIME2;
v3 = rotateLeft(v3, 13);
v3 *= PRIME1;

v4 += readInt(memory, 12) * PRIME2;
v4 += readIntLE(memory, 12) * PRIME2;
v4 = rotateLeft(v4, 13);
v4 *= PRIME1;

Expand All @@ -111,22 +111,22 @@ public void update(byte[] buf, int off, int len) {
int v4 = this.v4;

while (off <= limit) {
v1 += readInt(buf, off) * PRIME2;
v1 += readIntLE(buf, off) * PRIME2;
v1 = rotateLeft(v1, 13);
v1 *= PRIME1;
off += 4;

v2 += readInt(buf, off) * PRIME2;
v2 += readIntLE(buf, off) * PRIME2;
v2 = rotateLeft(v2, 13);
v2 *= PRIME1;
off += 4;

v3 += readInt(buf, off) * PRIME2;
v3 += readIntLE(buf, off) * PRIME2;
v3 = rotateLeft(v3, 13);
v3 *= PRIME1;
off += 4;

v4 += readInt(buf, off) * PRIME2;
v4 += readIntLE(buf, off) * PRIME2;
v4 = rotateLeft(v4, 13);
v4 *= PRIME1;
off += 4;
Expand Down
12 changes: 6 additions & 6 deletions src/java/net/jpountz/xxhash/XXHash32JavaSafe.java
Expand Up @@ -19,7 +19,7 @@

import static java.lang.Integer.rotateLeft;
import static net.jpountz.util.Utils.checkRange;
import static net.jpountz.util.Utils.readInt;
import static net.jpountz.util.Utils.readIntLE;
import static net.jpountz.xxhash.XXHashUtils.PRIME1;
import static net.jpountz.xxhash.XXHashUtils.PRIME2;
import static net.jpountz.xxhash.XXHashUtils.PRIME3;
Expand Down Expand Up @@ -47,22 +47,22 @@ public int hash(byte[] buf, int off, int len, int seed) {
int v3 = seed + 0;
int v4 = seed - PRIME1;
do {
v1 += readInt(buf, off) * PRIME2;
v1 += readIntLE(buf, off) * PRIME2;
v1 = rotateLeft(v1, 13);
v1 *= PRIME1;
off += 4;

v2 += readInt(buf, off) * PRIME2;
v2 += readIntLE(buf, off) * PRIME2;
v2 = rotateLeft(v2, 13);
v2 *= PRIME1;
off += 4;

v3 += readInt(buf, off) * PRIME2;
v3 += readIntLE(buf, off) * PRIME2;
v3 = rotateLeft(v3, 13);
v3 *= PRIME1;
off += 4;

v4 += readInt(buf, off) * PRIME2;
v4 += readIntLE(buf, off) * PRIME2;
v4 = rotateLeft(v4, 13);
v4 *= PRIME1;
off += 4;
Expand All @@ -76,7 +76,7 @@ public int hash(byte[] buf, int off, int len, int seed) {
h32 += len;

while (off <= end - 4) {
h32 += readInt(buf, off) * PRIME3;
h32 += readIntLE(buf, off) * PRIME3;
h32 = rotateLeft(h32, 17) * PRIME4;
off += 4;
}
Expand Down
12 changes: 6 additions & 6 deletions src/java/net/jpountz/xxhash/XXHash32JavaUnsafe.java
Expand Up @@ -19,7 +19,7 @@

import static java.lang.Integer.rotateLeft;
import static net.jpountz.util.UnsafeUtils.readByte;
import static net.jpountz.util.UnsafeUtils.readInt;
import static net.jpountz.util.UnsafeUtils.readIntLE;
import static net.jpountz.util.Utils.checkRange;
import static net.jpountz.xxhash.XXHashUtils.PRIME1;
import static net.jpountz.xxhash.XXHashUtils.PRIME2;
Expand Down Expand Up @@ -48,22 +48,22 @@ public int hash(byte[] buf, int off, int len, int seed) {
int v3 = seed + 0;
int v4 = seed - PRIME1;
do {
v1 += readInt(buf, off) * PRIME2;
v1 += readIntLE(buf, off) * PRIME2;
v1 = rotateLeft(v1, 13);
v1 *= PRIME1;
off += 4;

v2 += readInt(buf, off) * PRIME2;
v2 += readIntLE(buf, off) * PRIME2;
v2 = rotateLeft(v2, 13);
v2 *= PRIME1;
off += 4;

v3 += readInt(buf, off) * PRIME2;
v3 += readIntLE(buf, off) * PRIME2;
v3 = rotateLeft(v3, 13);
v3 *= PRIME1;
off += 4;

v4 += readInt(buf, off) * PRIME2;
v4 += readIntLE(buf, off) * PRIME2;
v4 = rotateLeft(v4, 13);
v4 *= PRIME1;
off += 4;
Expand All @@ -77,7 +77,7 @@ public int hash(byte[] buf, int off, int len, int seed) {
h32 += len;

while (off <= end - 4) {
h32 += readInt(buf, off) * PRIME3;
h32 += readIntLE(buf, off) * PRIME3;
h32 = rotateLeft(h32, 17) * PRIME4;
off += 4;
}
Expand Down
1 change: 1 addition & 0 deletions src/java/net/jpountz/xxhash/XXHashJNI.java
Expand Up @@ -31,6 +31,7 @@ enum XXHashJNI {
static native int XXH32(byte[] input, int offset, int len, int seed);
static native long XXH32_init(int seed);
static native void XXH32_feed(long state, byte[] input, int offset, int len);
static native int XXH32_getIntermediateResult(long state);
static native int XXH32_result(long state);

}
13 changes: 13 additions & 0 deletions src/jni/net_jpountz_xxhash_XXHashJNI.c
Expand Up @@ -87,6 +87,18 @@ JNIEXPORT void JNICALL Java_net_jpountz_xxhash_XXHashJNI_XXH32_1feed

}

/*
* Class: net_jpountz_xxhash_XXHashJNI
* Method: XXH32_getIntermediateResult
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_net_jpountz_xxhash_XXHashJNI_XXH32_1getIntermediateResult
(JNIEnv *env, jclass cls, jlong state) {

return XXH32_getIntermediateResult((void*) state);

}

/*
* Class: net_jpountz_xxhash_XXHashJNI
* Method: XXH32_result
Expand All @@ -98,3 +110,4 @@ JNIEXPORT jint JNICALL Java_net_jpountz_xxhash_XXHashJNI_XXH32_1result
return XXH32_result((void*) state);

}

0 comments on commit 54a51f9

Please sign in to comment.