interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / tests / omc / examples / count_word.om

201 lines · 3519 bytes Raw Blame History
1note count_word: OM-2 `count A off n v`, a ulong, is how many of the n uchar elements of A from off
2note equal v. Checked here against a byte-by-byte loop: every off 0..47 with lengths 0..40, the 16-byte
3note chunk edges and the 4080-byte block edges, four values, over mixed bytes and over a run where every
4note byte equals v; then in a local array, a text, a sized view, an unsized view, a part and an edit
5note parameter, and with the destination also an input. Prints the number of cases and their counts.
6om2
7global g uchar 70000
8global lens ulong 64
9global nlen ulong 1
10global cases ulong 1
11global total ulong 1
12global ob uchar 64
13text tok "count_word ok cases "
14text tsum " sum "
15text tbad "count_word MISMATCH\n"
16text tnl "\n"
17text tt "a\nbb\n\nccc\n"
18
19note naive: the same count over g, one byte at a time
20fn naive o ulong n ulong v uchar to ulong
21c = ulong 0
22for i ulong n bound 70000
23if eq get g add o i v
24c = add c 1
25end
26end
27ret c
28end
29
30note bad: report a mismatch and stop with exit 1
31fn bad to void
32k = write 1 tbad 0 len tbad
33exit 1
34end
35
36note one: compare count with naive for g[o, o + n) and value v
37fn one o ulong n ulong v uchar to void
38a = count g o n v
39b = naive o n v
40if ne a b
41bad
42end
43put cases 0 add get cases 0 1
44put total 0 add get total 0 a
45end
46
47note addlen: append n to the length list
48fn addlen n ulong to void
49k = get nlen 0
50put lens k n
51put nlen 0 add k 1
52end
53
54note sweep: every off in [base, base + 48) with every listed length and four values
55fn sweep base ulong to void
56for j ulong 48
57o = add base j
58for q ulong get nlen 0 bound 64
59n = get lens q
60one o n 10
61one o n 0
62one o n 255
63one o n 37
64end
65end
66end
67
68note emu: print v in decimal
69fn emu v ulong to void
70d = new uchar 20 0
71x = v
72k = ulong 0
73going = true
74while going bound 21
75put d k uchar add rem x 10 48
76k = add k 1
77x = div x 10
78if eq x 0
79going = false
80end
81end
82p = ulong 0
83while gt k 0 bound 21
84k = sub k 1
85put ob p get d k
86p = add p 1
87end
88w = write 1 ob 0 p
89end
90
91note views: count through a sized view, an unsized view and an edit parameter
92fn cs a view uchar 16 o ulong n ulong v uchar to ulong
93ret count a o n v
94end
95
96fn cv a view uchar * o ulong n ulong v uchar to ulong
97ret count a o n v
98end
99
100fn ce a edit uchar * o ulong n ulong v uchar to ulong
101put a 0 v
102ret count a o n v
103end
104
105note forms: the other kinds of array, and a destination that is also an input
106fn forms to void
107loc = new uchar 16 7
108put loc 3 10
109put loc 9 10
110put loc 15 10
111if ne count loc 0 16 10 3
112bad
113end
114if ne count loc 4 11 10 1
115bad
116end
117if ne count loc 0 16 7 13
118bad
119end
120if ne count tt 0 len tt 10 4
121bad
122end
123if ne count tt 1 3 98 2
124bad
125end
126if ne cs loc 0 16 10 3
127bad
128end
129if ne cs loc 10 6 10 1
130bad
131end
132if ne cv loc 0 16 10 3
133bad
134end
135if ne cv part loc 3 7 0 7 10 2
136bad
137end
138if ne cv part loc 4 0 0 0 10 0
139bad
140end
141if ne ce loc 0 16 10 4
142bad
143end
144o = ulong 2
145o = count loc o 14 10
146if ne o 3
147bad
148end
149n = ulong 16
150n = count loc 0 n 10
151if ne n 4
152bad
153end
154end
155
156fn main to int
157x = uint 12345
158for i ulong 30000
159x = add mul x 1103515245 12345
160b = uchar and shr x 16 255
161if lt b 64
162b = uchar 10
163end
164put g i b
165end
166for i ulong 40000
167put g add 30000 i 10
168end
169for i ulong 41
170addlen i
171end
172addlen 63
173addlen 64
174addlen 65
175addlen 255
176addlen 256
177addlen 257
178addlen 1000
179addlen 4079
180addlen 4080
181addlen 4081
182addlen 4095
183addlen 4096
184addlen 4097
185addlen 8160
186addlen 8161
187addlen 12000
188sweep 0
189sweep 30000
190one 0 70000 10
191one 0 70000 37
192one 69999 1 10
193one 70000 0 10
194forms
195k = write 1 tok 0 len tok
196emu get cases 0
197k = write 1 tsum 0 len tsum
198emu get total 0
199k = write 1 tnl 0 len tnl
200ret 0
201end