Re: Best way to emulate BSR?
- From: "Herbert Glarner" <spamtrap@xxxxxxxxxx>
- Date: Thu, 31 Aug 2006 08:33:20 +0200
certainJim Leonard wrote:
I have a need to determine the minimum number of bits to hold a
likenumber, and BSR looks like a great way to do that. Unfortunately, the
target platform for this project does not support 386 instructions
BSR. Is there a bitwise trick on emulating BSR?
Also, if your machine can convert an integer into IEEE 754 Floating Point
format (Single, 32 bits), there's a pretty elegant way for the whole
thingie.
IEEE will hold the 32 bits as follows:
seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
where s=sign bit, e=8 biased exponent bits, m=23 mantissa bits (after
decimal point)
such that (1+m)*2^^(e-127)
Basically, eeeeeeee-127 is the MSB already, so that you simply can extract
the bits and subtract 127.
For 0, the 9 MSBs will be set = no MSB exists in your integer.
If s is set, you have a negative value = bit 31 is set.
Some examples:
0 -> 1 11111111 11111111111111110000001 -> 1 11111111b = No MSB exists
1 -> 0 01111111 00000000000000000000000 -> 01111111b-127=0
2 -> 0 10000000 00000000000000000000000 -> 10000000b-127=1
3 -> 0 10000000 10000000000000000000000 -> 10000000b-127=1
4 -> 0 10000001 00000000000000000000000 -> 10000001b-127=2
7 -> 0 10000001 11000000000000000000000 -> 10000001b-127=2
8 -> 0 10000010 00000000000000000000000 -> 10000010b-127=3
15 -> 0 10000010 11100000000000000000000 -> 10000010b-127=3
16 -> 0 10000011 00000000000000000000000 -> 10000011b-127=4
31 -> 0 10000011 11110000000000000000000 -> 10000011b-127=4
32 -> 0 10000100 00000000000000000000000 -> 10000100b-127=5
255 -> 0 10000110 11111110000000000000000 -> 10000110b-127=7
256 -> 0 10000111 00000000000000000000000 -> 10000111b-127=8
2^31-1 -> 0 10011101 11111111111111111111111 -> 10011101b-127=30
2^31 -> 1 10011110 00000000000000000000000 -> Signbit -> 31
2^32-1 -> 1 01111111 00000000000000000000000 -> Signbit -> 31
So, this is your pseudocode:
Convert integer into Floating Point Single Precision
If 9 MSBs are set then
integer is 0 (no MSB in integer)
Elseif 1 MSB is set
integer is negative = MSB is 31
Else
Shift right 23 bits
Subtract 127
Regards
//Herbert Glarner
.
- References:
- Best way to emulate BSR?
- From: Jim Leonard
- Re: Best way to emulate BSR?
- From: randyhyde@xxxxxxxxxxxxx
- Re: Best way to emulate BSR?
- From: Herbert Glarner
- Best way to emulate BSR?
- Prev by Date: Re: AoA setup
- Next by Date: Re: C++
- Previous by thread: Re: Best way to emulate BSR?
- Next by thread: Re: Best way to emulate BSR?
- Index(es):