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