Sujet : Re: Fast division (was Re: Suggested method for returning a string from a C program?)
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 26. Mar 2025, 17:55:13
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vs1bhh$24nub$6@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 26/03/2025 14:47, Janis Papanagnou wrote:
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.
For non-constant divisors, it's a bit more difficult! If you are going to use the same divisor several times, it can be worth computing the scaled reciprocal, so that you only have one hard operation. I believe that hardware floating point units do that anyway for division.
| Date | Sujet | # | | Auteur |
| 22 Apr 26 | … | | | |
Haut de la page
Les messages affichés proviennent d'usenet.
NewsPortal