3c7dda228b10d8d6c105b243e1f3b3da3c74d32f
[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 (inst xor result result)))))))
693
694 (define-vop (fast-ash-left/signed=>signed)
695   (:translate ash)
696   (:args (number :scs (signed-reg) :target result
697                  :load-if (not (and (sc-is number signed-stack)
698                                     (sc-is result signed-stack)
699                                     (location= number result))))
700          (amount :scs (unsigned-reg) :target ecx))
701   (:arg-types signed-num positive-fixnum)
702   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
703   (:results (result :scs (signed-reg) :from (:argument 0)
704                     :load-if (not (and (sc-is number signed-stack)
705                                        (sc-is result signed-stack)
706                                        (location= number result)))))
707   (:result-types signed-num)
708   (:policy :fast-safe)
709   (:note "inline ASH")
710   (:generator 4
711     (move result number)
712     (move ecx amount)
713     (inst shl result :cl)))
714
715 (define-vop (fast-ash-left/unsigned=>unsigned)
716   (:translate ash)
717   (:args (number :scs (unsigned-reg) :target result
718                  :load-if (not (and (sc-is number unsigned-stack)
719                                     (sc-is result unsigned-stack)
720                                     (location= number result))))
721          (amount :scs (unsigned-reg) :target ecx))
722   (:arg-types unsigned-num positive-fixnum)
723   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
724   (:results (result :scs (unsigned-reg) :from (:argument 0)
725                     :load-if (not (and (sc-is number unsigned-stack)
726                                        (sc-is result unsigned-stack)
727                                        (location= number result)))))
728   (:result-types unsigned-num)
729   (:policy :fast-safe)
730   (:note "inline ASH")
731   (:generator 4
732     (move result number)
733     (move ecx amount)
734     (inst shl result :cl)))
735
736 (define-vop (fast-ash/signed=>signed)
737   (:translate ash)
738   (:policy :fast-safe)
739   (:args (number :scs (signed-reg) :target result)
740          (amount :scs (signed-reg) :target ecx))
741   (:arg-types signed-num signed-num)
742   (:results (result :scs (signed-reg) :from (:argument 0)))
743   (:result-types signed-num)
744   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
745   (:note "inline ASH")
746   (:generator 5
747     (move result number)
748     (move ecx amount)
749     (inst or ecx ecx)
750     (inst jmp :ns positive)
751     (inst neg ecx)
752     (inst cmp ecx 31)
753     (inst jmp :be okay)
754     (inst mov ecx 31)
755     OKAY
756     (inst sar result :cl)
757     (inst jmp done)
758
759     POSITIVE
760     ;; The result-type ensures us that this shift will not overflow.
761     (inst shl result :cl)
762
763     DONE))
764
765 (define-vop (fast-ash/unsigned=>unsigned)
766   (:translate ash)
767   (:policy :fast-safe)
768   (:args (number :scs (unsigned-reg) :target result)
769          (amount :scs (signed-reg) :target ecx))
770   (:arg-types unsigned-num signed-num)
771   (:results (result :scs (unsigned-reg) :from (:argument 0)))
772   (:result-types unsigned-num)
773   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
774   (:note "inline ASH")
775   (:generator 5
776     (move result number)
777     (move ecx amount)
778     (inst or ecx ecx)
779     (inst jmp :ns positive)
780     (inst neg ecx)
781     (inst cmp ecx 31)
782     (inst jmp :be okay)
783     (inst xor result result)
784     (inst jmp done)
785     OKAY
786     (inst shr result :cl)
787     (inst jmp done)
788
789     POSITIVE
790     ;; The result-type ensures us that this shift will not overflow.
791     (inst shl result :cl)
792
793     DONE))
794
795 ;;; FIXME: before making knowledge of this too public, it needs to be
796 ;;; fixed so that it's actually _faster_ than the non-CMOV version; at
797 ;;; least on my Celeron-XXX laptop, this version is marginally slower
798 ;;; than the above version with branches.  -- CSR, 2003-09-04
799 (define-vop (fast-cmov-ash/unsigned=>unsigned)
800   (:translate ash)
801   (:policy :fast-safe)
802   (:args (number :scs (unsigned-reg) :target result)
803          (amount :scs (signed-reg) :target ecx))
804   (:arg-types unsigned-num signed-num)
805   (:results (result :scs (unsigned-reg) :from (:argument 0)))
806   (:result-types unsigned-num)
807   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
808   (:temporary (:sc any-reg :from (:eval 0) :to (:eval 1)) zero)
809   (:note "inline ASH")
810   (:guard (member :cmov *backend-subfeatures*))
811   (:generator 4
812     (move result number)
813     (move ecx amount)
814     (inst or ecx ecx)
815     (inst jmp :ns positive)
816     (inst neg ecx)
817     (inst xor zero zero)
818     (inst shr result :cl)
819     (inst cmp ecx 31)
820     (inst cmov :nbe result zero)
821     (inst jmp done)
822     
823     POSITIVE
824     ;; The result-type ensures us that this shift will not overflow.
825     (inst shl result :cl)
826
827     DONE))
828 \f
829 ;;; Note: documentation for this function is wrong - rtfm
830 (define-vop (signed-byte-32-len)
831   (:translate integer-length)
832   (:note "inline (signed-byte 32) integer-length")
833   (:policy :fast-safe)
834   (:args (arg :scs (signed-reg) :target res))
835   (:arg-types signed-num)
836   (:results (res :scs (unsigned-reg)))
837   (:result-types unsigned-num)
838   (:generator 28
839     (move res arg)
840     (inst cmp res 0)
841     (inst jmp :ge POS)
842     (inst not res)
843     POS
844     (inst bsr res res)
845     (inst jmp :z zero)
846     (inst inc res)
847     (inst jmp done)
848     ZERO
849     (inst xor res res)
850     DONE))
851
852 (define-vop (unsigned-byte-32-len)
853   (:translate integer-length)
854   (:note "inline (unsigned-byte 32) integer-length")
855   (:policy :fast-safe)
856   (:args (arg :scs (unsigned-reg)))
857   (:arg-types unsigned-num)
858   (:results (res :scs (unsigned-reg)))
859   (:result-types unsigned-num)
860   (:generator 26
861     (inst bsr res arg)
862     (inst jmp :z zero)
863     (inst inc res)
864     (inst jmp done)
865     ZERO
866     (inst xor res res)
867     DONE))
868
869 (define-vop (unsigned-byte-32-count)
870   (:translate logcount)
871   (:note "inline (unsigned-byte 32) logcount")
872   (:policy :fast-safe)
873   (:args (arg :scs (unsigned-reg)))
874   (:arg-types unsigned-num)
875   (:results (result :scs (unsigned-reg)))
876   (:result-types positive-fixnum)
877   (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
878   (:generator 30
879     (move result arg)
880
881     (inst mov temp result)
882     (inst shr temp 1)
883     (inst and result #x55555555)
884     (inst and temp #x55555555)
885     (inst add result temp)
886
887     (inst mov temp result)
888     (inst shr temp 2)
889     (inst and result #x33333333)
890     (inst and temp #x33333333)
891     (inst add result temp)
892
893     (inst mov temp result)
894     (inst shr temp 4)
895     (inst and result #x0f0f0f0f)
896     (inst and temp #x0f0f0f0f)
897     (inst add result temp)
898
899     (inst mov temp result)
900     (inst shr temp 8)
901     (inst and result #x00ff00ff)
902     (inst and temp #x00ff00ff)
903     (inst add result temp)
904
905     (inst mov temp result)
906     (inst shr temp 16)
907     (inst and result #x0000ffff)
908     (inst and temp #x0000ffff)
909     (inst add result temp)))
910 \f
911 ;;;; binary conditional VOPs
912
913 (define-vop (fast-conditional)
914   (:conditional)
915   (:info target not-p)
916   (:effects)
917   (:affected)
918   (:policy :fast-safe))
919
920 (define-vop (fast-conditional/fixnum fast-conditional)
921   (:args (x :scs (any-reg)
922             :load-if (not (and (sc-is x control-stack)
923                                (sc-is y any-reg))))
924          (y :scs (any-reg control-stack)))
925   (:arg-types tagged-num tagged-num)
926   (:note "inline fixnum comparison"))
927
928 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
929   (:args (x :scs (any-reg control-stack)))
930   (:arg-types tagged-num (:constant (signed-byte 30)))
931   (:info target not-p y))
932
933 (define-vop (fast-conditional/signed fast-conditional)
934   (:args (x :scs (signed-reg)
935             :load-if (not (and (sc-is x signed-stack)
936                                (sc-is y signed-reg))))
937          (y :scs (signed-reg signed-stack)))
938   (:arg-types signed-num signed-num)
939   (:note "inline (signed-byte 32) comparison"))
940
941 (define-vop (fast-conditional-c/signed fast-conditional/signed)
942   (:args (x :scs (signed-reg signed-stack)))
943   (:arg-types signed-num (:constant (signed-byte 32)))
944   (:info target not-p y))
945
946 (define-vop (fast-conditional/unsigned fast-conditional)
947   (:args (x :scs (unsigned-reg)
948             :load-if (not (and (sc-is x unsigned-stack)
949                                (sc-is y unsigned-reg))))
950          (y :scs (unsigned-reg unsigned-stack)))
951   (:arg-types unsigned-num unsigned-num)
952   (:note "inline (unsigned-byte 32) comparison"))
953
954 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
955   (:args (x :scs (unsigned-reg unsigned-stack)))
956   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
957   (:info target not-p y))
958
959
960 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
961              `(progn
962                 ,@(mapcar
963                    (lambda (suffix cost signed)
964                      `(define-vop (;; FIXME: These could be done more
965                                    ;; cleanly with SYMBOLICATE.
966                                    ,(intern (format nil "~:@(FAST-IF-~A~A~)"
967                                                     tran suffix))
968                                    ,(intern
969                                      (format nil "~:@(FAST-CONDITIONAL~A~)"
970                                              suffix)))
971                         (:translate ,tran)
972                         (:generator ,cost
973                                     (inst cmp x
974                                           ,(if (eq suffix '-c/fixnum)
975                                                '(fixnumize y)
976                                                'y))
977                                     (inst jmp (if not-p
978                                                   ,(if signed
979                                                        not-cond
980                                                        not-unsigned)
981                                                   ,(if signed
982                                                        cond
983                                                        unsigned))
984                                           target))))
985                    '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
986                    '(4 3 6 5 6 5)
987                    '(t t t t nil nil)))))
988
989   (define-conditional-vop < :l :b :ge :ae)
990   (define-conditional-vop > :g :a :le :be))
991
992 (define-vop (fast-if-eql/signed fast-conditional/signed)
993   (:translate eql)
994   (:generator 6
995     (inst cmp x y)
996     (inst jmp (if not-p :ne :e) target)))
997
998 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
999   (:translate eql)
1000   (:generator 5
1001     (cond ((and (sc-is x signed-reg) (zerop y))
1002            (inst test x x))  ; smaller instruction
1003           (t
1004            (inst cmp x y)))
1005     (inst jmp (if not-p :ne :e) target)))
1006
1007 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1008   (:translate eql)
1009   (:generator 6
1010     (inst cmp x y)
1011     (inst jmp (if not-p :ne :e) target)))
1012
1013 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
1014   (:translate eql)
1015   (:generator 5
1016     (cond ((and (sc-is x unsigned-reg) (zerop y))
1017            (inst test x x))  ; smaller instruction
1018           (t
1019            (inst cmp x y)))
1020     (inst jmp (if not-p :ne :e) target)))
1021
1022 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1023 ;;; known fixnum.
1024
1025 ;;; These versions specify a fixnum restriction on their first arg. We have
1026 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1027 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1028 ;;; fixnum specific operations from being used on word integers, spuriously
1029 ;;; consing the argument.
1030
1031 (define-vop (fast-eql/fixnum fast-conditional)
1032   (:args (x :scs (any-reg)
1033             :load-if (not (and (sc-is x control-stack)
1034                                (sc-is y any-reg))))
1035          (y :scs (any-reg control-stack)))
1036   (:arg-types tagged-num tagged-num)
1037   (:note "inline fixnum comparison")
1038   (:translate eql)
1039   (:generator 4
1040     (inst cmp x y)
1041     (inst jmp (if not-p :ne :e) target)))
1042 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1043   (:args (x :scs (any-reg descriptor-reg)
1044             :load-if (not (and (sc-is x control-stack)
1045                                (sc-is y any-reg))))
1046          (y :scs (any-reg control-stack)))
1047   (:arg-types * tagged-num)
1048   (:variant-cost 7))
1049
1050 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1051   (:args (x :scs (any-reg control-stack)))
1052   (:arg-types tagged-num (:constant (signed-byte 30)))
1053   (:info target not-p y)
1054   (:translate eql)
1055   (:generator 2
1056     (cond ((and (sc-is x any-reg) (zerop y))
1057            (inst test x x))  ; smaller instruction
1058           (t
1059            (inst cmp x (fixnumize y))))
1060     (inst jmp (if not-p :ne :e) target)))
1061 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1062   (:args (x :scs (any-reg descriptor-reg control-stack)))
1063   (:arg-types * (:constant (signed-byte 30)))
1064   (:variant-cost 6))
1065 \f
1066 ;;;; 32-bit logical operations
1067
1068 (define-vop (merge-bits)
1069   (:translate merge-bits)
1070   (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
1071          (prev :scs (unsigned-reg) :target result)
1072          (next :scs (unsigned-reg)))
1073   (:arg-types tagged-num unsigned-num unsigned-num)
1074   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
1075   (:results (result :scs (unsigned-reg) :from (:argument 1)))
1076   (:result-types unsigned-num)
1077   (:policy :fast-safe)
1078   (:generator 4
1079     (move ecx shift)
1080     (move result prev)
1081     (inst shrd result next :cl)))
1082
1083 (define-source-transform 32bit-logical-not (x)
1084   `(logand (lognot (the (unsigned-byte 32) ,x)) #.(1- (ash 1 32))))
1085
1086 (deftransform 32bit-logical-and ((x y))
1087   '(logand x y))
1088
1089 (define-source-transform 32bit-logical-nand (x y)
1090   `(32bit-logical-not (32bit-logical-and ,x ,y)))
1091
1092 (deftransform 32bit-logical-or ((x y))
1093   '(logior x y))
1094
1095 (define-source-transform 32bit-logical-nor (x y)
1096   `(32bit-logical-not (32bit-logical-or ,x ,y)))
1097
1098 (deftransform 32bit-logical-xor ((x y))
1099   '(logxor x y))
1100
1101 (define-source-transform 32bit-logical-eqv (x y)
1102   `(32bit-logical-not (32bit-logical-xor ,x ,y)))
1103
1104 (define-source-transform 32bit-logical-orc1 (x y)
1105   `(32bit-logical-or (32bit-logical-not ,x) ,y))
1106
1107 (define-source-transform 32bit-logical-orc2 (x y)
1108   `(32bit-logical-or ,x (32bit-logical-not ,y)))
1109
1110 (define-source-transform 32bit-logical-andc1 (x y)
1111   `(32bit-logical-and (32bit-logical-not ,x) ,y))
1112
1113 (define-source-transform 32bit-logical-andc2 (x y)
1114   `(32bit-logical-and ,x (32bit-logical-not ,y)))
1115
1116 ;;; Only the lower 5 bits of the shift amount are significant.
1117 (define-vop (shift-towards-someplace)
1118   (:policy :fast-safe)
1119   (:args (num :scs (unsigned-reg) :target r)
1120          (amount :scs (signed-reg) :target ecx))
1121   (:arg-types unsigned-num tagged-num)
1122   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1123   (:results (r :scs (unsigned-reg) :from (:argument 0)))
1124   (:result-types unsigned-num))
1125
1126 (define-vop (shift-towards-start shift-towards-someplace)
1127   (:translate shift-towards-start)
1128   (:note "SHIFT-TOWARDS-START")
1129   (:generator 1
1130     (move r num)
1131     (move ecx amount)
1132     (inst shr r :cl)))
1133
1134 (define-vop (shift-towards-end shift-towards-someplace)
1135   (:translate shift-towards-end)
1136   (:note "SHIFT-TOWARDS-END")
1137   (:generator 1
1138     (move r num)
1139     (move ecx amount)
1140     (inst shl r :cl)))
1141 \f
1142 ;;;; Modular functions
1143
1144 (define-modular-fun +-mod32 (x y) + 32)
1145 (define-vop (fast-+-mod32/unsigned=>unsigned fast-+/unsigned=>unsigned)
1146   (:translate +-mod32))
1147 (define-vop (fast-+-mod32-c/unsigned=>unsigned fast-+-c/unsigned=>unsigned)
1148   (:translate +-mod32))
1149 (define-modular-fun --mod32 (x y) - 32)
1150 (define-vop (fast---mod32/unsigned=>unsigned fast--/unsigned=>unsigned)
1151   (:translate --mod32))
1152 (define-vop (fast---mod32-c/unsigned=>unsigned fast---c/unsigned=>unsigned)
1153   (:translate --mod32))
1154
1155 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
1156              fast-ash-c/unsigned=>unsigned)
1157   (:translate ash-left-mod32))
1158
1159 ;;; logical operations
1160 (define-modular-fun lognot-mod32 (x) lognot 32)
1161 (define-vop (lognot-mod32/unsigned=>unsigned)
1162   (:translate lognot-mod32)
1163   (:args (x :scs (unsigned-reg unsigned-stack) :target r
1164             :load-if (not (and (sc-is x unsigned-stack)
1165                                (sc-is r unsigned-stack)
1166                                (location= x r)))))
1167   (:arg-types unsigned-num)
1168   (:results (r :scs (unsigned-reg)
1169                :load-if (not (and (sc-is x unsigned-stack)
1170                                   (sc-is r unsigned-stack)
1171                                   (location= x r)))))
1172   (:result-types unsigned-num)
1173   (:policy :fast-safe)
1174   (:generator 1
1175     (move r x)
1176     (inst not r)))
1177
1178 (define-modular-fun logxor-mod32 (x y) logxor 32)
1179 (define-vop (fast-logxor-mod32/unsigned=>unsigned
1180              fast-logxor/unsigned=>unsigned)
1181   (:translate logxor-mod32))
1182 (define-vop (fast-logxor-mod32-c/unsigned=>unsigned
1183              fast-logxor-c/unsigned=>unsigned)
1184   (:translate logxor-mod32))
1185
1186 (define-source-transform logeqv (&rest args)
1187   (if (oddp (length args))
1188       `(logxor ,@args)
1189       `(lognot (logxor ,@args))))
1190 (define-source-transform logandc1 (x y)
1191   `(logand (lognot ,x) ,y))
1192 (define-source-transform logandc2 (x y)
1193   `(logand ,x (lognot ,y)))
1194 (define-source-transform logorc1 (x y)
1195   `(logior (lognot ,x) ,y))
1196 (define-source-transform logorc2 (x y)
1197   `(logior ,x (lognot ,y)))
1198 (define-source-transform lognor (x y)
1199   `(lognot (logior ,x ,y)))
1200 (define-source-transform lognand (x y)
1201   `(lognot (logand ,x ,y)))
1202 \f
1203 ;;;; bignum stuff
1204
1205 (define-vop (bignum-length get-header-data)
1206   (:translate sb!bignum:%bignum-length)
1207   (:policy :fast-safe))
1208
1209 (define-vop (bignum-set-length set-header-data)
1210   (:translate sb!bignum:%bignum-set-length)
1211   (:policy :fast-safe))
1212
1213 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1214   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1215
1216 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1217   (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1218
1219 (define-vop (digit-0-or-plus)
1220   (:translate sb!bignum:%digit-0-or-plusp)
1221   (:policy :fast-safe)
1222   (:args (digit :scs (unsigned-reg)))
1223   (:arg-types unsigned-num)
1224   (:conditional)
1225   (:info target not-p)
1226   (:generator 3
1227     (inst or digit digit)
1228     (inst jmp (if not-p :s :ns) target)))
1229
1230
1231 ;;; For add and sub with carry the sc of carry argument is any-reg so
1232 ;;; the it may be passed as a fixnum or word and thus may be 0, 1, or
1233 ;;; 4. This is easy to deal with and may save a fixnum-word
1234 ;;; conversion.
1235 (define-vop (add-w/carry)
1236   (:translate sb!bignum:%add-with-carry)
1237   (:policy :fast-safe)
1238   (:args (a :scs (unsigned-reg) :target result)
1239          (b :scs (unsigned-reg unsigned-stack) :to :eval)
1240          (c :scs (any-reg) :target temp))
1241   (:arg-types unsigned-num unsigned-num positive-fixnum)
1242   (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1243   (:results (result :scs (unsigned-reg) :from (:argument 0))
1244             (carry :scs (unsigned-reg)))
1245   (:result-types unsigned-num positive-fixnum)
1246   (:generator 4
1247     (move result a)
1248     (move temp c)
1249     (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1250     (inst adc result b)
1251     (inst mov carry 0)
1252     (inst adc carry carry)))
1253
1254 ;;; Note: the borrow is the oppostite of the x86 convention - 1 for no
1255 ;;; borrow and 0 for a borrow.
1256 (define-vop (sub-w/borrow)
1257   (:translate sb!bignum:%subtract-with-borrow)
1258   (:policy :fast-safe)
1259   (:args (a :scs (unsigned-reg) :to :eval :target result)
1260          (b :scs (unsigned-reg unsigned-stack) :to :result)
1261          (c :scs (any-reg control-stack)))
1262   (:arg-types unsigned-num unsigned-num positive-fixnum)
1263   (:results (result :scs (unsigned-reg) :from :eval)
1264             (borrow :scs (unsigned-reg)))
1265   (:result-types unsigned-num positive-fixnum)
1266   (:generator 5
1267     (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1268     (move result a)
1269     (inst sbb result b)
1270     (inst mov borrow 0)
1271     (inst adc borrow borrow)
1272     (inst xor borrow 1)))
1273
1274
1275 (define-vop (bignum-mult-and-add-3-arg)
1276   (:translate sb!bignum:%multiply-and-add)
1277   (:policy :fast-safe)
1278   (:args (x :scs (unsigned-reg) :target eax)
1279          (y :scs (unsigned-reg unsigned-stack))
1280          (carry-in :scs (unsigned-reg unsigned-stack)))
1281   (:arg-types unsigned-num unsigned-num unsigned-num)
1282   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1283                    :to (:result 1) :target lo) eax)
1284   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1285                    :to (:result 0) :target hi) edx)
1286   (:results (hi :scs (unsigned-reg))
1287             (lo :scs (unsigned-reg)))
1288   (:result-types unsigned-num unsigned-num)
1289   (:generator 20
1290     (move eax x)
1291     (inst mul eax y)
1292     (inst add eax carry-in)
1293     (inst adc edx 0)
1294     (move hi edx)
1295     (move lo eax)))
1296
1297 (define-vop (bignum-mult-and-add-4-arg)
1298   (:translate sb!bignum:%multiply-and-add)
1299   (:policy :fast-safe)
1300   (:args (x :scs (unsigned-reg) :target eax)
1301          (y :scs (unsigned-reg unsigned-stack))
1302          (prev :scs (unsigned-reg unsigned-stack))
1303          (carry-in :scs (unsigned-reg unsigned-stack)))
1304   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1305   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1306                    :to (:result 1) :target lo) eax)
1307   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1308                    :to (:result 0) :target hi) edx)
1309   (:results (hi :scs (unsigned-reg))
1310             (lo :scs (unsigned-reg)))
1311   (:result-types unsigned-num unsigned-num)
1312   (:generator 20
1313     (move eax x)
1314     (inst mul eax y)
1315     (inst add eax prev)
1316     (inst adc edx 0)
1317     (inst add eax carry-in)
1318     (inst adc edx 0)
1319     (move hi edx)
1320     (move lo eax)))
1321
1322
1323 (define-vop (bignum-mult)
1324   (:translate sb!bignum:%multiply)
1325   (:policy :fast-safe)
1326   (:args (x :scs (unsigned-reg) :target eax)
1327          (y :scs (unsigned-reg unsigned-stack)))
1328   (:arg-types unsigned-num unsigned-num)
1329   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1330                    :to (:result 1) :target lo) eax)
1331   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1332                    :to (:result 0) :target hi) edx)
1333   (:results (hi :scs (unsigned-reg))
1334             (lo :scs (unsigned-reg)))
1335   (:result-types unsigned-num unsigned-num)
1336   (:generator 20
1337     (move eax x)
1338     (inst mul eax y)
1339     (move hi edx)
1340     (move lo eax)))
1341
1342 (define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
1343   (:translate sb!bignum:%lognot))
1344
1345 (define-vop (fixnum-to-digit)
1346   (:translate sb!bignum:%fixnum-to-digit)
1347   (:policy :fast-safe)
1348   (:args (fixnum :scs (any-reg control-stack) :target digit))
1349   (:arg-types tagged-num)
1350   (:results (digit :scs (unsigned-reg)
1351                    :load-if (not (and (sc-is fixnum control-stack)
1352                                       (sc-is digit unsigned-stack)
1353                                       (location= fixnum digit)))))
1354   (:result-types unsigned-num)
1355   (:generator 1
1356     (move digit fixnum)
1357     (inst sar digit 2)))
1358
1359 (define-vop (bignum-floor)
1360   (:translate sb!bignum:%floor)
1361   (:policy :fast-safe)
1362   (:args (div-high :scs (unsigned-reg) :target edx)
1363          (div-low :scs (unsigned-reg) :target eax)
1364          (divisor :scs (unsigned-reg unsigned-stack)))
1365   (:arg-types unsigned-num unsigned-num unsigned-num)
1366   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1367                    :to (:result 0) :target quo) eax)
1368   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1369                    :to (:result 1) :target rem) edx)
1370   (:results (quo :scs (unsigned-reg))
1371             (rem :scs (unsigned-reg)))
1372   (:result-types unsigned-num unsigned-num)
1373   (:generator 300
1374     (move edx div-high)
1375     (move eax div-low)
1376     (inst div eax divisor)
1377     (move quo eax)
1378     (move rem edx)))
1379
1380 (define-vop (signify-digit)
1381   (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1382   (:policy :fast-safe)
1383   (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1384   (:arg-types unsigned-num)
1385   (:results (res :scs (any-reg signed-reg)
1386                  :load-if (not (and (sc-is digit unsigned-stack)
1387                                     (sc-is res control-stack signed-stack)
1388                                     (location= digit res)))))
1389   (:result-types signed-num)
1390   (:generator 1
1391     (move res digit)
1392     (when (sc-is res any-reg control-stack)
1393       (inst shl res 2))))
1394
1395 (define-vop (digit-ashr)
1396   (:translate sb!bignum:%ashr)
1397   (:policy :fast-safe)
1398   (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1399          (count :scs (unsigned-reg) :target ecx))
1400   (:arg-types unsigned-num positive-fixnum)
1401   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1402   (:results (result :scs (unsigned-reg) :from (:argument 0)
1403                     :load-if (not (and (sc-is result unsigned-stack)
1404                                        (location= digit result)))))
1405   (:result-types unsigned-num)
1406   (:generator 1
1407     (move result digit)
1408     (move ecx count)
1409     (inst sar result :cl)))
1410
1411 (define-vop (digit-lshr digit-ashr)
1412   (:translate sb!bignum:%digit-logical-shift-right)
1413   (:generator 1
1414     (move result digit)
1415     (move ecx count)
1416     (inst shr result :cl)))
1417
1418 (define-vop (digit-ashl digit-ashr)
1419   (:translate sb!bignum:%ashl)
1420   (:generator 1
1421     (move result digit)
1422     (move ecx count)
1423     (inst shl result :cl)))
1424 \f
1425 ;;;; static functions
1426
1427 (define-static-fun two-arg-/ (x y) :translate /)
1428
1429 (define-static-fun two-arg-gcd (x y) :translate gcd)
1430 (define-static-fun two-arg-lcm (x y) :translate lcm)
1431
1432 (define-static-fun two-arg-and (x y) :translate logand)
1433 (define-static-fun two-arg-ior (x y) :translate logior)
1434 (define-static-fun two-arg-xor (x y) :translate logxor)
1435
1436 \f
1437 ;;; Support for the Mersenne Twister, MT19937, random number generator
1438 ;;; due to Matsumoto and Nishimura.
1439 ;;;
1440 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
1441 ;;; 623-dimensionally equidistributed uniform pseudorandom number
1442 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
1443 ;;; 1997, to appear.
1444 ;;;
1445 ;;; State:
1446 ;;;  0-1:   Constant matrix A. [0, #x9908b0df] (not used here)
1447 ;;;  2:     Index; init. to 1.
1448 ;;;  3-626: State.
1449 (defknown random-mt19937 ((simple-array (unsigned-byte 32) (*)))
1450   (unsigned-byte 32) ())
1451 (define-vop (random-mt19937)
1452   (:policy :fast-safe)
1453   (:translate random-mt19937)
1454   (:args (state :scs (descriptor-reg) :to :result))
1455   (:arg-types simple-array-unsigned-byte-32)
1456   (:temporary (:sc unsigned-reg :from (:eval 0) :to :result) k)
1457   (:temporary (:sc unsigned-reg :offset eax-offset
1458                    :from (:eval 0) :to :result) tmp)
1459   (:results (y :scs (unsigned-reg) :from (:eval 0)))
1460   (:result-types unsigned-num)
1461   (:generator 50
1462     (inst mov k (make-ea :dword :base state
1463                          :disp (- (* (+ 2 vector-data-offset)
1464                                      n-word-bytes)
1465                                   other-pointer-lowtag)))
1466     (inst cmp k 624)
1467     (inst jmp :ne no-update)
1468     (inst mov tmp state)        ; The state is passed in EAX.
1469     (inst call (make-fixup 'random-mt19937-update :assembly-routine))
1470     ;; Restore k, and set to 0.
1471     (inst xor k k)
1472     NO-UPDATE
1473     ;; y = ptgfsr[k++];
1474     (inst mov y (make-ea :dword :base state :index k :scale 4
1475                          :disp (- (* (+ 3 vector-data-offset)
1476                                      n-word-bytes)
1477                                   other-pointer-lowtag)))
1478     ;; y ^= (y >> 11);
1479     (inst shr y 11)
1480     (inst xor y (make-ea :dword :base state :index k :scale 4
1481                          :disp (- (* (+ 3 vector-data-offset)
1482                                      n-word-bytes)
1483                                   other-pointer-lowtag)))
1484     ;; y ^= (y << 7) & #x9d2c5680
1485     (inst mov tmp y)
1486     (inst inc k)
1487     (inst shl tmp 7)
1488     (inst mov (make-ea :dword :base state
1489                        :disp (- (* (+ 2 vector-data-offset)
1490                                    n-word-bytes)
1491                                 other-pointer-lowtag))
1492           k)
1493     (inst and tmp #x9d2c5680)
1494     (inst xor y tmp)
1495     ;; y ^= (y << 15) & #xefc60000
1496     (inst mov tmp y)
1497     (inst shl tmp 15)
1498     (inst and tmp #xefc60000)
1499     (inst xor y tmp)
1500     ;; y ^= (y >> 18);
1501     (inst mov tmp y)
1502     (inst shr tmp 18)
1503     (inst xor y tmp)))
1504
1505 (in-package "SB!C")
1506
1507 (defknown %lea ((or (signed-byte 32) (unsigned-byte 32))
1508                 (or (signed-byte 32) (unsigned-byte 32))
1509                 (member 1 2 4 8) (signed-byte 32))
1510   (or (signed-byte 32) (unsigned-byte 32))
1511   (foldable flushable))
1512
1513 (defoptimizer (%lea derive-type) ((base index scale disp))
1514   (when (and (constant-lvar-p scale)
1515              (constant-lvar-p disp))
1516     (let ((scale (lvar-value scale))
1517           (disp (lvar-value disp))
1518           (base-type (lvar-type base))
1519           (index-type (lvar-type index)))
1520       (when (and (numeric-type-p base-type)
1521                  (numeric-type-p index-type))
1522         (let ((base-lo (numeric-type-low base-type))
1523               (base-hi (numeric-type-high base-type))
1524               (index-lo (numeric-type-low index-type))
1525               (index-hi (numeric-type-high index-type)))
1526           (make-numeric-type :class 'integer
1527                              :complexp :real
1528                              :low (when (and base-lo index-lo)
1529                                     (+ base-lo (* index-lo scale) disp))
1530                              :high (when (and base-hi index-hi)
1531                                      (+ base-hi (* index-hi scale) disp))))))))
1532
1533 (defun %lea (base index scale disp)
1534   (+ base (* index scale) disp))
1535
1536 (in-package "SB!VM")
1537
1538 (define-vop (%lea/unsigned=>unsigned)
1539   (:translate %lea)
1540   (:policy :fast-safe)
1541   (:args (base :scs (unsigned-reg))
1542          (index :scs (unsigned-reg)))
1543   (:info scale disp)
1544   (:arg-types unsigned-num unsigned-num
1545               (:constant (member 1 2 4 8))
1546               (:constant (signed-byte 32)))
1547   (:results (r :scs (unsigned-reg)))
1548   (:result-types unsigned-num)
1549   (:generator 5
1550     (inst lea r (make-ea :dword :base base :index index
1551                          :scale scale :disp disp))))
1552
1553 (define-vop (%lea/signed=>signed)
1554   (:translate %lea)
1555   (:policy :fast-safe)
1556   (:args (base :scs (signed-reg))
1557          (index :scs (signed-reg)))
1558   (:info scale disp)
1559   (:arg-types signed-num signed-num
1560               (:constant (member 1 2 4 8))
1561               (:constant (signed-byte 32)))
1562   (:results (r :scs (signed-reg)))
1563   (:result-types signed-num)
1564   (:generator 4
1565     (inst lea r (make-ea :dword :base base :index index
1566                          :scale scale :disp disp))))
1567
1568 (define-vop (%lea/fixnum=>fixnum)
1569   (:translate %lea)
1570   (:policy :fast-safe)
1571   (:args (base :scs (any-reg))
1572          (index :scs (any-reg)))
1573   (:info scale disp)
1574   (:arg-types tagged-num tagged-num
1575               (:constant (member 1 2 4 8))
1576               (:constant (signed-byte 32)))
1577   (:results (r :scs (any-reg)))
1578   (:result-types tagged-num)
1579   (:generator 3
1580     (inst lea r (make-ea :dword :base base :index index
1581                          :scale scale :disp disp))))
1582
1583 (in-package "SB!C")
1584
1585 ;;; This is essentially a straight implementation of the algorithm in
1586 ;;; "Strength Reduction of Multiplications by Integer Constants",
1587 ;;; Youfeng Wu, ACM SIGPLAN Notices, Vol. 30, No.2, February 1995.
1588 (defun basic-decompose-multiplication (arg num n-bits condensed)
1589   (case (aref condensed 0)
1590     (0
1591      (let ((tmp (min 3 (aref condensed 1))))
1592        (decf (aref condensed 1) tmp)
1593        `(logand #xffffffff
1594          (%lea ,arg
1595                ,(decompose-multiplication
1596                  arg (ash (1- num) (- tmp)) (1- n-bits) (subseq condensed 1))
1597                ,(ash 1 tmp) 0))))
1598     ((1 2 3)
1599      (let ((r0 (aref condensed 0)))
1600        (incf (aref condensed 1) r0)
1601        `(logand #xffffffff
1602          (%lea ,(decompose-multiplication
1603                  arg (- num (ash 1 r0)) (1- n-bits) (subseq condensed 1))
1604                ,arg
1605                ,(ash 1 r0) 0))))
1606     (t (let ((r0 (aref condensed 0)))
1607          (setf (aref condensed 0) 0)
1608          `(logand #xffffffff
1609            (ash ,(decompose-multiplication
1610                   arg (ash num (- r0)) n-bits condensed)
1611                 ,r0))))))
1612
1613 (defun decompose-multiplication (arg num n-bits condensed)
1614   (cond
1615     ((= n-bits 0) 0)
1616     ((= num 1) arg)
1617     ((= n-bits 1)
1618      `(logand #xffffffff (ash ,arg ,(1- (integer-length num)))))
1619     ((let ((max 0) (end 0))
1620        (loop for i from 2 to (length condensed)
1621              for j = (reduce #'+ (subseq condensed 0 i))
1622              when (and (> (- (* 2 i) 3 j) max)
1623                        (< (+ (ash 1 (1+ j))
1624                              (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num)
1625                                   (1+ j)))
1626                           (ash 1 32)))
1627                do (setq max (- (* 2 i) 3 j)
1628                         end i))
1629        (when (> max 0)
1630          (let ((j (reduce #'+ (subseq condensed 0 end))))
1631            (let ((n2 (+ (ash 1 (1+ j))
1632                         (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num) (1+ j))))
1633                  (n1 (1+ (ldb (byte (1+ j) 0) (lognot num)))))
1634            `(logand #xffffffff
1635              (- ,(optimize-multiply arg n2) ,(optimize-multiply arg n1))))))))
1636     ((dolist (i '(9 5 3))
1637        (when (integerp (/ num i))
1638          (when (< (logcount (/ num i)) (logcount num))
1639            (let ((x (gensym)))
1640              (return `(let ((,x ,(optimize-multiply arg (/ num i))))
1641                        (logand #xffffffff
1642                         (%lea ,x ,x (1- ,i) 0)))))))))
1643     (t (basic-decompose-multiplication arg num n-bits condensed))))
1644            
1645 (defun optimize-multiply (arg x)
1646   (let* ((n-bits (logcount x))
1647          (condensed (make-array n-bits)))
1648     (let ((count 0) (bit 0))
1649       (dotimes (i 32)
1650         (cond ((logbitp i x)
1651                (setf (aref condensed bit) count)
1652                (setf count 1)
1653                (incf bit))
1654               (t (incf count)))))
1655     (decompose-multiplication arg x n-bits condensed)))
1656
1657 (deftransform * ((x y)
1658                  ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1659                  (unsigned-byte 32))
1660   "recode as leas, shifts and adds"
1661   (let ((y (lvar-value y)))
1662     (cond
1663       ((= y (ash 1 (integer-length y)))
1664        ;; there's a generic transform for y = 2^k
1665        (give-up-ir1-transform))
1666       ((member y '(3 5 9))
1667        ;; we can do these multiplications directly using LEA
1668        `(%lea x x ,(1- y) 0))
1669       ((member :pentium4 *backend-subfeatures*)
1670        ;; the pentium4's multiply unit is reportedly very good
1671        (give-up-ir1-transform))
1672       ;; FIXME: should make this more fine-grained.  If nothing else,
1673       ;; there should probably be a cutoff of about 9 instructions on
1674       ;; pentium-class machines.
1675       (t (optimize-multiply 'x y)))))
1676
1677 ;;; FIXME: we should also be able to write an optimizer or two to
1678 ;;; convert (+ (* x 2) 17), (- (* x 9) 5) to a %LEA.