| 1 |
;ACME 0.94.5 |
| 2 |
|
| 3 |
!ifdef lib_cbm_mflpt_a !eof |
| 4 |
lib_cbm_mflpt_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 five-byte |
| 14 |
; "mflpt" format, where the sign bit is packed into the mantissa. |
| 15 |
; Several interpreter functions use this format (see <cbm/c64/float.a>). |
| 16 |
|
| 17 |
; Use the macro like this: |
| 18 |
; +mflpt 3.1415926 ; each use will take up five bytes of memory |
| 19 |
|
| 20 |
|
| 21 |
; now for the technical stuff (stop reading right now if you value your sanity) |
| 22 |
|
| 23 |
; five-byte layout in memory: |
| 24 |
; eeeeeeee smmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm ; eight bits exponent, 32 bits mantissa with sign bit overlay |
| 25 |
|
| 26 |
; exponent byte: |
| 27 |
; exponent has a bias of 128 (128 means the decimal point is right before the mantissa's leading digit) |
| 28 |
; if exponent is zero, number value is considered to be zero, regardless of mantissa |
| 29 |
; exponents 1..128 are for values < 1 |
| 30 |
; exponents 129..255 are for values >= 1 |
| 31 |
|
| 32 |
; mantissa: |
| 33 |
; mantissa is stored big-endian(!) |
| 34 |
; the mantissa's mandatory leading '1' is replaced by the sign bit |
| 35 |
|
| 36 |
; so logically, this is equivalent to: |
| 37 |
; + .1mmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm * 2^(eeeeeeee - 128) if sign bit is 0 |
| 38 |
; - .1mmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm * 2^(eeeeeeee - 128) if sign bit is 1 |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
; this is ugly, but it gets the job done |
| 43 |
; (if it's stupid, but it works, then it's not stupid) |
| 44 |
!macro mflpt @value { |
| 45 |
!set @float = float(@value) |
| 46 |
!ifndef @float { |
| 47 |
!by <@float, $ff, $ff, $ff, $ff ; five place holder bytes |
| 48 |
; (first one depends on @float just to make sure more passes are done until value is defined) |
| 49 |
} else { |
| 50 |
; value is defined, so split up into sign and non-negative value |
| 51 |
!if @float < 0 { |
| 52 |
!set @sign = $80 |
| 53 |
!set @float = -@float |
| 54 |
} else { |
| 55 |
!set @sign = $00 |
| 56 |
} |
| 57 |
!if @float = 0 { |
| 58 |
!by 0, 0, 0, 0, 0 ; five zeroes (zero is represented by all bits zero) |
| 59 |
} else { |
| 60 |
; split up into exponent and mantissa |
| 61 |
!set @exponent = 128 + 32 ; 128 is cbm's bias, 32 is this algo's bias |
| 62 |
; if mantissa is too large, shift right and adjust exponent |
| 63 |
!do while @float >= (2.0 ^ 32.0) { |
| 64 |
!set @float = @float >> 1 |
| 65 |
!set @exponent = @exponent + 1 |
| 66 |
} |
| 67 |
; if mantissa is too small, shift left and adjust exponent |
| 68 |
!do while @float < (2.0 ^ 31.0) { |
| 69 |
!set @float = @float << 1 |
| 70 |
!set @exponent = @exponent - 1 |
| 71 |
} |
| 72 |
!if @exponent < 1 { |
| 73 |
!warn "MFLPT underflow, using zero instead" |
| 74 |
!set @float = 0 |
| 75 |
!set @exponent = 0 |
| 76 |
!set @sign = 0 |
| 77 |
} |
| 78 |
!if @exponent > 255 { |
| 79 |
!error "MFLPT overflow" |
| 80 |
} |
| 81 |
!by @exponent |
| 82 |
!by (127 & int(@float >> 24)) | @sign |
| 83 |
!by 255 & int(@float >> 16) |
| 84 |
!by 255 & int(@float >> 8) |
| 85 |
!by 255 & int(@float) |
| 86 |
} |
| 87 |
} |
| 88 |
} |