473,471 Members | 4,650 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Optimize this please?

typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
dst[1] |= src[1] >2;
dst[2] |= src[1] << 6;
dst[2] |= src[2] >3;
dst[3] |= src[2] << 5;
dst[3] |= src[3] >4;
dst[4] |= src[3] << 4;
dst[4] |= src[4] >5;
dst[5] |= src[4] << 3;
dst[5] |= src[5] >6;
dst[6] |= src[5] << 2;
dst[6] |= src[6] >7;
dst[7] |= src[6] << 1;
dst[7] |= src[7] >8;
dst[8] |= src[7];
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}

I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can get
it and we can compare notes.

--
To send me email, put "sheltie" in the subject.
Nov 29 '06 #1
13 1382
Daniel T. wrote:
typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
dst[1] |= src[1] >2;
dst[2] |= src[1] << 6;
dst[2] |= src[2] >3;
dst[3] |= src[2] << 5;
dst[3] |= src[3] >4;
dst[4] |= src[3] << 4;
dst[4] |= src[4] >5;
dst[5] |= src[4] << 3;
dst[5] |= src[5] >6;
dst[6] |= src[5] << 2;
dst[6] |= src[6] >7;
dst[7] |= src[6] << 1;
dst[7] |= src[7] >8;
dst[8] |= src[7];
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}

I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can get
it and we can compare notes.
Replace all but the last semicolon in the body of this function
with *commas*. Here, I saved you 19 semicolons (if I can count).
Now, where are _your_ notes to compare?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 29 '06 #2
Daniel T. wrote:
typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
>
I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can get
it and we can compare notes.
Why? Did someone put a quota on semicolons?
You can replace every semicolon except for the last with a comma
if you like. If there is some goal here other than reducing the
number of semicolons, then you'll have to tell us what you really
want to do before we can make an intelligent answer.

Nov 29 '06 #3
Ron Natalie <ro*@spamcop.netwrote:
Daniel T. wrote:
typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;

I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can get
it and we can compare notes.

Why? Did someone put a quota on semicolons?
You can replace every semicolon except for the last with a comma
if you like. If there is some goal here other than reducing the
number of semicolons, then you'll have to tell us what you really
want to do before we can make an intelligent answer.
I'm looking for the loop. I can almost see one in the code but I can't
quite find it.

--
To send me email, put "sheltie" in the subject.
Nov 29 '06 #4
Daniel T. wrote:
Ron Natalie <ro*@spamcop.netwrote:
>Daniel T. wrote:
>>typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
>>>
I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can
get it and we can compare notes.

Why? Did someone put a quota on semicolons?
You can replace every semicolon except for the last with a comma
if you like. If there is some goal here other than reducing the
number of semicolons, then you'll have to tell us what you really
want to do before we can make an intelligent answer.

I'm looking for the loop.
Don't. Unrolled code is almost always faster than any loops.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 29 '06 #5

"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
Ron Natalie <ro*@spamcop.netwrote:
>Daniel T. wrote:
typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
>
I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can get
it and we can compare notes.

Why? Did someone put a quota on semicolons?
You can replace every semicolon except for the last with a comma
if you like. If there is some goal here other than reducing the
number of semicolons, then you'll have to tell us what you really
want to do before we can make an intelligent answer.

I'm looking for the loop. I can almost see one in the code but I can't
quite find it.
Why do you feel that a loop will be any more
efficient than what you already have? All that
I think it might do is reduce the length of your
source; but is that really so important?

-Mike
Nov 29 '06 #6
In article <da****************************@news.west.earthlin k.net>,
"Daniel T." <da******@earthlink.netwrote:
typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
dst[1] |= src[1] >2;
dst[2] |= src[1] << 6;
dst[2] |= src[2] >3;
dst[3] |= src[2] << 5;
dst[3] |= src[3] >4;
dst[4] |= src[3] << 4;
dst[4] |= src[4] >5;
dst[5] |= src[4] << 3;
dst[5] |= src[5] >6;
dst[6] |= src[5] << 2;
dst[6] |= src[6] >7;
dst[7] |= src[6] << 1;
dst[7] |= src[7] >8;
dst[8] |= src[7];
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}

I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can get
it and we can compare notes.
Here is what I have so far:

void packBits( u8* dst, const u16* src )
{
for ( unsigned i = 0; i < 8; ++i )
{
dst[i] |= src[i] >(i + 1);
dst[i + 1] |= src[i] << (7 - i);
}
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}

