Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can SWITCH be avoided by using BIT OPERATORS or anything else ?

Reply
Thread Tools

Can SWITCH be avoided by using BIT OPERATORS or anything else ?

 
 
mast4as
Guest
Posts: n/a
 
      04-20-2010
On Apr 20, 4:49*pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * mast4as:
>
>
>
> > On Apr 20, 4:06 pm, "Alf P. Steinbach" <al...@start.no> wrote:
> >> * mast4as:

>
> >>> Hi everyone
> >>> It seems that one day I saw some code where the developper managed to
> >>> use bit operations to avoid using a switch which I would like to avoid
> >>> for performance reasons. Here is what I am trying to do.
> >>> typedef float color[3];
> >>> typedef struct Test
> >>> {
> >>> * *float a;
> >>> * *float b;
> >>> * *color c;
> >>> * *float d;
> >>> };
> >>> typedef enum Type
> >>> {
> >>> * *TYPE_A *= 1 << 0,
> >>> * *TYPE_B *= 1 << 1,
> >>> * *TYPE_C *= 1 << 2,
> >>> * *TYPE_D *= 1 << 3,
> >>> };
> >>> int main( int argc, char **argv )
> >>> {
> >>> * *Type type = TYPE_B;
> >>> * *Test test;
> >>> * *memset( &test.a, 0x0, sizeof( Test ) );
> >>> * *switch ( type )
> >>> * *{
> >>> * * * * * *case TYPE_A: test.a += 1; * * * * * * * break;
> >>> * * * * * *case TYPE_B: test.b -= 1; * * * * * * * break;
> >>> * * * * * *case TYPE_C: test.c[ 0 ] = 1; * break;
> >>> * * * * * *case TYPE_D: test.d += 2; * * * * * * * break;
> >>> * * * * * *defaullt: ;
> >>> * *}
> >>> }
> >>> Basically I would like to avoid the switch statetement. What I am
> >>> trying to do in the code is add the result to the appropriate element
> >>> of the variable "test" of type Test based on the type of the variable
> >>> "type". Do you guys see a super efficient way of doing this
> >> Reorder your struct so that the values you're interested in are consecutive.

>
> >> Represent them as an array, then use indexing.

>
> >> That said, this smells very much like a kind of Evil(TM) premature optimization.
> >> The cost of catering to this micro-efficiency can be quite high. Conversely,
> >> much work can be saved by disregardning this level of efficiency completely,
> >> which is what people do when they program in languages like C#, Java or Python.

>
> >> Cheers & hth.

>
> >> - Alf
> >> '

>
> > Thanks a lot Alf... However saddly I don't understand what you
> > mean Could you please show me an example. I am happy to fix my
> > code but I just can't make sense of what you are saying...

>
> > You are right about the optimization but I have to do this switch
> > millions of time per process (and this program is runned thousands and
> > thousands of time which is the reason I want to be sure this is
> > fast ...

>
> Oh well.
>
> Off the cuff:
>
> * *class Whatever
> * *{
> * *private:
> * * * *float myItems[6];
> * *public:
> * * * *enum Kind{ kind_a, kind_b, kind_d, kind_c };
> * * * *Whatever(): myItems() {} *// Zero the items
> * * * *float& a() { return myItems[0]; }
> * * * *float& b() { return myItems[1]; }
> * * * *float& d() { return myItems[2]; }
> * * * *float& c() { return myItems[3]; }
> * * * *float* c_arr() { return myItems + 3; }
> * * * *float& operator[]( Kind const i ) { return myItems[i]; }
> * *};
>
> * *int main()
> * *{
> * * * *static const delta[] = { 1, 1, 2, 1 }
> * * * *Whatever test;
> * * * *Whatever::Kind const i = Whatever::kind_c;
> * * * *test[i] += delta[i];
> * *}
>
> Cheers & hth.,
>
> - Alf


I see very nice... interesting. Thank you so much.
 
Reply With Quote
 
 
 
 
Keith H Duggar
Guest
Posts: n/a
 
      04-20-2010
On Apr 20, 12:17 pm, "Leigh Johnston" <le...@i42.co.uk> wrote:
> "Keith H Duggar" wrote:
> > On Apr 20, 11:55 am, "Leigh Johnston" <le...@i42.co.uk> wrote:
> >> Keith H Duggar wrote:
> >> > I think you will be very interested in this site:

>
> >> > http://graphics.stanford.edu/~seander/bithacks.html

>
> >> > However, please use with restraint and caution.

>
> >> Interestingly that site of yours has the following suggestion:

>
> >> Compute the integer absolute value (abs) without branching
> >> int v; // we want to find the absolute value of v
> >> unsigned int r; // the result goes here
> >> int const mask = v >> sizeof(int) * CHAR_BIT - 1;

>
> >> Gosh, using an "unsigned int" to store an integer absolute value? Who
> >> would
> >> have thought it.

>
> > Geez I didn't know ... oh wait! I actually /contributed/ to
> > that section:

>
> > "On March 14, 2004, Keith H. Duggar sent me the patented
> > variation above; it is superior to the one I initially came
> > up with, r=(+1|(v>>(sizeof(int)*CHAR_BIT-1)))*v, because a
> > multiply is not used."

>
> > PWNED ... again.

>
> > FYI there is also an /intentional/ right shift of a signed int.
> > I bet you think that is "wrong" just like Alf's vector reserve
> > hack.

>
> > Also, int r gives the exact same results and that is what I use
> > in my personal implementation of that hack. I must be a "use int
> > everywhere" troll ... who expertly uses signed AND unsigned.

>
> > KHD

>
> Typical troll, saying someone is pwned when it is they who are in fact the
> pwned. Yes I saw your name there before I posted


LMAO ... pathetic liar. We have AMPLE evidence that your ADHD and
proclivity for selection bias would have prevented you from reading
ANYTHING past "unsigned int r" before IMMEDIATELY regurgitating a
post.

You fail.

KHD
 
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
Is this bad? (How can this be avoided?) mike3 C++ 9 11-30-2011 08:36 PM
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
any particular things which need to be avoided? jacko VHDL 8 01-12-2007 08:52 PM
Parental Control proggy to be avoided. TheNIGHTCRAWLER Computer Support 11 08-01-2005 01:50 PM
Why should use of instanceof be avoided HalcyonWild Java 30 05-27-2005 11:47 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