Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  Crazy bytecode madness...  (Read 1820 times)
0 Members and 1 Guest are viewing this topic.
Offline crystalsquid

Junior Member




... Boing ...


« Posted 2004-01-15 21:48:11 »

Ive been thinking about writing Java bytecodes directly (using one of the Java assemblers out there) and just want to ask if anyone knows whether this will work:

Can I push a float value onto the stack, and then pop it off & use it as an int?

Does the VM ever check this sort of thing?

If it works, then there are a few sneaky optimisations I can make for a few maths functions (sqrt in particular) as well as doubling the speed of my 3D vertex transform & clip. (& I would definately like an inline bytecode assembler in the java compiler if you can!)

- Dom
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2004-01-15 22:07:35 »

Hmm. I dunno.  Type safety may be built into the machine for security reasons.  Even if the VM allows it the byte code verifier may barf on it.

try it and see...

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 rreyelts

Junior Member




There is nothing Nu under the sun


« Reply #2 - Posted 2004-01-16 00:46:57 »

Quote
Ive been thinking about writing Java bytecodes directly (using one of the Java assemblers out there) and just want to ask if anyone knows whether this will work:


Forget it. Writing Java bytecode by hand for optimization purposes is a worthless endeavor. Java bytecode is useful in generative techniques, where you're writing a compiler for a language that targets the Java VM, or developing an aspecting system (ala AspectWerkz/AspectJ) / rolling your own aspects. Occassionally you might write some bytecode by hand for correctness purposes - like invoking a MonitorEnter without a corresponding MonitorExit for a self-written mutex class.

Quote
Can I push a float value onto the stack, and then pop it off & use it as an int?


Nope. The verifier will check this the moment it loads your class file. If it didn't there'd be security loopholes in the VM. Sun's verifier is a piece of crap, in that it doesn't give you much information when verification fails. You can use BCEL's verifier, Justice, for much better error messages. Unfortunately, Justice has some quirks in that certain classes pass Sun's verifier while failing Justice.

God bless,
-Toby Reyelts

About me: http://jroller.com/page/rreyelts
Jace - Easier JNI: http://jace.reyelts.com/jace
Retroweaver - Compile on JDK1.5, and deploy on 1.4: http://retroweaver.sf.net.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline crystalsquid

Junior Member




... Boing ...


« Reply #3 - Posted 2004-01-16 09:48:19 »

Quote
Nope. The verifier will check this the moment it loads your class file


Ok... How does the floatToIntBits() work then? I would be upset if it took more than a couple of bytecodes - or then again would the JIT compiler convert this down to pretty much a move from float to int register anyway?

The reason I ask is because I am writing my inner loop for a transform & clip function, and I know I should be able to generate clipping flags during the stall for the z divide, providing that the floatToIntBits does not use any floating point operations at all.

Thanks for the comments,

- Dom
Offline Woz

Senior Newbie




A Troll who lives in a hole


« Reply #4 - Posted 2004-01-16 12:09:50 »

Dom,

floatToIntBits() is a JNI call, once you have the value in C or C++ you can do what you like to it.

Although you should still adhere to sun's type definition, so most likely the value is loaded into a jvalue as a jfloat and then read out as an jint. This is exactly how Kaffe does it:
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  
Extract taken from Fp.c of Kaffe 1.0.7.

/*
 * Convert float to int.
 */

jint
floatToInt(jfloat val)
{
        jvalue d;

      d.f = val;
      return d.i;
}


Extract from Sun's JNI header file shipped with SDK1.3 jni.h

typedef union jvalue {
    jboolean z;
    jbyte    b;
    jchar    c;
    jshort   s;
    jint     i;
    jlong    j;
    jfloat   f;
    jdouble  d;
    jobject  l;
} jvalue;


Although the couple hundered clock cycles required to make the JNI call make this unsuitable for your inner loop as the Divide is considerably less.

Hope that helps,
------
Woz.
Offline Mark Thornton

Senior Member





« Reply #5 - Posted 2004-01-16 13:08:06 »

Quote
Dom,

floatToIntBits() is a JNI call, once you have the value in C or C++ you can do what you like to it.

It might look like a JNI call, but the JIT could easily optimise that away. There is no way of telling without the source to the JIT or timing it. Just like many of the methods in the Math class are routinely replaced by inline equivalents.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #6 - Posted 2004-01-16 13:11:23 »

Quote

It might look like a JNI call, but the JIT could easily optimise that away. There is no way of telling without the source to the JIT or timing it. Just like many of the methods in the Math class are routinely replaced by inline equivalents.


You know that you can get the source to HotSpot for free right?  You just have to agree to some license with a click of a mouse.

Offline elias

Senior Member





« Reply #7 - Posted 2004-01-16 13:22:22 »

Of course it's allowed to convert between float<->int in that way. How can that be a security problem anyway? float and int are same fundamental type but with different semantics.

- elias

Offline crystalsquid

Junior Member




... Boing ...


« Reply #8 - Posted 2004-01-16 13:55:31 »

Its a JNI call?!?

Its in the Java 1.1 spec as java.lang.Float.floatToIntBits()

- Dom
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #9 - Posted 2004-01-16 15:34:45 »

If you look in the source from java.lang.Float:

public static native int floatToIntBits(float value);

(native = JNI  Smiley)

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

Senior Member





« Reply #10 - Posted 2004-01-16 16:06:31 »

Quote
If you look in the source from java.lang.Float:

public static native int floatToIntBits(float value);

(native = JNI  Smiley)

Likewise the source for java.lang.Math appears to delegate many methods to StrictMath, but this is not necessarily what actually happens inside the JVM. In fact Math.sin for example apparently uses the argument reduction that would be used by StrictMath but the actual sin is computed using the Intel instruction. As I said before to determine what really happens you have to look at the JVM source, or deduce it from timing information. In all cases the specification requires that the result value have certain properties but does not require the JVM to obtain the result in any particular way.

Also see the comments in the "implementation" of Math.sqrt()
Offline Jeff

JGO Coder




Got any cats?


« Reply #11 - Posted 2004-01-16 21:25:58 »

Mark's right that the VM recognizes the basic math calls and "cheats" doign them directly in-line in compiled code Smiley

Thsi is the same kind of mechanism that is used for native Byte Buffers Smiley

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 crystalsquid

Junior Member




... Boing ...


« Reply #12 - Posted 2004-01-17 14:37:56 »

Ok - Any idea whether the VM 'cheats' (as you put it) with the floatToIntBits?

- Dom

Noob question: Where do I get the source code for these bits Huh
Offline Jeff

JGO Coder




Got any cats?


« Reply #13 - Posted 2004-01-17 20:26:47 »

At a guess probably not as its not a call that I woudl have expected to geta lot of use.. OTOH if the VM guys needed it for the center of a loo pthen it might.

Best way to find out is to *gag* microbenchmark it and see if the result loosk like a JNI call in terms of time or if its faster Smiley


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
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.

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

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

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

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

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
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!