CLZ, CTZ and POPCOUNT: counts of bits, as report 2026-10.36 has them
The built-ins CLZ(x), CTZ(x) and POPCOUNT(x): Sym enters them, Parse's
builtin reads them, and Item's countBits checks x (an unsigned integer
type, or a set for POPCOUNT; an untyped constant is refused, with help
to give it a type), folds a constant by countOf, in a constant too, or
has Gen count the bits. The result is an INT.
The code keeps to the baseline x86-64, which has no lzcnt, tzcnt or
popcnt, and the compiler doesn't ask which machine it is on.
Gen.countZeros makes CLZ by bsr and CTZ by bsf, through X64.bitScan,
on all 64 bits, since a narrow value's bits above its width are zero;
0, which neither finds a one in and says so by ZF, counts the width:
bsr r, r; jnz L; mov r, -1; L: neg r; add r, w - 1 (CLZ)
bsf r, r; jnz L; mov r, w; L: (CTZ)
Gen.popCount sums the ones as Bytes.count does, with no loop: each
two bits, then four, then each byte, in rax and rdx, and a multiply
by 0101...H adds the eight bytes into the top one, shifted down by 56.
Tests: conform/run/Count (each unsigned type at 0, 1, its top bit, all
ones and a mixed value; constants; a U32 wrapped to 0; counts beside
other values on the stack; sets); conform/err ClzUntyped, CtzSigned,
PopcountReal and ClzSet, their messages in test/msg. compiler.md, the
editors' built-in lists and CLAUDE.md's readings say so. Code lines
11,902 -> 11,997.
| @@ -343,3 +343,10 @@ | ||
| 343 | 343 | `MAX(U64)` and `MAX(UINT)`, above `high(I64)`, keep their type, as a |
| 344 | 344 | hex literal above it is a `U64`. A `T` not an integer type is an |
| 345 | 345 | error; `MAX(x)` of a variable is "x isn't a type". |
| 346 | +- `CLZ(x)`, `CTZ(x)` and `POPCOUNT(x)` (report 2026-10.36) give an | |
| 347 | + `INT`, a constant of one when `x` is. They use only baseline x86-64, | |
| 348 | + no `lzcnt`, `tzcnt` or `popcnt`: `bsr` or `bsf` on all 64 bits (a | |
| 349 | + narrow value's upper ones are zero), with `jnz` past setting the | |
| 350 | + count of 0 to the width; `POPCOUNT` by `Bytes.count`'s sums of two, | |
| 351 | + four and eight bits and a multiply by `0101...H`, in rax and rdx. A | |
| 352 | + set's `POPCOUNT` counts its bits the same way. |
| @@ -0,0 +1,9 @@ | ||
| 1 | +module ClzSet; | |
| 2 | +type Day = (mon, tue); | |
| 3 | + Days = set of Day; | |
| 4 | +proc main*; | |
| 5 | +var n: INT; s: Days; | |
| 6 | +begin | |
| 7 | + n := CLZ(s); | |
| 8 | +end; | |
| 9 | +end ClzSet. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ClzUntyped; | |
| 2 | +proc main*; | |
| 3 | +var n: INT; | |
| 4 | +begin | |
| 5 | + n := CLZ(1); | |
| 6 | +end; | |
| 7 | +end ClzUntyped. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module CtzSigned; | |
| 2 | +proc main*; | |
| 3 | +var n: INT; x: I32; | |
| 4 | +begin | |
| 5 | + n := CTZ(x); | |
| 6 | +end; | |
| 7 | +end CtzSigned. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module PopcountReal; | |
| 2 | +const N = POPCOUNT(1.5); | |
| 3 | +end PopcountReal. |
| @@ -0,0 +1,31 @@ | ||
| 1 | +8 8 0 | |
| 2 | +7 0 1 | |
| 3 | +0 7 1 | |
| 4 | +0 0 8 | |
| 5 | +2 3 2 | |
| 6 | +16 16 0 | |
| 7 | +15 0 1 | |
| 8 | +0 15 1 | |
| 9 | +0 0 16 | |
| 10 | +4 8 4 | |
| 11 | +32 32 0 | |
| 12 | +31 0 1 | |
| 13 | +0 31 1 | |
| 14 | +0 0 32 | |
| 15 | +15 8 2 | |
| 16 | +64 64 0 | |
| 17 | +63 0 1 | |
| 18 | +0 63 1 | |
| 19 | +0 0 64 | |
| 20 | +0 4 8 | |
| 21 | +64 64 0 | |
| 22 | +63 0 1 | |
| 23 | +0 63 1 | |
| 24 | +0 0 64 | |
| 25 | +21 41 2 | |
| 26 | +63 3 8 | |
| 27 | +3 40 64 | |
| 28 | +15 8 0 | |
| 29 | +32 32 0 | |
| 30 | +1980 | |
| 31 | +4 3 0 |
| @@ -0,0 +1,73 @@ | ||
| 1 | +(* CLZ(x), CTZ(x) and POPCOUNT(x) count the bits of x (report section | |
| 2 | + 6.1): of each unsigned type, at its own width, with 0 counting the | |
| 3 | + whole width; of variables, of a value an operator wrapped, beside | |
| 4 | + other values, and of constants, in a constant too; and POPCOUNT of a | |
| 5 | + set. Each line is CLZ, CTZ and POPCOUNT of one value. *) | |
| 6 | +module Count; | |
| 7 | +import Out; | |
| 8 | + | |
| 9 | +type | |
| 10 | + Day = (mon, tue, wed, thu, fri, sat, sun); | |
| 11 | + Days = set of Day; | |
| 12 | + | |
| 13 | +const | |
| 14 | + Top = CLZ(U64(1)); (* 63 *) | |
| 15 | + Low = CTZ(U16(8)); (* 3 *) | |
| 16 | + Ones = POPCOUNT(U32(0F0F0H)); (* 8 *) | |
| 17 | + Week = POPCOUNT({mon, wed, sun}); (* 3 *) | |
| 18 | + None = CLZ(U8(0)) + CTZ(U32(0)); (* 8 + 32 *) | |
| 19 | + | |
| 20 | +proc show(a, b, c: INT); | |
| 21 | +begin | |
| 22 | + Out.int(a); Out.str(" "); Out.int(b); Out.str(" "); Out.int(c); Out.ln; | |
| 23 | +end; | |
| 24 | + | |
| 25 | +proc u8(x: U8); | |
| 26 | +begin | |
| 27 | + show(CLZ(x), CTZ(x), POPCOUNT(x)); | |
| 28 | +end; | |
| 29 | + | |
| 30 | +proc u16(x: U16); | |
| 31 | +begin | |
| 32 | + show(CLZ(x), CTZ(x), POPCOUNT(x)); | |
| 33 | +end; | |
| 34 | + | |
| 35 | +proc u32(x: U32); | |
| 36 | +begin | |
| 37 | + show(CLZ(x), CTZ(x), POPCOUNT(x)); | |
| 38 | +end; | |
| 39 | + | |
| 40 | +proc u64(x: U64); | |
| 41 | +begin | |
| 42 | + show(CLZ(x), CTZ(x), POPCOUNT(x)); | |
| 43 | +end; | |
| 44 | + | |
| 45 | +proc uint(x: UINT); | |
| 46 | +begin | |
| 47 | + show(CLZ(x), CTZ(x), POPCOUNT(x)); | |
| 48 | +end; | |
| 49 | + | |
| 50 | +proc main*; | |
| 51 | +var c: U32; d: U64; n: INT; s, none: Days; | |
| 52 | +begin | |
| 53 | + u8(0); u8(1); u8(80H); u8(0FFH); u8(28H); | |
| 54 | + u16(0); u16(1); u16(8000H); u16(0FFFFH); u16(0F00H); | |
| 55 | + u32(0); u32(1); u32(80000000H); u32(0FFFFFFFFH); u32(10100H); | |
| 56 | + u64(0); u64(1); u64(8000000000000000H); u64(0FFFFFFFFFFFFFFFFH); u64(0F0000000000000F0H); | |
| 57 | + uint(0); uint(1); uint(UINT(1) shl 63); uint(UINT(0FFFFFFFFFFFFFFFFH)); uint(UINT(6) shl 40); | |
| 58 | + (* constants *) | |
| 59 | + show(Top, Low, Ones); | |
| 60 | + show(Week, None, POPCOUNT(U64(0FFFFFFFFFFFFFFFFH))); | |
| 61 | + show(CLZ(U16(1)), CTZ(U8(0)), POPCOUNT(U8(0))); | |
| 62 | + (* a U32 that wrapped to 0 is 0, whatever the machine held above it *) | |
| 63 | + c := 80000000H; | |
| 64 | + show(CLZ(c + c), CTZ(c + c), POPCOUNT(c + c)); | |
| 65 | + (* beside other values on the stack *) | |
| 66 | + d := 0F0H; | |
| 67 | + n := 1000 + CLZ(d) * 10 + CTZ(d) * 100 + POPCOUNT(d) * (POPCOUNT(d) + 1); | |
| 68 | + Out.int(n); Out.ln; | |
| 69 | + s := {tue, thu, sat, sun}; | |
| 70 | + show(POPCOUNT(s), POPCOUNT(s - {sun}), POPCOUNT(none)); | |
| 71 | +end; | |
| 72 | + | |
| 73 | +end Count. |
| @@ -18,12 +18,12 @@ | ||
| 18 | 18 | |---|---|---|---| |
| 19 | 19 | | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | |
| 20 | 20 | | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | |
| 21 | -| `src/Sym.raz` | 718 | 576 | Types, objects, scopes, layout | | |
| 21 | +| `src/Sym.raz` | 721 | 579 | Types, objects, scopes, layout | | |
| 22 | 22 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 23 | -| `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler | | |
| 24 | -| `src/Gen.raz` | 2,592 | 1,843 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 25 | -| `src/Item.raz` | 2,244 | 1,800 | Items: types, constants, variables, operators, conversions, calls | | |
| 26 | -| `src/Parse.raz` | 4,400 | 3,800 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 23 | +| `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler | | |
| 24 | +| `src/Gen.raz` | 2,666 | 1,885 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 25 | +| `src/Item.raz` | 2,293 | 1,841 | Items: types, constants, variables, operators, conversions, calls | | |
| 26 | +| `src/Parse.raz` | 4,403 | 3,803 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 27 | 27 | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| 29 | 29 | | `src/Raz.raz` | 418 | 333 | The command line | |
| @@ -1156,6 +1156,32 @@ | ||
| 1156 | 1156 | x86 takes it modulo 32, or 64, and a byte's or a word's rotation then |
| 1157 | 1157 | goes round modulo 8 or 16 itself, the count modulo the width. |
| 1158 | 1158 | |
| 1159 | +The counts of bits, `CLZ`, `CTZ` and `POPCOUNT`, are made with what | |
| 1160 | +every x86-64 has, as `lzcnt`, `tzcnt` and `popcnt` came later and the | |
| 1161 | +compiler doesn't ask which machine it is on. As a narrow integer's | |
| 1162 | +bits above its width are zero, all of them count in 64 bits. | |
| 1163 | +`countZeros` makes `CLZ` from `bsr`, the place of the highest one bit, | |
| 1164 | +and `CTZ` from `bsf`, the place of the lowest; neither has a place to | |
| 1165 | +give for 0, which they say by the zero flag, and 0 counts the whole | |
| 1166 | +width. For a `U32`: | |
| 1167 | + | |
| 1168 | + CLZ(x) CTZ(x) | |
| 1169 | + bsr r, r bsf r, r | |
| 1170 | + jnz L jnz L | |
| 1171 | + mov r, -1 mov r, 32 | |
| 1172 | + L: neg r L: | |
| 1173 | + add r, 31 | |
| 1174 | + | |
| 1175 | +`popCount` adds up the ones as `lib/Bytes.raz`'s `count` does, with | |
| 1176 | +no loop: each two bits become the number of their ones, then each | |
| 1177 | +four, then each byte, and a multiplication by `0101...H` adds the | |
| 1178 | +eight bytes into the top one, in rax and rdx beside `x`'s register: | |
| 1179 | + | |
| 1180 | + mov rax, r; shr rax, 1; mov rdx, 5555...H; and rax, rdx; sub r, rax | |
| 1181 | + mov rax, r; shr rax, 2; mov rdx, 3333...H; and rax, rdx; and r, rdx; add r, rax | |
| 1182 | + mov rax, r; shr rax, 4; add r, rax; mov rdx, 0F0F...H; and r, rdx | |
| 1183 | + mov rdx, 0101...H; imul r, rdx; shr r, 56 | |
| 1184 | + | |
| 1159 | 1185 | Checks: `check` jumps, when the check fails, to code at the end of the |
| 1160 | 1186 | procedure (`endFails`), so that a check that holds is a jump not taken. |
| 1161 | 1187 | That code puts the values involved where `Rt.fail` wants them and calls |
| @@ -1644,6 +1670,13 @@ | ||
| 1644 | 1670 | but for `MAX(U64)` and `MAX(UINT)`, which are above `high(I64)`, where |
| 1645 | 1671 | an untyped constant ends, and keep their type, as a hexadecimal number |
| 1646 | 1672 | above it is a `U64`. |
| 1673 | + | |
| 1674 | +`CLZ(x)`, `CTZ(x)` and `POPCOUNT(x)` (report 2026-10.36) are Item's | |
| 1675 | +`countBits`, which refuses an `x` not of an unsigned type, or a set for | |
| 1676 | +`POPCOUNT`, and an untyped constant with help that says to give it a | |
| 1677 | +type, as `rotand` does; folds a constant by counting its bits one by one | |
| 1678 | +(`countOf`); or has `Gen.countZeros` or `Gen.popCount` count them. The | |
| 1679 | +result is an `INT`. | |
| 1647 | 1680 | |
| 1648 | 1681 | Two mismatches in `coerce` that people meet on the way to it, a slice |
| 1649 | 1682 | where its element's pointer is wanted and a pointer where a slice is, |
| @@ -33,7 +33,8 @@ | ||
| 33 | 33 | |
| 34 | 34 | (defconst raz-builtins |
| 35 | 35 | '("SIZE" "ADR" "LEN" "PTR" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" |
| 36 | - "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR" "MIN" "MAX")) | |
| 36 | + "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR" "MIN" "MAX" | |
| 37 | + "CLZ" "CTZ" "POPCOUNT")) | |
| 37 | 38 | |
| 38 | 39 | (defvar raz-mode-syntax-table |
| 39 | 40 | (let ((st (make-syntax-table))) |
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR |
| 17 | 17 | syntax keyword razBuiltin SIZE ADR LEN PTR NEW MSG SYSCALL FENCE CAS XCHG XADD |
| 18 | 18 | syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR MIN MAX |
| 19 | +syntax keyword razBuiltin CLZ CTZ POPCOUNT | |
| 19 | 20 | syntax keyword razConstant true false nil |
| 20 | 21 | |
| 21 | 22 | " 123, 0FFH, 1.5, 2.0E-3 |
| @@ -1065,6 +1065,79 @@ | ||
| 1065 | 1065 | stack[depth - 1].k := kWord; |
| 1066 | 1066 | end; |
| 1067 | 1067 | |
| 1068 | +(* CLZ, if leading, or CTZ of the unsigned integer of size bytes on top | |
| 1069 | + (report section 6.1). bsr finds the place h of its highest one bit, | |
| 1070 | + and the zeros above it are the width less 1 less h; bsf finds the | |
| 1071 | + place of its lowest, which is the zeros below it. Neither finds a | |
| 1072 | + place in 0, the one value with no one bit, and both say so by ZF: | |
| 1073 | + then the count is the whole width, as if h were -1, or the lowest | |
| 1074 | + one at the width. The bits above size bytes are zero, so a scan of | |
| 1075 | + all 64 sees the value's own. The baseline x86-64 lacks lzcnt and | |
| 1076 | + tzcnt, which would count 0 by themselves. For a U32: | |
| 1077 | + | |
| 1078 | + bsr r, r bsf r, r | |
| 1079 | + jnz L jnz L | |
| 1080 | + mov r, -1 mov r, 32 | |
| 1081 | + L: neg r L: | |
| 1082 | + add r, 31 *) | |
| 1083 | +proc countZeros*(leading: BOOL; size: INT); | |
| 1084 | +var r, w, L: INT; | |
| 1085 | +begin | |
| 1086 | + r := top(); | |
| 1087 | + w := size * 8; | |
| 1088 | + L := NoLabel; | |
| 1089 | + X.bitScan(leading, r); | |
| 1090 | + X.jcc(X.ccNE, L); | |
| 1091 | + if leading then X.movRI(r, -1); else X.movRI(r, w); end; | |
| 1092 | + X.fixLink(L); | |
| 1093 | + if leading then | |
| 1094 | + X.unary(3, r); (* neg *) | |
| 1095 | + X.aluI(X.aluAdd, r, w - 1); | |
| 1096 | + end; | |
| 1097 | + stack[depth - 1].k := kWord; | |
| 1098 | +end; | |
| 1099 | + | |
| 1100 | +(* POPCOUNT of the integer or set on top (report section 6.1), counted | |
| 1101 | + as lib/Bytes.raz counts bytes, without a loop: each two bits are | |
| 1102 | + made the sum of their ones, then each four, then each byte, and a | |
| 1103 | + multiplication by 0101...H adds the eight bytes' sums into the top | |
| 1104 | + byte. The baseline x86-64 lacks popcnt. rax and rdx are | |
| 1105 | + free between the stack machine's operations. | |
| 1106 | + | |
| 1107 | + mov rax, r; shr rax, 1; mov rdx, 5555...H; and rax, rdx; sub r, rax | |
| 1108 | + mov rax, r; shr rax, 2; mov rdx, 3333...H; and rax, rdx; and r, rdx; add r, rax | |
| 1109 | + mov rax, r; shr rax, 4; add r, rax; mov rdx, 0F0F...H; and r, rdx | |
| 1110 | + mov rdx, 0101...H; imul r, rdx; shr r, 56 *) | |
| 1111 | +proc popCount*; | |
| 1112 | +var r: INT; | |
| 1113 | +begin | |
| 1114 | + r := top(); | |
| 1115 | + (* each two bits: their value less its upper bit, which is its ones *) | |
| 1116 | + X.movRR(RAX, r); | |
| 1117 | + X.shiftI(X.shShr, RAX, 1); | |
| 1118 | + X.movRI(RDX, 5555555555555555H); | |
| 1119 | + X.alu(X.aluAnd, RAX, RDX); | |
| 1120 | + X.alu(X.aluSub, r, RAX); | |
| 1121 | + (* each four bits: the sums of its two pairs *) | |
| 1122 | + X.movRR(RAX, r); | |
| 1123 | + X.shiftI(X.shShr, RAX, 2); | |
| 1124 | + X.movRI(RDX, 3333333333333333H); | |
| 1125 | + X.alu(X.aluAnd, RAX, RDX); | |
| 1126 | + X.alu(X.aluAnd, r, RDX); | |
| 1127 | + X.alu(X.aluAdd, r, RAX); | |
| 1128 | + (* each byte: the sums of its two halves, at most 8, which fits four bits *) | |
| 1129 | + X.movRR(RAX, r); | |
| 1130 | + X.shiftI(X.shShr, RAX, 4); | |
| 1131 | + X.alu(X.aluAdd, r, RAX); | |
| 1132 | + X.movRI(RDX, 0F0F0F0F0F0F0F0FH); | |
| 1133 | + X.alu(X.aluAnd, r, RDX); | |
| 1134 | + (* the top byte of r times 0101...H is the sum of all eight *) | |
| 1135 | + X.movRI(RDX, 0101010101010101H); | |
| 1136 | + X.imul(r, RDX); | |
| 1137 | + X.shiftI(X.shShr, r, 56); | |
| 1138 | + stack[depth - 1].k := kWord; | |
| 1139 | +end; | |
| 1140 | + | |
| 1068 | 1141 | (* The left op the right of the two values on top, as a BOOL *) |
| 1069 | 1142 | proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL); |
| 1070 | 1143 | var f: BOOL; r, x, y, i: INT; v: I64; c: BOOL; m: X.Mem; |
| @@ -1560,6 +1560,55 @@ | ||
| 1560 | 1560 | return nil; |
| 1561 | 1561 | end; |
| 1562 | 1562 | |
| 1563 | +(* CLZ, CTZ or POPCOUNT, which, of v, a value w bits wide, folded *) | |
| 1564 | +proc countOf(which: INT; v: U64; w: I64): I64; | |
| 1565 | +var n, i: I64; | |
| 1566 | +begin | |
| 1567 | + n := 0; | |
| 1568 | + if which = Sym.bPopcount then | |
| 1569 | + for i := 0 to w - 1 do n := n + I64((v shr i) & 1); end; | |
| 1570 | + elsif which = Sym.bClz then | |
| 1571 | + while (n < w) and (((v shr (w - 1 - n)) & 1) = 0) do INC(n); end; | |
| 1572 | + else | |
| 1573 | + while (n < w) and (((v shr n) & 1) = 0) do INC(n); end; | |
| 1574 | + end; | |
| 1575 | + return n; | |
| 1576 | +end; | |
| 1577 | + | |
| 1578 | +(* CLZ(x), CTZ(x) or POPCOUNT(x), which: the zero bits of x above its | |
| 1579 | + highest one, below its lowest, or its one bits, an INT (report | |
| 1580 | + section 6.1). x is of an unsigned integer type, whose width is what | |
| 1581 | + 0 counts, or for POPCOUNT a set; an untyped constant has no width. | |
| 1582 | + Of a constant the count is folded. *) | |
| 1583 | +proc countBits*(var x: Item; which: INT): ERROR; | |
| 1584 | +var name, s: []U8; ok: BOOL; | |
| 1585 | +begin | |
| 1586 | + if x.mode in {mType, mBuiltin, mNone, mCase, mStr} then return notValue(x); end; | |
| 1587 | + if which = Sym.bClz then name := "CLZ"; elsif which = Sym.bCtz then name := "CTZ"; else name := "POPCOUNT"; end; | |
| 1588 | + if which = Sym.bPopcount then | |
| 1589 | + s := say2("{} needs an unsigned integer or a set, found {}", name, what(x)); | |
| 1590 | + else | |
| 1591 | + s := say2("{} needs an unsigned integer, found {}", name, what(x)); | |
| 1592 | + end; | |
| 1593 | + if x.typ = Sym.tConst then | |
| 1594 | + return Scan.markHelp(x.pos, s, | |
| 1595 | + say1("an untyped constant has no width to count in: give it a type, as in U32({})", Diag.source(x.pos))); | |
| 1596 | + end; | |
| 1597 | + ok := (x.typ^.form = Sym.fInt) and not x.typ^.signed; | |
| 1598 | + if (x.typ^.form = Sym.fSet) and (which = Sym.bPopcount) then ok := true; end; | |
| 1599 | + if not ok then return Scan.markAt(x.pos, s); end; | |
| 1600 | + if x.mode = mConst then | |
| 1601 | + makeConst(x, Sym.tInt, countOf(which, U64(x.val), bits(x.typ))); | |
| 1602 | + return nil; | |
| 1603 | + end; | |
| 1604 | + try value(x); | |
| 1605 | + if which = Sym.bPopcount then Gen.popCount(); else Gen.countZeros(which = Sym.bClz, intSize(x.typ)); end; | |
| 1606 | + x.typ := Sym.tInt; | |
| 1607 | + x.mode := mStack; | |
| 1608 | + x.made := false; (* a built-in's result, not an operator's *) | |
| 1609 | + return nil; | |
| 1610 | +end; | |
| 1611 | + | |
| 1563 | 1612 | (* MIN(T), or MAX(T) if max: the smallest or the largest value of the |
| 1564 | 1613 | integer type t, an untyped constant (report section 6.1); p is where |
| 1565 | 1614 | T is written. MAX(U64) and MAX(UINT) are above high(I64), where the |
| @@ -1106,6 +1106,9 @@ | ||
| 1106 | 1106 | start := Scan.tokPos(); |
| 1107 | 1107 | t := try parseType(); |
| 1108 | 1108 | try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(start)); |
| 1109 | + | Sym.bClz, Sym.bCtz, Sym.bPopcount: | |
| 1110 | + try expression(x, nil); | |
| 1111 | + try Item.countBits(x, which); | |
| 1109 | 1112 | | Sym.bCopy: |
| 1110 | 1113 | try expression(x, nil); |
| 1111 | 1114 | try Item.copyDest(x); |
| @@ -132,7 +132,7 @@ | ||
| 132 | 132 | bSize* = 0; bAdr* = 1; bLen* = 2; bNew* = 3; bMsg* = 4; bSyscall* = 5; |
| 133 | 133 | bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11; |
| 134 | 134 | bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17; |
| 135 | - bMin* = 18; bMax* = 19; | |
| 135 | + bMin* = 18; bMax* = 19; bClz* = 20; bCtz* = 21; bPopcount* = 22; | |
| 136 | 136 | |
| 137 | 137 | MaxDepth* = 64; (* scopes open at once *) |
| 138 | 138 | |
| @@ -715,4 +715,7 @@ | ||
| 715 | 715 | enter("ROR", oBuiltin, nil, bRor); |
| 716 | 716 | enter("MIN", oBuiltin, nil, bMin); |
| 717 | 717 | enter("MAX", oBuiltin, nil, bMax); |
| 718 | + enter("CLZ", oBuiltin, nil, bClz); | |
| 719 | + enter("CTZ", oBuiltin, nil, bCtz); | |
| 720 | + enter("POPCOUNT", oBuiltin, nil, bPopcount); | |
| 718 | 721 | end Sym. |
| @@ -533,6 +533,17 @@ | ||
| 533 | 533 | if n >= 0 then Chunk.put1(n); end; |
| 534 | 534 | end; |
| 535 | 535 | |
| 536 | +(* bsr r, r, if reverse, or bsf r, r: the place of r's highest, or | |
| 537 | + lowest, one bit, 0F BD and 0F BC. ZF is set if r is 0, which has no | |
| 538 | + one bit, and r is then left undefined. *) | |
| 539 | +proc bitScan*(reverse: BOOL; r: INT); | |
| 540 | +var opc: INT; name: []U8; | |
| 541 | +begin | |
| 542 | + if reverse then opc := 0BD0FH; name := "bsr"; else opc := 0BC0FH; name := "bsf"; end; | |
| 543 | + if listing then say(text2(name, rn(r), rn(r))); end; | |
| 544 | + opR(0, true, opc, r, r, false); | |
| 545 | +end; | |
| 546 | + | |
| 536 | 547 | (* F7 /ext: 2 not, 3 neg, 6 div, 7 idiv *) |
| 537 | 548 | proc unary*(ext, r: INT); |
| 538 | 549 | begin |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ClzSet.raz:7:14: error: CLZ needs an unsigned integer, found Days | |
| 2 | + 7 | n := CLZ(s); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ClzUntyped.raz:5:14: error: CLZ needs an unsigned integer, found 1 | |
| 2 | + 5 | n := CLZ(1); | |
| 3 | + | ^ | |
| 4 | +help: an untyped constant has no width to count in: give it a type, as in U32(1) | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/CtzSigned.raz:5:14: error: CTZ needs an unsigned integer, found I32 | |
| 2 | + 5 | n := CTZ(x); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/PopcountReal.raz:2:20: error: POPCOUNT needs an unsigned integer or a set, found 1.5 | |
| 2 | + 2 | const N = POPCOUNT(1.5); | |
| 3 | + | ^^^ | |
| 4 | +raz: 1 error |