1.0.16.10: function-ify ERROR-CALL and GENERATE-ERROR-CODE on x86
[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)
1013   (:info target not-p)
1014   (:effects)
1015   (:affected)
1016   (:policy :fast-safe))
1017
1018 (define-vop (fast-conditional/fixnum fast-conditional)
1019   (:args (x :scs (any-reg)
1020             :load-if (not (and (sc-is x control-stack)
1021                                (sc-is y any-reg))))
1022          (y :scs (any-reg control-stack)))
1023   (:arg-types tagged-num tagged-num)
1024   (:note "inline fixnum comparison"))
1025
1026 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
1027   (:args (x :scs (any-reg control-stack)))
1028   (:arg-types tagged-num (:constant (signed-byte 30)))
1029   (:info target not-p y))
1030
1031 (define-vop (fast-conditional/signed fast-conditional)
1032   (:args (x :scs (signed-reg)
1033             :load-if (not (and (sc-is x signed-stack)
1034                                (sc-is y signed-reg))))
1035          (y :scs (signed-reg signed-stack)))
1036   (:arg-types signed-num signed-num)
1037   (:note "inline (signed-byte 32) comparison"))
1038
1039 (define-vop (fast-conditional-c/signed fast-conditional/signed)
1040   (:args (x :scs (signed-reg signed-stack)))
1041   (:arg-types signed-num (:constant (signed-byte 32)))
1042   (:info target not-p y))
1043
1044 (define-vop (fast-conditional/unsigned fast-conditional)
1045   (:args (x :scs (unsigned-reg)
1046             :load-if (not (and (sc-is x unsigned-stack)
1047                                (sc-is y unsigned-reg))))
1048          (y :scs (unsigned-reg unsigned-stack)))
1049   (:arg-types unsigned-num unsigned-num)
1050   (:note "inline (unsigned-byte 32) comparison"))
1051
1052 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
1053   (:args (x :scs (unsigned-reg unsigned-stack)))
1054   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
1055   (:info target not-p y))
1056
1057 (macrolet ((define-logtest-vops ()
1058              `(progn
1059                ,@(loop for suffix in '(/fixnum -c/fixnum
1060                                        /signed -c/signed
1061                                        /unsigned -c/unsigned)
1062                        for cost in '(4 3 6 5 6 5)
1063                        collect
1064                        `(define-vop (,(symbolicate "FAST-LOGTEST" suffix)
1065                                      ,(symbolicate "FAST-CONDITIONAL" suffix))
1066                          (:translate logtest)
1067                          (:generator ,cost
1068                           (emit-optimized-test-inst x
1069                                                     ,(if (eq suffix '-c/fixnum)
1070                                                          '(fixnumize y)
1071                                                          'y))
1072                           (inst jmp (if not-p :e :ne) target)))))))
1073   (define-logtest-vops))
1074
1075 (defknown %logbitp (integer unsigned-byte) boolean
1076   (movable foldable flushable always-translatable))
1077
1078 ;;; only for constant folding within the compiler
1079 (defun %logbitp (integer index)
1080   (logbitp index integer))
1081
1082 ;;; too much work to do the non-constant case (maybe?)
1083 (define-vop (fast-logbitp-c/fixnum fast-conditional-c/fixnum)
1084   (:translate %logbitp)
1085   (:arg-types tagged-num (:constant (integer 0 29)))
1086   (:generator 4
1087     (inst bt x (+ y n-fixnum-tag-bits))
1088     (inst jmp (if not-p :nc :c) target)))
1089
1090 (define-vop (fast-logbitp/signed fast-conditional/signed)
1091   (:args (x :scs (signed-reg signed-stack))
1092          (y :scs (signed-reg)))
1093   (:translate %logbitp)
1094   (:generator 6
1095     (inst bt x y)
1096     (inst jmp (if not-p :nc :c) target)))
1097
1098 (define-vop (fast-logbitp-c/signed fast-conditional-c/signed)
1099   (:translate %logbitp)
1100   (:arg-types signed-num (:constant (integer 0 31)))
1101   (:generator 5
1102     (inst bt x y)
1103     (inst jmp (if not-p :nc :c) target)))
1104
1105 (define-vop (fast-logbitp/unsigned fast-conditional/unsigned)
1106   (:args (x :scs (unsigned-reg unsigned-stack))
1107          (y :scs (unsigned-reg)))
1108   (:translate %logbitp)
1109   (:generator 6
1110     (inst bt x y)
1111     (inst jmp (if not-p :nc :c) target)))
1112
1113 (define-vop (fast-logbitp-c/unsigned fast-conditional-c/unsigned)
1114   (:translate %logbitp)
1115   (:arg-types unsigned-num (:constant (integer 0 31)))
1116   (:generator 5
1117     (inst bt x y)
1118     (inst jmp (if not-p :nc :c) target)))
1119
1120 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
1121              `(progn
1122                 ,@(mapcar
1123                    (lambda (suffix cost signed)
1124                      `(define-vop (;; FIXME: These could be done more
1125                                    ;; cleanly with SYMBOLICATE.
1126                                    ,(intern (format nil "~:@(FAST-IF-~A~A~)"
1127                                                     tran suffix))
1128                                    ,(intern
1129                                      (format nil "~:@(FAST-CONDITIONAL~A~)"
1130                                              suffix)))
1131                         (:translate ,tran)
1132                         (:generator ,cost
1133                                     (inst cmp x
1134                                           ,(if (eq suffix '-c/fixnum)
1135                                                '(fixnumize y)
1136                                                'y))
1137                                     (inst jmp (if not-p
1138                                                   ,(if signed
1139                                                        not-cond
1140                                                        not-unsigned)
1141                                                   ,(if signed
1142                                                        cond
1143                                                        unsigned))
1144                                           target))))
1145                    '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
1146                    '(4 3 6 5 6 5)
1147                    '(t t t t nil nil)))))
1148
1149   (define-conditional-vop < :l :b :ge :ae)
1150   (define-conditional-vop > :g :a :le :be))
1151
1152 (define-vop (fast-if-eql/signed fast-conditional/signed)
1153   (:translate eql)
1154   (:generator 6
1155     (inst cmp x y)
1156     (inst jmp (if not-p :ne :e) target)))
1157
1158 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
1159   (:translate eql)
1160   (:generator 5
1161     (cond ((and (sc-is x signed-reg) (zerop y))
1162            (inst test x x))  ; smaller instruction
1163           (t
1164            (inst cmp x y)))
1165     (inst jmp (if not-p :ne :e) target)))
1166
1167 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1168   (:translate eql)
1169   (:generator 6
1170     (inst cmp x y)
1171     (inst jmp (if not-p :ne :e) target)))
1172
1173 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
1174   (:translate eql)
1175   (:generator 5
1176     (cond ((and (sc-is x unsigned-reg) (zerop y))
1177            (inst test x x))  ; smaller instruction
1178           (t
1179            (inst cmp x y)))
1180     (inst jmp (if not-p :ne :e) target)))
1181
1182 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1183 ;;; known fixnum.
1184
1185 ;;; These versions specify a fixnum restriction on their first arg. We have
1186 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1187 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1188 ;;; fixnum specific operations from being used on word integers, spuriously
1189 ;;; consing the argument.
1190
1191 (define-vop (fast-eql/fixnum fast-conditional)
1192   (:args (x :scs (any-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 tagged-num)
1197   (:note "inline fixnum comparison")
1198   (:translate eql)
1199   (:generator 4
1200     (inst cmp x y)
1201     (inst jmp (if not-p :ne :e) target)))
1202 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1203   (:args (x :scs (any-reg descriptor-reg)
1204             :load-if (not (and (sc-is x control-stack)
1205                                (sc-is y any-reg))))
1206          (y :scs (any-reg control-stack)))
1207   (:arg-types * tagged-num)
1208   (:variant-cost 7))
1209
1210 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1211   (:args (x :scs (any-reg control-stack)))
1212   (:arg-types tagged-num (:constant (signed-byte 30)))
1213   (:info target not-p y)
1214   (:translate eql)
1215   (:generator 2
1216     (cond ((and (sc-is x any-reg) (zerop y))
1217            (inst test x x))  ; smaller instruction
1218           (t
1219            (inst cmp x (fixnumize y))))
1220     (inst jmp (if not-p :ne :e) target)))
1221 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1222   (:args (x :scs (any-reg descriptor-reg control-stack)))
1223   (:arg-types * (:constant (signed-byte 30)))
1224   (:variant-cost 6))
1225 \f
1226 ;;;; 32-bit logical operations
1227
1228 (define-vop (merge-bits)
1229   (:translate merge-bits)
1230   (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
1231          (prev :scs (unsigned-reg) :target result)
1232          (next :scs (unsigned-reg)))
1233   (:arg-types tagged-num unsigned-num unsigned-num)
1234   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
1235   (:results (result :scs (unsigned-reg) :from (:argument 1)))
1236   (:result-types unsigned-num)
1237   (:policy :fast-safe)
1238   (:generator 4
1239     (move ecx shift)
1240     (move result prev)
1241     (inst shrd result next :cl)))
1242
1243 ;;; Only the lower 5 bits of the shift amount are significant.
1244 (define-vop (shift-towards-someplace)
1245   (:policy :fast-safe)
1246   (:args (num :scs (unsigned-reg) :target r)
1247          (amount :scs (signed-reg) :target ecx))
1248   (:arg-types unsigned-num tagged-num)
1249   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1250   (:results (r :scs (unsigned-reg) :from (:argument 0)))
1251   (:result-types unsigned-num))
1252
1253 (define-vop (shift-towards-start shift-towards-someplace)
1254   (:translate shift-towards-start)
1255   (:note "SHIFT-TOWARDS-START")
1256   (:generator 1
1257     (move r num)
1258     (move ecx amount)
1259     (inst shr r :cl)))
1260
1261 (define-vop (shift-towards-end shift-towards-someplace)
1262   (:translate shift-towards-end)
1263   (:note "SHIFT-TOWARDS-END")
1264   (:generator 1
1265     (move r num)
1266     (move ecx amount)
1267     (inst shl r :cl)))
1268 \f
1269 ;;;; Modular functions
1270 (defmacro define-mod-binop ((name prototype) function)
1271   `(define-vop (,name ,prototype)
1272        (:args (x :target r :scs (unsigned-reg signed-reg)
1273                  :load-if (not (and (or (sc-is x unsigned-stack)
1274                                         (sc-is x signed-stack))
1275                                     (or (sc-is y unsigned-reg)
1276                                         (sc-is y signed-reg))
1277                                     (or (sc-is r unsigned-stack)
1278                                         (sc-is r signed-stack))
1279                                     (location= x r))))
1280               (y :scs (unsigned-reg signed-reg unsigned-stack signed-stack)))
1281      (:arg-types untagged-num untagged-num)
1282      (:results (r :scs (unsigned-reg signed-reg) :from (:argument 0)
1283                   :load-if (not (and (or (sc-is x unsigned-stack)
1284                                          (sc-is x signed-stack))
1285                                      (or (sc-is y unsigned-reg)
1286                                          (sc-is y unsigned-reg))
1287                                      (or (sc-is r unsigned-stack)
1288                                          (sc-is r unsigned-stack))
1289                                      (location= x r)))))
1290      (:result-types unsigned-num)
1291      (:translate ,function)))
1292 (defmacro define-mod-binop-c ((name prototype) function)
1293   `(define-vop (,name ,prototype)
1294        (:args (x :target r :scs (unsigned-reg signed-reg)
1295                  :load-if (not (and (or (sc-is x unsigned-stack)
1296                                         (sc-is x signed-stack))
1297                                     (or (sc-is r unsigned-stack)
1298                                         (sc-is r signed-stack))
1299                                     (location= x r)))))
1300      (:info y)
1301      (:arg-types untagged-num (:constant (or (unsigned-byte 32) (signed-byte 32))))
1302      (:results (r :scs (unsigned-reg signed-reg) :from (:argument 0)
1303                   :load-if (not (and (or (sc-is x unsigned-stack)
1304                                          (sc-is x signed-stack))
1305                                      (or (sc-is r unsigned-stack)
1306                                          (sc-is r unsigned-stack))
1307                                      (location= x r)))))
1308      (:result-types unsigned-num)
1309      (:translate ,function)))
1310
1311 (macrolet ((def (name -c-p)
1312              (let ((fun32 (intern (format nil "~S-MOD32" name)))
1313                    (vopu (intern (format nil "FAST-~S/UNSIGNED=>UNSIGNED" name)))
1314                    (vopcu (intern (format nil "FAST-~S-C/UNSIGNED=>UNSIGNED" name)))
1315                    (vopf (intern (format nil "FAST-~S/FIXNUM=>FIXNUM" name)))
1316                    (vopcf (intern (format nil "FAST-~S-C/FIXNUM=>FIXNUM" name)))
1317                    (vop32u (intern (format nil "FAST-~S-MOD32/WORD=>UNSIGNED" name)))
1318                    (vop32f (intern (format nil "FAST-~S-MOD32/FIXNUM=>FIXNUM" name)))
1319                    (vop32cu (intern (format nil "FAST-~S-MOD32-C/WORD=>UNSIGNED" name)))
1320                    (vop32cf (intern (format nil "FAST-~S-MOD32-C/FIXNUM=>FIXNUM" name)))
1321                    (sfun30 (intern (format nil "~S-SMOD30" name)))
1322                    (svop30f (intern (format nil "FAST-~S-SMOD30/FIXNUM=>FIXNUM" name)))
1323                    (svop30cf (intern (format nil "FAST-~S-SMOD30-C/FIXNUM=>FIXNUM" name))))
1324                `(progn
1325                   (define-modular-fun ,fun32 (x y) ,name :untagged nil 32)
1326                   (define-modular-fun ,sfun30 (x y) ,name :tagged t 30)
1327                   (define-mod-binop (,vop32u ,vopu) ,fun32)
1328                   (define-vop (,vop32f ,vopf) (:translate ,fun32))
1329                   (define-vop (,svop30f ,vopf) (:translate ,sfun30))
1330                   ,@(when -c-p
1331                       `((define-mod-binop-c (,vop32cu ,vopcu) ,fun32)
1332                         (define-vop (,svop30cf ,vopcf) (:translate ,sfun30))))))))
1333   (def + t)
1334   (def - t)
1335   ;; (no -C variant as x86 MUL instruction doesn't take an immediate)
1336   (def * nil))
1337
1338
1339 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
1340              fast-ash-c/unsigned=>unsigned)
1341   (:translate ash-left-mod32))
1342
1343 (define-vop (fast-ash-left-mod32/unsigned=>unsigned
1344              fast-ash-left/unsigned=>unsigned))
1345 (deftransform ash-left-mod32 ((integer count)
1346                               ((unsigned-byte 32) (unsigned-byte 5)))
1347   (when (sb!c::constant-lvar-p count)
1348     (sb!c::give-up-ir1-transform))
1349   '(%primitive fast-ash-left-mod32/unsigned=>unsigned integer count))
1350
1351 (define-vop (fast-ash-left-smod30-c/fixnum=>fixnum
1352              fast-ash-c/fixnum=>fixnum)
1353   (:translate ash-left-smod30))
1354
1355 (define-vop (fast-ash-left-smod30/fixnum=>fixnum
1356              fast-ash-left/fixnum=>fixnum))
1357 (deftransform ash-left-smod30 ((integer count)
1358                                ((signed-byte 30) (unsigned-byte 5)))
1359   (when (sb!c::constant-lvar-p count)
1360     (sb!c::give-up-ir1-transform))
1361   '(%primitive fast-ash-left-smod30/fixnum=>fixnum integer count))
1362
1363 (in-package "SB!C")
1364
1365 (defknown sb!vm::%lea-mod32 (integer integer (member 1 2 4 8) (signed-byte 32))
1366   (unsigned-byte 32)
1367   (foldable flushable movable))
1368 (defknown sb!vm::%lea-smod30 (integer integer (member 1 2 4 8) (signed-byte 32))
1369   (signed-byte 30)
1370   (foldable flushable movable))
1371
1372 (define-modular-fun-optimizer %lea ((base index scale disp) :untagged nil :width width)
1373   (when (and (<= width 32)
1374              (constant-lvar-p scale)
1375              (constant-lvar-p disp))
1376     (cut-to-width base :untagged width nil)
1377     (cut-to-width index :untagged width nil)
1378     'sb!vm::%lea-mod32))
1379 (define-modular-fun-optimizer %lea ((base index scale disp) :tagged t :width width)
1380   (when (and (<= width 30)
1381              (constant-lvar-p scale)
1382              (constant-lvar-p disp))
1383     (cut-to-width base :tagged width t)
1384     (cut-to-width index :tagged width t)
1385     'sb!vm::%lea-smod30))
1386
1387 #+sb-xc-host
1388 (progn
1389   (defun sb!vm::%lea-mod32 (base index scale disp)
1390     (ldb (byte 32 0) (%lea base index scale disp)))
1391   (defun sb!vm::%lea-smod30 (base index scale disp)
1392     (mask-signed-field 30 (%lea base index scale disp))))
1393 #-sb-xc-host
1394 (progn
1395   (defun sb!vm::%lea-mod32 (base index scale disp)
1396     (let ((base (logand base #xffffffff))
1397           (index (logand index #xffffffff)))
1398       ;; can't use modular version of %LEA, as we only have VOPs for
1399       ;; constant SCALE and DISP.
1400       (ldb (byte 32 0) (+ base (* index scale) disp))))
1401   (defun sb!vm::%lea-smod30 (base index scale disp)
1402     (let ((base (mask-signed-field 30 base))
1403           (index (mask-signed-field 30 index)))
1404       ;; can't use modular version of %LEA, as we only have VOPs for
1405       ;; constant SCALE and DISP.
1406       (mask-signed-field 30 (+ base (* index scale) disp)))))
1407
1408 (in-package "SB!VM")
1409
1410 (define-vop (%lea-mod32/unsigned=>unsigned
1411              %lea/unsigned=>unsigned)
1412   (:translate %lea-mod32))
1413 (define-vop (%lea-smod30/fixnum=>fixnum
1414              %lea/fixnum=>fixnum)
1415   (:translate %lea-smod30))
1416
1417 ;;; logical operations
1418 (define-modular-fun lognot-mod32 (x) lognot :untagged nil 32)
1419 (define-vop (lognot-mod32/word=>unsigned)
1420   (:translate lognot-mod32)
1421   (:args (x :scs (unsigned-reg signed-reg unsigned-stack signed-stack) :target r
1422             :load-if (not (and (or (sc-is x unsigned-stack)
1423                                    (sc-is x signed-stack))
1424                                (or (sc-is r unsigned-stack)
1425                                    (sc-is r signed-stack))
1426                                (location= x r)))))
1427   (:arg-types unsigned-num)
1428   (:results (r :scs (unsigned-reg)
1429                :load-if (not (and (or (sc-is x unsigned-stack)
1430                                       (sc-is x signed-stack))
1431                                   (or (sc-is r unsigned-stack)
1432                                       (sc-is r signed-stack))
1433                                   (sc-is r unsigned-stack)
1434                                   (location= x r)))))
1435   (:result-types unsigned-num)
1436   (:policy :fast-safe)
1437   (:generator 1
1438     (move r x)
1439     (inst not r)))
1440
1441 (define-source-transform logeqv (&rest args)
1442   (if (oddp (length args))
1443       `(logxor ,@args)
1444       `(lognot (logxor ,@args))))
1445 (define-source-transform logandc1 (x y)
1446   `(logand (lognot ,x) ,y))
1447 (define-source-transform logandc2 (x y)
1448   `(logand ,x (lognot ,y)))
1449 (define-source-transform logorc1 (x y)
1450   `(logior (lognot ,x) ,y))
1451 (define-source-transform logorc2 (x y)
1452   `(logior ,x (lognot ,y)))
1453 (define-source-transform lognor (x y)
1454   `(lognot (logior ,x ,y)))
1455 (define-source-transform lognand (x y)
1456   `(lognot (logand ,x ,y)))
1457 \f
1458 ;;;; bignum stuff
1459
1460 (define-vop (bignum-length get-header-data)
1461   (:translate sb!bignum:%bignum-length)
1462   (:policy :fast-safe))
1463
1464 (define-vop (bignum-set-length set-header-data)
1465   (:translate sb!bignum:%bignum-set-length)
1466   (:policy :fast-safe))
1467
1468 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1469   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1470 (define-full-reffer+offset bignum-ref-with-offset *
1471   bignum-digits-offset other-pointer-lowtag
1472   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref-with-offset)
1473 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1474   (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1475
1476 (define-vop (digit-0-or-plus)
1477   (:translate sb!bignum:%digit-0-or-plusp)
1478   (:policy :fast-safe)
1479   (:args (digit :scs (unsigned-reg)))
1480   (:arg-types unsigned-num)
1481   (:conditional)
1482   (:info target not-p)
1483   (:generator 3
1484     (inst or digit digit)
1485     (inst jmp (if not-p :s :ns) target)))
1486
1487
1488 ;;; For add and sub with carry the sc of carry argument is any-reg so
1489 ;;; that it may be passed as a fixnum or word and thus may be 0, 1, or
1490 ;;; 4. This is easy to deal with and may save a fixnum-word
1491 ;;; conversion.
1492 (define-vop (add-w/carry)
1493   (:translate sb!bignum:%add-with-carry)
1494   (:policy :fast-safe)
1495   (:args (a :scs (unsigned-reg) :target result)
1496          (b :scs (unsigned-reg unsigned-stack) :to :eval)
1497          (c :scs (any-reg) :target temp))
1498   (:arg-types unsigned-num unsigned-num positive-fixnum)
1499   (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1500   (:results (result :scs (unsigned-reg) :from (:argument 0))
1501             (carry :scs (unsigned-reg)))
1502   (:result-types unsigned-num positive-fixnum)
1503   (:generator 4
1504     (move result a)
1505     (move temp c)
1506     (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1507     (inst adc result b)
1508     (inst mov carry 0)
1509     (inst adc carry carry)))
1510
1511 ;;; Note: the borrow is 1 for no borrow and 0 for a borrow, the opposite
1512 ;;; of the x86 convention.
1513 (define-vop (sub-w/borrow)
1514   (:translate sb!bignum:%subtract-with-borrow)
1515   (:policy :fast-safe)
1516   (:args (a :scs (unsigned-reg) :to :eval :target result)
1517          (b :scs (unsigned-reg unsigned-stack) :to :result)
1518          (c :scs (any-reg control-stack)))
1519   (:arg-types unsigned-num unsigned-num positive-fixnum)
1520   (:results (result :scs (unsigned-reg) :from :eval)
1521             (borrow :scs (unsigned-reg)))
1522   (:result-types unsigned-num positive-fixnum)
1523   (:generator 5
1524     (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1525     (move result a)
1526     (inst sbb result b)
1527     (inst mov borrow 1)
1528     (inst sbb borrow 0)))
1529
1530
1531 (define-vop (bignum-mult-and-add-3-arg)
1532   (:translate sb!bignum:%multiply-and-add)
1533   (:policy :fast-safe)
1534   (:args (x :scs (unsigned-reg) :target eax)
1535          (y :scs (unsigned-reg unsigned-stack))
1536          (carry-in :scs (unsigned-reg unsigned-stack)))
1537   (:arg-types unsigned-num unsigned-num unsigned-num)
1538   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1539                    :to (:result 1) :target lo) eax)
1540   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1541                    :to (:result 0) :target hi) edx)
1542   (:results (hi :scs (unsigned-reg))
1543             (lo :scs (unsigned-reg)))
1544   (:result-types unsigned-num unsigned-num)
1545   (:generator 20
1546     (move eax x)
1547     (inst mul eax y)
1548     (inst add eax carry-in)
1549     (inst adc edx 0)
1550     (move hi edx)
1551     (move lo eax)))
1552
1553 (define-vop (bignum-mult-and-add-4-arg)
1554   (:translate sb!bignum:%multiply-and-add)
1555   (:policy :fast-safe)
1556   (:args (x :scs (unsigned-reg) :target eax)
1557          (y :scs (unsigned-reg unsigned-stack))
1558          (prev :scs (unsigned-reg unsigned-stack))
1559          (carry-in :scs (unsigned-reg unsigned-stack)))
1560   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1561   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1562                    :to (:result 1) :target lo) eax)
1563   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1564                    :to (:result 0) :target hi) edx)
1565   (:results (hi :scs (unsigned-reg))
1566             (lo :scs (unsigned-reg)))
1567   (:result-types unsigned-num unsigned-num)
1568   (:generator 20
1569     (move eax x)
1570     (inst mul eax y)
1571     (inst add eax prev)
1572     (inst adc edx 0)
1573     (inst add eax carry-in)
1574     (inst adc edx 0)
1575     (move hi edx)
1576     (move lo eax)))
1577
1578
1579 (define-vop (bignum-mult)
1580   (:translate sb!bignum:%multiply)
1581   (:policy :fast-safe)
1582   (:args (x :scs (unsigned-reg) :target eax)
1583          (y :scs (unsigned-reg unsigned-stack)))
1584   (:arg-types unsigned-num unsigned-num)
1585   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1586                    :to (:result 1) :target lo) eax)
1587   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1588                    :to (:result 0) :target hi) edx)
1589   (:results (hi :scs (unsigned-reg))
1590             (lo :scs (unsigned-reg)))
1591   (:result-types unsigned-num unsigned-num)
1592   (:generator 20
1593     (move eax x)
1594     (inst mul eax y)
1595     (move hi edx)
1596     (move lo eax)))
1597
1598 (define-vop (bignum-lognot lognot-mod32/word=>unsigned)
1599   (:translate sb!bignum:%lognot))
1600
1601 (define-vop (fixnum-to-digit)
1602   (:translate sb!bignum:%fixnum-to-digit)
1603   (:policy :fast-safe)
1604   (:args (fixnum :scs (any-reg control-stack) :target digit))
1605   (:arg-types tagged-num)
1606   (:results (digit :scs (unsigned-reg)
1607                    :load-if (not (and (sc-is fixnum control-stack)
1608                                       (sc-is digit unsigned-stack)
1609                                       (location= fixnum digit)))))
1610   (:result-types unsigned-num)
1611   (:generator 1
1612     (move digit fixnum)
1613     (inst sar digit 2)))
1614
1615 (define-vop (bignum-floor)
1616   (:translate sb!bignum:%floor)
1617   (:policy :fast-safe)
1618   (:args (div-high :scs (unsigned-reg) :target edx)
1619          (div-low :scs (unsigned-reg) :target eax)
1620          (divisor :scs (unsigned-reg unsigned-stack)))
1621   (:arg-types unsigned-num unsigned-num unsigned-num)
1622   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1623                    :to (:result 0) :target quo) eax)
1624   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1625                    :to (:result 1) :target rem) edx)
1626   (:results (quo :scs (unsigned-reg))
1627             (rem :scs (unsigned-reg)))
1628   (:result-types unsigned-num unsigned-num)
1629   (:generator 300
1630     (move edx div-high)
1631     (move eax div-low)
1632     (inst div eax divisor)
1633     (move quo eax)
1634     (move rem edx)))
1635
1636 (define-vop (signify-digit)
1637   (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1638   (:policy :fast-safe)
1639   (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1640   (:arg-types unsigned-num)
1641   (:results (res :scs (any-reg signed-reg)
1642                  :load-if (not (and (sc-is digit unsigned-stack)
1643                                     (sc-is res control-stack signed-stack)
1644                                     (location= digit res)))))
1645   (:result-types signed-num)
1646   (:generator 1
1647     (move res digit)
1648     (when (sc-is res any-reg control-stack)
1649       (inst shl res 2))))
1650
1651 (define-vop (digit-ashr)
1652   (:translate sb!bignum:%ashr)
1653   (:policy :fast-safe)
1654   (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1655          (count :scs (unsigned-reg) :target ecx))
1656   (:arg-types unsigned-num positive-fixnum)
1657   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1658   (:results (result :scs (unsigned-reg) :from (:argument 0)
1659                     :load-if (not (and (sc-is result unsigned-stack)
1660                                        (location= digit result)))))
1661   (:result-types unsigned-num)
1662   (:generator 2
1663     (move result digit)
1664     (move ecx count)
1665     (inst sar result :cl)))
1666
1667 (define-vop (digit-ashr/c)
1668   (:translate sb!bignum:%ashr)
1669   (:policy :fast-safe)
1670   (:args (digit :scs (unsigned-reg unsigned-stack) :target result))
1671   (:arg-types unsigned-num (:constant (integer 0 31)))
1672   (:info count)
1673   (:results (result :scs (unsigned-reg) :from (:argument 0)
1674                     :load-if (not (and (sc-is result unsigned-stack)
1675                                        (location= digit result)))))
1676   (:result-types unsigned-num)
1677   (:generator 1
1678     (move result digit)
1679     (inst sar result count)))
1680
1681 (define-vop (digit-lshr digit-ashr)
1682   (:translate sb!bignum:%digit-logical-shift-right)
1683   (:generator 1
1684     (move result digit)
1685     (move ecx count)
1686     (inst shr result :cl)))
1687
1688 (define-vop (digit-ashl digit-ashr)
1689   (:translate sb!bignum:%ashl)
1690   (:generator 1
1691     (move result digit)
1692     (move ecx count)
1693     (inst shl result :cl)))
1694 \f
1695 ;;;; static functions
1696
1697 (define-static-fun two-arg-/ (x y) :translate /)
1698
1699 (define-static-fun two-arg-gcd (x y) :translate gcd)
1700 (define-static-fun two-arg-lcm (x y) :translate lcm)
1701
1702 (define-static-fun two-arg-and (x y) :translate logand)
1703 (define-static-fun two-arg-ior (x y) :translate logior)
1704 (define-static-fun two-arg-xor (x y) :translate logxor)
1705
1706 \f
1707 ;;; Support for the Mersenne Twister, MT19937, random number generator
1708 ;;; due to Matsumoto and Nishimura.
1709 ;;;
1710 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
1711 ;;; 623-dimensionally equidistributed uniform pseudorandom number
1712 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
1713 ;;; 1997, to appear.
1714 ;;;
1715 ;;; State:
1716 ;;;  0-1:   Constant matrix A. [0, #x9908b0df] (not used here)
1717 ;;;  2:     Index; init. to 1.
1718 ;;;  3-626: State.
1719 (defknown random-mt19937 ((simple-array (unsigned-byte 32) (*)))
1720   (unsigned-byte 32) ())
1721 (define-vop (random-mt19937)
1722   (:policy :fast-safe)
1723   (:translate random-mt19937)
1724   (:args (state :scs (descriptor-reg) :to :result))
1725   (:arg-types simple-array-unsigned-byte-32)
1726   (:temporary (:sc unsigned-reg :from (:eval 0) :to :result) k)
1727   (:temporary (:sc unsigned-reg :offset eax-offset
1728                    :from (:eval 0) :to :result) tmp)
1729   (:results (y :scs (unsigned-reg) :from (:eval 0)))
1730   (:result-types unsigned-num)
1731   (:generator 50
1732     (loadw k state (+ 2 vector-data-offset) other-pointer-lowtag)
1733     (inst cmp k 624)
1734     (inst jmp :ne no-update)
1735     (inst mov tmp state)        ; The state is passed in EAX.
1736     (inst call (make-fixup 'random-mt19937-update :assembly-routine))
1737     ;; Restore k, and set to 0.
1738     (inst xor k k)
1739     NO-UPDATE
1740     ;; y = ptgfsr[k++];
1741     (inst mov y (make-ea-for-vector-data state :index k :offset 3))
1742     ;; y ^= (y >> 11);
1743     (inst shr y 11)
1744     (inst xor y (make-ea-for-vector-data state :index k :offset 3))
1745     ;; y ^= (y << 7) & #x9d2c5680
1746     (inst mov tmp y)
1747     (inst inc k)
1748     (inst shl tmp 7)
1749     (storew k state (+ 2 vector-data-offset) other-pointer-lowtag)
1750     (inst and tmp #x9d2c5680)
1751     (inst xor y tmp)
1752     ;; y ^= (y << 15) & #xefc60000
1753     (inst mov tmp y)
1754     (inst shl tmp 15)
1755     (inst and tmp #xefc60000)
1756     (inst xor y tmp)
1757     ;; y ^= (y >> 18);
1758     (inst mov tmp y)
1759     (inst shr tmp 18)
1760     (inst xor y tmp)))
1761
1762 (in-package "SB!C")
1763
1764 (defun mask-result (class width result)
1765   (ecase class
1766     (:unsigned
1767      `(logand ,result ,(1- (ash 1 width))))
1768     (:signed
1769      `(mask-signed-field ,width ,result))))
1770
1771 ;;; This is essentially a straight implementation of the algorithm in
1772 ;;; "Strength Reduction of Multiplications by Integer Constants",
1773 ;;; Youfeng Wu, ACM SIGPLAN Notices, Vol. 30, No.2, February 1995.
1774 (defun basic-decompose-multiplication (class width arg num n-bits condensed)
1775   (case (aref condensed 0)
1776     (0
1777      (let ((tmp (min 3 (aref condensed 1))))
1778        (decf (aref condensed 1) tmp)
1779        (mask-result class width
1780                     `(%lea ,arg
1781                            ,(decompose-multiplication class width
1782                              arg (ash (1- num) (- tmp)) (1- n-bits) (subseq condensed 1))
1783                            ,(ash 1 tmp) 0))))
1784     ((1 2 3)
1785      (let ((r0 (aref condensed 0)))
1786        (incf (aref condensed 1) r0)
1787        (mask-result class width
1788                     `(%lea ,(decompose-multiplication class width
1789                              arg (- num (ash 1 r0)) (1- n-bits) (subseq condensed 1))
1790                            ,arg
1791                            ,(ash 1 r0) 0))))
1792     (t (let ((r0 (aref condensed 0)))
1793          (setf (aref condensed 0) 0)
1794          (mask-result class width
1795                       `(ash ,(decompose-multiplication class width
1796                               arg (ash num (- r0)) n-bits condensed)
1797                             ,r0))))))
1798
1799 (defun decompose-multiplication (class width arg num n-bits condensed)
1800   (cond
1801     ((= n-bits 0) 0)
1802     ((= num 1) arg)
1803     ((= n-bits 1)
1804      (mask-result class width `(ash ,arg ,(1- (integer-length num)))))
1805     ((let ((max 0) (end 0))
1806        (loop for i from 2 to (length condensed)
1807              for j = (reduce #'+ (subseq condensed 0 i))
1808              when (and (> (- (* 2 i) 3 j) max)
1809                        (< (+ (ash 1 (1+ j))
1810                              (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num)
1811                                   (1+ j)))
1812                           (ash 1 32)))
1813                do (setq max (- (* 2 i) 3 j)
1814                         end i))
1815        (when (> max 0)
1816          (let ((j (reduce #'+ (subseq condensed 0 end))))
1817            (let ((n2 (+ (ash 1 (1+ j))
1818                         (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num) (1+ j))))
1819                  (n1 (1+ (ldb (byte (1+ j) 0) (lognot num)))))
1820            (mask-result class width
1821                         `(- ,(optimize-multiply class width arg n2)
1822                             ,(optimize-multiply  class width arg n1))))))))
1823     ((dolist (i '(9 5 3))
1824        (when (integerp (/ num i))
1825          (when (< (logcount (/ num i)) (logcount num))
1826            (let ((x (gensym)))
1827              (return `(let ((,x ,(optimize-multiply class width arg (/ num i))))
1828                        ,(mask-result class width
1829                                      `(%lea ,x ,x (1- ,i) 0)))))))))
1830     (t (basic-decompose-multiplication class width arg num n-bits condensed))))
1831
1832 (defun optimize-multiply (class width arg x)
1833   (let* ((n-bits (logcount x))
1834          (condensed (make-array n-bits)))
1835     (let ((count 0) (bit 0))
1836       (dotimes (i 32)
1837         (cond ((logbitp i x)
1838                (setf (aref condensed bit) count)
1839                (setf count 1)
1840                (incf bit))
1841               (t (incf count)))))
1842     (decompose-multiplication class width arg x n-bits condensed)))
1843
1844 (defun *-transformer (class width y)
1845   (cond
1846     ((= y (ash 1 (integer-length y)))
1847      ;; there's a generic transform for y = 2^k
1848      (give-up-ir1-transform))
1849     ((member y '(3 5 9))
1850      ;; we can do these multiplications directly using LEA
1851      `(%lea x x ,(1- y) 0))
1852     ((member :pentium4 *backend-subfeatures*)
1853      ;; the pentium4's multiply unit is reportedly very good
1854      (give-up-ir1-transform))
1855     ;; FIXME: should make this more fine-grained.  If nothing else,
1856     ;; there should probably be a cutoff of about 9 instructions on
1857     ;; pentium-class machines.
1858     (t (optimize-multiply class width 'x y))))
1859
1860 (deftransform * ((x y)
1861                  ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1862                  (unsigned-byte 32))
1863   "recode as leas, shifts and adds"
1864   (let ((y (lvar-value y)))
1865     (*-transformer :unsigned 32 y)))
1866 (deftransform sb!vm::*-mod32
1867     ((x y) ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1868      (unsigned-byte 32))
1869   "recode as leas, shifts and adds"
1870   (let ((y (lvar-value y)))
1871     (*-transformer :unsigned 32 y)))
1872
1873 (deftransform * ((x y)
1874                  ((signed-byte 30) (constant-arg (unsigned-byte 32)))
1875                  (signed-byte 30))
1876   "recode as leas, shifts and adds"
1877   (let ((y (lvar-value y)))
1878     (*-transformer :signed 30 y)))
1879 (deftransform sb!vm::*-smod30
1880     ((x y) ((signed-byte 30) (constant-arg (unsigned-byte 32)))
1881      (signed-byte 30))
1882   "recode as leas, shifts and adds"
1883   (let ((y (lvar-value y)))
1884     (*-transformer :signed 30 y)))
1885
1886 ;;; FIXME: we should also be able to write an optimizer or two to
1887 ;;; convert (+ (* x 2) 17), (- (* x 9) 5) to a %LEA.