1 ;;;; simple cases for generic arithmetic
3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 ;;;; addition, subtraction, and multiplication
16 (macrolet ((define-generic-arith-routine ((fun cost) &body body)
17 `(define-assembly-routine (,(symbolicate "GENERIC-" fun)
19 (:return-style :full-call)
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?
28 (:res res (descriptor-reg any-reg) edx-offset)
30 (:temp eax unsigned-reg eax-offset)
31 (:temp ecx unsigned-reg ecx-offset))
35 (inst test ecx fixnum-tag-mask) ; both fixnums?
36 (inst jmp :nz DO-STATIC-FUN) ; no - do generic
39 (inst clc) ; single-value return
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
55 ',(symbolicate "TWO-ARG-" fun))))))))
57 (define-generic-arith-routine (+ 10)
61 (inst rcr res 1) ; carry has correct sign
62 (inst sar res 1) ; remove type bits
66 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
67 (storew ecx res bignum-digits-offset other-pointer-lowtag))
71 (define-generic-arith-routine (- 10)
75 (inst cmc) ; carry has correct sign now
77 (inst sar res 1) ; remove type bits
81 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
82 (storew ecx res bignum-digits-offset other-pointer-lowtag))
85 (define-generic-arith-routine (* 30)
86 (move eax x) ; must use eax for 64-bit result
87 (inst sar eax n-fixnum-tag-bits) ; remove *4 fixnum bias
88 (inst imul y) ; result in edx:eax
89 (inst jmp :no okay) ; still fixnum
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 n-fixnum-tag-bits) ; high bits from edx
95 (inst sar x n-fixnum-tag-bits) ; now shift edx too
97 (move ecx x) ; save high bits from cdq
98 (inst cdq) ; edx:eax <- sign-extend of eax
100 (inst jmp :e SINGLE-WORD-BIGNUM)
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))
109 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
110 (storew eax res bignum-digits-offset other-pointer-lowtag))
119 (define-assembly-routine (generic-negate
121 (:return-style :full-call)
125 ((:arg x (descriptor-reg any-reg) edx-offset)
126 (:res res (descriptor-reg any-reg) edx-offset)
128 (:temp eax unsigned-reg eax-offset)
129 (:temp ecx unsigned-reg ecx-offset))
130 (inst test x fixnum-tag-mask)
135 (inst lea ebp-tn (make-ea :dword :base esp-tn :disp n-word-bytes))
136 (inst sub esp-tn (fixnumize 2))
138 (inst mov ecx (fixnumize 1)) ; arg count
139 (inst jmp (make-ea :dword
140 :disp (+ nil-value (static-fun-offset '%negate))))
144 (inst neg res) ; (- most-negative-fixnum) is BIGNUM
146 (inst shr res n-fixnum-tag-bits) ; sign bit is data - remove type bits
149 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
150 (storew ecx res bignum-digits-offset other-pointer-lowtag))
156 (macrolet ((define-cond-assem-rtn (name translate static-fn test)
158 `(define-assembly-routine (,name
159 (:return-style :none))
160 ((:arg x (descriptor-reg any-reg) edx-offset)
161 (:arg y (descriptor-reg any-reg) edi-offset)
163 (:temp ecx unsigned-reg ecx-offset))
167 (inst test ecx fixnum-tag-mask)
168 (inst jmp :nz DO-STATIC-FUN) ; are both fixnums?
175 (inst sub esp-tn (fixnumize 3))
176 (inst mov (make-ea :dword
177 :base ecx :disp (fixnumize -1))
180 (inst mov ecx (fixnumize 2))
181 (inst call (make-ea :dword
183 (static-fun-offset ',static-fn))))
184 ;; HACK: We depend on NIL having the lowest address of all
185 ;; static symbols (including T)
187 (:l `((inst mov y (1+ nil-value))
189 (:g `((inst cmp x (1+ nil-value)))))
193 (:translate ,translate)
196 (:args (x :scs (descriptor-reg any-reg) :target edx)
197 (y :scs (descriptor-reg any-reg) :target edi))
199 (:temporary (:sc unsigned-reg :offset edx-offset
202 (:temporary (:sc unsigned-reg :offset edi-offset
206 (:temporary (:sc unsigned-reg :offset ecx-offset
213 (inst lea ecx (make-ea :dword
214 :disp (make-fixup ',name :assembly-routine)))
217 (define-cond-assem-rtn generic-< < two-arg-< :l)
218 (define-cond-assem-rtn generic-> > two-arg-> :g))
221 (define-assembly-routine (generic-eql
222 (:return-style :none))
223 ((:arg x (descriptor-reg any-reg) edx-offset)
224 (:arg y (descriptor-reg any-reg) edi-offset)
226 (:temp ecx unsigned-reg ecx-offset))
229 (inst and ecx lowtag-mask)
230 (inst cmp ecx other-pointer-lowtag)
231 (inst jmp :e DO-STATIC-FUN)
233 ;; Not both other pointers
239 ;; Might as well fast path that...
244 (inst sub esp-tn (fixnumize 3))
245 (inst mov (make-ea :dword
247 :disp (fixnumize -1))
250 (inst mov ecx (fixnumize 2))
251 (inst call (make-ea :dword
252 :disp (+ nil-value (static-fun-offset 'eql))))
258 (define-vop (generic-eql)
262 (:args (x :scs (descriptor-reg any-reg) :target edx)
263 (y :scs (descriptor-reg any-reg) :target edi))
265 (:temporary (:sc unsigned-reg :offset edx-offset
268 (:temporary (:sc unsigned-reg :offset edi-offset
272 (:temporary (:sc unsigned-reg :offset ecx-offset
279 (inst lea ecx (make-ea :dword
280 :disp (make-fixup 'generic-eql :assembly-routine)))
284 (define-assembly-routine (generic-=
285 (:return-style :none))
286 ((:arg x (descriptor-reg any-reg) edx-offset)
287 (:arg y (descriptor-reg any-reg) edi-offset)
289 (:temp ecx unsigned-reg ecx-offset))
292 (inst test ecx fixnum-tag-mask)
293 (inst jmp :nz DO-STATIC-FUN)
301 (inst sub esp-tn (fixnumize 3))
302 (inst mov (make-ea :dword
304 :disp (fixnumize -1))
307 (inst mov ecx (fixnumize 2))
308 (inst call (make-ea :dword
309 :disp (+ nil-value (static-fun-offset 'two-arg-=))))
315 (define-vop (generic-=)
319 (:args (x :scs (descriptor-reg any-reg) :target edx)
320 (y :scs (descriptor-reg any-reg) :target edi))
322 (:temporary (:sc unsigned-reg :offset edx-offset
325 (:temporary (:sc unsigned-reg :offset edi-offset
329 (:temporary (:sc unsigned-reg :offset ecx-offset
336 (inst lea ecx (make-ea :dword
337 :disp (make-fixup 'generic-= :assembly-routine)))
341 ;;; Support for the Mersenne Twister, MT19937, random number generator
342 ;;; due to Matsumoto and Nishimura.
344 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
345 ;;; 623-dimensionally equidistributed uniform pseudorandom number
346 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
350 ;;; 0-1: Constant matrix A. [0, #x9908b0df] (not used here)
351 ;;; 2: Index; init. to 1.
354 ;;; This assembly routine is called from the inline VOP and updates
355 ;;; the state vector with new random numbers. The state vector is
356 ;;; passed in the EAX register.
357 #+sb-assembling ; We don't want a vop for this one.
358 (define-assembly-routine
359 (random-mt19937-update)
360 ((:temp state unsigned-reg eax-offset)
361 (:temp k unsigned-reg ebx-offset)
362 (:temp y unsigned-reg ecx-offset)
363 (:temp tmp unsigned-reg edx-offset))
365 ;; Save the temporary registers.
370 ;; Generate a new set of results.
373 (inst mov y (make-ea-for-vector-data state :index k :offset 3))
374 (inst mov tmp (make-ea-for-vector-data state :index k :offset (+ 1 3)))
375 (inst and y #x80000000)
376 (inst and tmp #x7fffffff)
380 (inst xor y #x9908b0df)
382 (inst xor y (make-ea-for-vector-data state :index k :offset (+ 397 3)))
383 (inst mov (make-ea-for-vector-data state :index k :offset 3) y)
385 (inst cmp k (- 624 397))
388 (inst mov y (make-ea-for-vector-data state :index k :offset 3))
389 (inst mov tmp (make-ea-for-vector-data state :index k :offset (+ 1 3)))
390 (inst and y #x80000000)
391 (inst and tmp #x7fffffff)
395 (inst xor y #x9908b0df)
397 (inst xor y (make-ea-for-vector-data state :index k :offset (+ (- 397 624) 3)))
398 (inst mov (make-ea-for-vector-data state :index k :offset 3) y)
400 (inst cmp k (- 624 1))
403 (inst mov y (make-ea-for-vector-data state :offset (+ (- 624 1) 3)))
404 (inst mov tmp (make-ea-for-vector-data state :offset (+ 0 3)))
405 (inst and y #x80000000)
406 (inst and tmp #x7fffffff)
410 (inst xor y #x9908b0df)
412 (inst xor y (make-ea-for-vector-data state :offset (+ (- 397 1) 3)))
413 (inst mov (make-ea-for-vector-data state :offset (+ (- 624 1) 3)) y)
415 ;; Restore the temporary registers and return.