On 10/4/2012 7:13 PM, andy pugh wrote: > On 4 October 2012 01:37, Dave Hylands <[email protected]> wrote: > >> There is do_div (for all architectures) and div_u64_rem (for x86) in >> <asm/div64.h> > do_div appears to be almost perfect. But I really don't understand it. > > I can pull out the digits really easily with > > for (i = c ; i > 0 ; i--){ > out[i] = digits[do_div(tmp, 10)]; > } > (digits is an array of numerals and abcdef) > > do_div returns the remainder, and divides the first argument by the > second. _in_place_. > It's the in-place bit I don't understand. It isn't passed by > reference, it isn't a pointer, it is a normal u64 integer. > (I tried using a pointer, and it doesn't work) > > http://codepad.org/UdqruNIt shows an example. > > What am I missing? I read of "understanding the side effects" on the > web. I don't even understand the intended effects. > > Andy:
You get, I assume, that dividing one integer (the dividend) by another integer (the divisor) results generally in an integer quotient plus an integer remainder*. That's a problem for C, where a function returns a single value. That "in-place bit" you question refers to the black magic in do_div() (which is actually a macro that generates asm code) that replaces the dividend by the quotient during the computation and returns the remainder as the value of the function. That the dividend is no longer the dividend when the function is exited is the side effect. Of course, a practical person would assume do_div() returns the quotient. Hence the 2003 email exchange "do_div considered harmful" in which even Linus agrees do_div() has strange semantics. To quote him "Not very many people should use "do_div()" directly (and a quick grep shows that not very many people do). It's generally a mistake to do so, I suspect. The thing was originally written explicitly for "printk()" and nothing else." Who am I to question Linus's use of grep but according to Free Electron's Linux Cross Reference, do_div() is referenced in 292 files. To me it's a "smoke 'em if you got 'em" situation, e.g., if you've got do_div() at your disposal, use it. Regards, Kent *this is one of the few things I remember from my math studies: the set of integers is not closed with respect to division. ------------------------------------------------------------------------------ Don't let slow site performance ruin your business. Deploy New Relic APM Deploy New Relic app performance management and know exactly what is happening inside your Ruby, Python, PHP, Java, and .NET app Try New Relic at no cost today and get our sweet Data Nerd shirt too! http://p.sf.net/sfu/newrelic-dev2dev _______________________________________________ Emc-developers mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/emc-developers