--
To send me email, put "sheltie" in the subject.
Nov 29 '06 #7
Daniel T. wrote:
In article <da****************************@news.west.earthlin k.net>,
"Daniel T." <da******@earthlink.netwrote:
>typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
dst[1] |= src[1] >2;
dst[2] |= src[1] << 6;
dst[2] |= src[2] >3;
dst[3] |= src[2] << 5;
dst[3] |= src[3] >4;
dst[4] |= src[3] << 4;
dst[4] |= src[4] >5;
dst[5] |= src[4] << 3;
dst[5] |= src[5] >6;
dst[6] |= src[5] << 2;
dst[6] |= src[6] >7;
dst[7] |= src[6] << 1;
dst[7] |= src[7] >8;
dst[8] |= src[7];
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}

I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can
get it and we can compare notes.

Here is what I have so far:

void packBits( u8* dst, const u16* src )
{
for ( unsigned i = 0; i < 8; ++i )
{
dst[i] |= src[i] >(i + 1);
dst[i + 1] |= src[i] << (7 - i);
}
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}
Why isn't it good enough? You could make the shift values also part
of the loop, thus making it

for (unsigned i = 0, sl = 1, sr = 7; i < 8; ++i, ++sl, --sr)
{
dst[i] |= src[i] >sr;
dst[i+1] |= src[i] << sl;
}
...

But is it better? What is it you're trying to accomplish? Speed?
Readability? Maintainability?

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

Daniel T. wrote:
In article <da****************************@news.west.earthlin k.net>,
"Daniel T." <da******@earthlink.netwrote:
typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
dst[1] |= src[1] >2;
dst[2] |= src[1] << 6;
dst[2] |= src[2] >3;
dst[3] |= src[2] << 5;
dst[3] |= src[3] >4;
dst[4] |= src[3] << 4;
dst[4] |= src[4] >5;
dst[5] |= src[4] << 3;
dst[5] |= src[5] >6;
dst[6] |= src[5] << 2;
dst[6] |= src[6] >7;
dst[7] |= src[6] << 1;
dst[7] |= src[7] >8;
dst[8] |= src[7];
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}

I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can get
it and we can compare notes.

Here is what I have so far:

void packBits( u8* dst, const u16* src )
{
for ( unsigned i = 0; i < 8; ++i )
{
dst[i] |= src[i] >(i + 1);
dst[i + 1] |= src[i] << (7 - i);
}
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}
To unroll the loop use a recursive functor with specialistaions for
those that dont fit:
(Not tested) e.g:
template <int N>
struct functor;

// end condition
template <>
struct functor<0>{
void operator()( u8* dst, const u16* src)
{
dst[0] |= src[i] >( 1);
dst[1] |= src[0] << (7 );
}
};
// main case
template <int I>
struct functor{
void operator()( u8* dst, const u16* src)
{
functor<I-1f_prev;
f_prev(dst,src); // call the rest
dst[i] |= src[i] >(I + 1);
dst[I + 1] |= src[i] << (7 - I);
}
};
// required explicit specialisations for functors<9to functor<11>
not shown

int main()
{
functor<11f; // useage
f (dst, src);
}

Nov 29 '06 #9
kwikius wrote:
[..]
template <int N>
struct functor;

// end condition
template <>
struct functor<0>{
void operator()( u8* dst, const u16* src)
{
dst[0] |= src[i] >( 1);
^^^^^^
I believe 'src[0]' is meant here.
dst[1] |= src[0] << (7 );
}
};
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 29 '06 #10
* Daniel T.:
typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
dst[1] |= src[1] >2;
dst[2] |= src[1] << 6;
dst[2] |= src[2] >3;
dst[3] |= src[2] << 5;
dst[3] |= src[3] >4;
dst[4] |= src[3] << 4;
dst[4] |= src[4] >5;
dst[5] |= src[4] << 3;
dst[5] |= src[5] >6;
dst[6] |= src[5] << 2;
dst[6] |= src[6] >7;
dst[7] |= src[6] << 1;
dst[7] |= src[7] >8;
dst[8] |= src[7];
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}

I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can get
it and we can compare notes.
I'd use a single bitblt-call, using the system's hardware, if efficiency
is a great concern (src seems to be a 9x9 monochrome bitmap).

Single most important technical lesson learned from early Smalltalk: we
need hardware supported bitblt.

Conspiciously lacking in C++ standard library: bitblt.

--
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?
Nov 29 '06 #11

Victor Bazarov wrote:
kwikius wrote:
[..]
template <int N>
struct functor;

