Thursday, August 23, 2018

A little magic

The square root function I was planning on adding to a future ROM patch for the CoCo or MC-10 comes from the fast inverse square root code used in Quake.  It seems to be the fastest version that maintains accuracy.  There is just one little problem with porting the code, it depends on a "magic constant".   This constant will not work with the BASIC ROM as is.  Since it appears to be treated like a floating point number adjustment, the first thing to try is to simply convert the number from single precision to the packed floating point number format used by Microsoft BASIC.  There is no guarantee this will work. 

Here is the breakdown and resulting Magic Constant for BASIC... maybe.

5F3759DF - magic constant
0  - sign
10111110  - exponent
01101110101100111011111  - mantissa

10111110001101110101100111011111 - BASIC packed format
BE3759DF - BASIC magic constant in hex


*edit*
Please be aware that even if this does work for these machines, it may not work with a version of Microsoft BASIC using lower precision.  That would probably just require truncating the mantissa, but it may result in an unacceptable amount of error.

No comments:

Post a Comment