(63 - nlz) / 7, where nlz is between 0 and 63 lookup table with 64 entries anyone?
A bitshift oneliner IS nicer.
Bitwise Division
31–40 of 75 posts
Re: Bitwise Division
#32ChatGPT tells me: Dividing a number by 7 using bitwise operations can be done using a technique called "magic numbers." This involves precomputing a number that, when combined with bitwise operations, will give the correct result. Here's how it can be done: First, we need to find a "magic number" that will help us divide by 7. One way to do this is to use the fact that 7 is a prime number, which means that there is a…
Re: Bitwise Division
#33Re: Bitwise Division
#34I always have a hard time grooking bit wise operations. Any good resources?
Re: Bitwise Division
#35Wouldn’t a lookup table be a whole lot easier? I’d think a 64 entry table of integers would be fast to access once cached.
I don't use lookup tables that much when optimizing these days, because most math instructions cost far fewer cycles than a cache miss. And even if your lookup table is in L3, it still costs ~40 cycles to retrieve each line.
The miss cost is, therefore, not really relevant: if this code is hotpath at all the cost of a single cache miss is amortized across millions of calls. Your lookup table will be in cache as surely as the code that reads it is.
Re: Bitwise Division
#36I always have a hard time grooking bit wise operations. Any good resources?
Re: Bitwise Division
#37Re: Bitwise Division
#38(63 - nlz) / 7, where nlz is between 0 and 63 lookup table with 64 entries anyone?
Re: Bitwise Division
#39Nice, if a little bit hand wavy. Seems a little bit of a stretch to call an operation that still includes a multiply bitwise , though.
Re: Bitwise Division
#40Earlier quoted context omitted.
Maybe nicer, but slower. And I'm not even sure about the former.
Probably faster: no memory loads, no cache pressure