mod
On this page

mod()

Function in Builtins

Floor modulo: x - y * floor(x / y), with identical semantics on both targets.

import { mod } from 'typeshade'

The signature, the description and the examples come from the compiler's own source at commit 26de7be8.

Syntax

const mod: <K extends FloatKey>(
x: ReadonlyNode<K>,
y: NoInfer<ArithArg<K>>,
) => Node<K>

Parameters

x ReadonlyNode<K>

the value to wrap.

y NoInfer<ArithArg<K>>

the modulus, a vector of the same shape or a scalar to broadcast.

Return value

Node<K>

the wrapped value, in [0, y) for a positive y.

Description

Use it wherever a negative operand is possible, which is what domain repetition and angle folds need: the result takes the sign of y, so for a positive y every input wraps into [0, y) and mod(-1, 4) is 3.

The .mod method and % are the other modulo, truncated modulo, whose result takes the sign of x: there (-1) % 4 is -1. That is WGSL’s % semantics, and since GLSL ES 3.00 keeps % for integers only, the GLSL writer spells the float case as a - b * trunc(a / b). This free function is the portable float modulo of the two. It is deliberately not named fmod, which in C and HLSL means the truncated one.

Component-wise. y may be a scalar broadcast over a vector x.

Examples

Example

import { fn, mod, f32T } from 'typeshade'
// Fold an angle into one revolution, whatever sign it arrives with.
const wrap = fn('wrap_angle', { a: f32T }, ({ a }) => mod(a, 6.283185307179586))

Targets

TargetSupportNotes
WGSL (WebGPU) Supported

(x - y * floor(x / y)).

GLSL ES 3.00 (WebGL2) Supported

mod(x, y).

CPU oracle Supported

Evaluated in f64 by the oracle.

In the guide

See also

Source

src/core/ir/node.ts, line 1695, at commit 26de7be8

Edit this page Report a problem