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

Implements the OP_GCD opcode for greatest common divisor. More...

Go to the source code of this file.

Functions

 if (!((va.type==VAL_INT)||(va.type==VAL_FLOAT))||!((vb.type==VAL_INT)||(vb.type==VAL_FLOAT)))
 if (a==INT64_MIN)
 free_value (va)
 free_value (vb)
 push_value (vm, res)

Variables

case OP_GCD
Value va = pop_value(vm)
int64_t a = (va.type == VAL_INT) ? va.i : (int64_t)va.d
int64_t b = (vb.type == VAL_INT) ? vb.i : (int64_t)vb.d
Value res = make_int(a)
 break

Detailed Description

Implements the OP_GCD opcode for greatest common divisor.

Behavior:

  • Pops two numeric operands (a, b). Floats are truncated to int64.
  • Computes gcd(|a|, |b|) using Euclid's algorithm.
  • Pushes VAL_INT result.

Stack effect:

  • Pop: b, a
  • Push: gcd(a, b)

Types:

  • Accepts VAL_INT and VAL_FLOAT; others cause a runtime error.

Definition in file gcd.c.

Function Documentation

◆ free_value() [1/2]

free_value(va)

◆ free_value() [2/2]

free_value(vb)

◆ if() [1/2]

if(!(va.type==VAL_INT)||(va.type==VAL_FLOAT))||!((vb.type==VAL_INT)||(vb.type==VAL_FLOAT))

Definition at line 30 of file gcd.c.

◆ if() [2/2]

if(a = = INT64_MIN)

Definition at line 38 of file gcd.c.

◆ push_value()

push_value(vm,
res )

Variable Documentation

◆ a

int64_t a = (va.type == VAL_INT) ? va.i : (int64_t)va.d

Definition at line 36 of file gcd.c.

◆ b

int64_t b = (vb.type == VAL_INT) ? vb.i : (int64_t)vb.d

Definition at line 37 of file gcd.c.

◆ break

break

Definition at line 55 of file gcd.c.

◆ OP_GCD

case OP_GCD

Definition at line 27 of file gcd.c.

◆ res

Definition at line 51 of file gcd.c.

◆ va

Value va = pop_value(vm)

Definition at line 29 of file gcd.c.