Re: How to count zeros in registers?



On Thu, 1 Dec 2005 15:12:26 +0000 (UTC), the renowned
kensmith@xxxxxxxxxxxxxxx (Ken Smith) wrote:

>In article <pan.2005.12.01.03.10.44.378533@xxxxxxxxxxx>,
>Rich Grise <rich@xxxxxxxxxxx> wrote:
>[...]
>>NZeros = 0;
>>loop_count = 7; // or maybe this should be 8;
>>While ((X & 0x80) && loop_count) (
>> NZeros++;
>> X <<= 1;
>> loop_count--;
>>}
>
>If there are only 8 bits to worry about:
>
> X := (X and $55) + ((X shr 1) and $55);
> X := (X and $33) + ((X shr 2) and $33);
> X := (X and $07) + ((X shr 4) and $07);

Clever, but doesn't that yield the total number of '1's rather than
the number of leading zeros?

/* return the number of leading zeros, 0x08 if all zeros,
for 0 <= n <= 255 /
int countlz(int n)
{
int i=0;
while ((i < 8) && (0 == (n & 0x80)))
{
i++;
n <<=1;
};
return i;
}

It might be faster to change the while condition to something like
(0==((i | n) & 0x80)), and check for the zero condition first.



Best regards,
Spehro Pefhany
--
"it's the network..." "The Journey is the reward"
speff@xxxxxxxxxxxx Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
.



Relevant Pages

  • reading data from a file
    ... This method fills the last row with only zeros for some reason. ... checked and there are rows with only zeros in the original data file. ... int col = 0; ... public void printArray{ ...
    (comp.lang.java.help)
  • function
    ... A file of zeros. ... main (int argc, char *argv) ... int b, num; ...
    (comp.lang.c)
  • Zero padding - CUDA vs MATLAB - fft2() vs CUFFT
    ... I am trying to convert a matlab code to CUDA. ... In matlab, the functionY = fft2truncates X, or pads X with zeros to create an m-by-n array before doing the transform. ... zeroPadding(cufftComplex* Filter, cufftComplex* InputFilterFFT, int newCols, int newRows, int oldCols, int oldRows) { ...
    (comp.soft-sys.matlab)
  • Re: Size of a process
    ... int main ... so there is no need to store the zeros in the executable. ... Copyright Microsoft Corporation. ... Microsoft Incremental Linker Version 9.00.30729.01 ...
    (comp.lang.c)
  • Re: Left Shift with Unsigned Long
    ... Leading zeros have no meaning. ... Since version 1.52 there hasn't been any MS compiler where that would be ... then after shifting 0x8000 is the value and it's promoted to unsigned int ...
    (microsoft.public.vc.language)