88c81228f3ba125e7f733e62bc2e143befb3cf35
[sbcl.git] / src / compiler / x86 / arith.lisp
1 ;;;; the VM definition arithmetic VOPs for the x86
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 ;;;; unary operations
15
16 (define-vop (fast-safe-arith-op)
17   (:policy :fast-safe)
18   (:effects)
19   (:affected))
20
21 (define-vop (fixnum-unop fast-safe-arith-op)
22   (:args (x :scs (any-reg) :target res))
23   (:results (res :scs (any-reg)))
24   (:note "inline fixnum arithmetic")
25   (:arg-types tagged-num)
26   (:result-types tagged-num))
27
28 (define-vop (signed-unop fast-safe-arith-op)
29   (:args (x :scs (signed-reg) :target res))
30   (:results (res :scs (signed-reg)))
31   (:note "inline (signed-byte 32) arithmetic")
32   (:arg-types signed-num)
33   (:result-types signed-num))
34
35 (define-vop (fast-negate/fixnum fixnum-unop)
36   (:translate %negate)
37   (:generator 1
38     (move res x)
39     (inst neg res)))
40
41 (define-vop (fast-negate/signed signed-unop)
42   (:translate %negate)
43   (:generator 2
44     (move res x)
45     (inst neg res)))
46
47 (define-vop (fast-lognot/fixnum fixnum-unop)
48   (:translate lognot)
49   (:generator 2
50     (move res x)
51     (inst xor res (fixnumize -1))))
52
53 (define-vop (fast-lognot/signed signed-unop)
54   (:translate lognot)
55   (:generator 1
56     (move res x)
57     (inst not res)))
58 \f
59 ;;;; binary fixnum operations
60
61 ;;; Assume that any constant operand is the second arg...
62
63 (define-vop (fast-fixnum-binop fast-safe-arith-op)
64   (:args (x :target r :scs (any-reg)
65             :load-if (not (and (sc-is x control-stack)
66                                (sc-is y any-reg)
67                                (sc-is r control-stack)
68                                (location= x r))))
69          (y :scs (any-reg control-stack)))
70   (:arg-types tagged-num tagged-num)
71   (:results (r :scs (any-reg) :from (:argument 0)
72                :load-if (not (and (sc-is x control-stack)
73                                   (sc-is y any-reg)
74                                   (sc-is r control-stack)
75                                   (location= x r)))))
76   (:result-types tagged-num)
77   (:note "inline fixnum arithmetic"))
78
79 (define-vop (fast-unsigned-binop fast-safe-arith-op)
80   (:args (x :target r :scs (unsigned-reg)
81             :load-if (not (and (sc-is x unsigned-stack)
82                                (sc-is y unsigned-reg)
83                                (sc-is r unsigned-stack)
84                                (location= x r))))
85          (y :scs (unsigned-reg unsigned-stack)))
86   (:arg-types unsigned-num unsigned-num)
87   (:results (r :scs (unsigned-reg) :from (:argument 0)
88             :load-if (not (and (sc-is x unsigned-stack)
89                                (sc-is y unsigned-reg)
90                                (sc-is r unsigned-stack)
91                                (location= x r)))))
92   (:result-types unsigned-num)
93   (:note "inline (unsigned-byte 32) arithmetic"))
94
95 (define-vop (fast-signed-binop fast-safe-arith-op)
96   (:args (x :target r :scs (signed-reg)
97             :load-if (not (and (sc-is x signed-stack)
98                                (sc-is y signed-reg)
99                                (sc-is r signed-stack)
100                                (location= x r))))
101          (y :scs (signed-reg signed-stack)))
102   (:arg-types signed-num signed-num)
103   (:results (r :scs (signed-reg) :from (:argument 0)
104             :load-if (not (and (sc-is x signed-stack)
105                                (sc-is y signed-reg)
106                                (sc-is r signed-stack)
107                                (location= x r)))))
108   (:result-types signed-num)
109   (:note "inline (signed-byte 32) arithmetic"))
110
111 (define-vop (fast-fixnum-binop-c fast-safe-arith-op)
112   (:args (x :target r :scs (any-reg control-stack)))
113   (:info y)
114   (:arg-types tagged-num (:constant (signed-byte 30)))
115   (:results (r :scs (any-reg)
116                :load-if (not (location= x r))))
117   (:result-types tagged-num)
118   (:note "inline fixnum arithmetic"))
119
120 (define-vop (fast-unsigned-binop-c fast-safe-arith-op)
121   (:args (x :target r :scs (unsigned-reg unsigned-stack)))
122   (:info y)
123   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
124   (:results (r :scs (unsigned-reg)
125                :load-if (not (location= x r))))
126   (:result-types unsigned-num)
127   (:note "inline (unsigned-byte 32) arithmetic"))
128
129 (define-vop (fast-signed-binop-c fast-safe-arith-op)
130   (:args (x :target r :scs (signed-reg signed-stack)))
131   (:info y)
132   (:arg-types signed-num (:constant (signed-byte 32)))
133   (:results (r :scs (signed-reg)
134                :load-if (not (location= x r))))
135   (:result-types signed-num)
136   (:note "inline (signed-byte 32) arithmetic"))
137
138 (macrolet ((define-binop (translate untagged-penalty op)
139              `(progn
140                 (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
141                              fast-fixnum-binop)
142                   (:translate ,translate)
143                   (:generator 2
144                               (move r x)
145                               (inst ,op r y)))
146                 (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
147                              fast-fixnum-binop-c)
148                   (:translate ,translate)
149                   (:generator 1
150                   (move r x)
151                   (inst ,op r (fixnumize y))))
152                 (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
153                              fast-signed-binop)
154                   (:translate ,translate)
155                   (:generator ,(1+ untagged-penalty)
156                   (move r x)
157                   (inst ,op r y)))
158                 (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
159                              fast-signed-binop-c)
160                   (:translate ,translate)
161                   (:generator ,untagged-penalty
162                   (move r x)
163                   (inst ,op r y)))
164                 (define-vop (,(symbolicate "FAST-"
165                                            translate
166                                            "/UNSIGNED=>UNSIGNED")
167                 fast-unsigned-binop)
168                   (:translate ,translate)
169                   (:generator ,(1+ untagged-penalty)
170                   (move r x)
171                   (inst ,op r y)))
172                 (define-vop (,(symbolicate 'fast-
173                                            translate
174                                            '-c/unsigned=>unsigned)
175                              fast-unsigned-binop-c)
176                   (:translate ,translate)
177                   (:generator ,untagged-penalty
178                   (move r x)
179                   (inst ,op r y))))))
180
181   ;;(define-binop + 4 add)
182   (define-binop - 4 sub)
183   (define-binop logand 2 and)
184   (define-binop logior 2 or)
185   (define-binop logxor 2 xor))
186
187
188 ;;; Special handling of add on the x86; can use lea to avoid a
189 ;;; register load, otherwise it uses add.
190 (define-vop (fast-+/fixnum=>fixnum fast-safe-arith-op)
191   (:translate +)
192   (:args (x :scs (any-reg) :target r
193             :load-if (not (and (sc-is x control-stack)
194                                (sc-is y any-reg)
195                                (sc-is r control-stack)
196                                (location= x r))))
197          (y :scs (any-reg control-stack)))
198   (:arg-types tagged-num tagged-num)
199   (:results (r :scs (any-reg) :from (:argument 0)
200                :load-if (not (and (sc-is x control-stack)
201                                   (sc-is y any-reg)
202                                   (sc-is r control-stack)
203                                   (location= x r)))))
204   (:result-types tagged-num)
205   (:note "inline fixnum arithmetic")
206   (:generator 2
207     (cond ((and (sc-is x any-reg) (sc-is y any-reg) (sc-is r any-reg)
208                 (not (location= x r)))
209            (inst lea r (make-ea :dword :base x :index y :scale 1)))
210           (t
211            (move r x)
212            (inst add r y)))))
213
214 (define-vop (fast-+-c/fixnum=>fixnum fast-safe-arith-op)
215   (:translate +)
216   (:args (x :target r :scs (any-reg control-stack)))
217   (:info y)
218   (:arg-types tagged-num (:constant (signed-byte 30)))
219   (:results (r :scs (any-reg)
220                :load-if (not (location= x r))))
221   (:result-types tagged-num)
222   (:note "inline fixnum arithmetic")
223   (:generator 1
224     (cond ((and (sc-is x any-reg) (sc-is r any-reg) (not (location= x r)))
225            (inst lea r (make-ea :dword :base x :disp (fixnumize y))))
226           (t
227            (move r x)
228            (inst add r (fixnumize y))))))
229
230 (define-vop (fast-+/signed=>signed fast-safe-arith-op)
231   (:translate +)
232   (:args (x :scs (signed-reg) :target r
233             :load-if (not (and (sc-is x signed-stack)
234                                (sc-is y signed-reg)
235                                (sc-is r signed-stack)
236                                (location= x r))))
237          (y :scs (signed-reg signed-stack)))
238   (:arg-types signed-num signed-num)
239   (:results (r :scs (signed-reg) :from (:argument 0)
240                :load-if (not (and (sc-is x signed-stack)
241                                   (sc-is y signed-reg)
242                                   (location= x r)))))
243   (:result-types signed-num)
244   (:note "inline (signed-byte 32) arithmetic")
245   (:generator 5
246     (cond ((and (sc-is x signed-reg) (sc-is y signed-reg) (sc-is r signed-reg)
247                 (not (location= x r)))
248            (inst lea r (make-ea :dword :base x :index y :scale 1)))
249           (t
250            (move r x)
251            (inst add r y)))))
252
253
254 ;;;; Special logand cases: (logand signed unsigned) => unsigned
255
256 (define-vop (fast-logand/signed-unsigned=>unsigned
257              fast-logand/unsigned=>unsigned)
258   (:args (x :target r :scs (signed-reg)
259             :load-if (not (and (sc-is x signed-stack)
260                                (sc-is y unsigned-reg)
261                                (sc-is r unsigned-stack)
262                                (location= x r))))
263          (y :scs (unsigned-reg unsigned-stack)))
264   (:arg-types signed-num unsigned-num))
265
266 (define-vop (fast-logand-c/signed-unsigned=>unsigned
267              fast-logand-c/unsigned=>unsigned)
268   (:args (x :target r :scs (signed-reg signed-stack)))
269   (:arg-types signed-num (:constant (unsigned-byte 32))))
270
271 (define-vop (fast-logand/unsigned-signed=>unsigned
272              fast-logand/unsigned=>unsigned)
273   (:args (x :target r :scs (unsigned-reg)
274             :load-if (not (and (sc-is x unsigned-stack)
275                                (sc-is y signed-reg)
276                                (sc-is r unsigned-stack)
277                                (location= x r))))
278          (y :scs (signed-reg signed-stack)))
279   (:arg-types unsigned-num signed-num))
280 \f
281
282 (define-vop (fast-+-c/signed=>signed fast-safe-arith-op)
283   (:translate +)
284   (:args (x :target r :scs (signed-reg signed-stack)))
285   (:info y)
286   (:arg-types signed-num (:constant (signed-byte 32)))
287   (:results (r :scs (signed-reg)
288                :load-if (not (location= x r))))
289   (:result-types signed-num)
290   (:note "inline (signed-byte 32) arithmetic")
291   (:generator 4
292     (cond ((and (sc-is x signed-reg) (sc-is r signed-reg)
293                 (not (location= x r)))
294            (inst lea r (make-ea :dword :base x :disp y)))
295           (t
296            (move r x)
297            (if (= y 1)
298                (inst inc r)
299              (inst add r y))))))
300
301 (define-vop (fast-+/unsigned=>unsigned fast-safe-arith-op)
302   (:translate +)
303   (:args (x :scs (unsigned-reg) :target r
304             :load-if (not (and (sc-is x unsigned-stack)
305                                (sc-is y unsigned-reg)
306                                (sc-is r unsigned-stack)
307                                (location= x r))))
308          (y :scs (unsigned-reg unsigned-stack)))
309   (:arg-types unsigned-num unsigned-num)
310   (:results (r :scs (unsigned-reg) :from (:argument 0)
311                :load-if (not (and (sc-is x unsigned-stack)
312                                   (sc-is y unsigned-reg)
313                                   (sc-is r unsigned-stack)
314                                   (location= x r)))))
315   (:result-types unsigned-num)
316   (:note "inline (unsigned-byte 32) arithmetic")
317   (:generator 5
318     (cond ((and (sc-is x unsigned-reg) (sc-is y unsigned-reg)
319                 (sc-is r unsigned-reg) (not (location= x r)))
320            (inst lea r (make-ea :dword :base x :index y :scale 1)))
321           (t
322            (move r x)
323            (inst add r y)))))
324
325 (define-vop (fast-+-c/unsigned=>unsigned fast-safe-arith-op)
326   (:translate +)
327   (:args (x :target r :scs (unsigned-reg unsigned-stack)))
328   (:info y)
329   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
330   (:results (r :scs (unsigned-reg)
331                :load-if (not (location= x r))))
332   (:result-types unsigned-num)
333   (:note "inline (unsigned-byte 32) arithmetic")
334   (:generator 4
335     (cond ((and (sc-is x unsigned-reg) (sc-is r unsigned-reg)
336                 (not (location= x r)))
337            (inst lea r (make-ea :dword :base x :disp y)))
338           (t
339            (move r x)
340            (if (= y 1)
341                (inst inc r)
342              (inst add r y))))))
343 \f
344 ;;;; multiplication and division
345
346 (define-vop (fast-*/fixnum=>fixnum fast-safe-arith-op)
347   (:translate *)
348   ;; We need different loading characteristics.
349   (:args (x :scs (any-reg) :target r)
350          (y :scs (any-reg control-stack)))
351   (:arg-types tagged-num tagged-num)
352   (:results (r :scs (any-reg) :from (:argument 0)))
353   (:result-types tagged-num)
354   (:note "inline fixnum arithmetic")
355   (:generator 4
356     (move r x)
357     (inst sar r 2)
358     (inst imul r y)))
359
360 (define-vop (fast-*-c/fixnum=>fixnum fast-safe-arith-op)
361   (:translate *)
362   ;; We need different loading characteristics.
363   (:args (x :scs (any-reg control-stack)))
364   (:info y)
365   (:arg-types tagged-num (:constant (signed-byte 30)))
366   (:results (r :scs (any-reg)))
367   (:result-types tagged-num)
368   (:note "inline fixnum arithmetic")
369   (:generator 3
370     (inst imul r x y)))
371
372 (define-vop (fast-*/signed=>signed fast-safe-arith-op)
373   (:translate *)
374   ;; We need different loading characteristics.
375   (:args (x :scs (signed-reg) :target r)
376          (y :scs (signed-reg signed-stack)))
377   (:arg-types signed-num signed-num)
378   (:results (r :scs (signed-reg) :from (:argument 0)))
379   (:result-types signed-num)
380   (:note "inline (signed-byte 32) arithmetic")
381   (:generator 5
382     (move r x)
383     (inst imul r y)))
384
385 (define-vop (fast-*-c/signed=>signed fast-safe-arith-op)
386   (:translate *)
387   ;; We need different loading characteristics.
388   (:args (x :scs (signed-reg signed-stack)))
389   (:info y)
390   (:arg-types signed-num (:constant (signed-byte 32)))
391   (:results (r :scs (signed-reg)))
392   (:result-types signed-num)
393   (:note "inline (signed-byte 32) arithmetic")
394   (:generator 4
395     (inst imul r x y)))
396
397 (define-vop (fast-*/unsigned=>unsigned fast-safe-arith-op)
398   (:translate *)
399   (:args (x :scs (unsigned-reg) :target eax)
400          (y :scs (unsigned-reg unsigned-stack)))
401   (:arg-types unsigned-num unsigned-num)
402   (:temporary (:sc unsigned-reg :offset eax-offset :target result
403                    :from (:argument 0) :to :result) eax)
404   (:temporary (:sc unsigned-reg :offset edx-offset
405                    :from :eval :to :result) edx)
406   (:ignore edx)
407   (:results (result :scs (unsigned-reg)))
408   (:result-types unsigned-num)
409   (:note "inline (unsigned-byte 32) arithmetic")
410   (:vop-var vop)
411   (:save-p :compute-only)
412   (:generator 6
413     (move eax x)
414     (inst mul eax y)
415     (move result eax)))
416
417
418 (define-vop (fast-truncate/fixnum=>fixnum fast-safe-arith-op)
419   (:translate truncate)
420   (:args (x :scs (any-reg) :target eax)
421          (y :scs (any-reg control-stack)))
422   (:arg-types tagged-num tagged-num)
423   (:temporary (:sc signed-reg :offset eax-offset :target quo
424                    :from (:argument 0) :to (:result 0)) eax)
425   (:temporary (:sc unsigned-reg :offset edx-offset :target rem
426                    :from (:argument 0) :to (:result 1)) edx)
427   (:results (quo :scs (any-reg))
428             (rem :scs (any-reg)))
429   (:result-types tagged-num tagged-num)
430   (:note "inline fixnum arithmetic")
431   (:vop-var vop)
432   (:save-p :compute-only)
433   (:generator 31
434     (let ((zero (generate-error-code vop division-by-zero-error x y)))
435       (if (sc-is y any-reg)
436           (inst test y y)  ; smaller instruction
437           (inst cmp y 0))
438       (inst jmp :eq zero))
439     (move eax x)
440     (inst cdq)
441     (inst idiv eax y)
442     (if (location= quo eax)
443         (inst shl eax 2)
444         (inst lea quo (make-ea :dword :index eax :scale 4)))
445     (move rem edx)))
446
447 (define-vop (fast-truncate-c/fixnum=>fixnum fast-safe-arith-op)
448   (:translate truncate)
449   (:args (x :scs (any-reg) :target eax))
450   (:info y)
451   (:arg-types tagged-num (:constant (signed-byte 30)))
452   (:temporary (:sc signed-reg :offset eax-offset :target quo
453                    :from :argument :to (:result 0)) eax)
454   (:temporary (:sc any-reg :offset edx-offset :target rem
455                    :from :eval :to (:result 1)) edx)
456   (:temporary (:sc any-reg :from :eval :to :result) y-arg)
457   (:results (quo :scs (any-reg))
458             (rem :scs (any-reg)))
459   (:result-types tagged-num tagged-num)
460   (:note "inline fixnum arithmetic")
461   (:vop-var vop)
462   (:save-p :compute-only)
463   (:generator 30
464     (move eax x)
465     (inst cdq)
466     (inst mov y-arg (fixnumize y))
467     (inst idiv eax y-arg)
468     (if (location= quo eax)
469         (inst shl eax 2)
470         (inst lea quo (make-ea :dword :index eax :scale 4)))
471     (move rem edx)))
472
473 (define-vop (fast-truncate/unsigned=>unsigned fast-safe-arith-op)
474   (:translate truncate)
475   (:args (x :scs (unsigned-reg) :target eax)
476          (y :scs (unsigned-reg signed-stack)))
477   (:arg-types unsigned-num unsigned-num)
478   (:temporary (:sc unsigned-reg :offset eax-offset :target quo
479                    :from (:argument 0) :to (:result 0)) eax)
480   (:temporary (:sc unsigned-reg :offset edx-offset :target rem
481                    :from (:argument 0) :to (:result 1)) edx)
482   (:results (quo :scs (unsigned-reg))
483             (rem :scs (unsigned-reg)))
484   (:result-types unsigned-num unsigned-num)
485   (:note "inline (unsigned-byte 32) arithmetic")
486   (:vop-var vop)
487   (:save-p :compute-only)
488   (:generator 33
489     (let ((zero (generate-error-code vop division-by-zero-error x y)))
490       (if (sc-is y unsigned-reg)
491           (inst test y y)  ; smaller instruction
492           (inst cmp y 0))
493       (inst jmp :eq zero))
494     (move eax x)
495     (inst xor edx edx)
496     (inst div eax y)
497     (move quo eax)
498     (move rem edx)))
499
500 (define-vop (fast-truncate-c/unsigned=>unsigned fast-safe-arith-op)
501   (:translate truncate)
502   (:args (x :scs (unsigned-reg) :target eax))
503   (:info y)
504   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
505   (:temporary (:sc unsigned-reg :offset eax-offset :target quo
506                    :from :argument :to (:result 0)) eax)
507   (:temporary (:sc unsigned-reg :offset edx-offset :target rem
508                    :from :eval :to (:result 1)) edx)
509   (:temporary (:sc unsigned-reg :from :eval :to :result) y-arg)
510   (:results (quo :scs (unsigned-reg))
511             (rem :scs (unsigned-reg)))
512   (:result-types unsigned-num unsigned-num)
513   (:note "inline (unsigned-byte 32) arithmetic")
514   (:vop-var vop)
515   (:save-p :compute-only)
516   (:generator 32
517     (move eax x)
518     (inst xor edx edx)
519     (inst mov y-arg y)
520     (inst div eax y-arg)
521     (move quo eax)
522     (move rem edx)))
523
524 (define-vop (fast-truncate/signed=>signed fast-safe-arith-op)
525   (:translate truncate)
526   (:args (x :scs (signed-reg) :target eax)
527          (y :scs (signed-reg signed-stack)))
528   (:arg-types signed-num signed-num)
529   (:temporary (:sc signed-reg :offset eax-offset :target quo
530                    :from (:argument 0) :to (:result 0)) eax)
531   (:temporary (:sc signed-reg :offset edx-offset :target rem
532                    :from (:argument 0) :to (:result 1)) edx)
533   (:results (quo :scs (signed-reg))
534             (rem :scs (signed-reg)))
535   (:result-types signed-num signed-num)
536   (:note "inline (signed-byte 32) arithmetic")
537   (:vop-var vop)
538   (:save-p :compute-only)
539   (:generator 33
540     (let ((zero (generate-error-code vop division-by-zero-error x y)))
541       (if (sc-is y signed-reg)
542           (inst test y y)  ; smaller instruction
543           (inst cmp y 0))
544       (inst jmp :eq zero))
545     (move eax x)
546     (inst cdq)
547     (inst idiv eax y)
548     (move quo eax)
549     (move rem edx)))
550
551 (define-vop (fast-truncate-c/signed=>signed fast-safe-arith-op)
552   (:translate truncate)
553   (:args (x :scs (signed-reg) :target eax))
554   (:info y)
555   (:arg-types signed-num (:constant (signed-byte 32)))
556   (:temporary (:sc signed-reg :offset eax-offset :target quo
557                    :from :argument :to (:result 0)) eax)
558   (:temporary (:sc signed-reg :offset edx-offset :target rem
559                    :from :eval :to (:result 1)) edx)
560   (:temporary (:sc signed-reg :from :eval :to :result) y-arg)
561   (:results (quo :scs (signed-reg))
562             (rem :scs (signed-reg)))
563   (:result-types signed-num signed-num)
564   (:note "inline (signed-byte 32) arithmetic")
565   (:vop-var vop)
566   (:save-p :compute-only)
567   (:generator 32
568     (move eax x)
569     (inst cdq)
570     (inst mov y-arg y)
571     (inst idiv eax y-arg)
572     (move quo eax)
573     (move rem edx)))
574
575
576 \f
577 ;;;; Shifting
578 (define-vop (fast-ash-c/fixnum=>fixnum)
579   (:translate ash)
580   (:policy :fast-safe)
581   (:args (number :scs (any-reg) :target result
582                  :load-if (not (and (sc-is number any-reg control-stack)
583                                     (sc-is result any-reg control-stack)
584                                     (location= number result)))))
585   (:info amount)
586   (:arg-types tagged-num (:constant integer))
587   (:results (result :scs (any-reg)
588                     :load-if (not (and (sc-is number control-stack)
589                                        (sc-is result control-stack)
590                                        (location= number result)))))
591   (:result-types tagged-num)
592   (:note "inline ASH")
593   (:generator 2
594     (cond ((and (= amount 1) (not (location= number result)))
595            (inst lea result (make-ea :dword :index number :scale 2)))
596           ((and (= amount 2) (not (location= number result)))
597            (inst lea result (make-ea :dword :index number :scale 4)))
598           ((and (= amount 3) (not (location= number result)))
599            (inst lea result (make-ea :dword :index number :scale 8)))
600           (t
601            (move result number)
602            (cond ((plusp amount)
603                   ;; We don't have to worry about overflow because of the
604                   ;; result type restriction.
605                   (inst shl result amount))
606                  (t
607                   ;; If the amount is greater than 31, only shift by 31. We
608                   ;; have to do this because the shift instructions only look
609                   ;; at the low five bits of the result.
610                   (inst sar result (min 31 (- amount)))
611                   ;; Fixnum correction.
612                   (inst and result #xfffffffc)))))))
613
614 (define-vop (fast-ash-left/fixnum=>fixnum)
615   (:translate ash)
616   (:args (number :scs (any-reg) :target result
617                  :load-if (not (and (sc-is number control-stack)
618                                     (sc-is result control-stack)
619                                     (location= number result))))
620          (amount :scs (unsigned-reg) :target ecx))
621   (:arg-types tagged-num positive-fixnum)
622   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
623   (:results (result :scs (any-reg) :from (:argument 0)
624                     :load-if (not (and (sc-is number control-stack)
625                                        (sc-is result control-stack)
626                                        (location= number result)))))
627   (:result-types tagged-num)
628   (:policy :fast-safe)
629   (:note "inline ASH")
630   (:generator 3
631     (move result number)
632     (move ecx amount)
633     ;; The result-type ensures us that this shift will not overflow.
634     (inst shl result :cl)))
635
636 (define-vop (fast-ash-c/signed=>signed)
637   (:translate ash)
638   (:policy :fast-safe)
639   (:args (number :scs (signed-reg) :target result
640                  :load-if (not (and (sc-is number signed-stack)
641                                     (sc-is result signed-stack)
642                                     (location= number result)))))
643   (:info amount)
644   (:arg-types signed-num (:constant integer))
645   (:results (result :scs (signed-reg)
646                     :load-if (not (and (sc-is number signed-stack)
647                                        (sc-is result signed-stack)
648                                        (location= number result)))))
649   (:result-types signed-num)
650   (:note "inline ASH")
651   (:generator 3
652     (cond ((and (= amount 1) (not (location= number result)))
653            (inst lea result (make-ea :dword :index number :scale 2)))
654           ((and (= amount 2) (not (location= number result)))
655            (inst lea result (make-ea :dword :index number :scale 4)))
656           ((and (= amount 3) (not (location= number result)))
657            (inst lea result (make-ea :dword :index number :scale 8)))
658           (t
659            (move result number)
660            (cond ((plusp amount) (inst shl result amount))
661                  (t (inst sar result (min 31 (- amount)))))))))
662
663 (define-vop (fast-ash-c/unsigned=>unsigned)
664   (:translate ash)
665   (:policy :fast-safe)
666   (:args (number :scs (unsigned-reg) :target result
667                  :load-if (not (and (sc-is number unsigned-stack)
668                                     (sc-is result unsigned-stack)
669                                     (location= number result)))))
670   (:info amount)
671   (:arg-types unsigned-num (:constant integer))
672   (:results (result :scs (unsigned-reg)
673                     :load-if (not (and (sc-is number unsigned-stack)
674                                        (sc-is result unsigned-stack)
675                                        (location= number result)))))
676   (:result-types unsigned-num)
677   (:note "inline ASH")
678   (:generator 3
679     (cond ((and (= amount 1) (not (location= number result)))
680            (inst lea result (make-ea :dword :index number :scale 2)))
681           ((and (= amount 2) (not (location= number result)))
682            (inst lea result (make-ea :dword :index number :scale 4)))
683           ((and (= amount 3) (not (location= number result)))
684            (inst lea result (make-ea :dword :index number :scale 8)))
685           (t
686            (move result number)
687            (cond ((plusp amount) (inst shl result amount))
688                  ((< amount -31) (inst xor result result))
689                  (t (inst shr result (- amount))))))))
690
691 (define-vop (fast-ash-left/signed)
692   (:translate ash)
693   (:args (number :scs (signed-reg) :target result
694                  :load-if (not (and (sc-is number signed-stack)
695                                     (sc-is result signed-stack)
696                                     (location= number result))))
697          (amount :scs (unsigned-reg) :target ecx))
698   (:arg-types signed-num positive-fixnum)
699   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
700   (:results (result :scs (signed-reg) :from (:argument 0)
701                     :load-if (not (and (sc-is number signed-stack)
702                                        (sc-is result signed-stack)
703                                        (location= number result)))))
704   (:result-types signed-num)
705   (:policy :fast-safe)
706   (:note "inline ASH")
707   (:generator 4
708     (move result number)
709     (move ecx amount)
710     (inst shl result :cl)))
711
712 (define-vop (fast-ash-left/unsigned)
713   (:translate ash)
714   (:args (number :scs (unsigned-reg) :target result
715                  :load-if (not (and (sc-is number unsigned-stack)
716                                     (sc-is result unsigned-stack)
717                                     (location= number result))))
718          (amount :scs (unsigned-reg) :target ecx))
719   (:arg-types unsigned-num positive-fixnum)
720   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
721   (:results (result :scs (unsigned-reg) :from (:argument 0)
722                     :load-if (not (and (sc-is number unsigned-stack)
723                                        (sc-is result unsigned-stack)
724                                        (location= number result)))))
725   (:result-types unsigned-num)
726   (:policy :fast-safe)
727   (:note "inline ASH")
728   (:generator 4
729     (move result number)
730     (move ecx amount)
731     (inst shl result :cl)))
732
733 (define-vop (fast-ash/signed=>signed)
734   (:translate ash)
735   (:policy :fast-safe)
736   (:args (number :scs (signed-reg) :target result)
737          (amount :scs (signed-reg) :target ecx))
738   (:arg-types signed-num signed-num)
739   (:results (result :scs (signed-reg) :from (:argument 0)))
740   (:result-types signed-num)
741   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
742   (:note "inline ASH")
743   (:generator 5
744     (move result number)
745     (move ecx amount)
746     (inst or ecx ecx)
747     (inst jmp :ns positive)
748     (inst neg ecx)
749     (inst cmp ecx 31)
750     (inst jmp :be okay)
751     (inst mov ecx 31)
752     OKAY
753     (inst sar result :cl)
754     (inst jmp done)
755
756     POSITIVE
757     ;; The result-type ensures us that this shift will not overflow.
758     (inst shl result :cl)
759
760     DONE))
761
762 (define-vop (fast-ash/unsigned=>unsigned)
763   (:translate ash)
764   (:policy :fast-safe)
765   (:args (number :scs (unsigned-reg) :target result)
766          (amount :scs (signed-reg) :target ecx))
767   (:arg-types unsigned-num signed-num)
768   (:results (result :scs (unsigned-reg) :from (:argument 0)))
769   (:result-types unsigned-num)
770   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
771   (:note "inline ASH")
772   (:generator 5
773     (move result number)
774     (move ecx amount)
775     (inst or ecx ecx)
776     (inst jmp :ns positive)
777     (inst neg ecx)
778     (inst cmp ecx 31)
779     (inst jmp :be okay)
780     (inst xor result result)
781     (inst jmp done)
782     OKAY
783     (inst shr result :cl)
784     (inst jmp done)
785
786     POSITIVE
787     ;; The result-type ensures us that this shift will not overflow.
788     (inst shl result :cl)
789
790     DONE))
791
792 ;;; FIXME: before making knowledge of this too public, it needs to be
793 ;;; fixed so that it's actually _faster_ than the non-CMOV version; at
794 ;;; least on my Celeron-XXX laptop, this version is marginally slower
795 ;;; than the above version with branches.  -- CSR, 2003-09-04
796 (define-vop (fast-cmov-ash/unsigned=>unsigned)
797   (:translate ash)
798   (:policy :fast-safe)
799   (:args (number :scs (unsigned-reg) :target result)
800          (amount :scs (signed-reg) :target ecx))
801   (:arg-types unsigned-num signed-num)
802   (:results (result :scs (unsigned-reg) :from (:argument 0)))
803   (:result-types unsigned-num)
804   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
805   (:temporary (:sc any-reg :from (:eval 0) :to (:eval 1)) zero)
806   (:note "inline ASH")
807   (:guard (member :cmov *backend-subfeatures*))
808   (:generator 4
809     (move result number)
810     (move ecx amount)
811     (inst or ecx ecx)
812     (inst jmp :ns positive)
813     (inst neg ecx)
814     (inst xor zero zero)
815     (inst shr result :cl)
816     (inst cmp ecx 31)
817     (inst cmov :nbe result zero)
818     (inst jmp done)
819     
820     POSITIVE
821     ;; The result-type ensures us that this shift will not overflow.
822     (inst shl result :cl)
823
824     DONE))
825 \f
826 ;;; Note: documentation for this function is wrong - rtfm
827 (define-vop (signed-byte-32-len)
828   (:translate integer-length)
829   (:note "inline (signed-byte 32) integer-length")
830   (:policy :fast-safe)
831   (:args (arg :scs (signed-reg) :target res))
832   (:arg-types signed-num)
833   (:results (res :scs (unsigned-reg)))
834   (:result-types unsigned-num)
835   (:generator 28
836     (move res arg)
837     (inst cmp res 0)
838     (inst jmp :ge POS)
839     (inst not res)
840     POS
841     (inst bsr res res)
842     (inst jmp :z zero)
843     (inst inc res)
844     (inst jmp done)
845     ZERO
846     (inst xor res res)
847     DONE))
848
849 (define-vop (unsigned-byte-32-len)
850   (:translate integer-length)
851   (:note "inline (unsigned-byte 32) integer-length")
852   (:policy :fast-safe)
853   (:args (arg :scs (unsigned-reg)))
854   (:arg-types unsigned-num)
855   (:results (res :scs (unsigned-reg)))
856   (:result-types unsigned-num)
857   (:generator 26
858     (inst bsr res arg)
859     (inst jmp :z zero)
860     (inst inc res)
861     (inst jmp done)
862     ZERO
863     (inst xor res res)
864     DONE))
865
866 (define-vop (unsigned-byte-32-count)
867   (:translate logcount)
868   (:note "inline (unsigned-byte 32) logcount")
869   (:policy :fast-safe)
870   (:args (arg :scs (unsigned-reg)))
871   (:arg-types unsigned-num)
872   (:results (result :scs (unsigned-reg)))
873   (:result-types positive-fixnum)
874   (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
875   (:generator 30
876     (move result arg)
877
878     (inst mov temp result)
879     (inst shr temp 1)
880     (inst and result #x55555555)
881     (inst and temp #x55555555)
882     (inst add result temp)
883
884     (inst mov temp result)
885     (inst shr temp 2)
886     (inst and result #x33333333)
887     (inst and temp #x33333333)
888     (inst add result temp)
889
890     (inst mov temp result)
891     (inst shr temp 4)
892     (inst and result #x0f0f0f0f)
893     (inst and temp #x0f0f0f0f)
894     (inst add result temp)
895
896     (inst mov temp result)
897     (inst shr temp 8)
898     (inst and result #x00ff00ff)
899     (inst and temp #x00ff00ff)
900     (inst add result temp)
901
902     (inst mov temp result)
903     (inst shr temp 16)
904     (inst and result #x0000ffff)
905     (inst and temp #x0000ffff)
906     (inst add result temp)))
907 \f
908 ;;;; binary conditional VOPs
909
910 (define-vop (fast-conditional)
911   (:conditional)
912   (:info target not-p)
913   (:effects)
914   (:affected)
915   (:policy :fast-safe))
916
917 (define-vop (fast-conditional/fixnum fast-conditional)
918   (:args (x :scs (any-reg)
919             :load-if (not (and (sc-is x control-stack)
920                                (sc-is y any-reg))))
921          (y :scs (any-reg control-stack)))
922   (:arg-types tagged-num tagged-num)
923   (:note "inline fixnum comparison"))
924
925 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
926   (:args (x :scs (any-reg control-stack)))
927   (:arg-types tagged-num (:constant (signed-byte 30)))
928   (:info target not-p y))
929
930 (define-vop (fast-conditional/signed fast-conditional)
931   (:args (x :scs (signed-reg)
932             :load-if (not (and (sc-is x signed-stack)
933                                (sc-is y signed-reg))))
934          (y :scs (signed-reg signed-stack)))
935   (:arg-types signed-num signed-num)
936   (:note "inline (signed-byte 32) comparison"))
937
938 (define-vop (fast-conditional-c/signed fast-conditional/signed)
939   (:args (x :scs (signed-reg signed-stack)))
940   (:arg-types signed-num (:constant (signed-byte 32)))
941   (:info target not-p y))
942
943 (define-vop (fast-conditional/unsigned fast-conditional)
944   (:args (x :scs (unsigned-reg)
945             :load-if (not (and (sc-is x unsigned-stack)
946                                (sc-is y unsigned-reg))))
947          (y :scs (unsigned-reg unsigned-stack)))
948   (:arg-types unsigned-num unsigned-num)
949   (:note "inline (unsigned-byte 32) comparison"))
950
951 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
952   (:args (x :scs (unsigned-reg unsigned-stack)))
953   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
954   (:info target not-p y))
955
956
957 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
958              `(progn
959                 ,@(mapcar
960                    (lambda (suffix cost signed)
961                      `(define-vop (;; FIXME: These could be done more
962                                    ;; cleanly with SYMBOLICATE.
963                                    ,(intern (format nil "~:@(FAST-IF-~A~A~)"
964                                                     tran suffix))
965                                    ,(intern
966                                      (format nil "~:@(FAST-CONDITIONAL~A~)"
967                                              suffix)))
968                         (:translate ,tran)
969                         (:generator ,cost
970                                     (inst cmp x
971                                           ,(if (eq suffix '-c/fixnum)
972                                                '(fixnumize y)
973                                                'y))
974                                     (inst jmp (if not-p
975                                                   ,(if signed
976                                                        not-cond
977                                                        not-unsigned)
978                                                   ,(if signed
979                                                        cond
980                                                        unsigned))
981                                           target))))
982                    '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
983                    '(4 3 6 5 6 5)
984                    '(t t t t nil nil)))))
985
986   (define-conditional-vop < :l :b :ge :ae)
987   (define-conditional-vop > :g :a :le :be))
988
989 (define-vop (fast-if-eql/signed fast-conditional/signed)
990   (:translate eql)
991   (:generator 6
992     (inst cmp x y)
993     (inst jmp (if not-p :ne :e) target)))
994
995 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
996   (:translate eql)
997   (:generator 5
998     (cond ((and (sc-is x signed-reg) (zerop y))
999            (inst test x x))  ; smaller instruction
1000           (t
1001            (inst cmp x y)))
1002     (inst jmp (if not-p :ne :e) target)))
1003
1004 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1005   (:translate eql)
1006   (:generator 6
1007     (inst cmp x y)
1008     (inst jmp (if not-p :ne :e) target)))
1009
1010 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
1011   (:translate eql)
1012   (:generator 5
1013     (cond ((and (sc-is x unsigned-reg) (zerop y))
1014            (inst test x x))  ; smaller instruction
1015           (t
1016            (inst cmp x y)))
1017     (inst jmp (if not-p :ne :e) target)))
1018
1019 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1020 ;;; known fixnum.
1021
1022 ;;; These versions specify a fixnum restriction on their first arg. We have
1023 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1024 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1025 ;;; fixnum specific operations from being used on word integers, spuriously
1026 ;;; consing the argument.
1027
1028 (define-vop (fast-eql/fixnum fast-conditional)
1029   (:args (x :scs (any-reg)
1030             :load-if (not (and (sc-is x control-stack)
1031                                (sc-is y any-reg))))
1032          (y :scs (any-reg control-stack)))
1033   (:arg-types tagged-num tagged-num)
1034   (:note "inline fixnum comparison")
1035   (:translate eql)
1036   (:generator 4
1037     (inst cmp x y)
1038     (inst jmp (if not-p :ne :e) target)))
1039 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1040   (:args (x :scs (any-reg descriptor-reg)
1041             :load-if (not (and (sc-is x control-stack)
1042                                (sc-is y any-reg))))
1043          (y :scs (any-reg control-stack)))
1044   (:arg-types * tagged-num)
1045   (:variant-cost 7))
1046
1047 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1048   (:args (x :scs (any-reg control-stack)))
1049   (:arg-types tagged-num (:constant (signed-byte 30)))
1050   (:info target not-p y)
1051   (:translate eql)
1052   (:generator 2
1053     (cond ((and (sc-is x any-reg) (zerop y))
1054            (inst test x x))  ; smaller instruction
1055           (t
1056            (inst cmp x (fixnumize y))))
1057     (inst jmp (if not-p :ne :e) target)))
1058 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1059   (:args (x :scs (any-reg descriptor-reg control-stack)))
1060   (:arg-types * (:constant (signed-byte 30)))
1061   (:variant-cost 6))
1062 \f
1063 ;;;; 32-bit logical operations
1064
1065 (define-vop (merge-bits)
1066   (:translate merge-bits)
1067   (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
1068          (prev :scs (unsigned-reg) :target result)
1069          (next :scs (unsigned-reg)))
1070   (:arg-types tagged-num unsigned-num unsigned-num)
1071   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
1072   (:results (result :scs (unsigned-reg) :from (:argument 1)))
1073   (:result-types unsigned-num)
1074   (:policy :fast-safe)
1075   (:generator 4
1076     (move ecx shift)
1077     (move result prev)
1078     (inst shrd result next :cl)))
1079
1080 (define-source-transform 32bit-logical-not (x)
1081   `(logand (lognot (the (unsigned-byte 32) ,x)) #.(1- (ash 1 32))))
1082
1083 (deftransform 32bit-logical-and ((x y))
1084   '(logand x y))
1085
1086 (define-source-transform 32bit-logical-nand (x y)
1087   `(32bit-logical-not (32bit-logical-and ,x ,y)))
1088
1089 (deftransform 32bit-logical-or ((x y))
1090   '(logior x y))
1091
1092 (define-source-transform 32bit-logical-nor (x y)
1093   `(32bit-logical-not (32bit-logical-or ,x ,y)))
1094
1095 (deftransform 32bit-logical-xor ((x y))
1096   '(logxor x y))
1097
1098 (define-source-transform 32bit-logical-eqv (x y)
1099   `(32bit-logical-not (32bit-logical-xor ,x ,y)))
1100
1101 (define-source-transform 32bit-logical-orc1 (x y)
1102   `(32bit-logical-or (32bit-logical-not ,x) ,y))
1103
1104 (define-source-transform 32bit-logical-orc2 (x y)
1105   `(32bit-logical-or ,x (32bit-logical-not ,y)))
1106
1107 (define-source-transform 32bit-logical-andc1 (x y)
1108   `(32bit-logical-and (32bit-logical-not ,x) ,y))
1109
1110 (define-source-transform 32bit-logical-andc2 (x y)
1111   `(32bit-logical-and ,x (32bit-logical-not ,y)))
1112
1113 ;;; Only the lower 5 bits of the shift amount are significant.
1114 (define-vop (shift-towards-someplace)
1115   (:policy :fast-safe)
1116   (:args (num :scs (unsigned-reg) :target r)
1117          (amount :scs (signed-reg) :target ecx))
1118   (:arg-types unsigned-num tagged-num)
1119   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1120   (:results (r :scs (unsigned-reg) :from (:argument 0)))
1121   (:result-types unsigned-num))
1122
1123 (define-vop (shift-towards-start shift-towards-someplace)
1124   (:translate shift-towards-start)
1125   (:note "SHIFT-TOWARDS-START")
1126   (:generator 1
1127     (move r num)
1128     (move ecx amount)
1129     (inst shr r :cl)))
1130
1131 (define-vop (shift-towards-end shift-towards-someplace)
1132   (:translate shift-towards-end)
1133   (:note "SHIFT-TOWARDS-END")
1134   (:generator 1
1135     (move r num)
1136     (move ecx amount)
1137     (inst shl r :cl)))
1138 \f
1139 ;;;; Modular functions
1140
1141 (define-modular-fun +-mod32 (x y) + 32)
1142 (define-vop (fast-+-mod32/unsigned=>unsigned fast-+/unsigned=>unsigned)
1143   (:translate +-mod32))
1144 (define-vop (fast-+-mod32-c/unsigned=>unsigned fast-+-c/unsigned=>unsigned)
1145   (:translate +-mod32))
1146
1147 ;;; logical operations
1148 (define-modular-fun lognot-mod32 (x) lognot 32)
1149 (define-vop (lognot-mod32/unsigned=>unsigned)
1150   (:translate lognot-mod32)
1151   (:args (x :scs (unsigned-reg unsigned-stack) :target r
1152             :load-if (not (and (sc-is x unsigned-stack)
1153                                (sc-is r unsigned-stack)
1154                                (location= x r)))))
1155   (:arg-types unsigned-num)
1156   (:results (r :scs (unsigned-reg)
1157                :load-if (not (and (sc-is x unsigned-stack)
1158                                   (sc-is r unsigned-stack)
1159                                   (location= x r)))))
1160   (:result-types unsigned-num)
1161   (:policy :fast-safe)
1162   (:generator 1
1163     (move r x)
1164     (inst not r)))
1165
1166 (define-modular-fun logxor-mod32 (x y) logxor 32)
1167 (define-vop (fast-logxor-mod32/unsigned=>unsigned
1168              fast-logxor/unsigned=>unsigned)
1169   (:translate logxor-mod32))
1170 (define-vop (fast-logxor-mod32-c/unsigned=>unsigned
1171              fast-logxor-c/unsigned=>unsigned)
1172   (:translate logxor-mod32))
1173 \f
1174 ;;;; bignum stuff
1175
1176 (define-vop (bignum-length get-header-data)
1177   (:translate sb!bignum::%bignum-length)
1178   (:policy :fast-safe))
1179
1180 (define-vop (bignum-set-length set-header-data)
1181   (:translate sb!bignum::%bignum-set-length)
1182   (:policy :fast-safe))
1183
1184 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1185   (unsigned-reg) unsigned-num sb!bignum::%bignum-ref)
1186
1187 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1188   (unsigned-reg) unsigned-num sb!bignum::%bignum-set)
1189
1190 (define-vop (digit-0-or-plus)
1191   (:translate sb!bignum::%digit-0-or-plusp)
1192   (:policy :fast-safe)
1193   (:args (digit :scs (unsigned-reg)))
1194   (:arg-types unsigned-num)
1195   (:conditional)
1196   (:info target not-p)
1197   (:generator 3
1198     (inst or digit digit)
1199     (inst jmp (if not-p :s :ns) target)))
1200
1201
1202 ;;; For add and sub with carry the sc of carry argument is any-reg so
1203 ;;; the it may be passed as a fixnum or word and thus may be 0, 1, or
1204 ;;; 4. This is easy to deal with and may save a fixnum-word
1205 ;;; conversion.
1206 (define-vop (add-w/carry)
1207   (:translate sb!bignum::%add-with-carry)
1208   (:policy :fast-safe)
1209   (:args (a :scs (unsigned-reg) :target result)
1210          (b :scs (unsigned-reg unsigned-stack) :to :eval)
1211          (c :scs (any-reg) :target temp))
1212   (:arg-types unsigned-num unsigned-num positive-fixnum)
1213   (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1214   (:results (result :scs (unsigned-reg) :from (:argument 0))
1215             (carry :scs (unsigned-reg)))
1216   (:result-types unsigned-num positive-fixnum)
1217   (:generator 4
1218     (move result a)
1219     (move temp c)
1220     (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1221     (inst adc result b)
1222     (inst mov carry 0)
1223     (inst adc carry carry)))
1224
1225 ;;; Note: the borrow is the oppostite of the x86 convention - 1 for no
1226 ;;; borrow and 0 for a borrow.
1227 (define-vop (sub-w/borrow)
1228   (:translate sb!bignum::%subtract-with-borrow)
1229   (:policy :fast-safe)
1230   (:args (a :scs (unsigned-reg) :to :eval :target result)
1231          (b :scs (unsigned-reg unsigned-stack) :to :result)
1232          (c :scs (any-reg control-stack)))
1233   (:arg-types unsigned-num unsigned-num positive-fixnum)
1234   (:results (result :scs (unsigned-reg) :from :eval)
1235             (borrow :scs (unsigned-reg)))
1236   (:result-types unsigned-num positive-fixnum)
1237   (:generator 5
1238     (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1239     (move result a)
1240     (inst sbb result b)
1241     (inst mov borrow 0)
1242     (inst adc borrow borrow)
1243     (inst xor borrow 1)))
1244
1245
1246 (define-vop (bignum-mult-and-add-3-arg)
1247   (:translate sb!bignum::%multiply-and-add)
1248   (:policy :fast-safe)
1249   (:args (x :scs (unsigned-reg) :target eax)
1250          (y :scs (unsigned-reg unsigned-stack))
1251          (carry-in :scs (unsigned-reg unsigned-stack)))
1252   (:arg-types unsigned-num unsigned-num unsigned-num)
1253   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1254                    :to (:result 1) :target lo) eax)
1255   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1256                    :to (:result 0) :target hi) edx)
1257   (:results (hi :scs (unsigned-reg))
1258             (lo :scs (unsigned-reg)))
1259   (:result-types unsigned-num unsigned-num)
1260   (:generator 20
1261     (move eax x)
1262     (inst mul eax y)
1263     (inst add eax carry-in)
1264     (inst adc edx 0)
1265     (move hi edx)
1266     (move lo eax)))
1267
1268 (define-vop (bignum-mult-and-add-4-arg)
1269   (:translate sb!bignum::%multiply-and-add)
1270   (:policy :fast-safe)
1271   (:args (x :scs (unsigned-reg) :target eax)
1272          (y :scs (unsigned-reg unsigned-stack))
1273          (prev :scs (unsigned-reg unsigned-stack))
1274          (carry-in :scs (unsigned-reg unsigned-stack)))
1275   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1276   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1277                    :to (:result 1) :target lo) eax)
1278   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1279                    :to (:result 0) :target hi) edx)
1280   (:results (hi :scs (unsigned-reg))
1281             (lo :scs (unsigned-reg)))
1282   (:result-types unsigned-num unsigned-num)
1283   (:generator 20
1284     (move eax x)
1285     (inst mul eax y)
1286     (inst add eax prev)
1287     (inst adc edx 0)
1288     (inst add eax carry-in)
1289     (inst adc edx 0)
1290     (move hi edx)
1291     (move lo eax)))
1292
1293
1294 (define-vop (bignum-mult)
1295   (:translate sb!bignum::%multiply)
1296   (:policy :fast-safe)
1297   (:args (x :scs (unsigned-reg) :target eax)
1298          (y :scs (unsigned-reg unsigned-stack)))
1299   (:arg-types unsigned-num unsigned-num)
1300   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1301                    :to (:result 1) :target lo) eax)
1302   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1303                    :to (:result 0) :target hi) edx)
1304   (:results (hi :scs (unsigned-reg))
1305             (lo :scs (unsigned-reg)))
1306   (:result-types unsigned-num unsigned-num)
1307   (:generator 20
1308     (move eax x)
1309     (inst mul eax y)
1310     (move hi edx)
1311     (move lo eax)))
1312
1313 (define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
1314   (:translate sb!bignum::%lognot))
1315
1316 (define-vop (fixnum-to-digit)
1317   (:translate sb!bignum::%fixnum-to-digit)
1318   (:policy :fast-safe)
1319   (:args (fixnum :scs (any-reg control-stack) :target digit))
1320   (:arg-types tagged-num)
1321   (:results (digit :scs (unsigned-reg)
1322                    :load-if (not (and (sc-is fixnum control-stack)
1323                                       (sc-is digit unsigned-stack)
1324                                       (location= fixnum digit)))))
1325   (:result-types unsigned-num)
1326   (:generator 1
1327     (move digit fixnum)
1328     (inst sar digit 2)))
1329
1330 (define-vop (bignum-floor)
1331   (:translate sb!bignum::%floor)
1332   (:policy :fast-safe)
1333   (:args (div-high :scs (unsigned-reg) :target edx)
1334          (div-low :scs (unsigned-reg) :target eax)
1335          (divisor :scs (unsigned-reg unsigned-stack)))
1336   (:arg-types unsigned-num unsigned-num unsigned-num)
1337   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1338                    :to (:result 0) :target quo) eax)
1339   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1340                    :to (:result 1) :target rem) edx)
1341   (:results (quo :scs (unsigned-reg))
1342             (rem :scs (unsigned-reg)))
1343   (:result-types unsigned-num unsigned-num)
1344   (:generator 300
1345     (move edx div-high)
1346     (move eax div-low)
1347     (inst div eax divisor)
1348     (move quo eax)
1349     (move rem edx)))
1350
1351 (define-vop (signify-digit)
1352   (:translate sb!bignum::%fixnum-digit-with-correct-sign)
1353   (:policy :fast-safe)
1354   (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1355   (:arg-types unsigned-num)
1356   (:results (res :scs (any-reg signed-reg)
1357                  :load-if (not (and (sc-is digit unsigned-stack)
1358                                     (sc-is res control-stack signed-stack)
1359                                     (location= digit res)))))
1360   (:result-types signed-num)
1361   (:generator 1
1362     (move res digit)
1363     (when (sc-is res any-reg control-stack)
1364       (inst shl res 2))))
1365
1366 (define-vop (digit-ashr)
1367   (:translate sb!bignum::%ashr)
1368   (:policy :fast-safe)
1369   (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1370          (count :scs (unsigned-reg) :target ecx))
1371   (:arg-types unsigned-num positive-fixnum)
1372   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1373   (:results (result :scs (unsigned-reg) :from (:argument 0)
1374                     :load-if (not (and (sc-is result unsigned-stack)
1375                                        (location= digit result)))))
1376   (:result-types unsigned-num)
1377   (:generator 1
1378     (move result digit)
1379     (move ecx count)
1380     (inst sar result :cl)))
1381
1382 (define-vop (digit-lshr digit-ashr)
1383   (:translate sb!bignum::%digit-logical-shift-right)
1384   (:generator 1
1385     (move result digit)
1386     (move ecx count)
1387     (inst shr result :cl)))
1388
1389 (define-vop (digit-ashl digit-ashr)
1390   (:translate sb!bignum::%ashl)
1391   (:generator 1
1392     (move result digit)
1393     (move ecx count)
1394     (inst shl result :cl)))
1395 \f
1396 ;;;; static functions
1397
1398 (define-static-fun two-arg-/ (x y) :translate /)
1399
1400 (define-static-fun two-arg-gcd (x y) :translate gcd)
1401 (define-static-fun two-arg-lcm (x y) :translate lcm)
1402
1403 (define-static-fun two-arg-and (x y) :translate logand)
1404 (define-static-fun two-arg-ior (x y) :translate logior)
1405 (define-static-fun two-arg-xor (x y) :translate logxor)
1406
1407 \f
1408 ;;; Support for the Mersenne Twister, MT19937, random number generator
1409 ;;; due to Matsumoto and Nishimura.
1410 ;;;
1411 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
1412 ;;; 623-dimensionally equidistributed uniform pseudorandom number
1413 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
1414 ;;; 1997, to appear.
1415 ;;;
1416 ;;; State:
1417 ;;;  0-1:   Constant matrix A. [0, #x9908b0df] (not used here)
1418 ;;;  2:     Index; init. to 1.
1419 ;;;  3-626: State.
1420 (defknown random-mt19937 ((simple-array (unsigned-byte 32) (*)))
1421   (unsigned-byte 32) ())
1422 (define-vop (random-mt19937)
1423   (:policy :fast-safe)
1424   (:translate random-mt19937)
1425   (:args (state :scs (descriptor-reg) :to :result))
1426   (:arg-types simple-array-unsigned-byte-32)
1427   (:temporary (:sc unsigned-reg :from (:eval 0) :to :result) k)
1428   (:temporary (:sc unsigned-reg :offset eax-offset
1429                    :from (:eval 0) :to :result) tmp)
1430   (:results (y :scs (unsigned-reg) :from (:eval 0)))
1431   (:result-types unsigned-num)
1432   (:generator 50
1433     (inst mov k (make-ea :dword :base state
1434                          :disp (- (* (+ 2 vector-data-offset)
1435                                      n-word-bytes)
1436                                   other-pointer-lowtag)))
1437     (inst cmp k 624)
1438     (inst jmp :ne no-update)
1439     (inst mov tmp state)        ; The state is passed in EAX.
1440     (inst call (make-fixup 'random-mt19937-update :assembly-routine))
1441     ;; Restore k, and set to 0.
1442     (inst xor k k)
1443     NO-UPDATE
1444     ;; y = ptgfsr[k++];
1445     (inst mov y (make-ea :dword :base state :index k :scale 4
1446                          :disp (- (* (+ 3 vector-data-offset)
1447                                      n-word-bytes)
1448                                   other-pointer-lowtag)))
1449     ;; y ^= (y >> 11);
1450     (inst shr y 11)
1451     (inst xor y (make-ea :dword :base state :index k :scale 4
1452                          :disp (- (* (+ 3 vector-data-offset)
1453                                      n-word-bytes)
1454                                   other-pointer-lowtag)))
1455     ;; y ^= (y << 7) & #x9d2c5680
1456     (inst mov tmp y)
1457     (inst inc k)
1458     (inst shl tmp 7)
1459     (inst mov (make-ea :dword :base state
1460                        :disp (- (* (+ 2 vector-data-offset)
1461                                    n-word-bytes)
1462                                 other-pointer-lowtag))
1463           k)
1464     (inst and tmp #x9d2c5680)
1465     (inst xor y tmp)
1466     ;; y ^= (y << 15) & #xefc60000
1467     (inst mov tmp y)
1468     (inst shl tmp 15)
1469     (inst and tmp #xefc60000)
1470     (inst xor y tmp)
1471     ;; y ^= (y >> 18);
1472     (inst mov tmp y)
1473     (inst shr tmp 18)
1474     (inst xor y tmp)))
1475
1476 (in-package "SB!C")
1477
1478 (defknown %lea ((or (signed-byte 32) (unsigned-byte 32))
1479                 (or (signed-byte 32) (unsigned-byte 32))
1480                 (member 1 2 4 8) (signed-byte 32))
1481   (or (signed-byte 32) (unsigned-byte 32))
1482   (foldable flushable))
1483
1484 (defoptimizer (%lea derive-type) ((base index scale disp))
1485   (when (and (constant-continuation-p scale)
1486              (constant-continuation-p disp))
1487     (let ((scale (continuation-value scale))
1488           (disp (continuation-value disp))
1489           (base-type (continuation-type base))
1490           (index-type (continuation-type index)))
1491       (when (and (numeric-type-p base-type)
1492                  (numeric-type-p index-type))
1493         (let ((base-lo (numeric-type-low base-type))
1494               (base-hi (numeric-type-high base-type))
1495               (index-lo (numeric-type-low index-type))
1496               (index-hi (numeric-type-high index-type)))
1497           (make-numeric-type :class 'integer
1498                              :complexp :real
1499                              :low (when (and base-lo index-lo)
1500                                     (+ base-lo (* index-lo scale) disp))
1501                              :high (when (and base-hi index-hi)
1502                                      (+ base-hi (* index-hi scale) disp))))))))
1503
1504 (defun %lea (base index scale disp)
1505   (+ base (* index scale) disp))
1506
1507 (in-package "SB!VM")
1508
1509 (define-vop (%lea/unsigned=>unsigned)
1510   (:translate %lea)
1511   (:policy :fast-safe)
1512   (:args (base :scs (unsigned-reg))
1513          (index :scs (unsigned-reg)))
1514   (:info scale disp)
1515   (:arg-types unsigned-num unsigned-num
1516               (:constant (member 1 2 4 8))
1517               (:constant (signed-byte 32)))
1518   (:results (r :scs (unsigned-reg)))
1519   (:result-types unsigned-num)
1520   (:generator 5
1521     (inst lea r (make-ea :dword :base base :index index
1522                          :scale scale :disp disp))))
1523
1524 (define-vop (%lea/signed=>signed)
1525   (:translate %lea)
1526   (:policy :fast-safe)
1527   (:args (base :scs (signed-reg))
1528          (index :scs (signed-reg)))
1529   (:info scale disp)
1530   (:arg-types signed-num signed-num
1531               (:constant (member 1 2 4 8))
1532               (:constant (signed-byte 32)))
1533   (:results (r :scs (signed-reg)))
1534   (:result-types signed-num)
1535   (:generator 4
1536     (inst lea r (make-ea :dword :base base :index index
1537                          :scale scale :disp disp))))
1538
1539 (define-vop (%lea/fixnum=>fixnum)
1540   (:translate %lea)
1541   (:policy :fast-safe)
1542   (:args (base :scs (any-reg))
1543          (index :scs (any-reg)))
1544   (:info scale disp)
1545   (:arg-types tagged-num tagged-num
1546               (:constant (member 1 2 4 8))
1547               (:constant (signed-byte 32)))
1548   (:results (r :scs (any-reg)))
1549   (:result-types tagged-num)
1550   (:generator 3
1551     (inst lea r (make-ea :dword :base base :index index
1552                          :scale scale :disp disp))))
1553
1554 (in-package "SB!C")
1555
1556 ;;; This is essentially a straight implementation of the algorithm in
1557 ;;; "Strength Reduction of Multiplications by Integer Constants",
1558 ;;; Youfeng Wu, ACM SIGPLAN Notices, Vol. 30, No.2, February 1995.
1559 (defun basic-decompose-multiplication (arg num n-bits condensed)
1560   (case (aref condensed 0)
1561     (0
1562      (let ((tmp (min 3 (aref condensed 1))))
1563        (decf (aref condensed 1) tmp)
1564        `(truly-the (unsigned-byte 32)
1565          (%lea ,arg
1566                ,(decompose-multiplication
1567                  arg (ash (1- num) (- tmp)) (1- n-bits) (subseq condensed 1))
1568                ,(ash 1 tmp) 0))))
1569     ((1 2 3)
1570      (let ((r0 (aref condensed 0)))
1571        (incf (aref condensed 1) r0)
1572        `(truly-the (unsigned-byte 32)
1573          (%lea ,(decompose-multiplication
1574                  arg (- num (ash 1 r0)) (1- n-bits) (subseq condensed 1))
1575                ,arg
1576                ,(ash 1 r0) 0))))
1577     (t (let ((r0 (aref condensed 0)))
1578          (setf (aref condensed 0) 0)
1579          `(truly-the (unsigned-byte 32)
1580            (ash ,(decompose-multiplication
1581                   arg (ash num (- r0)) n-bits condensed)
1582                 ,r0))))))
1583
1584 (defun decompose-multiplication (arg num n-bits condensed)
1585   (cond
1586     ((= n-bits 0) 0)
1587     ((= num 1) arg)
1588     ((= n-bits 1)
1589      `(truly-the (unsigned-byte 32) (ash ,arg ,(1- (integer-length num)))))
1590     ((let ((max 0) (end 0))
1591        (loop for i from 2 to (length condensed)
1592              for j = (reduce #'+ (subseq condensed 0 i))
1593              when (and (> (- (* 2 i) 3 j) max)
1594                        (< (+ (ash 1 (1+ j))
1595                              (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num)
1596                                   (1+ j)))
1597                           (ash 1 32)))
1598                do (setq max (- (* 2 i) 3 j)
1599                         end i))
1600        (when (> max 0)
1601          (let ((j (reduce #'+ (subseq condensed 0 end))))
1602            (let ((n2 (+ (ash 1 (1+ j))
1603                         (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num) (1+ j))))
1604                  (n1 (1+ (ldb (byte (1+ j) 0) (lognot num)))))
1605            `(truly-the (unsigned-byte 32)
1606              (- ,(optimize-multiply arg n2) ,(optimize-multiply arg n1))))))))
1607     ((dolist (i '(9 5 3))
1608        (when (integerp (/ num i))
1609          (when (< (logcount (/ num i)) (logcount num))
1610            (let ((x (gensym)))
1611              (return `(let ((,x ,(optimize-multiply arg (/ num i))))
1612                        (truly-the (unsigned-byte 32)
1613                         (%lea ,x ,x (1- ,i) 0)))))))))
1614     (t (basic-decompose-multiplication arg num n-bits condensed))))
1615            
1616 (defun optimize-multiply (arg x)
1617   (let* ((n-bits (logcount x))
1618          (condensed (make-array n-bits)))
1619     (let ((count 0) (bit 0))
1620       (dotimes (i 32)
1621         (cond ((logbitp i x)
1622                (setf (aref condensed bit) count)
1623                (setf count 1)
1624                (incf bit))
1625               (t (incf count)))))
1626     (decompose-multiplication arg x n-bits condensed)))
1627
1628 (deftransform * ((x y)
1629                  ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1630                  (unsigned-byte 32))
1631   "recode as leas, shifts and adds"
1632   (let ((y (continuation-value y)))
1633     (cond
1634       ((= y (ash 1 (integer-length y)))
1635        ;; there's a generic transform for y = 2^k
1636        (give-up-ir1-transform))
1637       ((member y '(3 5 9))
1638        ;; we can do these multiplications directly using LEA
1639        `(%lea x x ,(1- y) 0))
1640       ((member :pentium4 *backend-subfeatures*)
1641        ;; the pentium4's multiply unit is reportedly very good
1642        (give-up-ir1-transform))
1643       ;; FIXME: should make this more fine-grained.  If nothing else,
1644       ;; there should probably be a cutoff of about 9 instructions on
1645       ;; pentium-class machines.
1646       (t (optimize-multiply 'x y)))))
1647
1648 ;;; FIXME: we should also be able to write an optimizer or two to
1649 ;;; convert (+ (* x 2) 17), (- (* x 9) 5) to a %LEA.