Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Fast addition for n+1 or n+0

Reply
Thread Tools

Fast addition for n+1 or n+0

 
 
Alex Vinokur
Guest
Posts: n/a
 
      02-18-2005
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




 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      02-18-2005
"Alex Vinokur" <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-18-2005
* 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?
 
Reply With Quote
 
Gregory Toomey
Guest
Posts: n/a
 
      02-18-2005
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
adders, or faster algorithms like carry lookahead.

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

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

Carry look ahead:
http://www.seas.upenn.edu/~ee201/lab...kAheadF01.html


gtoomey
www.gregorytoomey.com
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      02-18-2005
In article <(E-Mail Removed)>,
Alex Vinokur <(E-Mail Removed)> 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
 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      02-18-2005

"Richard Tobin" <(E-Mail Removed)> wrote in message news:cv525n$1i7f$(E-Mail Removed)...
> In article <(E-Mail Removed)>,
> Alex Vinokur <(E-Mail Removed)> 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



 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      02-18-2005


Alex Vinokur wrote:
> "Richard Tobin" <(E-Mail Removed)> wrote in message news:cv525n$1i7f$(E-Mail Removed)...
>
>>In article <(E-Mail Removed)>,
>>Alex Vinokur <(E-Mail Removed)> 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
Richard's suggestion about comp.arch.*


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

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      02-18-2005
In article <(E-Mail Removed)>,
Alex Vinokur <(E-Mail Removed)> 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
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      02-18-2005
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.
 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      02-18-2005
"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> wrote in message news:cv563d$d73$(E-Mail Removed)...
> In article <(E-Mail Removed)>,
> Alex Vinokur <(E-Mail Removed)> 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
http://groups-beta.google.com/group/...e76b12150613a1

Function AddUnits() contains a line
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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fast addition for n+1 or n+0 Alex Vinokur C Programming 23 02-22-2005 01:01 AM
More memory: How fast is fast rfdjr1@optonline.net Computer Support 5 05-19-2004 05:45 PM
Canon S30 Fast shutter mode... Why so fast? mark popp Digital Photography 1 02-08-2004 10:07 PM
I NEED HELP FAST!!!!! REAL FAST!!!!! R. Jizzle MCSE 3 09-29-2003 08:51 PM
Super-fast AA Chargers: Anything as fast as the 15 minute Rayovac? David Chien Digital Photography 4 08-30-2003 07:49 PM



Advertisments