Re: bsr-algorithm?



Anybody got an idea about a branch-free 'emulation' of bsr?

Sure!

Getting the most significant bit is easy in SSE, as long as you don't
need to work with integers larger than 2^24, or can stand the overhead
of converting to double first:

Convert the 4x32-bit (i.e. 4x24-bit) integers into 4xfloat, then treat
the result as integers, extracting the exponent field and subtracting
127 to get the most significant bit position.

If you have to handle zero, then you'll have to add a compare against
zero either before or after the conversion, then use that as a mask to
POR on top of the regular result, so that you'll return -1 for zero inputs.

Branchless, maybe 10 instructions, mostly fast opcodes.

OK?

Uff, that's crazy ... you will think that too, if you know for what I
actually use the function: short <-> half-float <-> float <-> long
conversion, for transcoding and creating the exponent.

What you are actually suggesting is to treat a 16bit half-float as a
short, convert it's exponent into a float, and extracting the
exponent, to know which is the first-exponent bit set on the half-
float.

I suppose it works and is possibly even fast enough, I'd hoped for a
pure integer-algorithm though.

Thanks!
Niels

.



Relevant Pages

  • Re: Type casting
    ... I have a small mess with the conversion of numeric types in the ... the outcome of E / 2 cannot be of type float. ... trying to truncate a float to an integer, ... Float'Truncation truncates F to an integer, truncating toward zero, ...
    (comp.lang.ada)
  • Re: defpackage and intern- internal and external
    ... I've put his macros in a seperate package because ... (multiple-value-bind (mantissa exponent sign) ... (integer-decode-float float) ... (byte,exponent-bits,(1- mantissa-bits)) ...
    (comp.lang.lisp)
  • Re: RfD: IEEE-FP 0.5.2 -- FNEXTUP
    ... >> borrows into the exponent field are exactly what's needed. ... >> And that it'll still work for binary80. ... of two (zero or nonzero fractional part of mantissa). ...
    (comp.lang.forth)
  • Re: How to output FLOAT(18) in full form by E-format in VA PL/I?
    ... > dcl a float(18), b float, c float; ... positions for E and the sign of the exponent ... Your problem is caused by having mis-matched format items ... Otherwise digits or sign will or may be lost. ...
    (comp.lang.pl1)
  • Re: how do I get more numbers past the decimal?
    ... A floating point number is stored on a 32 bit machine using the 24 bit for the mantissa and its sign and 8 bits for the exponent and its sign. ... A float value can be exactly represented by up to 9 decimal digits. ...
    (comp.lang.php)

Quantcast