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