JavaScript "spin"

         
/**
* 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.
*/
export const MAX_DATA = 128;
export let util;
function init_util() {
let types = `
fn gpu_lock_tryacquire(sem: ptr<storage, atomic<i32>, read_write>) -> bool {
let value : i32 = atomicLoad(sem);
if (value == 0) {
return false;
}
if (!atomicCompareExchangeWeak(sem, value, value-1).exchanged) {
return false;
}
return true;
}
fn gpu_lock_release(sem: ptr<storage, atomic<i32>, read_write>) {
atomicAdd(sem, 1);
}
fn gpu_data_store(data: ptr<storage, array<i32, ${1+MAX_DATA}>, read_write>,
src: ptr<storage, array<i32>, read_write>, offset: i32, len: i32) {
data[0] = len;
for (var i = 0; i < len; i++) {
data[1+i] = src[offset+i];
}
}
fn gpu_data_load(data: ptr<storage, array<i32, ${1+MAX_DATA}>, read_write>,
dst: ptr<storage, array<i32>, read_write>, offset: i32, len: i32) -> i32 {
let res = data[0];
if (res != len) {
return res;
}
for (var i = 0; i < len; i++) {
dst[offset+i] = data[1+i];
}
return len;
}
struct Flit {
head : atomic<i32>,
tail : atomic<i32>,
data : array<i32, ${1+MAX_DATA}>
}
fn gpu_flit_offer(chan: ptr<storage, Flit, read_write>,
src: ptr<storage, array<i32>, read_write>, offset: i32, len: i32) -> bool {
if (!gpu_lock_tryacquire(&chan.tail)) {
return false;
}
gpu_data_store(&chan.data, src, offset, len);
gpu_lock_release(&chan.head);
return true;
}
fn gpu_flit_poll(chan: ptr<storage, Flit, read_write>,
dst: ptr<storage, array<i32>, read_write>, offset: i32, len: i32) -> i32 {
if (!gpu_lock_tryacquire(&chan.head)) {
return -1;
}
let res = gpu_data_load(&chan.data, dst, offset, len);
gpu_lock_release(&chan.tail);
return res;
}
fn extra_output(obj: i32, base: i32) -> i32 {
let chan: ptr<storage, Flit, read_write> = &comm[obj];
return select(0, 1, gpu_flit_offer(chan, &state, base+1, state[base]));
}
fn extra_input(obj: i32, base: i32) -> i32 {
let chan: ptr<storage, Flit, read_write> = &comm[obj];
let len = state[base];
let res = gpu_flit_poll(chan, &state, base+1, state[base])
if (res == -1) {
return 0;
}
if (res != len) {
state[base] = res
}
return 1
}
`
return types;
}
util = init_util();