1
0
Fork 0
forked from fun/fun

Classes are a real type class now and some fixes. (0.10.0)

This commit is contained in:
Johannes Findeisen 2025-09-28 23:56:45 +02:00
commit 5d3cfd9f65
12 changed files with 203 additions and 41 deletions

View file

@ -8,32 +8,27 @@
*/
case OP_SCLAMP: {
/* Clamp/wrap to signed N-bit range: [-2^(N-1), 2^(N-1)-1] */
/* Saturating clamp to signed N-bit range: [-2^(N-1) .. 2^(N-1)-1] */
Value v = pop_value(vm);
int bits = inst.operand;
uint64_t u = 0;
if (v.type == VAL_INT) {
u = (uint64_t)v.i;
} else {
u = 0;
}
int64_t vi = (v.type == VAL_INT) ? v.i : 0;
int64_t smin, smax;
if (bits <= 0) {
/* treat as no-op for invalid widths */
} else if (bits >= 64) {
/* 64-bit signed: nothing to mask, keep as is */
smin = 0; smax = 0;
} else if (bits >= 63) {
/* cover full int64_t domain for 63+ bits */
smin = INT64_MIN;
smax = INT64_MAX;
} else {
uint64_t mask = (1ULL << bits) - 1ULL;
u &= mask;
uint64_t sign_bit = 1ULL << (bits - 1);
if (u & sign_bit) {
/* negative value in two's complement */
u -= (1ULL << bits);
}
smin = -(1LL << (bits - 1));
smax = (1LL << (bits - 1)) - 1LL;
}
push_value(vm, make_int((int64_t)u));
if (vi < smin) vi = smin;
else if (vi > smax) vi = smax;
push_value(vm, make_int(vi));
free_value(v);
break;
}