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
math
pow.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
30
31
case
OP_POW
: {
32
Value
b
= pop_value(vm);
33
Value
a
= pop_value(vm);
34
if
(
a
.type !=
VAL_INT
||
b
.type !=
VAL_INT
) {
35
fprintf
(stderr,
"POW expects ints\n"
);
36
exit
(1);
37
}
38
int64_t
base
=
a
.i;
39
int64_t
exp
=
b
.i;
40
int64_t
res
= 1;
41
if
(
exp
< 0) {
42
res
= 0;
43
}
else
{
44
while
(
exp
> 0) {
45
if
(
exp
& 1)
res
*=
base
;
46
base
*=
base
;
47
exp
>>= 1;
48
}
49
}
50
push_value(vm,
make_int
(
res
));
51
free_value
(
a
);
52
free_value
(
b
);
53
break
;
54
}
a
Value a
Definition
add.c:37
res
int res
Definition
and.c:34
b
uint32_t b
Definition
band.c:32
OP_POW
@ OP_POW
Definition
bytecode.h:125
exp
int64_t exp
Definition
pow.c:39
free_value
free_value(a)
base
int64_t base
Definition
pow.c:38
Value
Tagged union representing a Fun value.
Definition
value.h:68
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
fprintf
#define fprintf
Definition
vm.c:200
exit
#define exit(code)
Definition
vm.c:230
Generated on
for Fun by
1.16.1