Fix Makefile to generate a platform-independent binary

This commit is contained in:
Takeshi YAMAMURO 2016-03-31 12:38:14 +09:00
parent 0df5e90422
commit 695af1b189
5 changed files with 25 additions and 95 deletions

View File

@ -49,7 +49,8 @@ $(SNAPPY_OUT)/%.o : $(BITSHUFFLE_SRC_DIR)/%.c
SNAPPY_OBJ:=$(addprefix $(SNAPPY_OUT)/,$(patsubst %.cc,%.o,$(SNAPPY_CC)) $(patsubst %.c,%.o,$(BITSHUFFLE_C)) SnappyNative.o) SNAPPY_OBJ:=$(addprefix $(SNAPPY_OUT)/,$(patsubst %.cc,%.o,$(SNAPPY_CC)) $(patsubst %.c,%.o,$(BITSHUFFLE_C)) SnappyNative.o)
CXXFLAGS:=$(CXXFLAGS) -DSNAPPY_BITSHUFFLE_ENABLED -I$(SNAPPY_SRC_DIR) -I$(BITSHUFFLE_SRC_DIR) # Undefined macros for sse2/avx2 to generate a platform-independent binary
CXXFLAGS:=$(CXXFLAGS) -U__AVX2__ -U__SSE2__ -I$(SNAPPY_SRC_DIR) -I$(BITSHUFFLE_SRC_DIR)
ifeq ($(OS_NAME),SunOS) ifeq ($(OS_NAME),SunOS)
TAR:= gtar TAR:= gtar

View File

