home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!caen!batcomputer!cornell!ressler
- From: ressler@cs.cornell.edu (Gene Ressler)
- Subject: Re: finding 1st one in integer
- Message-ID: <1992Jul22.152827.14267@cs.cornell.edu>
- Organization: Cornell Univ. CS Dept, Ithaca NY 14853
- References: <Brqu3F.1J4@undergrad.math.waterloo.edu> <LOWRY.92Jul21165844@rotor.watson.ibm.com> <BrsMBE.98t@undergrad.math.waterloo.edu>
- Date: Wed, 22 Jul 1992 15:28:27 GMT
- Lines: 38
-
- In article <BrsMBE.98t@undergrad.math.waterloo.edu> amichail@cayley.waterloo.edu (Amir Michail) writes:
- >In article <LOWRY.92Jul21165844@rotor.watson.ibm.com>, lowry@watson.ibm.com (Andy Lowry) writes:
- >> return keys[x % 37];
- >
- >Yes, I thought of this method initially. The only problem is that the
- >modulus operation is *VERY* slow on some architectures such as the sparc.
-
- Right. This is why I proposed counting 1's. If you assume 2s complement and
- that n has exactly one 1, then just decrement and count. From the nice posted
- benchmark code (chris torek?), this runs about 400% faster than the 37 element
- table lookup on a Sparc:
-
- int position_of_1(n)
- register unsigned long n;
- {
- --n; /* Replace with n=(n&-n)-1 to find pos of ls1b in any number. */
- n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
- n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
- n = (n + (n >> 4)) & 0x0f0f0f0f;
- n += n >> 8;
- n += n >> 16;
- return (n & 0xff);
- }
-
- Even without slow %, this seems better than tables because table
- accesses can cause cache misses.
-
- Murphey's 1st law of caches:
- With an n-way set-associative cach, you'll have n+1 conflicting
- accesses in the inner loop.
-
- Murphey's 2nd law of caches:
- Murphey's 1st law doesn't apply to timing benchmarks.
-
- It also promises better instruction schedules than vadim's jumping
- code. Is it faster? I haven't tried it.
-
- Gene
-