Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
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
22
23case 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));
41 break;
42}
@ OP_UCLAMP
Definition bytecode.h:97
Value v
Definition cast.c:22
int64_t vi
Definition sclamp.c:31
uint64_t wrapped
Definition sclamp.c:38
int bits
Definition sclamp.c:30
Tagged union representing a Fun value.
Definition value.h:68
push_value(vm, make_int((int64_t) wrapped))
uint64_t mask
Definition uclamp.c:29
free_value(v)
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_INT
Definition value.h:51