pfirst

SVE Predicate First Active

PFIRST <Pd>.B, <Pg>, <Pn>.B

Sets destination predicate to true only at the first active element.

Details

Sets the destination predicate Pd.B such that only the first element (in vector order) where both Pg and Pn are true is set to true; all other elements are set to false. This is useful for scalar tail-processing in loops. This is an AArch64-only SVE instruction; all condition flags are unaffected.

Pseudocode Operation

for i = 0 to VL/8-1
  if Pg[i] == 1 and Pn[i] == 1 then
    Pd[i] ← 1
    break
  else
    Pd[i] ← 0
  endif
endfor
for j = i+1 to VL/8-1
  Pd[j] ← 0
endfor

Example

PFIRST p0.B, p0/m, p1.B

Encoding

Binary Layout
00100101
0
1
011000110000
0
Pg
0
Pdn
 
Format SVE Predicate
Opcode 0x2558C000
Extension SVE

Operands

  • Pd
    Dest Pred
  • Pg
    Mask
  • Pn
    First source predicate register (SVE)

Reference (Arm A64 ISA)

Instruction Forms

Encoding Instruction ISA Bit pattern
0x2558C000 PFIRST <Pdn>.B, <Pg>, <Pdn>.B A64 00100101 | 0 | 1 | 011000110000 | 0 | Pg | 0 | Pdn

Description

Sets the first active element in the destination predicate to true, otherwise elements from the source predicate are passed through unchanged. Sets the First (N), None (Z), !Last (C) condition flags based on the predicate result, and the V flag to zero.

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) result = P[dn, PL];
integer first = -1;
constant integer psize = esize DIV 8;

for e = 0 to elements-1
    if ActivePredicateElement(mask, e, esize) && first == -1 then
        first = e;

if first >= 0 then
    Elem[result, first, psize] = ZeroExtend('1', psize);

PSTATE.<N,Z,C,V> = PredTest(mask, result, esize);
P[dn, PL] = result;