Change x86oid modular arithmetic to work if fixnum width changes.
[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 n-fixnum-tag-bits)
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 n-fixnum-tag-bits)
449         (inst lea quo (make-ea :dword :index eax
450                                :scale (ash 1 n-fixnum-tag-bits))))
451     (move rem edx)))
452
453 (define-vop (fast-truncate-c/fixnum=>fixnum fast-safe-arith-op)
454   (:translate truncate)
455   (:args (x :scs (any-reg) :target eax))
456   (:info y)
457   (:arg-types tagged-num (:constant (signed-byte 30)))
458   (:temporary (:sc signed-reg :offset eax-offset :target quo
459                    :from :argument :to (:result 0)) eax)
460   (:temporary (:sc any-reg :offset edx-offset :target rem
461                    :from :eval :to (:result 1)) edx)
462   (:temporary (:sc any-reg :from :eval :to :result) y-arg)
463   (:results (quo :scs (any-reg))
464             (rem :scs (any-reg)))
465   (:result-types tagged-num tagged-num)
466   (:note "inline fixnum arithmetic")
467   (:vop-var vop)
468   (:save-p :compute-only)
469   (:generator 30
470     (move eax x)
471     (inst cdq)
472     (inst mov y-arg (fixnumize y))
473     (inst idiv eax y-arg)
474     (if (location= quo eax)
475         (inst shl eax n-fixnum-tag-bits)
476         (inst lea quo (make-ea :dword :index eax
477                                :scale (ash 1 n-fixnum-tag-bits))))
478     (move rem edx)))
479
480 (define-vop (fast-truncate/unsigned=>unsigned fast-safe-arith-op)
481   (:translate truncate)
482   (:args (x :scs (unsigned-reg) :target eax)
483          (y :scs (unsigned-reg signed-stack)))
484   (:arg-types unsigned-num unsigned-num)
485   (:temporary (:sc unsigned-reg :offset eax-offset :target quo
486                    :from (:argument 0) :to (:result 0)) eax)
487   (:temporary (:sc unsigned-reg :offset edx-offset :target rem
488                    :from (:argument 0) :to (:result 1)) edx)
489   (:results (quo :scs (unsigned-reg))
490             (rem :scs (unsigned-reg)))
491   (:result-types unsigned-num unsigned-num)
492   (:note "inline (unsigned-byte 32) arithmetic")
493   (:vop-var vop)
494   (:save-p :compute-only)
495   (:generator 33
496     (let ((zero (generate-error-code vop 'division-by-zero-error x y)))
497       (if (sc-is y unsigned-reg)
498           (inst test y y)  ; smaller instruction
499           (inst cmp y 0))
500       (inst jmp :eq zero))
501     (move eax x)
502     (inst xor edx edx)
503     (inst div eax y)
504     (move quo eax)
505     (move rem edx)))
506
507 (define-vop (fast-truncate-c/unsigned=>unsigned fast-safe-arith-op)
508   (:translate truncate)
509   (:args (x :scs (unsigned-reg) :target eax))
510   (:info y)
511   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
512   (:temporary (:sc unsigned-reg :offset eax-offset :target quo
513                    :from :argument :to (:result 0)) eax)
514   (:temporary (:sc unsigned-reg :offset edx-offset :target rem
515                    :from :eval :to (:result 1)) edx)
516   (:temporary (:sc unsigned-reg :from :eval :to :result) y-arg)
517   (:results (quo :scs (unsigned-reg))
518             (rem :scs (unsigned-reg)))
519   (:result-types unsigned-num unsigned-num)
520   (:note "inline (unsigned-byte 32) arithmetic")
521   (:vop-var vop)
522   (:save-p :compute-only)
523   (:generator 32
524     (move eax x)
525     (inst xor edx edx)
526     (inst mov y-arg y)
527     (inst div eax y-arg)
528     (move quo eax)
529     (move rem edx)))
530
531 (define-vop (fast-truncate/signed=>signed fast-safe-arith-op)
532   (:translate truncate)
533   (:args (x :scs (signed-reg) :target eax)
534          (y :scs (signed-reg signed-stack)))
535   (:arg-types signed-num signed-num)
536   (:temporary (:sc signed-reg :offset eax-offset :target quo
537                    :from (:argument 0) :to (:result 0)) eax)
538   (:temporary (:sc signed-reg :offset edx-offset :target rem
539                    :from (:argument 0) :to (:result 1)) edx)
540   (:results (quo :scs (signed-reg))
541             (rem :scs (signed-reg)))
542   (:result-types signed-num signed-num)
543   (:note "inline (signed-byte 32) arithmetic")
544   (:vop-var vop)
545   (:save-p :compute-only)
546   (:generator 33
547     (let ((zero (generate-error-code vop 'division-by-zero-error x y)))
548       (if (sc-is y signed-reg)
549           (inst test y y)  ; smaller instruction
550           (inst cmp y 0))
551       (inst jmp :eq zero))
552     (move eax x)
553     (inst cdq)
554     (inst idiv eax y)
555     (move quo eax)
556     (move rem edx)))
557
558 (define-vop (fast-truncate-c/signed=>signed fast-safe-arith-op)
559   (:translate truncate)
560   (:args (x :scs (signed-reg) :target eax))
561   (:info y)
562   (:arg-types signed-num (:constant (signed-byte 32)))
563   (:temporary (:sc signed-reg :offset eax-offset :target quo
564                    :from :argument :to (:result 0)) eax)
565   (:temporary (:sc signed-reg :offset edx-offset :target rem
566                    :from :eval :to (:result 1)) edx)
567   (:temporary (:sc signed-reg :from :eval :to :result) y-arg)
568   (:results (quo :scs (signed-reg))
569             (rem :scs (signed-reg)))
570   (:result-types signed-num signed-num)
571   (:note "inline (signed-byte 32) arithmetic")
572   (:vop-var vop)
573   (:save-p :compute-only)
574   (:generator 32
575     (move eax x)
576     (inst cdq)
577     (inst mov y-arg y)
578     (inst idiv eax y-arg)
579     (move quo eax)
580     (move rem edx)))
581
582
583 \f
584 ;;;; Shifting
585 (define-vop (fast-ash-c/fixnum=>fixnum)
586   (:translate ash)
587   (:policy :fast-safe)
588   (:args (number :scs (any-reg) :target result
589                  :load-if (not (and (sc-is number any-reg control-stack)
590                                     (sc-is result any-reg control-stack)
591                                     (location= number result)))))
592   (:info amount)
593   (:arg-types tagged-num (:constant integer))
594   (:results (result :scs (any-reg)
595                     :load-if (not (and (sc-is number control-stack)
596                                        (sc-is result control-stack)
597                                        (location= number result)))))
598   (:result-types tagged-num)
599   (:note "inline ASH")
600   (:variant nil)
601   (:variant-vars modularp)
602   (:generator 2
603     (cond ((and (= amount 1) (not (location= number result)))
604            (inst lea result (make-ea :dword :base number :index number)))
605           ((and (= amount 2) (not (location= number result)))
606            (inst lea result (make-ea :dword :index number :scale 4)))
607           ((and (= amount 3) (not (location= number result)))
608            (inst lea result (make-ea :dword :index number :scale 8)))
609           (t
610            (move result number)
611            (cond ((< -32 amount 32)
612                   ;; this code is used both in ASH and ASH-MODFX, so
613                   ;; be careful
614                   (if (plusp amount)
615                       (inst shl result amount)
616                       (progn
617                         (inst sar result (- amount))
618                         (inst and result (lognot fixnum-tag-mask)))))
619                  ((plusp amount)
620                   (unless modularp
621                     (aver (not "Impossible: fixnum ASH should not be called with
622 constant shift greater than word length")))
623                   (if (sc-is result any-reg)
624                       (inst xor result result)
625                       (inst mov result 0)))
626                  (t (inst sar result 31)
627                     (inst and result (lognot fixnum-tag-mask))))))))
628
629 (define-vop (fast-ash-left/fixnum=>fixnum)
630   (:translate ash)
631   (:args (number :scs (any-reg) :target result
632                  :load-if (not (and (sc-is number control-stack)
633                                     (sc-is result control-stack)
634                                     (location= number result))))
635          (amount :scs (unsigned-reg) :target ecx))
636   (:arg-types tagged-num positive-fixnum)
637   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
638   (:results (result :scs (any-reg) :from (:argument 0)
639                     :load-if (not (and (sc-is number control-stack)
640                                        (sc-is result control-stack)
641                                        (location= number result)))))
642   (:result-types tagged-num)
643   (:policy :fast-safe)
644   (:note "inline ASH")
645   (:generator 3
646     (move result number)
647     (move ecx amount)
648     ;; The result-type ensures us that this shift will not overflow.
649     (inst shl result :cl)))
650
651 (define-vop (fast-ash-c/signed=>signed)
652   (:translate ash)
653   (:policy :fast-safe)
654   (:args (number :scs (signed-reg) :target result
655                  :load-if (not (and (sc-is number signed-stack)
656                                     (sc-is result signed-stack)
657                                     (location= number result)))))
658   (:info amount)
659   (:arg-types signed-num (:constant integer))
660   (:results (result :scs (signed-reg)
661                     :load-if (not (and (sc-is number signed-stack)
662                                        (sc-is result signed-stack)
663                                        (location= number result)))))
664   (:result-types signed-num)
665   (:note "inline ASH")
666   (:generator 3
667     (cond ((and (= amount 1) (not (location= number result)))
668            (inst lea result (make-ea :dword :base number :index number)))
669           ((and (= amount 2) (not (location= number result)))
670            (inst lea result (make-ea :dword :index number :scale 4)))
671           ((and (= amount 3) (not (location= number result)))
672            (inst lea result (make-ea :dword :index number :scale 8)))
673           (t
674            (move result number)
675            (cond ((plusp amount) (inst shl result amount))
676                  (t (inst sar result (min 31 (- amount)))))))))
677
678 (define-vop (fast-ash-c/unsigned=>unsigned)
679   (:translate ash)
680   (:policy :fast-safe)
681   (:args (number :scs (unsigned-reg) :target result
682                  :load-if (not (and (sc-is number unsigned-stack)
683                                     (sc-is result unsigned-stack)
684                                     (location= number result)))))
685   (:info amount)
686   (:arg-types unsigned-num (:constant integer))
687   (:results (result :scs (unsigned-reg)
688                     :load-if (not (and (sc-is number unsigned-stack)
689                                        (sc-is result unsigned-stack)
690                                        (location= number result)))))
691   (:result-types unsigned-num)
692   (:note "inline ASH")
693   (:generator 3
694     (cond ((and (= amount 1) (not (location= number result)))
695            (inst lea result (make-ea :dword :base number :index number)))
696           ((and (= amount 2) (not (location= number result)))
697            (inst lea result (make-ea :dword :index number :scale 4)))
698           ((and (= amount 3) (not (location= number result)))
699            (inst lea result (make-ea :dword :index number :scale 8)))
700           (t
701            (move result number)
702            (cond ((< -32 amount 32)
703                   ;; this code is used both in ASH and ASH-MOD32, so
704                   ;; be careful
705                   (if (plusp amount)
706                       (inst shl result amount)
707                       (inst shr result (- amount))))
708                  (t (if (sc-is result unsigned-reg)
709                         (inst xor result result)
710                         (inst mov result 0))))))))
711
712 (define-vop (fast-ash-left/signed=>signed)
713   (:translate ash)
714   (:args (number :scs (signed-reg) :target result
715                  :load-if (not (and (sc-is number signed-stack)
716                                     (sc-is result signed-stack)
717                                     (location= number result))))
718          (amount :scs (unsigned-reg) :target ecx))
719   (:arg-types signed-num positive-fixnum)
720   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
721   (:results (result :scs (signed-reg) :from (:argument 0)
722                     :load-if (not (and (sc-is number signed-stack)
723                                        (sc-is result signed-stack)
724                                        (location= number result)))))
725   (:result-types signed-num)
726   (:policy :fast-safe)
727   (:note "inline ASH")
728   (:generator 4
729     (move result number)
730     (move ecx amount)
731     (inst shl result :cl)))
732
733 (define-vop (fast-ash-left/unsigned=>unsigned)
734   (:translate ash)
735   (:args (number :scs (unsigned-reg) :target result
736                  :load-if (not (and (sc-is number unsigned-stack)
737                                     (sc-is result unsigned-stack)
738                                     (location= number result))))
739          (amount :scs (unsigned-reg) :target ecx))
740   (:arg-types unsigned-num positive-fixnum)
741   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
742   (:results (result :scs (unsigned-reg) :from (:argument 0)
743                     :load-if (not (and (sc-is number unsigned-stack)
744                                        (sc-is result unsigned-stack)
745                                        (location= number result)))))
746   (:result-types unsigned-num)
747   (:policy :fast-safe)
748   (:note "inline ASH")
749   (:generator 4
750     (move result number)
751     (move ecx amount)
752     (inst shl result :cl)))
753
754 (define-vop (fast-ash/signed=>signed)
755   (:translate ash)
756   (:policy :fast-safe)
757   (:args (number :scs (signed-reg) :target result)
758          (amount :scs (signed-reg) :target ecx))
759   (:arg-types signed-num signed-num)
760   (:results (result :scs (signed-reg) :from (:argument 0)))
761   (:result-types signed-num)
762   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
763   (:note "inline ASH")
764   (:generator 5
765     (move result number)
766     (move ecx amount)
767     (inst or ecx ecx)
768     (inst jmp :ns positive)
769     (inst neg ecx)
770     (inst cmp ecx 31)
771     (inst jmp :be okay)
772     (inst mov ecx 31)
773     OKAY
774     (inst sar result :cl)
775     (inst jmp done)
776
777     POSITIVE
778     ;; The result-type ensures us that this shift will not overflow.
779     (inst shl result :cl)
780
781     DONE))
782
783 (define-vop (fast-ash/unsigned=>unsigned)
784   (:translate ash)
785   (:policy :fast-safe)
786   (:args (number :scs (unsigned-reg) :target result)
787          (amount :scs (signed-reg) :target ecx))
788   (:arg-types unsigned-num signed-num)
789   (:results (result :scs (unsigned-reg) :from (:argument 0)))
790   (:result-types unsigned-num)
791   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
792   (:note "inline ASH")
793   (:generator 5
794     (move result number)
795     (move ecx amount)
796     (inst or ecx ecx)
797     (inst jmp :ns positive)
798     (inst neg ecx)
799     (inst cmp ecx 31)
800     (inst jmp :be okay)
801     (inst xor result result)
802     (inst jmp done)
803     OKAY
804     (inst shr result :cl)
805     (inst jmp done)
806
807     POSITIVE
808     ;; The result-type ensures us that this shift will not overflow.
809     (inst shl result :cl)
810
811     DONE))
812
813 (in-package "SB!C")
814
815 (defknown %lea (integer integer (member 1 2 4 8) (signed-byte 32))
816   integer
817   (foldable flushable movable))
818
819 (defoptimizer (%lea derive-type) ((base index scale disp))
820   (when (and (constant-lvar-p scale)
821              (constant-lvar-p disp))
822     (let ((scale (lvar-value scale))
823           (disp (lvar-value disp))
824           (base-type (lvar-type base))
825           (index-type (lvar-type index)))
826       (when (and (numeric-type-p base-type)
827                  (numeric-type-p index-type))
828         (let ((base-lo (numeric-type-low base-type))
829               (base-hi (numeric-type-high base-type))
830               (index-lo (numeric-type-low index-type))
831               (index-hi (numeric-type-high index-type)))
832           (make-numeric-type :class 'integer
833                              :complexp :real
834                              :low (when (and base-lo index-lo)
835                                     (+ base-lo (* index-lo scale) disp))
836                              :high (when (and base-hi index-hi)
837                                      (+ base-hi (* index-hi scale) disp))))))))
838
839 (defun %lea (base index scale disp)
840   (+ base (* index scale) disp))
841
842 (in-package "SB!VM")
843
844 (define-vop (%lea/unsigned=>unsigned)
845   (:translate %lea)
846   (:policy :fast-safe)
847   (:args (base :scs (unsigned-reg))
848          (index :scs (unsigned-reg)))
849   (:info scale disp)
850   (:arg-types unsigned-num unsigned-num
851               (:constant (member 1 2 4 8))
852               (:constant (signed-byte 32)))
853   (:results (r :scs (unsigned-reg)))
854   (:result-types unsigned-num)
855   (:generator 5
856     (inst lea r (make-ea :dword :base base :index index
857                          :scale scale :disp disp))))
858
859 (define-vop (%lea/signed=>signed)
860   (:translate %lea)
861   (:policy :fast-safe)
862   (:args (base :scs (signed-reg))
863          (index :scs (signed-reg)))
864   (:info scale disp)
865   (:arg-types signed-num signed-num
866               (:constant (member 1 2 4 8))
867               (:constant (signed-byte 32)))
868   (:results (r :scs (signed-reg)))
869   (:result-types signed-num)
870   (:generator 4
871     (inst lea r (make-ea :dword :base base :index index
872                          :scale scale :disp disp))))
873
874 (define-vop (%lea/fixnum=>fixnum)
875   (:translate %lea)
876   (:policy :fast-safe)
877   (:args (base :scs (any-reg))
878          (index :scs (any-reg)))
879   (:info scale disp)
880   (:arg-types tagged-num tagged-num
881               (:constant (member 1 2 4 8))
882               (:constant (signed-byte 32)))
883   (:results (r :scs (any-reg)))
884   (:result-types tagged-num)
885   (:generator 3
886     (inst lea r (make-ea :dword :base base :index index
887                          :scale scale :disp disp))))
888
889 ;;; FIXME: before making knowledge of this too public, it needs to be
890 ;;; fixed so that it's actually _faster_ than the non-CMOV version; at
891 ;;; least on my Celeron-XXX laptop, this version is marginally slower
892 ;;; than the above version with branches.  -- CSR, 2003-09-04
893 (define-vop (fast-cmov-ash/unsigned=>unsigned)
894   (:translate ash)
895   (:policy :fast-safe)
896   (:args (number :scs (unsigned-reg) :target result)
897          (amount :scs (signed-reg) :target ecx))
898   (:arg-types unsigned-num signed-num)
899   (:results (result :scs (unsigned-reg) :from (:argument 0)))
900   (:result-types unsigned-num)
901   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
902   (:temporary (:sc any-reg :from (:eval 0) :to (:eval 1)) zero)
903   (:note "inline ASH")
904   (:guard (member :cmov *backend-subfeatures*))
905   (:generator 4
906     (move result number)
907     (move ecx amount)
908     (inst or ecx ecx)
909     (inst jmp :ns positive)
910     (inst neg ecx)
911     (inst xor zero zero)
912     (inst shr result :cl)
913     (inst cmp ecx 31)
914     (inst cmov :nbe result zero)
915     (inst jmp done)
916
917     POSITIVE
918     ;; The result-type ensures us that this shift will not overflow.
919     (inst shl result :cl)
920
921     DONE))
922 \f
923 (define-vop (signed-byte-32-len)
924   (:translate integer-length)
925   (:note "inline (signed-byte 32) integer-length")
926   (:policy :fast-safe)
927   (:args (arg :scs (signed-reg) :target res))
928   (:arg-types signed-num)
929   (:results (res :scs (unsigned-reg)))
930   (:result-types unsigned-num)
931   (:generator 28
932     (move res arg)
933     (if (sc-is res unsigned-reg)
934         (inst test res res)
935         (inst cmp res 0))
936     (inst jmp :ge POS)
937     (inst not res)
938     POS
939     (inst bsr res res)
940     (inst jmp :z zero)
941     (inst inc res)
942     (inst jmp done)
943     ZERO
944     (inst xor res res)
945     DONE))
946
947 (define-vop (unsigned-byte-32-len)
948   (:translate integer-length)
949   (:note "inline (unsigned-byte 32) integer-length")
950   (:policy :fast-safe)
951   (:args (arg :scs (unsigned-reg)))
952   (:arg-types unsigned-num)
953   (:results (res :scs (unsigned-reg)))
954   (:result-types unsigned-num)
955   (:generator 26
956     (inst bsr res arg)
957     (inst jmp :z zero)
958     (inst inc res)
959     (inst jmp done)
960     ZERO
961     (inst xor res res)
962     DONE))
963
964 (define-vop (unsigned-byte-32-count)
965   (:translate logcount)
966   (:note "inline (unsigned-byte 32) logcount")
967   (:policy :fast-safe)
968   (:args (arg :scs (unsigned-reg) :target result))
969   (:arg-types unsigned-num)
970   (:results (result :scs (unsigned-reg)))
971   (:result-types positive-fixnum)
972   (:temporary (:sc unsigned-reg) temp)
973   (:generator 14
974     ;; See the comments below for how the algorithm works. The tricks
975     ;; used can be found for example in AMD's software optimization
976     ;; guide or at "http://www.hackersdelight.org/HDcode/pop.cc" in the
977     ;; function "pop1".
978     ;; Calculate 2-bit sums. Note that the value of a two-digit binary
979     ;; number is the sum of the right digit and twice the left digit.
980     ;; Thus we can calculate the sum of the two digits by shifting the
981     ;; left digit to the right position and doing a two-bit subtraction.
982     ;; This subtraction will never create a borrow and thus can be made
983     ;; on all 16 2-digit numbers at once.
984     (move result arg)
985     (move temp arg)
986     (inst shr result 1)
987     (inst and result #x55555555)
988     (inst sub temp result)
989     ;; Calculate 4-bit sums by straightforward shift, mask and add.
990     ;; Note that we shift the source operand of the MOV and not its
991     ;; destination so that the SHR and the MOV can execute in the same
992     ;; clock cycle.
993     (inst mov result temp)
994     (inst shr temp 2)
995     (inst and result #x33333333)
996     (inst and temp #x33333333)
997     (inst add result temp)
998     ;; Calculate 8-bit sums. Since each sum is at most 8, which fits
999     ;; into 4 bits, we can apply the mask after the addition, saving one
1000     ;; instruction.
1001     (inst mov temp result)
1002     (inst shr result 4)
1003     (inst add result temp)
1004     (inst and result #x0f0f0f0f)
1005     ;; Calculate the two 16-bit sums and the 32-bit sum. No masking is
1006     ;; necessary inbetween since the final sum is at most 32 which fits
1007     ;; into 6 bits.
1008     (inst mov temp result)
1009     (inst shr result 8)
1010     (inst add result temp)
1011     (inst mov temp result)
1012     (inst shr result 16)
1013     (inst add result temp)
1014     (inst and result #xff)))
1015 \f
1016 ;;;; binary conditional VOPs
1017
1018 (define-vop (fast-conditional)
1019   (:conditional :e)
1020   (:effects)
1021   (:affected)
1022   (:policy :fast-safe))
1023
1024 (define-vop (fast-conditional/fixnum fast-conditional)
1025   (:args (x :scs (any-reg)
1026             :load-if (not (and (sc-is x control-stack)
1027                                (sc-is y any-reg))))
1028          (y :scs (any-reg control-stack)))
1029   (:arg-types tagged-num tagged-num)
1030   (:note "inline fixnum comparison"))
1031
1032 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
1033   (:args (x :scs (any-reg control-stack)))
1034   (:arg-types tagged-num (:constant (signed-byte 30)))
1035   (:info y))
1036
1037 (define-vop (fast-conditional/signed fast-conditional)
1038   (:args (x :scs (signed-reg)
1039             :load-if (not (and (sc-is x signed-stack)
1040                                (sc-is y signed-reg))))
1041          (y :scs (signed-reg signed-stack)))
1042   (:arg-types signed-num signed-num)
1043   (:note "inline (signed-byte 32) comparison"))
1044
1045 (define-vop (fast-conditional-c/signed fast-conditional/signed)
1046   (:args (x :scs (signed-reg signed-stack)))
1047   (:arg-types signed-num (:constant (signed-byte 32)))
1048   (:info y))
1049
1050 (define-vop (fast-conditional/unsigned fast-conditional)
1051   (:args (x :scs (unsigned-reg)
1052             :load-if (not (and (sc-is x unsigned-stack)
1053                                (sc-is y unsigned-reg))))
1054          (y :scs (unsigned-reg unsigned-stack)))
1055   (:arg-types unsigned-num unsigned-num)
1056   (:note "inline (unsigned-byte 32) comparison"))
1057
1058 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
1059   (:args (x :scs (unsigned-reg unsigned-stack)))
1060   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
1061   (:info y))
1062
1063 (macrolet ((define-logtest-vops ()
1064              `(progn
1065                ,@(loop for suffix in '(/fixnum -c/fixnum
1066                                        /signed -c/signed
1067                                        /unsigned -c/unsigned)
1068                        for cost in '(4 3 6 5 6 5)
1069                        collect
1070                        `(define-vop (,(symbolicate "FAST-LOGTEST" suffix)
1071                                      ,(symbolicate "FAST-CONDITIONAL" suffix))
1072                          (:translate logtest)
1073                          (:conditional :ne)
1074                          (:generator ,cost
1075                           (emit-optimized-test-inst x
1076                                                     ,(if (eq suffix '-c/fixnum)
1077                                                          '(fixnumize y)
1078                                                          'y))))))))
1079   (define-logtest-vops))
1080
1081 (defknown %logbitp (integer unsigned-byte) boolean
1082   (movable foldable flushable always-translatable))
1083
1084 ;;; only for constant folding within the compiler
1085 (defun %logbitp (integer index)
1086   (logbitp index integer))
1087
1088 ;;; too much work to do the non-constant case (maybe?)
1089 (define-vop (fast-logbitp-c/fixnum fast-conditional-c/fixnum)
1090   (:translate %logbitp)
1091   (:conditional :c)
1092   (:arg-types tagged-num (:constant (integer 0 29)))
1093   (:generator 4
1094     (inst bt x (+ y n-fixnum-tag-bits))))
1095
1096 (define-vop (fast-logbitp/signed fast-conditional/signed)
1097   (:args (x :scs (signed-reg signed-stack))
1098          (y :scs (signed-reg)))
1099   (:translate %logbitp)
1100   (:conditional :c)
1101   (:generator 6
1102     (inst bt x y)))
1103
1104 (define-vop (fast-logbitp-c/signed fast-conditional-c/signed)
1105   (:translate %logbitp)
1106   (:conditional :c)
1107   (:arg-types signed-num (:constant (integer 0 31)))
1108   (:generator 5
1109     (inst bt x y)))
1110
1111 (define-vop (fast-logbitp/unsigned fast-conditional/unsigned)
1112   (:args (x :scs (unsigned-reg unsigned-stack))
1113          (y :scs (unsigned-reg)))
1114   (:translate %logbitp)
1115   (:conditional :c)
1116   (:generator 6
1117     (inst bt x y)))
1118
1119 (define-vop (fast-logbitp-c/unsigned fast-conditional-c/unsigned)
1120   (:translate %logbitp)
1121   (:conditional :c)
1122   (:arg-types unsigned-num (:constant (integer 0 31)))
1123   (:generator 5
1124     (inst bt x y)))
1125
1126 (macrolet ((define-conditional-vop (tran cond unsigned)
1127              `(progn
1128                 ,@(mapcar
1129                    (lambda (suffix cost signed)
1130                      `(define-vop (;; FIXME: These could be done more
1131                                    ;; cleanly with SYMBOLICATE.
1132                                    ,(intern (format nil "~:@(FAST-IF-~A~A~)"
1133                                                     tran suffix))
1134                                    ,(intern
1135                                      (format nil "~:@(FAST-CONDITIONAL~A~)"
1136                                              suffix)))
1137                         (:translate ,tran)
1138                         (:conditional ,(if signed
1139                                            cond
1140                                            unsigned))
1141                         (:generator ,cost
1142                                     (inst cmp x
1143                                           ,(if (eq suffix '-c/fixnum)
1144                                                '(fixnumize y)
1145                                                'y)))))
1146                    '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
1147                    '(4 3 6 5 6 5)
1148                    '(t t t t nil nil)))))
1149
1150   (define-conditional-vop < :l :b)
1151   (define-conditional-vop > :g :a))
1152
1153 (define-vop (fast-if-eql/signed fast-conditional/signed)
1154   (:translate eql)
1155   (:generator 6
1156     (inst cmp x y)))
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
1166 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1167   (:translate eql)
1168   (:generator 6
1169     (inst cmp x y)))
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
1179 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1180 ;;; known fixnum.
1181
1182 ;;; These versions specify a fixnum restriction on their first arg. We have
1183 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1184 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1185 ;;; fixnum specific operations from being used on word integers, spuriously
1186 ;;; consing the argument.
1187
1188 (define-vop (fast-eql/fixnum fast-conditional)
1189   (:args (x :scs (any-reg)
1190             :load-if (not (and (sc-is x control-stack)
1191                                (sc-is y any-reg))))
1192          (y :scs (any-reg control-stack)))
1193   (:arg-types tagged-num tagged-num)
1194   (:note "inline fixnum comparison")
1195   (:translate eql)
1196   (:generator 4
1197     (inst cmp x y)))
1198 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1199   (:args (x :scs (any-reg descriptor-reg)
1200             :load-if (not (and (sc-is x control-stack)
1201                                (sc-is y any-reg))))
1202          (y :scs (any-reg control-stack)))
1203   (:arg-types * tagged-num)
1204   (:variant-cost 7))
1205
1206 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1207   (:args (x :scs (any-reg control-stack)))
1208   (:arg-types tagged-num (:constant (signed-byte 30)))
1209   (:info y)
1210   (:translate eql)
1211   (:generator 2
1212     (cond ((and (sc-is x any-reg) (zerop y))
1213            (inst test x x))  ; smaller instruction
1214           (t
1215            (inst cmp x (fixnumize y))))))
1216 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1217   (:args (x :scs (any-reg descriptor-reg control-stack)))
1218   (:arg-types * (:constant (signed-byte 30)))
1219   (:variant-cost 6))
1220 \f
1221 ;;;; 32-bit logical operations
1222
1223 ;;; Only the lower 5 bits of the shift amount are significant.
1224 (define-vop (shift-towards-someplace)
1225   (:policy :fast-safe)
1226   (:args (num :scs (unsigned-reg) :target r)
1227          (amount :scs (signed-reg) :target ecx))
1228   (:arg-types unsigned-num tagged-num)
1229   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1230   (:results (r :scs (unsigned-reg) :from (:argument 0)))
1231   (:result-types unsigned-num))
1232
1233 (define-vop (shift-towards-start shift-towards-someplace)
1234   (:translate shift-towards-start)
1235   (:note "SHIFT-TOWARDS-START")
1236   (:generator 1
1237     (move r num)
1238     (move ecx amount)
1239     (inst shr r :cl)))
1240
1241 (define-vop (shift-towards-end shift-towards-someplace)
1242   (:translate shift-towards-end)
1243   (:note "SHIFT-TOWARDS-END")
1244   (:generator 1
1245     (move r num)
1246     (move ecx amount)
1247     (inst shl r :cl)))
1248 \f
1249 ;;;; Modular functions
1250 (defmacro define-mod-binop ((name prototype) function)
1251   `(define-vop (,name ,prototype)
1252        (:args (x :target r :scs (unsigned-reg signed-reg)
1253                  :load-if (not (and (or (sc-is x unsigned-stack)
1254                                         (sc-is x signed-stack))
1255                                     (or (sc-is y unsigned-reg)
1256                                         (sc-is y signed-reg))
1257                                     (or (sc-is r unsigned-stack)
1258                                         (sc-is r signed-stack))
1259                                     (location= x r))))
1260               (y :scs (unsigned-reg signed-reg unsigned-stack signed-stack)))
1261      (:arg-types untagged-num untagged-num)
1262      (:results (r :scs (unsigned-reg signed-reg) :from (:argument 0)
1263                   :load-if (not (and (or (sc-is x unsigned-stack)
1264                                          (sc-is x signed-stack))
1265                                      (or (sc-is y unsigned-reg)
1266                                          (sc-is y unsigned-reg))
1267                                      (or (sc-is r unsigned-stack)
1268                                          (sc-is r unsigned-stack))
1269                                      (location= x r)))))
1270      (:result-types unsigned-num)
1271      (:translate ,function)))
1272 (defmacro define-mod-binop-c ((name prototype) function)
1273   `(define-vop (,name ,prototype)
1274        (:args (x :target r :scs (unsigned-reg signed-reg)
1275                  :load-if (not (and (or (sc-is x unsigned-stack)
1276                                         (sc-is x signed-stack))
1277                                     (or (sc-is r unsigned-stack)
1278                                         (sc-is r signed-stack))
1279                                     (location= x r)))))
1280      (:info y)
1281      (:arg-types untagged-num (:constant (or (unsigned-byte 32) (signed-byte 32))))
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 r unsigned-stack)
1286                                          (sc-is r unsigned-stack))
1287                                      (location= x r)))))
1288      (:result-types unsigned-num)
1289      (:translate ,function)))
1290
1291 (macrolet ((def (name -c-p)
1292              (let ((fun32 (intern (format nil "~S-MOD32" name)))
1293                    (vopu (intern (format nil "FAST-~S/UNSIGNED=>UNSIGNED" name)))
1294                    (vopcu (intern (format nil "FAST-~S-C/UNSIGNED=>UNSIGNED" name)))
1295                    (vopf (intern (format nil "FAST-~S/FIXNUM=>FIXNUM" name)))
1296                    (vopcf (intern (format nil "FAST-~S-C/FIXNUM=>FIXNUM" name)))
1297                    (vop32u (intern (format nil "FAST-~S-MOD32/WORD=>UNSIGNED" name)))
1298                    (vop32f (intern (format nil "FAST-~S-MOD32/FIXNUM=>FIXNUM" name)))
1299                    (vop32cu (intern (format nil "FAST-~S-MOD32-C/WORD=>UNSIGNED" name)))
1300                    (vop32cf (intern (format nil "FAST-~S-MOD32-C/FIXNUM=>FIXNUM" name)))
1301                    (funfx (intern (format nil "~S-MODFX" name)))
1302                    (vopfxf (intern (format nil "FAST-~S-MODFX/FIXNUM=>FIXNUM" name)))
1303                    (vopfxcf (intern (format nil "FAST-~S-MODFX-C/FIXNUM=>FIXNUM" name))))
1304                `(progn
1305                   (define-modular-fun ,fun32 (x y) ,name :untagged nil 32)
1306                   (define-modular-fun ,funfx (x y) ,name :tagged t
1307                                       #.(- n-word-bits n-fixnum-tag-bits))
1308                   (define-mod-binop (,vop32u ,vopu) ,fun32)
1309                   (define-vop (,vop32f ,vopf) (:translate ,fun32))
1310                   (define-vop (,vopfxf ,vopf) (:translate ,funfx))
1311                   ,@(when -c-p
1312                       `((define-mod-binop-c (,vop32cu ,vopcu) ,fun32)
1313                         (define-vop (,vopfxcf ,vopcf) (:translate ,funfx))))))))
1314   (def + t)
1315   (def - t)
1316   ;; (no -C variant as x86 MUL instruction doesn't take an immediate)
1317   (def * nil))
1318
1319
1320 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
1321              fast-ash-c/unsigned=>unsigned)
1322   (:translate ash-left-mod32))
1323
1324 (define-vop (fast-ash-left-mod32/unsigned=>unsigned
1325              fast-ash-left/unsigned=>unsigned))
1326 (deftransform ash-left-mod32 ((integer count)
1327                               ((unsigned-byte 32) (unsigned-byte 5)))
1328   (when (sb!c::constant-lvar-p count)
1329     (sb!c::give-up-ir1-transform))
1330   '(%primitive fast-ash-left-mod32/unsigned=>unsigned integer count))
1331
1332 (define-vop (fast-ash-left-modfx-c/fixnum=>fixnum
1333              fast-ash-c/fixnum=>fixnum)
1334   (:variant :modular)
1335   (:translate ash-left-modfx))
1336
1337 (define-vop (fast-ash-left-modfx/fixnum=>fixnum
1338              fast-ash-left/fixnum=>fixnum))
1339 (deftransform ash-left-modfx ((integer count)
1340                               (fixnum (unsigned-byte 5)))
1341   (when (sb!c::constant-lvar-p count)
1342     (sb!c::give-up-ir1-transform))
1343   '(%primitive fast-ash-left-modfx/fixnum=>fixnum integer count))
1344
1345 (in-package "SB!C")
1346
1347 (defknown sb!vm::%lea-mod32 (integer integer (member 1 2 4 8) (signed-byte 32))
1348   (unsigned-byte 32)
1349   (foldable flushable movable))
1350 (defknown sb!vm::%lea-modfx (integer integer (member 1 2 4 8) (signed-byte 32))
1351   fixnum
1352   (foldable flushable movable))
1353
1354 (define-modular-fun-optimizer %lea ((base index scale disp) :untagged nil :width width)
1355   (when (and (<= width 32)
1356              (constant-lvar-p scale)
1357              (constant-lvar-p disp))
1358     (cut-to-width base :untagged width nil)
1359     (cut-to-width index :untagged width nil)
1360     'sb!vm::%lea-mod32))
1361 (define-modular-fun-optimizer %lea ((base index scale disp) :tagged t :width width)
1362   (when (and (<= width (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits))
1363              (constant-lvar-p scale)
1364              (constant-lvar-p disp))
1365     (cut-to-width base :tagged width t)
1366     (cut-to-width index :tagged width t)
1367     'sb!vm::%lea-modfx))
1368
1369 #+sb-xc-host
1370 (progn
1371   (defun sb!vm::%lea-mod32 (base index scale disp)
1372     (ldb (byte 32 0) (%lea base index scale disp)))
1373   (defun sb!vm::%lea-modfx (base index scale disp)
1374     (mask-signed-field (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits)
1375                        (%lea base index scale disp))))
1376 #-sb-xc-host
1377 (progn
1378   (defun sb!vm::%lea-mod32 (base index scale disp)
1379     (let ((base (logand base #xffffffff))
1380           (index (logand index #xffffffff)))
1381       ;; can't use modular version of %LEA, as we only have VOPs for
1382       ;; constant SCALE and DISP.
1383       (ldb (byte 32 0) (+ base (* index scale) disp))))
1384   (defun sb!vm::%lea-modfx (base index scale disp)
1385     (let* ((fixnum-width (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits))
1386            (base (mask-signed-field fixnum-width base))
1387            (index (mask-signed-field fixnum-width index)))
1388       ;; can't use modular version of %LEA, as we only have VOPs for
1389       ;; constant SCALE and DISP.
1390       (mask-signed-field fixnum-width (+ base (* index scale) disp)))))
1391
1392 (in-package "SB!VM")
1393
1394 (define-vop (%lea-mod32/unsigned=>unsigned
1395              %lea/unsigned=>unsigned)
1396   (:translate %lea-mod32))
1397 (define-vop (%lea-modfx/fixnum=>fixnum
1398              %lea/fixnum=>fixnum)
1399   (:translate %lea-modfx))
1400
1401 ;;; logical operations
1402 (define-modular-fun lognot-mod32 (x) lognot :untagged nil 32)
1403 (define-vop (lognot-mod32/word=>unsigned)
1404   (:translate lognot-mod32)
1405   (:args (x :scs (unsigned-reg signed-reg unsigned-stack signed-stack) :target r
1406             :load-if (not (and (or (sc-is x unsigned-stack)
1407                                    (sc-is x signed-stack))
1408                                (or (sc-is r unsigned-stack)
1409                                    (sc-is r signed-stack))
1410                                (location= x r)))))
1411   (:arg-types unsigned-num)
1412   (:results (r :scs (unsigned-reg)
1413                :load-if (not (and (or (sc-is x unsigned-stack)
1414                                       (sc-is x signed-stack))
1415                                   (or (sc-is r unsigned-stack)
1416                                       (sc-is r signed-stack))
1417                                   (sc-is r unsigned-stack)
1418                                   (location= x r)))))
1419   (:result-types unsigned-num)
1420   (:policy :fast-safe)
1421   (:generator 1
1422     (move r x)
1423     (inst not r)))
1424
1425 (define-source-transform logeqv (&rest args)
1426   (if (oddp (length args))
1427       `(logxor ,@args)
1428       `(lognot (logxor ,@args))))
1429 (define-source-transform logandc1 (x y)
1430   `(logand (lognot ,x) ,y))
1431 (define-source-transform logandc2 (x y)
1432   `(logand ,x (lognot ,y)))
1433 (define-source-transform logorc1 (x y)
1434   `(logior (lognot ,x) ,y))
1435 (define-source-transform logorc2 (x y)
1436   `(logior ,x (lognot ,y)))
1437 (define-source-transform lognor (x y)
1438   `(lognot (logior ,x ,y)))
1439 (define-source-transform lognand (x y)
1440   `(lognot (logand ,x ,y)))
1441 \f
1442 ;;;; bignum stuff
1443
1444 (define-vop (bignum-length get-header-data)
1445   (:translate sb!bignum:%bignum-length)
1446   (:policy :fast-safe))
1447
1448 (define-vop (bignum-set-length set-header-data)
1449   (:translate sb!bignum:%bignum-set-length)
1450   (:policy :fast-safe))
1451
1452 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1453   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1454 (define-full-reffer+offset bignum-ref-with-offset *
1455   bignum-digits-offset other-pointer-lowtag
1456   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref-with-offset)
1457 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1458   (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1459
1460 (define-vop (digit-0-or-plus)
1461   (:translate sb!bignum:%digit-0-or-plusp)
1462   (:policy :fast-safe)
1463   (:args (digit :scs (unsigned-reg)))
1464   (:arg-types unsigned-num)
1465   (:conditional :ns)
1466   (:generator 3
1467     (inst or digit digit)))
1468
1469
1470 ;;; For add and sub with carry the sc of carry argument is any-reg so
1471 ;;; that it may be passed as a fixnum or word and thus may be 0, 1, or
1472 ;;; 4. This is easy to deal with and may save a fixnum-word
1473 ;;; conversion.
1474 (define-vop (add-w/carry)
1475   (:translate sb!bignum:%add-with-carry)
1476   (:policy :fast-safe)
1477   (:args (a :scs (unsigned-reg) :target result)
1478          (b :scs (unsigned-reg unsigned-stack) :to :eval)
1479          (c :scs (any-reg) :target temp))
1480   (:arg-types unsigned-num unsigned-num positive-fixnum)
1481   (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1482   (:results (result :scs (unsigned-reg) :from (:argument 0))
1483             (carry :scs (unsigned-reg)))
1484   (:result-types unsigned-num positive-fixnum)
1485   (:generator 4
1486     (move result a)
1487     (move temp c)
1488     (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1489     (inst adc result b)
1490     (inst mov carry 0)
1491     (inst adc carry carry)))
1492
1493 ;;; Note: the borrow is 1 for no borrow and 0 for a borrow, the opposite
1494 ;;; of the x86 convention.
1495 (define-vop (sub-w/borrow)
1496   (:translate sb!bignum:%subtract-with-borrow)
1497   (:policy :fast-safe)
1498   (:args (a :scs (unsigned-reg) :to :eval :target result)
1499          (b :scs (unsigned-reg unsigned-stack) :to :result)
1500          (c :scs (any-reg control-stack)))
1501   (:arg-types unsigned-num unsigned-num positive-fixnum)
1502   (:results (result :scs (unsigned-reg) :from :eval)
1503             (borrow :scs (unsigned-reg)))
1504   (:result-types unsigned-num positive-fixnum)
1505   (:generator 5
1506     (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1507     (move result a)
1508     (inst sbb result b)
1509     (inst mov borrow 1)
1510     (inst sbb borrow 0)))
1511
1512
1513 (define-vop (bignum-mult-and-add-3-arg)
1514   (:translate sb!bignum:%multiply-and-add)
1515   (:policy :fast-safe)
1516   (:args (x :scs (unsigned-reg) :target eax)
1517          (y :scs (unsigned-reg unsigned-stack))
1518          (carry-in :scs (unsigned-reg unsigned-stack)))
1519   (:arg-types unsigned-num unsigned-num unsigned-num)
1520   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1521                    :to (:result 1) :target lo) eax)
1522   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1523                    :to (:result 0) :target hi) edx)
1524   (:results (hi :scs (unsigned-reg))
1525             (lo :scs (unsigned-reg)))
1526   (:result-types unsigned-num unsigned-num)
1527   (:generator 20
1528     (move eax x)
1529     (inst mul eax y)
1530     (inst add eax carry-in)
1531     (inst adc edx 0)
1532     (move hi edx)
1533     (move lo eax)))
1534
1535 (define-vop (bignum-mult-and-add-4-arg)
1536   (:translate sb!bignum:%multiply-and-add)
1537   (:policy :fast-safe)
1538   (:args (x :scs (unsigned-reg) :target eax)
1539          (y :scs (unsigned-reg unsigned-stack))
1540          (prev :scs (unsigned-reg unsigned-stack))
1541          (carry-in :scs (unsigned-reg unsigned-stack)))
1542   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1543   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1544                    :to (:result 1) :target lo) eax)
1545   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1546                    :to (:result 0) :target hi) edx)
1547   (:results (hi :scs (unsigned-reg))
1548             (lo :scs (unsigned-reg)))
1549   (:result-types unsigned-num unsigned-num)
1550   (:generator 20
1551     (move eax x)
1552     (inst mul eax y)
1553     (inst add eax prev)
1554     (inst adc edx 0)
1555     (inst add eax carry-in)
1556     (inst adc edx 0)
1557     (move hi edx)
1558     (move lo eax)))
1559
1560
1561 (define-vop (bignum-mult)
1562   (:translate sb!bignum:%multiply)
1563   (:policy :fast-safe)
1564   (:args (x :scs (unsigned-reg) :target eax)
1565          (y :scs (unsigned-reg unsigned-stack)))
1566   (:arg-types unsigned-num unsigned-num)
1567   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1568                    :to (:result 1) :target lo) eax)
1569   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1570                    :to (:result 0) :target hi) edx)
1571   (:results (hi :scs (unsigned-reg))
1572             (lo :scs (unsigned-reg)))
1573   (:result-types unsigned-num unsigned-num)
1574   (:generator 20
1575     (move eax x)
1576     (inst mul eax y)
1577     (move hi edx)
1578     (move lo eax)))
1579
1580 #!+multiply-high-vops
1581 (define-vop (mulhi)
1582   (:translate sb!kernel:%multiply-high)
1583   (:policy :fast-safe)
1584   (:args (x :scs (unsigned-reg) :target eax)
1585          (y :scs (unsigned-reg unsigned-stack)))
1586   (:arg-types unsigned-num unsigned-num)
1587   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0))
1588               eax)
1589   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1590                    :to (:result 0) :target hi) edx)
1591   (:results (hi :scs (unsigned-reg)))
1592   (:result-types unsigned-num)
1593   (:generator 20
1594     (move eax x)
1595     (inst mul eax y)
1596     (move hi edx)))
1597
1598 #!+multiply-high-vops
1599 (define-vop (mulhi/fx)
1600   (:translate sb!kernel:%multiply-high)
1601   (:policy :fast-safe)
1602   (:args (x :scs (any-reg) :target eax)
1603          (y :scs (unsigned-reg unsigned-stack)))
1604   (:arg-types positive-fixnum unsigned-num)
1605   (:temporary (:sc any-reg :offset eax-offset :from (:argument 0)) eax)
1606   (:temporary (:sc any-reg :offset edx-offset :from (:argument 1)
1607                    :to (:result 0) :target hi) edx)
1608   (:results (hi :scs (any-reg)))
1609   (:result-types positive-fixnum)
1610   (:generator 15
1611     (move eax x)
1612     (inst mul eax y)
1613     (move hi edx)
1614     (inst and hi (lognot fixnum-tag-mask))))
1615
1616 (define-vop (bignum-lognot lognot-mod32/word=>unsigned)
1617   (:translate sb!bignum:%lognot))
1618
1619 (define-vop (fixnum-to-digit)
1620   (:translate sb!bignum:%fixnum-to-digit)
1621   (:policy :fast-safe)
1622   (:args (fixnum :scs (any-reg control-stack) :target digit))
1623   (:arg-types tagged-num)
1624   (:results (digit :scs (unsigned-reg)
1625                    :load-if (not (and (sc-is fixnum control-stack)
1626                                       (sc-is digit unsigned-stack)
1627                                       (location= fixnum digit)))))
1628   (:result-types unsigned-num)
1629   (:generator 1
1630     (move digit fixnum)
1631     (inst sar digit n-fixnum-tag-bits)))
1632
1633 (define-vop (bignum-floor)
1634   (:translate sb!bignum:%bigfloor)
1635   (:policy :fast-safe)
1636   (:args (div-high :scs (unsigned-reg) :target edx)
1637          (div-low :scs (unsigned-reg) :target eax)
1638          (divisor :scs (unsigned-reg unsigned-stack)))
1639   (:arg-types unsigned-num unsigned-num unsigned-num)
1640   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1641                    :to (:result 0) :target quo) eax)
1642   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1643                    :to (:result 1) :target rem) edx)
1644   (:results (quo :scs (unsigned-reg))
1645             (rem :scs (unsigned-reg)))
1646   (:result-types unsigned-num unsigned-num)
1647   (:generator 300
1648     (move edx div-high)
1649     (move eax div-low)
1650     (inst div eax divisor)
1651     (move quo eax)
1652     (move rem edx)))
1653
1654 (define-vop (signify-digit)
1655   (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1656   (:policy :fast-safe)
1657   (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1658   (:arg-types unsigned-num)
1659   (:results (res :scs (any-reg signed-reg)
1660                  :load-if (not (and (sc-is digit unsigned-stack)
1661                                     (sc-is res control-stack signed-stack)
1662                                     (location= digit res)))))
1663   (:result-types signed-num)
1664   (:generator 1
1665     (move res digit)
1666     (when (sc-is res any-reg control-stack)
1667       (inst shl res n-fixnum-tag-bits))))
1668
1669 (define-vop (digit-ashr)
1670   (:translate sb!bignum:%ashr)
1671   (:policy :fast-safe)
1672   (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1673          (count :scs (unsigned-reg) :target ecx))
1674   (:arg-types unsigned-num positive-fixnum)
1675   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1676   (:results (result :scs (unsigned-reg) :from (:argument 0)
1677                     :load-if (not (and (sc-is result unsigned-stack)
1678                                        (location= digit result)))))
1679   (:result-types unsigned-num)
1680   (:generator 2
1681     (move result digit)
1682     (move ecx count)
1683     (inst sar result :cl)))
1684
1685 (define-vop (digit-ashr/c)
1686   (:translate sb!bignum:%ashr)
1687   (:policy :fast-safe)
1688   (:args (digit :scs (unsigned-reg unsigned-stack) :target result))
1689   (:arg-types unsigned-num (:constant (integer 0 31)))
1690   (:info count)
1691   (:results (result :scs (unsigned-reg) :from (:argument 0)
1692                     :load-if (not (and (sc-is result unsigned-stack)
1693                                        (location= digit result)))))
1694   (:result-types unsigned-num)
1695   (:generator 1
1696     (move result digit)
1697     (inst sar result count)))
1698
1699 (define-vop (digit-lshr digit-ashr)
1700   (:translate sb!bignum:%digit-logical-shift-right)
1701   (:generator 1
1702     (move result digit)
1703     (move ecx count)
1704     (inst shr result :cl)))
1705
1706 (define-vop (digit-ashl digit-ashr)
1707   (:translate sb!bignum:%ashl)
1708   (:generator 1
1709     (move result digit)
1710     (move ecx count)
1711     (inst shl result :cl)))
1712 \f
1713 ;;;; static functions
1714
1715 (define-static-fun two-arg-/ (x y) :translate /)
1716
1717 (define-static-fun two-arg-gcd (x y) :translate gcd)
1718 (define-static-fun two-arg-lcm (x y) :translate lcm)
1719
1720 (define-static-fun two-arg-and (x y) :translate logand)
1721 (define-static-fun two-arg-ior (x y) :translate logior)
1722 (define-static-fun two-arg-xor (x y) :translate logxor)
1723
1724 \f
1725 ;;; Support for the Mersenne Twister, MT19937, random number generator
1726 ;;; due to Matsumoto and Nishimura.
1727 ;;;
1728 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
1729 ;;; 623-dimensionally equidistributed uniform pseudorandom number
1730 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
1731 ;;; 1997, to appear.
1732 ;;;
1733 ;;; State:
1734 ;;;  0-1:   Constant matrix A. [0, #x9908b0df] (not used here)
1735 ;;;  2:     Index; init. to 1.
1736 ;;;  3-626: State.
1737 (defknown random-mt19937 ((simple-array (unsigned-byte 32) (*)))
1738   (unsigned-byte 32) ())
1739 (define-vop (random-mt19937)
1740   (:policy :fast-safe)
1741   (:translate random-mt19937)
1742   (:args (state :scs (descriptor-reg) :to :result))
1743   (:arg-types simple-array-unsigned-byte-32)
1744   (:temporary (:sc unsigned-reg :from (:eval 0) :to :result) k)
1745   (:temporary (:sc unsigned-reg :offset eax-offset
1746                    :from (:eval 0) :to :result) tmp)
1747   (:results (y :scs (unsigned-reg) :from (:eval 0)))
1748   (:result-types unsigned-num)
1749   (:generator 50
1750     (loadw k state (+ 2 vector-data-offset) other-pointer-lowtag)
1751     (inst cmp k 624)
1752     (inst jmp :ne no-update)
1753     (inst mov tmp state)        ; The state is passed in EAX.
1754     (inst call (make-fixup 'random-mt19937-update :assembly-routine))
1755     ;; Restore k, and set to 0.
1756     (inst xor k k)
1757     NO-UPDATE
1758     ;; y = ptgfsr[k++];
1759     (inst mov y (make-ea-for-vector-data state :index k :offset 3))
1760     ;; y ^= (y >> 11);
1761     (inst shr y 11)
1762     (inst xor y (make-ea-for-vector-data state :index k :offset 3))
1763     ;; y ^= (y << 7) & #x9d2c5680
1764     (inst mov tmp y)
1765     (inst inc k)
1766     (inst shl tmp 7)
1767     (storew k state (+ 2 vector-data-offset) other-pointer-lowtag)
1768     (inst and tmp #x9d2c5680)
1769     (inst xor y tmp)
1770     ;; y ^= (y << 15) & #xefc60000
1771     (inst mov tmp y)
1772     (inst shl tmp 15)
1773     (inst and tmp #xefc60000)
1774     (inst xor y tmp)
1775     ;; y ^= (y >> 18);
1776     (inst mov tmp y)
1777     (inst shr tmp 18)
1778     (inst xor y tmp)))
1779
1780 (in-package "SB!C")
1781
1782 (defun mask-result (class width result)
1783   (ecase class
1784     (:unsigned
1785      `(logand ,result ,(1- (ash 1 width))))
1786     (:signed
1787      `(mask-signed-field ,width ,result))))
1788
1789 ;;; This is essentially a straight implementation of the algorithm in
1790 ;;; "Strength Reduction of Multiplications by Integer Constants",
1791 ;;; Youfeng Wu, ACM SIGPLAN Notices, Vol. 30, No.2, February 1995.
1792 (defun basic-decompose-multiplication (class width arg num n-bits condensed)
1793   (case (aref condensed 0)
1794     (0
1795      (let ((tmp (min 3 (aref condensed 1))))
1796        (decf (aref condensed 1) tmp)
1797        (mask-result class width
1798                     `(%lea ,arg
1799                            ,(decompose-multiplication class width
1800                              arg (ash (1- num) (- tmp)) (1- n-bits) (subseq condensed 1))
1801                            ,(ash 1 tmp) 0))))
1802     ((1 2 3)
1803      (let ((r0 (aref condensed 0)))
1804        (incf (aref condensed 1) r0)
1805        (mask-result class width
1806                     `(%lea ,(decompose-multiplication class width
1807                              arg (- num (ash 1 r0)) (1- n-bits) (subseq condensed 1))
1808                            ,arg
1809                            ,(ash 1 r0) 0))))
1810     (t (let ((r0 (aref condensed 0)))
1811          (setf (aref condensed 0) 0)
1812          (mask-result class width
1813                       `(ash ,(decompose-multiplication class width
1814                               arg (ash num (- r0)) n-bits condensed)
1815                             ,r0))))))
1816
1817 (defun decompose-multiplication (class width arg num n-bits condensed)
1818   (cond
1819     ((= n-bits 0) 0)
1820     ((= num 1) arg)
1821     ((= n-bits 1)
1822      (mask-result class width `(ash ,arg ,(1- (integer-length num)))))
1823     ((let ((max 0) (end 0))
1824        (loop for i from 2 to (length condensed)
1825              for j = (reduce #'+ (subseq condensed 0 i))
1826              when (and (> (- (* 2 i) 3 j) max)
1827                        (< (+ (ash 1 (1+ j))
1828                              (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num)
1829                                   (1+ j)))
1830                           (ash 1 32)))
1831                do (setq max (- (* 2 i) 3 j)
1832                         end i))
1833        (when (> max 0)
1834          (let ((j (reduce #'+ (subseq condensed 0 end))))
1835            (let ((n2 (+ (ash 1 (1+ j))
1836                         (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num) (1+ j))))
1837                  (n1 (1+ (ldb (byte (1+ j) 0) (lognot num)))))
1838            (mask-result class width
1839                         `(- ,(optimize-multiply class width arg n2)
1840                             ,(optimize-multiply  class width arg n1))))))))
1841     ((dolist (i '(9 5 3))
1842        (when (integerp (/ num i))
1843          (when (< (logcount (/ num i)) (logcount num))
1844            (let ((x (gensym)))
1845              (return `(let ((,x ,(optimize-multiply class width arg (/ num i))))
1846                        ,(mask-result class width
1847                                      `(%lea ,x ,x (1- ,i) 0)))))))))
1848     (t (basic-decompose-multiplication class width arg num n-bits condensed))))
1849
1850 (defun optimize-multiply (class width arg x)
1851   (let* ((n-bits (logcount x))
1852          (condensed (make-array n-bits)))
1853     (let ((count 0) (bit 0))
1854       (dotimes (i 32)
1855         (cond ((logbitp i x)
1856                (setf (aref condensed bit) count)
1857                (setf count 1)
1858                (incf bit))
1859               (t (incf count)))))
1860     (decompose-multiplication class width arg x n-bits condensed)))
1861
1862 (defun *-transformer (class width y)
1863   (cond
1864     ((= y (ash 1 (integer-length y)))
1865      ;; there's a generic transform for y = 2^k
1866      (give-up-ir1-transform))
1867     ((member y '(3 5 9))
1868      ;; we can do these multiplications directly using LEA
1869      `(%lea x x ,(1- y) 0))
1870     ((member :pentium4 *backend-subfeatures*)
1871      ;; the pentium4's multiply unit is reportedly very good
1872      (give-up-ir1-transform))
1873     ;; FIXME: should make this more fine-grained.  If nothing else,
1874     ;; there should probably be a cutoff of about 9 instructions on
1875     ;; pentium-class machines.
1876     (t (optimize-multiply class width 'x y))))
1877
1878 (deftransform * ((x y)
1879                  ((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 (deftransform sb!vm::*-mod32
1885     ((x y) ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1886      (unsigned-byte 32))
1887   "recode as leas, shifts and adds"
1888   (let ((y (lvar-value y)))
1889     (*-transformer :unsigned 32 y)))
1890
1891 (deftransform * ((x y)
1892                  (fixnum (constant-arg (unsigned-byte 32)))
1893                  fixnum)
1894   "recode as leas, shifts and adds"
1895   (let ((y (lvar-value y)))
1896     (*-transformer :signed (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits) y)))
1897 (deftransform sb!vm::*-modfx
1898     ((x y) (fixnum (constant-arg (unsigned-byte 32)))
1899      fixnum)
1900   "recode as leas, shifts and adds"
1901   (let ((y (lvar-value y)))
1902     (*-transformer :signed (- sb!vm:n-word-bits sb!vm:n-fixnum-tag-bits) y)))
1903
1904 ;;; FIXME: we should also be able to write an optimizer or two to
1905 ;;; convert (+ (* x 2) 17), (- (* x 9) 5) to a %LEA.