Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch > #6620

Re: ffs16

Date 2012-04-23 16:22 +0200
From Terje Mathisen <"terje.mathisen at tmsw.no">
Newsgroups comp.arch, comp.lang.c
Subject Re: ffs16
References <jn1tku$pga$1@reader1.panix.com>
Message-ID <v65g69-41e2.ln1@ntp6.tmsw.no> (permalink)

Cross-posted to 2 groups.

Show all headers | View raw


So you've been looking into Find First Set bit for 16-bit (effectively 
unsigned) inputs?

What do you need it for?

Should it be fast? Small? Portable?

The fastest is probably to get some help from the fp hw:

int ffs16f(int x)
{
   float f = x;
   uint32_t fb = *(uint32_t *) &f; // Get access to the fp bit pattern!

   // The exponent is stored in 8 bits following the sign bit:

   int exp = (fb >> 23); // The sign bit is zero, so no need to mask

   exp -= 127; // The exponent bias

   // I assume you need to handle zero inputs? Return -1?
   exp |= (0-(x==0));

   return exp;
}
(Modulo possibly an off by one error in the bias handling...)

Terje

Joe keane wrote:
> case A
>
>      int ffs16a(int x)
>      {
> 	int res;
>
> 	if ((x&  0xFF) == 0)
> 	    res = kk_ffstab[x&  0xFF];
> 	else
> 	    res = kk_ffstab[x>>  8&  0xFF] + 8;
> 	return res;
>      }
>
> case B
>
>      int ffs16b(int x)
>      {
> 	int resl;
> 	int resh;
> 	int res;
>
> 	fla = (x&  0xFF) != 0;
> 	resl = kk_ffstab[x&  0xFF];
> 	resh = kk_ffstab[x>>  8&  0xFF] + 8;
> 	res = (fla - 1)&  resl | (0 - fla)&  resh;
> 	return res;
>      }


-- 
- <Terje.Mathisen at tmsw.no>
"almost all programming can be viewed as an exercise in caching"

Back to comp.arch | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

ffs16 jgk@panix.com (Joe keane) - 2012-04-22 21:36 +0000
  Re: ffs16 Keith Thompson <kst-u@mib.org> - 2012-04-22 15:23 -0700
  Re: ffs16 Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-22 20:37 -0400
    Re: ffs16 Philip Lantz <prl@canterey.us> - 2012-04-24 06:36 -0700
      Re: ffs16 Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-24 20:32 -0400
        Re: ffs16 Philip Lantz <prl@canterey.us> - 2012-04-25 00:30 -0700
  Re: ffs16 Terje Mathisen <"terje.mathisen at tmsw.no"> - 2012-04-23 16:22 +0200
    Re: ffs16 Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-23 21:46 -0400
      Re: ffs16 Terje Mathisen <"terje.mathisen at tmsw.no"> - 2012-04-24 14:16 +0200
        Re: ffs16 Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-24 20:40 -0400
    Re: ffs16 jgk@panix.com (Joe keane) - 2012-04-24 19:06 +0000
      Re: ffs16 Terje Mathisen <"terje.mathisen at tmsw.no"> - 2012-04-25 00:38 +0200
        Re: ffs16 jgk@panix.com (Joe keane) - 2012-04-25 20:08 +0000
          Re: ffs16 "James Van Buskirk" <not_valid@comcast.net> - 2012-04-25 15:06 -0600
            Re: ffs16 "James Van Buskirk" <not_valid@comcast.net> - 2012-04-25 16:30 -0600
        Re: ffs16 jgk@panix.com (Joe keane) - 2012-04-25 21:25 +0000
          Re: ffs16 Terje Mathisen <"terje.mathisen at tmsw.no"> - 2012-04-26 01:38 +0200
            Re: ffs16 jgk@panix.com (Joe keane) - 2012-04-26 19:34 +0000
            Re: ffs16 jgk@panix.com (Joe keane) - 2012-04-26 20:41 +0000
              Re: ffs16 Terje Mathisen <"terje.mathisen at tmsw.no"> - 2012-04-27 02:47 +0200
  Re: ffs16 MitchAlsup <MitchAlsup@aol.com> - 2012-04-23 13:01 -0700
    Re: ffs16 Terje Mathisen <"terje.mathisen at tmsw.no"> - 2012-04-24 03:28 +0200
      Re: ffs16 nmm1@cam.ac.uk - 2012-04-24 09:15 +0100
      Re: ffs16 Chris Gray <cg@GraySage.com> - 2012-04-24 13:37 -0600

csiph-web