Admin User, created Apr 26. 2025
package foreign.util;
import nova.eval;
import nova.Machine;
import nova.special;
import nova.Store;
import nova.envir.LibraryMath;
/**
* Foreign functions for library(util/math)
* <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(F):
* The predicate succeeds in F 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(LibraryMath.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(LibraryMath.lsb(alpha));
}
/**
* popcount(X):
* If X is an integer, then the function returns the number of ones.
*/
private static Number arit_popcount(Object[] args) {
Number alpha = Machine.exec_eval(args[0]);
special.check_integer(alpha);
return Integer.valueOf(LibraryMath.popcount(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 LibraryMath.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 = LibraryMath.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("popcount", 2, special.make_arithmetic(bitlib::arit_popcount));
Store.add("testbit", 2, special.make_check(bitlib::test_testbit));
Store.add("divmod", 4, special.make_check(bitlib::test_divmod));
}
}