/**
* 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.
*
* 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.
*
* 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.
*
* 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.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
import {
set, deref, is_structure, make_check, MAX_ARITY,
check_nil, exec_build, exec_unify, check_integer,
check_nonvar, copy_term, make_error, Compound
} from "../../nova/core.mjs";
import {
int32
} from "./xbarlib.mjs";
import {
MAX_DATA
} from "./spin.mjs";
/******************************************************************/
/* GPU Integer Arrays */
/******************************************************************/
/**
* test_gpu_buf_new(A, B): internal only
* The predicate succeeds in H with the buffer from the list D.
*/
function test_gpu_buf_new(args) {
let alpha = exec_build(args[0]);
let res = gpu_list_ints(alpha);
return exec_unify(args[1], res);
}
function gpu_list_ints(obj) {
let peek = obj;
let i = 0;
while (is_structure(peek) &&
"." === peek.functor &&
peek.args.length === 2 &&
i < MAX_ARITY) {
i++;
peek = deref(peek.args[1]);
}
check_nil(peek);
let buf = new ArrayBuffer(i * 4);
let args = new Int32Array(buf);
peek = obj;
i = 0;
while (is_structure(peek) &&
"." === peek.functor &&
peek.args.length === 2) {
let val = deref(peek.args[0]);
check_integer(val);
args[i++] = int32(val);
peek = deref(peek.args[1]);
}
return args;
}
/******************************************************************/
/* GPU Flit Interface */
/******************************************************************/
function Flit() {
/* */
}
/**
* test_gpu_flit_new(A): internal only
* The predicate succeeds in A with a new channel.
*/
function test_gpu_flit_new(args) {
let res = new Flit();
return exec_unify(args[0], res);
}
/******************************************************************/
/* GPU Flit Arrays */
/******************************************************************/
/**
* test_gpu_comm_new(A, B): internal only
* The predicate succeeds in H with the table from the list D.
*/
function test_gpu_comm_new(args) {
let alpha = exec_build(args[0]);
let list = gpu_list_comm(alpha);
let len = Math.max(1, list.length);
let buf = new ArrayBuffer(len*(3+MAX_DATA)*4);
let res = new Int32Array(buf);
for (let i = 0; i < len; i++)
res[i*(3+MAX_DATA)+1] = 1;
return exec_unify(args[1], res);
}
function gpu_list_comm(obj) {
let peek = obj;
let i = 0;
while (is_structure(peek) &&
"." === peek.functor &&
peek.args.length === 2 &&
i < MAX_ARITY) {
i++;
peek = deref(peek.args[1]);
}
check_nil(peek);
let args = new Array(i);
peek = obj;
i = 0;
while (is_structure(peek) &&
"." === peek.functor &&
peek.args.length === 2) {
let val = deref(peek.args[0]);
check_flit(val);
args[i++] = val;
peek = deref(peek.args[1]);
}
return args;
}
function check_flit(beta) {
if (!(beta instanceof Flit)) {
check_nonvar(beta);
beta = copy_term(beta);
throw make_error(new Compound("type_error",
["flit", beta]));
}
}
/******************************************************************/
/* Tin Lib Init */
/******************************************************************/
export function main() {
set("gpu_buf_new", 2, make_check(test_gpu_buf_new));
set("gpu_flit_new", 1, make_check(test_gpu_flit_new));
set("gpu_comm_new", 2, make_check(test_gpu_comm_new));
}