RIOT-OS / RIOT

RIOT - The friendly OS for IoT
https://riot-os.org
GNU Lesser General Public License v2.1
4.93k stars 1.99k forks source link

bitarithm_msb implementation #2092

Closed attdona closed 9 years ago

attdona commented 9 years ago

The current implementation of bitarithm_msb does not return the Most Significant Byte:

unsigned bitarithm_msb(unsigned v)
{
   if ((signed) v >= 0) {
      v &= -v;
      return bitarithm_lsb(v);
  }
  else {
    return sizeof (v) * 8 - 1;
  }
}

A right implementation could be:

unsigned bitarithm_msb(unsigned v)
{
unsigned int r = 0; // r will be lg(v)

while (v >>= 1) // unroll for more speed...
{
  r++;
}
return r;
}

according to http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious

OlegHahm commented 9 years ago

Thanks for spotting. At a first glance #975 seems to have broken it. Reverting ed7e233876239e5a6815150c33fb74215069f829 apparently solves the misbehavior. The second problem is that the unittests are somewhat poorly designed for this module.

attdona commented 9 years ago

Ciao Oleg,

I'm just started to play with RIOT and I think it rocks!

I'm working with msp430fram chip (http://www.ti.com/tool/msp-exp430fr5969) and if I get some interesting results I will share.

Greetings from Trento Attilio

On Wed, Nov 26, 2014 at 3:42 PM, Oleg Hahm notifications@github.com wrote:

Thanks for spotting. At a first glance #975 https://github.com/RIOT-OS/RIOT/pull/975 seems to have broken it. Reverting ed7e233 https://github.com/RIOT-OS/RIOT/commit/ed7e233876239e5a6815150c33fb74215069f829 apparently solves the misbehavior. The second problem is that the unittests are somewhat poorly designed for this module.

— Reply to this email directly or view it on GitHub https://github.com/RIOT-OS/RIOT/issues/2092#issuecomment-64654784.

OlegHahm commented 9 years ago

Closed by #2094.