Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 ... 4 5 [6] 7 8
  ignore  |  Print  
  If you ever wanted a reason to use a modern langauge...  (Read 41257 times)
0 Members and 1 Guest are viewing this topic.
Offline Mr_Light

Senior Member




shiny.


« Reply #150 - Posted 2006-02-15 01:09:03 »

tbh my first answer was "what's that operator again" too. I gues I've should have picked up the whole 6502 days hint. this is a funny one for first year freshmen.

I'm afraid I'd hit the reject pile.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #151 - Posted 2006-02-15 02:30:06 »

Oh oh oh.. I would pass! ... but then I would ask for too much money and Jeff would throw my résumé in the bin Smiley

Offline Jeff

JGO Coder




Got any cats?


« Reply #152 - Posted 2006-02-15 06:45:50 »

I dont think I've ever used  ^= before so I looked it up in my Java book.

Its not a common  operator so I tell then its XOR-assignment if they don't know Cool


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #153 - Posted 2006-02-16 01:48:47 »

Rememebr folks, the point is not to get the right anser necessarily.

Its just that watching people attack it gives me insight  into how they are going to attack OTHER problems they've never seen ebfore.

ANd thats the most fundemental skill set I know for a good engineer-- being able to tackle new problems you've never seen before.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #154 - Posted 2006-02-16 03:09:03 »

At first, I thought it zeroed both A.  But found out the true answer by trying it in code.

Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #155 - Posted 2006-02-16 03:44:32 »

>But found out the true answer by trying it in code.

Tsk. Tongue

You should solve that in your head, with your fingers (counting in binary is bad for your finger joints tho) or with pen&paper. (Or well, already know about it... heh.)

A 0101
B 1100

A^=B

A 1001
B 1100

B^=A

A 1001
B 0101

A^=B

A 1100
B 0101


Thats everything. Once you take it apart its very easy to grasp.

弾幕 ☆ @mahonnaiseblog
Offline Mr_Light

Senior Member




shiny.


« Reply #156 - Posted 2006-02-16 04:57:30 »

Thats everything. Once you take it apart its very easy to grasp.

I thought the actual thing to note was that the value of a is at the end of the code the value of b and vice versa without using a (precious?) additional help var

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Jeff

JGO Coder




Got any cats?


« Reply #157 - Posted 2006-02-16 05:26:27 »

That is correct, that it is a two-register swap.

very useful on processors like the 6502 that only had 2 registers and each had special uses.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #158 - Posted 2006-02-16 13:51:54 »

Thats everything. Once you take it apart its very easy to grasp.

I thought the actual thing to note was that the value of a is at the end of the code the value of b and vice versa without using a (precious?) additional help var

Obviously. Smiley

弾幕 ☆ @mahonnaiseblog
Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #159 - Posted 2006-02-16 20:47:57 »

IIRC IA32/64 assembly has special instruction for this. Java JIT should be also able to recognize the pattern "exchange". However most persons with experience should be able to recognize, or guess, the purpose of this pattern.

There are worse ones, for example change sign from negative to positive without if.
so A = -3 ... A = 3
   B = 3 ... B = 3

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #160 - Posted 2006-02-17 10:56:02 »

Square and then square root?
Bit-twiddling?
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #161 - Posted 2006-02-17 14:17:20 »

IIRC IA32/64 assembly has special instruction for this. Java JIT should be also able to recognize the pattern "exchange". However most persons with experience should be able to recognize, or guess, the purpose of this pattern.

There are worse ones, for example change sign from negative to positive without if.
so A = -3 ... A = 3
   B = 3 ... B = 3



Ooh!
I had never heard about that one before, so I tried figuring it out:

1  
2  
3  
int val = random.nextInt();
int sign = ((val&0x80000000)>>>31);
int val2 = (val^(0xFFFFFFFF*sign))+sign;


Probably not optimal, but fun. :-D


edit:

Cleaner version with less bit twiddling.
1  
2  
3  
int val = random.nextInt();
int sign = ((val&0x80000000)>>>31);
int val2 = val*(1-sign*2);



edit again:

Heh. Even easier.
1  
2  
int val = random.nextInt();
int val2 = val*(1-(val>>>31)*2);

Play Minecraft!
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #162 - Posted 2006-02-17 14:21:47 »

IIRC IA32/64 assembly has special instruction for this. Java JIT should be also able to recognize the pattern "exchange". However most persons with experience should be able to recognize, or guess, the purpose of this pattern.

There are worse ones, for example change sign from negative to positive without if.
so A = -3 ... A = 3
   B = 3 ... B = 3

Never come across that one before.

I think this should work?
Dunno if its the optimal solution though.

1  
2  
3  
int a = random.nextInt();
final int sign = a>>31;
a = (a-(sign&1)))^sign;


:edit:

he, beaten to it.
Slightly different algorithm though =)
No multiplies either Tongue
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #163 - Posted 2006-02-17 14:32:41 »

Yes, and yours is faster!

