1module Std.Byte
2
3import Std.Natural
4
5family ByteAddResult : Type 0
6constructor ByteAddResultValue
7field unrestricted byteAddLow : Byte
8field unrestricted byteAddCarry : Nat
9
10end-family
11
12family ByteMultiplyResult : Type 0
13constructor ByteMultiplyResultValue
14field unrestricted byteMultiplyLow : Byte
15field unrestricted byteMultiplyHigh : Byte
16
17end-family
18
19def byteNaturalTwo =
20 2
21
22def byteNaturalEight =
23 8
24
25def byteNaturalTwoHundredFiftySix =
26 256
27
28def byteAdd =
29 (lambda unrestricted left : Byte . unrestricted right : Byte .
30 app
31 (lambda unrestricted total : Nat .
32 constructor
33 ByteAddResult
34 ByteAddResultValue
35 -- total is in 0..510. The primitive conversion keeps its low
36 -- eight bits, and its high part is exactly the flag total > 255.
37 -- Avoid general unary division/modulo in this bounded operation.
38 (nat-to-byte total)
39 (nat-less-than 255 total))
40 (naturalAdd (byte-to-nat left) (byte-to-nat right)))
41
42def byteMultiply =
43 (lambda unrestricted left : Byte . unrestricted right : Byte .
44 app
45 (lambda unrestricted product : Nat .
46 constructor
47 ByteMultiplyResult
48 ByteMultiplyResultValue
49 (nat-to-byte (naturalModuloUnchecked product byteNaturalTwoHundredFiftySix))
50 (nat-to-byte (naturalDivideUnchecked product byteNaturalTwoHundredFiftySix)))
51 (naturalMultiply (byte-to-nat left) (byte-to-nat right)))
52
53-- The bit operations are the L21 byte primitives (one machine operation on
54-- every lane). A shift amount is a Nat here, as it always was: an amount of
55-- eight or more yields zero, decided by the primitive comparison so that
56-- `nat-to-byte` never truncates an amount of 256 or more into a small one.
57def byteAnd =
58 (lambda unrestricted left : Byte . unrestricted right : Byte . byte-and left right)
59
60def byteOr =
61 (lambda unrestricted left : Byte . unrestricted right : Byte . byte-or left right)
62
63def byteXor =
64 (lambda unrestricted left : Byte . unrestricted right : Byte . byte-xor left right)
65
66def byteShiftRight =
67 (lambda unrestricted value : Byte . unrestricted amount : Nat .
68 nat-eliminate
69 (lambda unrestricted current : Nat . Byte)
70 (byte 0)
71 (lambda unrestricted predecessor : Nat . unrestricted induction : Byte .
72 byte-shift-right value (nat-to-byte amount))
73 (nat-less-than amount byteNaturalEight))
74
75def byteShiftLeftTruncated =
76 (lambda unrestricted value : Byte . unrestricted amount : Nat .
77 nat-eliminate
78 (lambda unrestricted current : Nat . Byte)
79 (byte 0)
80 (lambda unrestricted predecessor : Nat . unrestricted induction : Byte .
81 byte-shift-left value (nat-to-byte amount))
82 (nat-less-than amount byteNaturalEight))
83
84def byteAddWithCarry =
85 (lambda unrestricted left : Byte . unrestricted right : Byte . unrestricted carry : Nat .
86 eliminate
87 ByteAddResult
88 (lambda unrestricted current : (family ByteAddResult) . family ByteAddResult)
89 (byteAdd left right)
90 (branch
91 ByteAddResultValue
92 firstLow
93 firstCarry
94 .
95 eliminate
96 ByteAddResult
97 (lambda unrestricted current : (family ByteAddResult) . family ByteAddResult)
98 (byteAdd firstLow (nat-to-byte carry))
99 (branch
100 ByteAddResultValue
101 secondLow
102 secondCarry
103 .
104 constructor
105 ByteAddResult
106 ByteAddResultValue
107 secondLow
108 (naturalAdd firstCarry secondCarry))))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.