Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)

 Alex Vinokur 02-18-2005 07:51 AM

Fast addition for n+1 or n+0

Consider the following statement:
n+i, where i = 1 or 0.

Is there more fast method for computing n+i than direct computing that sum?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

 Keith Thompson 02-18-2005 07:57 AM

Re: Fast addition for n+1 or n+0

"Alex Vinokur" <alexvn@big-foot.com> writes:
> Consider the following statement:
> n+i, where i = 1 or 0.
>
> Is there more fast method for computing n+i than direct computing that sum?

The best way to compute n+0 is n.

The best way to compute n+1 is n+1; if the CPU provides something
faster than a general add instruction, the compiler will generate it
for you.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

 Alf P. Steinbach 02-18-2005 07:58 AM

Re: Fast addition for n+1 or n+0

* Alex Vinokur:
> Consider the following statement:
> n+i, where i = 1 or 0.
>
> Is there more fast method for computing n+i than direct computing that sum?

That depends on the types involved.

For built-in numeric types, direct computation is probably fastest.

Measure if you're in doubt (and it really matters).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

 Gregory Toomey 02-18-2005 08:49 AM

Re: Fast addition for n+1 or n+0

Alex Vinokur wrote:

> Consider the following statement:
> n+i, where i = 1 or 0.
>
> Is there more fast method for computing n+i than direct computing that
> sum?
>

Assuming integers, hardware addition is implemented simply using full

n+0 has no carries is is fast; many compliers will constant fold to n
n+1 has potentially m carries in m-bit arithmetic

http://isweb.redwoods.cc.ca.us/INSTR...logic/full.htm

gtoomey
www.gregorytoomey.com

 Richard Tobin 02-18-2005 03:38 PM

Re: Fast addition for n+1 or n+0

In article <37ll5mF5es672U1@individual.net>,
Alex Vinokur <alexvn@big-foot.com> wrote:

>Consider the following statement:
>n+i, where i = 1 or 0.
>
>Is there more fast method for computing n+i than direct computing that sum?

Assuming n and i are ints, not on a modern general purpose computer.
Addition typically takes one cycle, once the operands are in
registers.

Any attempt to use a conditional will almost certainly be much slower.

For more details, try a newsgroup for the processor you're interested
in, or maybe comp.arch.

-- Richard

 Alex Vinokur 02-18-2005 03:59 PM

Re: Fast addition for n+1 or n+0

"Richard Tobin" <richard@cogsci.ed.ac.uk> wrote in message news:cv525n\$1i7f\$1@pc-news.cogsci.ed.ac.uk...
> In article <37ll5mF5es672U1@individual.net>,
> Alex Vinokur <alexvn@big-foot.com> wrote:
>
> >Consider the following statement:
> >n+i, where i = 1 or 0.
> >
> >Is there more fast method for computing n+i than direct computing that sum?

>
> Assuming n and i are ints, not on a modern general purpose computer.
> Addition typically takes one cycle, once the operands are in
> registers.
>
> Any attempt to use a conditional will almost certainly be much slower.
>
> For more details, try a newsgroup for the processor you're interested
> in, or maybe comp.arch.
>
> -- Richard

I need that in C/C++ program.

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

 Michael Mair 02-18-2005 04:15 PM

Re: Fast addition for n+1 or n+0

Alex Vinokur wrote:
> "Richard Tobin" <richard@cogsci.ed.ac.uk> wrote in message news:cv525n\$1i7f\$1@pc-news.cogsci.ed.ac.uk...
>
>>In article <37ll5mF5es672U1@individual.net>,
>>Alex Vinokur <alexvn@big-foot.com> wrote:
>>
>>
>>>Consider the following statement:
>>>n+i, where i = 1 or 0.
>>>
>>>Is there more fast method for computing n+i than direct computing that sum?

