3 ms·
> In 320x200 mode, instead of constructing X and Y from the screen pointer DI with DIV, you can get a decent estimation with multiplying the screen pointer with
by yread 7y ago
> In 320x200 mode, instead of constructing X and Y from the screen pointer DI with DIV, you can get a decent estimation with multiplying the screen pointer with 0xCCCD and read X and Y from the 8bit registers DH (+DL as 16bit value) and DL (+AH as 16bit value). The idea is to interpret DI as a kind of 16 bit float in the range [0,1], from start to end. Multiplying this number in [0,1] with 65536 / 320 = 204,8 results in the row before the comma, and again as a kind of a float, the column after the comma. The representation 0xCCCD is the nearest rounding of 204,8 * 256 ( = 52428,8 ~ 52429 = 0xCCCD). As long as the 16 bit representations are used, there is no precision loss.
Řrřola's trick. A bit like
https://en.wikipedia.org/wiki/Fast_inverse_square_root https://en.wikipedia.org/wiki/Fast_inverse_square_root
- Firadeoclus 7y agoIt strikes me as odd to describe an 8.8 fixed point representation as "a kind of a float". That said, those demos are truly impressive.
- MisterTea 7y agoI'm sure the author is keeping the language simple and approachable while conveying the idea of "decimal number".
- Firadeoclus 7y agoTrying and not succeeding to keep the language simple. As evidenced by the sibling comment by pjc50. Fixed point numbers are at least as approachable as floating point numbers in my opinion.
- pjc50 7y agoThis explanation was so confusing that I had to write a program to get it clear in my head. https://gistpreview.github.io/?9b252f267cd1fdf9754059bb73a18487 https://gistpreview.github.io/?9b252f267cd1fdf9754059bb73a18... More clearly: DI = (y * 320) + x Multiply by 0xCCCD => (y * 0x1000040) + (x * 0xcccd) Take top byte is equivalent to divide by 0x1000000. So that gives you Y. The next lower (third) byte is then (x * 0xcccd / 0x10000) == (x * 52429 / 65536) =~ (x * 256/320). And the lower two bytes are noise.
- HellMood 7y ago(author here) you're right (about confusing), i wasn't expecting more than a few people to actually read this ;) at least i quickly repaired the float/fixed thing.
- HellMood 7y agoi included your explanation in the wiki http://www.sizecoding.org/wiki/General_Coding_Tricks#Obtaining_X_and_Y_without_DIV_.28The_Rrrola_Trick.29 http://www.sizecoding.org/wiki/General_Coding_Tricks#Obtaini...
- HellMood 7y ago(author here) I didn't know about this fast inverse trick, but i find it VERY funny that in "Memories" i use almost the same technique to create the "ocean" effect ;) http://www.sizecoding.org/wiki/Memories#Ocean_night_to_day_2 http://www.sizecoding.org/wiki/Memories#Ocean_night_to_day_2 Maybe i was inverse square rooting all the time without knowing it ^^