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