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