interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / matrix-multiplication / matrix-multiplication.om

112 lines · 2244 bytes Raw Blame History
1note Rosetta Code task "Matrix multiplication": https://rosettacode.org/wiki/Matrix_multiplication
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text ttimes "times\n"
8text tequals "equals\n"
9global mat long 27
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note emitwi writes a signed n right-aligned in w columns (w at most 20)
37fn emitwi n long w ulong to void
38d = new uchar 21 32
39i = ulong 21
40v = n
41if lt n 0
42v = neg n
43end
44while true bound 20
45i = sub i 1
46put d i uchar add long 48 rem v 10
47v = div v 10
48if eq v 0
49break
50end
51end
52if lt n 0
53i = sub i 1
54put d i 45
55end
56if gt w sub 21 i
57i = sub 21 w
58end
59emit part d i sub 21 i
60end
61note mat holds the three matrices row by row: a at offset 0, b at 9 and their product at 18
62note showm writes the r x c matrix at offset o of mat, each entry right-aligned in w columns
63fn showm o ulong r ulong c ulong w ulong to void
64for i ulong r
65for j ulong c
66emitwi get mat add o add mul i c j w
67end
68emit nl
69end
70end
71note dot is row i of the m-column matrix a times column j of the p-column matrix b
72fn dot i ulong j ulong m ulong p ulong to long
73s = long 0
74for k ulong m
75x = get mat add mul i m k
76y = get mat add 9 add mul k p j
77s = add s mul x y
78end
79ret s
80end
81note matmul computes the n x p product of the n x m matrix a and the m x p matrix b
82fn matmul n ulong m ulong p ulong to void
83for i ulong n
84for j ulong p
85put mat add 18 add mul i p j dot i j m p
86end
87end
88end
89fn main to int
90for i ulong 6
91put mat i long add i 1
92put mat add 9 i long add i 7
93end
94showm 0 2 3 5
95emit ttimes
96showm 9 3 2 5
97emit tequals
98matmul 2 3 2
99showm 18 2 2 5
100emit nl
101for i ulong 9
102put mat i sub long mul i 3 10
103put mat add 9 i sub 4 long rem mul i 5 7
104end
105showm 0 3 3 5
106emit ttimes
107showm 9 3 3 5
108emit tequals
109matmul 3 3 3
110showm 18 3 3 5
111ret 0
112end