Mercurial > hg > index.cgi
annotate src/number.s @ 80:bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
This commit has implementations for floating point add, subtract, multiply,
and divide, along with 32 bit signed integer equivalents. These can probably
be optimized and they are untested.
author | William Astle <lost@l-w.ca> |
---|---|
date | Sat, 07 Oct 2023 02:56:59 -0600 |
parents | df86e6d64ce2 |
children | f959c92bc329 |
rev | line source |
---|---|
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
1 *pragmapush list |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
2 *pragma list |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
4 ; Arithmetic package |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
5 ; |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
6 ; This section contains routines that handle floating point and integer arithmetic. It mostly delegates to int.s and |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
7 ; fps.s. |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
8 ; |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
9 ; Most routines take a pointer to a value accumulator in X. Some take two pointers with the second in U. |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
11 ; Match operands for a numeric calculation. This works as follows: |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
12 ; |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
13 ; * If both operands are the same, ensure the type is numeric and return |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
14 ; * If one operand is floating point, convert the other to floating point, as long as it is numeric |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
15 ; * If one or both operands are not numeric, raise a type mismatch |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
16 ; The operands are in (X) and (U) |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
17 val_matchtypes ldb val.type,x ; get the type of first argument |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
18 cmpb #valtype_int ; is it integer? |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
19 beq val_matchtypes0 ; brif so |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
20 cmpb #valtype_float ; is it floating point? |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
21 beq val_matchtypes1 ; brif so |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
22 TMERROR ldb #err_tm ; raise a type mismatch |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
23 jmp ERROR |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
24 val_matchtypes0 ldb val.type,u ; get type of second operand |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
25 cmpb #valtype_int ; is it integer? |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
26 bne val_matchtypes2 ; brif not |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
27 val_matchtypes3 rts ; both types int - we're good so return |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
28 val_matchtypes2 cmpb #valtype_float ; is it floating point? |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
29 bne TMERROR ; brif not - raise error |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
30 pshs x ; save X which may be clobbered |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
31 leay ,x ; point to input operand as destination for conversion |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
32 jsr fps_fromint32 ; convert first argument to floating point |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
33 puls x,pc ; restore second operand pointer and return |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
34 val_matchtypes1 ldb val.type,u ; get second argument type |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
35 cmpb #valtype_float ; is it floating point? |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
36 beq val_matchtypes3 ; brif so - we're good |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
37 cmpb #valtype_int ; is it integer? |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
38 bne TMERROR ; brif not - invalid type combination |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
39 pshs x ; save X which mill be clobbered |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
40 leax ,u ; convert (U) to floating point |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
41 leay ,u |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
42 jsr fps_fromint32 |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
43 puls x,pc ; restore argument pointer and return |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
45 ; Addition and subtraction of values; must enter with values of matching types and the result type already set |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
46 ; to the correct type. |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
47 ; |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
48 ; Calculates (X) + (U) -> (Y) (addition) |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
49 ; Calculates (X) - (U) -> (Y) (subtraction) |
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
50 val_add ldb val.type,x ; get type of left operand |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
51 cmpb valtype_int ; is it integer? |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
52 lbeq int32_add ; brif so - do integer addition |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
53 cmpb #valtype_float ; floating point? |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
54 lbeq fps_add ; brif so - do floating point addition |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
55 jmp TMERROR ; we have a type we don't understand |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
56 val_sub ldb val.type,x ; get type of left operand |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
57 cmpb valtype_int ; is it integer? |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
58 lbeq int32_sub ; brif so - do integer addition |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
59 cmpb #valtype_float ; floating point? |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
60 lbeq fps_sub ; brif so - do floating point addition |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
61 jmp TMERROR ; we have a type we don't understand |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
63 ; Multiplication |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
64 ; |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
65 ; Calculates (X) × (U) -> (Y) |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
66 ; |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
67 ; The result might overflow the integer type. In this case, an actual overflow error will occur. |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
68 val_mul ldb val.type,x ; get type of left operand |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
69 cmpb #valtype_int ; integer? |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
70 lbeq int32_mul ; brif so - do integer multiplication |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
71 cmpb #valtype_float ; is it float? |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
72 lbeq fps_mul ; brif so - do floating point multiplication |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
73 jmp TMERROR ; have an unhandled type - bail on it |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
74 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
75 ; Division |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
76 ; |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
77 ; Calculates (X) ÷ (U) -> (Y) |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
78 ; |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
79 ; The integer operation simply truncates the result ("rounds toward zero") |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
80 val_div ldb val.type,x ; get type of left operand |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
81 cmpb #valtype_int ; integer? |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
82 lbeq int32_div ; brif so - do integerdivision |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
83 cmpb #valtype_float ; floating point? |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
84 lbeq fps_div ; brif so - do floating point division |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
85 jmp TMERROR ; unsupported type |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
86 if 0 |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
87 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
80
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
88 ; Modulus - note that this is a division operator returning effectively the remainder, not an absolute value as is |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
89 ; sometimes meant by "modulus". |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
90 ; |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
91 ; Calculates (X) <MOD> (U) -> (Y) |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
92 ; |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
93 ; Note: modulus will have the same sign as the quotient so that (U) * [(X) / (U)] + [(X) MOD (U)] gives (X) (integer) |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
94 ; Note2: the modulus can be calculated on floating point values in which case it will represent the fraction part |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
95 ; of the quotient multiplied by the divisor, again with the same sign as the quotient |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
96 val_mod ldb val.type,x ; get type of left operand |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
97 cmpb #valtype_int ; integer? |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
98 lbeq int32_mod ; do integer modulus |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
99 cmpb #valtype_float ; floating point? |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
100 lbeq fps_mod ; floating point modulus |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
101 jmp TMERROR ; unsupported type |
bb50ac9fdf37
Checkpoint with very basic integer and floating point arithmetic, untested
William Astle <lost@l-w.ca>
parents:
79
diff
changeset
|
102 endc |
76
eb2681108660
Split some code into separate files for easier management (4)
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
103 *pragmapop list |