www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - clz

reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
I have this c++ code with clang

uint32_t val = 1111490560;
int leading_zeros = __builtin_clz( val << 1); // equals 0
int leading_ones  = __builtin_clz(~val << 1); // equals 1
return (lz == 0 ? lo - 1 : -lz);

and want to translate it to D.

import core.bitop : bsf,bsr;

uint val = 1111490560;

int leading_zeros_r = bsr( val << 1); // equals 31
int leading_ones_r  = bsr(~val << 1); // equals 30
int leading_zeros_f = bsf( val << 1); // equals 23
int leading_ones_f  = bsf(~val << 1); // equals 1


 From what I gather from wikipedia I ought to be using bsr. But 
none of them work.

WTF?
Apr 01
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 02/04/2017 6:28 AM, Nicholas Wilson wrote:
 I have this c++ code with clang

 uint32_t val = 1111490560;
 int leading_zeros = __builtin_clz( val << 1); // equals 0
 int leading_ones  = __builtin_clz(~val << 1); // equals 1
 return (lz == 0 ? lo - 1 : -lz);

 and want to translate it to D.

 import core.bitop : bsf,bsr;

 uint val = 1111490560;

 int leading_zeros_r = bsr( val << 1); // equals 31
 int leading_ones_r  = bsr(~val << 1); // equals 30
 int leading_zeros_f = bsf( val << 1); // equals 23
 int leading_ones_f  = bsf(~val << 1); // equals 1


 From what I gather from wikipedia I ought to be using bsr. But none of
 them work.

 WTF?
BSR looks like its working. http://x86.renejeschke.de/html/file_module_x86_id_20.html http://stackoverflow.com/a/9353998
Apr 01
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 2 April 2017 at 05:33:31 UTC, rikki cattermole wrote:
 On 02/04/2017 6:28 AM, Nicholas Wilson wrote:
 [...]
BSR looks like its working. http://x86.renejeschke.de/html/file_module_x86_id_20.html http://stackoverflow.com/a/9353998
Ahh there's a subtraction involved. Many thanks!
Apr 01