Sujet : Re: Fast division (was Re: Suggested method for returning a string from a C program?)
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 26. Mar 2025, 14:47:02
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vs10go$1sing$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 26.03.2025 12:40, David Brown wrote:
>
[ substituting a division by multiplication (and some primitives) ]
Roughly speaking, when you want division of "y" by a fixed - i.e.,
constant value known at compile time - number "x", you can do it by
pre-calculating z = 2^n / x and then you implement "y / x" by "y * z /
2^n". (There's also some stuff to handle correct rounding, especially
with signed types.)
Thanks for the terse explanation; the formulas helped me to detect
that the constants in Waldek's assembler code are the pre-computed
reciprocals.
First I thought that there's a trick to avoid division of non-const
expressions.
Janis
[...]