Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can replace integer division by a constant, by multiplication (modulo wordsize) with a magic number and a shift.</p> <p>The magic numbers can be pre-calculated for known constants.</p> <p>Since n can't take many values e.g. 0..31 it is "easy" to pre-calculate these magic numbers for all n and store it in a table with 32 elements.</p> <p><a href="http://www.hackersdelight.org/magic.htm" rel="noreferrer">Javascript Page for calculating the magic numbers</a></p> <p>A good compiler can compute the magic numbers and replace integer division by multiplication and shift if the divisor is constant at compile time. Depending on how the rest of the code is structured around the performance critical code you could use macro or inline tricks to unroll for all possible values of n and let the compiler do the work of finding the magic numbers (<em>similar to the answer with the switch, but I would put more code in the constant region otherwise it might be a tradeof not worth it -- branching can cost you performance also</em>)</p> <p>Detailed description together with code for calculating the magic numbers can be fund in the Book "Hackers Delight" by Henry S. Warren, Jr. (<em>highly recommended must have book!</em>) pp. 180ff. </p> <p>Link to Google Books for the relevant chapter:</p> <p><a href="http://books.google.de/books?id=iBNKMspIlqEC&amp;lpg=PP1&amp;dq=hackers%20delight%20book&amp;pg=SL20-PA80#v=onepage&amp;q=hackers%20delight%20book&amp;f=false" rel="noreferrer">Chapter 10-9 Unsigned Division by Divisors >= 1</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload