Scenergy #02
31 декабря 1999

Coding - 16-bit procedure is fast exponentiation.

<b>Coding</b> - 16-bit procedure is fast exponentiation.
                    FAST 16-bit SQUARE ROOT

This is nice, but what about arbitrary numbers which are not
powers of two? Fortunatelly, there is nice trick which allows us
to look at arbitrary number this way:

X = 2 ^ N * M

Where:
N is number of bits needed to represent number X
M is mantissa of X and lies in interval <0, 1>

Thinking this way, we can say:

128 = 2 ^ 8 * 0.5
 40 = 2 ^ 6 * 0,625

 Since we are experienced in mathematics [are we?], We can
calculate sqrt (X) easily:

sqrt (X) = 2 ^ (N / 2) * sqrt (M)

For example:

sqrt (144) = sqrt (2 ^ 8 * 0.5625) = 2 ^ 4 * sqrt (0.5625) =
= 2 ^ 4 * 0.75 = 12

 Voila! It works! Let's summarize it and write square root
algorithm:


  1) Find out N [number of bits we need to represent number X].

  2) Calculate mantissa M.

  3) Take sqrt (M) from table.

  4) Take N / 2 highest bits from mantissa to get result.

 One may ask how do we calculate the mantissa. Answer is simple.
We just take highest 8 bits of number X [highest 8 bits does not
mean higher byte!]. Then we make one lookup to SQRT table which
is built this way:

for i = 0 to 255

  SQRT [i] = 256 * sqrt (i / 256)

 Few more questions appear. Is 8 bits enough? Is square root
stored with enough precision? Yes. Since we are calculating
square root of 16-bit number, result will be surely 8-bit. In
addition, sqrt (M) is even bigger number than M [but still lies
in interval <0, 1>]. Smart reader can also ask: "What if N will
be odd? How we half it then? ". Again, solution is simple. We
will take nearest higher value as N. For example:

24 = 2 ^ 5 * 0.75 => 2 ^ 6 * 0.375

 That's all about basis of this method. Now we can start
developing routine. The main trick is to put highest 8 bits of X
into one byte to take mantissa [as these bits can lie partly in
higher byte and partly in lower byte of X]. I solved this
problem by set of tables. First table shifts higher byte of X
into leftmost position. Few examples should make it clear:

01 -> 10 ... shifted left 4 times

                ^
00110101 -> 11010100 ... shifted left 2 times

                  ^ ^
0110 -> 110 ... shifted left 5 times

               ^
 Now we need just to add few highest bits from lower byte of X
at highlighted positions. This is done by set of masking tables
[There are eight of them; each will return 0-7 highest bits for
given number]. Let me show some examples:

Table 3

1 - "0111 ... three highest bits
10100110 -> 0101
^ ^ ^
Table 5

1 - "0001 ... five highest bits
10011010 -> 00010011
^

 This table set can be also used to get final N / 2 highest bits
from mantissa. Of course, we must somehow remember how many bits
we shifted left, because we must know how many bits to add from
lower byte. This is where next table come from. For each shifted
number it contains pointer to related masking table. I am not
going to confuse you with more explanations, so look at the
final code:

BC - number to proccess

 ld h, SHIFT; we are going to shift higher byte of X
 ld l, b
 ld a, (hl); shift it!
 inc h
 ld b, (hl); get pointer to N / 2 masking table
 inc h
 ld h, (hl); get pointer to masking table
 ld l, c
 or (hl); add lower bits [calculate the mantissa]
 ld h, SQRT
 ld l, a
 ld c, (hl); get square root of mantissa
 ld a, (bc); get N / 2 highest bits from mantissa

 Summa summarum, 76T and just 3K of tables. Would you ever say
that this linear piece of code can perform square root? Of
course, some extremists [those like me] can save another 3T by
changing "ld h, SQRT" to "inc h" and having SQRT table stored in
memory several times. It is upon you to decide if 3T are worth
of additional 2K. Oh! I forgot table generator, so you will have
to make it by yourself. It will help you to understand this
method better and maybe you will find something even more
usable. Happy coding!


  -Baze-3SC->





Other articles:

AOSS - "The scene is sick" experiences Random'a.

AOSS - Raver discusses stsenovoy journalism.

AOSS - an analytical article on the music scene from Andy Fer.

AOSS - how easy is it to be the organizer of the group?

AOSS - On journals (thinking aloud).

AOSS - on the example of the canons of demoscene magazine Deja Vu # 9.

AOSS - Today and Tomorrow domestic demoscene.

AOSS - Spectrum banner network.

Charts - all time favorites.

Charts - current rules (fall edition 1999).

Charts - indexed.

Charts - voting rules.

Coding - 16-bit procedure is fast exponentiation.

Coding - Flying is makrobiblioteku: Memory Management Library.

Coding - Texture Mapping - Implementation of SaiR00S/EI.

Coding - Texture mapping + Phong shading implementation of the Senat / Eternity Industry.

Coding - ZXA library: a library for creating and playing animations.

Coding - A bug in the STS?

Coding - Comments to the sources, published in Scenergy # 1

Coding - the libraries of programming on the Spectrum.

