Java "bitlib"

Admin User, created Apr 11. 2024
         
package liblet.util;
import nova.envir.LibraryCompat;
import nova.Eval;
import nova.Machine;
import nova.Special;
import nova.Store;
/**
* Modern Albufeira Prolog Interpreter
* <p>
* 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 bitlib {
/******************************************************************/
/* Random Numbers */
/******************************************************************/
/**
* random(A):
* The predicate succeeds in A with a uniform random 64-bit
* floating point value in the interval [0..1).
*/
private static boolean test_random(Object[] args) {
Object alpha = Double.valueOf(Math.random());
return Machine.exec_unify(args[0], alpha);
}
/******************************************************************/
/* msb/2, lsb/2 and testbit/2 */
/******************************************************************/
/**
* msb(X):
* If X is an integer, then the function returns the most significant bit.
*/
private static Number arit_msb(Object[] args) {
Number alpha = Machine.exec_eval(args[0]);
Special.check_integer(alpha);
return Integer.valueOf(LibraryCompat.msb(alpha));
}
/**
* lsb(X):
* If X is an integer, then the function returns the least significant bit.
*/
private static Number arit_lsb(Object[] args) {
Number alpha = Machine.exec_eval(args[0]);
Special.check_integer(alpha);
return Integer.valueOf(LibraryCompat.lsb(alpha));
}
/**
* testbit(X, Y):
* The predicate succeeds when X /\ (1 << Y) =\= 0.
*/
private static boolean test_testbit(Object[] args) {
try {
Object alpha = Machine.deref(Machine.exec_build(args[0]));
Special.check_integer(alpha);
Object beta = Machine.deref(Machine.exec_build(args[1]));
Special.check_integer(beta);
return LibraryCompat.testBit((Number) alpha, (Number) beta);
} catch (RuntimeException x) {
throw Eval.mapRuntime(x);
}
}
/******************************************************************/
/* divmod/4 */
/******************************************************************/
/**
* divmod(X, Y, Z, T):
* If X and Y are both integers then the predicate succeeds in
* Z with the division of X by Y, and in T with the modulo of X by Y.
*/
private static boolean test_divmod(Object[] args) {
try {
Object alpha = Machine.deref(Machine.exec_build(args[0]));
Special.check_integer(alpha);
Object beta = Machine.deref(Machine.exec_build(args[1]));
Special.check_integer(beta);
Number[] res = LibraryCompat.divMod((Number) alpha, (Number) beta);
if (!Machine.exec_unify(args[2], res[0]))
return false;
return Machine.exec_unify(args[3], res[1]);
} catch (RuntimeException x) {
throw Eval.mapRuntime(x);
}
}
/******************************************************************/
/* Bit Lib Init */
/******************************************************************/
public static void main() {
Store.add("random", 1, Special.make_check(bitlib::test_random));
Store.add("msb", 2, Special.make_arithmetic(bitlib::arit_msb));
Store.add("lsb", 2, Special.make_arithmetic(bitlib::arit_lsb));
Store.add("testbit", 2, Special.make_check(bitlib::test_testbit));
Store.add("divmod", 4, Special.make_check(bitlib::test_divmod));
}
}