# power.mul

The power program written in MUL, Go, PHP, C and Rust, with the same output and exit code, and the token count of each version.

## Source

`examples/compare/power.mul` is stored in the compact layout, as AI agents write it: a space only between two words, no other whitespace. The page lays it out for reading; the Compact button shows the stored text, and Copy always copies it.

```mul
module compare.power fn powMod(base:i32,exponent:i32,modulus:i32)->i32{var result=1 var factor=base%modulus var left=exponent while left>0{if left%2==1{result=result*factor%modulus}factor=factor*factor%modulus left=left/2}return result}pub fn main()->i32 io{print("3^13 mod 200")return powMod(3,13,200)}
```

## Verified IR

What `mul ir` prints after the verifier has accepted the program.

```mul-ir
module compare.power

fn @powMod(%0: i32, %1: i32, %2: i32) -> i32 {
b0:
  %3 = const.i32 1
  %4 = rem.i32 %0, %2
  jump b1(%3, %4, %1)
b1(%5: i32, %6: i32, %7: i32):
  %8 = const.i32 0
  %9 = gt.i32 %7, %8
  branch %9, b2, b3
b2:
  %10 = const.i32 2
  %11 = rem.i32 %7, %10
  %12 = const.i32 1
  %13 = eq.i32 %11, %12
  branch %13, b4, b5
b3:
  return %5
b4:
  %14 = mul.i32 %5, %6
  %15 = rem.i32 %14, %2
  jump b6(%15)
b5:
  jump b6(%5)
b6(%16: i32):
  %17 = mul.i32 %6, %6
  %18 = rem.i32 %17, %2
  %19 = const.i32 2
  %20 = div.i32 %7, %19
  jump b1(%16, %18, %20)
}

fn @main() -> i32 io {
b0:
  %0 = const.string "3^13 mod 200"
  print %0
  %1 = const.i32 3
  %2 = const.i32 13
  %3 = const.i32 200
  %4 = call.i32 @powMod(%1, %2, %3)
  return %4
}
```

## Run

Output and exit code of the program in MUL (interpreter and Cranelift), Go, PHP, C and Rust, from `examples/compare/evidence/power/run.txt`. The value `main` returns is the exit code.

```console
$ mul run examples/compare/power.mul
3^13 mod 200
exit code 123

$ mul run --backend cranelift examples/compare/power.mul
3^13 mod 200
exit code 123

$ go build -o power examples/compare/power.go && ./power
3^13 mod 200
exit code 123

$ php examples/compare/power.php
3^13 mod 200
exit code 123

$ cc -o power examples/compare/power.c && ./power
3^13 mod 200
exit code 123

$ rustc --edition 2021 -O -o power examples/compare/power.rs && ./power
3^13 mod 200
exit code 123
```

## Tokens

Token count of each file, MUL as the base: first the offline estimate of `mul tokens`, then the same files counted with the real BPE vocabularies (`--tokenizer-cmd`). The estimate applies fixed rules fitted to the pilot study (design document, chapter 23) instead of a learned vocabulary of merges, so it is a few percent off on each file; quote the BPE numbers.

```console
$ mul tokens examples/compare/power.mul --compare examples/compare/power.go examples/compare/power.php examples/compare/power.c examples/compare/power.rs
tokenizer: estimate
note: offline estimate, not a real tokenizer (about 5% per file on GPT-style BPE); use --tokenizer-cmd for exact counts
file                        layout        tokens      diff         %
examples/compare/power.mul  compact          108      base
examples/compare/power.go   as written       133       +25    +23.1%
examples/compare/power.php  as written       146       +38    +35.2%
examples/compare/power.c    as written       148       +40    +37.0%
examples/compare/power.rs   as written       132       +24    +22.2%
```

```console
$ mul tokens examples/compare/power.mul --compare examples/compare/power.go examples/compare/power.php examples/compare/power.c examples/compare/power.rs --tokenizer-cmd "python3 research/token-sample/count.py cl100k_base"
tokenizer: cl100k_base
file                        layout        tokens      diff         %
examples/compare/power.mul  compact          103      base
examples/compare/power.go   as written       128       +25    +24.3%
examples/compare/power.php  as written       142       +39    +37.9%
examples/compare/power.c    as written       144       +41    +39.8%
examples/compare/power.rs   as written       127       +24    +23.3%
```

```console
$ mul tokens examples/compare/power.mul --compare examples/compare/power.go examples/compare/power.php examples/compare/power.c examples/compare/power.rs --tokenizer-cmd "python3 research/token-sample/count.py o200k_base"
tokenizer: o200k_base
file                        layout        tokens      diff         %
examples/compare/power.mul  compact          103      base
examples/compare/power.go   as written       128       +25    +24.3%
examples/compare/power.php  as written       142       +39    +37.9%
examples/compare/power.c    as written       144       +41    +39.8%
examples/compare/power.rs   as written       127       +24    +23.3%
```

## The same program in other languages

`make compare` checks that each of them prints the same output and exits with the same code.

### Go

`examples/compare/power.go`

```go
package main

import (
	"fmt"
	"os"
)

func powMod(base int32, exponent int32, modulus int32) int32 {
	result := int32(1)
	factor := base % modulus
	left := exponent
	for left > 0 {
		if left%2 == 1 {
			result = result * factor % modulus
		}
		factor = factor * factor % modulus
		left = left / 2
	}
	return result
}

func main() {
	fmt.Println("3^13 mod 200")
	os.Exit(int(powMod(3, 13, 200)))
}
```

### PHP

`examples/compare/power.php`

```php
<?php

function powMod(int $base, int $exponent, int $modulus): int
{
    $result = 1;
    $factor = $base % $modulus;
    $left = $exponent;
    while ($left > 0) {
        if ($left % 2 == 1) {
            $result = $result * $factor % $modulus;
        }
        $factor = $factor * $factor % $modulus;
        $left = intdiv($left, 2);
    }
    return $result;
}

echo "3^13 mod 200\n";
exit(powMod(3, 13, 200));
```

### C

`examples/compare/power.c`

```c
#include <stdint.h>
#include <stdio.h>

static int32_t powMod(int32_t base, int32_t exponent, int32_t modulus) {
    int32_t result = 1;
    int32_t factor = base % modulus;
    int32_t left = exponent;
    while (left > 0) {
        if (left % 2 == 1) {
            result = result * factor % modulus;
        }
        factor = factor * factor % modulus;
        left = left / 2;
    }
    return result;
}

int main(void) {
    puts("3^13 mod 200");
    return powMod(3, 13, 200);
}
```

### Rust

`examples/compare/power.rs`

```rust
fn pow_mod(base: i32, exponent: i32, modulus: i32) -> i32 {
    let mut result = 1;
    let mut factor = base % modulus;
    let mut left = exponent;
    while left > 0 {
        if left % 2 == 1 {
            result = result * factor % modulus;
        }
        factor = factor * factor % modulus;
        left /= 2;
    }
    result
}

fn main() {
    println!("3^13 mod 200");
    std::process::exit(pow_mod(3, 13, 200));
}
```