Math.abs(): 17.61743 ns
My way: 6.5100017 ns
Your way: 5.472977 ns


This is fun, in a geeky way!

Play Minecraft!
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #164 - Posted 2006-02-17 14:48:37 »

lol, is that with the code inside a method?
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #165 - Posted 2006-02-17 14:58:35 »

Even faster:

1  
a = (a^(a>>31))-(a>>31);

4.3739724 ns

Storing the sign in a final int is actually SLOWER on my jvm (1.5.0_06, client).


And, no, it's not including a method call. =)
I set up an int[] full of random integers, then do four for loops like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
int res = 0;
long pre = System.nanoTime();
for (int i = 0; i < vals.length; i++)
{
    int a = vals[i];
    a = (a^(a>>31))-(a>>31);
    res += a;
}
long post = System.nanoTime();
Sytem.out.println((post - pre)/(float)vals.length + " ns");


The res values are then compared to each other to make sure they all get the same result.
Of course, this is all done several times to make sure the code has a chance to warm up.

Edit:
Just to clarify, the ONLY thing that changes in the loops is the middle row. (ie the one that says "a = (a^(a>>31))-(a>>31);" above)
This makes sure the loop overhead is the same.

Play Minecraft!
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #166 - Posted 2006-02-17 15:39:14 »

You should stick these in a final method and compare with Math.abs()...   Then if it is still faster submit a performance enhancement to Mustang Smiley

Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #167 - Posted 2006-02-17 15:58:27 »

Math.abs: 17.01518 ns
1  
2  
3  
public static int abs(int a) {
    return (a < 0) ? -a : a;
}


Foo.abs: 4.990932 ns
1  
2  
3  
4  
public static int abs(int a)
{
    return (a^(a>>31))-(a>>31);
}


That's.. quite a lot faster.

Play Minecraft!
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #168 - Posted 2006-02-17 16:08:15 »

Sweet! I like your further optimised version!
I presume that is the optimal solution.
Offline Mark Thornton

Senior Member





« Reply #169 - Posted 2006-02-17 17:46:49 »

Sweet! I like your further optimised version!
I presume that is the optimal solution.
It may not be so efficient on processors that lack a fast shifter. While many processors will do a 31 bit shift in 1 or 2 clock cycles some require 31 clock cycles.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 74
Projects: 15


★★★★★


« Reply #170 - Posted 2006-02-17 19:41:38 »

You should stick these in a final method and compare with Math.abs()...   Then if it is still faster submit a performance enhancement to Mustang Smiley

yup gotta agree with that, markus u gonna submit it?
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #171 - Posted 2006-02-17 21:27:21 »

Is Math.abs() really ever even close to being a bottleneck? ;-)

If someone else wants to submit it, go for it.

Play Minecraft!
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #172 - Posted 2006-02-17 21:50:04 »

Is Math.abs() really ever even close to being a bottleneck? ;-)

Doubtful, though Java is renown for having slow trig.

I realize this is in part due to the accuracy specifications,
but any unnecesary slowdowns in core methods (such as abs()) are always going to have a knock-on effect when they are relied upon in higher level functionality.
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #173 - Posted 2006-02-17 22:40:20 »

That is true. :-)

Now we just need to figure out a fast abs for floats and doubles. Wink

Play Minecraft!
Offline Jeff

JGO Coder




Got any cats?


« Reply #174 - Posted 2006-02-17 23:03:47 »

Is Math.abs() really ever even close to being a bottleneck? ;-)

Doubtful, though Java is renown for having slow trig.

See the gems section of my FAQ for a way to improve this.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #175 - Posted 2006-02-17 23:05:47 »

Speaking of which has anyone timed this out against Math.abs(int) ?

If its faster, I'll add it to the Gems section Cool

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #176 - Posted 2006-02-17 23:22:23 »

That is true. :-)

Now we just need to figure out a fast abs for floats and doubles. Wink

That's easy, actually it's much easier than for integers. Just use one AND, and you are done. ^_^
Of course I asume you are using SSE2 registers for all floating point work.
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #177 - Posted 2006-02-17 23:31:25 »

Speaking of which has anyone timed this out against Math.abs(int) ?

If its faster, I'll add it to the Gems section Cool

Yes, see my post a few post backs.
Even when in a method, the hack runs at about 30% of the speed of Math.abs(int)

That's a speedup of ~250%!

Play Minecraft!
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #178 - Posted 2006-02-18 06:48:00 »

Wow this thread actually became useful again. What a surprise Smiley

弾幕 ☆ @mahonnaiseblog
Offline .uj

Junior Member





« Reply #179 - Posted 2006-02-18 08:01:17 »

Wow this thread actually became useful again. What a surprise Smiley

But awfully dull isn't it. The coolest accessory you can wear at this forum probably is blinkers.  Grin

Good luck with Math.abs! Once you got that optimized everybody will start using Java I'm sure.
Pages: 1 ... 4 5 [6] 7 8
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (12 views)
2014-07-29 18:09:19

Riven (9 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (26 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!