10.\7f\7fCVS: ----------------------------------------------------------------------
[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 r
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 (r :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 r 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 :base number :index number)))
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 :base number :index number)))
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 :base number :index number)))
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                         (zeroize 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     (zeroize 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     (zeroize 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     (zeroize 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     (zeroize 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 (defmacro define-mod-binop ((name prototype) function)
1211   `(define-vop (,name ,prototype)
1212        (:args (x :target r :scs (unsigned-reg signed-reg)
1213                  :load-if (not (and (or (sc-is x unsigned-stack)
1214                                         (sc-is x signed-stack))
1215                                     (or (sc-is y unsigned-reg)
1216                                         (sc-is y signed-reg))
1217                                     (or (sc-is r unsigned-stack)
1218                                         (sc-is r signed-stack))
1219                                     (location= x r))))
1220               (y :scs (unsigned-reg signed-reg unsigned-stack signed-stack)))
1221      (:arg-types untagged-num untagged-num)
1222      (:results (r :scs (unsigned-reg signed-reg) :from (:argument 0)
1223                   :load-if (not (and (or (sc-is x unsigned-stack)
1224                                          (sc-is x signed-stack))
1225                                      (or (sc-is y unsigned-reg)
1226                                          (sc-is y unsigned-reg))
1227                                      (or (sc-is r unsigned-stack)
1228                                          (sc-is r unsigned-stack))
1229                                      (location= x r)))))
1230      (:result-types unsigned-num)
1231      (:translate ,function)))
1232 (defmacro define-mod-binop-c ((name prototype) function)
1233   `(define-vop (,name ,prototype)
1234        (:args (x :target r :scs (unsigned-reg signed-reg)
1235                  :load-if (not (and (or (sc-is x unsigned-stack)
1236                                         (sc-is x signed-stack))
1237                                     (or (sc-is r unsigned-stack)
1238                                         (sc-is r signed-stack))
1239                                     (location= x r)))))
1240      (:info y)
1241      (:arg-types untagged-num (:constant (or (unsigned-byte 31) (signed-byte 32))))
1242      (:results (r :scs (unsigned-reg signed-reg) :from (:argument 0)
1243                   :load-if (not (and (or (sc-is x unsigned-stack)
1244                                          (sc-is x signed-stack))
1245                                      (or (sc-is r unsigned-stack)
1246                                          (sc-is r unsigned-stack))
1247                                      (location= x r)))))
1248      (:result-types unsigned-num)
1249      (:translate ,function)))
1250
1251 (macrolet ((def (name -c-p)
1252              (let ((fun64 (intern (format nil "~S-MOD64" name)))
1253                    (vopu (intern (format nil "FAST-~S/UNSIGNED=>UNSIGNED" name)))
1254                    (vopcu (intern (format nil "FAST-~S-C/UNSIGNED=>UNSIGNED" name)))
1255                    (vopf (intern (format nil "FAST-~S/FIXNUM=>FIXNUM" name)))
1256                    (vopcf (intern (format nil "FAST-~S-C/FIXNUM=>FIXNUM" name)))
1257                    (vop64u (intern (format nil "FAST-~S-MOD64/WORD=>UNSIGNED" name)))
1258                    (vop64f (intern (format nil "FAST-~S-MOD64/FIXNUM=>FIXNUM" name)))
1259                    (vop64cu (intern (format nil "FAST-~S-MOD64-C/WORD=>UNSIGNED" name)))
1260                    (vop64cf (intern (format nil "FAST-~S-MOD64-C/FIXNUM=>FIXNUM" name)))
1261                    (sfun61 (intern (format nil "~S-SMOD61" name)))
1262                    (svop61f (intern (format nil "FAST-~S-SMOD61/FIXNUM=>FIXNUM" name)))
1263                    (svop61cf (intern (format nil "FAST-~S-SMOD61-C/FIXNUM=>FIXNUM" name))))
1264                `(progn
1265                   (define-modular-fun ,fun64 (x y) ,name :unsigned 64)
1266                   (define-modular-fun ,sfun61 (x y) ,name :signed 61)
1267                   (define-mod-binop (,vop64u ,vopu) ,fun64)
1268                   (define-vop (,vop64f ,vopf) (:translate ,fun64))
1269                   (define-vop (,svop61f ,vopf) (:translate ,sfun61))
1270                   ,@(when -c-p
1271                       `((define-mod-binop-c (,vop64cu ,vopcu) ,fun64)
1272                         (define-vop (,svop61cf ,vopcf) (:translate ,sfun61))))))))
1273   (def + t)
1274   (def - t)
1275   ;; (no -C variant as x86 MUL instruction doesn't take an immediate)
1276   (def * nil))
1277
1278 (define-vop (fast-ash-left-mod64-c/unsigned=>unsigned
1279              fast-ash-c/unsigned=>unsigned)
1280   (:translate ash-left-mod64))
1281 (define-vop (fast-ash-left-mod64/unsigned=>unsigned
1282              fast-ash-left/unsigned=>unsigned))
1283 (deftransform ash-left-mod64 ((integer count)
1284                               ((unsigned-byte 64) (unsigned-byte 6)))
1285   (when (sb!c::constant-lvar-p count)
1286     (sb!c::give-up-ir1-transform))
1287   '(%primitive fast-ash-left-mod64/unsigned=>unsigned integer count))
1288
1289 (define-vop (fast-ash-left-smod61-c/fixnum=>fixnum
1290              fast-ash-c/fixnum=>fixnum)
1291   (:translate ash-left-smod61))
1292 (define-vop (fast-ash-left-smod61/fixnum=>fixnum
1293              fast-ash-left/fixnum=>fixnum))
1294 (deftransform ash-left-smod61 ((integer count)
1295                                ((signed-byte 61) (unsigned-byte 6)))
1296   (when (sb!c::constant-lvar-p count)
1297     (sb!c::give-up-ir1-transform))
1298   '(%primitive fast-ash-left-smod61/fixnum=>fixnum integer count))
1299
1300 (in-package "SB!C")
1301
1302 (defknown sb!vm::%lea-mod64 (integer integer (member 1 2 4 8) (signed-byte 64))
1303   (unsigned-byte 64)
1304   (foldable flushable movable))
1305 (defknown sb!vm::%lea-smod61 (integer integer (member 1 2 4 8) (signed-byte 64))
1306   (signed-byte 61)
1307   (foldable flushable movable))
1308
1309 (define-modular-fun-optimizer %lea ((base index scale disp) :unsigned :width width)
1310   (when (and (<= width 64)
1311              (constant-lvar-p scale)
1312              (constant-lvar-p disp))
1313     (cut-to-width base :unsigned width)
1314     (cut-to-width index :unsigned width)
1315     'sb!vm::%lea-mod64))
1316 (define-modular-fun-optimizer %lea ((base index scale disp) :signed :width width)
1317   (when (and (<= width 61)
1318              (constant-lvar-p scale)
1319              (constant-lvar-p disp))
1320     (cut-to-width base :signed width)
1321     (cut-to-width index :signed width)
1322     'sb!vm::%lea-smod61))
1323
1324 #+sb-xc-host
1325 (progn
1326   (defun sb!vm::%lea-mod64 (base index scale disp)
1327     (ldb (byte 64 0) (%lea base index scale disp)))
1328   (defun sb!vm::%lea-smod61 (base index scale disp)
1329     (mask-signed-field 61 (%lea base index scale disp))))
1330 #-sb-xc-host
1331 (progn
1332   (defun sb!vm::%lea-mod64 (base index scale disp)
1333     (let ((base (logand base #xffffffffffffffff))
1334           (index (logand index #xffffffffffffffff)))
1335       ;; can't use modular version of %LEA, as we only have VOPs for
1336       ;; constant SCALE and DISP.
1337       (ldb (byte 64 0) (+ base (* index scale) disp))))
1338   (defun sb!vm::%lea-smod61 (base index scale disp)
1339     (let ((base (mask-signed-field 61 base))
1340           (index (mask-signed-field 61 index)))
1341       ;; can't use modular version of %LEA, as we only have VOPs for
1342       ;; constant SCALE and DISP.
1343       (mask-signed-field 61 (+ base (* index scale) disp)))))
1344
1345 (in-package "SB!VM")
1346
1347 (define-vop (%lea-mod64/unsigned=>unsigned
1348              %lea/unsigned=>unsigned)
1349   (:translate %lea-mod64))
1350 (define-vop (%lea-smod61/fixnum=>fixnum
1351              %lea/fixnum=>fixnum)
1352   (:translate %lea-smod61))
1353
1354 ;;; logical operations
1355 (define-modular-fun lognot-mod64 (x) lognot :unsigned 64)
1356 (define-vop (lognot-mod64/unsigned=>unsigned)
1357   (:translate lognot-mod64)
1358   (:args (x :scs (unsigned-reg unsigned-stack) :target r
1359             :load-if (not (and (sc-is x unsigned-stack)
1360                                (sc-is r unsigned-stack)
1361                                (location= x r)))))
1362   (:arg-types unsigned-num)
1363   (:results (r :scs (unsigned-reg)
1364                :load-if (not (and (sc-is x unsigned-stack)
1365                                   (sc-is r unsigned-stack)
1366                                   (location= x r)))))
1367   (:result-types unsigned-num)
1368   (:policy :fast-safe)
1369   (:generator 1
1370     (move r x)
1371     (inst not r)))
1372
1373 (define-modular-fun logxor-mod64 (x y) logxor :unsigned 64)
1374 (define-mod-binop (fast-logxor-mod64/word=>unsigned
1375                    fast-logxor/unsigned=>unsigned)
1376     logxor-mod64)
1377 (define-mod-binop-c (fast-logxor-mod64-c/word=>unsigned
1378                      fast-logxor-c/unsigned=>unsigned)
1379     logxor-mod64)
1380 (define-vop (fast-logxor-mod64/fixnum=>fixnum
1381              fast-logxor/fixnum=>fixnum)
1382   (:translate logxor-mod64))
1383 (define-vop (fast-logxor-mod64-c/fixnum=>fixnum
1384              fast-logxor-c/fixnum=>fixnum)
1385   (:translate logxor-mod64))
1386
1387 (define-source-transform logeqv (&rest args)
1388   (if (oddp (length args))
1389       `(logxor ,@args)
1390       `(lognot (logxor ,@args))))
1391 (define-source-transform logandc1 (x y)
1392   `(logand (lognot ,x) ,y))
1393 (define-source-transform logandc2 (x y)
1394   `(logand ,x (lognot ,y)))
1395 (define-source-transform logorc1 (x y)
1396   `(logior (lognot ,x) ,y))
1397 (define-source-transform logorc2 (x y)
1398   `(logior ,x (lognot ,y)))
1399 (define-source-transform lognor (x y)
1400   `(lognot (logior ,x ,y)))
1401 (define-source-transform lognand (x y)
1402   `(lognot (logand ,x ,y)))
1403 \f
1404 ;;;; bignum stuff
1405
1406 (define-vop (bignum-length get-header-data)
1407   (:translate sb!bignum:%bignum-length)
1408   (:policy :fast-safe))
1409
1410 (define-vop (bignum-set-length set-header-data)
1411   (:translate sb!bignum:%bignum-set-length)
1412   (:policy :fast-safe))
1413
1414 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1415   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1416 (define-full-reffer+offset bignum--ref-with-offset * bignum-digits-offset
1417   other-pointer-lowtag (unsigned-reg) unsigned-num
1418   sb!bignum:%bignum-ref-with-offset)
1419 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1420   (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1421
1422 (define-vop (digit-0-or-plus)
1423   (:translate sb!bignum:%digit-0-or-plusp)
1424   (:policy :fast-safe)
1425   (:args (digit :scs (unsigned-reg)))
1426   (:arg-types unsigned-num)
1427   (:conditional)
1428   (:info target not-p)
1429   (:generator 3
1430     (inst or digit digit)
1431     (inst jmp (if not-p :s :ns) target)))
1432
1433
1434 ;;; For add and sub with carry the sc of carry argument is any-reg so
1435 ;;; that it may be passed as a fixnum or word and thus may be 0, 1, or
1436 ;;; 8. This is easy to deal with and may save a fixnum-word
1437 ;;; conversion.
1438 (define-vop (add-w/carry)
1439   (:translate sb!bignum:%add-with-carry)
1440   (:policy :fast-safe)
1441   (:args (a :scs (unsigned-reg) :target result)
1442          (b :scs (unsigned-reg unsigned-stack) :to :eval)
1443          (c :scs (any-reg) :target temp))
1444   (:arg-types unsigned-num unsigned-num positive-fixnum)
1445   (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1446   (:results (result :scs (unsigned-reg) :from (:argument 0))
1447             (carry :scs (unsigned-reg)))
1448   (:result-types unsigned-num positive-fixnum)
1449   (:generator 4
1450     (move result a)
1451     (move temp c)
1452     (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1453     (inst adc result b)
1454     (inst mov carry 0)
1455     (inst adc carry carry)))
1456
1457 ;;; Note: the borrow is 1 for no borrow and 0 for a borrow, the opposite
1458 ;;; of the x86-64 convention.
1459 (define-vop (sub-w/borrow)
1460   (:translate sb!bignum:%subtract-with-borrow)
1461   (:policy :fast-safe)
1462   (:args (a :scs (unsigned-reg) :to :eval :target result)
1463          (b :scs (unsigned-reg unsigned-stack) :to :result)
1464          (c :scs (any-reg control-stack)))
1465   (:arg-types unsigned-num unsigned-num positive-fixnum)
1466   (:results (result :scs (unsigned-reg) :from :eval)
1467             (borrow :scs (unsigned-reg)))
1468   (:result-types unsigned-num positive-fixnum)
1469   (:generator 5
1470     (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1471     (move result a)
1472     (inst sbb result b)
1473     (inst mov borrow 1)
1474     (inst sbb borrow 0)))
1475
1476
1477 (define-vop (bignum-mult-and-add-3-arg)
1478   (:translate sb!bignum:%multiply-and-add)
1479   (:policy :fast-safe)
1480   (:args (x :scs (unsigned-reg) :target eax)
1481          (y :scs (unsigned-reg unsigned-stack))
1482          (carry-in :scs (unsigned-reg unsigned-stack)))
1483   (:arg-types unsigned-num unsigned-num unsigned-num)
1484   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1485                    :to (:result 1) :target lo) eax)
1486   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1487                    :to (:result 0) :target hi) edx)
1488   (:results (hi :scs (unsigned-reg))
1489             (lo :scs (unsigned-reg)))
1490   (:result-types unsigned-num unsigned-num)
1491   (:generator 20
1492     (move eax x)
1493     (inst mul eax y)
1494     (inst add eax carry-in)
1495     (inst adc edx 0)
1496     (move hi edx)
1497     (move lo eax)))
1498
1499 (define-vop (bignum-mult-and-add-4-arg)
1500   (:translate sb!bignum:%multiply-and-add)
1501   (:policy :fast-safe)
1502   (:args (x :scs (unsigned-reg) :target eax)
1503          (y :scs (unsigned-reg unsigned-stack))
1504          (prev :scs (unsigned-reg unsigned-stack))
1505          (carry-in :scs (unsigned-reg unsigned-stack)))
1506   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1507   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1508                    :to (:result 1) :target lo) eax)
1509   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1510                    :to (:result 0) :target hi) edx)
1511   (:results (hi :scs (unsigned-reg))
1512             (lo :scs (unsigned-reg)))
1513   (:result-types unsigned-num unsigned-num)
1514   (:generator 20
1515     (move eax x)
1516     (inst mul eax y)
1517     (inst add eax prev)
1518     (inst adc edx 0)
1519     (inst add eax carry-in)
1520     (inst adc edx 0)
1521     (move hi edx)
1522     (move lo eax)))
1523
1524
1525 (define-vop (bignum-mult)
1526   (:translate sb!bignum:%multiply)
1527   (:policy :fast-safe)
1528   (:args (x :scs (unsigned-reg) :target eax)
1529          (y :scs (unsigned-reg unsigned-stack)))
1530   (:arg-types unsigned-num unsigned-num)
1531   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1532                    :to (:result 1) :target lo) eax)
1533   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1534                    :to (:result 0) :target hi) edx)
1535   (:results (hi :scs (unsigned-reg))
1536             (lo :scs (unsigned-reg)))
1537   (:result-types unsigned-num unsigned-num)
1538   (:generator 20
1539     (move eax x)
1540     (inst mul eax y)
1541     (move hi edx)
1542     (move lo eax)))
1543
1544 (define-vop (bignum-lognot lognot-mod64/unsigned=>unsigned)
1545   (:translate sb!bignum:%lognot))
1546
1547 (define-vop (fixnum-to-digit)
1548   (:translate sb!bignum:%fixnum-to-digit)
1549   (:policy :fast-safe)
1550   (:args (fixnum :scs (any-reg control-stack) :target digit))
1551   (:arg-types tagged-num)
1552   (:results (digit :scs (unsigned-reg)
1553                    :load-if (not (and (sc-is fixnum control-stack)
1554                                       (sc-is digit unsigned-stack)
1555                                       (location= fixnum digit)))))
1556   (:result-types unsigned-num)
1557   (:generator 1
1558     (move digit fixnum)
1559     (inst sar digit 3)))
1560
1561 (define-vop (bignum-floor)
1562   (:translate sb!bignum:%floor)
1563   (:policy :fast-safe)
1564   (:args (div-high :scs (unsigned-reg) :target edx)
1565          (div-low :scs (unsigned-reg) :target eax)
1566          (divisor :scs (unsigned-reg unsigned-stack)))
1567   (:arg-types unsigned-num unsigned-num unsigned-num)
1568   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1569                    :to (:result 0) :target quo) eax)
1570   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1571                    :to (:result 1) :target rem) edx)
1572   (:results (quo :scs (unsigned-reg))
1573             (rem :scs (unsigned-reg)))
1574   (:result-types unsigned-num unsigned-num)
1575   (:generator 300
1576     (move edx div-high)
1577     (move eax div-low)
1578     (inst div eax divisor)
1579     (move quo eax)
1580     (move rem edx)))
1581
1582 (define-vop (signify-digit)
1583   (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1584   (:policy :fast-safe)
1585   (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1586   (:arg-types unsigned-num)
1587   (:results (res :scs (any-reg signed-reg)
1588                  :load-if (not (and (sc-is digit unsigned-stack)
1589                                     (sc-is res control-stack signed-stack)
1590                                     (location= digit res)))))
1591   (:result-types signed-num)
1592   (:generator 1
1593     (move res digit)
1594     (when (sc-is res any-reg control-stack)
1595       (inst shl res 3))))
1596
1597 (define-vop (digit-ashr)
1598   (:translate sb!bignum:%ashr)
1599   (:policy :fast-safe)
1600   (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1601          (count :scs (unsigned-reg) :target ecx))
1602   (:arg-types unsigned-num positive-fixnum)
1603   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1604   (:results (result :scs (unsigned-reg) :from (:argument 0)
1605                     :load-if (not (and (sc-is result unsigned-stack)
1606                                        (location= digit result)))))
1607   (:result-types unsigned-num)
1608   (:generator 2
1609     (move result digit)
1610     (move ecx count)
1611     (inst sar result :cl)))
1612
1613 (define-vop (digit-ashr/c)
1614   (:translate sb!bignum:%ashr)
1615   (:policy :fast-safe)
1616   (:args (digit :scs (unsigned-reg unsigned-stack) :target result))
1617   (:arg-types unsigned-num (:constant (integer 0 63)))
1618   (:info count)
1619   (:results (result :scs (unsigned-reg) :from (:argument 0)
1620                     :load-if (not (and (sc-is result unsigned-stack)
1621                                        (location= digit result)))))
1622   (:result-types unsigned-num)
1623   (:generator 1
1624     (move result digit)
1625     (inst sar result count)))
1626
1627 (define-vop (digit-lshr digit-ashr)
1628   (:translate sb!bignum:%digit-logical-shift-right)
1629   (:generator 1
1630     (move result digit)
1631     (move ecx count)
1632     (inst shr result :cl)))
1633
1634 (define-vop (digit-ashl digit-ashr)
1635   (:translate sb!bignum:%ashl)
1636   (:generator 1
1637     (move result digit)
1638     (move ecx count)
1639     (inst shl result :cl)))
1640 \f
1641 ;;;; static functions
1642
1643 (define-static-fun two-arg-/ (x y) :translate /)
1644
1645 (define-static-fun two-arg-gcd (x y) :translate gcd)
1646 (define-static-fun two-arg-lcm (x y) :translate lcm)
1647
1648 (define-static-fun two-arg-and (x y) :translate logand)
1649 (define-static-fun two-arg-ior (x y) :translate logior)
1650 (define-static-fun two-arg-xor (x y) :translate logxor)
1651
1652
1653 (in-package "SB!C")
1654
1655 (defun *-transformer (y)
1656   (cond
1657     ((= y (ash 1 (integer-length y)))
1658      ;; there's a generic transform for y = 2^k
1659      (give-up-ir1-transform))
1660     ((member y '(3 5 9))
1661      ;; we can do these multiplications directly using LEA
1662      `(%lea x x ,(1- y) 0))
1663     (t
1664      ;; A normal 64-bit multiplication takes 4 cycles on Athlon 64/Opteron.
1665      ;; Optimizing multiplications (other than the above cases) to
1666      ;; shifts/adds/leas gives a maximum improvement of 1 cycle, but requires
1667      ;; quite a lot of hairy code.
1668      (give-up-ir1-transform))))
1669
1670 (deftransform * ((x y)
1671                  ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1672                  (unsigned-byte 64))
1673   "recode as leas, shifts and adds"
1674   (let ((y (lvar-value y)))
1675     (*-transformer y)))
1676 (deftransform sb!vm::*-mod64
1677     ((x y) ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1678      (unsigned-byte 64))
1679   "recode as leas, shifts and adds"
1680   (let ((y (lvar-value y)))
1681     (*-transformer y)))
1682
1683 (deftransform * ((x y)
1684                  ((signed-byte 61) (constant-arg (unsigned-byte 64)))
1685                  (signed-byte 61))
1686   "recode as leas, shifts and adds"
1687   (let ((y (lvar-value y)))
1688     (*-transformer y)))
1689 (deftransform sb!vm::*-smod61
1690     ((x y) ((signed-byte 61) (constant-arg (unsigned-byte 64)))
1691      (signed-byte 61))
1692   "recode as leas, shifts and adds"
1693   (let ((y (lvar-value y)))
1694     (*-transformer y)))