0.9.15.1:
[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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 64) 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 (define-vop (unsigned-byte-64-count)
951   (:translate logcount)
952   (:note "inline (unsigned-byte 64) logcount")
953   (:policy :fast-safe)
954   (:args (arg :scs (unsigned-reg) :target result))
955   (:arg-types unsigned-num)
956   (:results (result :scs (unsigned-reg)))
957   (:result-types positive-fixnum)
958   (:temporary (:sc unsigned-reg) temp)
959   (:temporary (:sc unsigned-reg) mask)
960   (:generator 14
961     ;; See the comments below for how the algorithm works. The tricks
962     ;; used can be found for example in AMD's software optimization
963     ;; guide or at "http://www.hackersdelight.org/HDcode/pop.cc" in the
964     ;; function "pop1", for 32-bit words. The extension to 64 bits is
965     ;; straightforward.
966     ;; Calculate 2-bit sums. Note that the value of a two-digit binary
967     ;; number is the sum of the right digit and twice the left digit.
968     ;; Thus we can calculate the sum of the two digits by shifting the
969     ;; left digit to the right position and doing a two-bit subtraction.
970     ;; This subtraction will never create a borrow and thus can be made
971     ;; on all 32 2-digit numbers at once.
972     (move result arg)
973     (move temp arg)
974     (inst shr result 1)
975     (inst mov mask #x5555555555555555)
976     (inst and result mask)
977     (inst sub temp result)
978     ;; Calculate 4-bit sums by straightforward shift, mask and add.
979     ;; Note that we shift the source operand of the MOV and not its
980     ;; destination so that the SHR and the MOV can execute in the same
981     ;; clock cycle.
982     (inst mov result temp)
983     (inst shr temp 2)
984     (inst mov mask #x3333333333333333)
985     (inst and result mask)
986     (inst and temp mask)
987     (inst add result temp)
988     ;; Calculate 8-bit sums. Since each sum is at most 8, which fits
989     ;; into 4 bits, we can apply the mask after the addition, saving one
990     ;; instruction.
991     (inst mov temp result)
992     (inst shr result 4)
993     (inst add result temp)
994     (inst mov mask #x0f0f0f0f0f0f0f0f)
995     (inst and result mask)
996     ;; Add all 8 bytes at once by multiplying with #256r11111111.
997     ;; We need to calculate only the lower 8 bytes of the product.
998     ;; Of these the most significant byte contains the final result.
999     ;; Note that there can be no overflow from one byte to the next
1000     ;; as the sum is at most 64 which needs only 7 bits.
1001     (inst mov mask #x0101010101010101)
1002     (inst imul result mask)
1003     (inst shr result 56)))
1004 \f
1005 ;;;; binary conditional VOPs
1006
1007 (define-vop (fast-conditional)
1008   (:conditional)
1009   (:info target not-p)
1010   (:effects)
1011   (:affected)
1012   (:policy :fast-safe))
1013
1014 ;;; constant variants are declared for 32 bits not 64 bits, because
1015 ;;; loading a 64 bit constant is silly
1016
1017 (define-vop (fast-conditional/fixnum fast-conditional)
1018   (:args (x :scs (any-reg)
1019             :load-if (not (and (sc-is x control-stack)
1020                                (sc-is y any-reg))))
1021          (y :scs (any-reg control-stack)))
1022   (:arg-types tagged-num tagged-num)
1023   (:note "inline fixnum comparison"))
1024
1025 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
1026   (:args (x :scs (any-reg control-stack)))
1027   (:arg-types tagged-num (:constant (signed-byte 29)))
1028   (:info target not-p y))
1029
1030 (define-vop (fast-conditional/signed fast-conditional)
1031   (:args (x :scs (signed-reg)
1032             :load-if (not (and (sc-is x signed-stack)
1033                                (sc-is y signed-reg))))
1034          (y :scs (signed-reg signed-stack)))
1035   (:arg-types signed-num signed-num)
1036   (:note "inline (signed-byte 64) comparison"))
1037
1038 (define-vop (fast-conditional-c/signed fast-conditional/signed)
1039   (:args (x :scs (signed-reg signed-stack)))
1040   (:arg-types signed-num (:constant (signed-byte 31)))
1041   (:info target not-p y))
1042
1043 (define-vop (fast-conditional/unsigned fast-conditional)
1044   (:args (x :scs (unsigned-reg)
1045             :load-if (not (and (sc-is x unsigned-stack)
1046                                (sc-is y unsigned-reg))))
1047          (y :scs (unsigned-reg unsigned-stack)))
1048   (:arg-types unsigned-num unsigned-num)
1049   (:note "inline (unsigned-byte 64) comparison"))
1050
1051 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
1052   (:args (x :scs (unsigned-reg unsigned-stack)))
1053   (:arg-types unsigned-num (:constant (unsigned-byte 31)))
1054   (:info target not-p y))
1055
1056 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
1057              `(progn
1058                 ,@(mapcar
1059                    (lambda (suffix cost signed)
1060                      `(define-vop (;; FIXME: These could be done more
1061                                    ;; cleanly with SYMBOLICATE.
1062                                    ,(intern (format nil "~:@(FAST-IF-~A~A~)"
1063                                                     tran suffix))
1064                                    ,(intern
1065                                      (format nil "~:@(FAST-CONDITIONAL~A~)"
1066                                              suffix)))
1067                         (:translate ,tran)
1068                         (:generator ,cost
1069                                     (inst cmp x
1070                                           ,(if (eq suffix '-c/fixnum)
1071                                                '(fixnumize y)
1072                                                'y))
1073                                     (inst jmp (if not-p
1074                                                   ,(if signed
1075                                                        not-cond
1076                                                        not-unsigned)
1077                                                   ,(if signed
1078                                                        cond
1079                                                        unsigned))
1080                                           target))))
1081                    '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
1082 ;                  '(/fixnum  /signed  /unsigned)
1083                    '(4 3 6 5 6 5)
1084                    '(t t t t nil nil)))))
1085
1086   (define-conditional-vop < :l :b :ge :ae)
1087   (define-conditional-vop > :g :a :le :be))
1088
1089 (define-vop (fast-if-eql/signed fast-conditional/signed)
1090   (:translate eql)
1091   (:generator 6
1092     (inst cmp x y)
1093     (inst jmp (if not-p :ne :e) target)))
1094
1095 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
1096   (:translate eql)
1097   (:generator 5
1098     (cond ((and (sc-is x signed-reg) (zerop y))
1099            (inst test x x))  ; smaller instruction
1100           (t
1101            (inst cmp x y)))
1102     (inst jmp (if not-p :ne :e) target)))
1103
1104 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1105   (:translate eql)
1106   (:generator 6
1107     (inst cmp x y)
1108     (inst jmp (if not-p :ne :e) target)))
1109
1110 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
1111   (:translate eql)
1112   (:generator 5
1113     (cond ((and (sc-is x unsigned-reg) (zerop y))
1114            (inst test x x))  ; smaller instruction
1115           (t
1116            (inst cmp x y)))
1117     (inst jmp (if not-p :ne :e) target)))
1118
1119 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1120 ;;; known fixnum.
1121
1122 ;;; These versions specify a fixnum restriction on their first arg. We have
1123 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1124 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1125 ;;; fixnum specific operations from being used on word integers, spuriously
1126 ;;; consing the argument.
1127
1128 (define-vop (fast-eql/fixnum fast-conditional)
1129   (:args (x :scs (any-reg)
1130             :load-if (not (and (sc-is x control-stack)
1131                                (sc-is y any-reg))))
1132          (y :scs (any-reg control-stack)))
1133   (:arg-types tagged-num tagged-num)
1134   (:note "inline fixnum comparison")
1135   (:translate eql)
1136   (:generator 4
1137     (inst cmp x y)
1138     (inst jmp (if not-p :ne :e) target)))
1139 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1140   (:args (x :scs (any-reg descriptor-reg)
1141             :load-if (not (and (sc-is x control-stack)
1142                                (sc-is y any-reg))))
1143          (y :scs (any-reg control-stack)))
1144   (:arg-types * tagged-num)
1145   (:variant-cost 7))
1146
1147
1148 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1149   (:args (x :scs (any-reg control-stack)))
1150   (:arg-types tagged-num (:constant (signed-byte 29)))
1151   (:info target not-p y)
1152   (:translate eql)
1153   (:generator 2
1154     (cond ((and (sc-is x any-reg) (zerop y))
1155            (inst test x x))  ; smaller instruction
1156           (t
1157            (inst cmp x (fixnumize y))))
1158     (inst jmp (if not-p :ne :e) target)))
1159
1160 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1161   (:args (x :scs (any-reg descriptor-reg control-stack)))
1162   (:arg-types * (:constant (signed-byte 29)))
1163   (:variant-cost 6))
1164 \f
1165 ;;;; 32-bit logical operations
1166
1167 (define-vop (merge-bits)
1168   (:translate merge-bits)
1169   (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
1170          (prev :scs (unsigned-reg) :target result)
1171          (next :scs (unsigned-reg)))
1172   (:arg-types tagged-num unsigned-num unsigned-num)
1173   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
1174   (:results (result :scs (unsigned-reg) :from (:argument 1)))
1175   (:result-types unsigned-num)
1176   (:policy :fast-safe)
1177   (:generator 4
1178     (move ecx shift)
1179     (move result prev)
1180     (inst shrd result next :cl)))
1181
1182 ;;; Only the lower 6 bits of the shift amount are significant.
1183 (define-vop (shift-towards-someplace)
1184   (:policy :fast-safe)
1185   (:args (num :scs (unsigned-reg) :target r)
1186          (amount :scs (signed-reg) :target ecx))
1187   (:arg-types unsigned-num tagged-num)
1188   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1189   (:results (r :scs (unsigned-reg) :from (:argument 0)))
1190   (:result-types unsigned-num))
1191
1192 (define-vop (shift-towards-start shift-towards-someplace)
1193   (:translate shift-towards-start)
1194   (:note "SHIFT-TOWARDS-START")
1195   (:generator 1
1196     (move r num)
1197     (move ecx amount)
1198     (inst shr r :cl)))
1199
1200 (define-vop (shift-towards-end shift-towards-someplace)
1201   (:translate shift-towards-end)
1202   (:note "SHIFT-TOWARDS-END")
1203   (:generator 1
1204     (move r num)
1205     (move ecx amount)
1206     (inst shl r :cl)))
1207 \f
1208 ;;;; Modular functions
1209
1210 (macrolet ((def (name -c-p)
1211              (let ((fun64 (intern (format nil "~S-MOD64" name)))
1212                    (vopu (intern (format nil "FAST-~S/UNSIGNED=>UNSIGNED" name)))
1213                    (vopcu (intern (format nil "FAST-~S-C/UNSIGNED=>UNSIGNED" name)))
1214                    (vopf (intern (format nil "FAST-~S/FIXNUM=>FIXNUM" name)))
1215                    (vopcf (intern (format nil "FAST-~S-C/FIXNUM=>FIXNUM" name)))
1216                    (vop64u (intern (format nil "FAST-~S-MOD64/UNSIGNED=>UNSIGNED" name)))
1217                    (vop64f (intern (format nil "FAST-~S-MOD64/FIXNUM=>FIXNUM" name)))
1218                    (vop64cu (intern (format nil "FAST-~S-MOD64-C/UNSIGNED=>UNSIGNED" name)))
1219                    (vop64cf (intern (format nil "FAST-~S-MOD64-C/FIXNUM=>FIXNUM" name)))
1220                    (sfun61 (intern (format nil "~S-SMOD61" name)))
1221                    (svop61f (intern (format nil "FAST-~S-SMOD61/FIXNUM=>FIXNUM" name)))
1222                    (svop61cf (intern (format nil "FAST-~S-SMOD61-C/FIXNUM=>FIXNUM" name))))
1223                `(progn
1224                   (define-modular-fun ,fun64 (x y) ,name :unsigned 64)
1225                   (define-modular-fun ,sfun61 (x y) ,name :signed 61)
1226                   (define-vop (,vop64u ,vopu) (:translate ,fun64))
1227                   (define-vop (,vop64f ,vopf) (:translate ,fun64))
1228                   (define-vop (,svop61f ,vopf) (:translate ,sfun61))
1229                   ,@(when -c-p
1230                       `((define-vop (,vop64cu ,vopcu) (:translate ,fun64))
1231                         (define-vop (,svop61cf ,vopcf) (:translate ,sfun61))))))))
1232   (def + t)
1233   (def - t)
1234   ;; (no -C variant as x86 MUL instruction doesn't take an immediate)
1235   (def * nil))
1236
1237 (define-vop (fast-ash-left-mod64-c/unsigned=>unsigned
1238              fast-ash-c/unsigned=>unsigned)
1239   (:translate ash-left-mod64))
1240 (define-vop (fast-ash-left-mod64/unsigned=>unsigned
1241              fast-ash-left/unsigned=>unsigned))
1242 (deftransform ash-left-mod64 ((integer count)
1243                               ((unsigned-byte 64) (unsigned-byte 6)))
1244   (when (sb!c::constant-lvar-p count)
1245     (sb!c::give-up-ir1-transform))
1246   '(%primitive fast-ash-left-mod64/unsigned=>unsigned integer count))
1247
1248 (define-vop (fast-ash-left-smod61-c/fixnum=>fixnum
1249              fast-ash-c/fixnum=>fixnum)
1250   (:translate ash-left-smod61))
1251 (define-vop (fast-ash-left-smod61/fixnum=>fixnum
1252              fast-ash-left/fixnum=>fixnum))
1253 (deftransform ash-left-smod61 ((integer count)
1254                                ((signed-byte 61) (unsigned-byte 6)))
1255   (when (sb!c::constant-lvar-p count)
1256     (sb!c::give-up-ir1-transform))
1257   '(%primitive fast-ash-left-smod61/fixnum=>fixnum integer count))
1258
1259 (in-package "SB!C")
1260
1261 (defknown sb!vm::%lea-mod64 (integer integer (member 1 2 4 8) (signed-byte 64))
1262   (unsigned-byte 64)
1263   (foldable flushable movable))
1264 (defknown sb!vm::%lea-smod61 (integer integer (member 1 2 4 8) (signed-byte 64))
1265   (signed-byte 61)
1266   (foldable flushable movable))
1267
1268 (define-modular-fun-optimizer %lea ((base index scale disp) :unsigned :width width)
1269   (when (and (<= width 64)
1270              (constant-lvar-p scale)
1271              (constant-lvar-p disp))
1272     (cut-to-width base :unsigned width)
1273     (cut-to-width index :unsigned width)
1274     'sb!vm::%lea-mod64))
1275 (define-modular-fun-optimizer %lea ((base index scale disp) :signed :width width)
1276   (when (and (<= width 61)
1277              (constant-lvar-p scale)
1278              (constant-lvar-p disp))
1279     (cut-to-width base :signed width)
1280     (cut-to-width index :signed width)
1281     'sb!vm::%lea-smod61))
1282
1283 #+sb-xc-host
1284 (progn
1285   (defun sb!vm::%lea-mod64 (base index scale disp)
1286     (ldb (byte 64 0) (%lea base index scale disp)))
1287   (defun sb!vm::%lea-smod61 (base index scale disp)
1288     (mask-signed-field 61 (%lea base index scale disp))))
1289 #-sb-xc-host
1290 (progn
1291   (defun sb!vm::%lea-mod64 (base index scale disp)
1292     (let ((base (logand base #xffffffffffffffff))
1293           (index (logand index #xffffffffffffffff)))
1294       ;; can't use modular version of %LEA, as we only have VOPs for
1295       ;; constant SCALE and DISP.
1296       (ldb (byte 64 0) (+ base (* index scale) disp))))
1297   (defun sb!vm::%lea-smod61 (base index scale disp)
1298     (let ((base (mask-signed-field 61 base))
1299           (index (mask-signed-field 61 index)))
1300       ;; can't use modular version of %LEA, as we only have VOPs for
1301       ;; constant SCALE and DISP.
1302       (mask-signed-field 61 (+ base (* index scale) disp)))))
1303
1304 (in-package "SB!VM")
1305
1306 (define-vop (%lea-mod64/unsigned=>unsigned
1307              %lea/unsigned=>unsigned)
1308   (:translate %lea-mod64))
1309 (define-vop (%lea-smod61/fixnum=>fixnum
1310              %lea/fixnum=>fixnum)
1311   (:translate %lea-smod61))
1312
1313 ;;; logical operations
1314 (define-modular-fun lognot-mod64 (x) lognot :unsigned 64)
1315 (define-vop (lognot-mod64/unsigned=>unsigned)
1316   (:translate lognot-mod64)
1317   (:args (x :scs (unsigned-reg unsigned-stack) :target r
1318             :load-if (not (and (sc-is x unsigned-stack)
1319                                (sc-is r unsigned-stack)
1320                                (location= x r)))))
1321   (:arg-types unsigned-num)
1322   (:results (r :scs (unsigned-reg)
1323                :load-if (not (and (sc-is x unsigned-stack)
1324                                   (sc-is r unsigned-stack)
1325                                   (location= x r)))))
1326   (:result-types unsigned-num)
1327   (:policy :fast-safe)
1328   (:generator 1
1329     (move r x)
1330     (inst not r)))
1331
1332 (define-modular-fun logxor-mod64 (x y) logxor :unsigned 64)
1333 (define-vop (fast-logxor-mod64/unsigned=>unsigned
1334              fast-logxor/unsigned=>unsigned)
1335   (:translate logxor-mod64))
1336 (define-vop (fast-logxor-mod64-c/unsigned=>unsigned
1337              fast-logxor-c/unsigned=>unsigned)
1338   (:translate logxor-mod64))
1339 (define-vop (fast-logxor-mod64/fixnum=>fixnum
1340              fast-logxor/fixnum=>fixnum)
1341   (:translate logxor-mod64))
1342 (define-vop (fast-logxor-mod64-c/fixnum=>fixnum
1343              fast-logxor-c/fixnum=>fixnum)
1344   (:translate logxor-mod64))
1345
1346 (define-source-transform logeqv (&rest args)
1347   (if (oddp (length args))
1348       `(logxor ,@args)
1349       `(lognot (logxor ,@args))))
1350 (define-source-transform logandc1 (x y)
1351   `(logand (lognot ,x) ,y))
1352 (define-source-transform logandc2 (x y)
1353   `(logand ,x (lognot ,y)))
1354 (define-source-transform logorc1 (x y)
1355   `(logior (lognot ,x) ,y))
1356 (define-source-transform logorc2 (x y)
1357   `(logior ,x (lognot ,y)))
1358 (define-source-transform lognor (x y)
1359   `(lognot (logior ,x ,y)))
1360 (define-source-transform lognand (x y)
1361   `(lognot (logand ,x ,y)))
1362 \f
1363 ;;;; bignum stuff
1364
1365 (define-vop (bignum-length get-header-data)
1366   (:translate sb!bignum:%bignum-length)
1367   (:policy :fast-safe))
1368
1369 (define-vop (bignum-set-length set-header-data)
1370   (:translate sb!bignum:%bignum-set-length)
1371   (:policy :fast-safe))
1372
1373 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1374   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1375
1376 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1377   (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1378
1379 (define-vop (digit-0-or-plus)
1380   (:translate sb!bignum:%digit-0-or-plusp)
1381   (:policy :fast-safe)
1382   (:args (digit :scs (unsigned-reg)))
1383   (:arg-types unsigned-num)
1384   (:conditional)
1385   (:info target not-p)
1386   (:generator 3
1387     (inst or digit digit)
1388     (inst jmp (if not-p :s :ns) target)))
1389
1390
1391 ;;; For add and sub with carry the sc of carry argument is any-reg so
1392 ;;; that it may be passed as a fixnum or word and thus may be 0, 1, or
1393 ;;; 8. This is easy to deal with and may save a fixnum-word
1394 ;;; conversion.
1395 (define-vop (add-w/carry)
1396   (:translate sb!bignum:%add-with-carry)
1397   (:policy :fast-safe)
1398   (:args (a :scs (unsigned-reg) :target result)
1399          (b :scs (unsigned-reg unsigned-stack) :to :eval)
1400          (c :scs (any-reg) :target temp))
1401   (:arg-types unsigned-num unsigned-num positive-fixnum)
1402   (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1403   (:results (result :scs (unsigned-reg) :from (:argument 0))
1404             (carry :scs (unsigned-reg)))
1405   (:result-types unsigned-num positive-fixnum)
1406   (:generator 4
1407     (move result a)
1408     (move temp c)
1409     (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1410     (inst adc result b)
1411     (inst mov carry 0)
1412     (inst adc carry carry)))
1413
1414 ;;; Note: the borrow is 1 for no borrow and 0 for a borrow, the opposite
1415 ;;; of the x86-64 convention.
1416 (define-vop (sub-w/borrow)
1417   (:translate sb!bignum:%subtract-with-borrow)
1418   (:policy :fast-safe)
1419   (:args (a :scs (unsigned-reg) :to :eval :target result)
1420          (b :scs (unsigned-reg unsigned-stack) :to :result)
1421          (c :scs (any-reg control-stack)))
1422   (:arg-types unsigned-num unsigned-num positive-fixnum)
1423   (:results (result :scs (unsigned-reg) :from :eval)
1424             (borrow :scs (unsigned-reg)))
1425   (:result-types unsigned-num positive-fixnum)
1426   (:generator 5
1427     (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1428     (move result a)
1429     (inst sbb result b)
1430     (inst mov borrow 1)
1431     (inst sbb borrow 0)))
1432
1433
1434 (define-vop (bignum-mult-and-add-3-arg)
1435   (:translate sb!bignum:%multiply-and-add)
1436   (:policy :fast-safe)
1437   (:args (x :scs (unsigned-reg) :target eax)
1438          (y :scs (unsigned-reg unsigned-stack))
1439          (carry-in :scs (unsigned-reg unsigned-stack)))
1440   (:arg-types unsigned-num unsigned-num unsigned-num)
1441   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1442                    :to (:result 1) :target lo) eax)
1443   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1444                    :to (:result 0) :target hi) edx)
1445   (:results (hi :scs (unsigned-reg))
1446             (lo :scs (unsigned-reg)))
1447   (:result-types unsigned-num unsigned-num)
1448   (:generator 20
1449     (move eax x)
1450     (inst mul eax y)
1451     (inst add eax carry-in)
1452     (inst adc edx 0)
1453     (move hi edx)
1454     (move lo eax)))
1455
1456 (define-vop (bignum-mult-and-add-4-arg)
1457   (:translate sb!bignum:%multiply-and-add)
1458   (:policy :fast-safe)
1459   (:args (x :scs (unsigned-reg) :target eax)
1460          (y :scs (unsigned-reg unsigned-stack))
1461          (prev :scs (unsigned-reg unsigned-stack))
1462          (carry-in :scs (unsigned-reg unsigned-stack)))
1463   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1464   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1465                    :to (:result 1) :target lo) eax)
1466   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1467                    :to (:result 0) :target hi) edx)
1468   (:results (hi :scs (unsigned-reg))
1469             (lo :scs (unsigned-reg)))
1470   (:result-types unsigned-num unsigned-num)
1471   (:generator 20
1472     (move eax x)
1473     (inst mul eax y)
1474     (inst add eax prev)
1475     (inst adc edx 0)
1476     (inst add eax carry-in)
1477     (inst adc edx 0)
1478     (move hi edx)
1479     (move lo eax)))
1480
1481
1482 (define-vop (bignum-mult)
1483   (:translate sb!bignum:%multiply)
1484   (:policy :fast-safe)
1485   (:args (x :scs (unsigned-reg) :target eax)
1486          (y :scs (unsigned-reg unsigned-stack)))
1487   (:arg-types unsigned-num unsigned-num)
1488   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1489                    :to (:result 1) :target lo) eax)
1490   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1491                    :to (:result 0) :target hi) edx)
1492   (:results (hi :scs (unsigned-reg))
1493             (lo :scs (unsigned-reg)))
1494   (:result-types unsigned-num unsigned-num)
1495   (:generator 20
1496     (move eax x)
1497     (inst mul eax y)
1498     (move hi edx)
1499     (move lo eax)))
1500
1501 (define-vop (bignum-lognot lognot-mod64/unsigned=>unsigned)
1502   (:translate sb!bignum:%lognot))
1503
1504 (define-vop (fixnum-to-digit)
1505   (:translate sb!bignum:%fixnum-to-digit)
1506   (:policy :fast-safe)
1507   (:args (fixnum :scs (any-reg control-stack) :target digit))
1508   (:arg-types tagged-num)
1509   (:results (digit :scs (unsigned-reg)
1510                    :load-if (not (and (sc-is fixnum control-stack)
1511                                       (sc-is digit unsigned-stack)
1512                                       (location= fixnum digit)))))
1513   (:result-types unsigned-num)
1514   (:generator 1
1515     (move digit fixnum)
1516     (inst sar digit 3)))
1517
1518 (define-vop (bignum-floor)
1519   (:translate sb!bignum:%floor)
1520   (:policy :fast-safe)
1521   (:args (div-high :scs (unsigned-reg) :target edx)
1522          (div-low :scs (unsigned-reg) :target eax)
1523          (divisor :scs (unsigned-reg unsigned-stack)))
1524   (:arg-types unsigned-num unsigned-num unsigned-num)
1525   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1526                    :to (:result 0) :target quo) eax)
1527   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1528                    :to (:result 1) :target rem) edx)
1529   (:results (quo :scs (unsigned-reg))
1530             (rem :scs (unsigned-reg)))
1531   (:result-types unsigned-num unsigned-num)
1532   (:generator 300
1533     (move edx div-high)
1534     (move eax div-low)
1535     (inst div eax divisor)
1536     (move quo eax)
1537     (move rem edx)))
1538
1539 (define-vop (signify-digit)
1540   (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1541   (:policy :fast-safe)
1542   (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1543   (:arg-types unsigned-num)
1544   (:results (res :scs (any-reg signed-reg)
1545                  :load-if (not (and (sc-is digit unsigned-stack)
1546                                     (sc-is res control-stack signed-stack)
1547                                     (location= digit res)))))
1548   (:result-types signed-num)
1549   (:generator 1
1550     (move res digit)
1551     (when (sc-is res any-reg control-stack)
1552       (inst shl res 3))))
1553
1554 (define-vop (digit-ashr)
1555   (:translate sb!bignum:%ashr)
1556   (:policy :fast-safe)
1557   (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1558          (count :scs (unsigned-reg) :target ecx))
1559   (:arg-types unsigned-num positive-fixnum)
1560   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1561   (:results (result :scs (unsigned-reg) :from (:argument 0)
1562                     :load-if (not (and (sc-is result unsigned-stack)
1563                                        (location= digit result)))))
1564   (:result-types unsigned-num)
1565   (:generator 2
1566     (move result digit)
1567     (move ecx count)
1568     (inst sar result :cl)))
1569
1570 (define-vop (digit-ashr/c)
1571   (:translate sb!bignum:%ashr)
1572   (:policy :fast-safe)
1573   (:args (digit :scs (unsigned-reg unsigned-stack) :target result))
1574   (:arg-types unsigned-num (:constant (integer 0 63)))
1575   (:info count)
1576   (:results (result :scs (unsigned-reg) :from (:argument 0)
1577                     :load-if (not (and (sc-is result unsigned-stack)
1578                                        (location= digit result)))))
1579   (:result-types unsigned-num)
1580   (:generator 1
1581     (move result digit)
1582     (inst sar result count)))
1583
1584 (define-vop (digit-lshr digit-ashr)
1585   (:translate sb!bignum:%digit-logical-shift-right)
1586   (:generator 1
1587     (move result digit)
1588     (move ecx count)
1589     (inst shr result :cl)))
1590
1591 (define-vop (digit-ashl digit-ashr)
1592   (:translate sb!bignum:%ashl)
1593   (:generator 1
1594     (move result digit)
1595     (move ecx count)
1596     (inst shl result :cl)))
1597 \f
1598 ;;;; static functions
1599
1600 (define-static-fun two-arg-/ (x y) :translate /)
1601
1602 (define-static-fun two-arg-gcd (x y) :translate gcd)
1603 (define-static-fun two-arg-lcm (x y) :translate lcm)
1604
1605 (define-static-fun two-arg-and (x y) :translate logand)
1606 (define-static-fun two-arg-ior (x y) :translate logior)
1607 (define-static-fun two-arg-xor (x y) :translate logxor)
1608
1609
1610 (in-package "SB!C")
1611
1612 (defun *-transformer (y)
1613   (cond
1614     ((= y (ash 1 (integer-length y)))
1615      ;; there's a generic transform for y = 2^k
1616      (give-up-ir1-transform))
1617     ((member y '(3 5 9))
1618      ;; we can do these multiplications directly using LEA
1619      `(%lea x x ,(1- y) 0))
1620     (t
1621      ;; A normal 64-bit multiplication takes 4 cycles on Athlon 64/Opteron.
1622      ;; Optimizing multiplications (other than the above cases) to
1623      ;; shifts/adds/leas gives a maximum improvement of 1 cycle, but requires
1624      ;; quite a lot of hairy code.
1625      (give-up-ir1-transform))))
1626
1627 (deftransform * ((x y)
1628                  ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1629                  (unsigned-byte 64))
1630   "recode as leas, shifts and adds"
1631   (let ((y (lvar-value y)))
1632     (*-transformer y)))
1633 (deftransform sb!vm::*-mod64
1634     ((x y) ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1635      (unsigned-byte 64))
1636   "recode as leas, shifts and adds"
1637   (let ((y (lvar-value y)))
1638     (*-transformer y)))
1639
1640 (deftransform * ((x y)
1641                  ((signed-byte 61) (constant-arg (unsigned-byte 64)))
1642                  (signed-byte 61))
1643   "recode as leas, shifts and adds"
1644   (let ((y (lvar-value y)))
1645     (*-transformer y)))
1646 (deftransform sb!vm::*-smod61
1647     ((x y) ((signed-byte 61) (constant-arg (unsigned-byte 64)))
1648      (signed-byte 61))
1649   "recode as leas, shifts and adds"
1650   (let ((y (lvar-value y)))
1651     (*-transformer y)))