Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
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  
  how to compute log2  (Read 3923 times)
0 Members and 1 Guest are viewing this topic.
Offline shareme

Junior Devvie

Java games rock!

« Posted 2003-12-11 20:27:06 »

I want to speed up fixed point math computations by using shifting..

so how do I compute Log2 of a number considering that I do not have access to Math.log?

Offline tom
« Reply #1 - Posted 2003-12-11 22:48:32 »

Here is a fixed point math library:

Offline davidaprice

Junior Devvie

« Reply #2 - Posted 2003-12-12 05:32:34 »

Log2(x) is just the opposite of 2^x.  So where 2^x is just "1 shifted left x times", log2(x) is just "the position of the left-most 1". E.g.:

log2(00000001b) = 0   ('b' suffix means binary here)
log2(00000100b) = 2
log2(10000000b) = 7

For numbers which aren't powers of 2, you must decide whether to round to the nearest integer or just round down. Rounding down is much easier Smiley. In which case:

log2(00000111b) = 2

Then your code will look something like this:

int log2 = 0;
while (x > 1) {
   x >>>= 1;

Note that I'm assuming x is positive! Negative numbers or zero would be invalid arguments for a log2 method.

If you're emulating fixed point (e.g. by storing all numbers << 8 ) then just subtract the number of fractional binary digits (e.g. 8 ) from the result of log2.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline shareme

Junior Devvie

Java games rock!

« Reply #3 - Posted 2003-12-12 06:37:38 »

I foudn it in my knuth vollumes of books Smiley

let me explain why I want to sue it..

you can speed up fp math on non FPU cpus such as arm by

6*32=6<<log2 32=192

25/4 25>>log2 4=6

of course division is truncated and you have to allow for that Smiley

of course if I convert to binary before any computation than division is no longer truncated Smiley hey that works!

Offline shareme

Junior Devvie

Java games rock!

« Reply #4 - Posted 2003-12-13 22:03:03 »

here is my solution and an example of where and how I am usng it:

Pages: [1]
  ignore  |  Print  

hadezbladez (276 views)
2018-11-16 13:46:03

hadezbladez (154 views)
2018-11-16 13:41:33

hadezbladez (287 views)
2018-11-16 13:35:35

hadezbladez (69 views)
2018-11-16 13:32:03

EgonOlsen (2124 views)
2018-06-10 19:43:48

EgonOlsen (2148 views)
2018-06-10 19:43:44

EgonOlsen (1361 views)
2018-06-10 19:43:20

DesertCoockie (1957 views)
2018-05-13 18:23:11

nelsongames (1598 views)
2018-04-24 18:15:36

nelsongames (2246 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 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‑
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!