1
0
Fork 0
forked from fun/fun

Integer/Number and uint(8,16,32,64bit) fun... ;)

This commit is contained in:
Johannes Findeisen 2025-09-16 12:29:28 +02:00
commit 6d3456b62c
6 changed files with 182 additions and 6 deletions

24
src/vm/uclamp.c Normal file
View file

@ -0,0 +1,24 @@
case OP_UCLAMP: {
/* Clamp/wrap the integer on top of the stack to 'bits' width (unsigned) */
Value v = pop_value(vm);
int bits = inst.operand;
uint64_t u = 0;
if (v.type == VAL_INT) {
u = (uint64_t) v.i;
} else {
/* Non-integers clamp to 0 */
u = 0;
}
if (bits > 0 && bits < 64) {
uint64_t mask = (1ULL << bits) - 1ULL;
u &= mask;
} else {
/* bits >= 64 -> no-op; bits <= 0 -> no-op */
}
push_value(vm, make_int((int64_t)u));
free_value(v);
break;
}