Skip to main content

Mojo struct

FastMathFlag

struct FastMathFlag

Flags for controlling fast-math optimizations in floating-point operations.

FastMathFlag provides compile-time controls for various floating-point math optimization modes that trade strict IEEE 754 compliance for performance.

Available flags:

  • NONE: No fast-math optimizations.
  • NNAN: Assume operands and results are not NaN.
  • NINF: Assume operands and results are not +/- infinity.
  • NSZ: Treat the sign of a zero as insignificant.
  • ARCP: Allow reciprocal of values.
  • CONTRACT: Allow floating-point contraction (e.g., fused multiply-add).
  • AFN: Allow algebraic function approximations.
  • REASSOC: Allow reassociation of floating-point operations.
  • FAST: Enable all fast-math optimizations.

Examples:

from builtin.simd import FastMathFlag

var value = Float32(2.0)
var multiplier = Float32(3.0)
var accumulator = Float32(1.0)

# Use contract flag for fused multiply-add
var result = value.fma[FastMathFlag.CONTRACT](multiplier, accumulator)

# Use fast flag for maximum optimization
var fast_result = value.fma[FastMathFlag.FAST](multiplier, accumulator)

Implemented traits

AnyType, Copyable, Equatable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, RegisterPassable

comptime members

AFN

comptime AFN = FastMathFlag(SIMD(6))

Allow approximate function implementations.

ARCP

comptime ARCP = FastMathFlag(SIMD(4))

Allow reciprocal approximations.

CONTRACT

comptime CONTRACT = FastMathFlag(SIMD(5))

Allow floating-point contraction.

FAST

comptime FAST = FastMathFlag(SIMD(8))

Enable all fast-math optimizations.

NINF

comptime NINF = FastMathFlag(SIMD(2))

Assume no infinite values.

NNAN

comptime NNAN = FastMathFlag(SIMD(1))

Assume no NaN values.

NONE

comptime NONE = FastMathFlag(SIMD(0))

No fast-math optimizations enabled.

NSZ

comptime NSZ = FastMathFlag(SIMD(3))

Treat the sign of zero as insignificant.

REASSOC

comptime REASSOC = FastMathFlag(SIMD(7))

Allow reassociation of operations.

Methods

__eq__

__eq__(self, other: Self) -> Bool

Compares two FastMathFlag values for identity.

Args:

  • other (Self): The FastMathFlag to compare against.

Returns:

Bool: True if both flags have the same value, False otherwise.

Was this page helpful?