1.0.10.36: support for Darwin versions that support __DARWIN_UNIX03
[sbcl.git] / src / compiler / x86 / arith.lisp
1 ;;;; the VM definition of arithmetic VOPs for the x86
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; unary operations
15
16 (define-vop (fast-safe-arith-op)
17   (:policy :fast-safe)
18   (:effects)
19   (:affected))
20
21 (define-vop (fixnum-unop fast-safe-arith-op)
22   (:args (x :scs (any-reg) :target res))
23   (:results (res :scs (any-reg)))
24   (:note "inline fixnum arithmetic")
25   (:arg-types tagged-num)
26   (:result-types tagged-num))
27
28 (define-vop (signed-unop fast-safe-arith-op)
29   (:args (x :scs (signed-reg) :target res))
30   (:results (res :scs (signed-reg)))
31   (:note "inline (signed-byte 32) arithmetic")
32   (:arg-types signed-num)
33   (:result-types signed-num))
34
35 (define-vop (fast-negate/fixnum fixnum-unop)
36   (:translate %negate)
37   (:generator 1
38     (move res x)
39     (inst neg res)))
40
41 (define-vop (fast-negate/signed signed-unop)
42   (:translate %negate)
43   (:generator 2
44     (move res x)
45     (inst neg res)))
46
47 (define-vop (fast-lognot/fixnum fixnum-unop)
48   (:translate lognot)
49   (:generator 2
50     (move res x)
51     (inst xor res (fixnumize -1))))
52
53 (define-vop (fast-lognot/signed signed-unop)
54   (:translate lognot)
55   (:generator 1
56     (move res x)
57     (inst not res)))
58 \f
59 ;;;; binary fixnum operations
60
61 ;;; Assume that any constant operand is the second arg...
62
63 (define-vop (fast-fixnum-binop fast-safe-arith-op)
64   (:args (x :target r :scs (any-reg)
65             :load-if (not (and (sc-is x control-stack)
66                                (sc-is y any-reg)
67                                (sc-is r control-stack)
68                                (location= x r))))
69          (y :scs (any-reg control-stack)))
70   (:arg-types tagged-num tagged-num)
71   (:results (r :scs (any-reg) :from (:argument 0)
72                :load-if (not (and (sc-is x control-stack)
73                                   (sc-is y any-reg)
74                                   (sc-is r control-stack)
75                                   (location= x r)))))
76   (:result-types tagged-num)
77   (:note "inline fixnum arithmetic"))
78
79 (define-vop (fast-unsigned-binop fast-safe-arith-op)
80   (:args (x :target r :scs (unsigned-reg)
81             :load-if (not (and (sc-is x unsigned-stack)
82                                (sc-is y unsigned-reg)
83                                (sc-is r unsigned-stack)
84                                (location= x r))))
85          (y :scs (unsigned-reg unsigned-stack)))
86   (:arg-types unsigned-num unsigned-num)
87   (:results (r :scs (unsigned-reg) :from (:argument 0)
88             :load-if (not (and (sc-is x unsigned-stack)
89                                (sc-is y unsigned-reg)
90                                (sc-is r unsigned-stack)
91                                (location= x r)))))
92   (:result-types unsigned-num)
93   (:note "inline (unsigned-byte 32) arithmetic"))
94
95 (define-vop (fast-signed-binop fast-safe-arith-op)
96   (:args (x :target r :scs (signed-reg)
97             :load-if (not (and (sc-is x signed-stack)
98                                (sc-is y signed-reg)
99                                (sc-is r signed-stack)
100                                (location= x r))))
101          (y :scs (signed-reg signed-stack)))
102   (:arg-types signed-num signed-num)
103   (:results (r :scs (signed-reg) :from (:argument 0)
104             :load-if (not (and (sc-is x signed-stack)
105                                (sc-is y signed-reg)
106                                (sc-is r signed-stack)
107                                (location= x r)))))
108   (:result-types signed-num)
109   (:note "inline (signed-byte 32) arithmetic"))
110
111 (define-vop (fast-fixnum-binop-c fast-safe-arith-op)
112   (:args (x :target r :scs (any-reg control-stack)))
113   (:info y)
114   (:arg-types tagged-num (:constant (signed-byte 30)))
115   (:results (r :scs (any-reg)
116                :load-if (not (location= x r))))
117   (:result-types tagged-num)
118   (:note "inline fixnum arithmetic"))
119
120 (define-vop (fast-unsigned-binop-c fast-safe-arith-op)
121   (:args (x :target r :scs (unsigned-reg unsigned-stack)))
122   (:info y)
123   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
124   (:results (r :scs (unsigned-reg)
125                :load-if (not (location= x r))))
126   (:result-types unsigned-num)
127   (:note "inline (unsigned-byte 32) arithmetic"))
128
129 (define-vop (fast-signed-binop-c fast-safe-arith-op)
130   (:args (x :target r :scs (signed-reg signed-stack)))
131   (:info y)
132   (:arg-types signed-num (:constant (signed-byte 32)))
133   (:results (r :scs (signed-reg)
134                :load-if (not (location= x r))))
135   (:result-types signed-num)
136   (:note "inline (signed-byte 32) arithmetic"))
137
138 (macrolet ((define-binop (translate untagged-penalty op)
139              `(progn
140                 (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
141                              fast-fixnum-binop)
142                   (:translate ,translate)
143                   (:generator 2
144                               (move r x)
145                               (inst ,op r y)))
146                 (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
147                              fast-fixnum-binop-c)
148                   (:translate ,translate)
149                   (:generator 1
150                   (move r x)
151                   (inst ,op r (fixnumize y))))
152                 (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
153                              fast-signed-binop)
154                   (:translate ,translate)
155                   (:generator ,(1+ untagged-penalty)
156                   (move r x)
157                   (inst ,op r y)))
158                 (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
159                              fast-signed-binop-c)
160                   (:translate ,translate)
161                   (:generator ,untagged-penalty
162                   (move r x)
163                   (inst ,op r y)))
164                 (define-vop (,(symbolicate "FAST-"
165                                            translate
166                                            "/UNSIGNED=>UNSIGNED")
167                 fast-unsigned-binop)
168                   (:translate ,translate)
169                   (:generator ,(1+ untagged-penalty)
170                   (move r x)
171                   (inst ,op r y)))
172                 (define-vop (,(symbolicate 'fast-
173                                            translate
174                                            '-c/unsigned=>unsigned)
175                              fast-unsigned-binop-c)
176                   (:translate ,translate)
177                   (:generator ,untagged-penalty
178                   (move r x)
179                   ,(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     (inst cmp res 0)
927     (inst jmp :ge POS)
928     (inst not res)
929     POS
930     (inst bsr res res)
931     (inst jmp :z zero)
932     (inst inc res)
933     (inst jmp done)
934     ZERO
935     (inst xor res res)
936     DONE))
937
938 (define-vop (unsigned-byte-32-len)
939   (:translate integer-length)
940   (:note "inline (unsigned-byte 32) integer-length")
941   (:policy :fast-safe)
942   (:args (arg :scs (unsigned-reg)))
943   (:arg-types unsigned-num)
944   (:results (res :scs (unsigned-reg)))
945   (:result-types unsigned-num)
946   (:generator 26
947     (inst bsr res arg)
948     (inst jmp :z zero)
949     (inst inc res)
950     (inst jmp done)
951     ZERO
952     (inst xor res res)
953     DONE))
954
955 (define-vop (unsigned-byte-32-count)
956   (:translate logcount)
957   (:note "inline (unsigned-byte 32) logcount")
958   (:policy :fast-safe)
959   (:args (arg :scs (unsigned-reg) :target result))
960   (:arg-types unsigned-num)
961   (:results (result :scs (unsigned-reg)))
962   (:result-types positive-fixnum)
963   (:temporary (:sc unsigned-reg) temp)
964   (:generator 14
965     ;; See the comments below for how the algorithm works. The tricks
966     ;; used can be found for example in AMD's software optimization
967     ;; guide or at "http://www.hackersdelight.org/HDcode/pop.cc" in the
968     ;; function "pop1".
969     ;; Calculate 2-bit sums. Note that the value of a two-digit binary
970     ;; number is the sum of the right digit and twice the left digit.
971     ;; Thus we can calculate the sum of the two digits by shifting the
972     ;; left digit to the right position and doing a two-bit subtraction.
973     ;; This subtraction will never create a borrow and thus can be made
974     ;; on all 16 2-digit numbers at once.
975     (move result arg)
976     (move temp arg)
977     (inst shr result 1)
978     (inst and result #x55555555)
979     (inst sub temp result)
980     ;; Calculate 4-bit sums by straightforward shift, mask and add.
981     ;; Note that we shift the source operand of the MOV and not its
982     ;; destination so that the SHR and the MOV can execute in the same
983     ;; clock cycle.
984     (inst mov result temp)
985     (inst shr temp 2)
986     (inst and result #x33333333)
987     (inst and temp #x33333333)
988     (inst add result temp)
989     ;; Calculate 8-bit sums. Since each sum is at most 8, which fits
990     ;; into 4 bits, we can apply the mask after the addition, saving one
991     ;; instruction.
992     (inst mov temp result)
993     (inst shr result 4)
994     (inst add result temp)
995     (inst and result #x0f0f0f0f)
996     ;; Calculate the two 16-bit sums and the 32-bit sum. No masking is
997     ;; necessary inbetween since the final sum is at most 32 which fits
998     ;; into 6 bits.
999     (inst mov temp result)
1000     (inst shr result 8)
1001     (inst add result temp)
1002     (inst mov temp result)
1003     (inst shr result 16)
1004     (inst add result temp)
1005     (inst and result #xff)))
1006 \f
1007 ;;;; binary conditional VOPs
1008
1009 (define-vop (fast-conditional)
1010   (:conditional)
1011   (:info target not-p)
1012   (:effects)
1013   (:affected)
1014   (:policy :fast-safe))
1015
1016 (define-vop (fast-conditional/fixnum fast-conditional)
1017   (:args (x :scs (any-reg)
1018             :load-if (not (and (sc-is x control-stack)
1019                                (sc-is y any-reg))))
1020          (y :scs (any-reg control-stack)))
1021   (:arg-types tagged-num tagged-num)
1022   (:note "inline fixnum comparison"))
1023
1024 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
1025   (:args (x :scs (any-reg control-stack)))
1026   (:arg-types tagged-num (:constant (signed-byte 30)))
1027   (:info target not-p y))
1028
1029 (define-vop (fast-conditional/signed fast-conditional)
1030   (:args (x :scs (signed-reg)
1031             :load-if (not (and (sc-is x signed-stack)
1032                                (sc-is y signed-reg))))
1033          (y :scs (signed-reg signed-stack)))
1034   (:arg-types signed-num signed-num)
1035   (:note "inline (signed-byte 32) comparison"))
1036
1037 (define-vop (fast-conditional-c/signed fast-conditional/signed)
1038   (:args (x :scs (signed-reg signed-stack)))
1039   (:arg-types signed-num (:constant (signed-byte 32)))
1040   (:info target not-p y))
1041
1042 (define-vop (fast-conditional/unsigned fast-conditional)
1043   (:args (x :scs (unsigned-reg)
1044             :load-if (not (and (sc-is x unsigned-stack)
1045                                (sc-is y unsigned-reg))))
1046          (y :scs (unsigned-reg unsigned-stack)))
1047   (:arg-types unsigned-num unsigned-num)
1048   (:note "inline (unsigned-byte 32) comparison"))
1049
1050 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
1051   (:args (x :scs (unsigned-reg unsigned-stack)))
1052   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
1053   (:info target not-p y))
1054
1055 (macrolet ((define-logtest-vops ()
1056              `(progn
1057                ,@(loop for suffix in '(/fixnum -c/fixnum
1058                                        /signed -c/signed
1059                                        /unsigned -c/unsigned)
1060                        for cost in '(4 3 6 5 6 5)
1061                        collect
1062                        `(define-vop (,(symbolicate "FAST-LOGTEST" suffix)
1063                                      ,(symbolicate "FAST-CONDITIONAL" suffix))
1064                          (:translate logtest)
1065                          (:generator ,cost
1066                           (emit-optimized-test-inst x
1067                                                     ,(if (eq suffix '-c/fixnum)
1068                                                          '(fixnumize y)
1069                                                          'y))
1070                           (inst jmp (if not-p :e :ne) target)))))))
1071   (define-logtest-vops))
1072
1073 (defknown %logbitp (integer unsigned-byte) boolean
1074   (movable foldable flushable always-translatable))
1075
1076 ;;; only for constant folding within the compiler
1077 (defun %logbitp (integer index)
1078   (logbitp index integer))
1079
1080 ;;; too much work to do the non-constant case (maybe?)
1081 (define-vop (fast-logbitp-c/fixnum fast-conditional-c/fixnum)
1082   (:translate %logbitp)
1083   (:arg-types tagged-num (:constant (integer 0 29)))
1084   (:generator 4
1085     (inst bt x (+ y n-fixnum-tag-bits))
1086     (inst jmp (if not-p :nc :c) target)))
1087
1088 (define-vop (fast-logbitp/signed fast-conditional/signed)
1089   (:args (x :scs (signed-reg signed-stack))
1090          (y :scs (signed-reg)))
1091   (:translate %logbitp)
1092   (:generator 6
1093     (inst bt x y)
1094     (inst jmp (if not-p :nc :c) target)))
1095
1096 (define-vop (fast-logbitp-c/signed fast-conditional-c/signed)
1097   (:translate %logbitp)
1098   (:arg-types signed-num (:constant (integer 0 31)))
1099   (:generator 5
1100     (inst bt x y)
1101     (inst jmp (if not-p :nc :c) target)))
1102
1103 (define-vop (fast-logbitp/unsigned fast-conditional/unsigned)
1104   (:args (x :scs (unsigned-reg unsigned-stack))
1105          (y :scs (unsigned-reg)))
1106   (:translate %logbitp)
1107   (:generator 6
1108     (inst bt x y)
1109     (inst jmp (if not-p :nc :c) target)))
1110
1111 (define-vop (fast-logbitp-c/unsigned fast-conditional-c/unsigned)
1112   (:translate %logbitp)
1113   (:arg-types unsigned-num (:constant (integer 0 31)))
1114   (:generator 5
1115     (inst bt x y)
1116     (inst jmp (if not-p :nc :c) target)))
1117
1118 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
1119              `(progn
1120                 ,@(mapcar
1121                    (lambda (suffix cost signed)
1122                      `(define-vop (;; FIXME: These could be done more
1123                                    ;; cleanly with SYMBOLICATE.
1124                                    ,(intern (format nil "~:@(FAST-IF-~A~A~)"
1125                                                     tran suffix))
1126                                    ,(intern
1127                                      (format nil "~:@(FAST-CONDITIONAL~A~)"
1128                                              suffix)))
1129                         (:translate ,tran)
1130                         (:generator ,cost
1131                                     (inst cmp x
1132                                           ,(if (eq suffix '-c/fixnum)
1133                                                '(fixnumize y)
1134                                                'y))
1135                                     (inst jmp (if not-p
1136                                                   ,(if signed
1137                                                        not-cond
1138                                                        not-unsigned)
1139                                                   ,(if signed
1140                                                        cond
1141                                                        unsigned))
1142                                           target))))
1143                    '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
1144                    '(4 3 6 5 6 5)
1145                    '(t t t t nil nil)))))
1146
1147   (define-conditional-vop < :l :b :ge :ae)
1148   (define-conditional-vop > :g :a :le :be))
1149
1150 (define-vop (fast-if-eql/signed fast-conditional/signed)
1151   (:translate eql)
1152   (:generator 6
1153     (inst cmp x y)
1154     (inst jmp (if not-p :ne :e) target)))
1155
1156 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
1157   (:translate eql)
1158   (:generator 5
1159     (cond ((and (sc-is x signed-reg) (zerop y))
1160            (inst test x x))  ; smaller instruction
1161           (t
1162            (inst cmp x y)))
1163     (inst jmp (if not-p :ne :e) target)))
1164
1165 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1166   (:translate eql)
1167   (:generator 6
1168     (inst cmp x y)
1169     (inst jmp (if not-p :ne :e) target)))
1170
1171 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
1172   (:translate eql)
1173   (:generator 5
1174     (cond ((and (sc-is x unsigned-reg) (zerop y))
1175            (inst test x x))  ; smaller instruction
1176           (t
1177            (inst cmp x y)))
1178     (inst jmp (if not-p :ne :e) target)))
1179
1180 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1181 ;;; known fixnum.
1182
1183 ;;; These versions specify a fixnum restriction on their first arg. We have
1184 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1185 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1186 ;;; fixnum specific operations from being used on word integers, spuriously
1187 ;;; consing the argument.
1188
1189 (define-vop (fast-eql/fixnum fast-conditional)
1190   (:args (x :scs (any-reg)
1191             :load-if (not (and (sc-is x control-stack)
1192                                (sc-is y any-reg))))
1193          (y :scs (any-reg control-stack)))
1194   (:arg-types tagged-num tagged-num)
1195   (:note "inline fixnum comparison")
1196   (:translate eql)
1197   (:generator 4
1198     (inst cmp x y)
1199     (inst jmp (if not-p :ne :e) target)))
1200 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1201   (:args (x :scs (any-reg descriptor-reg)
1202             :load-if (not (and (sc-is x control-stack)
1203                                (sc-is y any-reg))))
1204          (y :scs (any-reg control-stack)))
1205   (:arg-types * tagged-num)
1206   (:variant-cost 7))
1207
1208 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1209   (:args (x :scs (any-reg control-stack)))
1210   (:arg-types tagged-num (:constant (signed-byte 30)))
1211   (:info target not-p y)
1212   (:translate eql)
1213   (:generator 2
1214     (cond ((and (sc-is x any-reg) (zerop y))
1215            (inst test x x))  ; smaller instruction
1216           (t
1217            (inst cmp x (fixnumize y))))
1218     (inst jmp (if not-p :ne :e) target)))
1219 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1220   (:args (x :scs (any-reg descriptor-reg control-stack)))
1221   (:arg-types * (:constant (signed-byte 30)))
1222   (:variant-cost 6))
1223 \f
1224 ;;;; 32-bit logical operations
1225
1226 (define-vop (merge-bits)
1227   (:translate merge-bits)
1228   (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
1229          (prev :scs (unsigned-reg) :target result)
1230          (next :scs (unsigned-reg)))
1231   (:arg-types tagged-num unsigned-num unsigned-num)
1232   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
1233   (:results (result :scs (unsigned-reg) :from (:argument 1)))
1234   (:result-types unsigned-num)
1235   (:policy :fast-safe)
1236   (:generator 4
1237     (move ecx shift)
1238     (move result prev)
1239     (inst shrd result next :cl)))
1240
1241 ;;; Only the lower 5 bits of the shift amount are significant.
1242 (define-vop (shift-towards-someplace)
1243   (:policy :fast-safe)
1244   (:args (num :scs (unsigned-reg) :target r)
1245          (amount :scs (signed-reg) :target ecx))
1246   (:arg-types unsigned-num tagged-num)
1247   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1248   (:results (r :scs (unsigned-reg) :from (:argument 0)))
1249   (:result-types unsigned-num))
1250
1251 (define-vop (shift-towards-start shift-towards-someplace)
1252   (:translate shift-towards-start)
1253   (:note "SHIFT-TOWARDS-START")
1254   (:generator 1
1255     (move r num)
1256     (move ecx amount)
1257     (inst shr r :cl)))
1258
1259 (define-vop (shift-towards-end shift-towards-someplace)
1260   (:translate shift-towards-end)
1261   (:note "SHIFT-TOWARDS-END")
1262   (:generator 1
1263     (move r num)
1264     (move ecx amount)
1265     (inst shl r :cl)))
1266 \f
1267 ;;;; Modular functions
1268 (defmacro define-mod-binop ((name prototype) function)
1269   `(define-vop (,name ,prototype)
1270        (:args (x :target r :scs (unsigned-reg signed-reg)
1271                  :load-if (not (and (or (sc-is x unsigned-stack)
1272                                         (sc-is x signed-stack))
1273                                     (or (sc-is y unsigned-reg)
1274                                         (sc-is y signed-reg))
1275                                     (or (sc-is r unsigned-stack)
1276                                         (sc-is r signed-stack))
1277                                     (location= x r))))
1278               (y :scs (unsigned-reg signed-reg unsigned-stack signed-stack)))
1279      (:arg-types untagged-num untagged-num)
1280      (:results (r :scs (unsigned-reg signed-reg) :from (:argument 0)
1281                   :load-if (not (and (or (sc-is x unsigned-stack)
1282                                          (sc-is x signed-stack))
1283                                      (or (sc-is y unsigned-reg)
1284                                          (sc-is y unsigned-reg))
1285                                      (or (sc-is r unsigned-stack)
1286                                          (sc-is r unsigned-stack))
1287                                      (location= x r)))))
1288      (:result-types unsigned-num)
1289      (:translate ,function)))
1290 (defmacro define-mod-binop-c ((name prototype) function)
1291   `(define-vop (,name ,prototype)
1292        (:args (x :target r :scs (unsigned-reg signed-reg)
1293                  :load-if (not (and (or (sc-is x unsigned-stack)
1294                                         (sc-is x signed-stack))
1295                                     (or (sc-is r unsigned-stack)
1296                                         (sc-is r signed-stack))
1297                                     (location= x r)))))
1298      (:info y)
1299      (:arg-types untagged-num (:constant (or (unsigned-byte 32) (signed-byte 32))))
1300      (:results (r :scs (unsigned-reg signed-reg) :from (:argument 0)
1301                   :load-if (not (and (or (sc-is x unsigned-stack)
1302                                          (sc-is x signed-stack))
1303                                      (or (sc-is r unsigned-stack)
1304                                          (sc-is r unsigned-stack))
1305                                      (location= x r)))))
1306      (:result-types unsigned-num)
1307      (:translate ,function)))
1308
1309 (macrolet ((def (name -c-p)
1310              (let ((fun32 (intern (format nil "~S-MOD32" name)))
1311                    (vopu (intern (format nil "FAST-~S/UNSIGNED=>UNSIGNED" name)))
1312                    (vopcu (intern (format nil "FAST-~S-C/UNSIGNED=>UNSIGNED" name)))
1313                    (vopf (intern (format nil "FAST-~S/FIXNUM=>FIXNUM" name)))
1314                    (vopcf (intern (format nil "FAST-~S-C/FIXNUM=>FIXNUM" name)))
1315                    (vop32u (intern (format nil "FAST-~S-MOD32/WORD=>UNSIGNED" name)))
1316                    (vop32f (intern (format nil "FAST-~S-MOD32/FIXNUM=>FIXNUM" name)))
1317                    (vop32cu (intern (format nil "FAST-~S-MOD32-C/WORD=>UNSIGNED" name)))
1318                    (vop32cf (intern (format nil "FAST-~S-MOD32-C/FIXNUM=>FIXNUM" name)))
1319                    (sfun30 (intern (format nil "~S-SMOD30" name)))
1320                    (svop30f (intern (format nil "FAST-~S-SMOD30/FIXNUM=>FIXNUM" name)))
1321                    (svop30cf (intern (format nil "FAST-~S-SMOD30-C/FIXNUM=>FIXNUM" name))))
1322                `(progn
1323                   (define-modular-fun ,fun32 (x y) ,name :unsigned 32)
1324                   (define-modular-fun ,sfun30 (x y) ,name :signed 30)
1325                   (define-mod-binop (,vop32u ,vopu) ,fun32)
1326                   (define-vop (,vop32f ,vopf) (:translate ,fun32))
1327                   (define-vop (,svop30f ,vopf) (:translate ,sfun30))
1328                   ,@(when -c-p
1329                       `((define-mod-binop-c (,vop32cu ,vopcu) ,fun32)
1330                         (define-vop (,svop30cf ,vopcf) (:translate ,sfun30))))))))
1331   (def + t)
1332   (def - t)
1333   ;; (no -C variant as x86 MUL instruction doesn't take an immediate)
1334   (def * nil))
1335
1336
1337 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
1338              fast-ash-c/unsigned=>unsigned)
1339   (:translate ash-left-mod32))
1340
1341 (define-vop (fast-ash-left-mod32/unsigned=>unsigned
1342              fast-ash-left/unsigned=>unsigned))
1343 (deftransform ash-left-mod32 ((integer count)
1344                               ((unsigned-byte 32) (unsigned-byte 5)))
1345   (when (sb!c::constant-lvar-p count)
1346     (sb!c::give-up-ir1-transform))
1347   '(%primitive fast-ash-left-mod32/unsigned=>unsigned integer count))
1348
1349 (define-vop (fast-ash-left-smod30-c/fixnum=>fixnum
1350              fast-ash-c/fixnum=>fixnum)
1351   (:translate ash-left-smod30))
1352
1353 (define-vop (fast-ash-left-smod30/fixnum=>fixnum
1354              fast-ash-left/fixnum=>fixnum))
1355 (deftransform ash-left-smod30 ((integer count)
1356                                ((signed-byte 30) (unsigned-byte 5)))
1357   (when (sb!c::constant-lvar-p count)
1358     (sb!c::give-up-ir1-transform))
1359   '(%primitive fast-ash-left-smod30/fixnum=>fixnum integer count))
1360
1361 (in-package "SB!C")
1362
1363 (defknown sb!vm::%lea-mod32 (integer integer (member 1 2 4 8) (signed-byte 32))
1364   (unsigned-byte 32)
1365   (foldable flushable movable))
1366 (defknown sb!vm::%lea-smod30 (integer integer (member 1 2 4 8) (signed-byte 32))
1367   (signed-byte 30)
1368   (foldable flushable movable))
1369
1370 (define-modular-fun-optimizer %lea ((base index scale disp) :unsigned :width width)
1371   (when (and (<= width 32)
1372              (constant-lvar-p scale)
1373              (constant-lvar-p disp))
1374     (cut-to-width base :unsigned width)
1375     (cut-to-width index :unsigned width)
1376     'sb!vm::%lea-mod32))
1377 (define-modular-fun-optimizer %lea ((base index scale disp) :signed :width width)
1378   (when (and (<= width 30)
1379              (constant-lvar-p scale)
1380              (constant-lvar-p disp))
1381     (cut-to-width base :signed width)
1382     (cut-to-width index :signed width)
1383     'sb!vm::%lea-smod30))
1384
1385 #+sb-xc-host
1386 (progn
1387   (defun sb!vm::%lea-mod32 (base index scale disp)
1388     (ldb (byte 32 0) (%lea base index scale disp)))
1389   (defun sb!vm::%lea-smod30 (base index scale disp)
1390     (mask-signed-field 30 (%lea base index scale disp))))
1391 #-sb-xc-host
1392 (progn
1393   (defun sb!vm::%lea-mod32 (base index scale disp)
1394     (let ((base (logand base #xffffffff))
1395           (index (logand index #xffffffff)))
1396       ;; can't use modular version of %LEA, as we only have VOPs for
1397       ;; constant SCALE and DISP.
1398       (ldb (byte 32 0) (+ base (* index scale) disp))))
1399   (defun sb!vm::%lea-smod30 (base index scale disp)
1400     (let ((base (mask-signed-field 30 base))
1401           (index (mask-signed-field 30 index)))
1402       ;; can't use modular version of %LEA, as we only have VOPs for
1403       ;; constant SCALE and DISP.
1404       (mask-signed-field 30 (+ base (* index scale) disp)))))
1405
1406 (in-package "SB!VM")
1407
1408 (define-vop (%lea-mod32/unsigned=>unsigned
1409              %lea/unsigned=>unsigned)
1410   (:translate %lea-mod32))
1411 (define-vop (%lea-smod30/fixnum=>fixnum
1412              %lea/fixnum=>fixnum)
1413   (:translate %lea-smod30))
1414
1415 ;;; logical operations
1416 (define-modular-fun lognot-mod32 (x) lognot :unsigned 32)
1417 (define-vop (lognot-mod32/word=>unsigned)
1418   (:translate lognot-mod32)
1419   (:args (x :scs (unsigned-reg signed-reg unsigned-stack signed-stack) :target r
1420             :load-if (not (and (or (sc-is x unsigned-stack)
1421                                    (sc-is x signed-stack))
1422                                (or (sc-is r unsigned-stack)
1423                                    (sc-is r signed-stack))
1424                                (location= x r)))))
1425   (:arg-types unsigned-num)
1426   (:results (r :scs (unsigned-reg)
1427                :load-if (not (and (or (sc-is x unsigned-stack)
1428                                       (sc-is x signed-stack))
1429                                   (or (sc-is r unsigned-stack)
1430                                       (sc-is r signed-stack))
1431                                   (sc-is r unsigned-stack)
1432                                   (location= x r)))))
1433   (:result-types unsigned-num)
1434   (:policy :fast-safe)
1435   (:generator 1
1436     (move r x)
1437     (inst not r)))
1438
1439 (define-modular-fun logxor-mod32 (x y) logxor :unsigned 32)
1440 (define-mod-binop (fast-logxor-mod32/word=>unsigned
1441                    fast-logxor/unsigned=>unsigned)
1442     logxor-mod32)
1443 (define-mod-binop-c (fast-logxor-mod32-c/word=>unsigned
1444                      fast-logxor-c/unsigned=>unsigned)
1445     logxor-mod32)
1446 (define-vop (fast-logxor-mod32/fixnum=>fixnum
1447              fast-logxor/fixnum=>fixnum)
1448   (:translate logxor-mod32))
1449 (define-vop (fast-logxor-mod32-c/fixnum=>fixnum
1450              fast-logxor-c/fixnum=>fixnum)
1451   (:translate logxor-mod32))
1452
1453 (define-source-transform logeqv (&rest args)
1454   (if (oddp (length args))
1455       `(logxor ,@args)
1456       `(lognot (logxor ,@args))))
1457 (define-source-transform logandc1 (x y)
1458   `(logand (lognot ,x) ,y))
1459 (define-source-transform logandc2 (x y)
1460   `(logand ,x (lognot ,y)))
1461 (define-source-transform logorc1 (x y)
1462   `(logior (lognot ,x) ,y))
1463 (define-source-transform logorc2 (x y)
1464   `(logior ,x (lognot ,y)))
1465 (define-source-transform lognor (x y)
1466   `(lognot (logior ,x ,y)))
1467 (define-source-transform lognand (x y)
1468   `(lognot (logand ,x ,y)))
1469 \f
1470 ;;;; bignum stuff
1471
1472 (define-vop (bignum-length get-header-data)
1473   (:translate sb!bignum:%bignum-length)
1474   (:policy :fast-safe))
1475
1476 (define-vop (bignum-set-length set-header-data)
1477   (:translate sb!bignum:%bignum-set-length)
1478   (:policy :fast-safe))
1479
1480 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1481   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1482 (define-full-reffer+offset bignum-ref-with-offset *
1483   bignum-digits-offset other-pointer-lowtag
1484   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref-with-offset)
1485 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1486   (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1487
1488 (define-vop (digit-0-or-plus)
1489   (:translate sb!bignum:%digit-0-or-plusp)
1490   (:policy :fast-safe)
1491   (:args (digit :scs (unsigned-reg)))
1492   (:arg-types unsigned-num)
1493   (:conditional)
1494   (:info target not-p)
1495   (:generator 3
1496     (inst or digit digit)
1497     (inst jmp (if not-p :s :ns) target)))
1498
1499
1500 ;;; For add and sub with carry the sc of carry argument is any-reg so
1501 ;;; that it may be passed as a fixnum or word and thus may be 0, 1, or
1502 ;;; 4. This is easy to deal with and may save a fixnum-word
1503 ;;; conversion.
1504 (define-vop (add-w/carry)
1505   (:translate sb!bignum:%add-with-carry)
1506   (:policy :fast-safe)
1507   (:args (a :scs (unsigned-reg) :target result)
1508          (b :scs (unsigned-reg unsigned-stack) :to :eval)
1509          (c :scs (any-reg) :target temp))
1510   (:arg-types unsigned-num unsigned-num positive-fixnum)
1511   (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1512   (:results (result :scs (unsigned-reg) :from (:argument 0))
1513             (carry :scs (unsigned-reg)))
1514   (:result-types unsigned-num positive-fixnum)
1515   (:generator 4
1516     (move result a)
1517     (move temp c)
1518     (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1519     (inst adc result b)
1520     (inst mov carry 0)
1521     (inst adc carry carry)))
1522
1523 ;;; Note: the borrow is 1 for no borrow and 0 for a borrow, the opposite
1524 ;;; of the x86 convention.
1525 (define-vop (sub-w/borrow)
1526   (:translate sb!bignum:%subtract-with-borrow)
1527   (:policy :fast-safe)
1528   (:args (a :scs (unsigned-reg) :to :eval :target result)
1529          (b :scs (unsigned-reg unsigned-stack) :to :result)
1530          (c :scs (any-reg control-stack)))
1531   (:arg-types unsigned-num unsigned-num positive-fixnum)
1532   (:results (result :scs (unsigned-reg) :from :eval)
1533             (borrow :scs (unsigned-reg)))
1534   (:result-types unsigned-num positive-fixnum)
1535   (:generator 5
1536     (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1537     (move result a)
1538     (inst sbb result b)
1539     (inst mov borrow 1)
1540     (inst sbb borrow 0)))
1541
1542
1543 (define-vop (bignum-mult-and-add-3-arg)
1544   (:translate sb!bignum:%multiply-and-add)
1545   (:policy :fast-safe)
1546   (:args (x :scs (unsigned-reg) :target eax)
1547          (y :scs (unsigned-reg unsigned-stack))
1548          (carry-in :scs (unsigned-reg unsigned-stack)))
1549   (:arg-types unsigned-num unsigned-num unsigned-num)
1550   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1551                    :to (:result 1) :target lo) eax)
1552   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1553                    :to (:result 0) :target hi) edx)
1554   (:results (hi :scs (unsigned-reg))
1555             (lo :scs (unsigned-reg)))
1556   (:result-types unsigned-num unsigned-num)
1557   (:generator 20
1558     (move eax x)
1559     (inst mul eax y)
1560     (inst add eax carry-in)
1561     (inst adc edx 0)
1562     (move hi edx)
1563     (move lo eax)))
1564
1565 (define-vop (bignum-mult-and-add-4-arg)
1566   (:translate sb!bignum:%multiply-and-add)
1567   (:policy :fast-safe)
1568   (:args (x :scs (unsigned-reg) :target eax)
1569          (y :scs (unsigned-reg unsigned-stack))
1570          (prev :scs (unsigned-reg unsigned-stack))
1571          (carry-in :scs (unsigned-reg unsigned-stack)))
1572   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1573   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1574                    :to (:result 1) :target lo) eax)
1575   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1576                    :to (:result 0) :target hi) edx)
1577   (:results (hi :scs (unsigned-reg))
1578             (lo :scs (unsigned-reg)))
1579   (:result-types unsigned-num unsigned-num)
1580   (:generator 20
1581     (move eax x)
1582     (inst mul eax y)
1583     (inst add eax prev)
1584     (inst adc edx 0)
1585     (inst add eax carry-in)
1586     (inst adc edx 0)
1587     (move hi edx)
1588     (move lo eax)))
1589
1590
1591 (define-vop (bignum-mult)
1592   (:translate sb!bignum:%multiply)
1593   (:policy :fast-safe)
1594   (:args (x :scs (unsigned-reg) :target eax)
1595          (y :scs (unsigned-reg unsigned-stack)))
1596   (:arg-types unsigned-num unsigned-num)
1597   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1598                    :to (:result 1) :target lo) eax)
1599   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1600                    :to (:result 0) :target hi) edx)
1601   (:results (hi :scs (unsigned-reg))
1602             (lo :scs (unsigned-reg)))
1603   (:result-types unsigned-num unsigned-num)
1604   (:generator 20
1605     (move eax x)
1606     (inst mul eax y)
1607     (move hi edx)
1608     (move lo eax)))
1609
1610 (define-vop (bignum-lognot lognot-mod32/word=>unsigned)
1611   (:translate sb!bignum:%lognot))
1612
1613 (define-vop (fixnum-to-digit)
1614   (:translate sb!bignum:%fixnum-to-digit)
1615   (:policy :fast-safe)
1616   (:args (fixnum :scs (any-reg control-stack) :target digit))
1617   (:arg-types tagged-num)
1618   (:results (digit :scs (unsigned-reg)
1619                    :load-if (not (and (sc-is fixnum control-stack)
1620                                       (sc-is digit unsigned-stack)
1621                                       (location= fixnum digit)))))
1622   (:result-types unsigned-num)
1623   (:generator 1
1624     (move digit fixnum)
1625     (inst sar digit 2)))
1626
1627 (define-vop (bignum-floor)
1628   (:translate sb!bignum:%floor)
1629   (:policy :fast-safe)
1630   (:args (div-high :scs (unsigned-reg) :target edx)
1631          (div-low :scs (unsigned-reg) :target eax)
1632          (divisor :scs (unsigned-reg unsigned-stack)))
1633   (:arg-types unsigned-num unsigned-num unsigned-num)
1634   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1635                    :to (:result 0) :target quo) eax)
1636   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1637                    :to (:result 1) :target rem) edx)
1638   (:results (quo :scs (unsigned-reg))
1639             (rem :scs (unsigned-reg)))
1640   (:result-types unsigned-num unsigned-num)
1641   (:generator 300
1642     (move edx div-high)
1643     (move eax div-low)
1644     (inst div eax divisor)
1645     (move quo eax)
1646     (move rem edx)))
1647
1648 (define-vop (signify-digit)
1649   (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1650   (:policy :fast-safe)
1651   (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1652   (:arg-types unsigned-num)
1653   (:results (res :scs (any-reg signed-reg)
1654                  :load-if (not (and (sc-is digit unsigned-stack)
1655                                     (sc-is res control-stack signed-stack)
1656                                     (location= digit res)))))
1657   (:result-types signed-num)
1658   (:generator 1
1659     (move res digit)
1660     (when (sc-is res any-reg control-stack)
1661       (inst shl res 2))))
1662
1663 (define-vop (digit-ashr)
1664   (:translate sb!bignum:%ashr)
1665   (:policy :fast-safe)
1666   (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1667          (count :scs (unsigned-reg) :target ecx))
1668   (:arg-types unsigned-num positive-fixnum)
1669   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1670   (:results (result :scs (unsigned-reg) :from (:argument 0)
1671                     :load-if (not (and (sc-is result unsigned-stack)
1672                                        (location= digit result)))))
1673   (:result-types unsigned-num)
1674   (:generator 2
1675     (move result digit)
1676     (move ecx count)
1677     (inst sar result :cl)))
1678
1679 (define-vop (digit-ashr/c)
1680   (:translate sb!bignum:%ashr)
1681   (:policy :fast-safe)
1682   (:args (digit :scs (unsigned-reg unsigned-stack) :target result))
1683   (:arg-types unsigned-num (:constant (integer 0 31)))
1684   (:info count)
1685   (:results (result :scs (unsigned-reg) :from (:argument 0)
1686                     :load-if (not (and (sc-is result unsigned-stack)
1687                                        (location= digit result)))))
1688   (:result-types unsigned-num)
1689   (:generator 1
1690     (move result digit)
1691     (inst sar result count)))
1692
1693 (define-vop (digit-lshr digit-ashr)
1694   (:translate sb!bignum:%digit-logical-shift-right)
1695   (:generator 1
1696     (move result digit)
1697     (move ecx count)
1698     (inst shr result :cl)))
1699
1700 (define-vop (digit-ashl digit-ashr)
1701   (:translate sb!bignum:%ashl)
1702   (:generator 1
1703     (move result digit)
1704     (move ecx count)
1705     (inst shl result :cl)))
1706 \f
1707 ;;;; static functions
1708
1709 (define-static-fun two-arg-/ (x y) :translate /)
1710
1711 (define-static-fun two-arg-gcd (x y) :translate gcd)
1712 (define-static-fun two-arg-lcm (x y) :translate lcm)
1713
1714 (define-static-fun two-arg-and (x y) :translate logand)
1715 (define-static-fun two-arg-ior (x y) :translate logior)
1716 (define-static-fun two-arg-xor (x y) :translate logxor)
1717
1718 \f
1719 ;;; Support for the Mersenne Twister, MT19937, random number generator
1720 ;;; due to Matsumoto and Nishimura.
1721 ;;;
1722 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
1723 ;;; 623-dimensionally equidistributed uniform pseudorandom number
1724 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
1725 ;;; 1997, to appear.
1726 ;;;
1727 ;;; State:
1728 ;;;  0-1:   Constant matrix A. [0, #x9908b0df] (not used here)
1729 ;;;  2:     Index; init. to 1.
1730 ;;;  3-626: State.
1731 (defknown random-mt19937 ((simple-array (unsigned-byte 32) (*)))
1732   (unsigned-byte 32) ())
1733 (define-vop (random-mt19937)
1734   (:policy :fast-safe)
1735   (:translate random-mt19937)
1736   (:args (state :scs (descriptor-reg) :to :result))
1737   (:arg-types simple-array-unsigned-byte-32)
1738   (:temporary (:sc unsigned-reg :from (:eval 0) :to :result) k)
1739   (:temporary (:sc unsigned-reg :offset eax-offset
1740                    :from (:eval 0) :to :result) tmp)
1741   (:results (y :scs (unsigned-reg) :from (:eval 0)))
1742   (:result-types unsigned-num)
1743   (:generator 50
1744     (loadw k state (+ 2 vector-data-offset) other-pointer-lowtag)
1745     (inst cmp k 624)
1746     (inst jmp :ne no-update)
1747     (inst mov tmp state)        ; The state is passed in EAX.
1748     (inst call (make-fixup 'random-mt19937-update :assembly-routine))
1749     ;; Restore k, and set to 0.
1750     (inst xor k k)
1751     NO-UPDATE
1752     ;; y = ptgfsr[k++];
1753     (inst mov y (make-ea-for-vector-data state :index k :offset 3))
1754     ;; y ^= (y >> 11);
1755     (inst shr y 11)
1756     (inst xor y (make-ea-for-vector-data state :index k :offset 3))
1757     ;; y ^= (y << 7) & #x9d2c5680
1758     (inst mov tmp y)
1759     (inst inc k)
1760     (inst shl tmp 7)
1761     (storew k state (+ 2 vector-data-offset) other-pointer-lowtag)
1762     (inst and tmp #x9d2c5680)
1763     (inst xor y tmp)
1764     ;; y ^= (y << 15) & #xefc60000
1765     (inst mov tmp y)
1766     (inst shl tmp 15)
1767     (inst and tmp #xefc60000)
1768     (inst xor y tmp)
1769     ;; y ^= (y >> 18);
1770     (inst mov tmp y)
1771     (inst shr tmp 18)
1772     (inst xor y tmp)))
1773
1774 (in-package "SB!C")
1775
1776 (defun mask-result (class width result)
1777   (ecase class
1778     (:unsigned
1779      `(logand ,result ,(1- (ash 1 width))))
1780     (:signed
1781      `(mask-signed-field ,width ,result))))
1782
1783 ;;; This is essentially a straight implementation of the algorithm in
1784 ;;; "Strength Reduction of Multiplications by Integer Constants",
1785 ;;; Youfeng Wu, ACM SIGPLAN Notices, Vol. 30, No.2, February 1995.
1786 (defun basic-decompose-multiplication (class width arg num n-bits condensed)
1787   (case (aref condensed 0)
1788     (0
1789      (let ((tmp (min 3 (aref condensed 1))))
1790        (decf (aref condensed 1) tmp)
1791        (mask-result class width
1792                     `(%lea ,arg
1793                            ,(decompose-multiplication class width
1794                              arg (ash (1- num) (- tmp)) (1- n-bits) (subseq condensed 1))
1795                            ,(ash 1 tmp) 0))))
1796     ((1 2 3)
1797      (let ((r0 (aref condensed 0)))
1798        (incf (aref condensed 1) r0)
1799        (mask-result class width
1800                     `(%lea ,(decompose-multiplication class width
1801                              arg (- num (ash 1 r0)) (1- n-bits) (subseq condensed 1))
1802                            ,arg
1803                            ,(ash 1 r0) 0))))
1804     (t (let ((r0 (aref condensed 0)))
1805          (setf (aref condensed 0) 0)
1806          (mask-result class width
1807                       `(ash ,(decompose-multiplication class width
1808                               arg (ash num (- r0)) n-bits condensed)
1809                             ,r0))))))
1810
1811 (defun decompose-multiplication (class width arg num n-bits condensed)
1812   (cond
1813     ((= n-bits 0) 0)
1814     ((= num 1) arg)
1815     ((= n-bits 1)
1816      (mask-result class width `(ash ,arg ,(1- (integer-length num)))))
1817     ((let ((max 0) (end 0))
1818        (loop for i from 2 to (length condensed)
1819              for j = (reduce #'+ (subseq condensed 0 i))
1820              when (and (> (- (* 2 i) 3 j) max)
1821                        (< (+ (ash 1 (1+ j))
1822                              (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num)
1823                                   (1+ j)))
1824                           (ash 1 32)))
1825                do (setq max (- (* 2 i) 3 j)
1826                         end i))
1827        (when (> max 0)
1828          (let ((j (reduce #'+ (subseq condensed 0 end))))
1829            (let ((n2 (+ (ash 1 (1+ j))
1830                         (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num) (1+ j))))
1831                  (n1 (1+ (ldb (byte (1+ j) 0) (lognot num)))))
1832            (mask-result class width
1833                         `(- ,(optimize-multiply class width arg n2)
1834                             ,(optimize-multiply  class width arg n1))))))))
1835     ((dolist (i '(9 5 3))
1836        (when (integerp (/ num i))
1837          (when (< (logcount (/ num i)) (logcount num))
1838            (let ((x (gensym)))
1839              (return `(let ((,x ,(optimize-multiply class width arg (/ num i))))
1840                        ,(mask-result class width
1841                                      `(%lea ,x ,x (1- ,i) 0)))))))))
1842     (t (basic-decompose-multiplication class width arg num n-bits condensed))))
1843
1844 (defun optimize-multiply (class width arg x)
1845   (let* ((n-bits (logcount x))
1846          (condensed (make-array n-bits)))
1847     (let ((count 0) (bit 0))
1848       (dotimes (i 32)
1849         (cond ((logbitp i x)
1850                (setf (aref condensed bit) count)
1851                (setf count 1)
1852                (incf bit))
1853               (t (incf count)))))
1854     (decompose-multiplication class width arg x n-bits condensed)))
1855
1856 (defun *-transformer (class width y)
1857   (cond
1858     ((= y (ash 1 (integer-length y)))
1859      ;; there's a generic transform for y = 2^k
1860      (give-up-ir1-transform))
1861     ((member y '(3 5 9))
1862      ;; we can do these multiplications directly using LEA
1863      `(%lea x x ,(1- y) 0))
1864     ((member :pentium4 *backend-subfeatures*)
1865      ;; the pentium4's multiply unit is reportedly very good
1866      (give-up-ir1-transform))
1867     ;; FIXME: should make this more fine-grained.  If nothing else,
1868     ;; there should probably be a cutoff of about 9 instructions on
1869     ;; pentium-class machines.
1870     (t (optimize-multiply class width 'x y))))
1871
1872 (deftransform * ((x y)
1873                  ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1874                  (unsigned-byte 32))
1875   "recode as leas, shifts and adds"
1876   (let ((y (lvar-value y)))
1877     (*-transformer :unsigned 32 y)))
1878 (deftransform sb!vm::*-mod32
1879     ((x y) ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1880      (unsigned-byte 32))
1881   "recode as leas, shifts and adds"
1882   (let ((y (lvar-value y)))
1883     (*-transformer :unsigned 32 y)))
1884
1885 (deftransform * ((x y)
1886                  ((signed-byte 30) (constant-arg (unsigned-byte 32)))
1887                  (signed-byte 30))
1888   "recode as leas, shifts and adds"
1889   (let ((y (lvar-value y)))
1890     (*-transformer :signed 30 y)))
1891 (deftransform sb!vm::*-smod30
1892     ((x y) ((signed-byte 30) (constant-arg (unsigned-byte 32)))
1893      (signed-byte 30))
1894   "recode as leas, shifts and adds"
1895   (let ((y (lvar-value y)))
1896     (*-transformer :signed 30 y)))
1897
1898 ;;; FIXME: we should also be able to write an optimizer or two to
1899 ;;; convert (+ (* x 2) 17), (- (* x 9) 5) to a %LEA.