Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
sclamp.c File Reference

Implements the OP_SCLAMP opcode for signed N-bit two's-complement wrapping. More...

Go to the source code of this file.

Functions

 if (bits<=0)
 if (bits >=64)
 if (wrapped &sign_bit)
 push_value (vm, make_int(out))
 free_value (v)

Variables

case OP_SCLAMP
int bits = inst.operand
int64_t vi = (v.type == VAL_INT) ? v.i : 0
int64_t out = 0
 else
uint64_t wrapped = ((uint64_t)vi) & mask
 break

Detailed Description

Implements the OP_SCLAMP opcode for signed N-bit two's-complement wrapping.

This VM opcode wraps an integer into the signed N-bit range using two's complement semantics. It first masks to N bits and then sign-extends if the sign bit is set, yielding a value in the range [-2^(N-1) .. 2^(N-1)-1]. N is provided by the instruction operand.

Stack contract:

  • Pops: value (int)
  • Pushes: value (int)

Definition in file sclamp.c.

Function Documentation

◆ free_value()

free_value(v)

◆ if() [1/3]

if(bits >=64)

Definition at line 39 of file sclamp.c.

◆ if() [2/3]

if(bits<=0)

Definition at line 34 of file sclamp.c.

◆ if() [3/3]

if(wrapped &sign_bit)

Definition at line 44 of file sclamp.c.

◆ push_value()

push_value(vm,
make_int(out) )

Variable Documentation

◆ bits

int bits = inst.operand

Definition at line 30 of file sclamp.c.

◆ break

break

Definition at line 55 of file sclamp.c.

◆ else

else
Initial value:
{
uint64_t mask = (bits >= 64) ? UINT64_MAX : ((1ULL << bits) - 1ULL)
int bits
Definition sclamp.c:30
uint64_t mask
Definition uclamp.c:29

Definition at line 36 of file sclamp.c.

◆ OP_SCLAMP

case OP_SCLAMP

Definition at line 24 of file sclamp.c.

◆ out

int64_t out = 0

Definition at line 33 of file sclamp.c.

◆ vi

int64_t vi = (v.type == VAL_INT) ? v.i : 0

Definition at line 31 of file sclamp.c.

◆ wrapped

uint64_t wrapped = ((uint64_t)vi) & mask

Definition at line 38 of file sclamp.c.