Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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 1900 times)
0 Members and 1 Guest are viewing this topic.
Offline crystalsquid

Junior Duke




... Boing ...


« Posted 2004-05-13 15: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 15: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 15: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 Duke





« Reply #3 - Posted 2004-05-13 18: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 Duke




... Boing ...


« Reply #4 - Posted 2004-05-14 10: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 Duke




Come get some


« Reply #5 - Posted 2004-05-17 09: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.

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
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

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