Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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  
  mod & div...  (Read 1650 times)
0 Members and 1 Guest are viewing this topic.
Offline crystalsquid

Junior Member




... Boing ...


« Posted 2004-05-13 17:04:56 »

Curious question - how does the VM actually do an integer mod operation?
There does not appear to be an 'imod' bytecode, and the 'idiv' doesn't return the modulus (although I don't know of any processor that doesnt return the mod when it does an integer divide).

I just so happen to want to do a divide and a mod at the same time, and I don't particularly wish to do:

1  
2  
int div = a / b;
int mod = a - (div*b);


Any suggestions?

- Dom

PS: This is in a loop I am looking at running >5000 times a frame, so I would _really_ like to do it quite quickly Smiley
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2004-05-13 17:56:51 »

Well, the first question is: what happens if you do
1  
2  
c1 = a % b;
c2 = a / b;

malloc will be first against the wall when the revolution comes...
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #2 - Posted 2004-05-13 17:58:20 »

PS what are you doing 5k divs and mods for in one frame? Is there possibly a better algo?

In particular, there are some interesting identities when you start working in modulus arithmetic (which are heavily used in crypto), and there's a small chance some of those could help you rearrange your algo...

malloc will be first against the wall when the revolution comes...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mark Thornton

Senior Member





« Reply #3 - Posted 2004-05-13 20:31:08 »

Quote
Curious question - how does the VM actually do an integer mod operation?
There does not appear to be an 'imod' bytecode,


Remainder: irem, lrem, frem, drem.
Offline crystalsquid

Junior Member




... Boing ...


« Reply #4 - Posted 2004-05-14 12:51:08 »

Ah - rem for remainder!
There I was looking for the conventional one used by most of the known computing world!

Still - does it reuse the result from a divide once the bytecode is compiled?

As to what I am doing - its a good old fashioned software renderer to get 3D running for Java 1.1. The mod/div is for generating DDA values for the edge gradients - although I may not be doing that now Smiley

- Dom
Offline phazer

Junior Member




Come get some


« Reply #5 - Posted 2004-05-17 11:42:50 »

It's up to the JVM to make the div followed by mod optimization. I doubt they make this optimization because it occurs very seldom.

Here is a code example and the generated byte code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
class Test {
  public void test(int a, int b) {
    int y = a / b;
    int x = a % b;
  }
}

javap -c Test
...

Method void test(int, int)
   0 iload_1
   1 iload_2
   2 idiv
   3 istore_3
   4 iload_1
   5 iload_2
   6 irem
   7 istore 4
   9 return

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (32 views)
2013-05-17 21:29:12

alaslipknot (40 views)
2013-05-16 21:24:48

gouessej (70 views)
2013-05-16 00:53:38

gouessej (69 views)
2013-05-16 00:17:58

theagentd (78 views)
2013-05-15 15:01:13

theagentd (73 views)
2013-05-15 15:00:54

StreetDoggy (113 views)
2013-05-14 15:56:26

kutucuk (137 views)
2013-05-12 17:10:36

kutucuk (137 views)
2013-05-12 15:36:09

UnluckyDevil (145 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.082 seconds with 20 queries.