Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: integer divide by 7 trick?

Reply
Thread Tools

Re: integer divide by 7 trick?

 
 
MG
Guest
Posts: n/a
 
      07-20-2003
how about trying this in the first approach:

(val<<5 - val)>>6

this shd give a better apprimate than first right shift and subtracting....

"krazyman" <> wrote in message
news: om...
> Does anyone know a good trick to perform an integer divide by 7
> (the equivalent of x/7 in C) without using a divide? I'm familiar
> with the multiply by 7 trick (x<<3-x), and I was asked this question
> with the implication that something similar exists for divide-by-7. I
> came up with these 2 ways below, but the fast one is inaccurate and
> the correct one probably takes too many cpu cycles to be an
> optimization over the straight integer divide instruction.
>
> ------------------------------------
> #include <stdio.h>
>
> unsigned int ApproximateDiv7(unsigned int val) {
> // 1/7 = 1/8 + 1/56 =~ 1/8 + 1/64,
> // and 1/64 is much easier to compute than 1/56.
> // still, the returned value will be off by as
> // much as 1/56-1/64 =~ .2%, or more for small values
> // due to truncation
>
> return (val >> 3) + (val >> 5);
> }
>
> // do integer divide by 7 without using division
> unsigned int Div7(unsigned int val) {
> // this works in O(log2(val)) time
>
> unsigned int seventimes2pow[32];
> unsigned int result=0;
> int i;
>
> for(i=0;i<32;i++) {
> seventimes2pow[i] = 7<<i;
> if(seventimes2pow[i] > val)
> break;
> }
>
> i--;
> while(i>=0) {
> if(seventimes2pow[i] <= val) {
> val -= seventimes2pow[i];
> result += 1<<i;
> }
> i--;
> }
>
> return result;
> }



 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      07-21-2003
Just out of curiosity, what's wrong with "i / 7"?
 
Reply With Quote
 
 
 
 
Stewart Gordon
Guest
Posts: n/a
 
      07-21-2003
On 21/7/03 4:35 pm (UK time), Roy Smith let loose these words:

> Just out of curiosity, what's wrong with "i / 7"?


In case you hadn't noticed, the OP is looking for a method that is
faster than the hardware divide function or whatever the compiler
generates for that code.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      07-21-2003
On Mon, 21 Jul 2003 18:49:19 +0100, in comp.lang.c , Stewart Gordon
<> wrote:

>On 21/7/03 4:35 pm (UK time), Roy Smith let loose these words:
>
>> Just out of curiosity, what's wrong with "i / 7"?

>
>In case you hadn't noticed, the OP is looking for a method that is
>faster than the hardware divide function or whatever the compiler
>generates for that code.


He's very unlikely to find one, any well written compiler ought to be
aware of any integer divide tricks.

IMHO its more likely a homework question. I have to say, submitting
such to CLC is likely to get one an F--, because there's almost no
chance of understanding the responses, and parroting them back at your
teacher will make it VERY obvious how you found the "answer"....



--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
MG
Guest
Posts: n/a
 
      07-22-2003

"Stewart Gordon" <> wrote in message
news:bfh0t5$1k1$...
> MG wrote:
>
> > how about trying this in the first approach:
> >
> > (val<<5 - val)>>6
> >
> > this shd give a better apprimate than first right shift and

subtracting....
> <snip>
>
> I presume you've tried that formula for something other than the case

val=0?
>

Frankly speaking...i havent tried it..but it shd work better than the
original alogorithm declared by Krazyman..



 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      07-22-2003
In article <pv5Ta.56$>,
"MG" <> wrote:

> "Stewart Gordon" <> wrote in message
> news:bfh0t5$1k1$...
> > MG wrote:
> >
> > > how about trying this in the first approach:
> > >
> > > (val<<5 - val)>>6
> > >
> > > this shd give a better apprimate than first right shift and

> subtracting....
> > <snip>
> >
> > I presume you've tried that formula for something other than the case

> val=0?
> >

> Frankly speaking...i havent tried it..but it shd work better than the
> original alogorithm declared by Krazyman..


I would first change it to

((val << 5) - val) >> 6

because I don't want to look up priorities of << and -, and (val << (5 -
val)) >> 6 would be nonsense.

And this expression calculates val * 31 / 64 which doesn't look like it
is anywhere near val / 7.
 
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
NEED HELP: multiply and divide with integer in VHDL ledinhkha@gmail.com VHDL 2 12-15-2005 08:06 AM
Re: integer divide by 7 trick? yvan joffre C++ 3 07-25-2003 03:03 PM
Re: integer divide by 7 trick? Robert W Hand C++ 12 07-25-2003 03:19 AM
Re: integer divide by 7 trick? JS C++ 1 07-22-2003 09:05 AM
Re: integer divide by 7 trick? MG C Programming 5 07-22-2003 07:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57