1module Model.Word32
2
3import Model.Config
4import Std.Byte
5import Std.Natural
6
7family ModelWord32ArithmeticErrorCode : Type 0
8constructor ModelWord32ModuloByZero
9
10end-family
11
12family ModelWord32ModuloResult : Type 0
13constructor ModelWord32ModuloSucceeded
14field unrestricted modelWord32ModuloValue : Nat
15constructor ModelWord32ModuloFailed
16field unrestricted modelWord32ModuloError : (family ModelWord32ArithmeticErrorCode)
17
18end-family
19
20family ModelWord32MultiplyState : Type 0
21constructor ModelWord32MultiplyStateValue
22field unrestricted modelWord32MultiplyMultiplicand : (family ModelWord32)
23field unrestricted modelWord32MultiplyMultiplier : (family ModelWord32)
24field unrestricted modelWord32MultiplyProduct : (family ModelWord32)
25
26end-family
27
28def modelWord32ArithmeticErrorCodeBytes =
29 (lambda unrestricted code : (family ModelWord32ArithmeticErrorCode) .
30 eliminate
31 ModelWord32ArithmeticErrorCode
32 (lambda unrestricted current : (family ModelWord32ArithmeticErrorCode) . Bytes)
33 code
34 (branch ModelWord32ModuloByZero . b"ALPHA-MODEL-001"))
35
36def modelWord32Zero =
37 (constructor ModelWord32 ModelWord32Value 0 0 0 0)
38
39def modelWord32NaturalOne =
40 1
41
42def modelWord32NaturalSeven =
43 7
44
45def modelWord32NaturalThirtyTwo =
46 32
47
48def modelWord32Select =
49 (lambda unrestricted condition : Nat .
50 unrestricted whenTrue : (family ModelWord32) .
51 unrestricted whenFalse : (family ModelWord32) .
52 nat-eliminate
53 (lambda unrestricted current : Nat . family ModelWord32)
54 whenFalse
55 (lambda unrestricted predecessor : Nat . unrestricted induction : (family ModelWord32) .
56 whenTrue)
57 condition)
58
59def modelWord32Xor =
60 (lambda unrestricted left : (family ModelWord32) . unrestricted right : (family ModelWord32) .
61 eliminate
62 ModelWord32
63 (lambda unrestricted current : (family ModelWord32) . family ModelWord32)
64 left
65 (branch ModelWord32Value l0 l1 l2 l3 .
66 eliminate
67 ModelWord32
68 (lambda unrestricted current : (family ModelWord32) . family ModelWord32)
69 right
70 (branch ModelWord32Value r0 r1 r2 r3 .
71 constructor
72 ModelWord32
73 ModelWord32Value
74 (byteXor l0 r0)
75 (byteXor l1 r1)
76 (byteXor l2 r2)
77 (byteXor l3 r3))))
78
79def modelWord32Add =
80 (lambda unrestricted left : (family ModelWord32) . unrestricted right : (family ModelWord32) .
81 eliminate
82 ModelWord32
83 (lambda unrestricted current : (family ModelWord32) . family ModelWord32)
84 left
85 (branch ModelWord32Value l0 l1 l2 l3 .
86 eliminate
87 ModelWord32
88 (lambda unrestricted current : (family ModelWord32) . family ModelWord32)
89 right
90 (branch ModelWord32Value r0 r1 r2 r3 .
91 eliminate
92 ByteAddResult
93 (lambda unrestricted current : (family ByteAddResult) . family ModelWord32)
94 (byteAddWithCarry l0 r0 0)
95 (branch ByteAddResultValue sum0 carry0 .
96 eliminate
97 ByteAddResult
98 (lambda unrestricted current : (family ByteAddResult) . family ModelWord32)
99 (byteAddWithCarry l1 r1 carry0)
100 (branch ByteAddResultValue sum1 carry1 .
101 eliminate
102 ByteAddResult
103 (lambda unrestricted current : (family ByteAddResult) . family ModelWord32)
104 (byteAddWithCarry l2 r2 carry1)
105 (branch ByteAddResultValue sum2 carry2 .
106 eliminate
107 ByteAddResult
108 (lambda unrestricted current : (family ByteAddResult) .
109 family ModelWord32)
110 (byteAddWithCarry l3 r3 carry2)
111 (branch ByteAddResultValue sum3 carry3 .
112 constructor ModelWord32 ModelWord32Value sum0 sum1 sum2 sum3)))))))
113
114def modelWord32ShiftRightOne =
115 (lambda unrestricted value : (family ModelWord32) .
116 eliminate
117 ModelWord32
118 (lambda unrestricted current : (family ModelWord32) . family ModelWord32)
119 value
120 (branch ModelWord32Value b0 b1 b2 b3 .
121 constructor
122 ModelWord32
123 ModelWord32Value
124 (byteOr
125 (byteShiftRight b0 modelWord32NaturalOne)
126 (byteShiftLeftTruncated (byteAnd b1 1) modelWord32NaturalSeven))
127 (byteOr
128 (byteShiftRight b1 modelWord32NaturalOne)
129 (byteShiftLeftTruncated (byteAnd b2 1) modelWord32NaturalSeven))
130 (byteOr
131 (byteShiftRight b2 modelWord32NaturalOne)
132 (byteShiftLeftTruncated (byteAnd b3 1) modelWord32NaturalSeven))
133 (byteShiftRight b3 modelWord32NaturalOne)))
134
135def modelWord32ShiftLeftOne =
136 (lambda unrestricted value : (family ModelWord32) .
137 eliminate
138 ModelWord32
139 (lambda unrestricted current : (family ModelWord32) . family ModelWord32)
140 value
141 (branch ModelWord32Value b0 b1 b2 b3 .
142 constructor
143 ModelWord32
144 ModelWord32Value
145 (byteShiftLeftTruncated b0 modelWord32NaturalOne)
146 (byteOr
147 (byteShiftLeftTruncated b1 modelWord32NaturalOne)
148 (byteShiftRight b0 modelWord32NaturalSeven))
149 (byteOr
150 (byteShiftLeftTruncated b2 modelWord32NaturalOne)
151 (byteShiftRight b1 modelWord32NaturalSeven))
152 (byteOr
153 (byteShiftLeftTruncated b3 modelWord32NaturalOne)
154 (byteShiftRight b2 modelWord32NaturalSeven))))
155
156def modelWord32ToNatural =
157 (lambda unrestricted value : (family ModelWord32) .
158 eliminate
159 ModelWord32
160 (lambda unrestricted current : (family ModelWord32) . Nat)
161 value
162 (branch ModelWord32Value b0 b1 b2 b3 .
163 then (naturalAdd (byte-to-nat b0))
164 (naturalMultiply byteNaturalTwoHundredFiftySix)
165 (naturalAdd (byte-to-nat b1))
166 (naturalMultiply byteNaturalTwoHundredFiftySix)
167 (naturalAdd (byte-to-nat b2))
168 (naturalMultiply byteNaturalTwoHundredFiftySix)
169 byte-to-nat
170 b3))
171
172-- General path: four divisions by 256 (a fold over the value each). Reached
173-- only for values of 256 and above; `modelWord32FromNaturalTruncated` takes
174-- the O(1) byte path below that (D17).
175def modelWord32FromNaturalDivided =
176 (lambda unrestricted value : Nat .
177 app
178 (lambda unrestricted quotient1 : Nat .
179 app
180 (lambda unrestricted quotient2 : Nat .
181 app
182 (lambda unrestricted quotient3 : Nat .
183 constructor
184 ModelWord32
185 ModelWord32Value
186 (nat-to-byte (naturalModuloUnchecked value byteNaturalTwoHundredFiftySix))
187 (nat-to-byte (naturalModuloUnchecked quotient1 byteNaturalTwoHundredFiftySix))
188 (nat-to-byte (naturalModuloUnchecked quotient2 byteNaturalTwoHundredFiftySix))
189 (nat-to-byte (naturalModuloUnchecked quotient3 byteNaturalTwoHundredFiftySix)))
190 (naturalDivideUnchecked quotient2 byteNaturalTwoHundredFiftySix))
191 (naturalDivideUnchecked quotient1 byteNaturalTwoHundredFiftySix))
192 (naturalDivideUnchecked value byteNaturalTwoHundredFiftySix))
193
194def modelWord32FromNaturalTruncated =
195 (lambda unrestricted value : Nat .
196 app
197 (nat-eliminate
198 (lambda unrestricted small : Nat . pi unrestricted unit : Nat . family ModelWord32)
199 (lambda unrestricted unit : Nat . modelWord32FromNaturalDivided value)
200 (lambda unrestricted predecessor : Nat .
201 unrestricted induction : (pi unrestricted unit : Nat . family ModelWord32) .
202 unrestricted unit : Nat .
203 constructor ModelWord32 ModelWord32Value (nat-to-byte value) 0 0 0)
204 (nat-less-than value byteNaturalTwoHundredFiftySix))
205 0)
206
207-- A logical shift is division/multiplication by 2^amount followed by the
208-- word conversion, rather than amount separate carry traversals. The width
209-- guard is lazy: amounts >= 32 return zero without computing their power.
210-- On the bounded u64 runtime lane the largest admitted multiplication is
211-- (2^32 - 1) * 2^31 < 2^63, so no host overflow precedes truncation. Keeping
212-- the one-bit operations independent also leaves a byte-lane reference for
213-- the full shift tests. The Baguette realize profile run-430189202 motivated
214-- this change; equality of emitted bytes still needs artifact qualification.
215def modelWord32ShiftRight =
216 (lambda unrestricted value : (family ModelWord32) . unrestricted amount : Nat .
217 app
218 (nat-eliminate
219 (lambda unrestricted current : Nat . pi unrestricted unit : Nat . family ModelWord32)
220 (lambda unrestricted unit : Nat . modelWord32Zero)
221 (lambda unrestricted predecessor : Nat .
222 unrestricted induction : (pi unrestricted unit : Nat . family ModelWord32) .
223 unrestricted unit : Nat .
224 modelWord32FromNaturalTruncated
225 (naturalDivideUnchecked (modelWord32ToNatural value) (naturalPowerOfTwo amount)))
226 (naturalLess amount modelWord32NaturalThirtyTwo))
227 0)
228
229def modelWord32ShiftLeft =
230 (lambda unrestricted value : (family ModelWord32) . unrestricted amount : Nat .
231 app
232 (nat-eliminate
233 (lambda unrestricted current : Nat . pi unrestricted unit : Nat . family ModelWord32)
234 (lambda unrestricted unit : Nat . modelWord32Zero)
235 (lambda unrestricted predecessor : Nat .
236 unrestricted induction : (pi unrestricted unit : Nat . family ModelWord32) .
237 unrestricted unit : Nat .
238 modelWord32FromNaturalTruncated
239 (naturalMultiply (modelWord32ToNatural value) (naturalPowerOfTwo amount)))
240 (naturalLess amount modelWord32NaturalThirtyTwo))
241 0)
242
243def modelWord32LeastBit =
244 (lambda unrestricted value : (family ModelWord32) .
245 eliminate
246 ModelWord32
247 (lambda unrestricted current : (family ModelWord32) . Nat)
248 value
249 (branch ModelWord32Value b0 b1 b2 b3 . byte-to-nat (byteAnd b0 1)))
250
251def modelWord32MultiplyStep =
252 (lambda unrestricted state : (family ModelWord32MultiplyState) .
253 eliminate
254 ModelWord32MultiplyState
255 (lambda unrestricted current : (family ModelWord32MultiplyState) .
256 family ModelWord32MultiplyState)
257 state
258 (branch ModelWord32MultiplyStateValue multiplicand multiplier product .
259 constructor
260 ModelWord32MultiplyState
261 ModelWord32MultiplyStateValue
262 (modelWord32ShiftLeftOne multiplicand)
263 (modelWord32ShiftRightOne multiplier)
264 (modelWord32Select
265 (modelWord32LeastBit multiplier)
266 (modelWord32Add product multiplicand)
267 product)))
268
269def modelWord32MultiplyStateRun =
270 (lambda unrestricted left : (family ModelWord32) . unrestricted right : (family ModelWord32) .
271 nat-eliminate
272 (lambda unrestricted current : Nat . family ModelWord32MultiplyState)
273 (constructor
274 ModelWord32MultiplyState
275 ModelWord32MultiplyStateValue
276 left
277 right
278 modelWord32Zero)
279 (lambda unrestricted predecessor : Nat .
280 unrestricted induction : (family ModelWord32MultiplyState) .
281 modelWord32MultiplyStep induction)
282 modelWord32NaturalThirtyTwo)
283
284def modelWord32Multiply =
285 (lambda unrestricted left : (family ModelWord32) . unrestricted right : (family ModelWord32) .
286 eliminate
287 ModelWord32MultiplyState
288 (lambda unrestricted current : (family ModelWord32MultiplyState) . family ModelWord32)
289 (modelWord32MultiplyStateRun left right)
290 (branch ModelWord32MultiplyStateValue multiplicand multiplier product . product))
291
292def modelWord32ModuloStep =
293 (lambda unrestricted remainder : Nat . unrestricted value : Byte . unrestricted divisor : Nat .
294 naturalModuloUnchecked
295 (naturalAdd (naturalMultiply remainder byteNaturalTwoHundredFiftySix) (byte-to-nat value))
296 divisor)
297
298def modelWord32ModuloUnchecked =
299 (lambda unrestricted value : (family ModelWord32) . unrestricted divisor : Nat .
300 eliminate
301 ModelWord32
302 (lambda unrestricted current : (family ModelWord32) . Nat)
303 value
304 (branch ModelWord32Value b0 b1 b2 b3 .
305 modelWord32ModuloStep
306 (pipe 0
307 (modelWord32ModuloStep b3 divisor)
308 (modelWord32ModuloStep b2 divisor)
309 (modelWord32ModuloStep b1 divisor))
310 b0
311 divisor))
312
313def modelWord32Modulo =
314 (lambda unrestricted value : (family ModelWord32) . unrestricted divisor : Nat .
315 nat-eliminate
316 (lambda unrestricted current : Nat . family ModelWord32ModuloResult)
317 (constructor
318 ModelWord32ModuloResult
319 ModelWord32ModuloFailed
320 (constructor ModelWord32ArithmeticErrorCode ModelWord32ModuloByZero))
321 (lambda unrestricted predecessor : Nat .
322 unrestricted induction : (family ModelWord32ModuloResult) .
323 constructor
324 ModelWord32ModuloResult
325 ModelWord32ModuloSucceeded
326 (modelWord32ModuloUnchecked value divisor))
327 divisor)
328
329-- Successor modulo 2^32: one carry chain, no fold over the value (D17).
330def modelWord32Increment =
331 (lambda unrestricted value : (family ModelWord32) . modelWord32Add value modelWord32One)
332
333-- Order one byte position: 1 when left is below right, 0 when above, and the
334-- lower positions' verdict when equal (most significant position outermost).
335def modelWord32OrderByte =
336 (lambda unrestricted left : Byte . unrestricted right : Byte . unrestricted equalResult : Nat .
337 nat-eliminate
338 (lambda unrestricted less : Nat . Nat)
339 (nat-eliminate
340 (lambda unrestricted greater : Nat . Nat)
341 equalResult
342 (lambda unrestricted predecessor : Nat . unrestricted induction : Nat . 0)
343 (byte-less-than right left))
344 (lambda unrestricted predecessor : Nat . unrestricted induction : Nat . 1)
345 (byte-less-than left right))
346
347-- Unsigned order in four byte comparisons (D17).
348def modelWord32LessThan =
349 (lambda unrestricted left : (family ModelWord32) . unrestricted right : (family ModelWord32) .
350 eliminate
351 ModelWord32
352 (lambda unrestricted current : (family ModelWord32) . Nat)
353 left
354 (branch ModelWord32Value l0 l1 l2 l3 .
355 eliminate
356 ModelWord32
357 (lambda unrestricted current : (family ModelWord32) . Nat)
358 right
359 (branch ModelWord32Value r0 r1 r2 r3 .
360 then (modelWord32OrderByte l3 r3)
361 (modelWord32OrderByte l2 r2)
362 (modelWord32OrderByte l1 r1)
363 (modelWord32OrderByte l0 r0)
364 0)))
365
366def modelWord32Equal =
367 (lambda unrestricted left : (family ModelWord32) . unrestricted right : (family ModelWord32) .
368 naturalAnd
369 (naturalIsZero (modelWord32LessThan left right))
370 (naturalIsZero (modelWord32LessThan right left)))
371
372-- value × 10 = (value << 3) + (value << 1), modulo 2^32.
373def modelWord32TimesTen =
374 (lambda unrestricted value : (family ModelWord32) .
375 modelWord32Add
376 (modelWord32ShiftLeftOne (modelWord32ShiftLeftOne (modelWord32ShiftLeftOne value)))
377 (modelWord32ShiftLeftOne value))
378
379-- The value of one ASCII decimal digit byte (0x30..0x39) as a word.
380def modelWord32DecimalDigit =
381 (lambda unrestricted digit : Byte .
382 constructor ModelWord32 ModelWord32Value (byteAnd digit 15) 0 0 0)
383
384-- Decimal digit bytes, most significant first, to a word modulo 2^32; one
385-- step per digit. Callers check the bytes are digits and the value fits.
386def modelWord32FromDecimalDigitsTruncated =
387 (lambda unrestricted digits : Bytes .
388 app
389 (bytes-eliminate
390 (lambda unrestricted current : Bytes .
391 pi unrestricted accumulator : (family ModelWord32) . family ModelWord32)
392 (lambda unrestricted accumulator : (family ModelWord32) . accumulator)
393 (lambda unrestricted head : Byte .
394 unrestricted tail : Bytes .
395 unrestricted induction : (pi unrestricted accumulator : (family ModelWord32) . family ModelWord32) .
396 unrestricted accumulator : (family ModelWord32) .
397 induction
398 (modelWord32Add (modelWord32TimesTen accumulator) (modelWord32DecimalDigit head)))
399 digits)
400 modelWord32Zero)The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.