Coding - The principle of packing animations in the demo JAM.

Coding - procedure for rapid multiplication.

Coding - parsing intro Daingy from Cryss / Razzlers.

Demo Party - Cafe'2000: Official invitation

Demo Party - CC999.999 information (eng).

Demo Party - D-Man/EI: Report on Di: Halt: 99.

Demo Party - Hartman: report CC'999.

Demo Party - Maxwell and Mr. John: report CC'999.

Demo Party - Merlin / CC: Report CC'999.

Demo Party - Paradox'99 - as it was, but it would be better if he mUst dIe!!!

Demo Party - PHAT'9: list of visitors.

Demo Party - POL / PHT: report Doxycon '99.

Demo Party - Random / CC: volumetric report CC'999.

Demo Party - SerzhSoft: Legend of the CC'999.

Demo Party - Zlincon 2e3 party: minireportazh.

Demo Party - information about the upcoming party PHAT'0.

Demo Party - Information on demoparti CC999.999.

Demo Party - unofficial results Di: Halt'99 with comments Diver'a.

Demo Party - an overview of demoscene 1999.

Demo Party - report the organizers CAFe'99.

Demo Party - Press release Latvian demopati PHAT'9.

Demo Party - an invitation to Latvia demopati PHAT'9.

Demo Party - a story about a trip to Kazan on Antares CAFe'99

Demo Party - Results CC.999.999

Demo Party - Results CC999.999.

Demo Party - the results of Chaos Construction 999.

Demo Party - Results Computer Art Festival 1999.

Demo Party - Results Doxycon'99.

Demo Party - Results Millenium Party.

Demo Party - Results Paradox'2k demoparty.

Demo Party - Results of the Latvian demopati PHAT'9.

Demo Party - the results of the Rostov party Paradox'99.

Demo Party - reportage Gasman'a with Forever 2e3.

Demo Party - a report from Minsk demopati Millennium'2000.

Demo Party - final results Forever 2E3.

Editorial - Opening remarks by Arty.

Editorial - vystupitelnoe word from Random.

Editorial - pens Raver'a entitled "Scenes."

Groups - survey of operating groups: Amaltiya Incoropration Software.

Groups - survey of operating groups: Antares.

Groups - survey of operating groups: Ascendancy Creative Labs.

Groups - survey of operating groups: Crushers.

Groups - survey of operating groups: E-mage.

Groups - survey of operating groups: Eternity Industry.

Groups - survey of operating groups: Excess team.

Groups - survey of operating groups: Extreme Entertainment.

Groups - survey of operating groups: Fatality.

Groups - survey of operating groups: Jupiter 77.

Groups - survey of operating groups: Proxima Centauri.

Groups - survey of operating groups: RaZZLeRs.

Groups - survey of operating groups: RUSH.

Groups - survey of operating groups: Smash Hackers Band.

Illegal Corner - Razzlers justified for the release of the demo First Association.

Illegal Corner - Scenergy Release Charts - Competition crack.

Illegal Corner - Welcome to Scenergy Release Charts (SRC).

Illegal Corner - softografiya Fatality Group.

Lits - Pussy: the history of creation of the famous game from the Fatality.

Lits - Scenergized beyond the belief.

Lits - speed.

Lits - History of Education Association Rostov PartyZans.

Lits - the story of the game "White Eagle - Comrade known."

Lits - the story of how Fatality produces toys.

Mail Box - letter: Ellvis and Fatality happy Scenergy # 1, Ulterior defy Antares and BrainWave, Realtimer disappointed.

News - Doom'a will not!

News - Virtual pati Millennium, X-Raizor returned to the stage, Andrew Fer organized a new group, the failure of the German party Spectrology, news from 3SC, Zero Team, Extreme.

News - The view of Megus'a dentro compo SS'2000.

News - News from the OHG, Delta Hackers Group, Die Crupps, Volgodonsk spektrumisto and from a group SpeedWay.

Scenergy - addresses to communicate with the editors.

Scenergy - thanks to the sponsors of the magazine.

Scenergy - new in the shell of the journal.

Scenergy - the promised video articles will not ...

VIP - Random interviews Unbel! Ever / Sage / XTM.

VIP - The most noble tale of the scene.

VIP - an interview with Arny and Mythos, the creators of Elite clone game Awaken.

VIP - An interview with Fatality, widely known and crackers Game Maker

VIP - an interview with one of the authors of the game Elite.

VIP - an interview with one of the most progressive artists in the Spectrum Diver/4D.

VIP - interviews with Random'a some PC-magazine

Warez Pack - description of Inertia Player.

Warez Pack - description of the demo 1140.

Warez Pack - description of the import demo 'no work'.


Темы: Игры, Программное обеспечение, Пресса, Аппаратное обеспечение, Сеть, Демосцена, Люди, Программирование

Similar articles:
four kilobytes - Change of Chief Editor.
Smiley - a collection of humorous banalities.
A BOX part 1 - Step into a Dream: The product data, facts, concepts A BOX; Silicone dream; Features Laipitinha; Features A BOX; Operating System A BOX; development in the future.

В этот день...   23 November