bfffo

Find first one in bit field 6802068000 with opt bit

Finds the first 1 in a bit field and writes its bit offset to a data register.

Select opt p=68020, or use opt bit with the 68000 or 68010, before any code or data

XNZVC –●●00
  • ● set from the result
  • 0 always 0
  • – not changed

Forms

Operands

<ea>
An operand using one of the addressing modes listed for this form. The list determines whether it can be a register, memory or an immediate value. With opt p=68020, an allowed d(An,Xn) or d(PC,Xn) mode also permits the corresponding 68020 indexed and memory indirect forms. See 68020 addressing. More
{offset:width}
A field of 1 to 32 bits, written immediately after the operand. Literal offsets range from 0 to 31, and literal widths from 1 to 32. Either may be a data register, as in {d1:d2}. See the linked section for register values, negative memory offsets and wrapping. More
Dn
A data register, d0 to d7. More

The search starts at the field’s highest bit. The result is the supplied field offset plus the number of leading zero bits in the field. If the field is all zeros, the result is offset + width, the position just after the field.

bfffo d0{0:32}, d1 counts the leading zeros of d0. Subtract that count from 31 to obtain the highest set bit’s usual bit number. For a bitmap, the returned offset can identify the first slot marked by a 1.

N copies the field’s highest bit, and Z is set if no 1 was found. V and C are cleared, and X is unchanged.

Write the field as {offset:width} immediately after its operand. It selects 1 to 32 bits, counted from bit 31 of a data register or bit 7 of the byte at the memory address. The offset and width may each be a constant or a data register. See bit fields for their ranges, register values and wrapping rules.

opt p=68020
* --- The highest bit set in a number ---
move.l #$12345, d0
bfffo d0{0:32}, d1 ; 15 zeros come before the first 1: d1 = 15
moveq #31, d2
sub.l d1, d2 ; d2 = 16: bit 16 is the highest bit set

* --- The first free slot of a bitmap, where 1 is free ---
lea slots, a0
bfffo (a0){0:16}, d3 ; slots 0 to 4 are taken: d3 = 5
bra END

slots:
    dc.w %0000010000000000

END: