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) edi-offset)
26 (:res res (descriptor-reg any-reg) edx-offset)
28 (:temp eax unsigned-reg eax-offset)
29 (:temp ecx unsigned-reg ecx-offset))
33 (inst test ecx fixnum-tag-mask) ; both fixnums?
34 (inst jmp :nz DO-STATIC-FUN) ; no - do generic
37 (inst clc) ; single-value return
41 ;; Same as: (inst enter (fixnumize 1))
43 (inst mov ebp-tn esp-tn)
44 (inst sub esp-tn (fixnumize 1))
45 (inst push (make-ea :dword :base ebp-tn
46 :disp (frame-byte-offset return-pc-save-offset)))
47 (inst mov ecx (fixnumize 2)) ; arg count
52 ',(symbolicate "TWO-ARG-" fun))))))))
54 (define-generic-arith-routine (+ 10)
58 (inst rcr res 1) ; carry has correct sign
59 (inst sar res 1) ; remove type bits
63 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
64 (storew ecx res bignum-digits-offset other-pointer-lowtag))
68 (define-generic-arith-routine (- 10)
72 (inst cmc) ; carry has correct sign now
74 (inst sar res 1) ; remove type bits
78 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
79 (storew ecx res bignum-digits-offset other-pointer-lowtag))
82 (define-generic-arith-routine (* 30)
83 (move eax x) ; must use eax for 64-bit result
84 (inst sar eax n-fixnum-tag-bits) ; remove *4 fixnum bias
85 (inst imul y) ; result in edx:eax
86 (inst jmp :no OKAY) ; still fixnum
88 ;; zzz jrd changed edx to ebx in here, as edx isn't listed as a temp, above
89 ;; pfw says that loses big -- edx is target for arg x and result res
90 ;; note that 'edx' is not defined -- using x
91 (inst shrd eax x n-fixnum-tag-bits) ; high bits from edx
92 (inst sar x n-fixnum-tag-bits) ; now shift edx too
94 (move ecx x) ; save high bits from cdq
95 (inst cdq) ; edx:eax <- sign-extend of eax
97 (inst jmp :e SINGLE-WORD-BIGNUM)
99 (with-fixed-allocation (res bignum-widetag (+ bignum-digits-offset 2))
100 (storew eax res bignum-digits-offset other-pointer-lowtag)
101 (storew ecx res (1+ bignum-digits-offset) other-pointer-lowtag))
106 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
107 (storew eax res bignum-digits-offset other-pointer-lowtag))
116 (define-assembly-routine (generic-negate
118 (:return-style :full-call)
122 ((:arg x (descriptor-reg any-reg) edx-offset)
123 (:res res (descriptor-reg any-reg) edx-offset)
125 (:temp eax unsigned-reg eax-offset)
126 (:temp ecx unsigned-reg ecx-offset))
127 (inst test x fixnum-tag-mask)
131 (inst mov ebp-tn esp-tn)
132 (inst sub esp-tn (fixnumize 1))
133 (inst push (make-ea :dword :base ebp-tn
134 :disp (frame-byte-offset return-pc-save-offset)))
135 (inst mov ecx (fixnumize 1)) ; arg count
136 (inst jmp (make-ea :dword
137 :disp (+ nil-value (static-fun-offset '%negate))))
141 (inst neg res) ; (- most-negative-fixnum) is BIGNUM
143 (inst shr res n-fixnum-tag-bits) ; sign bit is data - remove type bits
146 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
147 (storew ecx res bignum-digits-offset other-pointer-lowtag))
153 (macrolet ((define-cond-assem-rtn (name translate static-fn test)
155 `(define-assembly-routine (,name
156 (:return-style :none))
157 ((:arg x (descriptor-reg any-reg) edx-offset)
158 (:arg y (descriptor-reg any-reg) edi-offset)
160 (:temp ecx unsigned-reg ecx-offset))
164 (inst test ecx fixnum-tag-mask)
165 (inst jmp :nz DO-STATIC-FUN) ; are both fixnums?
172 (inst mov ebp-tn esp-tn)
173 (inst sub esp-tn (fixnumize 3))
174 (inst mov (make-ea :dword :base esp-tn
175 :disp (frame-byte-offset
180 (inst lea ebp-tn (make-ea :dword :base esp-tn
181 :disp (frame-byte-offset
185 (inst mov ecx (fixnumize 2))
186 (inst call (make-ea :dword
188 (static-fun-offset ',static-fn))))
189 ;; HACK: We depend on NIL having the lowest address of all
190 ;; static symbols (including T)
192 (:l `((inst mov y (1+ nil-value))
194 (:g `((inst cmp x (1+ nil-value)))))
199 (:translate ,translate)
202 (:args (x :scs (descriptor-reg any-reg) :target edx)
203 (y :scs (descriptor-reg any-reg) :target edi))
205 (:temporary (:sc unsigned-reg :offset edx-offset
208 (:temporary (:sc unsigned-reg :offset edi-offset
215 (inst call (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 ;; At least one fixnum
239 ;; Might as well fast path that...
244 (inst mov ebp-tn esp-tn)
245 (inst sub esp-tn (fixnumize 3))
246 (inst mov (make-ea :dword :base esp-tn
247 :disp (frame-byte-offset
252 (inst lea ebp-tn (make-ea :dword :base esp-tn
253 :disp (frame-byte-offset
257 (inst mov ecx (fixnumize 2))
258 (inst call (make-ea :dword
259 :disp (+ nil-value (static-fun-offset 'eql))))
266 (define-vop (generic-eql)
270 (:args (x :scs (descriptor-reg any-reg) :target edx)
271 (y :scs (descriptor-reg any-reg) :target edi))
273 (:temporary (:sc unsigned-reg :offset edx-offset
276 (:temporary (:sc unsigned-reg :offset edi-offset
284 (inst call (make-fixup 'generic-eql :assembly-routine))))
287 (define-assembly-routine (generic-=
288 (:return-style :none))
289 ((:arg x (descriptor-reg any-reg) edx-offset)
290 (:arg y (descriptor-reg any-reg) edi-offset)
292 (:temp ecx unsigned-reg ecx-offset))
295 (inst test ecx fixnum-tag-mask)
296 (inst jmp :nz DO-STATIC-FUN)
304 (inst mov ebp-tn esp-tn)
305 (inst sub esp-tn (fixnumize 3))
306 (inst mov (make-ea :dword :base esp-tn
307 :disp (frame-byte-offset
312 (inst lea ebp-tn (make-ea :dword :base esp-tn
313 :disp (frame-byte-offset
317 (inst mov ecx (fixnumize 2))
318 (inst call (make-ea :dword
319 :disp (+ nil-value (static-fun-offset 'two-arg-=))))
326 (define-vop (generic-=)
330 (:args (x :scs (descriptor-reg any-reg) :target edx)
331 (y :scs (descriptor-reg any-reg) :target edi))
333 (:temporary (:sc unsigned-reg :offset edx-offset
336 (:temporary (:sc unsigned-reg :offset edi-offset
344 (inst call (make-fixup 'generic-= :assembly-routine))))
347 ;;; Support for the Mersenne Twister, MT19937, random number generator
348 ;;; due to Matsumoto and Nishimura.
350 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
351 ;;; 623-dimensionally equidistributed uniform pseudorandom number
352 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
356 ;;; 0-1: Constant matrix A. [0, #x9908b0df] (not used here)
357 ;;; 2: Index; init. to 1.
360 ;;; This assembly routine is called from the inline VOP and updates
361 ;;; the state vector with new random numbers. The state vector is
362 ;;; passed in the EAX register.
363 #+sb-assembling ; We don't want a vop for this one.
364 (define-assembly-routine
365 (random-mt19937-update)
366 ((:temp state unsigned-reg eax-offset)
367 (:temp k unsigned-reg ebx-offset)
368 (:temp y unsigned-reg ecx-offset)
369 (:temp tmp unsigned-reg edx-offset))
371 ;; Save the temporary registers.
376 ;; Generate a new set of results.
379 (inst mov y (make-ea-for-vector-data state :index k :offset 3))
380 (inst mov tmp (make-ea-for-vector-data state :index k :offset (+ 1 3)))
381 (inst and y #x80000000)
382 (inst and tmp #x7fffffff)
386 (inst xor y #x9908b0df)
388 (inst xor y (make-ea-for-vector-data state :index k :offset (+ 397 3)))
389 (inst mov (make-ea-for-vector-data state :index k :offset 3) y)
391 (inst cmp k (- 624 397))
394 (inst mov y (make-ea-for-vector-data state :index k :offset 3))
395 (inst mov tmp (make-ea-for-vector-data state :index k :offset (+ 1 3)))
396 (inst and y #x80000000)
397 (inst and tmp #x7fffffff)
401 (inst xor y #x9908b0df)
403 (inst xor y (make-ea-for-vector-data state :index k :offset (+ (- 397 624) 3)))
404 (inst mov (make-ea-for-vector-data state :index k :offset 3) y)
406 (inst cmp k (- 624 1))
409 (inst mov y (make-ea-for-vector-data state :offset (+ (- 624 1) 3)))
410 (inst mov tmp (make-ea-for-vector-data state :offset (+ 0 3)))
411 (inst and y #x80000000)
412 (inst and tmp #x7fffffff)
416 (inst xor y #x9908b0df)
418 (inst xor y (make-ea-for-vector-data state :offset (+ (- 397 1) 3)))
419 (inst mov (make-ea-for-vector-data state :offset (+ (- 624 1) 3)) y)
421 ;; Restore the temporary registers and return.