raz / site / content / tour / 03-integers.txt

273 lines · 9.3 KiB · raw · history · last change a5145f64

1title: Integers
2part: Raz
3summary: The ten integer types, literals, wrapping, signed and unsigned, division, shifts and bits.
4---
5raz has ten integer types, and a program that works with bytes, addresses, hashes and file offsets uses most of them. Each is a different type, so each has rules, and the rules are the same everywhere: that is the point of having them.
6
7| Type | Size | Values |
8|---|---|---|
9| `I8` `I16` `I32` `I64` | 1, 2, 4, 8 bytes | signed, two's complement |
10| `U8` `U16` `U32` `U64` | 1, 2, 4, 8 bytes | unsigned |
11| `INT` | an address's: 8 on x86-64 | signed, as big as an address |
12| `UINT` | an address's | unsigned, as big as an address |
13
14`SIZE(T)` says how many bytes a type takes, and an unsigned type's largest value is what you get by taking one from zero. This program asks each type.
15
16```raz Family run
17module Family;
18import Out;
19
20proc main*;
21var a: U8; b: U16; c: U32; d: U64;
22begin
23 a := 0; a := a - 1;
24 b := 0; b := b - 1;
25 c := 0; c := c - 1;
26 d := 0; d := d - 1;
27 Out.str("U8 "); Out.int(SIZE(U8)); Out.str(" byte, largest "); Out.int(I64(a)); Out.ln;
28 Out.str("U16 "); Out.int(SIZE(U16)); Out.str(" bytes, largest "); Out.int(I64(b)); Out.ln;
29 Out.str("U32 "); Out.int(SIZE(U32)); Out.str(" bytes, largest "); Out.int(I64(c)); Out.ln;
30 Out.str("U64 "); Out.int(SIZE(U64)); Out.str(" bytes, largest 0x"); Out.hex(d, 16); Out.ln;
31 Out.str("INT "); Out.int(SIZE(INT)); Out.str(" bytes, as big as an address\n");
32 Out.str("I8 "); Out.int(SIZE(I8)); Out.str(" byte, from -128 to 127\n");
33end;
34
35end Family.
36```
37
38## INT is the everyday one
39
40Lengths, indexes, counts and sizes are `INT`s. `LEN(s)` of a slice is an `INT`, and so is the loop variable that walks it. It is as big as an address, so it is the integer a pointer converts to, and the word the atomics work on. Reach for `INT` unless you have a reason: a byte is a `U8`, a character an `I32`, a file's size an `I64`, a hash a `U64`.
41
42`INT` and `I64` are two types even where they have the same size. An `INT` goes silently into an `I64` (and a `UINT` into a `U64`) wherever it meets one, in `pos + LEN(s)` as in a call, which is why `Out.int(n)` and `Fmt.int(n)` take an `I64` and accept any `INT`. The other way is written, `INT(pos)`, since an `INT` may be smaller on another machine. An `INT` holds every `U8`, `I8` and `I16` on any machine, so those go silently into an `INT` too (and a `U8` or `U16` into a `UINT`): `n := s[i]`, and `n * 10 + s[i] - '0'` for a digit. Not what an operator made of them, which was computed in the small type and may have wrapped there: with `a, b: U8`, `n := a + b` is an error, and `INT(a) + b` the sum. And only one step: a `U8` goes into an `INT`, not on into an `I64`, so `Out.int(I64(b))` still says it. Nothing else converts unasked.
43
44## A number written in the program
45
46A number in the program text is untyped until it meets a type, and then it must fit. `0FFH` is hexadecimal (it starts with a digit and ends in `H`), a character in quotes is its code point, and `'\u{E9}'` is a code point written by number. A hexadecimal literal too big for an `I64` is a `U64`.
47
48```raz Literals run
49module Literals;
50import Out;
51
52proc main*;
53var b: U8; w: I16; big: U64;
54begin
55 b := 0FFH; (* fits a U8 *)
56 w := -300;
57 big := 0FFFFFFFFFFFFFFFFH; (* above the largest I64: a U64 *)
58 Out.int(I64(b)); Out.char(' ');
59 Out.int(I64(w)); Out.char(' ');
60 Out.int(I64('a')); Out.char(' ');
61 Out.int(I64('\u{E9}')); Out.char(' ');
62 Out.hex(big, 16); Out.ln;
63end;
64
65end Literals.
66```
67
68A constant that doesn't fit is an error, not a quiet cut. A conversion of a constant, which says you mean it, is allowed: `U8(300)` is 44.
69
70```raz Fits
71module Fits;
72
73proc main*;
74var b: U8; c: U8;
75begin
76 b := 300;
77 c := U8(300);
78end;
79
80end Fits.
81```
82
83```sh
84$ raz Fits.raz
85```
86
87## Wrapping
88
89Arithmetic wraps at the type's width and never stops the program. `250 + 10` in a `U8` is 4; one more than the largest `I8` is the smallest. A unary minus wraps too: for an unsigned `x`, `-x` is `0 - x`. This is not undefined behaviour, as in C; it is what the report says, in every program, on every target, so a hash function can rely on it.
90
91```raz Wrap run
92module Wrap;
93import Out;
94
95proc main*;
96var a: U8; b: I8; c: U32; d: U8;
97begin
98 a := 250; a := a + 10;
99 Out.int(I64(a)); Out.ln; (* 4 *)
100 b := 127; b := b + 1;
101 Out.int(I64(b)); Out.ln; (* -128 *)
102 c := 0; c := c - 1;
103 Out.int(I64(c)); Out.ln; (* 4294967295 *)
104 d := 1; d := -d;
105 Out.int(I64(d)); Out.ln; (* 255: 0 - 1 *)
106end;
107
108end Wrap.
109```
110
111Because wrapping is defined, a hash that depends on it is portable. Here is the FNV-1a hash that `Str.hash` uses, written in a few lines; the multiplication overflows a `U64` on every byte, on purpose.
112
113```raz Fnv run
114module Fnv;
115import Out, Str;
116
117const Offset = 0CBF29CE484222325H;
118const Prime = 100000001B3H;
119
120(* FNV-1a: xor each byte in, then multiply, all modulo 2^64 *)
121proc fnv(s: []const U8): U64;
122var h: U64; i: INT;
123begin
124 h := Offset;
125 for i := 0 to LEN(s) - 1 do
126 h := (h xor U64(s[i])) * Prime;
127 end;
128 return h;
129end;
130
131proc main*;
132begin
133 Out.hex(fnv("raz"), 16); Out.ln;
134 Out.hex(Str.hash("raz"), 16); Out.ln;
135 if fnv("raz") = Str.hash("raz") then Out.str("the same\n"); end;
136end;
137
138end Fnv.
139```
140
141## Widening, narrowing, and sign
142
143A conversion to a narrower integer cuts the value to the new width. To a wider one it extends by the sign for a signed source and with zeros for an unsigned one. That difference is the whole story of "why did my byte come out negative".
144
145```raz Widths run
146module Widths;
147import Out;
148
149proc main*;
150var s: I8; u: U8;
151begin
152 s := -1;
153 u := 255; (* the same eight bits *)
154 Out.int(I64(s)); Out.char(' '); (* sign-extended: -1 *)
155 Out.int(I64(u)); Out.char(' '); (* zero-extended: 255 *)
156 Out.int(I64(U8(s))); Out.char(' '); (* the bits, read as unsigned: 255 *)
157 Out.int(I64(I8(u))); Out.char(' '); (* and back: -1 *)
158 Out.int(I64(U8(300 + INT(u)))); Out.ln; (* 555 cut to 8 bits: 43 *)
159end;
160
161end Widths.
162```
163
164Relations compare as signed or unsigned according to the type, so the same bits order differently:
165
166```raz Order run
167module Order;
168import Out;
169
170proc main*;
171var s: I8; u: U8;
172begin
173 s := -1;
174 u := U8(s); (* 255 *)
175 if s < 0 then Out.str("s is below zero\n"); end;
176 if u > 100 then Out.str("u is above 100\n"); end;
177 if U8(s) = u then Out.str("but they are the same bits\n"); end;
178end;
179
180end Order.
181```
182
183## Division
184
185`div` rounds toward zero, and `mod` takes the sign of the dividend, as in C: `-7 div 2` is -3 and `-7 mod 2` is -1. Dividing by zero stops the program, and so does dividing the smallest value of a signed type by -1, whose result can't be represented. Neither is a silent wrong answer.
186
187```raz Divide run
188module Divide;
189import Out;
190
191proc main*;
192var n, d: INT;
193begin
194 n := -7; d := 2;
195 Out.int(n div d); Out.char(' '); Out.int(n mod d); Out.char(' '); (* -3 -1 *)
196 n := 7; d := -2;
197 Out.int(n div d); Out.char(' '); Out.int(n mod d); Out.ln; (* -3 1 *)
198end;
199
200end Divide.
201```
202
203## Shifts and bits
204
205`&` (and), `|` (or), `xor` and `~` (not) work on every bit. `x shl n` and `x shr n` shift by `n` modulo the width of `x`'s type: for a `U8`, `x shl 9` is `x shl 1`. `shr` brings in copies of the sign bit for signed types, so it divides by two keeping the sign, and zeros for unsigned ones. The result has `x`'s type, cut to its width: in a `U8`, `0C0H shl 1` is `80H`.
206
207```raz Shifts run
208module Shifts;
209import Out;
210
211proc main*;
212var u: U8; s: I8;
213begin
214 u := 0C0H;
215 Out.hex(U64(u shl 1), 2); Out.char(' '); (* 80: cut to 8 bits *)
216 Out.hex(U64(u shr 4), 2); Out.char(' '); (* 0C: zeros come in *)
217 Out.hex(U64(u shl 9), 2); Out.ln; (* 9 mod 8 = 1: 80 *)
218 s := -64;
219 Out.int(I64(s shr 2)); Out.char(' '); (* -16: the sign comes in *)
220 Out.int(I64(s shr 7)); Out.ln; (* -1 *)
221end;
222
223end Shifts.
224```
225
226A packed value is the usual use of all this: several small numbers in one word. This puts a colour in the bytes of a `U32` and takes it out again.
227
228```raz Colour run
229module Colour;
230import Out;
231
232(* red, green and blue, each 0 to 255, in the low 24 bits *)
233proc pack(r, g, b: U8): U32;
234begin
235 return (U32(r) shl 16) | (U32(g) shl 8) | U32(b);
236end;
237
238proc red(c: U32): U8;
239begin
240 return U8(c shr 16);
241end;
242
243proc green(c: U32): U8;
244begin
245 return U8((c shr 8) & 0FFH);
246end;
247
248proc blue(c: U32): U8;
249begin
250 return U8(c & 0FFH);
251end;
252
253proc main*;
254var c: U32;
255begin
256 c := pack(0FFH, 80H, 10H);
257 Out.str("0x"); Out.hex(U64(c), 6); Out.ln;
258 Out.int(I64(red(c))); Out.char(' ');
259 Out.int(I64(green(c))); Out.char(' ');
260 Out.int(I64(blue(c))); Out.ln;
261end;
262
263end Colour.
264```
265
266## Which one to use
267
268- **`INT`** for lengths, indexes, counts, loop variables, and anything you'd write `int` for in C.
269- **`U8`** for bytes, and for a string's elements (`[]U8`). **`I32`** for a character, a code point.
270- **`U32`, `U64`** for hashes, bit sets, masks and addresses you do arithmetic on.
271- **`I64`** for what must not shrink on a smaller target: a file's size or position, a time in nanoseconds, a number read from text.
272- **`I8`, `I16`, `U16`** when a format or a layout says so, in a record or an array, to save space or match a protocol. Convert at the edge and compute in `INT`.
273- **`UINT`** rarely: for a count that must use the whole address range, or an address.