Friday, September 9, 2022

Digest for comp.lang.c++@googlegroups.com - 24 updates in 2 topics

Lynn McGuire <lynnmcguire5@gmail.com>: Sep 09 12:49PM -0500

"Richard Stallman Announces C Reference"

https://www.i-programmer.info/news/184-cc/15705-richard-stallman-announces-c-reference.html
 
"Richard Stallman (RMS) is a controversial figure - you either like or
dislike him - but you have to assume he knows his C. So an announcement
that he has a book on the subject is interesting."
 
"As you might guess, being the founder of the Free Software Foundation,
Stallman's book is free to download, but at this stage you will have to
do some work if you want to read it and it seems to be still in beta.
The text contains requests for comments."
 
I did not know that RS wrote the first gcc compiler.
 
Lynn
scott@slp53.sl.home (Scott Lurndal): Sep 09 07:03PM


>"Richard Stallman (RMS) is a controversial figure - you either like or
>dislike him - but you have to assume he knows his C. So an announcement
>that he has a book on the subject is interesting."
 
You know what they say about assumption. He hasn't had much to
do with the compiler for a couple of decades now.
Juha Nieminen <nospam@thanks.invalid>: Sep 09 05:29AM

>>something like 1 / (d^UNSIGNED_MAX) instead of 1/d.
 
> I didn't compile and run it , but something along those lines will do the
> exact same job as Bonitas code and probably more efficiently.
 
If you need the absolute value of an integer, I don't think there's any
way around having to use a conditional.
 
Or, better yet, just std::abs(). (The compiler will do a better job than
you at optimizing it. Eg. if there's a CPU instruction that directly
converts a register into its absolute value without conditionals, certainly
the compiler will use that.)
"Öö Tiib" <ootiib@hot.ee>: Sep 09 12:20AM -0700

On Friday, 9 September 2022 at 08:29:23 UTC+3, Juha Nieminen wrote:
> you at optimizing it. Eg. if there's a CPU instruction that directly
> converts a register into its absolute value without conditionals, certainly
> the compiler will use that.)
 
He just "optimised" sign check out and posted it as "simpler". But unfortunately
it does not matter how simply and quickly we get wrong answers.
Malcolm McLean <malcolm.arthur.mclean@gmail.com>: Sep 09 01:32AM -0700

On Friday, 9 September 2022 at 06:29:23 UTC+1, Juha Nieminen wrote:
> > exact same job as Bonitas code and probably more efficiently.
> If you need the absolute value of an integer, I don't think there's any
> way around having to use a conditional.
 
(int) sqrt(N*N);
Muttley@dastardlyhq.com: Sep 09 09:16AM

On Fri, 9 Sep 2022 00:20:33 -0700 (PDT)
 
>He just "optimised" sign check out and posted it as "simpler". But
>unfortunately
>it does not matter how simply and quickly we get wrong answers.
 
There's nothing clever or simple about writing code that has pointless
syntatic contructs and calls that don't need to be there. If you worked in
a team with other people you'd understand than when you have to debug/modify
someone elses code you don't want to have to wade through a load of ego
tripping show off code first.
"Öö Tiib" <ootiib@hot.ee>: Sep 09 02:39AM -0700

> >it does not matter how simply and quickly we get wrong answers.
 
> There's nothing clever or simple about writing code that has pointless
> syntatic contructs and calls that don't need to be there.
 
That is nothing I would dispute but irrelevant about your code snippet. Sign
check is not "syntatic contruct" (sic) but necessity in given algorithm.
 
> a team with other people you'd understand than when you have to debug/modify
> someone elses code you don't want to have to wade through a load of ego
> tripping show off code first.
 
Sign check is also not "ego tripping show off code" but necessity in given
algorithm.
Muttley@dastardlyhq.com: Sep 09 10:20AM

On Fri, 9 Sep 2022 02:39:33 -0700 (PDT)
>> tripping show off code first.
 
>Sign check is also not "ego tripping show off code" but necessity in given
>algorithm.
 
I never said my code was ready to run, I simply removed the pointless lambda
and boolean that didn't need to be there.
"Öö Tiib" <ootiib@hot.ee>: Sep 09 03:43AM -0700