>>
>>Assuming n and i are ints, not on a modern general purpose computer.
>>Addition typically takes one cycle, once the operands are in
>>registers.
>>
>>Any attempt to use a conditional will almost certainly be much slower.
>>
>>For more details, try a newsgroup for the processor you're interested
>>in, or maybe comp.arch.

>
> I need that in C/C++ program.

Well, there is no general truth helping you along to a portable,
always perfect solution.
If you want to optimise your code for speed, use a profiler to
determine which functions are called how often and take how much
time. Then you know _where_ you lose your time.
After that, try to find algorithms which reduce the number
of calls to small functions which take a good part of the overall
time and reduces the time spent in "big" functions taking much time.
If you afterwards really find that optimising code with
'n+0' and 'n+1' would be the best possible micro-optimisation
to gain some more cycles, then you should try to write as many
'n+0's/'n's and 'n+1's as possible explicitly in your code
instead of using 'n+i'. The compiler will optimise that if the
code has the potential for optimisation.
Afterwards, use the profiler to determine whether this actually
makes a difference.

Probably not much.
If you think you can do better than the compiler, then follow

Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

 Walter Roberson 02-18-2005 04:45 PM

Re: Fast addition for n+1 or n+0

In article <37ll5mF5es672U1@individual.net>,
Alex Vinokur <alexvn@big-foot.com> wrote:
:Consider the following statement:
:n+i, where i = 1 or 0.

:Is there more fast method for computing n+i than direct computing that sum?

It depends on the costs you assign to the various operations -- a
matter which is architecture dependant. Integer addition is usually one of
the fastest things a computer does. Suppose you were able to find a
two instruction sequence that was faster for that particular case: then
it is very likely to be slower because internally the CPU has
to perform an integer addition in order to find the address of the
second instruction.

Have you perhaps omitted some important facts about the circumstances?
For example, are you microprogramming, or is this a theory question
at the micro-level where each comparison and change of a bit in
the implimentation of the 'addition' operation is to be counted?
Is this an assignment in designing an IC which is faster for these
particular cases than building a full-blown adder circuit would be?

--
Reviewers should be required to produce a certain number of
negative reviews - like police given quotas for handing out
speeding tickets. -- The Audio Anarchist

 E. Robert Tisdale 02-18-2005 04:56 PM

Re: Fast addition for n+1 or n+0

Alex Vinokur wrote:

> Consider the following statement:
>
> n + i, where i = 1 or 0.
>
> Is there more fast method for computing n + i than direct computing that sum?

No.
But a good optimizing compiler should be able to
replace n + 0 with n and replace n + 1 with ++n.

 Alex Vinokur 02-18-2005 05:54 PM

Re: Fast addition for n+1 or n+0

"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message news:cv563d\$d73\$1@canopus.cc.umanitoba.ca...
> In article <37ll5mF5es672U1@individual.net>,
> Alex Vinokur <alexvn@big-foot.com> wrote:
> :Consider the following statement:
> :n+i, where i = 1 or 0.
>
> :Is there more fast method for computing n+i than direct computing that sum?
>
> It depends on the costs you assign to the various operations -- a
> matter which is architecture dependant. Integer addition is usually one of
> the fastest things a computer does. Suppose you were able to find a
> two instruction sequence that was faster for that particular case: then
> it is very likely to be slower because internally the CPU has
> to perform an integer addition in order to find the address of the
> second instruction.
>
> Have you perhaps omitted some important facts about the circumstances?
> For example, are you microprogramming, or is this a theory question
> at the micro-level where each comparison and change of a bit in
> the implimentation of the 'addition' operation is to be counted?
> Is this an assignment in designing an IC which is faster for these
> particular cases than building a full-blown adder circuit would be?
>

I would like to optimize (speed) an algorithm for computing very large Fibonacci numbers using the primary recursive formula.
The algorithm can be seen at

n1 += (n2 + carry_s); // carry_s == 0 or 1

The question is if is it possible to make that line to work faster?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

All times are GMT. The time now is 05:38 PM.