merge of '3c853434727997386e691eed12478317c5c5b918'

and 'e62ecd3b1887712e2408f2c716873e9433ee2e4f'
This commit is contained in:
dev
2015-06-13 19:57:10 +00:00
2 changed files with 61 additions and 69 deletions

View File

@@ -52,8 +52,9 @@ abstract class CPUIDCPUInfo implements CPUInfo
*/ */
public boolean hasAVX() public boolean hasAVX()
{ {
return (CPUID.getECXCPUFlags() & (1 << 28)) != 0 && //AVX: ECX Bit 28 int ecx = CPUID.getECXCPUFlags();
(CPUID.getECXCPUFlags() & (1 << 27)) != 0; //XSAVE enabled by OS: ECX Bit 27 return (ecx & (1 << 28)) != 0 && //AVX: ECX Bit 28
(ecx & (1 << 27)) != 0; //XSAVE enabled by OS: ECX Bit 27
} }
/** /**

View File

@@ -98,6 +98,8 @@ public class NativeBigInteger extends BigInteger {
private static boolean _nativeOk; private static boolean _nativeOk;
/** is native lib loaded and at least version 3? */ /** is native lib loaded and at least version 3? */
private static boolean _nativeOk3; private static boolean _nativeOk3;
/** is native lib loaded and at least version 3, and GMP at least version 5? */
private static boolean _nativeCTOk;
private static int _jbigiVersion; private static int _jbigiVersion;
private static String _libGMPVersion = "unknown"; private static String _libGMPVersion = "unknown";
private static String _loadStatus = "uninitialized"; private static String _loadStatus = "uninitialized";
@@ -144,12 +146,12 @@ public class NativeBigInteger extends BigInteger {
* 64-bit processors will fallback to athlon64 and athlon in getResourceList(). * 64-bit processors will fallback to athlon64 and athlon in getResourceList().
* @since 0.8.7 * @since 0.8.7
*/ */
private final static String JBIGI_OPTIMIZATION_PENTIUMM = "pentiumm";
private final static String JBIGI_OPTIMIZATION_ATOM = "atom"; private final static String JBIGI_OPTIMIZATION_ATOM = "atom";
private final static String JBIGI_OPTIMIZATION_CORE2 = "core2"; private final static String JBIGI_OPTIMIZATION_CORE2 = "core2";
private final static String JBIGI_OPTIMIZATION_COREI = "corei"; private final static String JBIGI_OPTIMIZATION_COREI = "corei";
private final static String JBIGI_OPTIMIZATION_GEODE = "geode"; private final static String JBIGI_OPTIMIZATION_GEODE = "geode";
private final static String JBIGI_OPTIMIZATION_NANO = "nano"; private final static String JBIGI_OPTIMIZATION_NANO = "nano";
private final static String JBIGI_OPTIMIZATION_PENTIUMM = "pentiumm";
/** all libjbibi builds are identical to pentium3, case handled in getMiddleName2() */ /** all libjbibi builds are identical to pentium3, case handled in getMiddleName2() */
private final static String JBIGI_OPTIMIZATION_VIAC32 = "viac32"; private final static String JBIGI_OPTIMIZATION_VIAC32 = "viac32";
/** /**
@@ -227,10 +229,10 @@ public class NativeBigInteger extends BigInteger {
JBIGI_OPTIMIZATION_COREI, JBIGI_OPTIMIZATION_CORE2, JBIGI_OPTIMIZATION_PENTIUMM, JBIGI_OPTIMIZATION_COREI, JBIGI_OPTIMIZATION_CORE2, JBIGI_OPTIMIZATION_PENTIUMM,
JBIGI_OPTIMIZATION_PENTIUM3, JBIGI_OPTIMIZATION_X86}; JBIGI_OPTIMIZATION_PENTIUM3, JBIGI_OPTIMIZATION_X86};
/** /**
* The mapping between CPU architecture and it's compatibility list. * The mapping between CPU architecture and its compatibility list.
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
private final static HashMap<String, String[] > JBIGI_COMPAT_MAP = new HashMap<String, String[]>(){{ private final static HashMap<String, String[]> JBIGI_COMPAT_MAP = new HashMap<String, String[]>() {{
put(JBIGI_OPTIMIZATION_PPC, JBIGI_COMPAT_LIST_PPC); put(JBIGI_OPTIMIZATION_PPC, JBIGI_COMPAT_LIST_PPC);
put(JBIGI_OPTIMIZATION_ARM_ARMV5, JBIGI_COMPAT_LIST_ARM); put(JBIGI_OPTIMIZATION_ARM_ARMV5, JBIGI_COMPAT_LIST_ARM);
@@ -316,7 +318,7 @@ public class NativeBigInteger extends BigInteger {
private static final String _libPrefix = (_isWin || _isOS2 ? "" : "lib"); private static final String _libPrefix = (_isWin || _isOS2 ? "" : "lib");
private static final String _libSuffix = (_isWin || _isOS2 ? ".dll" : _isMac ? ".jnilib" : ".so"); private static final String _libSuffix = (_isWin || _isOS2 ? ".dll" : _isMac ? ".jnilib" : ".so");
private final static String sCPUType; private final static String sCPUType; //The CPU Type to optimize for (one of the above strings)
static { static {
sCPUType = resolveCPUType(); sCPUType = resolveCPUType();
@@ -331,18 +333,18 @@ public class NativeBigInteger extends BigInteger {
private static String resolveCPUType() { private static String resolveCPUType() {
if(_isX86) { if(_isX86) {
try { try {
System.out.println("resolveType() x86"); //System.out.println("resolveType() x86");
CPUInfo c = CPUID.getInfo(); CPUInfo c = CPUID.getInfo();
try { try {
_cpuModel = c.getCPUModelString(); _cpuModel = c.getCPUModelString();
System.out.println("CPUModel: " + _cpuModel.toString()); //System.out.println("CPUModel: " + _cpuModel.toString());
} catch (UnknownCPUException e) {} } catch (UnknownCPUException e) {}
if (c instanceof VIACPUInfo){ if (c instanceof VIACPUInfo) {
VIACPUInfo viacpu = (VIACPUInfo) c; VIACPUInfo viacpu = (VIACPUInfo) c;
if (viacpu.IsNanoCompatible()) if (viacpu.IsNanoCompatible())
return JBIGI_OPTIMIZATION_NANO; return JBIGI_OPTIMIZATION_NANO;
return JBIGI_OPTIMIZATION_VIAC3; return JBIGI_OPTIMIZATION_VIAC3;
} else if(c instanceof AMDCPUInfo) { } else if (c instanceof AMDCPUInfo) {
AMDCPUInfo amdcpu = (AMDCPUInfo) c; AMDCPUInfo amdcpu = (AMDCPUInfo) c;
if (amdcpu.IsExcavatorCompatible()) if (amdcpu.IsExcavatorCompatible())
return JBIGI_OPTIMIZATION_EXCAVATOR; return JBIGI_OPTIMIZATION_EXCAVATOR;
@@ -400,17 +402,14 @@ public class NativeBigInteger extends BigInteger {
return null; return null;
} }
} else if (_isArm) { } else if (_isArm) {
if (_isWin)
return null;
Map<String, String> cpuinfo = getCPUInfo(); Map<String, String> cpuinfo = getCPUInfo();
String implementer = cpuinfo.get("cpu implementer"); String implementer = cpuinfo.get("cpu implementer");
String features = cpuinfo.get("features");
String part = cpuinfo.get("cpu part"); String part = cpuinfo.get("cpu part");
if (implementer == null || part == null) {
return null;
}
// If CPU implementer is ARM // If CPU implementer is ARM
if (implementer.contains("0x41")) { if (implementer != null && part != null && implementer.contains("0x41")) {
if (part.contains("0xc0f")) { if (part.contains("0xc0f")) {
return JBIGI_OPTIMIZATION_ARM_CORTEX_A15; return JBIGI_OPTIMIZATION_ARM_CORTEX_A15;
} else if (part.contains("0xc0e")) { } else if (part.contains("0xc0e")) {
@@ -433,24 +432,34 @@ public class NativeBigInteger extends BigInteger {
} }
// We couldn't identify the implementer // We couldn't identify the implementer
// Let's try by looking at model name // Let's try by looking at cpu arch
String arch = cpuinfo.get("cpu architecture");
String model = cpuinfo.get("model name"); String model = cpuinfo.get("model name");
if (model != null && model.contains("ARMv7")) { if (arch != null) {
return JBIGI_OPTIMIZATION_ARM_ARMV7; //CPU architecture: 5TEJ
} else if (model != null && model.contains("ARMv6")) { //CPU architecture: 7
if (arch.startsWith("7")) {
// Raspberry Pi workaround
// Processor : ARMv6-compatible processor rev 7 (v6l)
// CPU architecture: 7
if (model != null && model.contains("ARMv6"))
return JBIGI_OPTIMIZATION_ARM_ARMV6; return JBIGI_OPTIMIZATION_ARM_ARMV6;
} else if (model != null && model.contains("ARMv5")) { return JBIGI_OPTIMIZATION_ARM_ARMV7;
}
if (arch.startsWith("6"))
return JBIGI_OPTIMIZATION_ARM_ARMV6;
if (arch.startsWith("5"))
return JBIGI_OPTIMIZATION_ARM_ARMV5; return JBIGI_OPTIMIZATION_ARM_ARMV5;
} }
// We couldn't identify the model name // We couldn't identify the architecture
// Let's try by looking at cpu arch // Let's try by looking at model name
String arch = cpuinfo.get("cpu architecture"); if (model != null) {
if (arch != null && arch.equals("7")) { if (model.contains("ARMv7"))
return JBIGI_OPTIMIZATION_ARM_ARMV7; return JBIGI_OPTIMIZATION_ARM_ARMV7;
} else if (arch != null && arch.equals("6")) { if (model.contains("ARMv6"))
return JBIGI_OPTIMIZATION_ARM_ARMV6; return JBIGI_OPTIMIZATION_ARM_ARMV6;
} else if (arch != null && arch.equals("5")) { if (model.contains("ARMv5"))
return JBIGI_OPTIMIZATION_ARM_ARMV5; return JBIGI_OPTIMIZATION_ARM_ARMV5;
} }
@@ -472,7 +481,7 @@ public class NativeBigInteger extends BigInteger {
* big endian twos complement representation of the exponent * big endian twos complement representation of the exponent
* @param modulus * @param modulus
* big endian twos complement representation of the modulus * big endian twos complement representation of the modulus
* @throws ArithmeticException if modulus &lt;= 0 (since libjbigi version3) * @throws ArithmeticException if modulus &lt;= 0 (since libjbigi version 3)
* @return big endian twos complement representation of (base ^ exponent) % modulus * @return big endian twos complement representation of (base ^ exponent) % modulus
*/ */
private native static byte[] nativeModPow(byte base[], byte exponent[], byte modulus[]); private native static byte[] nativeModPow(byte base[], byte exponent[], byte modulus[]);
@@ -559,6 +568,7 @@ public class NativeBigInteger extends BigInteger {
int min = nativeGMPMinorVersion(); int min = nativeGMPMinorVersion();
int pat = nativeGMPPatchVersion(); int pat = nativeGMPPatchVersion();
_libGMPVersion = maj + "." + min + "." + pat; _libGMPVersion = maj + "." + min + "." + pat;
_nativeCTOk = maj >= 5;
} catch (Throwable t) { } catch (Throwable t) {
warn("jbigi version " + _jbigiVersion + " but GMP version not available???", t); warn("jbigi version " + _jbigiVersion + " but GMP version not available???", t);
} }
@@ -635,10 +645,10 @@ public class NativeBigInteger extends BigInteger {
/** /**
* @throws ArithmeticException if m &lt;= 0 * @throws ArithmeticException if m &lt;= 0
* @since 0.9.18 and libjbigi version 3 * @since 0.9.18 and libjbigi version 3 and GMP version 5
*/ */
public BigInteger modPowCT(BigInteger exponent, BigInteger m) { public BigInteger modPowCT(BigInteger exponent, BigInteger m) {
if (_nativeOk3) if (_nativeCTOk)
return new NativeBigInteger(nativeModPowCT(toByteArray(), exponent.toByteArray(), m.toByteArray())); return new NativeBigInteger(nativeModPowCT(toByteArray(), exponent.toByteArray(), m.toByteArray()));
else else
return modPow(exponent, m); return modPow(exponent, m);
@@ -655,7 +665,6 @@ public class NativeBigInteger extends BigInteger {
// Note that 'this' can be negative // Note that 'this' can be negative
// If this and m are not coprime, gmp will do a divide by zero exception and crash the JVM. // If this and m are not coprime, gmp will do a divide by zero exception and crash the JVM.
// super will throw an ArithmeticException // super will throw an ArithmeticException
// if (_nativeOk3 && m.signum() > 0)
if (_nativeOk3) if (_nativeOk3)
return new NativeBigInteger(nativeModInverse(toByteArray(), m.toByteArray())); return new NativeBigInteger(nativeModInverse(toByteArray(), m.toByteArray()));
else else
@@ -975,25 +984,6 @@ public class NativeBigInteger extends BigInteger {
_loadStatus = s; _loadStatus = s;
} }
/** @since 0.9.21 */
private static void crit(String s) {
crit(s, null);
}
/** @since 0.9.21 */
private static void crit(String s, Throwable t) {
if(_doLog) {
System.err.println("CRITICAL: " + s);
if (t != null)
t.printStackTrace();
}
I2PAppContext.getGlobalContext().logManager().getLog(NativeBigInteger.class).error(s, t);
if (t != null)
_loadStatus = s + ' ' + t;
else
_loadStatus = s;
}
/** /**
* <p>Try loading it from an explictly build jbigi.dll / libjbigi.so first, before * <p>Try loading it from an explictly build jbigi.dll / libjbigi.so first, before
* looking into a jbigi.jar for any other libraries.</p> * looking into a jbigi.jar for any other libraries.</p>
@@ -1103,18 +1093,17 @@ public class NativeBigInteger extends BigInteger {
* Generate a list of resources to search for, in-order. * Generate a list of resources to search for, in-order.
* See loadNative() comments for more info. * See loadNative() comments for more info.
* @return non-null * @return non-null
* @since 0.9.21 * @since 0.8.7
*/ */
private static List<String> getResourceList(){ private static List<String> getResourceList() {
if (_isAndroid)
return Collections.emptyList();
List<String> rv = new ArrayList<String>(20); List<String> rv = new ArrayList<String>(20);
String primary = getMiddleName2(true); String primary = getMiddleName2(true);
String[] compatList = JBIGI_COMPAT_MAP.get(primary); String[] compatList = JBIGI_COMPAT_MAP.get(primary);
if (_isAndroid) {
return Collections.emptyList();
}
if (primary != null && compatList == null) { if (primary != null && compatList == null) {
crit("A bug relating to how jbigi is loaded for \"" + primary + "\" has been spotted"); error("A bug relating to how jbigi is loaded for \"" + primary + "\" has been spotted");
} }
if (primary != null && if (primary != null &&
@@ -1128,22 +1117,22 @@ public class NativeBigInteger extends BigInteger {
} }
} }
for (; i < compatList.length; ++i) { for (; i < compatList.length; ++i) {
String middle = getMiddleName1();
if (_is64) { if (_is64) {
rv.add(_libPrefix + getMiddleName1() + compatList[i] + "_64" + _libSuffix); rv.add(_libPrefix + middle + compatList[i] + "_64" + _libSuffix);
} }
rv.add(_libPrefix + getMiddleName1() + compatList[i] + _libSuffix); rv.add(_libPrefix + middle + compatList[i] + _libSuffix);
} }
if (rv.size() <= 0) { if (rv.isEmpty()) {
error("Couldn't find the arch \"" + primary + "\" in it's compatibility map \"" + compatList.toString() + "\""); error("Couldn't find the arch \"" + primary + "\" in its compatibility map \"" + compatList.toString() + "\"");
} }
} else { } else {
error("Couldn't find the compatibility map for arch \"" + primary + "\""); error("Couldn't find the compatibility map for arch \"" + primary + "\"");
} }
//System.out.println("Primary: " + primary);
System.out.println("Primary: " + primary); //System.out.println("ResourceList: " + rv.toString());
System.out.println("ResourceList: " + rv.toString());
return rv; return rv;
} }
@@ -1181,12 +1170,14 @@ public class NativeBigInteger extends BigInteger {
/** /**
* @return may be null if optimized is true * @return may be null if optimized is true
*/ */
/****
private static final String getResourceName(boolean optimized) { private static final String getResourceName(boolean optimized) {
String middle = getMiddleName(optimized); String middle = getMiddleName(optimized);
if (middle == null) if (middle == null)
return null; return null;
return _libPrefix + middle + _libSuffix; return _libPrefix + middle + _libSuffix;
} }
****/
/** /**
* @return may be null if optimized is true; returns jbigi-xxx-none if optimize is false * @return may be null if optimized is true; returns jbigi-xxx-none if optimize is false