Examples
isqrt.mul
The isqrt 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/isqrt.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.
module compare.isqrt fn isqrt(n:i32)->i32{var low=0 var high=n while low<high{let mid=(low+high+1)/2 if mid<=n/mid{low=mid}else{high=mid-1}}return low}pub fn main()->i32 io{print("isqrt 50000")return isqrt(50000)}
Verified IR
What mul ir prints after the verifier has accepted the program.
module compare.isqrt
fn @isqrt(%0: i32) -> i32 {
b0:
%1 = const.i32 0
jump b1(%1, %0)
b1(%2: i32, %3: i32):
%4 = lt.i32 %2, %3
branch %4, b2, b3
b2:
%5 = add.i32 %2, %3
%6 = const.i32 1
%7 = add.i32 %5, %6
%8 = const.i32 2
%9 = div.i32 %7, %8
%10 = div.i32 %0, %9
%11 = le.i32 %9, %10
branch %11, b4, b5
b3:
return %2
b4:
jump b6(%9, %3)
b5:
%12 = const.i32 1
%13 = sub.i32 %9, %12
jump b6(%2, %13)
b6(%14: i32, %15: i32):
jump b1(%14, %15)
}
fn @main() -> i32 io {
b0:
%0 = const.string "isqrt 50000"
print %0
%1 = const.i32 50000
%2 = call.i32 @isqrt(%1)
return %2
}
Run
Output and exit code of the program in MUL (interpreter and Cranelift), Go, PHP, C and Rust, from examples/compare/evidence/isqrt/run.txt. The value main returns is the exit code.
$ mul run examples/compare/isqrt.mul
isqrt 50000
exit code 223
$ mul run --backend cranelift examples/compare/isqrt.mul
isqrt 50000
exit code 223
$ go build -o isqrt examples/compare/isqrt.go && ./isqrt
isqrt 50000
exit code 223
$ php examples/compare/isqrt.php
isqrt 50000
exit code 223
$ cc -o isqrt examples/compare/isqrt.c && ./isqrt
isqrt 50000
exit code 223
$ rustc --edition 2021 -O -o isqrt examples/compare/isqrt.rs && ./isqrt
isqrt 50000
exit code 223
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.
$ mul tokens examples/compare/isqrt.mul --compare examples/compare/isqrt.go examples/compare/isqrt.php examples/compare/isqrt.c examples/compare/isqrt.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/isqrt.mul compact 74 base
examples/compare/isqrt.go as written 103 +29 +39.2%
examples/compare/isqrt.php as written 110 +36 +48.6%
examples/compare/isqrt.c as written 114 +40 +54.1%
examples/compare/isqrt.rs as written 99 +25 +33.8%
$ mul tokens examples/compare/isqrt.mul --compare examples/compare/isqrt.go examples/compare/isqrt.php examples/compare/isqrt.c examples/compare/isqrt.rs --tokenizer-cmd "python3 research/token-sample/count.py cl100k_base"
tokenizer: cl100k_base
file layout tokens diff %
examples/compare/isqrt.mul compact 80 base
examples/compare/isqrt.go as written 116 +36 +45.0%
examples/compare/isqrt.php as written 115 +35 +43.8%
examples/compare/isqrt.c as written 124 +44 +55.0%
examples/compare/isqrt.rs as written 108 +28 +35.0%
$ mul tokens examples/compare/isqrt.mul --compare examples/compare/isqrt.go examples/compare/isqrt.php examples/compare/isqrt.c examples/compare/isqrt.rs --tokenizer-cmd "python3 research/token-sample/count.py o200k_base"
tokenizer: o200k_base
file layout tokens diff %
examples/compare/isqrt.mul compact 80 base
examples/compare/isqrt.go as written 116 +36 +45.0%
examples/compare/isqrt.php as written 115 +35 +43.8%
examples/compare/isqrt.c as written 124 +44 +55.0%
examples/compare/isqrt.rs as written 108 +28 +35.0%
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/isqrt.go
package main
import (
"fmt"
"os"
)
func isqrt(n int32) int32 {
low := int32(0)
high := n
for low < high {
mid := (low + high + 1) / 2
if mid <= n/mid {
low = mid
} else {
high = mid - 1
}
}
return low
}
func main() {
fmt.Println("isqrt 50000")
os.Exit(int(isqrt(50000)))
}
PHP
examples/compare/isqrt.php
<?php
function isqrt(int $n): int
{
$low = 0;
$high = $n;
while ($low < $high) {
$mid = intdiv($low + $high + 1, 2);
if ($mid <= intdiv($n, $mid)) {
$low = $mid;
} else {
$high = $mid - 1;
}
}
return $low;
}
echo "isqrt 50000\n";
exit(isqrt(50000));
C
examples/compare/isqrt.c
#include <stdint.h>
#include <stdio.h>
static int32_t isqrt(int32_t n) {
int32_t low = 0;
int32_t high = n;
while (low < high) {
int32_t mid = (low + high + 1) / 2;
if (mid <= n / mid) {
low = mid;
} else {
high = mid - 1;
}
}
return low;
}
int main(void) {
puts("isqrt 50000");
return isqrt(50000);
}
Rust
examples/compare/isqrt.rs
fn isqrt(n: i32) -> i32 {
let mut low = 0;
let mut high = n;
while low < high {
let mid = (low + high + 1) / 2;
if mid <= n / mid {
low = mid;
} else {
high = mid - 1;
}
}
low
}
fn main() {
println!("isqrt 50000");
std::process::exit(isqrt(50000));
}