nikolaos....@gmail.com wrote:
> Dear all,
>
> I believe I have invented a new approximation for quotient
> calculation that works for x/63 and x/127.
>
> The formula is as follows and it is divisionless and multiplierless:
>
> y = (((x>>n)+x+((1<<n)+1))>>n)-1;
>
> Use n=6 for 63, and n=7 for 127. 1<<n is the strength-reduced form
> for calculating 2^n.
Nothing new here.:-)
Instead of dividing by 63 you are multiplying by 65, and since
(x+1)(x-1) = x^2 - 1, the error is manageable.
Effectively you are doing reciprocal mul with a multiplier with very few
significant bits.
This also means that your formula only works for a limited range of
inputs (i.e. number of bits in x), and it fails badly when (for a 32-bit
number) x is so close to 2^32 that adding x/64 to it causes a carry.
BTW, I use a similar trick in my data calculations, when I need to
divide a small range of inputs by 100 in order to convert a year number
to century:
// y100 = y / 100; // Reciprocal mul
y100 = y * 41 >> 12; // Faster version with very short reciprocal
The multiplier has just three bits set (101001) so it can be calculated
with a pair of LEAs:
lea ebx, [eax+eax*4] ;; y*5
lea ebx, [eax+ebx*8] ;; y*41
shr ebx,12
The result is exact for all possible inputs in the 0..400 range which is
what I need at this point.
Terje
--
- <Terje.Mathisen at
tmsw.no>
"almost all programming can be viewed as an exercise in caching"