| 1 |
;ACME 0.96.4 |
| 2 |
|
| 3 |
!ifdef lib_cbm_flpt_a !eof |
| 4 |
lib_cbm_flpt_a = 1 |
| 5 |
|
| 6 |
; CAUTION! The Commodore BASIC interpreter uses two different formats for |
| 7 |
; handling floating-point values, so do not confuse them: |
| 8 |
; The "float registers" fac1 and fac2 (actually structures in zero page) use a |
| 9 |
; six-byte format commonly known as "flpt" (floating point). |
| 10 |
; When storing values in variables (or reading values from ROM), a compressed |
| 11 |
; five-byte format is used, commonly known as "mflpt" (memory floating point). |
| 12 |
|
| 13 |
; This file contains a macro for writing floating point numbers in the six-byte |
| 14 |
; "flpt" format, where the sign bit occupies the sixth byte. |
| 15 |
; There are no interpreter functions to use this format, so you will have to |
| 16 |
; write your own functions for "copy-mem-to-fac1", "copy-fac2-to-mem" etc. |
| 17 |
|
| 18 |
; Use the macro like this: |
| 19 |
; +flpt 3.1415926 ; each use will take up six bytes of memory |
| 20 |
|
| 21 |
|
| 22 |
; now for the technical stuff (stop reading right now if you value your sanity) |
| 23 |
|
| 24 |
; six-byte layout in memory: |
| 25 |
; eeeeeeee 1mmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm sxxxxxxx ; eight bits exponent, 32 bits mantissa with leading '1', sign byte |
| 26 |
|
| 27 |
; exponent byte: |
| 28 |
; exponent has a bias of 128 (128 means the decimal point is right before the mantissa's leading digit) |
| 29 |
; if exponent is zero, number value is considered to be zero, regardless of mantissa |
| 30 |
; exponents 1..128 are for values < 1 |
| 31 |
; exponents 129..255 are for values >= 1 |
| 32 |
|
| 33 |
; mantissa: |
| 34 |
; mantissa is stored big-endian(!) |
| 35 |
; the mantissa's leading digit is always '1' (unless the whole value represents zero) |
| 36 |
|
| 37 |
; sign byte: |
| 38 |
; most significant bit is sign: 0 means positive number, 1 means negative number |
| 39 |
; the seven lower bits are unused |
| 40 |
|
| 41 |
; so logically, this is equivalent to: |
| 42 |
; + .1mmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm * 2^(eeeeeeee - 128) if sign bit is 0 |
| 43 |
; - .1mmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm * 2^(eeeeeeee - 128) if sign bit is 1 |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
; this is ugly, but it gets the job done |
| 48 |
; (if it's stupid, but it works, then it's not stupid) |
| 49 |
!macro flpt @value { |
| 50 |
!set @float = float(@value) |
| 51 |
!ifndef @float { |
| 52 |
!by <@float, $ff, $ff, $ff, $ff, $ff ; six place holder bytes |
| 53 |
; (first one depends on @float just to make sure more passes are done until value is defined) |
| 54 |
} else { |
| 55 |
; value is defined, so split up into sign and non-negative value |
| 56 |
!if @float < 0 { |
| 57 |
!set @sign = $80 |
| 58 |
!set @float = -@float |
| 59 |
} else { |
| 60 |
!set @sign = $00 |
| 61 |
} |
| 62 |
!if @float = 0 { |
| 63 |
!by 0, 0, 0, 0, 0, 0 ; six zeroes (zero is represented by all bits zero) |
| 64 |
} else { |
| 65 |
; split up into exponent and mantissa |
| 66 |
!set @exponent = 128 + 32 ; 128 is cbm's bias, 32 is this algo's bias |
| 67 |
; if mantissa is too large, shift right and adjust exponent |
| 68 |
!do while @float >= (2.0 ^ 32.0) { |
| 69 |
!set @float = @float >> 1 |
| 70 |
!set @exponent = @exponent + 1 |
| 71 |
} |
| 72 |
; if mantissa is too small, shift left and adjust exponent |
| 73 |
!do while @float < (2.0 ^ 31.0) { |
| 74 |
!set @float = @float << 1 |
| 75 |
!set @exponent = @exponent - 1 |
| 76 |
} |
| 77 |
!if @exponent < 1 { |
| 78 |
!warn "FLPT underflow, using zero instead" |
| 79 |
!set @float = 0 |
| 80 |
!set @exponent = 0 |
| 81 |
!set @sign = 0 |
| 82 |
} |
| 83 |
!if @exponent > 255 { |
| 84 |
!error "FLPT overflow" |
| 85 |
} |
| 86 |
!by @exponent |
| 87 |
!by 255 & int(@float >> 24) |
| 88 |
!by 255 & int(@float >> 16) |
| 89 |
!by 255 & int(@float >> 8) |
| 90 |
!by 255 & int(@float) |
| 91 |
!by @sign |
| 92 |
} |
| 93 |
} |
| 94 |
} |