Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Divide by zero.

Reply
Thread Tools

Divide by zero.

 
 
shuisheng
Guest
Posts: n/a
 
      09-25-2006
Dear all,

Assume I have two big arrays A and B. And I want to element-wise divide
A by B. When an element of B is zero, the results are also zero.

Such as A = { 2, 4, 0, 6}
B = { 1, 0, 0, 2}
-----------------------------------------
R = { 2, 0, 0, 3}

Any fast way to avoid the slow 'if' operation.

Thanks,

Shuisheng

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-25-2006
shuisheng wrote:
> Assume I have two big arrays A and B. And I want to element-wise
> divide A by B. When an element of B is zero, the results are also
> zero.
>
> Such as A = { 2, 4, 0, 6}
> B = { 1, 0, 0, 2}
> -----------------------------------------
> R = { 2, 0, 0, 3}
>
> Any fast way to avoid the slow 'if' operation.


You like to avoid things, don't you?

How slow is it? If you take two arrays such that B doesn't have
any zeros in it, how much does 'if' slow you down? Is it really
so significant? AFAIUI, you cannot avoid some kind of checking
against 0 to prevent division by 0, right? You could try playing
tricks with arithmetic, but it is most likely just as expensive
as comparing B[i] with 0.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      09-25-2006
shuisheng posted:

> Dear all,
>
> Assume I have two big arrays A and B. And I want to element-wise divide
> A by B. When an element of B is zero, the results are also zero.
>
> Such as A = { 2, 4, 0, 6}
> B = { 1, 0, 0, 2}
> -----------------------------------------
> R = { 2, 0, 0, 3}
>
> Any fast way to avoid the slow 'if' operation.



The fastest code I can think of at the moment is the following (but
ofcourse, it tests a conditional):

#include <cstddef>
#include <cassert>

void Divide(int *pA, int const *pB, std::size_t const len)
{
assert(pA); assert(pB); assert(len);

int register temp;

int const *const poverA = pA + len;

do (temp = *pB++) ? (*pA++ /= temp) : (*pA++ = 0);
while(poverA!=pA);
}

Or maybe if you're working with a machine which has a single instruction
which takes a pointer plus an offset:

void Divide(int *const pA, int const *const pB, std::size_t const len)
{
assert(pA); assert(pB); assert(len);

int register temp;

std::size_t register i = 0;

do (temp = pB[i]) ? (pA[i] /= temp) : (pA[i] = 0);
while(++i!= len);
}

--

Frederick Gotham
 
Reply With Quote
 
shuisheng
Guest
Posts: n/a
 
      09-25-2006

Victor Bazarov 写道:

> How slow is it? If you take two arrays such that B doesn't have
> any zeros in it, how much does 'if' slow you down? Is it really
> so significant? AFAIUI, you cannot avoid some kind of checking
> against 0 to prevent division by 0, right? You could try playing
> tricks with arithmetic, but it is most likely just as expensive
> as comparing B[i] with 0.


The difference is very small: sevral percents and some times even the
same. Thanks!!!

Shuisheng

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-25-2006
shuisheng wrote:
> Victor Bazarov ??:
>
>> How slow is it? If you take two arrays such that B doesn't have
>> any zeros in it, how much does 'if' slow you down? Is it really
>> so significant? AFAIUI, you cannot avoid some kind of checking
>> against 0 to prevent division by 0, right? You could try playing
>> tricks with arithmetic, but it is most likely just as expensive
>> as comparing B[i] with 0.

>
> The difference is very small: sevral percents and some times even the
> same. Thanks!!!


Hey, don't mention it!

OK, how much of your application does division of the two arrays take?
If it's about 10 percent total (from start to finish), then if you
shave off another 10 percent of that by removing (magically) the 'if',
what is the final improvement for the application? 1 percent at best?
Is it worth the time you spend trying to find the magic to remove the
pesky 'if'?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
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
Re: code help and std_logic divide climberjc VHDL 0 12-08-2005 08:36 PM
Divide by 2 counter Ash12 VHDL 2 02-28-2005 06:00 AM
Divide by n Mushmech VHDL 2 04-12-2004 11:54 AM
Unsupported error,& Right operand of "Divide" operator must be a power of 2.. Kim JM VHDL 1 04-02-2004 06:58 PM
std_logic_vector divide Ciar?n Hughes VHDL 2 10-04-2003 01:34 PM



Advertisments