Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 how to compute log2  (Read 3687 times) 0 Members and 1 Guest are viewing this topic.
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?

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

Here is a fixed point math library:
http://home.rochester.rr.com/ohommes/MathFP/

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 . In which case:

log2(00000111b) = 2

Then your code will look something like this:

int log2 = 0;
while (x > 1) {
log2++;
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.
shareme

Junior Devvie

Java games rock!

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

I foudn it in my knuth vollumes of books

let me explain why I want to sue it..

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

6*32=6<<log2 32=192

25/4 25>>log2 4=6

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

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

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:

http://www.jroller.com/page/shareme/20031213

Pages: [1]
 ignore  |  Print

 ivj94 (585 views) 2018-03-24 14:47:39 ivj94 (49 views) 2018-03-24 14:46:31 ivj94 (383 views) 2018-03-24 14:43:53 Solater (63 views) 2018-03-17 05:04:08 nelsongames (109 views) 2018-03-05 17:56:34 Gornova (159 views) 2018-03-02 22:15:33 buddyBro (703 views) 2018-02-28 16:59:18 buddyBro (92 views) 2018-02-28 16:45:17 xxMrPHDxx (494 views) 2017-12-31 17:17:51 xxMrPHDxx (734 views) 2017-12-31 17:15:51
 KaiHH 14x SHC 10x NuclearPixels 10x Zemlaynin 10x ByerN 7x Damocles 6x Guerra2442 6x Spasi 5x VaTTeRGeR 5x ags1 4x orangepascal 4x philfrei 4x princec 3x ndnwarrior15 3x mesterh 3x Phased 2x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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