One application for the Population count of a 32-bit register gem is to implement a fast BSF macro that might actually be faster than the built-in BSF
instructions (this depends on the CPU, and the distribution if the data values fed into BSF
).
PREPBSF is a macro that implements the front end of a BSF
implementation based on population count. The macro makes use of the following observation. Assume an input represented by the following bit string (regular expression): {x}n1{0}m If this number is negated, if becomes {~x}n1{0}m. That is, all bits to the left of the least significant 1-bit become inverted. By OR-ing the original input with the negated input, we get {1}n1{0}m, that is all bits to the left of the least significant 1-bit are set. Inverting this result gives {0}(n+1){1}m. Note that the number of 1-bits in the transformed argument is equal to the bit position of the least significant 1-bit in the original argument.;
; front end for BSF macro
;
; input:
; eax = number to BSF
;
; output:
; esi = prepared number to BSF
;
; destroys:
; esi
; eflags
;
MACRO PREPBSF
mov esi,eax
neg esi ; -n
or esi,eax ; n | (-n)
not esi ; ~(n | (-n))
ENDM
Macro EMBSF1 emulates the BSF
instruction for non-zero inputs. It is based on an population count algorithm.;
; emulate BSF
;
; input:
; eax = word to BSF (!= 0)
;
; output:
; edx = bit number from result of BSF
;
; destroys:
; esi, edx
; eflags
;
MACRO EMBSF1
PREPBSF
POPCOUNT1
ENDM