dsforth: moved various word groups to separate includes
[urasm.git] / dsforth / math_misc.zas
blob780e152e8fd6fe41c9a3ee9dc629b558b87d8398
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; misc math words
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 $FORTH_CODE_WORD 1+
6 ;; k8
7   pop   hl
8   inc   hl
9   jp    i_pushhl
10 $FORTH_END_CODE_WORD 1+
12 $FORTH_CODE_WORD 2+
13 ;; k8
14   pop   hl
15   inc   hl
16   inc   hl
17   jp    i_pushhl
18 $FORTH_END_CODE_WORD 2+
20 $FORTH_CODE_WORD 4+
21 ;; k8
22   pop   hl
23   inc   hl
24   inc   hl
25   inc   hl
26   inc   hl
27   jp    i_pushhl
28 $FORTH_END_CODE_WORD 4+
30 $FORTH_CODE_WORD 1-
31 ;; k8
32   pop   hl
33   dec   hl
34   jp    i_pushhl
35 $FORTH_END_CODE_WORD 1-
37 $FORTH_CODE_WORD 2-
38 ;; k8
39   pop   hl
40   dec   hl
41   dec   hl
42   jp    i_pushhl
43 $FORTH_END_CODE_WORD 2-
45 $FORTH_CODE_WORD 4-
46 ;; k8
47   pop   hl
48   dec   hl
49   dec   hl
50   dec   hl
51   dec   hl
52   jp    i_pushhl
53 $FORTH_END_CODE_WORD 4-
55 $FORTH_CODE_WORD 256U*
56 ;; k8
57 ;; ( n -- n*256u )
58   pop   hl
59   ld    h,l
60   ld    l,0
61   jp    i_pushhl
62 $FORTH_END_CODE_WORD 256U*
64 $FORTH_CODE_WORD 256U/
65 ;; k8
66 ;; ( n -- n/256u )
67   pop   hl
68   ld    l,h
69   ld    h,0
70   jp    i_pushhl
71 $FORTH_END_CODE_WORD 256U/
73 $FORTH_CODE_WORD 2U/
74 ;; k8
75 ;; ( n -- n/2 )
76   pop   hl
77   or    a
78   rr    h
79   rr    l
80   jp    i_pushhl
81 $FORTH_END_CODE_WORD 2U/
83 $FORTH_CODE_WORD 2U*
84 ;; k8
85 ;; ( n -- n*2 )
86   pop   hl
87   or    a
88   rl    l
89   rl    h
90   jp    i_pushhl
91 $FORTH_END_CODE_WORD 2U*
93 $FORTH_CODE_WORD 2UMOD
94 ;; k8
95 ;; ( n -- n%2 )
96   pop   hl
97   ld    a,l
98   and   #01
99   ld    l,a
100   ld    h,0
101   jp    i_pushhl
102 $FORTH_END_CODE_WORD 2UMOD
104 $FORTH_CODE_WORD 256UMOD
105 ;; k8
106 ;; ( n -- n%256u )
107   pop   hl
108   ld    h,0
109   jp    i_pushhl
110 $FORTH_END_CODE_WORD 256UMOD
112 $FORTH_CODE_WORD USQRT
113 ;; k8
114 ;; ( 16 -- 8 )
115   pop   hl
116   push  bc
117   call  sqrt16_hl
118   pop   bc
119   ld    l,a
120   ld    h,0
121   jp    i_pushhl
122 $FORTH_END_CODE_WORD USQRT
124 $FORTH_CODE_WORD ISGN
125 ;; k8
126 ;; ( 16 -- -1 or 0 or 1 )
127   pop   hl
128   bit   7,h
129   jr    nz,isgn_negative
130   ld    a,h
131   or    l
132   jp    z,i_pushhl  ;; zero
133   ;; positive
134   ld    hl,1
135   jp    i_pushhl
136 isgn_negative:
137   ld    hl,0xffff
138   jp    i_pushhl
139 $FORTH_END_CODE_WORD ISGN
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143 ;; fast 16-bit integer square root
144 ;; 92 bytes, 344-379 cycles (average 362)
145 ;; v2 - 3 t-state optimization spotted by Russ McNulty
146 ;; http://www.retroprogramming.com/2017/07/a-fast-z80-integer-square-root.html
147 ;; k8: NOT TESTED YET!
149 ;; IN:
150 ;;   HL: number
151 ;; OUT:
152 ;;   A: square root
153 ;;   HL: dead
154 ;;   DE: dead
155 ;;   F: dead
156 sqrt16_hl:
157   ld    a,h
158   ld    de,0B0C0h
159   add   a,e
160   jr    c,.sq7
161   ld    a,h
162   ld    d,0F0h
163 .sq7:
164 ; ----------
165   add   a,d
166   jr    nc,.sq6
167   res   5,d
168   db    254
169 .sq6:
170   sub   d
171   sra   d
172 ; ----------
173   set   2,d
174   add   a,d
175   jr    nc,.sq5
176   res   3,d
177   db    254
178 .sq5:
179   sub   d
180   sra   d
181 ; ----------
182   inc   d
183   add   a,d
184   jr    nc,.sq4
185   res   1,d
186   db    254
187 .sq4:
188   sub   d
189   sra   d
190   ld    h,a
191 ; ----------
192   add   hl,de
193   jr    nc,.sq3
194   ld    e,040h
195   db    210
196 .sq3:
197   sbc   hl,de
198   sra   d
199   ld    a,e
200   rra
201 ; ----------
202   or    010h
203   ld    e,a
204   add   hl,de
205   jr    nc,.sq2
206   and   0DFh
207   db    218
208 .sq2:
209   sbc   hl,de
210   sra   d
211   rra
212 ; ----------
213   or    04h
214   ld    e,a
215   add   hl,de
216   jr    nc,.sq1
217   and   0F7h
218   db    218
219 .sq1:
220   sbc   hl,de
221   sra   d
222   rra
223 ; ----------
224   inc   a
225   ld    e,a
226   add   hl,de
227   jr    nc,.sq0
228   and   0FDh
229 .sq0:
230   sra   d
231   rra
232   cpl
233   ret