> >algorithm.
 
> I never said my code was ready to run, I simply removed the pointless lambda
> and boolean that didn't need to be there.
 
Not true ... your major complaint was about sign check: "She's setting the bool
based on whether e >= 0 then setting eAbs to e or -e based on the bool. Why
not just do "eAbs = e"?"
 
That sign check is needed for the algorithm to work. Explain to your rubber
duck <https://en.wikipedia.org/wiki/Rubber_duck_debugging> that it is
unneeded and see yourself how you two reach conclusion that it is needed.
Muttley@dastardlyhq.com: Sep 09 10:56AM

On Fri, 9 Sep 2022 03:43:44 -0700 (PDT)
 
>That sign check is needed for the algorithm to work. Explain to your rubber
>duck <https://en.wikipedia.org/wiki/Rubber_duck_debugging> that it is
>unneeded and see yourself how you two reach conclusion that it is needed.
 
Clearly you're looking for an argument. I'm not interested, find another foil.
"Öö Tiib" <ootiib@hot.ee>: Sep 09 04:05AM -0700

> >duck <https://en.wikipedia.org/wiki/Rubber_duck_debugging> that it is
> >unneeded and see yourself how you two reach conclusion that it is needed.
 
> Clearly you're looking for an argument. I'm not interested, find another foil.
 
I am totally uninterested in argument ... I was just pointing out that you were
incorrect. You want to argue because you are incapable to admit that you
were incorrect. But describing that sign check as "syntatic contruct" and
"ego tripping show off code" just digs you deeper. Indeed, better stop.
Mr Flibble <flibble@reddwarf.jmc.corp>: Sep 09 02:56PM +0100

On Tue, 6 Sep 2022 19:08:32 +0200
> v *= sq;
> return e >= 0 ? v : 1.0 / v;
> }
 
You can improve it in two ways:
 
1) parameter e should be a double not an int
2) use logarithms and exponents
 
/Flibble
David Brown <david.brown@hesbynett.no>: Sep 09 04:48PM +0200

On 09/09/2022 10:32, Malcolm McLean wrote:
>> If you need the absolute value of an integer, I don't think there's any
>> way around having to use a conditional.
 
> (int) sqrt(N*N);
 
(((x >> 31) << 1) + 1) * x
 
That assumes 32-bit two's complement integers and common implementation
dependent behaviour of bit shifts. I haven't checked corner cases such
as INT_MIN.
 
I've no idea if it is faster or not. And of course, a smart enough
compiler can turn it into a conditional if it thinks that is faster.
Muttley@dastardlyhq.com: Sep 09 03:42PM

On Fri, 9 Sep 2022 04:05:46 -0700 (PDT)
 
>incorrect. You want to argue because you are incapable to admit that you
>were incorrect. But describing that sign check as "syntatic contruct" and
>"ego tripping show off code" just digs you deeper. Indeed, better stop.
 
Get together with Bonita and write something. I'm sure we could all do with
a good laugh.
Muttley@dastardlyhq.com: Sep 09 03:44PM

On Fri, 9 Sep 2022 14:56:32 +0100
 
>You can improve it in two ways:
 
>1) parameter e should be a double not an int
>2) use logarithms and exponents
 
Possibly. I wasn't commenting on the maths which I'm sure is correct. It was
all the surrounding unnecessary syntax I object to mainly because I regularly
see that sort of showboating in code and its irritating when you have to
debug it. Either the author is showing off or they can't think clearly, there
is no other option.
"Alf P. Steinbach" <alf.p.steinbach@gmail.com>: Sep 09 06:27PM +0200

On 9 Sep 2022 15:56, Mr Flibble wrote:
 
> You can improve it in two ways:
 
> 1) parameter e should be a double not an int
> 2) use logarithms and exponents
 
The point of Bonita's code is to compute an exact integer power when
that can be represented as a `double`, and anyway to do it in a real
fastish way, with O(log n) multiplications where n is the exponent.
 
`double` exponent value and computation via logarithm and exponentiation
would yield a generally inexact result, and I believe also in a slower
way, even when logarithm and exponentiation are hardware operations.
 
