Skip to content

Commit

Permalink
Fixed bitwise impl error
Browse files Browse the repository at this point in the history
- Improve selector UI
- Add bitwise cast saturating
  • Loading branch information
A4-Tacks committed Sep 28, 2024
1 parent 9e8210b commit 4641e78
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Unless otherwise specified, return the value before the memory operation
| max | `m = max(m, n)` |
| min | `m = min(m, n)` |
| and | `m &= n` |
| nand | `m = !(m & n)` |
| nand | `m = ~(m & n)` |
| or | `m `|= n |
| xor | `m ^= n` |
| shl | `m <<= n` |
Expand Down
4 changes: 2 additions & 2 deletions src/mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "logic-atomics",
"displayName": "Logic-Atomics",
"author": "A4-Tacks",
"description": "Atomic operation instruction set extension.",
"version": "0.1.1",
"description": "Processor atomic operation extension.",
"version": "0.2.0",
"minGameVersion": 136
}
60 changes: 33 additions & 27 deletions src/scripts/inst/memory-atomics.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,24 @@ const cvar = (exec, type, id, ext) => {
return ext === undefined ? id[type]() : id[type](ext);
};

const BigInt = java.math.BigInteger;
const Double = java.lang.Double;
const BigIntMin = java.math.BigInteger('-9223372036854775808');
const BigIntMax = java.math.BigInteger( '9223372036854775807');
const LongMin = Double.valueOf(BigIntMin);
const LongMax = Double.valueOf(BigIntMax);

const boundLong = num => (
num < LongMin ? LongMin :
num > LongMax ? LongMax :
Double(num)
);
const BigInt = num => {
if (BigIntMin >= num) return BigIntMin;
if (BigIntMax <= num) return BigIntMax;

const bounded = boundLong(num);
return java.math.BigInteger.valueOf(bounded);
};


const DEFAULT_RESULT = null;
Expand Down Expand Up @@ -101,7 +118,7 @@ const OPTIONS = {
run(_exec, mem, index, args) {
let {num} = args;
let old = mem[index];
mem[index] = BigInt(num).and(BigInt(old));
mem[index] = BigInt(old).and(BigInt(num));
return old;
},
},
Expand All @@ -112,7 +129,7 @@ const OPTIONS = {
run(_exec, mem, index, args) {
let {num} = args;
let old = mem[index];
mem[index] = BigInt(num).andNot(BigInt(old));
mem[index] = boundLong(BigInt(old).andNot(BigInt(num)));
return old;
},
},
Expand All @@ -123,7 +140,7 @@ const OPTIONS = {
run(_exec, mem, index, args) {
let {num} = args;
let old = mem[index];
mem[index] = BigInt(num).or(BigInt(old));
mem[index] = boundLong(BigInt(old).or(BigInt(num)));
return old;
},
},
Expand All @@ -134,7 +151,7 @@ const OPTIONS = {
run(_exec, mem, index, args) {
let {num} = args;
let old = mem[index];
mem[index] = BigInt(num).xor(BigInt(old));
mem[index] = BigInt(old).xor(BigInt(num));
return old;
},
},
Expand All @@ -145,7 +162,7 @@ const OPTIONS = {
run(_exec, mem, index, args) {
let {num} = args;
let old = mem[index];
mem[index] = BigInt(num).shiftLeft(BigInt(old));
mem[index] = boundLong(BigInt(old).shiftLeft(BigInt(num)));
return old;
},
},
Expand All @@ -156,15 +173,15 @@ const OPTIONS = {
run(_exec, mem, index, args) {
let {num} = args;
let old = mem[index];
mem[index] = BigInt(num).shiftRight(BigInt(old));
mem[index] = BigInt(old).shiftRight(BigInt(num));
return old;
},
},
flip: {
args: {},
run(_exec, mem, index, _args) {
let old = mem[index];
mem[index] = BigInt(old).flipBit();
mem[index] = boundLong(BigInt(old).flipBit());
return old;
},
},
Expand Down Expand Up @@ -208,6 +225,7 @@ const OPTIONS = {
},
},
};
const OPTIONS_KEYS = Object.keys(OPTIONS);

const MemoryAtomicsI = {
_(builder, op, result, mem, index, args) {
Expand Down Expand Up @@ -327,6 +345,9 @@ const MemoryAtomicsStatement = {
table.row();
};

table.clearChildren();
table.left();

add("result");
row();

Expand All @@ -339,14 +360,10 @@ const MemoryAtomicsStatement = {
sep("fetch");

var optb = table.button(this.op, Styles.logict, () => {
this.showSelectTable(optb, (t, hide) => {
let i = 0;
for (var op in OPTIONS) {
let sub = this.setter(table, t, op, hide)
.width(90);
if (i++ & 1) sub.row();
}
});
this.showSelect(optb, OPTIONS_KEYS, this.op, op => {
this.op = op;
this.buildt(table);
}, 4, c => {c.width(90)});
}).width(80).color(table.color).get();

if (!OPTIONS[this.op]) return;
Expand All @@ -366,17 +383,6 @@ const MemoryAtomicsStatement = {
}
},

setter(root, table, op, hide) {
return table.button(op, () => {
if (this.op != op) {
this.op = op;
root.clearChildren();
this.buildt(root);
}
hide.run();
});
},

name: () => "Memory Atomics",
color: () => Pal.logicIo,
category: () => LCategory.io,
Expand Down

0 comments on commit 4641e78

Please sign in to comment.