1.0.36.30: on x86-64 split MOVE-TO-SINGLE into -REG and -STACK versions
[sbcl.git] / src / compiler / x86-64 / float.lisp
1 ;;;; floating point support 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 (macrolet ((ea-for-xf-desc (tn slot)
15              `(make-ea
16                :qword :base ,tn
17                :disp (- (* ,slot n-word-bytes)
18                         other-pointer-lowtag))))
19   (defun ea-for-df-desc (tn)
20     (ea-for-xf-desc tn double-float-value-slot))
21   ;; complex floats
22   (defun ea-for-csf-data-desc (tn)
23     (ea-for-xf-desc tn complex-single-float-data-slot))
24   (defun ea-for-csf-real-desc (tn)
25     (ea-for-xf-desc tn complex-single-float-data-slot))
26   (defun ea-for-csf-imag-desc (tn)
27     (ea-for-xf-desc tn (+ complex-single-float-data-slot 1/2)))
28
29   (defun ea-for-cdf-data-desc (tn)
30     (ea-for-xf-desc tn complex-double-float-real-slot))
31   (defun ea-for-cdf-real-desc (tn)
32     (ea-for-xf-desc tn complex-double-float-real-slot))
33   (defun ea-for-cdf-imag-desc (tn)
34     (ea-for-xf-desc tn complex-double-float-imag-slot)))
35
36 (macrolet ((ea-for-xf-stack (tn kind)
37              (declare (ignore kind))
38              `(make-ea
39                :qword :base rbp-tn
40                :disp (frame-byte-offset (tn-offset ,tn)))))
41   (defun ea-for-sf-stack (tn)
42     (ea-for-xf-stack tn :single))
43   (defun ea-for-df-stack (tn)
44     (ea-for-xf-stack tn :double)))
45
46 ;;; complex float stack EAs
47 (macrolet ((ea-for-cxf-stack (tn kind slot &optional base)
48              `(make-ea
49                :qword :base ,base
50                :disp (frame-byte-offset
51                       (+ (tn-offset ,tn)
52                        (cond ((= (tn-offset ,base) rsp-offset)
53                               sp->fp-offset)
54                              ((= (tn-offset ,base) rbp-offset)
55                               0)
56                              (t (error "Unexpected offset.")))
57                        (ecase ,kind
58                          (:single
59                             (ecase ,slot
60                               (:real 0)
61                               (:imag -1/2)))
62                          (:double
63                             (ecase ,slot
64                               (:real 1)
65                               (:imag 0)))))))))
66   (defun ea-for-csf-data-stack (tn &optional (base rbp-tn))
67     (ea-for-cxf-stack tn :single :real base))
68   (defun ea-for-csf-real-stack (tn &optional (base rbp-tn))
69     (ea-for-cxf-stack tn :single :real base))
70   (defun ea-for-csf-imag-stack (tn &optional (base rbp-tn))
71     (ea-for-cxf-stack tn :single :imag base))
72
73   (defun ea-for-cdf-data-stack (tn &optional (base rbp-tn))
74     (ea-for-cxf-stack tn :double :real base))
75   (defun ea-for-cdf-real-stack (tn &optional (base rbp-tn))
76     (ea-for-cxf-stack tn :double :real base))
77   (defun ea-for-cdf-imag-stack (tn &optional (base rbp-tn))
78     (ea-for-cxf-stack tn :double :imag base)))
79 \f
80 ;;;; move functions
81
82 ;;; X is source, Y is destination.
83
84 (define-move-fun (load-fp-zero 1) (vop x y)
85   ((fp-single-zero) (single-reg)
86    (fp-double-zero) (double-reg)
87    (fp-complex-single-zero) (complex-single-reg)
88    (fp-complex-double-zero) (complex-double-reg))
89   (identity x)
90   (sc-case y
91     ((single-reg complex-single-reg) (inst xorps y y))
92     ((double-reg complex-double-reg) (inst xorpd y y))))
93
94 (define-move-fun (load-fp-immediate 1) (vop x y)
95   ((fp-single-immediate) (single-reg)
96    (fp-double-immediate) (double-reg)
97    (fp-complex-single-immediate) (complex-single-reg)
98    (fp-complex-double-immediate) (complex-double-reg))
99   (let ((x (register-inline-constant (tn-value x))))
100     (sc-case y
101       (single-reg (inst movss y x))
102       (double-reg (inst movsd y x))
103       (complex-single-reg (inst movq y x))
104       (complex-double-reg (inst movapd y x)))))
105
106 (define-move-fun (load-single 2) (vop x y)
107   ((single-stack) (single-reg))
108   (inst movss y (ea-for-sf-stack x)))
109
110 (define-move-fun (store-single 2) (vop x y)
111   ((single-reg) (single-stack))
112   (inst movss (ea-for-sf-stack y) x))
113
114 (define-move-fun (load-double 2) (vop x y)
115   ((double-stack) (double-reg))
116   (inst movsd y (ea-for-df-stack x)))
117
118 (define-move-fun (store-double 2) (vop x y)
119   ((double-reg) (double-stack))
120   (inst movsd  (ea-for-df-stack y) x))
121
122 (eval-when (:compile-toplevel :execute)
123   (setf *read-default-float-format* 'single-float))
124 \f
125 ;;;; complex float move functions
126
127 ;;; X is source, Y is destination.
128 (define-move-fun (load-complex-single 2) (vop x y)
129   ((complex-single-stack) (complex-single-reg))
130   (inst movq y (ea-for-csf-data-stack x)))
131
132 (define-move-fun (store-complex-single 2) (vop x y)
133   ((complex-single-reg) (complex-single-stack))
134   (inst movq (ea-for-csf-data-stack y) x))
135
136 (define-move-fun (load-complex-double 2) (vop x y)
137   ((complex-double-stack) (complex-double-reg))
138   (inst movupd y (ea-for-cdf-data-stack x)))
139
140 (define-move-fun (store-complex-double 2) (vop x y)
141   ((complex-double-reg) (complex-double-stack))
142   (inst movupd (ea-for-cdf-data-stack y) x))
143 \f
144 ;;;; move VOPs
145
146 ;;; float register to register moves
147 (macrolet ((frob (vop sc)
148              `(progn
149                 (define-vop (,vop)
150                   (:args (x :scs (,sc)
151                             :target y
152                             :load-if (not (location= x y))))
153                   (:results (y :scs (,sc)
154                                :load-if (not (location= x y))))
155                   (:note "float move")
156                   (:generator 0
157                     (move y x)))
158                 (define-move-vop ,vop :move (,sc) (,sc)))))
159   (frob single-move single-reg)
160   (frob double-move double-reg)
161   (frob complex-single-move complex-single-reg)
162   (frob complex-double-move complex-double-reg))
163
164 \f
165 ;;; Move from float to a descriptor reg. allocating a new float
166 ;;; object in the process.
167 (define-vop (move-from-single)
168   (:args (x :scs (single-reg) :to :save))
169   (:results (y :scs (descriptor-reg)))
170   (:note "float to pointer coercion")
171   (:generator 4
172     (inst movd y x)
173     (inst shl y 32)
174     (inst or y single-float-widetag)))
175
176 (define-move-vop move-from-single :move
177   (single-reg) (descriptor-reg))
178
179 (define-vop (move-from-double)
180   (:args (x :scs (double-reg) :to :save))
181   (:results (y :scs (descriptor-reg)))
182   (:node-var node)
183   (:note "float to pointer coercion")
184   (:generator 13
185      (with-fixed-allocation (y
186                              double-float-widetag
187                              double-float-size
188                              node)
189        (inst movsd (ea-for-df-desc y) x))))
190 (define-move-vop move-from-double :move
191   (double-reg) (descriptor-reg))
192
193 ;;; Move from a descriptor to a float register.
194 (define-vop (move-to-single-reg)
195    (:args (x :scs (descriptor-reg) :target tmp
196              :load-if (not (sc-is x control-stack))))
197    (:temporary (:sc unsigned-reg :from :argument :to :result) tmp)
198    (:results (y :scs (single-reg)))
199    (:note "pointer to float coercion")
200    (:generator 2
201      (sc-case x
202        (descriptor-reg
203         (move tmp x)
204         (inst shr tmp 32)
205         (inst movd y tmp))
206        (control-stack
207         ;; When the single-float descriptor is in memory, the untagging
208         ;; is done in the target XMM register. This is faster than going
209         ;; through a general-purpose register and the code is smaller.
210         (inst movq y x)
211         (inst shufps y y #4r3331)))))
212 (define-move-vop move-to-single-reg :move (descriptor-reg) (single-reg))
213
214 ;;; Move from a descriptor to a float stack.
215 (define-vop (move-to-single-stack)
216   (:args (x :scs (descriptor-reg) :target tmp))
217   (:temporary (:sc unsigned-reg :from :argument :to :result) tmp)
218   (:results (y :scs (single-stack)))
219   (:note "pointer to float coercion")
220   (:generator 2
221     (move tmp x)
222     (inst shr tmp 32)
223     (let ((slot (make-ea :dword :base rbp-tn
224                          :disp (frame-byte-offset (tn-offset y)))))
225       (inst mov slot (reg-in-size tmp :dword)))))
226 (define-move-vop move-to-single-stack :move (descriptor-reg) (single-stack))
227
228 (define-vop (move-to-double)
229   (:args (x :scs (descriptor-reg)))
230   (:results (y :scs (double-reg)))
231   (:note "pointer to float coercion")
232   (:generator 2
233     (inst movsd y (ea-for-df-desc x))))
234 (define-move-vop move-to-double :move (descriptor-reg) (double-reg))
235
236 \f
237 ;;; Move from complex float to a descriptor reg. allocating a new
238 ;;; complex float object in the process.
239 (define-vop (move-from-complex-single)
240   (:args (x :scs (complex-single-reg) :to :save))
241   (:results (y :scs (descriptor-reg)))
242   (:node-var node)
243   (:note "complex float to pointer coercion")
244   (:generator 13
245      (with-fixed-allocation (y
246                              complex-single-float-widetag
247                              complex-single-float-size
248                              node)
249        (inst movq (ea-for-csf-data-desc y) x))))
250 (define-move-vop move-from-complex-single :move
251   (complex-single-reg) (descriptor-reg))
252
253 (define-vop (move-from-complex-double)
254   (:args (x :scs (complex-double-reg) :to :save))
255   (:results (y :scs (descriptor-reg)))
256   (:node-var node)
257   (:note "complex float to pointer coercion")
258   (:generator 13
259      (with-fixed-allocation (y
260                              complex-double-float-widetag
261                              complex-double-float-size
262                              node)
263        (inst movapd (ea-for-cdf-data-desc y) x))))
264 (define-move-vop move-from-complex-double :move
265   (complex-double-reg) (descriptor-reg))
266
267 ;;; Move from a descriptor to a complex float register.
268 (macrolet ((frob (name sc format)
269              `(progn
270                 (define-vop (,name)
271                   (:args (x :scs (descriptor-reg)))
272                   (:results (y :scs (,sc)))
273                   (:note "pointer to complex float coercion")
274                   (:generator 2
275                     ,(ecase format
276                       (:single
277                          '(inst movq y (ea-for-csf-data-desc x)))
278                       (:double
279                          '(inst movapd y (ea-for-cdf-data-desc x))))))
280                 (define-move-vop ,name :move (descriptor-reg) (,sc)))))
281   (frob move-to-complex-single complex-single-reg :single)
282   (frob move-to-complex-double complex-double-reg :double))
283 \f
284 ;;;; the move argument vops
285 ;;;;
286 ;;;; Note these are also used to stuff fp numbers onto the c-call
287 ;;;; stack so the order is different than the lisp-stack.
288
289 ;;; the general MOVE-ARG VOP
290 (macrolet ((frob (name sc stack-sc format)
291              `(progn
292                 (define-vop (,name)
293                   (:args (x :scs (,sc) :target y)
294                          (fp :scs (any-reg)
295                              :load-if (not (sc-is y ,sc))))
296                   (:results (y))
297                   (:note "float argument move")
298                   (:generator ,(case format (:single 2) (:double 3) )
299                     (sc-case y
300                       (,sc
301                        (move y x))
302                       (,stack-sc
303                        (if (= (tn-offset fp) esp-offset)
304                            (let* ((offset (* (tn-offset y) n-word-bytes))
305                                   (ea (make-ea :dword :base fp :disp offset)))
306                              ,@(ecase format
307                                       (:single '((inst movss ea x)))
308                                       (:double '((inst movsd ea x)))))
309                            (let ((ea (make-ea
310                                       :dword :base fp
311                                       :disp (frame-byte-offset (tn-offset y)))))
312                              ,@(ecase format
313                                  (:single '((inst movss ea x)))
314                                  (:double '((inst movsd ea x))))))))))
315                 (define-move-vop ,name :move-arg
316                   (,sc descriptor-reg) (,sc)))))
317   (frob move-single-float-arg single-reg single-stack :single)
318   (frob move-double-float-arg double-reg double-stack :double))
319
320 ;;;; complex float MOVE-ARG VOP
321 (macrolet ((frob (name sc stack-sc format)
322              `(progn
323                 (define-vop (,name)
324                   (:args (x :scs (,sc) :target y)
325                          (fp :scs (any-reg)
326                              :load-if (not (sc-is y ,sc))))
327                   (:results (y))
328                   (:note "complex float argument move")
329                   (:generator ,(ecase format (:single 2) (:double 3))
330                     (sc-case y
331                       (,sc
332                        (move y x))
333                       (,stack-sc
334                        ,(ecase format
335                           (:single
336                              '(inst movq (ea-for-csf-data-stack y fp) x))
337                           (:double
338                              '(inst movupd (ea-for-cdf-data-stack y fp) x)))))))
339                 (define-move-vop ,name :move-arg
340                   (,sc descriptor-reg) (,sc)))))
341   (frob move-complex-single-float-arg
342         complex-single-reg complex-single-stack :single)
343   (frob move-complex-double-float-arg
344         complex-double-reg complex-double-stack :double))
345
346 (define-move-vop move-arg :move-arg
347   (single-reg double-reg
348    complex-single-reg complex-double-reg)
349   (descriptor-reg))
350
351 \f
352 ;;;; arithmetic VOPs
353
354 (define-vop (float-op)
355   (:args (x) (y))
356   (:results (r))
357   (:policy :fast-safe)
358   (:note "inline float arithmetic")
359   (:vop-var vop)
360   (:save-p :compute-only))
361
362 (macrolet ((frob (name comm-name sc constant-sc ptype)
363              `(progn
364                 (define-vop (,name float-op)
365                   (:args (x :scs (,sc ,constant-sc)
366                             :target r
367                             :load-if (not (sc-is x ,constant-sc)))
368                          (y :scs (,sc ,constant-sc)
369                             :load-if (not (sc-is y ,constant-sc))))
370                   (:results (r :scs (,sc)))
371                   (:arg-types ,ptype ,ptype)
372                   (:result-types ,ptype))
373                 (define-vop (,comm-name float-op)
374                   (:args (x :scs (,sc ,constant-sc)
375                             :target r
376                             :load-if (not (sc-is x ,constant-sc)))
377                          (y :scs (,sc ,constant-sc)
378                             :target r
379                             :load-if (not (sc-is y ,constant-sc))))
380                   (:results (r :scs (,sc)))
381                   (:arg-types ,ptype ,ptype)
382                   (:result-types ,ptype)))))
383   (frob single-float-op single-float-comm-op
384         single-reg fp-single-immediate single-float)
385   (frob double-float-op double-float-comm-op
386         double-reg fp-double-immediate double-float)
387   (frob complex-single-float-op complex-single-float-comm-op
388         complex-single-reg fp-complex-single-immediate
389         complex-single-float)
390   (frob complex-double-float-op complex-double-float-comm-op
391         complex-double-reg fp-complex-double-immediate
392         complex-double-float))
393
394 (macrolet ((generate (opinst commutative constant-sc load-inst)
395              `(flet ((get-constant (tn)
396                        (register-inline-constant
397                         ,@(and (eq constant-sc 'fp-single-immediate)
398                                '(:aligned))
399                         (tn-value tn))))
400                 (declare (ignorable #'get-constant))
401                 (cond
402                   ((location= x r)
403                    (when (sc-is y ,constant-sc)
404                      (setf y (get-constant y)))
405                    (inst ,opinst x y))
406                   ((and ,commutative (location= y r))
407                    (when (sc-is x ,constant-sc)
408                      (setf x (get-constant x)))
409                    (inst ,opinst y x))
410                   ((not (location= r y))
411                    (if (sc-is x ,constant-sc)
412                        (inst ,load-inst r (get-constant x))
413                        (move r x))
414                    (when (sc-is y ,constant-sc)
415                      (setf y (get-constant y)))
416                    (inst ,opinst r y))
417                   (t
418                    (if (sc-is x ,constant-sc)
419                        (inst ,load-inst tmp (get-constant x))
420                        (move tmp x))
421                    (inst ,opinst tmp y)
422                    (move r tmp)))))
423            (frob (op sinst sname scost dinst dname dcost commutative
424                      &optional csinst csname cscost cdinst cdname cdcost)
425              `(progn
426                 (define-vop (,sname ,(if commutative
427                                          'single-float-comm-op
428                                          'single-float-op))
429                   (:translate ,op)
430                   (:temporary (:sc single-reg) tmp)
431                   (:generator ,scost
432                     (generate ,sinst ,commutative fp-single-immediate movss)))
433                 (define-vop (,dname ,(if commutative
434                                          'double-float-comm-op
435                                          'double-float-op))
436                   (:translate ,op)
437                   (:temporary (:sc double-reg) tmp)
438                   (:generator ,dcost
439                     (generate ,dinst ,commutative fp-double-immediate movsd)))
440                 ,(when csinst
441                    `(define-vop (,csname
442                                  ,(if commutative
443                                       'complex-single-float-comm-op
444                                       'complex-single-float-op))
445                       (:translate ,op)
446                       (:temporary (:sc complex-single-reg) tmp)
447                       (:generator ,cscost
448                         (generate ,csinst ,commutative
449                                   fp-complex-single-immediate movq))))
450                 ,(when cdinst
451                    `(define-vop (,cdname
452                                  ,(if commutative
453                                       'complex-double-float-comm-op
454                                       'complex-double-float-op))
455                       (:translate ,op)
456                       (:temporary (:sc complex-double-reg) tmp)
457                       (:generator ,cdcost
458                         (generate ,cdinst ,commutative
459                                   fp-complex-double-immediate movapd)))))))
460   (frob + addss +/single-float 2 addsd +/double-float 2 t
461         addps +/complex-single-float 3 addpd +/complex-double-float 3)
462   (frob - subss -/single-float 2 subsd -/double-float 2 nil
463         subps -/complex-single-float 3 subpd -/complex-double-float 3)
464   (frob * mulss */single-float 4 mulsd */double-float 5 t)
465   (frob / divss //single-float 12 divsd //double-float 19 nil))
466
467 (macrolet ((frob (op cost commutativep
468                      duplicate-inst op-inst real-move-inst complex-move-inst
469                      real-sc real-constant-sc real-type
470                      complex-sc complex-constant-sc complex-type
471                      real-complex-name complex-real-name)
472              (cond ((not duplicate-inst) ; simple case
473                     `(flet ((load-into (r x)
474                               (sc-case x
475                                 (,real-constant-sc
476                                  (inst ,real-move-inst r
477                                        (register-inline-constant (tn-value x))))
478                                 (,complex-constant-sc
479                                  (inst ,complex-move-inst r
480                                        (register-inline-constant (tn-value x))))
481                                 (t (move r x)))))
482                        ,(when real-complex-name
483                           `(define-vop (,real-complex-name float-op)
484                              (:translate ,op)
485                              (:args (x :scs (,real-sc ,real-constant-sc)
486                                        :target r
487                                        :load-if (not (sc-is x ,real-constant-sc)))
488                                     (y :scs (,complex-sc ,complex-constant-sc)
489                                        ,@(when commutativep '(:target r))
490                                        :load-if (not (sc-is y ,complex-constant-sc))))
491                              (:arg-types ,real-type ,complex-type)
492                              (:results (r :scs (,complex-sc)
493                                           ,@(unless commutativep '(:from (:argument 0)))))
494                              (:result-types ,complex-type)
495                              (:generator ,cost
496                                ,(when commutativep
497                                   `(when (location= y r)
498                                      (rotatef x y)))
499                                (load-into r x)
500                                (when (sc-is y ,real-constant-sc ,complex-constant-sc)
501                                  (setf y (register-inline-constant
502                                           :aligned (tn-value y))))
503                                (inst ,op-inst r y))))
504
505                        ,(when complex-real-name
506                           `(define-vop (,complex-real-name float-op)
507                              (:translate ,op)
508                              (:args (x :scs (,complex-sc ,complex-constant-sc)
509                                        :target r
510                                        :load-if (not (sc-is x ,complex-constant-sc)))
511                                     (y :scs (,real-sc ,real-constant-sc)
512                                        ,@(when commutativep '(:target r))
513                                        :load-if (not (sc-is y ,real-constant-sc))))
514                              (:arg-types ,complex-type ,real-type)
515                              (:results (r :scs (,complex-sc)
516                                           ,@(unless commutativep '(:from (:argument 0)))))
517                              (:result-types ,complex-type)
518                              (:generator ,cost
519                                ,(when commutativep
520                                   `(when (location= y r)
521                                      (rotatef x y)))
522                                (load-into r x)
523                                (when (sc-is y ,real-constant-sc ,complex-constant-sc)
524                                  (setf y (register-inline-constant
525                                           :aligned (tn-value y))))
526                                (inst ,op-inst r y))))))
527                    (commutativep ; must duplicate, but commutative
528                     `(progn
529                        ,(when real-complex-name
530                           `(define-vop (,real-complex-name float-op)
531                              (:translate ,op)
532                              (:args (x :scs (,real-sc ,real-constant-sc)
533                                        :target dup
534                                        :load-if (not (sc-is x ,real-constant-sc)))
535                                     (y :scs (,complex-sc ,complex-constant-sc)
536                                        :target r
537                                        :to  :result
538                                        :load-if (not (sc-is y ,complex-constant-sc))))
539                              (:arg-types ,real-type ,complex-type)
540                              (:temporary (:sc ,complex-sc :target r
541                                           :from (:argument 0)
542                                           :to   :result)
543                                          dup)
544                              (:results (r :scs (,complex-sc)))
545                              (:result-types ,complex-type)
546                              (:generator ,cost
547                                (if (sc-is x ,real-constant-sc)
548                                    (inst ,complex-move-inst dup
549                                          (register-inline-constant
550                                           (complex (tn-value x) (tn-value x))))
551                                    (let ((real x))
552                                      ,duplicate-inst))
553                                 ;; safe: dup /= y
554                                 (when (location= dup r)
555                                   (rotatef dup y))
556                                 (if (sc-is y ,complex-constant-sc)
557                                     (inst ,complex-move-inst r
558                                           (register-inline-constant (tn-value y)))
559                                     (move r y))
560                                 (when (sc-is dup ,complex-constant-sc)
561                                   (setf dup (register-inline-constant
562                                              :aligned (tn-value dup))))
563                                 (inst ,op-inst r dup))))
564
565                        ,(when complex-real-name
566                           `(define-vop (,complex-real-name float-op)
567                              (:translate ,op)
568                              (:args (x :scs (,complex-sc ,complex-constant-sc)
569                                        :target r
570                                        :to  :result
571                                        :load-if (not (sc-is x ,complex-constant-sc)))
572                                     (y :scs (,real-sc ,real-constant-sc)
573                                        :target dup
574                                        :load-if (not (sc-is y ,real-constant-sc))))
575                              (:arg-types ,complex-type ,real-type)
576                              (:temporary (:sc ,complex-sc :target r
577                                           :from (:argument 1)
578                                           :to :result)
579                                          dup)
580                              (:results (r :scs (,complex-sc)))
581                              (:result-types ,complex-type)
582                              (:generator ,cost
583                                (if (sc-is y ,real-constant-sc)
584                                    (inst ,complex-move-inst dup
585                                          (register-inline-constant
586                                           (complex (tn-value y) (tn-value y))))
587                                    (let ((real y))
588                                      ,duplicate-inst))
589                                 (when (location= dup r)
590                                   (rotatef x dup))
591                                 (if (sc-is x ,complex-constant-sc)
592                                     (inst ,complex-move-inst r
593                                           (register-inline-constant (tn-value x)))
594                                     (move r x))
595                                 (when (sc-is dup ,complex-constant-sc)
596                                   (setf dup (register-inline-constant
597                                              :aligned (tn-value dup))))
598                                 (inst ,op-inst r dup))))))
599                    (t ; duplicate, not commutative
600                     `(progn
601                        ,(when real-complex-name
602                           `(define-vop (,real-complex-name float-op)
603                              (:translate ,op)
604                              (:args (x :scs (,real-sc ,real-constant-sc)
605                                        :target r
606                                        :load-if (not (sc-is x ,real-constant-sc)))
607                                     (y :scs (,complex-sc ,complex-constant-sc)
608                                        :to :result
609                                        :load-if (not (sc-is y ,complex-constant-sc))))
610                              (:arg-types ,real-type ,complex-type)
611                              (:results (r :scs (,complex-sc) :from (:argument 0)))
612                              (:result-types ,complex-type)
613                              (:generator ,cost
614                                (if (sc-is x ,real-constant-sc)
615                                    (inst ,complex-move-inst dup
616                                          (register-inline-constant
617                                           (complex (tn-value x) (tn-value x))))
618                                    (let ((real x)
619                                          (dup  r))
620                                      ,duplicate-inst))
621                                (when (sc-is y ,complex-constant-sc)
622                                  (setf y (register-inline-constant
623                                           :aligned (tn-value y))))
624                                (inst ,op-inst r y))))
625
626                        ,(when complex-real-name
627                           `(define-vop (,complex-real-name float-op)
628                              (:translate ,op)
629                              (:args (x :scs (,complex-sc)
630                                        :target r
631                                        :to :eval)
632                                     (y :scs (,real-sc ,real-constant-sc)
633                                        :target dup
634                                        :load-if (not (sc-is y ,complex-constant-sc))))
635                              (:arg-types ,complex-type ,real-type)
636                              (:temporary (:sc ,complex-sc :from (:argument 1))
637                                          dup)
638                              (:results (r :scs (,complex-sc) :from :eval))
639                              (:result-types ,complex-type)
640                              (:generator ,cost
641                                (if (sc-is y ,real-constant-sc)
642                                    (setf dup (register-inline-constant
643                                               :aligned (complex (tn-value y)
644                                                                 (tn-value y))))
645                                    (let ((real y))
646                                      ,duplicate-inst))
647                                (move r x)
648                                (inst ,op-inst r dup))))))))
649            (def-real-complex-op (op commutativep duplicatep
650                                     single-inst single-real-complex-name single-complex-real-name single-cost
651                                     double-inst double-real-complex-name double-complex-real-name double-cost)
652                `(progn
653                   (frob ,op ,single-cost ,commutativep
654                         ,(and duplicatep
655                               `(progn
656                                  (move dup real)
657                                  (inst unpcklps dup dup)))
658                         ,single-inst movss movq
659                         single-reg fp-single-immediate single-float
660                         complex-single-reg fp-complex-single-immediate complex-single-float
661                         ,single-real-complex-name ,single-complex-real-name)
662                   (frob ,op ,double-cost ,commutativep
663                         ,(and duplicatep
664                               `(progn
665                                  (move dup real)
666                                  (inst unpcklpd dup dup)))
667                         ,double-inst movsd movapd
668                         double-reg fp-double-immediate double-float
669                         complex-double-reg fp-complex-double-immediate complex-double-float
670                         ,double-real-complex-name ,double-complex-real-name))))
671   (def-real-complex-op + t nil
672     addps +/real-complex-single-float +/complex-real-single-float 3
673     addpd +/real-complex-double-float +/complex-real-double-float 4)
674   (def-real-complex-op - nil nil
675     subps -/real-complex-single-float -/complex-real-single-float 3
676     subpd -/real-complex-double-float -/complex-real-double-float 4)
677   (def-real-complex-op * t t
678     mulps */real-complex-single-float */complex-real-single-float 4
679     mulpd */real-complex-double-float */complex-real-double-float 5)
680   (def-real-complex-op / nil t
681     nil nil nil nil
682     divpd nil //complex-real-double-float 19))
683
684 (define-vop (//complex-real-single-float float-op)
685   (:translate /)
686   (:args (x :scs (complex-single-reg fp-complex-single-immediate fp-complex-single-zero)
687             :to (:result 0)
688             :target r
689             :load-if (not (sc-is x fp-complex-single-immediate fp-complex-single-zero)))
690          (y :scs (single-reg fp-single-immediate fp-single-zero)
691             :target dup
692             :load-if (not (sc-is y fp-single-immediate fp-single-zero))))
693   (:arg-types complex-single-float single-float)
694   (:temporary (:sc complex-single-reg :from (:argument 1)) dup)
695   (:results (r :scs (complex-single-reg)))
696   (:result-types complex-single-float)
697   (:generator 12
698     (flet ((duplicate (x)
699              (let ((word (ldb (byte 64 0)
700                               (logior (ash (single-float-bits (imagpart x)) 32)
701                                       (ldb (byte 32 0)
702                                            (single-float-bits (realpart x)))))))
703                (register-inline-constant :oword (logior (ash word 64) word)))))
704       (sc-case y
705         (fp-single-immediate
706          (setf dup (duplicate (complex (tn-value y) (tn-value y)))))
707         (fp-single-zero
708          (inst xorps dup dup))
709         (t (move dup y)
710            (inst shufps dup dup #b00000000)))
711       (sc-case x
712         (fp-complex-single-immediate
713          (inst movaps r (duplicate (tn-value x))))
714         (fp-complex-single-zero
715          (inst xorps r r))
716         (t
717          (move r x)
718          (inst unpcklpd r r)))
719       (inst divps r dup)
720       (inst movq r r))))
721
722 ;; Complex multiplication
723 ;; r := rx * ry - ix * iy
724 ;; i := rx * iy + ix * ry
725 ;;
726 ;; Transpose for SIMDness
727 ;;  rx*ry    rx*iy
728 ;; -ix*iy   +ix*ry
729 ;;
730 ;;  [rx rx] * [ry iy]
731 ;;+ [ix ix] * [-iy ry]
732 ;;       [r i]
733
734 (macrolet ((define-complex-* (name cost type sc tmp-p &body body)
735                `(define-vop (,name float-op)
736                   (:translate *)
737                   (:args (x :scs (,sc) :target r)
738                          (y :scs (,sc) :target copy-y))
739                   (:arg-types ,type ,type)
740                   (:temporary (:sc ,sc) imag)
741                   (:temporary (:sc ,sc :from :eval) copy-y)
742                   ,@(when tmp-p
743                       `((:temporary (:sc ,sc) xmm)))
744                   (:results (r :scs (,sc) :from :eval))
745                   (:result-types ,type)
746                   (:generator ,cost
747                     (when (or (location= x copy-y)
748                               (location= y r))
749                       (rotatef x y))
750                     ,@body))))
751   (define-complex-* */complex-single-float 20
752     complex-single-float complex-single-reg t
753     (inst xorps xmm xmm)
754     (move r x)
755     (inst unpcklps r r)
756     (move imag r)
757     (inst unpckhpd imag xmm)
758     (inst unpcklpd r    xmm)
759     (move copy-y y)  ; y == r only if y == x == r
760     (setf y copy-y)
761
762     (inst mulps r y)
763
764     (inst shufps y y #b11110001)
765     (inst xorps y (register-inline-constant :oword (ash 1 31)))
766
767     (inst mulps imag y)
768     (inst addps r imag))
769   (define-complex-* */complex-double-float 25
770     complex-double-float complex-double-reg nil
771     (move imag x)
772     (move r x)
773     (move copy-y y)
774     (setf y copy-y)
775     (inst unpcklpd r r)
776     (inst unpckhpd imag imag)
777
778     (inst mulpd r y)
779
780     (inst shufpd y y #b01)
781     (inst xorpd y (register-inline-constant :oword (ash 1 63)))
782
783     (inst mulpd imag y)
784     (inst addpd r imag)))
785
786 (define-vop (fsqrt)
787   (:args (x :scs (double-reg)))
788   (:results (y :scs (double-reg)))
789   (:translate %sqrt)
790   (:policy :fast-safe)
791   (:arg-types double-float)
792   (:result-types double-float)
793   (:note "inline float arithmetic")
794   (:vop-var vop)
795   (:save-p :compute-only)
796   (:generator 1
797      (note-this-location vop :internal-error)
798      (inst sqrtsd y x)))
799 \f
800 (macrolet ((frob ((name translate sc type) &body body)
801              `(define-vop (,name)
802                   (:args (x :scs (,sc) :target y))
803                 (:results (y :scs (,sc)))
804                 (:translate ,translate)
805                 (:policy :fast-safe)
806                 (:arg-types ,type)
807                 (:result-types ,type)
808                 (:note "inline float arithmetic")
809                 (:vop-var vop)
810                 (:save-p :compute-only)
811                 (:generator 1
812                             (note-this-location vop :internal-error)
813                             ;; we should be able to do this better.  what we
814                             ;; really would like to do is use the target as the
815                             ;; temp whenever it's not also the source
816                             (move y x)
817                             ,@body))))
818   (frob (%negate/double-float %negate double-reg double-float)
819         (inst xorpd y (register-inline-constant :oword (ash 1 63))))
820   (frob (%negate/complex-double-float %negate complex-double-reg complex-double-float)
821         (inst xorpd y (register-inline-constant
822                        :oword (logior (ash 1 127) (ash 1 63)))))
823   (frob (conjugate/complex-double-float conjugate complex-double-reg complex-double-float)
824         (inst xorpd y (register-inline-constant :oword (ash 1 127))))
825   (frob (%negate/single-float %negate single-reg single-float)
826         (inst xorps y (register-inline-constant :oword (ash 1 31))))
827   (frob (%negate/complex-single-float %negate complex-single-reg complex-single-float)
828         (inst xorps y (register-inline-constant
829                        :oword (logior (ash 1 31) (ash 1 63)))))
830   (frob (conjugate/complex-single-float conjugate complex-single-reg complex-single-float)
831         (inst xorpd y (register-inline-constant :oword (ash 1 63))))
832   (frob (abs/double-float abs  double-reg double-float)
833         (inst andpd y (register-inline-constant :oword (ldb (byte 63 0) -1))))
834   (frob (abs/single-float abs  single-reg single-float)
835         (inst andps y (register-inline-constant :oword (ldb (byte 31 0) -1)))))
836
837 \f
838 ;;;; comparison
839
840 (define-vop (float-compare)
841   (:policy :fast-safe)
842   (:vop-var vop)
843   (:save-p :compute-only)
844   (:note "inline float comparison"))
845
846 ;;; EQL
847 (macrolet ((define-float-eql (name cost sc constant-sc type)
848                `(define-vop (,name float-compare)
849                   (:translate eql)
850                   (:args (x :scs (,sc ,constant-sc)
851                             :target mask
852                             :load-if (not (sc-is x ,constant-sc)))
853                          (y :scs (,sc ,constant-sc)
854                             :target mask
855                             :load-if (not (sc-is y ,constant-sc))))
856                   (:arg-types ,type ,type)
857                   (:temporary (:sc ,sc :from :eval) mask)
858                   (:temporary (:sc any-reg) bits)
859                   (:conditional :e)
860                   (:generator ,cost
861                     (when (or (location= y mask)
862                               (not (xmm-register-p x)))
863                       (rotatef x y))
864                     (aver (xmm-register-p x))
865                     (move mask x)
866                     (when (sc-is y ,constant-sc)
867                       (setf y (register-inline-constant :aligned (tn-value y))))
868                     (inst pcmpeqd mask y)
869                     (inst movmskps bits mask)
870                     (inst cmp bits #b1111)))))
871   (define-float-eql eql/single-float 4
872     single-reg fp-single-immediate single-float)
873   (define-float-eql eql/double-float 4
874     double-reg fp-double-immediate double-float)
875   (define-float-eql eql/complex-single-float 5
876     complex-single-reg fp-complex-single-immediate complex-single-float)
877   (define-float-eql eql/complex-double-float 5
878     complex-double-reg fp-complex-double-immediate complex-double-float))
879
880 ;;; comiss and comisd can cope with one or other arg in memory: we
881 ;;; could (should, indeed) extend these to cope with descriptor args
882 ;;; and stack args
883
884 (define-vop (single-float-compare float-compare)
885   (:args (x :scs (single-reg))
886          (y :scs (single-reg single-stack fp-single-immediate)
887             :load-if (not (sc-is y single-stack fp-single-immediate))))
888   (:arg-types single-float single-float))
889 (define-vop (double-float-compare float-compare)
890   (:args (x :scs (double-reg))
891          (y :scs (double-reg double-stack descriptor-reg fp-double-immediate)
892             :load-if (not (sc-is y double-stack descriptor-reg fp-double-immediate))))
893   (:arg-types double-float double-float))
894
895 (define-vop (=/single-float single-float-compare)
896   (:translate =)
897   (:args (x :scs (single-reg single-stack fp-single-immediate)
898             :target xmm
899             :load-if (not (sc-is x single-stack fp-single-immediate)))
900          (y :scs (single-reg single-stack fp-single-immediate)
901             :target xmm
902             :load-if (not (sc-is y single-stack fp-single-immediate))))
903   (:temporary (:sc single-reg :from :eval) xmm)
904   (:info)
905   (:conditional not :p :ne)
906   (:vop-var vop)
907   (:generator 3
908     (when (or (location= y xmm)
909               (and (not (xmm-register-p x))
910                    (xmm-register-p y)))
911       (rotatef x y))
912     (sc-case x
913       (single-reg (setf xmm x))
914       (single-stack (inst movss xmm (ea-for-sf-stack x)))
915       (fp-single-immediate
916        (inst movss xmm (register-inline-constant (tn-value x)))))
917     (sc-case y
918       (single-stack
919        (setf y (ea-for-sf-stack y)))
920       (fp-single-immediate
921        (setf y (register-inline-constant (tn-value y))))
922       (t))
923     (note-this-location vop :internal-error)
924     (inst comiss xmm y)
925     ;; if PF&CF, there was a NaN involved => not equal
926     ;; otherwise, ZF => equal
927     ))
928
929 (define-vop (=/double-float double-float-compare)
930   (:translate =)
931   (:args (x :scs (double-reg double-stack fp-double-immediate descriptor-reg)
932             :target xmm
933             :load-if (not (sc-is x double-stack fp-double-immediate descriptor-reg)))
934          (y :scs (double-reg double-stack fp-double-immediate descriptor-reg)
935             :target xmm
936             :load-if (not (sc-is y double-stack fp-double-immediate descriptor-reg))))
937   (:temporary (:sc double-reg :from :eval) xmm)
938   (:info)
939   (:conditional not :p :ne)
940   (:vop-var vop)
941   (:generator 3
942     (when (or (location= y xmm)
943               (and (not (xmm-register-p x))
944                    (xmm-register-p y)))
945       (rotatef x y))
946     (sc-case x
947       (double-reg
948        (setf xmm x))
949       (double-stack
950        (inst movsd xmm (ea-for-df-stack x)))
951       (fp-double-immediate
952        (inst movsd xmm (register-inline-constant (tn-value x))))
953       (descriptor-reg
954        (inst movsd xmm (ea-for-df-desc x))))
955     (sc-case y
956       (double-stack
957        (setf y (ea-for-df-stack y)))
958       (fp-double-immediate
959        (setf y (register-inline-constant (tn-value y))))
960       (descriptor-reg
961        (setf y (ea-for-df-desc y)))
962       (t))
963     (note-this-location vop :internal-error)
964     (inst comisd xmm y)))
965
966 (macrolet ((define-complex-float-= (complex-complex-name complex-real-name real-complex-name
967                                     real-sc real-constant-sc real-type
968                                     complex-sc complex-constant-sc complex-type
969                                     real-move-inst complex-move-inst
970                                     cmp-inst mask-inst mask)
971                `(progn
972                   (define-vop (,complex-complex-name float-compare)
973                     (:translate =)
974                     (:args (x :scs (,complex-sc ,complex-constant-sc)
975                               :target cmp
976                               :load-if (not (sc-is x ,complex-constant-sc)))
977                            (y :scs (,complex-sc ,complex-constant-sc)
978                               :target cmp
979                               :load-if (not (sc-is y ,complex-constant-sc))))
980                     (:arg-types ,complex-type ,complex-type)
981                     (:temporary (:sc ,complex-sc :from :eval) cmp)
982                     (:temporary (:sc unsigned-reg) bits)
983                     (:info)
984                     (:conditional :e)
985                     (:generator 3
986                       (when (location= y cmp)
987                         (rotatef x y))
988                       (sc-case x
989                         (,real-constant-sc
990                          (inst ,real-move-inst cmp (register-inline-constant
991                                                     (tn-value x))))
992                         (,complex-constant-sc
993                          (inst ,complex-move-inst cmp (register-inline-constant
994                                                        (tn-value x))))
995                         (t
996                          (move cmp x)))
997                       (when (sc-is y ,real-constant-sc ,complex-constant-sc)
998                         (setf y (register-inline-constant :aligned (tn-value y))))
999                       (note-this-location vop :internal-error)
1000                       (inst ,cmp-inst :eq cmp y)
1001                       (inst ,mask-inst bits cmp)
1002                       (inst cmp bits ,mask)))
1003                   (define-vop (,complex-real-name ,complex-complex-name)
1004                     (:args (x :scs (,complex-sc ,complex-constant-sc)
1005                               :target cmp
1006                               :load-if (not (sc-is x ,complex-constant-sc)))
1007                            (y :scs (,real-sc ,real-constant-sc)
1008                               :target cmp
1009                               :load-if (not (sc-is y ,real-constant-sc))))
1010                     (:arg-types ,complex-type ,real-type))
1011                   (define-vop (,real-complex-name ,complex-complex-name)
1012                     (:args (x :scs (,real-sc ,real-constant-sc)
1013                               :target cmp
1014                               :load-if (not (sc-is x ,real-constant-sc)))
1015                            (y :scs (,complex-sc ,complex-constant-sc)
1016                               :target cmp
1017                               :load-if (not (sc-is y ,complex-constant-sc))))
1018                     (:arg-types ,real-type ,complex-type)))))
1019   (define-complex-float-= =/complex-single-float =/complex-real-single-float =/real-complex-single-float
1020     single-reg fp-single-immediate single-float
1021     complex-single-reg fp-complex-single-immediate complex-single-float
1022     movss movq cmpps movmskps #b1111)
1023   (define-complex-float-= =/complex-double-float =/complex-real-double-float =/real-complex-double-float
1024     double-reg fp-double-immediate double-float
1025     complex-double-reg fp-complex-double-immediate complex-double-float
1026     movsd movapd cmppd movmskpd #b11))
1027
1028 (macrolet ((define-</> (op single-name double-name &rest flags)
1029                `(progn
1030                   (define-vop (,double-name double-float-compare)
1031                     (:translate ,op)
1032                     (:info)
1033                     (:conditional ,@flags)
1034                     (:generator 3
1035                       (sc-case y
1036                         (double-stack
1037                          (setf y (ea-for-df-stack y)))
1038                         (descriptor-reg
1039                          (setf y (ea-for-df-desc y)))
1040                         (fp-double-immediate
1041                          (setf y (register-inline-constant (tn-value y))))
1042                         (t))
1043                       (inst comisd x y)))
1044                   (define-vop (,single-name single-float-compare)
1045                     (:translate ,op)
1046                     (:info)
1047                     (:conditional ,@flags)
1048                     (:generator 3
1049                       (sc-case y
1050                         (single-stack
1051                          (setf y (ea-for-sf-stack y)))
1052                         (fp-single-immediate
1053                          (setf y (register-inline-constant (tn-value y))))
1054                         (t))
1055                       (inst comiss x y))))))
1056   (define-</> < <single-float <double-float not :p :nc)
1057   (define-</> > >single-float >double-float not :p :na))
1058
1059 \f
1060 ;;;; conversion
1061
1062 (macrolet ((frob (name translate inst to-sc to-type)
1063              `(define-vop (,name)
1064                 (:args (x :scs (signed-stack signed-reg)))
1065                 (:results (y :scs (,to-sc)))
1066                 (:arg-types signed-num)
1067                 (:result-types ,to-type)
1068                 (:policy :fast-safe)
1069                 (:note "inline float coercion")
1070                 (:translate ,translate)
1071                 (:vop-var vop)
1072                 (:save-p :compute-only)
1073                 (:generator 5
1074                   (note-this-location vop :internal-error)
1075                   (inst ,inst y x)))))
1076   (frob %single-float/signed %single-float cvtsi2ss single-reg single-float)
1077   (frob %double-float/signed %double-float cvtsi2sd double-reg double-float))
1078
1079 (macrolet ((frob (name translate inst from-scs from-type ea-func to-sc to-type)
1080              `(define-vop (,name)
1081                (:args (x :scs ,from-scs :target y))
1082                (:results (y :scs (,to-sc)))
1083                (:arg-types ,from-type)
1084                (:result-types ,to-type)
1085                (:policy :fast-safe)
1086                (:note "inline float coercion")
1087                (:translate ,translate)
1088                (:vop-var vop)
1089                (:save-p :compute-only)
1090                (:generator 2
1091                 (note-this-location vop :internal-error)
1092                 (inst ,inst y (sc-case x
1093                                 (,(first from-scs) x)
1094                                 (,(second from-scs) (,ea-func x))))))))
1095   (frob %single-float/double-float %single-float cvtsd2ss
1096         (double-reg double-stack) double-float ea-for-df-stack
1097         single-reg single-float)
1098
1099   (frob %double-float/single-float %double-float cvtss2sd
1100         (single-reg single-stack) single-float ea-for-sf-stack
1101         double-reg double-float))
1102
1103 (macrolet ((frob (trans inst from-scs from-type ea-func)
1104              `(define-vop (,(symbolicate trans "/" from-type))
1105                (:args (x :scs ,from-scs))
1106                (:results (y :scs (signed-reg)))
1107                (:arg-types ,from-type)
1108                (:result-types signed-num)
1109                (:translate ,trans)
1110                (:policy :fast-safe)
1111                (:note "inline float truncate")
1112                (:vop-var vop)
1113                (:save-p :compute-only)
1114                (:generator 5
1115                  (inst ,inst y (sc-case x
1116                                  (,(first from-scs) x)
1117                                  (,(second from-scs) (,ea-func x))))))))
1118   (frob %unary-truncate/single-float cvttss2si
1119         (single-reg single-stack) single-float ea-for-sf-stack)
1120   (frob %unary-truncate/double-float cvttsd2si
1121         (double-reg double-stack) double-float ea-for-df-stack)
1122
1123   (frob %unary-round cvtss2si
1124         (single-reg single-stack) single-float ea-for-sf-stack)
1125   (frob %unary-round cvtsd2si
1126         (double-reg double-stack) double-float ea-for-df-stack))
1127
1128 (define-vop (make-single-float)
1129   (:args (bits :scs (signed-reg) :target res
1130                :load-if (not (or (and (sc-is bits signed-stack)
1131                                       (sc-is res single-reg))
1132                                  (and (sc-is bits signed-stack)
1133                                       (sc-is res single-stack)
1134                                       (location= bits res))))))
1135   (:results (res :scs (single-reg single-stack)))
1136   (:arg-types signed-num)
1137   (:result-types single-float)
1138   (:translate make-single-float)
1139   (:policy :fast-safe)
1140   (:vop-var vop)
1141   (:generator 4
1142     (sc-case res
1143        (single-stack
1144         (sc-case bits
1145           (signed-reg
1146            (inst mov res bits))
1147           (signed-stack
1148            (aver (location= bits res)))))
1149        (single-reg
1150         (sc-case bits
1151           (signed-reg
1152            (inst movd res bits))
1153           (signed-stack
1154            (inst movd res bits)))))))
1155
1156 (define-vop (make-double-float)
1157   (:args (hi-bits :scs (signed-reg))
1158          (lo-bits :scs (unsigned-reg)))
1159   (:results (res :scs (double-reg)))
1160   (:temporary (:sc unsigned-reg) temp)
1161   (:arg-types signed-num unsigned-num)
1162   (:result-types double-float)
1163   (:translate make-double-float)
1164   (:policy :fast-safe)
1165   (:vop-var vop)
1166   (:generator 2
1167     (move temp hi-bits)
1168     (inst shl temp 32)
1169     (inst or temp lo-bits)
1170     (inst movd res temp)))
1171
1172 (define-vop (single-float-bits)
1173   (:args (float :scs (single-reg descriptor-reg)
1174                 :load-if (not (sc-is float single-stack))))
1175   (:results (bits :scs (signed-reg)))
1176   (:temporary (:sc signed-stack :from :argument :to :result) stack-temp)
1177   (:arg-types single-float)
1178   (:result-types signed-num)
1179   (:translate single-float-bits)
1180   (:policy :fast-safe)
1181   (:vop-var vop)
1182   (:generator 4
1183     (sc-case bits
1184       (signed-reg
1185        (sc-case float
1186          (single-reg
1187           (inst movss stack-temp float)
1188           (move bits stack-temp))
1189          (single-stack
1190           (move bits float))
1191          (descriptor-reg
1192           (move bits float)
1193           (inst shr bits 32))))
1194       (signed-stack
1195        (sc-case float
1196          (single-reg
1197           (inst movss bits float)))))
1198     ;; Sign-extend
1199     (inst shl bits 32)
1200     (inst sar bits 32)))
1201
1202 (define-vop (double-float-high-bits)
1203   (:args (float :scs (double-reg descriptor-reg)
1204                 :load-if (not (sc-is float double-stack))))
1205   (:results (hi-bits :scs (signed-reg)))
1206   (:temporary (:sc signed-stack :from :argument :to :result) temp)
1207   (:arg-types double-float)
1208   (:result-types signed-num)
1209   (:translate double-float-high-bits)
1210   (:policy :fast-safe)
1211   (:vop-var vop)
1212   (:generator 5
1213      (sc-case float
1214        (double-reg
1215         (inst movsd temp float)
1216         (move hi-bits temp))
1217        (double-stack
1218         (loadw hi-bits ebp-tn (frame-word-offset (tn-offset float))))
1219        (descriptor-reg
1220         (loadw hi-bits float double-float-value-slot
1221                other-pointer-lowtag)))
1222      (inst sar hi-bits 32)))
1223
1224 (define-vop (double-float-low-bits)
1225   (:args (float :scs (double-reg descriptor-reg)
1226                 :load-if (not (sc-is float double-stack))))
1227   (:results (lo-bits :scs (unsigned-reg)))
1228   (:temporary (:sc signed-stack :from :argument :to :result) temp)
1229   (:arg-types double-float)
1230   (:result-types unsigned-num)
1231   (:translate double-float-low-bits)
1232   (:policy :fast-safe)
1233   (:vop-var vop)
1234   (:generator 5
1235      (sc-case float
1236        (double-reg
1237         (inst movsd temp float)
1238         (move lo-bits temp))
1239        (double-stack
1240         (loadw lo-bits ebp-tn (frame-word-offset (tn-offset float))))
1241        (descriptor-reg
1242         (loadw lo-bits float double-float-value-slot
1243                other-pointer-lowtag)))
1244      (inst shl lo-bits 32)
1245      (inst shr lo-bits 32)))
1246
1247 \f
1248
1249 ;;;; complex float VOPs
1250
1251 (define-vop (make-complex-single-float)
1252   (:translate complex)
1253   (:args (real :scs (single-reg fp-single-zero)
1254                :target r
1255                :load-if (not (sc-is real fp-single-zero)))
1256          (imag :scs (single-reg fp-single-zero)
1257                :load-if (not (sc-is imag fp-single-zero))))
1258   (:arg-types single-float single-float)
1259   (:results (r :scs (complex-single-reg) :from (:argument 0)))
1260   (:result-types complex-single-float)
1261   (:note "inline complex single-float creation")
1262   (:policy :fast-safe)
1263   (:generator 5
1264     (cond ((sc-is real fp-single-zero)
1265            (inst xorps r r)
1266            (unless (sc-is imag fp-single-zero)
1267              (inst unpcklps r imag)))
1268           ((location= real imag)
1269            (move r real)
1270            (inst unpcklps r r))
1271           (t
1272            (move r real)
1273            (unless (sc-is imag fp-single-zero)
1274              (inst unpcklps r imag))))))
1275
1276 (define-vop (make-complex-double-float)
1277   (:translate complex)
1278   (:args (real :scs (double-reg fp-double-zero)
1279                :target r
1280                :load-if (not (sc-is real fp-double-zero)))
1281          (imag :scs (double-reg fp-double-zero)
1282                :load-if (not (sc-is imag fp-double-zero))))
1283   (:arg-types double-float double-float)
1284   (:results (r :scs (complex-double-reg) :from (:argument 0)))
1285   (:result-types complex-double-float)
1286   (:note "inline complex double-float creation")
1287   (:policy :fast-safe)
1288   (:generator 5
1289     (cond ((sc-is real fp-double-zero)
1290            (inst xorpd r r)
1291            (unless (sc-is imag fp-double-zero)
1292              (inst unpcklpd r imag)))
1293           ((location= real imag)
1294            (move r real)
1295            (inst unpcklpd r r))
1296           (t
1297            (move r real)
1298            (unless (sc-is imag fp-double-zero)
1299              (inst unpcklpd r imag))))))
1300
1301 (define-vop (complex-float-value)
1302   (:args (x :target r))
1303   (:temporary (:sc complex-double-reg) zero)
1304   (:results (r))
1305   (:variant-vars offset)
1306   (:policy :fast-safe)
1307   (:generator 3
1308     (cond ((sc-is x complex-double-reg)
1309            (move r x)
1310            (inst xorpd zero zero)
1311            (ecase offset
1312              (0 (inst unpcklpd r zero))
1313              (1 (inst unpckhpd r zero))))
1314           ((sc-is x complex-single-reg)
1315            (move r x)
1316            (ecase offset
1317              (0 (inst shufps r r #b11111100))
1318              (1 (inst shufps r r #b11111101))))
1319           ((sc-is r single-reg)
1320            (let ((ea (sc-case x
1321                        (complex-single-stack
1322                         (ecase offset
1323                           (0 (ea-for-csf-real-stack x))
1324                           (1 (ea-for-csf-imag-stack x))))
1325                        (descriptor-reg
1326                         (ecase offset
1327                           (0 (ea-for-csf-real-desc x))
1328                           (1 (ea-for-csf-imag-desc x)))))))
1329              (inst movss r ea)))
1330           ((sc-is r double-reg)
1331            (let ((ea (sc-case x
1332                        (complex-double-stack
1333                         (ecase offset
1334                           (0 (ea-for-cdf-real-stack x))
1335                           (1 (ea-for-cdf-imag-stack x))))
1336                        (descriptor-reg
1337                         (ecase offset
1338                           (0 (ea-for-cdf-real-desc x))
1339                           (1 (ea-for-cdf-imag-desc x)))))))
1340              (inst movsd r ea)))
1341           (t (error "COMPLEX-FLOAT-VALUE VOP failure")))))
1342
1343 (define-vop (realpart/complex-single-float complex-float-value)
1344   (:translate realpart)
1345   (:args (x :scs (complex-single-reg complex-single-stack descriptor-reg)
1346             :target r))
1347   (:arg-types complex-single-float)
1348   (:results (r :scs (single-reg)))
1349   (:result-types single-float)
1350   (:note "complex float realpart")
1351   (:variant 0))
1352
1353 (define-vop (realpart/complex-double-float complex-float-value)
1354   (:translate realpart)
1355   (:args (x :scs (complex-double-reg complex-double-stack descriptor-reg)
1356             :target r))
1357   (:arg-types complex-double-float)
1358   (:results (r :scs (double-reg)))
1359   (:result-types double-float)
1360   (:note "complex float realpart")
1361   (:variant 0))
1362
1363 (define-vop (imagpart/complex-single-float complex-float-value)
1364   (:translate imagpart)
1365   (:args (x :scs (complex-single-reg complex-single-stack descriptor-reg)
1366             :target r))
1367   (:arg-types complex-single-float)
1368   (:results (r :scs (single-reg)))
1369   (:result-types single-float)
1370   (:note "complex float imagpart")
1371   (:variant 1))
1372
1373 (define-vop (imagpart/complex-double-float complex-float-value)
1374   (:translate imagpart)
1375   (:args (x :scs (complex-double-reg complex-double-stack descriptor-reg)
1376             :target r))
1377   (:arg-types complex-double-float)
1378   (:results (r :scs (double-reg)))
1379   (:result-types double-float)
1380   (:note "complex float imagpart")
1381   (:variant 1))
1382
1383 \f
1384 ;;; hack dummy VOPs to bias the representation selection of their
1385 ;;; arguments towards a FP register, which can help avoid consing at
1386 ;;; inappropriate locations
1387 (defknown double-float-reg-bias (double-float) (values))
1388 (define-vop (double-float-reg-bias)
1389   (:translate double-float-reg-bias)
1390   (:args (x :scs (double-reg double-stack) :load-if nil))
1391   (:arg-types double-float)
1392   (:policy :fast-safe)
1393   (:note "inline dummy FP register bias")
1394   (:ignore x)
1395   (:generator 0))
1396 (defknown single-float-reg-bias (single-float) (values))
1397 (define-vop (single-float-reg-bias)
1398   (:translate single-float-reg-bias)
1399   (:args (x :scs (single-reg single-stack) :load-if nil))
1400   (:arg-types single-float)
1401   (:policy :fast-safe)
1402   (:note "inline dummy FP register bias")
1403   (:ignore x)
1404   (:generator 0))
1405
1406 (defknown swap-complex ((complex float)) (complex float)
1407     (foldable flushable movable always-translatable))
1408 (defoptimizer (swap-complex derive-type) ((x))
1409   (sb!c::lvar-type x))
1410 (defun swap-complex (x)
1411   (complex (imagpart x) (realpart x)))
1412 (define-vop (swap-complex-single-float)
1413   (:translate swap-complex)
1414   (:policy :fast-safe)
1415   (:args (x :scs (complex-single-reg) :target r))
1416   (:arg-types complex-single-float)
1417   (:results (r :scs (complex-single-reg)))
1418   (:result-types complex-single-float)
1419   (:generator 2
1420      (move r x)
1421      (inst shufps r r #b11110001)))
1422 (define-vop (swap-complex-double-float)
1423   (:translate swap-complex)
1424   (:policy :fast-safe)
1425   (:args (x :scs (complex-double-reg) :target r))
1426   (:arg-types complex-double-float)
1427   (:results (r :scs (complex-double-reg)))
1428   (:result-types complex-double-float)
1429   (:generator 2
1430      (move r x)
1431      (inst shufpd r r #b01)))