Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3368: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3370: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3371: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3372: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
OrbWorks Community Forum • View topic - Tips for fast programming and a question
Page 1 of 1

PostPosted: Mon Dec 02, 2002 1:47 pm
by darwin
Hi
For those ones who doesen't know: You can write function-like macros in PocketC. This has some great advances:

#define Square(x) x*x
slowersquare(float x) { return x*x; }
main () {
putsl(Square(3.4));
putsl(slowersquare(3.4);
}


The Complier will compile after preprocessing:

main () {
putsl((3.4*3.4));
putsl(slowersquare(3.4);
}

The big advance of this is that you don't spend time on calling functions and you don't have to care about variable types (square_float, square_int)! Actually, the time you win is not pretty much, but every bit may count if you do alot of calculations.

Of course this way of Programming won't fit too much complicated code - and there's a little bit of troubleshooting you have to watch for:

#define Square2(a,b) a=a*a; b=b*b;

main () {
int i,a=2,b=3;
for (i=0;i<10;i++) Square2(a,b);
}

will produce:

main () {
int i,a=2,b=3;
for (i=0;i<10;i++) a=a*a;

b=b*b;
}

This is a huge difference and makes you spending time on finding bugs.
Oh, and btw: Finding bugs is also more complicated that way, so watch out for them when writing code that way.


There's only one question left I have:

#define div(a,b) a/b

won't work because of the "/" - PocketC stops the definition here. That's weird and I ask you: Is there a workaround for this bug (except for an extra function named fdiv(float a, float b)) ???

cya

http://www.strange-phenomenom.de/

PostPosted: Mon Dec 02, 2002 4:17 pm
by soinsg
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by darwin</i>
<br />
#define Square2(a,b) a=a*a; b=b*b;

for (i=0;i<10;i++) Square2(a,b);

will produce:
for (i=0;i<10;i++) a=a*a;
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">

Should there be a ";" after "b=b*b"? Maybe it will work then, or it was a typo in your post.

PostPosted: Mon Dec 02, 2002 5:42 pm
by KOU
>>Should there be a ";" after "b=b*b"? Maybe it will work then, or it was a typo in your post.

Single ";" will do nothing, I think.

Code: Select all
Ex. putsl("test");
    ; //this will do nothing


KOU

PostPosted: Tue Dec 03, 2002 5:50 am
by soinsg
What I meant was:

#define Square2(a,b) a=a*a; b=b*b;

for (i=0;i<10;i++) Square2(a,b);

will produce:
for (i=0;i<10;i++) a=a*a<b>;;</b>

PostPosted: Tue Dec 03, 2002 7:31 am
by samps
This one works:

#define div a/b;


main()
{
int a=20, b=3;
a=div;
alert(a);
}

And correctly displays 6 in the Alert form.

Whereas the more direct:

#define div a/b;


main()
{
int a=20, b=3;
alert(div);
}

yields an error: ") expected at end of function call, possibly too many arguments...."

I won't even pretend to understand the phenomena, just adding a few observations to the discussion.

cheers
Samps

PostPosted: Tue Dec 03, 2002 11:05 am
by darwin
Sorry, but some of you didn't get the point...

1.:
#define div(a,b) a/b

main () {
putsl(div(8,7));
}

=> output: 8

Very wrong Result! - Tested multiple times on my ipaq.
PocketC seems to think that the follwing rest after the slash is a comment and doens's care about

#define hey(a,b) a/b or anything textylike that would cause a compilation error

putsl(hey(4,3));

won't throw an error neither.

So it is definitly a bug. Test it before posting it. I did only test the PocketC Compiler.

<hr noshade size="1">
2.:

To give you a concrete example I will show you a code for 2D Operations. The data will be stored in a 2-int sized arrays:

#define POINT pointer
#define Point_New malloc(2)
#define Point_Set(point,x,y) point[0]=x;point[1]=y
#define Point_Add(a,b,res) res[0]=a[0]+b[0];res[1]=a[1]+b[1]
#define Point_Typed(p) p[0],p[1]

main () {
int i;
POINT p1,p2;
p1=Point_New;
p2=Point_New;
Point_Set(p1,3,5);
Point_Set(p2,5,8);
Point_Add(p1,p2,p1); //p1[0]=8 and p1[1]=13 now!!!

setPixel(Point_Typed(p1)); //draws a pixel at 8,13
//very correct till now

for (i=0;i<10; i++) Point_add(p1,p2,p1);
//wrong - line above is equal to:
for (i=0;i<10;i++) p1[0]=p1[0]+p2[0];
p1[1]=p2[1]+p1[1];
//this is not the thing that we intended to do -
//prevent this by using brackets:
for (i=0;i<10;i++) {Point_add(p1,p2,p1);}

}


So, read this carefully. It is closely to write objectoriented-like in C.
Of course, you could write functions like
point_add(POINT p1, POINT p2) {...etc...}
but the way I described is smaller and faster. Of course, the price is that errorfinding _could be_ much harder... but it can prevent you from doing errors:

Example: You have created a pointer array that holds some information about something (for example a Rectangle).

Old fashion:
create_Rect(int x, int y, int w, int h) {
pointer p;
p=malloc(4);
p[0]=x;p[1]=y;p[2]=w;p[3]=h;
return p;
}

And everytime you access your Rectangle, you use x[0], x[1] etc.
This is not very good, because you can get mixed up easily and changes lateron will be hard to be done. If you write a macro functions this is much more flexible:

#define Rect_x(r) r[0]
#define Rect_y(r) r[1]
#define Rect_w(r) r[2]
#define Rect_h(r) r[3]
#define Rect_x2(r) r[0]+r[2]
#define Rect_y2(r) r[1]+r[3]

main () {
//init the rect etcetc.
rect(Rect_x(anyrect),Rect_y(anyrect),Rect_x2(anyrect),Rect_y2(anyrect));
}

This is a better way to struct your code - for example, if you decide to hold also color information of your rectangle and create a two-dimensional array:

r=malloc(2);
r[0]=malloc(4); //holds x,y,w,h
r[1]=malloc(3); //hold RGB information

Now, you simply change your define-macros:
#define Rect_x(r) r[0][0]
etc.

Hope this was clear right now. It is really a much nicer way to write applications.

If there wouldn't be the slash-bug it would be really wonderfull :)


I really don't know why the documentation of PocketC doesn't mention this because it enables you to write wrappers:

#define forto(var,start,stop) for(var=start;var<stop;var++)

main() {
int i;
for (i=0;i<10;i++) ;
//equal to:
forto(i,0,10); //saves 5 character of writing :))
}

Ok, questions are welcome!

cya.

http://www.strange-phenomenom.de/

PostPosted: Tue Dec 03, 2002 10:08 pm
by KOU
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by soinsg</i>
<br />What I meant was:

#define Square2(a,b) a=a*a; b=b*b;

for (i=0;i<10;i++) Square2(a,b);

will produce:
for (i=0;i<10;i++) a=a*a<b>;;</b>

<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">

Code: Select all
for (i=0;i<10;i++) a=a*a;;

is same as
Code: Select all
for (i=0;i<10;i++) a=a*a;
;


because the C compiler ignores all line breaks.
(except for end of "//" type comment)

>>darwin

Yes, I think it is a bug.
But be careful with macro functions.
It may slow the program sometimes.

Few things I want to point out:
Code: Select all
#define square(a) a*a  //kinda macro is very dangerous because this can happen

mult(i++);
>i++ * i++; //Two incliments!!!


so sometimes functions are better.

I don't know if the Pocket C parser will evaluate const type calculations when compiling...
Ex. calculations like 5*349/34-34 might be calculated at runtime

Always define macros like this
#define mult(a, b) <b>((</b>a<b>)</b>*<b>(</b>b<b>))</b>
for prevention of bugs.

mult(a, b-5)
with #define mult(a,b) a*b
a*b-5
(a*b)-5
:(
with #define mult(a, b) <b>((</b>a<b>)</b>*<b>(</b>b<b>))</b>
((a)*(b-5))
a*(b-5)
:)

Wrappers you wrote are great if you are going to use source code only for yourself, but it will be confusing if you are going to share program source codes.

USE FUNCTION MACROS ONLY FOR SAVING CODES YOU HAVE TO TYPE.
(AND CAREFUL NOT TO MAKE THE CODE CONFUSING)

Sorry for my English,
Kou

PostPosted: Tue Dec 03, 2002 10:14 pm
by KOU
PS for the last post:
If you are going to write type ignoring library like that in C++ (not in PocketC), <b>templates</b> are really useful.

PostPosted: Mon Dec 09, 2002 4:40 pm
by darwin
Hi
I didn't know about the i++ incrementing - thanks :) I hadn't this case till now, but maybe you saved me from painfull bugsearching ;)

Well - I just want to give here another example how to use macros efficiently: Saving memory.

If you store points you can do it like this:
point = malloc(2); point[0]=x; point[1]=y;

This is nice and easy to understand but if you store them in an array you waste memory and time (PocketC gets slower the more variables you allocate during runtime).
If you do only need integer resolution with 16 bit you can solve it that way too by using bitwise operations (likely known, but using macros it is slightly faster than with functions - and it is shorter code, which is welcome because I write my pc-files only on my ipaq):

#define UIpoint_new(x,y) ((x<<16)|y)
#define UIpoint_X(p) (p>>16)
#define UIpoint_Y(p) (p&0xFFFF)

Be aware that this is NOT signed - storing negative numbers cause troubles. But there's also a positive sideeffect:

int x,y,sum,dif;
x=UIpoint_new(10,4);
y=UIpoint_new(19,10);
sum=x+y;
dif=y-x;
putsl(UIpoint_X(sum)+":"+UIpoint_Y(sum));
putsl(UIpoint_X(dif)+":"+UIpoint_Y(dif));

will output correctly:
29:14
9:6;

Nice - isn't it?
This is also a usefull tool when saving a path in a rasterimage (I did develop that for pathfinding within an heightfield :) ) inside integer arrays. Instead of 3 Integers (a pointer is 32 bit like integer) you need only 1 - and faster because you don't have to allocate the memory.