// end condition
template <>
struct functor<0>{
void operator()( u8* dst, const u16* src)
{
dst[0] |= src[i] >( 1);
^^^^^^
I believe 'src[0]' is meant here.
Sure. make the functions const and probably the args * const too AFAICS
for good style.
I'm sure VC8 would make a nice job optimising it and probably gcc too.

Let me know if it works. ;-)
regards
Andy Little

Nov 29 '06 #12
* Alf P. Steinbach:
* Daniel T.:
>typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
dst[1] |= src[1] >2;
dst[2] |= src[1] << 6;
dst[2] |= src[2] >3;
dst[3] |= src[2] << 5;
dst[3] |= src[3] >4;
dst[4] |= src[3] << 4;
dst[4] |= src[4] >5;
dst[5] |= src[4] << 3;
dst[5] |= src[5] >6;
dst[6] |= src[5] << 2;
dst[6] |= src[6] >7;
dst[7] |= src[6] << 1;
dst[7] |= src[7] >8;
dst[8] |= src[7];
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}

I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can get
it and we can compare notes.

I'd use a single bitblt-call, using the system's hardware, if efficiency
is a great concern (src seems to be a 9x9 monochrome bitmap).

Single most important technical lesson learned from early Smalltalk: we
need hardware supported bitblt.

Conspiciously lacking in C++ standard library: bitblt.
Except -- just after hitting "Send" it occurred to me that the usual
bitblt doesn't solve this particular packing problem, probably.

Well.

Sorry.

--
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?
Nov 29 '06 #13

Alf P. Steinbach wrote:
* Daniel T.:
typedef unsigned short u16; // may be different on your machine
typedef unsigned char u8; // may be different on your machine

// assume dst is zero initialized upon entry
void packBits( u8* dst, const u16* src )
{
dst[0] |= src[0] >1;
dst[1] |= src[0] << 7;
dst[1] |= src[1] >2;
dst[2] |= src[1] << 6;
dst[2] |= src[2] >3;
dst[3] |= src[2] << 5;
dst[3] |= src[3] >4;
dst[4] |= src[3] << 4;
dst[4] |= src[4] >5;
dst[5] |= src[4] << 3;
dst[5] |= src[5] >6;
dst[6] |= src[5] << 2;
dst[6] |= src[6] >7;
dst[7] |= src[6] << 1;
dst[7] |= src[7] >8;
dst[8] |= src[7];
dst[9] |= src[8] >1;
dst[10] |= src[8] << 7;
dst[10] |= src[9] >2;
dst[11] |= src[9] << 6;
}

I want to duplicate the above function using as few semicolons as
possible. If you want to take up the puzzle, see how small you can get
it and we can compare notes.

I'd use a single bitblt-call, using the system's hardware, if efficiency
is a great concern (src seems to be a 9x9 monochrome bitmap).
If its a constant bitmap, as all the above integers are constants then
I reckon you could in fact do the whole thing at compile time using
Fusion

http://spirit.sourceforge.net/dl_mor...tml/index.html

regards
Andy Little

Nov 29 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for...
6
by: Bruce D | last post by:
Could someone please help to explain why the following query isn't using the index... explain select id from kbm where state = 'MA' table type possible_keys key key_len ref rows Extra...
0
by: Daniel | last post by:
Hi there, I recently came across an interesting option when right clicking on a project (Right click on the project -> properties -> Configuration Properties ->Build -> Optimize) There is an...
3
by: Gaffar | last post by:
Hello, I am Handling a project in ( ASP.NET with C#.NET) in which a module is slow and inefficient. How to optimize the code. Please give me some suggestions regarding this. If you know any...
4
by: Huaer.XC | last post by:
>From the following MySQL command: EXPLAIN SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id) JOIN t3 ON t3.name = t1.name WHERE t1.id IN(123, 124); which result is:...
15
by: kenneth | last post by:
I was trying to use multiple thread to optimize my following code, but met some problems, anyone can help me? k are initialized. int computePot() { int i, j; for( i=0; i<500; i++ ) { for(...
9
by: TF | last post by:
Hello all, I made a ASP.NET 2.0 site that shows possible "recipes" for paint colors stored in an access dbase. Basically, 1000 colors are stored with specific RGB values in separate columns. A...
3
by: Matthias Pospiech | last post by:
In FFT one usually needs to shift/cycle the values in an array. Typically the entries are shifted in the way: 012 345 to 345 012 The following code does this, but I wonder if there are...
2
by: pavanip | last post by:
Hi, I have an application like Optimize System Performance by using Memory speed, cpu speed and Disk speed. How to optimize memory speed,disk optimization and CPU optimization. Please provide me...
3
zabsmarty
by: zabsmarty | last post by:
Can any one help me to make my query code optimize and load faster. Please help me or any example what steps should we use to optimize. Thank You
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.