For the complete documentation index, see llms.txt. This page is also available as Markdown.

Bit and math built-ins

These functions operate on current 32-bit GPC integers. Valid bit indexes are 0..31. GPC integer division truncates, and arithmetic overflow must be prevented by the script.

set_bit

Compiler category: Bit Operations Backing opcode: sbit (sbita when the compiler selects an array form)

Sets one bit in a writable variable.

Syntax

set_bit(variable, bit_index);

Parameters

  • variable: a runtime variable to modify; an expression or const value is not accepted.

  • bit_index: 0..31, where 0 is the least-significant bit.

Returns

Nothing. The first argument is modified.

Example

int flags;

main {
    if(event_press(PS5_CROSS)) set_bit(flags, 3);
    set_val(TRACE_1, flags);
}

clear_bit

Compiler category: Bit Operations Backing opcode: cbit (cbita for the compiler-selected array form)

Clears one bit in a writable variable without changing its other bits.

Syntax

Parameters

  • variable: a runtime variable to modify.

  • bit_index: 0..31.

Returns

Nothing.

Example


test_bit

Compiler category: Bit Operations Backing opcode: tbit

Tests whether one bit is set.

Syntax

Parameters

  • value: the integer to inspect.

  • bit_index: 0..31.

Returns

TRUE when the bit is set; otherwise FALSE.

Example


set_bits

Compiler category: Bit Operations Backing opcode: sbits (sbitsa for the compiler-selected array form)

Writes a masked multi-bit field into a writable variable at a starting bit index.

Syntax

Parameters

  • variable: the runtime variable to modify.

  • value: the field value to insert.

  • bit_index: starting position 0..31.

  • bit_mask: the low-bit mask describing the field width, such as 0x0F for four bits.

Returns

Nothing.

Example

Keep the mask and starting index identical when the field is later read.


get_bits

Compiler category: Bit Operations Backing opcode: gbits

Extracts a masked field and shifts it down to the low bits.

Syntax

Parameters

  • value: packed integer to inspect.

  • bit_index: starting position 0..31.

  • bit_mask: low-bit mask for the field.

Returns

The extracted field value.

Example


abs

Compiler category: Math Functions Backing opcode: abs

Returns the magnitude of a signed value.

Syntax

Parameters

  • expression: any current-target integer expression.

Returns

The non-negative magnitude. Avoid the INT32_MIN edge because its positive magnitude is outside the signed 32-bit range.

Example


inv

Compiler category: Math Functions Backing opcode: inv

Negates a value. This is equivalent to multiplying by -1.

Syntax

Parameters

  • expression: integer expression to negate.

Returns

The sign-inverted value.

Example


pow

Compiler category: Math Functions Backing opcode: pow

Raises a base integer to an integer exponent.

Syntax

Parameters

  • base: integer base.

  • exponent: integer exponent accepted by the target function.

Returns

The integer power result.

Example

Powers grow quickly. Prove the result remains inside the signed 32-bit range before using runtime-controlled inputs.


isqrt

Compiler category: Math Functions Backing opcode: isqrt

Calculates an integer square root and discards any fractional part.

Syntax

Parameters

  • value: non-negative integer whose square root is required.

Returns

The truncated integer square root.

Example

Validate or clamp runtime input so a negative value is never passed.


random

Compiler category: Miscellaneous Functions Backing opcode: rand

Returns a pseudo-random integer between the supplied bounds.

Syntax

Parameters

  • minimum: lower signed 32-bit bound.

  • maximum: upper signed 32-bit bound.

Returns

A pseudo-random integer in the requested range.

Example

Keep minimum <= maximum and do not use this as a security or authorization mechanism.


clamp

Compiler category: Bit Operations Backing opcode: clamp

Restricts a value to an inclusive lower and upper bound.

Syntax

Parameters

  • value: value to restrict.

  • low: minimum permitted result.

  • high: maximum permitted result.

Returns

low when the value is too small, high when it is too large, or the original value when already in range.

Example


min

Compiler category: Bit Operations Backing opcode: min

Returns the smaller of two integers.

Syntax

Parameters

  • value1: first integer.

  • value2: second integer.

Returns

The smaller value.

Example


max

Compiler category: Bit Operations Backing opcode: max

Returns the larger of two integers.

Syntax

Parameters

  • value1: first integer.

  • value2: second integer.

Returns

The larger value.

Example

Back to the additional built-in index

Last updated