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