You can also write a function for storing signed coordinates by adding a fixed number. But I haven't figured out a stable working method because I always get mixed up with the signed int-bit of PocketC. But it should work also - at least for two 14 bit values (+/-~16.000).

I still think it is a nice way to integrate small functions - even when you share your code. I decided for myself to use uppercase words (MNU_NEW) for constants, firstcharacter-uppercase words (UIpoint) for macro functions and lowercase (openFile) for normal functions. This helps me for recognizing my own functions.

I hope this was an usefull information for you :)

cu

btw: what do you mean with templates?

http://www.strange-phenomenom.de/

PostPosted: Mon Dec 09, 2002 4:43 pm
by darwin
ps:
Of course you should write

#define UIpoint(x,y) ((x)<<16|(y))

to make sure it is done correctly.

http://www.strange-phenomenom.de/

PostPosted: Mon Dec 09, 2002 8:12 pm
by KOU
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by darwin</i>
<br />Hi
I didn't know about the i++ incrementing - thanks :) I hadn't this case till now, but maybe you saved me from painfull bugsearching ;)

Well - I just want to give here another example how to use macros efficiently: Saving memory.

If you store points you can do it like this:
point = malloc(2); point[0]=x; point[1]=y;

This is nice and easy to understand but if you store them in an array you waste memory and time (PocketC gets slower the more variables you allocate during runtime).
If you do only need integer resolution with 16 bit you can solve it that way too by using bitwise operations (likely known, but using macros it is slightly faster than with functions - and it is shorter code, which is welcome because I write my pc-files only on my ipaq):

