Fun
0.41.5
The programming language that makes you have fun!
Main Page
Data Structures
Files
File List
Globals
Loading...
Searching...
No Matches
src
vm
uclamp.c
Go to the documentation of this file.
1
/*
2
* This file is part of the Fun programming language.
3
* https://fun-lang.xyz/
4
*
5
* Copyright 2025 Johannes Findeisen <you@hanez.org>
6
* Licensed under the terms of the Apache-2.0 license.
7
* https://opensource.org/license/apache-2-0
8
*/
9
10
/**
11
* @file uclamp.c
12
* @brief Implements the OP_UCLAMP opcode for unsigned N-bit wrapping.
13
*
14
* This VM opcode masks an integer to the lower N bits (N taken from the
15
* instruction operand), effectively performing an unsigned wrap-around into
16
* the range [0 .. 2^N - 1].
17
*
18
* Stack contract:
19
* - Pops: value (int)
20
* - Pushes: value (int)
21
*/
22
23
case
OP_UCLAMP
: {
24
/* Unsigned wrap to N bits: mask lower N bits (operand = bits) */
25
Value
v
=
pop_value
(vm);
26
int
bits
= inst.operand;
27
int64_t
vi
= (
v
.type ==
VAL_INT
) ?
v
.i : 0;
28
29
uint64_t
mask
;
30
if
(
bits
<= 0) {
31
mask
= 0ULL;
32
}
else
if
(
bits
>= 64) {
33
mask
= UINT64_MAX;
34
}
else
{
35
mask
= (1ULL <<
bits
) - 1ULL;
36
}
37
38
uint64_t
wrapped
= ((uint64_t)
vi
) &
mask
;
39
push_value
(vm,
make_int
((int64_t)
wrapped
));
40
free_value
(
v
);
41
break
;
42
}
OP_UCLAMP
@ OP_UCLAMP
Definition
bytecode.h:97
v
Value v
Definition
cast.c:22
vi
int64_t vi
Definition
sclamp.c:31
wrapped
uint64_t wrapped
Definition
sclamp.c:38
bits
int bits
Definition
sclamp.c:30
Value
Tagged union representing a Fun value.
Definition
value.h:68
push_value
push_value(vm, make_int((int64_t) wrapped))
mask
uint64_t mask
Definition
uclamp.c:29
free_value
free_value(v)
make_int
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition
value.c:51
VAL_INT
@ VAL_INT
Definition
value.h:51
pop_value
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition
vm.c:580
Generated on
for Fun by
1.16.1