Java "LibraryCompat"

Admin User, erstellt 11. Apr. 2024
         
package nova.envir;
import java.math.BigInteger;
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
* <p/>
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
* <p/>
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
* <p/>
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
* <p/>
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
public final class LibraryCompat {
/********************************************************************/
/* Rounding Operations (Supplement): */
/* divmod/4: divMod() */
/********************************************************************/
/**
* <p>Divide and truncate the two numbers, and also compute modulo.</p>
* <p>The results corresponds to the truncation of the real division.</p>
* <pre>
* divmod(X,Y,Z,T) where
* Z = floor(X / Y)) and
* T = X - Z * Y
* </pre>
*
* @param a The first operand.
* @param b The second operand.
* @return The division result and the modulo.
*/
public static Number[] divMod(Number a, Number b) {
if (a instanceof Integer && b instanceof Integer) {
int u = b.intValue();
if (u == 0)
throw new ArithmeticException(ComputeElem.OP_EVALUATION_ZERO_DIVISOR);
int v = a.intValue();
if (v == Integer.MIN_VALUE && u == -1) {
Number[] res = new Number[2];
res[0] = ComputeElem.NEG_MIN_INTEGER;
res[1] = Integer.valueOf(0);
return res;
} else {
int[] res2 = divMod(v, u);
Number[] res = new Number[2];
res[0] = Integer.valueOf(res2[0]);
res[1] = Integer.valueOf(res2[1]);
return res;
}
} else {
BigInteger p = ComputeElem.widen_bigint(b);
if (p.signum() == 0)
throw new ArithmeticException(ComputeElem.OP_EVALUATION_ZERO_DIVISOR);
BigInteger[] res2 = divMod(ComputeElem.widen_bigint(a), p);
Number[] res = new Number[2];
res[0] = ComputeElem.norm_bigint(res2[0]);
res[1] = ComputeElem.norm_bigint(res2[1]);
return res;
}
}
/**
* <p>Compute the divmod.</p>
*
* @param v The numerator.
* @param u The denumerator.
* @return The div and mod.
*/
private static BigInteger[] divMod(BigInteger v, BigInteger u) {
BigInteger[] res = v.divideAndRemainder(u);
if ((v.signum() < 0) != (u.signum() < 0)) {
if (res[1].signum() != 0) {
res[0] = res[0].subtract(BigInteger.ONE);
res[1] = res[1].add(u);
}
}
return res;
}
/**
* <p>Compute the divmod.</p>
*
* @param v The numerator.
* @param u The denumerator.
* @return The div and mod.
*/
private static int[] divMod(int v, int u) {
int[] res = new int[2];
res[0] = v / u;
res[1] = v % u;
if ((v < 0) != (u < 0)) {
if (res[1] != 0) {
res[0]--;
res[1] += u;
}
}
return res;
}
/********************************************************************/
/* Additional Binary Number Built-in: */
/* testbit/2: sysTestBit() */
/********************************************************************/
/**
* <p>Test a bit.</p>
*
* @param m The first operand.
* @param n The second operand.
* @return The result.
*/
public static boolean testBit(Number m, Number n) {
if (n instanceof BigInteger)
throw new ArithmeticException("representation_error");
int k = n.intValue();
if (k < 0)
throw new RangeException(ComputeElem.OP_DOMAIN_NOT_LESS_THAN_ZERO, n);
if (m instanceof Integer) {
if (k < 31) {
return (m.intValue() & (1 << k)) != 0;
} else {
return m.intValue() < 0;
}
} else {
return ((BigInteger) m).testBit(k);
}
}
/********************************************************************/
/* Additional Unary Number Operations: */
/* msb/1: msb() */
/* lsb/1: lsb() */
/********************************************************************/
/**
* <p>The index of the most significant bit.</p>
*
* @param m The number.
* @return The msb.
*/
public static int msb(Number m) {
if (m instanceof Integer) {
int x = m.intValue();
return 31 - Integer.numberOfLeadingZeros((x < 0 ? ~x : x));
} else {
return ((BigInteger) m).bitLength() - 1;
}
}
/**
* <p>The lowest set bit of this number.</p>
*
* @param m The operand.
* @return The result.
*/
public static int lsb(Number m) {
if (m instanceof Integer) {
int x = m.intValue();
if (x == 0) {
return -1;
} else {
return Integer.numberOfTrailingZeros(x);
}
} else {
return ((BigInteger) m).getLowestSetBit();
}
}
}