#define UIpoint_new(x,y) ((x<<16)|y)
#define UIpoint_X(p) (p>>16)
#define UIpoint_Y(p) (p&0xFFFF)

Be aware that this is NOT signed - storing negative numbers cause troubles. But there's also a positive sideeffect:

int x,y,sum,dif;
x=UIpoint_new(10,4);
y=UIpoint_new(19,10);
sum=x+y;
dif=y-x;
putsl(UIpoint_X(sum)+":"+UIpoint_Y(sum));
putsl(UIpoint_X(dif)+":"+UIpoint_Y(dif));

will output correctly:
29:14
9:6;

Nice - isn't it?
This is also a usefull tool when saving a path in a rasterimage (I did develop that for pathfinding within an heightfield :) ) inside integer arrays. Instead of 3 Integers (a pointer is 32 bit like integer) you need only 1 - and faster because you don't have to allocate the memory.

You can also write a function for storing signed coordinates by adding a fixed number. But I haven't figured out a stable working method because I always get mixed up with the signed int-bit of PocketC. But it should work also - at least for two 14 bit values (+/-~16.000).

I still think it is a nice way to integrate small functions - even when you share your code. I decided for myself to use uppercase words (MNU_NEW) for constants, firstcharacter-uppercase words (UIpoint) for macro functions and lowercase (openFile) for normal functions. This helps me for recognizing my own functions.

I hope this was an usefull information for you :)

cu

btw: what do you mean with templates?

http://www.strange-phenomenom.de/
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">

PostPosted: Mon Dec 09, 2002 8:22 pm
by KOU
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">
btw: what do you mean with templates?
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">
I don't know why I wrote "templates". It is only for c++.
It is used to write functions that takes both integers and float values. (It can do lot more things with STL, you know)

I think I meant inline functions. It is also only for c++, but it enables us to write more efficient and MINI-functions. (also with out incliment bug.)

See 9.3 and others on
http://new-brunswick.net/workshop/c++/f ... tions.html

I think there was a section discussing cons and pros of #def type macros in "Game Programming Gems II".

Any way, it is completely off topic so you don't have to care.