| 1 | title: Integers |
| 2 | part: Raz |
| 3 | summary: The ten integer types, literals, wrapping, signed and unsigned, division, shifts and bits. |
| 4 | --- |
| 5 | raz 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 |
| 17 | module Family; |
| 18 | import Out; |
| 19 | |
| 20 | proc main*; |
| 21 | var a: U8; b: U16; c: U32; d: U64; |
| 22 | begin |
| 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"); |
| 33 | end; |
| 34 | |
| 35 | end Family. |
| 36 | ``` |
| 37 | |
| 38 | ## INT is the everyday one |
| 39 | |
| 40 | Lengths, 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 | |
| 46 | A 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 |
| 49 | module Literals; |
| 50 | import Out; |
| 51 | |
| 52 | proc main*; |
| 53 | var b: U8; w: I16; big: U64; |
| 54 | begin |
| 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; |
| 63 | end; |
| 64 | |
| 65 | end Literals. |
| 66 | ``` |
| 67 | |
| 68 | A 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 |
| 71 | module Fits; |
| 72 | |
| 73 | proc main*; |
| 74 | var b: U8; c: U8; |
| 75 | begin |
| 76 | b := 300; |
| 77 | c := U8(300); |
| 78 | end; |
| 79 | |
| 80 | end Fits. |
| 81 | ``` |
| 82 | |
| 83 | ```sh |
| 84 | $ raz Fits.raz |
| 85 | ``` |
| 86 | |
| 87 | ## Wrapping |
| 88 | |
| 89 | Arithmetic 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 |
| 92 | module Wrap; |
| 93 | import Out; |
| 94 | |
| 95 | proc main*; |
| 96 | var a: U8; b: I8; c: U32; d: U8; |
| 97 | begin |
| 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 *) |
| 106 | end; |
| 107 | |
| 108 | end Wrap. |
| 109 | ``` |
| 110 | |
| 111 | Because 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 |
| 114 | module Fnv; |
| 115 | import Out, Str; |
| 116 | |
| 117 | const Offset = 0CBF29CE484222325H; |
| 118 | const Prime = 100000001B3H; |
| 119 | |
| 120 | (* FNV-1a: xor each byte in, then multiply, all modulo 2^64 *) |
| 121 | proc fnv(s: []const U8): U64; |
| 122 | var h: U64; i: INT; |
| 123 | begin |
| 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; |
| 129 | end; |
| 130 | |
| 131 | proc main*; |
| 132 | begin |
| 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; |
| 136 | end; |
| 137 | |
| 138 | end Fnv. |
| 139 | ``` |
| 140 | |
| 141 | ## Widening, narrowing, and sign |
| 142 | |
| 143 | A 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 |
| 146 | module Widths; |
| 147 | import Out; |
| 148 | |
| 149 | proc main*; |
| 150 | var s: I8; u: U8; |
| 151 | begin |
| 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 *) |
| 159 | end; |
| 160 | |
| 161 | end Widths. |
| 162 | ``` |
| 163 | |
| 164 | Relations compare as signed or unsigned according to the type, so the same bits order differently: |
| 165 | |
| 166 | ```raz Order run |
| 167 | module Order; |
| 168 | import Out; |
| 169 | |
| 170 | proc main*; |
| 171 | var s: I8; u: U8; |
| 172 | begin |
| 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; |
| 178 | end; |
| 179 | |
| 180 | end 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 |
| 188 | module Divide; |
| 189 | import Out; |
| 190 | |
| 191 | proc main*; |
| 192 | var n, d: INT; |
| 193 | begin |
| 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 *) |
| 198 | end; |
| 199 | |
| 200 | end 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 |
| 208 | module Shifts; |
| 209 | import Out; |
| 210 | |
| 211 | proc main*; |
| 212 | var u: U8; s: I8; |
| 213 | begin |
| 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 *) |
| 221 | end; |
| 222 | |
| 223 | end Shifts. |
| 224 | ``` |
| 225 | |
| 226 | A 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 |
| 229 | module Colour; |
| 230 | import Out; |
| 231 | |
| 232 | (* red, green and blue, each 0 to 255, in the low 24 bits *) |
| 233 | proc pack(r, g, b: U8): U32; |
| 234 | begin |
| 235 | return (U32(r) shl 16) | (U32(g) shl 8) | U32(b); |
| 236 | end; |
| 237 | |
| 238 | proc red(c: U32): U8; |
| 239 | begin |
| 240 | return U8(c shr 16); |
| 241 | end; |
| 242 | |
| 243 | proc green(c: U32): U8; |
| 244 | begin |
| 245 | return U8((c shr 8) & 0FFH); |
| 246 | end; |
| 247 | |
| 248 | proc blue(c: U32): U8; |
| 249 | begin |
| 250 | return U8(c & 0FFH); |
| 251 | end; |
| 252 | |
| 253 | proc main*; |
| 254 | var c: U32; |
| 255 | begin |
| 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; |
| 261 | end; |
| 262 | |
| 263 | end 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. |