@ -51,11 +51,8 @@ public class BitShuffle
*/ */
public static byte[] bitShuffle(short[] input) throws IOException { public static byte[] bitShuffle(short[] input) throws IOException {
byte[] output = new byte[input.length * 2]; byte[] output = new byte[input.length * 2];
if (impl.supportBitSuffle()) { int numProcessed = impl.bitShuffle(input, 0, 2, input.length * 2, output, 0);
impl.bitShuffle(input, 0, 2, input.length * 2, output, 0); assert(numProcessed == input.length * 2);
} else {
Snappy.arrayCopy(input, 0, input.length * 2, output, 0);
}
return output; return output;
} }
@ -68,11 +65,8 @@ public class BitShuffle
*/ */
public static byte[] bitShuffle(int[] input) throws IOException { public static byte[] bitShuffle(int[] input) throws IOException {
byte[] output = new byte[input.length * 4]; byte[] output = new byte[input.length * 4];
if (impl.supportBitSuffle()) { int numProcessed = impl.bitShuffle(input, 0, 4, input.length * 4, output, 0);
impl.bitShuffle(input, 0, 4, input.length * 4, output, 0); assert(numProcessed == input.length * 4);
} else {
Snappy.arrayCopy(input, 0, input.length * 4, output, 0);
}
return output; return output;
} }
@ -85,11 +79,8 @@ public class BitShuffle
*/ */
public static byte[] bitShuffle(long[] input) throws IOException { public static byte[] bitShuffle(long[] input) throws IOException {
byte[] output = new byte[input.length * 8]; byte[] output = new byte[input.length * 8];
if (impl.supportBitSuffle()) { int numProcessed = impl.bitShuffle(input, 0, 8, input.length * 8, output, 0);
impl.bitShuffle(input, 0, 8, input.length * 8, output, 0); assert(numProcessed == input.length * 8);
} else {
Snappy.arrayCopy(input, 0, input.length * 8, output, 0);
}
return output; return output;
} }
@ -102,11 +93,8 @@ public class BitShuffle
*/ */
public static byte[] bitShuffle(float[] input) throws IOException { public static byte[] bitShuffle(float[] input) throws IOException {
byte[] output = new byte[input.length * 4]; byte[] output = new byte[input.length * 4];
if (impl.supportBitSuffle()) { int numProcessed = impl.bitShuffle(input, 0, 4, input.length * 4, output, 0);
impl.bitShuffle(input, 0, 4, input.length * 4, output, 0); assert(numProcessed == input.length * 4);
} else {
Snappy.arrayCopy(input, 0, input.length * 4, output, 0);
}
return output; return output;
} }
@ -119,11 +107,8 @@ public class BitShuffle
*/ */
public static byte[] bitShuffle(double[] input) throws IOException { public static byte[] bitShuffle(double[] input) throws IOException {
byte[] output = new byte[input.length * 8]; byte[] output = new byte[input.length * 8];
if (impl.supportBitSuffle()) { int numProcessed = impl.bitShuffle(input, 0, 8, input.length * 8, output, 0);
impl.bitShuffle(input, 0, 8, input.length * 8, output, 0); assert(numProcessed == input.length * 8);
} else {
Snappy.arrayCopy(input, 0, input.length * 8, output, 0);
}
return output; return output;
} }
@ -136,11 +121,8 @@ public class BitShuffle
*/ */
public static short[] bitUnShuffleShortArray(byte[] input) throws IOException { public static short[] bitUnShuffleShortArray(byte[] input) throws IOException {
short[] output = new short[input.length / 2]; short[] output = new short[input.length / 2];
if (impl.supportBitSuffle()) { int numProcessed = impl.bitUnShuffle(input, 0, 2, input.length, output, 0);
impl.bitUnShuffle(input, 0, 2, input.length, output, 0); assert(numProcessed == input.length);
} else {
Snappy.arrayCopy(input, 0, input.length, output, 0);
}
return output; return output;
} }
@ -153,11 +135,8 @@ public class BitShuffle
*/ */
public static int[] bitUnShuffleIntArray(byte[] input) throws IOException { public static int[] bitUnShuffleIntArray(byte[] input) throws IOException {
int[] output = new int[input.length / 4]; int[] output = new int[input.length / 4];
if (impl.supportBitSuffle()) { int numProcessed = impl.bitUnShuffle(input, 0, 4, input.length, output, 0);
impl.bitUnShuffle(input, 0, 4, input.length, output, 0); assert(numProcessed == input.length);
} else {
Snappy.arrayCopy(input, 0, input.length, output, 0);
}
return output; return output;
} }
@ -170,11 +149,8 @@ public class BitShuffle
*/ */
public static long[] bitUnShuffleLongArray(byte[] input) throws IOException { public static long[] bitUnShuffleLongArray(byte[] input) throws IOException {
long[] output = new long[input.length / 8]; long[] output = new long[input.length / 8];
if (impl.supportBitSuffle()) { int numProcessed = impl.bitUnShuffle(input, 0, 8, input.length, output, 0);
impl.bitUnShuffle(input, 0, 8, input.length, output, 0); assert(numProcessed == input.length);
} else {
Snappy.arrayCopy(input, 0, input.length, output, 0);
}
return output; return output;
} }
@ -187,11 +163,8 @@ public class BitShuffle
*/ */
public static float[] bitUnShuffleFloatArray(byte[] input) throws IOException { public static float[] bitUnShuffleFloatArray(byte[] input) throws IOException {
float[] output = new float[input.length / 4]; float[] output = new float[input.length / 4];
if (impl.supportBitSuffle()) { int numProcessed = impl.bitUnShuffle(input, 0, 4, input.length, output, 0);
impl.bitUnShuffle(input, 0, 4, input.length, output, 0); assert(numProcessed == input.length);
} else {
Snappy.arrayCopy(input, 0, input.length, output, 0);
}
return output; return output;
} }
@ -204,11 +177,8 @@ public class BitShuffle
*/ */
public static double[] bitUnShuffleDoubleArray(byte[] input) throws IOException { public static double[] bitUnShuffleDoubleArray(byte[] input) throws IOException {
double[] output = new double[input.length / 8]; double[] output = new double[input.length / 8];
if (impl.supportBitSuffle()) { int numProcessed = impl.bitUnShuffle(input, 0, 8, input.length, output, 0);
impl.bitUnShuffle(input, 0, 8, input.length, output, 0); assert(numProcessed == input.length);
} else {
Snappy.arrayCopy(input, 0, input.length, output, 0);
}
return output; return output;
} }
} }

View File

@ -7,14 +7,6 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/*
* Class: org_xerial_snappy_BitShuffleNative
* Method: supportBitSuffle
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_BitShuffleNative_supportBitSuffle
(JNIEnv *, jobject);
/* /*
* Class: org_xerial_snappy_BitShuffleNative * Class: org_xerial_snappy_BitShuffleNative
* Method: bitShuffle * Method: bitShuffle

View File

@ -45,11 +45,6 @@ public class BitShuffleNative
// A quick benchmark result can be found in a gist below; // A quick benchmark result can be found in a gist below;
// https://gist.github.com/maropu/01103215df34b317a7a7 // https://gist.github.com/maropu/01103215df34b317a7a7
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Returns true iff a native library loaded in JVMs can run bit-shuffling.
// Bit-shuffling is executable only in x86 environments that support SSE/AVX instructions.
public native boolean supportBitSuffle();
public native int bitShuffle(Object input, int inputOffset, int typeSize, int byteLength, Object output, int outputOffset) public native int bitShuffle(Object input, int inputOffset, int typeSize, int byteLength, Object output, int outputOffset)
throws IOException; throws IOException;

View File

@ -15,15 +15,13 @@
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <stdint.h>
#include <snappy.h> #include <snappy.h>
#include <bitshuffle.h>
#include "SnappyNative.h" #include "SnappyNative.h"
#include "BitShuffleNative.h" #include "BitShuffleNative.h"
#ifdef SNAPPY_BITSHUFFLE_ENABLED
#include <bitshuffle.h>
#include <stdint.h>
#endif
void throw_exception(JNIEnv *env, jobject self, int errorCode) void throw_exception(JNIEnv *env, jobject self, int errorCode)
{ {
jclass c = env->FindClass("org/xerial/snappy/SnappyNative"); jclass c = env->FindClass("org/xerial/snappy/SnappyNative");
@ -302,20 +300,6 @@ JNIEXPORT void JNICALL Java_org_xerial_snappy_SnappyNative_arrayCopy
env->ReleasePrimitiveArrayCritical((jarray) output, dest, 0); env->ReleasePrimitiveArrayCritical((jarray) output, dest, 0);
} }
/*
* Class: org_xerial_snappy_SnappyNative
* Method: supportBitSuffle
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_BitShuffleNative_supportBitSuffle
(JNIEnv *, jobject) {
#ifdef SNAPPY_BITSHUFFLE_ENABLED
return (jboolean) true;
#else
return (jboolean) false;
#endif
}
/* /*
* Class: org_xerial_snappy_SnappyNative * Class: org_xerial_snappy_SnappyNative
* Method: bitShuffle * Method: bitShuffle
@ -324,7 +308,6 @@ JNIEXPORT jboolean JNICALL Java_org_xerial_snappy_BitShuffleNative_supportBitSuf
JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitShuffle JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitShuffle
(JNIEnv * env, jobject self, jobject input, jint inputOffset, jint typeSize, jint length, jobject output, jint outputOffset) (JNIEnv * env, jobject self, jobject input, jint inputOffset, jint typeSize, jint length, jobject output, jint outputOffset)
{ {
#ifdef SNAPPY_BITSHUFFLE_ENABLED
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0); char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0); char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0);
if(in == 0 || out == 0) { if(in == 0 || out == 0) {
@ -346,11 +329,6 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitShuffle
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
return (jint) processedBytes; return (jint) processedBytes;
#else
// Returns an error code for unsupported operations
throw_exception(env, self, 1);
return (jint) 0;
#endif
} }
/* /*
@ -361,7 +339,6 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitShuffle
JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitUnShuffle JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitUnShuffle
(JNIEnv * env, jobject self, jobject input, jint inputOffset, jint typeSize, jint length, jobject output, jint outputOffset) (JNIEnv * env, jobject self, jobject input, jint inputOffset, jint typeSize, jint length, jobject output, jint outputOffset)
{ {
#ifdef SNAPPY_BITSHUFFLE_ENABLED
char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0); char* in = (char*) env->GetPrimitiveArrayCritical((jarray) input, 0);
char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0); char* out = (char*) env->GetPrimitiveArrayCritical((jarray) output, 0);
if(in == 0 || out == 0) { if(in == 0 || out == 0) {
@ -383,10 +360,5 @@ JNIEXPORT jint JNICALL Java_org_xerial_snappy_BitShuffleNative_bitUnShuffle
env->ReleasePrimitiveArrayCritical((jarray) output, out, 0); env->ReleasePrimitiveArrayCritical((jarray) output, out, 0);
return (jint) processedBytes; return (jint) processedBytes;
#else
// Returns an error code for unsupported operations
throw_exception(env, self, 1);
return (jint) 0;
#endif
} }