Skip to content

Add [Mojo] #148

Description

@ShalokShalom

Language Name

Mojo

Sample Implementation

fn compute_pi(var rounds: UInt32) -> Float64:
    comptime simd_width = simd_width_of[Float64]()
    comptime vector_indices = iota[DType.uint64, simd_width](0)

    var i: SIMD[DType.uint64, simd_width] = vector_indices
    var float_i: SIMD[DType.float64, simd_width] = vector_indices.cast[DType.float64]()
    var pi_acc: SIMD[DType.float64, simd_width] = {0.0}

    var rounds_cmp: SIMD[DType.uint64, simd_width] = {rounds}

    comptime x_mask: SIMD[DType.bool, simd_width] = (vector_indices & 1).eq(1)
    comptime ones: SIMD[DType.float64, simd_width] = {1.0}
    comptime negative_ones: SIMD[DType.float64, simd_width] = {-1.0}
    comptime x_values = x_mask.select(negative_ones, ones)

    while True:
        var mask = i.lt(rounds_cmp)

        if not any(mask):
            break

        var fmaed = float_i.fma[FastMathFlag.FAST](2.0, 1.0)
        var recip = inlined_assembly["vrcp14pd $0, $1;", SIMD[DType.float64, simd_width], constraints = "=v,v",](
            fmaed,
        )

        var tmp = recip.fma[FastMathFlag.FAST](x_values, pi_acc)
        pi_acc = mask.select(tmp, pi_acc)
        i += simd_width
        float_i += Float64(simd_width)
        
    var pi = pi_acc.reduce_add()
    return pi

Code provided by Owen Hilyard.

Why add this language?

Mojo is a high-performance language with a Python-like syntax that incorporates many interesting technologies, such as inherent SIMD use, MLIR, and other hot stuff.

Implementation Rules

  • Single-threaded (no concurrency/parallelism)
  • Uses the Leibniz formula for calculating pi
  • Reads rounds from rounds.txt
  • Outputs pi with 16 decimal places

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions