Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Bit Twiddle & Branch Removal  (Read 2891 times)
0 Members and 1 Guest are viewing this topic.
Offline Roquen
« Posted 2009-10-27 10:25:20 »

These are more interesting for other languages since the JDK does not do a good job of inline/reg-alloc & schedule.

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  
public static final int clampPositive(int a)
{
  // return (a >= 0) ? a : 0;
  return a & ~(a >> 31);
}

public static final int abs(int a)
{
  // return (a >= 0) ? a : -a;
  int s = a >> 31;
  a ^= s;
  a -= s;
  return a;
}

public static final int min(int a, int b)
{
  // return (a <= b) ? a : b;
  a -= b;
  a &= (a >> 31);
  a += b;

  return a;
}

public static final int max(int a, int b)
{
  // return (a >= b) ? a : b;
  a -= b;    
  a &= ~(a >> 31);
  a += b;    

  return a;
}

public static final int select(int a, int b, int c, int d)
{
  // return (a<b) ?c : d
  return (((a-b)>>31) & (c^d))^d;
}

public static final int sgn(int a)
{
  // return (a > 0) ? 1 : (a < 0) ? -1 : 0;
  return (-a >>> 31) | (a >> 31);
}
Offline Abuse

JGO Knight


Medals: 15


falling into the abyss of reality


« Reply #1 - Posted 2009-10-27 11:09:44 »

I especially like sgn(..)  Grin
Surely that must be faster than the 1.5 branches needed ordinarily?

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Stranger

Senior Devvie


Medals: 9


I'm going slightly mad It finally happened


« Reply #2 - Posted 2009-10-27 13:40:24 »

AFAIK, the shifts are some expensive for Java ...

Anton
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2009-10-27 13:45:51 »

The shifts are JVM opcodes and the cost will be strongly related to the hardware cost of performing a shift.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 842
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2009-10-27 13:53:50 »

At that level of execution, it's more about what (surrounding) native code the JIT generates, not so much the cost of the single instruction.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline Roquen
« Reply #5 - Posted 2009-10-27 15:29:45 »

This is a different issue related to the execution of a sequence of instructions and is what my opening comment was attempting to say.  As individual operations, shifts are very fast. 
Offline pjt33
« Reply #6 - Posted 2009-10-27 16:16:22 »

1  
2  
3  
4  
5  
public static final int sgn(int a)
{
  // return (a > 0) ? 1 : (a < 0) ? -1 : 0;
  return (a >> 31) | (a >>> 31);
}

Huh That's equivalent to
1  
return (a >= 0) ? 0 : -1;
Offline Abuse

JGO Knight


Medals: 15


falling into the abyss of reality


« Reply #7 - Posted 2009-10-27 16:28:47 »

Huh That's equivalent to
1  
return (a >= 0) ? 0 : -1;


Indeed; classic case of TGTBT =)

Also, clampPositive is doing more work than it needs to.

1  
2  
3  
4  
5  
public static final int clampPositive(int a)
{
  // return (a >= 0) ? a : 0;
  return a & ~(a >> 31);
}

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Roquen
« Reply #8 - Posted 2009-10-27 16:46:04 »

Classic case of screwing up when deleting a cast from C.  Nuked the negate.  And yeah, the &= should just be and.

(edit: I update the original code to reflect these changes)
Offline Jono
« Reply #9 - Posted 2009-10-27 20:13:13 »

Huh That's equivalent to
1  
return (a >= 0) ? 0 : -1;


Can't that one just be:

1  
return (a >>> 31) ^ 1


Edit: that was for sgn(a), incase it wasn't clear
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 842
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2009-10-27 21:30:28 »

Can't that one just be:

1  
return (a >>> 31) ^ 1


Edit: that was for sgn(a), incase it wasn't clear


sgn(int) => [-1, 0, +1]

Your code returns either a 0 or 1, never -1


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline Jono
« Reply #11 - Posted 2009-10-27 22:52:52 »


sgn(int) => [-1, 0, +1]

Your code returns either a 0 or 1, never -1
I blame it on the early morning. First I thought sgn returned 1 or -1 only, then I was working in 1's complement instead of 2's complement. Duh.
Pages: [1]
  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.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!