That belief is strengthened by Bonita's measurements that show distinct
improvement, more than twice as fast, compared to Microsoft's
implementation.
 
 
- Alf
Bonita Montero <Bonita.Montero@gmail.com>: Sep 09 06:28PM +0200

> see that sort of showboating in code and its irritating when you have to
> debug it. Either the author is showing off or they can't think clearly, there
> is no other option.
 
There's nothing wrong with my syntax - it's just a differnt style.
I used the templated lambda f.e. to prevent to have duplicate code
which I'd have to write to reduce two non-predictible branches to one.
If you've adapted this style the code becomes more readable than the
manual optimizations.
Bonita Montero <Bonita.Montero@gmail.com>: Sep 09 06:31PM +0200

Am 09.09.2022 um 18:27 schrieb Alf P. Steinbach:
 
> That belief is strengthened by Bonita's measurements that show distinct
> improvement, more than twice as fast, compared to Microsoft's
> implementation.
 
The implementations are likely to be nearly the same except from my
latest optimitation to prevent the repeated branch. This is because
the performance of my first implementation nearly matches the per-
formance of the Visual C++ runtime - this is unlikely to happen by
accident.
Bonita Montero <Bonita.Montero@gmail.com>: Sep 09 06:32PM +0200

>> "ego tripping show off code" just digs you deeper. Indeed, better stop.
 
> Get together with Bonita and write something. I'm sure we could all do with
> a good laugh.
 
There's nothing to laugh with my coding style. It's simply somewhat
different because I use a lot of functional programming, mainly to
reduce to size of the code.
Juha Nieminen <nospam@thanks.invalid>: Sep 09 04:54PM

> (((x >> 31) << 1) + 1) * x
 
That's clever.
 
> I've no idea if it is faster or not.
 
I suppose it depends on how many times the line of code is executed and
how often the CPU mispredicts the branch in the conditional version.
David Brown <david.brown@hesbynett.no>: Sep 09 07:17PM +0200

On 09/09/2022 18:54, Juha Nieminen wrote:
 
>> I've no idea if it is faster or not.
 
> I suppose it depends on how many times the line of code is executed and
> how often the CPU mispredicts the branch in the conditional version.
 
More obvious source code that has a conditional is usually compiled as a
conditional move, rather than a branch:
 
x < 0 ? -x : x
 
I don't think conditional moves are predicted in the same way. But the
efficiency is all going to depend on pipelining, superscaling,
vectorising (if possible), and so on. Modern x86 processors can do
register renaming and should allow code like mine to pass mix well with
other code, but I have no idea how well condition codes / flags and
conditional moves can be mixed with the flow of instructions.
Mr Flibble <flibble@reddwarf.jmc.corp>: Sep 09 06:39PM +0100

On Fri, 9 Sep 2022 18:27:48 +0200
 
> The point of Bonita's code is to compute an exact integer power when
> that can be represented as a `double`, and anyway to do it in a real
> fastish way, with O(log n) multiplications where n is the exponent.
 
std::pow taking an integer parameter was dropped in C++11: ask yourself
why.
 
> exponentiation would yield a generally inexact result, and I believe
> also in a slower way, even when logarithm and exponentiation are
> hardware operations.
 
Multiplication and logarithm/exponentiation are both equally inexact
when we are dealing with IEEE 754 floating point.
 
Bonita Montero <Bonita.Montero@gmail.com>: Sep 09 07:43PM +0200

Am 09.09.2022 um 19:39 schrieb Mr Flibble:
 
> std::pow taking an integer parameter was dropped in C++11: ask yourself
> why.
 
That's because the performance lagely is dependent on span of the
highest and lowest set bit of the exponent. This doesn't make much
difference if you have a integer or a floating point exponent for
the same value.
Bonita Montero <Bonita.Montero@gmail.com>: Sep 09 07:44PM +0200

Am 09.09.2022 um 19:17 schrieb David Brown:
> conditional move, rather than a branch:
 
>     x < 0 ? -x : x
 
> I don't think conditional moves are predicted in the same way. ...
 
Conditional moves don't affect the flow in the pipeline and
there are no mis-predictions.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: