--- old/src/java.base/share/classes/java/nio/Bits.java 2015-11-06 15:43:59.924971700 -0800 +++ new/src/java.base/share/classes/java/nio/Bits.java 2015-11-06 15:43:59.821023700 -0800 @@ -820,19 +820,93 @@ copyToShortArray(srcAddr, dst, dstPos, length); } + /** + * Copy from source array into given destination array. + * + * @param src + * the source array, must be a 16-bit primitive array type + * @param srcPos + * offset within source array of the first element to read + * @param dstAddr + * destination address + * @param length + * number of bytes to copy + */ static native void copyFromShortArray(Object src, long srcPos, long dstAddr, long length); + + /** + * Copy from source array into given destination array. + * + * @param srcAddr + * source address + * @param dst + * destination array, must be a 16-bit primitive array type + * @param dstPos + * offset within the destionation array of the first element to write + * @param length + * number of bytes to copy + */ static native void copyToShortArray(long srcAddr, Object dst, long dstPos, long length); + /** + * Copy from source array into given destination array. + * + * @param src + * the source array, must be a 32-bit primitive array type + * @param srcPos + * offset within source array of the first element to read + * @param dstAddr + * destination address + * @param length + * number of bytes to copy + */ static native void copyFromIntArray(Object src, long srcPos, long dstAddr, long length); + + /** + * Copy from source array into given destination array. + * + * @param srcAddr + * source address + * @param dst + * destination array, must be a 32-bit primitive array type + * @param dstPos + * offset within the destionation array of the first element to write + * @param length + * number of bytes to copy + */ static native void copyToIntArray(long srcAddr, Object dst, long dstPos, long length); + /** + * Copy from source array into given destination array. + * + * @param src + * the source array, must be a 64-bit primitive array type + * @param srcPos + * offset within source array of the first element to read + * @param dstAddr + * destination address + * @param length + * number of bytes to copy + */ static native void copyFromLongArray(Object src, long srcPos, long dstAddr, long length); + + /** + * Copy from source array into given destination array. + * + * @param srcAddr + * source address + * @param dst + * destination array, must be a 64-bit primitive array type + * @param dstPos + * offset within the destionation array of the first element to write + * @param length + * number of bytes to copy + */ static native void copyToLongArray(long srcAddr, Object dst, long dstPos, long length); - } --- old/make/lib/CoreLibraries.gmk 2015-11-06 15:44:00.060903700 -0800 +++ new/make/lib/CoreLibraries.gmk 2015-11-06 15:43:59.952957700 -0800 @@ -139,11 +139,11 @@ endif endif -ifeq ($(OPENJDK_TARGET_OS), linux) - ifeq ($(OPENJDK_TARGET_CPU), x86_64) - BUILD_LIBJAVA_Bits.c_CFLAGS := $(C_O_FLAG_NORM) - endif -endif +#ifeq ($(OPENJDK_TARGET_OS), linux) +# ifeq ($(OPENJDK_TARGET_CPU), x86_64) +# BUILD_LIBJAVA_Bits.c_CFLAGS := $(C_O_FLAG_NORM) +# endif +#endif $(eval $(call SetupNativeCompilation,BUILD_LIBJAVA, \ LIBRARY := java, \ --- old/src/java.base/share/native/libjava/Bits.c 2015-11-06 15:44:00.076895700 -0800 +++ new/src/java.base/share/native/libjava/Bits.c 2015-11-06 15:43:59.956955700 -0800 @@ -30,6 +30,7 @@ #include "jni_util.h" #include "jlong.h" #include +#include #define MBYTE 1048576 @@ -46,11 +47,53 @@ (*env)->ReleasePrimitiveArrayCritical(env, obj, bytes, mode); \ } -#define SWAPSHORT(x) ((jshort)(((x) << 8) | (((x) >> 8) & 0xff))) -#define SWAPINT(x) ((jint)((SWAPSHORT((jshort)(x)) << 16) | \ - (SWAPSHORT((jshort)((x) >> 16)) & 0xffff))) -#define SWAPLONG(x) ((jlong)(((jlong)SWAPINT((jint)(x)) << 32) | \ - ((jlong)SWAPINT((jint)((x) >> 32)) & 0xffffffff))) +#if defined(_LITTLE_ENDIAN) +#define IDX(i,n) (n - 1 - i) +#elif defined(_BIG_ENDIAN) +#define IDX(i,n) i +#else +#error "Unknown endian" +#endif + +/** + * Read and byte swap a value + * + * @param src the bytes to read + * @param n the size of the value, in bytes + * + * @return a jlong where the least significant (n*8) bits represent the byte swapped value + */ +static jlong +read_swapped_value(unsigned char* src, size_t n) { + size_t i; + jlong v = 0; + + assert(n <= sizeof(jlong)); + + for (i = 0; i < n; i++) { + v |= (jlong)src[IDX(i, n)] << (i * 8); + } + + return v; +} + +/** + * Write a value to memory, byte swapping first + * + * @param v the value to write, the low (n*8) bits of which will be used + * @param dst the target memory to which to write the swapped value + * @param n the size of the value, in bytes + */ +static void +write_swapped_value(jlong v, unsigned char* dst, size_t n) { + size_t i; + + assert(n <= sizeof(jlong)); + + for (i = 0; i < n; i++) { + dst[i] = (unsigned char)(v >> (IDX(i,n) * 8)); + } +} JNIEXPORT void JNICALL Java_java_nio_Bits_copyFromShortArray(JNIEnv *env, jclass clazz, jobject src, @@ -58,10 +101,10 @@ { jbyte *bytes; size_t size; - jshort *srcShort, *dstShort, *endShort; - jshort tmpShort; + jshort *srcShort, *endShort; + unsigned char* dst; - dstShort = (jshort *)jlong_to_ptr(dstAddr); + dst = jlong_to_ptr(dstAddr); while (length > 0) { size = (length < MBYTE) ? (size_t)length : (size_t)MBYTE; @@ -71,8 +114,8 @@ srcShort = (jshort *)(bytes + srcPos); endShort = srcShort + (size / sizeof(jshort)); while (srcShort < endShort) { - tmpShort = *srcShort++; - *dstShort++ = SWAPSHORT(tmpShort); + write_swapped_value(*srcShort++, dst, sizeof(jshort)); + dst += sizeof(jshort); } RELEASECRITICAL(bytes, env, src, JNI_ABORT); @@ -88,10 +131,10 @@ { jbyte *bytes; size_t size; - jshort *srcShort, *dstShort, *endShort; - jshort tmpShort; + unsigned char *src, *end; + jshort *dstShort; - srcShort = (jshort *)jlong_to_ptr(srcAddr); + src = jlong_to_ptr(srcAddr); while (length > 0) { size = (length < MBYTE) ? (size_t)length : (size_t)MBYTE; @@ -99,10 +142,10 @@ GETCRITICAL_OR_RETURN(bytes, env, dst); dstShort = (jshort *)(bytes + dstPos); - endShort = srcShort + (size / sizeof(jshort)); - while (srcShort < endShort) { - tmpShort = *srcShort++; - *dstShort++ = SWAPSHORT(tmpShort); + end = src + size; + while (src < end) { + *dstShort++ = (jshort)read_swapped_value(src, sizeof(jshort)); + src += sizeof(jshort); } RELEASECRITICAL(bytes, env, dst, 0); @@ -118,10 +161,10 @@ { jbyte *bytes; size_t size; - jint *srcInt, *dstInt, *endInt; - jint tmpInt; + jint *srcInt, *endInt; + unsigned char *dst; - dstInt = (jint *)jlong_to_ptr(dstAddr); + dst = jlong_to_ptr(dstAddr); while (length > 0) { size = (length < MBYTE) ? (size_t)length : (size_t)MBYTE; @@ -131,8 +174,8 @@ srcInt = (jint *)(bytes + srcPos); endInt = srcInt + (size / sizeof(jint)); while (srcInt < endInt) { - tmpInt = *srcInt++; - *dstInt++ = SWAPINT(tmpInt); + write_swapped_value(*srcInt++, dst, sizeof(jint)); + dst += sizeof(jint); } RELEASECRITICAL(bytes, env, src, JNI_ABORT); @@ -148,10 +191,10 @@ { jbyte *bytes; size_t size; - jint *srcInt, *dstInt, *endInt; - jint tmpInt; + unsigned char *src, *end; + jint *dstInt; - srcInt = (jint *)jlong_to_ptr(srcAddr); + src = jlong_to_ptr(srcAddr); while (length > 0) { size = (length < MBYTE) ? (size_t)length : (size_t)MBYTE; @@ -159,10 +202,10 @@ GETCRITICAL_OR_RETURN(bytes, env, dst); dstInt = (jint *)(bytes + dstPos); - endInt = srcInt + (size / sizeof(jint)); - while (srcInt < endInt) { - tmpInt = *srcInt++; - *dstInt++ = SWAPINT(tmpInt); + end = src + size; + while (src < end) { + *dstInt++ = (jint)read_swapped_value(src, sizeof(jint)); + src += sizeof(jint); } RELEASECRITICAL(bytes, env, dst, 0); @@ -178,10 +221,10 @@ { jbyte *bytes; size_t size; - jlong *srcLong, *dstLong, *endLong; - jlong tmpLong; + jlong *srcLong, *endLong; + unsigned char* dst; - dstLong = (jlong *)jlong_to_ptr(dstAddr); + dst = jlong_to_ptr(dstAddr); while (length > 0) { size = (length < MBYTE) ? (size_t)length : (size_t)MBYTE; @@ -191,8 +234,8 @@ srcLong = (jlong *)(bytes + srcPos); endLong = srcLong + (size / sizeof(jlong)); while (srcLong < endLong) { - tmpLong = *srcLong++; - *dstLong++ = SWAPLONG(tmpLong); + write_swapped_value(*srcLong++, dst, sizeof(jlong)); + dst += sizeof(jlong); } RELEASECRITICAL(bytes, env, src, JNI_ABORT); @@ -208,10 +251,10 @@ { jbyte *bytes; size_t size; - jlong *srcLong, *dstLong, *endLong; - jlong tmpLong; + unsigned char *src, *end; + jlong *dstLong; - srcLong = (jlong *)jlong_to_ptr(srcAddr); + src = jlong_to_ptr(srcAddr); while (length > 0) { size = (length < MBYTE) ? (size_t)length : (size_t)MBYTE; @@ -219,10 +262,10 @@ GETCRITICAL_OR_RETURN(bytes, env, dst); dstLong = (jlong *)(bytes + dstPos); - endLong = srcLong + (size / sizeof(jlong)); - while (srcLong < endLong) { - tmpLong = *srcLong++; - *dstLong++ = SWAPLONG(tmpLong); + end = src + size; + while (src < end) { + *dstLong++ = read_swapped_value(src, sizeof(jlong)); + src += sizeof(jlong); } RELEASECRITICAL(bytes, env, dst, 0);