0d3b72124143d47c7f7e5f671df59ab377e3fa2a
[sbcl.git] / src / assembly / x86 / arith.lisp
1 ;;;; simple cases for generic arithmetic
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; addition, subtraction, and multiplication
15
16 (macrolet ((define-generic-arith-routine ((fun cost) &body body)
17              `(define-assembly-routine (,(symbolicate "GENERIC-" fun)
18                                         (:cost ,cost)
19                                         (:return-style :full-call)
20                                         (:translate ,fun)
21                                         (:policy :safe)
22                                         (:save-p t))
23                 ((:arg x (descriptor-reg any-reg) edx-offset)
24                  (:arg y (descriptor-reg any-reg)
25                        ;; this seems wrong esi-offset -- FIXME: What's it mean?
26                        edi-offset)
27
28                  (:res res (descriptor-reg any-reg) edx-offset)
29
30                  (:temp eax unsigned-reg eax-offset)
31                  (:temp ecx unsigned-reg ecx-offset))
32
33                 (inst mov ecx x)
34                 (inst or ecx y)
35                 (inst test ecx 3)            ; both fixnums?
36                 (inst jmp :nz DO-STATIC-FUN) ; no - do generic
37
38                 ,@body
39                 (inst clc) ; single-value return
40                 (inst ret)
41
42                 DO-STATIC-FUN
43                 (inst pop eax)
44                 (inst push ebp-tn)
45                 (inst lea
46                       ebp-tn
47                       (make-ea :dword :base esp-tn :disp n-word-bytes))
48                 (inst sub esp-tn (fixnumize 2))
49                 (inst push eax)  ; callers return addr
50                 (inst mov ecx (fixnumize 2)) ; arg count
51                 (inst jmp
52                       (make-ea :dword
53                                :disp (+ nil-value
54                                         (static-fun-offset
55                                          ',(symbolicate "TWO-ARG-" fun))))))))
56
57   (define-generic-arith-routine (+ 10)
58     (move res x)
59     (inst add res y)
60     (inst jmp :no OKAY)
61     (inst rcr res 1)                  ; carry has correct sign
62     (inst sar res 1)                  ; remove type bits
63
64     (move ecx res)
65
66     (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
67       (storew ecx res bignum-digits-offset other-pointer-lowtag))
68
69     OKAY)
70
71   (define-generic-arith-routine (- 10)
72     (move res x)
73     (inst sub res y)
74     (inst jmp :no OKAY)
75     (inst cmc)                        ; carry has correct sign now
76     (inst rcr res 1)
77     (inst sar res 1)                  ; remove type bits
78
79     (move ecx res)
80
81     (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
82       (storew ecx res bignum-digits-offset other-pointer-lowtag))
83     OKAY)
84
85   (define-generic-arith-routine (* 30)
86     (move eax x)                          ; must use eax for 64-bit result
87     (inst sar eax 2)                  ; remove *4 fixnum bias
88     (inst imul y)                        ; result in edx:eax
89     (inst jmp :no okay)            ; still fixnum
90
91     ;; zzz jrd changed edx to ebx in here, as edx isn't listed as a temp, above
92     ;;     pfw says that loses big -- edx is target for arg x and result res
93     ;;     note that 'edx' is not defined -- using x
94     (inst shrd eax x 2)            ; high bits from edx
95     (inst sar x 2)                      ; now shift edx too
96
97     (move ecx x)                          ; save high bits from cdq
98     (inst cdq)                      ; edx:eax <- sign-extend of eax
99     (inst cmp x ecx)
100     (inst jmp :e SINGLE-WORD-BIGNUM)
101
102     (with-fixed-allocation (res bignum-widetag (+ bignum-digits-offset 2))
103       (storew eax res bignum-digits-offset other-pointer-lowtag)
104       (storew ecx res (1+ bignum-digits-offset) other-pointer-lowtag))
105     (inst jmp DONE)
106
107     SINGLE-WORD-BIGNUM
108
109     (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
110       (storew eax res bignum-digits-offset other-pointer-lowtag))
111     (inst jmp DONE)
112
113     OKAY
114     (move res eax)
115     DONE))
116 \f
117 ;;;; negation
118
119 (define-assembly-routine (generic-negate
120                           (:cost 10)
121                           (:return-style :full-call)
122                           (:policy :safe)
123                           (:translate %negate)
124                           (:save-p t))
125                          ((:arg x (descriptor-reg any-reg) edx-offset)
126                           (:res res (descriptor-reg any-reg) edx-offset)
127
128                           (:temp eax unsigned-reg eax-offset)
129                           (:temp ecx unsigned-reg ecx-offset))
130   (inst test x 3)
131   (inst jmp :z FIXNUM)
132
133   (inst pop eax)
134   (inst push ebp-tn)
135   (inst lea ebp-tn (make-ea :dword :base esp-tn :disp n-word-bytes))
136   (inst sub esp-tn (fixnumize 2))
137   (inst push eax)
138   (inst mov ecx (fixnumize 1))    ; arg count
139   (inst jmp (make-ea :dword
140                      :disp (+ nil-value (static-fun-offset '%negate))))
141
142   FIXNUM
143   (move res x)
144   (inst neg res)                        ; (- most-negative-fixnum) is BIGNUM
145   (inst jmp :no OKAY)
146   (inst shr res 2)                    ; sign bit is data - remove type bits
147   (move ecx res)
148
149   (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
150     (storew ecx res bignum-digits-offset other-pointer-lowtag))
151
152   OKAY)
153 \f
154 ;;;; comparison
155
156 (macrolet ((define-cond-assem-rtn (name translate static-fn test)
157              `(define-assembly-routine (,name
158                                         (:cost 10)
159                                         (:return-style :full-call)
160                                         (:policy :safe)
161                                         (:translate ,translate)
162                                         (:save-p t))
163                 ((:arg x (descriptor-reg any-reg) edx-offset)
164                  (:arg y (descriptor-reg any-reg) edi-offset)
165
166                  (:res res descriptor-reg edx-offset)
167
168                  (:temp eax unsigned-reg eax-offset)
169                  (:temp ecx unsigned-reg ecx-offset))
170
171                 ;; KLUDGE: The "3" here is a mask for the bits which will be
172                 ;; zero in a fixnum. It should have a symbolic name. (Actually,
173                 ;; it might already have a symbolic name which the coder
174                 ;; couldn't be bothered to use..) -- WHN 19990917
175                 (inst test x 3)
176                 (inst jmp :nz TAIL-CALL-TO-STATIC-FN)
177                 (inst test y 3)
178                 (inst jmp :z INLINE-FIXNUM-COMPARE)
179
180                 TAIL-CALL-TO-STATIC-FN
181                 (inst pop eax)
182                 (inst push ebp-tn)
183                 (inst lea ebp-tn (make-ea :dword
184                                           :base esp-tn
185                                           :disp n-word-bytes))
186                 (inst sub esp-tn (fixnumize 2)) ; FIXME: Push 2 words on stack,
187                                                 ; weirdly?
188                 (inst push eax)
189                 (inst mov ecx (fixnumize 2)) ; FIXME: FIXNUMIZE and
190                                         ; SINGLE-FLOAT-BITS are parallel,
191                                         ; should be named parallelly.
192                 (inst jmp (make-ea :dword
193                                    :disp (+ nil-value
194                                             (static-fun-offset ',static-fn))))
195
196                 INLINE-FIXNUM-COMPARE
197                 (inst cmp x y)
198                 (inst mov res nil-value)
199                 (inst jmp ,test RETURN-FALSE)
200
201                 (load-symbol res t)
202
203                 RETURN-FALSE
204                 DONE)))
205
206   (define-cond-assem-rtn generic-< < two-arg-< :ge)
207   (define-cond-assem-rtn generic-> > two-arg-> :le))
208
209 (define-assembly-routine (generic-eql
210                           (:cost 10)
211                           (:return-style :full-call)
212                           (:policy :safe)
213                           (:translate eql)
214                           (:save-p t))
215                          ((:arg x (descriptor-reg any-reg) edx-offset)
216                           (:arg y (descriptor-reg any-reg) edi-offset)
217
218                           (:res res descriptor-reg edx-offset)
219
220                           (:temp eax unsigned-reg eax-offset)
221                           (:temp ecx unsigned-reg ecx-offset))
222   (inst cmp x y)
223   (inst jmp :e RETURN-T)
224   (inst test x 3)
225   (inst jmp :z RETURN-NIL)
226   (inst test y 3)
227   (inst jmp :nz DO-STATIC-FN)
228
229   RETURN-NIL
230   (inst mov res nil-value)
231   (inst jmp DONE)
232
233   DO-STATIC-FN
234   (inst pop eax)
235   (inst push ebp-tn)
236   (inst lea ebp-tn (make-ea :dword :base esp-tn :disp n-word-bytes))
237   (inst sub esp-tn (fixnumize 2))
238   (inst push eax)
239   (inst mov ecx (fixnumize 2))
240   (inst jmp (make-ea :dword
241                      :disp (+ nil-value (static-fun-offset 'eql))))
242
243   RETURN-T
244   (load-symbol res t)
245
246   DONE)
247
248 (define-assembly-routine (generic-=
249                           (:cost 10)
250                           (:return-style :full-call)
251                           (:policy :safe)
252                           (:translate =)
253                           (:save-p t))
254                          ((:arg x (descriptor-reg any-reg) edx-offset)
255                           (:arg y (descriptor-reg any-reg) edi-offset)
256
257                           (:res res descriptor-reg edx-offset)
258
259                           (:temp eax unsigned-reg eax-offset)
260                           (:temp ecx unsigned-reg ecx-offset)
261                           )
262   (inst test x 3)                      ; descriptor?
263   (inst jmp :nz DO-STATIC-FN)          ; yes, do it here
264   (inst test y 3)                      ; descriptor?
265   (inst jmp :nz DO-STATIC-FN)
266   (inst cmp x y)
267   (inst jmp :e RETURN-T)                ; ok
268
269   (inst mov res nil-value)
270   (inst jmp DONE)
271
272   DO-STATIC-FN
273   (inst pop eax)
274   (inst push ebp-tn)
275   (inst lea ebp-tn (make-ea :dword :base esp-tn :disp n-word-bytes))
276   (inst sub esp-tn (fixnumize 2))
277   (inst push eax)
278   (inst mov ecx (fixnumize 2))
279   (inst jmp (make-ea :dword
280                      :disp (+ nil-value (static-fun-offset 'two-arg-=))))
281
282   RETURN-T
283   (load-symbol res t)
284
285   DONE)
286
287 \f
288 ;;; Support for the Mersenne Twister, MT19937, random number generator
289 ;;; due to Matsumoto and Nishimura.
290 ;;;
291 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
292 ;;; 623-dimensionally equidistributed uniform pseudorandom number
293 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
294 ;;; 1997, to appear.
295 ;;;
296 ;;; State:
297 ;;;  0-1:   Constant matrix A. [0, #x9908b0df] (not used here)
298 ;;;  2:     Index; init. to 1.
299 ;;;  3-626: State.
300
301 ;;; This assembly routine is called from the inline VOP and updates
302 ;;; the state vector with new random numbers. The state vector is
303 ;;; passed in the EAX register.
304 #+sb-assembling ; We don't want a vop for this one.
305 (define-assembly-routine
306     (random-mt19937-update)
307     ((:temp state unsigned-reg eax-offset)
308      (:temp k unsigned-reg ebx-offset)
309      (:temp y unsigned-reg ecx-offset)
310      (:temp tmp unsigned-reg edx-offset))
311
312   ;; Save the temporary registers.
313   (inst push k)
314   (inst push y)
315   (inst push tmp)
316
317   ;; Generate a new set of results.
318   (inst xor k k)
319   LOOP1
320   (inst mov y (make-ea-for-vector-data state :index k :offset 3))
321   (inst mov tmp (make-ea-for-vector-data state :index k :offset (+ 1 3)))
322   (inst and y #x80000000)
323   (inst and tmp #x7fffffff)
324   (inst or y tmp)
325   (inst shr y 1)
326   (inst jmp :nc skip1)
327   (inst xor y #x9908b0df)
328   SKIP1
329   (inst xor y (make-ea-for-vector-data state :index k :offset (+ 397 3)))
330   (inst mov (make-ea-for-vector-data state :index k :offset 3) y)
331   (inst inc k)
332   (inst cmp k (- 624 397))
333   (inst jmp :b loop1)
334   LOOP2
335   (inst mov y (make-ea-for-vector-data state :index k :offset 3))
336   (inst mov tmp (make-ea-for-vector-data state :index k :offset (+ 1 3)))
337   (inst and y #x80000000)
338   (inst and tmp #x7fffffff)
339   (inst or y tmp)
340   (inst shr y 1)
341   (inst jmp :nc skip2)
342   (inst xor y #x9908b0df)
343   SKIP2
344   (inst xor y (make-ea-for-vector-data state :index k :offset (+ (- 397 624) 3)))
345   (inst mov (make-ea-for-vector-data state :index k :offset 3) y)
346   (inst inc k)
347   (inst cmp k (- 624 1))
348   (inst jmp :b loop2)
349
350   (inst mov y (make-ea-for-vector-data state :offset (+ (- 624 1) 3)))
351   (inst mov tmp (make-ea-for-vector-data state :offset (+ 0 3)))
352   (inst and y #x80000000)
353   (inst and tmp #x7fffffff)
354   (inst or y tmp)
355   (inst shr y 1)
356   (inst jmp :nc skip3)
357   (inst xor y #x9908b0df)
358   SKIP3
359   (inst xor y (make-ea-for-vector-data state :offset (+ (- 397 1) 3)))
360   (inst mov (make-ea-for-vector-data state :offset (+ (- 624 1) 3)) y)
361
362   ;; Restore the temporary registers and return.
363   (inst pop tmp)
364   (inst pop y)
365   (inst pop k)
366   (inst ret))