cntp
SVE Count Active Predicates
CNTP <Xn>, <Pg>, <Pn>.<T>
Counts the number of true elements in a predicate.
Details
Counts the number of true bits in Pn.T (where the element size <T> determines the predicate width) that are also true in the governing predicate Pg, and writes the 64-bit count to the general-purpose register Xn. This is an AArch64-only SVE instruction; all condition flags are unaffected.
Pseudocode Operation
count ← 0
element_size ← size_in_bytes(<T>)
for i = 0 to VL/element_size-1
if Pg[i] == 1 and Pn[i] == 1 then
count ← count + 1
endif
endfor
Xn ← count
Example
CNTP x1, p0/m, p1.T
Encoding
Binary Layout
00100101
size
100
00
0
10
Pg
0
Pn
Rd
Operands
-
Xn
Dest GPR -
Pg
Mask -
Pn
First source predicate register (SVE)
Reference (Arm A64 ISA)
Instruction Forms
| Encoding | Instruction | ISA | Bit pattern | ||
|---|---|---|---|---|---|
| 0x25208000 | CNTP <Xd>, <Pg>, <Pn>.<T> | A64 | 00100101 | size | 100 | 00 | 0 | 10 | Pg | 0 | Pn | Rd | ||
| 0x25208200 | CNTP <Xd>, <PNn>.<T>, <vl> | A64 | 00100101 | size | 100 | 00 | 0 | 10000 | vl | 1 | PNn | Rd |
Description
Counts the number of active and true elements in the source predicate and places the scalar result in the destination general-purpose register. Inactive predicate elements are not counted.
Operation
CheckSVEEnabled();
constant integer VL = CurrentVL;
constant integer PL = VL DIV 8;
constant integer elements = VL DIV esize;
bits(PL) mask = P[g, PL];
bits(PL) operand = P[n, PL];
bits(64) sum = Zeros(64);
for e = 0 to elements-1
if ActivePredicateElement(mask, e, esize) && ActivePredicateElement(operand, e, esize) then
sum = sum + 1;
X[d, 64] = sum;