base:kernal_floating_point_mathematics

This shows you the differences between two versions of the page.

Both sides previous revision Previous revision Next revision | Previous revision | ||

base:kernal_floating_point_mathematics [2018-03-20 23:36] egonolsen [Faster Square Root] |
base:kernal_floating_point_mathematics [2020-04-22 06:06] (current) robin_8bst [7.3 Integer to FP] |
||
---|---|---|---|

Line 30: | Line 30: | ||

**$69** holds the exponent\\ | **$69** holds the exponent\\ | ||

**$6a-$6d** holds the mantissa\\ | **$6a-$6d** holds the mantissa\\ | ||

- | **$63** holds the sign in bit 7\\ | + | **$6e** holds the sign in bit 7\\ |

Line 41: | Line 41: | ||

**$68** is used during normalisation, and also when converting a FP number to an integer (two similar operations). | **$68** is used during normalisation, and also when converting a FP number to an integer (two similar operations). | ||

- | **$6f** holds the sign result of arithmetic operations. This byte is set when FAC2 is loaded, not when FAC1 is loaded. FAC2 should be loaded after FAC1 before addition, or multiplication. Division does its own compare, so copying FAC1 into FAC2 and then loading FAC1 does not produce erroneous signs. Subtraction negates FAC1 and then compares, setting this byte. | + | **$6f** holds the sign result of arithmetic operations. This byte is set when FAC2 is loaded, not when FAC1 is loaded. FAC2 should be loaded after FAC1 before addition, or multiplication. Subtraction negates FAC1 and then compares, setting this byte. Unless called at FDIVT ($bb12), division does its own compare, so copying FAC1 into FAC2 and then loading FAC1 does not produce erroneous signs. When FDIVT is used, value of this byte may contain incorrect sign, leading to errors in later operations. Please note that this address is used also by other routines – as string pointer for example – and therefore should be treated as volatile, even if no FP routines are invoked after its value is established. |

**$70** is the low order rounding byte for FAC1, essentially an extension of the mantissa. It is cleared whenever a variable is loaded into FAC1. Storing 0 here after obtaining a result can speed execution (at the cost of accuracy) by avoiding the rounding routines. | **$70** is the low order rounding byte for FAC1, essentially an extension of the mantissa. It is cleared whenever a variable is loaded into FAC1. Storing 0 here after obtaining a result can speed execution (at the cost of accuracy) by avoiding the rounding routines. | ||

Line 68: | Line 68: | ||

---- | ---- | ||

- | |||

====== The Floating Point Routines ====== | ====== The Floating Point Routines ====== | ||

Line 130: | Line 129: | ||

**$bb0f** = Divide the memory contents pointed to by A/Y (low/high) by FAC1.\\ | **$bb0f** = Divide the memory contents pointed to by A/Y (low/high) by FAC1.\\ | ||

- | **$bb12** = Entry if FAC2 already loaded. Accumulator must load FAC1 exponent ($61) beforehand to set the zero flag. | + | **$bb12** = Entry if FAC2 already loaded. Accumulator must load FAC1 exponent ($61) beforehand to set the zero flag. Sign comparison is not performed and ARISGN byte at $6f is not set, which has to be accounted for when using this entry point. Hard to debug sign errors may occur otherwise. |

Divides FAC2 by FAC1, leaving the quotient in FAC1, and the remainder in FAC2. | Divides FAC2 by FAC1, leaving the quotient in FAC1, and the remainder in FAC2. | ||

Line 240: | Line 239: | ||

The first routine is the actual code. The other entries simply set up $62/$63 for you. This routine wrecks FAC2, so if it is needed, it should be loaded after the conversion. It also uselessly (from our ML standpoint) sets the data type flag at $0d. Skipping these routines and jumping straight to the normalization routine at **$b8d2** gives greater control, i.e. 24-bit and 32-bit integer inputs are possible, as are signed 8-bit inputs. For example, to convert a 24-bit number, load FAC1 with the number from the most-significant byte to the least. Store zero in the last mantissa byte, and also in the sign and rounding bytes ($66 and $70). Then set the exponent to 24 (24 + $80 = $98). Jump to $b8d2 with the carry indicating whether the result should be positive (set) or negative (clear). | The first routine is the actual code. The other entries simply set up $62/$63 for you. This routine wrecks FAC2, so if it is needed, it should be loaded after the conversion. It also uselessly (from our ML standpoint) sets the data type flag at $0d. Skipping these routines and jumping straight to the normalization routine at **$b8d2** gives greater control, i.e. 24-bit and 32-bit integer inputs are possible, as are signed 8-bit inputs. For example, to convert a 24-bit number, load FAC1 with the number from the most-significant byte to the least. Store zero in the last mantissa byte, and also in the sign and rounding bytes ($66 and $70). Then set the exponent to 24 (24 + $80 = $98). Jump to $b8d2 with the carry indicating whether the result should be positive (set) or negative (clear). | ||

- | The above process is basically what the KERNAL authors did to implement the 24 bit TI variable in BASIC (though they zero the high byte instead of the low, and hence use a mantissa of $a0). | + | The above process is basically what the KERNAL authors did to implement the 24 bit TI variable in BASIC (though they zero the high byte instead of the low, and hence use an exponent of $a0). |

You can take advantage of their work with the following pair of calls to convert a 24 bit unsigned integer held in YXA: | You can take advantage of their work with the following pair of calls to convert a 24 bit unsigned integer held in YXA: |

base/kernal_floating_point_mathematics.1521585397.txt.gz · Last modified: 2018-03-20 23:36 by egonolsen