Fast abs()
Displaying 1-9 of 9 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Zip

Ever needed a stripper? A SIGN stripper? Go on, get your positive juices flowing.

This is the one that tend to be on the market, sure it works, but we can do better can't we?
int abs (int number)

{
if (number < 0) return 0 - number;
else return number;
}

then call with:
posnum = abs(num);

So I present the new, improved, reliant on dodgy defines version!
#define _ISIGN  & 2147483648 / 1073741824 + 1


then call with:
posnum = num * (num _ISIGN);

And the speed? My estimates are 3-5 times as fast. On my testbed:
//Blank; 371ms
posnum = num; 645ms
posnum = num * (num _ISIGN); 1011ms
posnum = abs(num); 3173ms

Thankyou and goodnight.

Zip

Posted on 2004-06-26 20:45:18

mcgrue

Zip, you mast0r haxx0r you.

Are these timeruns in vc?

Posted on 2004-06-26 20:59:35

rpgking

O_o

How..the..heck did you come up with that?

In any case, I'll be using absolute value a lot, and this #define should prove to be very useful. :D

Posted on 2004-06-27 00:14:03 (last edited on 2004-06-27 00:17:23)

mcgrue

He used it by understanding 2's compliment. Or was it 1's compliment?

Anyways, RPGKing! You're exactly the man I wanted to talk to... I think! I just found the ancient animeman v2 dev zip. You were involved in this project, yes? Do you want it?

Posted on 2004-06-27 00:40:57

Zip

Timed in verge indeed. With lots of programs running in the background. Here's the code:
void autoexec()

{
int count, num, posnum;
int too = 1024 * 1024 * 16;
// Can't remember the numbers I used
timer = 0; // Gets current time
for (num = 0 - too; num <= too; num++) // Loops around
{
posnum = num * (num _ISIGN);
//Log(str(posnum));
}
PrintCenter(ImageWidth(screen)/2, ImageHeight(screen)/2, screen, 0, str(timer));
// Prints time taken in middle
ShowPage(); // Displays the buffer on the screen
while (!b3) // If escape is not pressed
{
UpdateControls(); // Check inputs
}
exit("Bye!");
}


By the way, I tried bitshiffing, and it didn't make a great deal of difference, so I didn't bother. but if you want:
#define _ISIGN  & 2147483648 >> 30 + 1

Also thanks to zara for kicking a few basics back into me that I'd forgotten in years of neglect.

Zip

Posted on 2004-06-27 03:24:37

Gayo

You are a sick, sick man.

Posted on 2004-06-27 03:58:08

rpgking

Quote:Originally posted by mcgrue

He used it by understanding 2's compliment. Or was it 1's compliment?

Anyways, RPGKing! You're exactly the man I wanted to talk to... I think! I just found the ancient animeman v2 dev zip. You were involved in this project, yes? Do you want it?



Yes, I was involved in this project(I was actually the coder/map designer/map maker, etc. with Animeman doing art). I think I've gotten much further in development than what I last gave Animeman with new maps, code, etc. before I lost everything... But if you have that zipfile, I'm definitely interested out of sheer curiosity if anything. ;)

You can e-mail it to rpgking24@gmail.com

Posted on 2004-06-28 04:46:23 (last edited on 2004-06-28 04:51:05)

Thrasher

It takes 3 seconds to get an absolute value? O_o

Posted on 2004-06-28 20:31:21

Zip

Ignoring for the moment I was talking shit about milliseconds, I meant centi-seconds, it takes ~time to do a BIG for() loop and get lots of abs() values.
The reason for this define is not to help with people who might want to call use abs() once or twice, but more for all the monkey-pathfinding/decision making stuff I'm doing where it's likely be used thousands of times per game loop.

Zip

Posted on 2004-06-29 17:52:43


Displaying 1-9 of 9 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.