Newer
Older
;;; -*- Mode: LISP; Syntax: Common-Lisp; Base: 10; Package: x86 -*-
;;;
;;; **********************************************************************
;;; This code was written as part of the CMU Common Lisp project at
;;; Carnegie Mellon University, and has been placed in the public domain.
;;; If you want to use this code or any part of CMU Common Lisp, please contact
;;; Scott Fahlman or slisp-group@cs.cmu.edu.
;;;
(ext:file-comment
"$Header: src/compiler/x86/float-sse2.lisp $")
;;;
;;; **********************************************************************
;;;
;;; This file contains floating point support for the x86.
;;;
(in-package :x86)
(intl:textdomain "cmucl-sse2")
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
;;; Popping the FP stack.
;;;
;;; The default is to use a store and pop, fstp fr0.
;;; For the AMD Athlon, using ffreep fr0 is faster.
;;;
(defun fp-pop ()
(if (backend-featurep :athlon)
(inst ffreep fr0-tn)
(inst fstp fr0-tn)))
(macrolet ((ea-for-xf-desc (tn slot)
`(make-ea
:dword :base ,tn
:disp (- (* ,slot vm:word-bytes) vm:other-pointer-type))))
(defun ea-for-sf-desc (tn)
(ea-for-xf-desc tn vm:single-float-value-slot))
(defun ea-for-df-desc (tn)
(ea-for-xf-desc tn vm:double-float-value-slot))
#+long-float
(defun ea-for-lf-desc (tn)
(ea-for-xf-desc tn vm:long-float-value-slot))
;; Complex floats
(defun ea-for-csf-real-desc (tn)
(ea-for-xf-desc tn vm:complex-single-float-real-slot))
(defun ea-for-csf-imag-desc (tn)
(ea-for-xf-desc tn vm:complex-single-float-imag-slot))
(defun ea-for-cdf-real-desc (tn)
(ea-for-xf-desc tn vm:complex-double-float-real-slot))
(defun ea-for-cdf-imag-desc (tn)
(ea-for-xf-desc tn vm:complex-double-float-imag-slot))
#+long-float
(defun ea-for-clf-real-desc (tn)
(ea-for-xf-desc tn vm:complex-long-float-real-slot))
#+long-float
(defun ea-for-clf-imag-desc (tn)
(ea-for-xf-desc tn vm:complex-long-float-imag-slot))
#+double-double
(defun ea-for-cddf-real-hi-desc (tn)
(ea-for-xf-desc tn vm:complex-double-double-float-real-hi-slot))
#+double-double
(defun ea-for-cddf-real-lo-desc (tn)
(ea-for-xf-desc tn vm:complex-double-double-float-real-lo-slot))
#+double-double
(defun ea-for-cddf-imag-hi-desc (tn)
(ea-for-xf-desc tn vm:complex-double-double-float-imag-hi-slot))
#+double-double
(defun ea-for-cddf-imag-lo-desc (tn)
(ea-for-xf-desc tn vm:complex-double-double-float-imag-lo-slot))
)
(macrolet ((ea-for-xf-stack (tn kind)
`(make-ea
:dword :base ebp-tn
:disp (- (* (+ (tn-offset ,tn)
(ecase ,kind (:single 1) (:double 2) (:long 3)))
vm:word-bytes)))))
(defun ea-for-sf-stack (tn)
(ea-for-xf-stack tn :single))
(defun ea-for-df-stack (tn)
(ea-for-xf-stack tn :double))
#+long-float
(defun ea-for-lf-stack (tn)
(ea-for-xf-stack tn :long)))
;;; Complex float stack EAs
(macrolet ((ea-for-cxf-stack (tn kind slot &optional base)
`(make-ea
:dword :base ,base
:disp (- (* (+ (tn-offset ,tn)
(* (ecase ,kind
(:single 1)
(:double 2)
(:long 3))
(ecase ,slot
;; We want the real part to be at
;; the lower address!
(:real 2)
(:imag 1)
(:real-hi 1)
(:real-lo 2)
(:imag-hi 3)
(:imag-lo 4))))
vm:word-bytes)))))
(defun ea-for-csf-real-stack (tn &optional (base ebp-tn))
(ea-for-cxf-stack tn :single :real base))
(defun ea-for-csf-imag-stack (tn &optional (base ebp-tn))
(ea-for-cxf-stack tn :single :imag base))
(defun ea-for-cdf-real-stack (tn &optional (base ebp-tn))
(ea-for-cxf-stack tn :double :real base))
(defun ea-for-cdf-imag-stack (tn &optional (base ebp-tn))
(ea-for-cxf-stack tn :double :imag base))
;;
#+long-float
(defun ea-for-clf-real-stack (tn &optional (base ebp-tn))
(ea-for-cxf-stack tn :long :real base))
#+long-float
(defun ea-for-clf-imag-stack (tn &optional (base ebp-tn))
(ea-for-cxf-stack tn :long :imag base))
#+double-double
(defun ea-for-cddf-real-hi-stack (tn &optional (base ebp-tn))
(ea-for-cxf-stack tn :double :real-hi base))
#+double-double
(defun ea-for-cddf-real-lo-stack (tn &optional (base ebp-tn))
(ea-for-cxf-stack tn :double :real-lo base))
#+double-double
(defun ea-for-cddf-imag-hi-stack (tn &optional (base ebp-tn))
(ea-for-cxf-stack tn :double :imag-hi base))
#+double-double
(defun ea-for-cddf-imag-lo-stack (tn &optional (base ebp-tn))
(ea-for-cxf-stack tn :double :imag-lo base))
)
;;; The x86 can't store a long-float to memory without popping the
;;; stack and marking a register as empty, so it is necessary to
;;; restore the register from memory.
(defun store-long-float (ea)
(inst fstpl ea)
(inst fldl ea))
;;;; Move functions:
;;; x is source, y is destination
(define-move-function (load-single 2) (vop x y)
((single-stack) (single-reg))
(inst movss y (ea-for-sf-stack x)))
(define-move-function (store-single 2) (vop x y)
((single-reg) (single-stack))
(inst movss (ea-for-sf-stack y) x))
(define-move-function (load-double 2) (vop x y)
((double-stack) (double-reg))
(inst movsd y (ea-for-df-stack x)))
(define-move-function (store-double 2) (vop x y)
((double-reg) (double-stack))
(inst movsd (ea-for-df-stack y) x))
#+long-float
(define-move-function (load-long 2) (vop x y)
((long-stack) (long-reg))
(with-empty-tn@fp-top(y)
(inst fldl (ea-for-lf-stack x))))
#+long-float
(define-move-function (store-long 2) (vop x y)
((long-reg) (long-stack))
(cond ((zerop (tn-offset x))
(store-long-float (ea-for-lf-stack y)))
(t
(inst fxch x)
(store-long-float (ea-for-lf-stack y))
;; This may not be necessary as ST0 is likely invalid now.
(inst fxch x))))
(define-move-function (load-fp-constant 2) (vop x y)
((fp-constant) (single-reg double-reg))
(let ((value (c::constant-value (c::tn-leaf x))))
(cond ((and (zerop value)
(= (float-sign value) 1))
(sc-case y
(single-reg (inst xorps y y))
(double-reg (inst xorpd y y))))
(t
(warn (intl:gettext "Ignoring bogus i387 Constant ~a") value)))))
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
;;;; Complex float move functions
(defun complex-single-reg-real-tn (x)
(make-random-tn :kind :normal :sc (sc-or-lose 'single-reg *backend*)
:offset (tn-offset x)))
(defun complex-single-reg-imag-tn (x)
(make-random-tn :kind :normal :sc (sc-or-lose 'single-reg *backend*)
:offset (1+ (tn-offset x))))
(defun complex-double-reg-real-tn (x)
(make-random-tn :kind :normal :sc (sc-or-lose 'double-reg *backend*)
:offset (tn-offset x)))
(defun complex-double-reg-imag-tn (x)
(make-random-tn :kind :normal :sc (sc-or-lose 'double-reg *backend*)
:offset (1+ (tn-offset x))))
#+long-float
(defun complex-long-reg-real-tn (x)
(make-random-tn :kind :normal :sc (sc-or-lose 'long-reg *backend*)
:offset (tn-offset x)))
#+long-float
(defun complex-long-reg-imag-tn (x)
(make-random-tn :kind :normal :sc (sc-or-lose 'long-reg *backend*)
:offset (1+ (tn-offset x))))
#+double-double
(progn
(defun complex-double-double-reg-real-hi-tn (x)
(make-random-tn :kind :normal :sc (sc-or-lose 'double-reg *backend*)
:offset (tn-offset x)))
(defun complex-double-double-reg-real-lo-tn (x)
(make-random-tn :kind :normal :sc (sc-or-lose 'double-reg *backend*)
:offset (+ 1 (tn-offset x))))
(defun complex-double-double-reg-imag-hi-tn (x)
(make-random-tn :kind :normal :sc (sc-or-lose 'double-reg *backend*)
:offset (+ 2 (tn-offset x))))
(defun complex-double-double-reg-imag-lo-tn (x)
(make-random-tn :kind :normal :sc (sc-or-lose 'double-reg *backend*)
:offset (+ 3 (tn-offset x))))
)
;;; x is source, y is destination
(define-move-function (load-complex-single 2) (vop x y)
((complex-single-stack) (complex-single-reg))
(inst movlps y (ea-for-csf-real-stack x)))
(define-move-function (store-complex-single 2) (vop x y)
((complex-single-reg) (complex-single-stack))
(inst movlps (ea-for-csf-real-stack y) x))
(define-move-function (load-complex-double 2) (vop x y)
((complex-double-stack) (complex-double-reg))
(inst movupd y (ea-for-cdf-real-stack x)))
(define-move-function (store-complex-double 2) (vop x y)
((complex-double-reg) (complex-double-stack))
(inst movupd (ea-for-cdf-real-stack y) x))
#+long-float
(define-move-function (load-complex-long 2) (vop x y)
((complex-long-stack) (complex-long-reg))
(let ((real-tn (complex-long-reg-real-tn y)))
(with-empty-tn@fp-top(real-tn)
(inst fldl (ea-for-clf-real-stack x))))
(let ((imag-tn (complex-long-reg-imag-tn y)))
(with-empty-tn@fp-top(imag-tn)
(inst fldl (ea-for-clf-imag-stack x)))))
#+long-float
(define-move-function (store-complex-long 2) (vop x y)
((complex-long-reg) (complex-long-stack))
(let ((real-tn (complex-long-reg-real-tn x)))
(cond ((zerop (tn-offset real-tn))
(store-long-float (ea-for-clf-real-stack y)))
(t
(inst fxch real-tn)
(store-long-float (ea-for-clf-real-stack y))
(inst fxch real-tn))))
(let ((imag-tn (complex-long-reg-imag-tn x)))
(inst fxch imag-tn)
(store-long-float (ea-for-clf-imag-stack y))
(inst fxch imag-tn)))
#+double-double
(progn
(define-move-function (load-complex-double-double 4) (vop x y)
((complex-double-double-stack) (complex-double-double-reg))
(let ((real-tn (complex-double-double-reg-real-hi-tn y)))
(inst movsd real-tn (ea-for-cddf-real-hi-stack x)))
(let ((real-tn (complex-double-double-reg-real-lo-tn y)))
(inst movsd real-tn (ea-for-cddf-real-lo-stack x)))
(let ((imag-tn (complex-double-double-reg-imag-hi-tn y)))
(inst movsd imag-tn (ea-for-cddf-imag-hi-stack x)))
(let ((imag-tn (complex-double-double-reg-imag-lo-tn y)))
(inst movsd imag-tn (ea-for-cddf-imag-lo-stack x))))
(define-move-function (store-complex-double-double 4) (vop x y)
((complex-double-double-reg) (complex-double-double-stack))
;; FIXME: These may not be right!!!!
(let ((real-tn (complex-double-double-reg-real-hi-tn x)))
(inst movsd (ea-for-cddf-real-hi-stack y) real-tn))
(let ((real-tn (complex-double-double-reg-real-lo-tn x)))
(inst movsd (ea-for-cddf-real-lo-stack y) real-tn))
(let ((imag-tn (complex-double-double-reg-imag-hi-tn x)))
(inst movsd (ea-for-cddf-imag-hi-stack y) imag-tn))
(let ((imag-tn (complex-double-double-reg-imag-lo-tn x)))
(inst movsd (ea-for-cddf-imag-lo-stack y) imag-tn)))
)
;;;; Move VOPs:
;;;
;;; Float register to register moves.
;;;
(define-vop (float-move)
(:args (x))
(:results (y))
(:note _N"float move")
(:generator 0
(unless (location= x y)
(inst movq y x))))
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
(define-vop (float-move/single)
(:args (x))
(:results (y))
(:note _N"float move")
(:temporary (:sc single-stack) temp)
(:generator 0
(unless (location= x y)
(let ((x-offset (tn-offset x))
(y-offset (tn-offset y)))
(cond ((and (zerop x-offset)
(>= y-offset 8))
;; Move fr0 to xmm
(inst fst (ea-for-sf-stack temp))
(inst movss y (ea-for-sf-stack temp)))
((and (>= x-offset 8)
(>= y-offset 8))
(inst movq y x))
(t
(error "Don't know how to move ~S to ~S" x y)))))))
(define-vop (float-move/double)
(:args (x))
(:results (y))
(:note _N"float move")
(:temporary (:sc double-stack) temp)
(:generator 0
(unless (location= x y)
(let ((x-offset (tn-offset x))
(y-offset (tn-offset y)))
(cond ((and (zerop x-offset)
(>= y-offset 8))
;; Move fr0 to xmm
(inst fstd (ea-for-df-stack temp))
(inst movsd y (ea-for-df-stack temp)))
((and (>= x-offset 8)
(>= y-offset 8))
(inst movq y x))
(t
(error "Don't know how to move ~S to ~S" x y)))))))
(define-vop (single-move float-move/single)
(:args (x :scs (single-reg) :target y :load-if (not (location= x y))))
(:results (y :scs (single-reg) :load-if (not (location= x y)))))
(define-move-vop single-move :move (single-reg) (single-reg))
(define-vop (double-move float-move/double)
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
(:args (x :scs (double-reg) :target y :load-if (not (location= x y))))
(:results (y :scs (double-reg) :load-if (not (location= x y)))))
(define-move-vop double-move :move (double-reg) (double-reg))
#+long-float
(define-vop (long-move float-move)
(:args (x :scs (long-reg) :target y :load-if (not (location= x y))))
(:results (y :scs (long-reg) :load-if (not (location= x y)))))
#+long-float
(define-move-vop long-move :move (long-reg) (long-reg))
;;;
;;; Complex float register to register moves.
;;;
(define-vop (complex-single-move)
(:args (x :scs (complex-single-reg) :target y
:load-if (not (location= x y))))
(:results (y :scs (complex-single-reg) :load-if (not (location= x y))))
(:generator 0
(unless (location= x y)
(inst movaps y x))))
(define-move-vop complex-single-move :move
(complex-single-reg) (complex-single-reg))
(define-vop (complex-double-move)
(:args (x :scs (complex-double-reg)
:target y :load-if (not (location= x y))))
(:results (y :scs (complex-double-reg) :load-if (not (location= x y))))
(:generator 0
(unless (location= x y)
(inst movapd y x))))
(define-move-vop complex-double-move :move
(complex-double-reg) (complex-double-reg))
#+long-float
(define-vop (complex-long-move complex-float-move)
(:args (x :scs (complex-long-reg)
:target y :load-if (not (location= x y))))
(:results (y :scs (complex-long-reg) :load-if (not (location= x y)))))
#+long-float
(define-move-vop complex-long-move :move
(complex-long-reg) (complex-long-reg))
;;;
;;; Move from float to a descriptor reg. allocating a new float
;;; object in the process.
;;;
(define-vop (move-from-single)
(:args (x :scs (single-reg) :to :save))
(:results (y :scs (descriptor-reg)))
(:node-var node)
(:note _N"float to pointer coercion")
(:generator 13
(with-fixed-allocation (y vm:single-float-type vm:single-float-size node)
(inst movss (ea-for-sf-desc y) x))))
(define-move-vop move-from-single :move
(single-reg) (descriptor-reg))
(define-vop (move-from-double)
(:args (x :scs (double-reg) :to :save))
(:results (y :scs (descriptor-reg)))
(:node-var node)
(:note _N"float to pointer coercion")
(:generator 13
(with-fixed-allocation (y vm:double-float-type vm:double-float-size node)
(inst movsd (ea-for-df-desc y) x))))
(define-move-vop move-from-double :move
(double-reg) (descriptor-reg))
#+long-float
(define-vop (move-from-long)
(:args (x :scs (long-reg) :to :save))
(:results (y :scs (descriptor-reg)))
(:node-var node)
(:note _N"float to pointer coercion")
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
(:generator 13
(with-fixed-allocation (y vm:long-float-type vm:long-float-size node)
(with-tn@fp-top(x)
(store-long-float (ea-for-lf-desc y))))))
#+long-float
(define-move-vop move-from-long :move
(long-reg) (descriptor-reg))
(define-vop (move-from-fp-constant)
(:args (x :scs (fp-constant)))
(:results (y :scs (descriptor-reg)))
(:generator 2
(ecase (c::constant-value (c::tn-leaf x))
(0f0 (load-symbol-value y *fp-constant-0s0*))
#+nil
(1f0 (load-symbol-value y *fp-constant-1s0*))
(0d0 (load-symbol-value y *fp-constant-0d0*))
#+nil
(1d0 (load-symbol-value y *fp-constant-1d0*)))))
(define-move-vop move-from-fp-constant :move
(fp-constant) (descriptor-reg))
;;;
;;; Move from a descriptor to a float register
;;;
(define-vop (move-to-single)
(:args (x :scs (descriptor-reg)))
(:results (y :scs (single-reg)))
(:note _N"pointer to float coercion")
(:generator 2
(inst movss y (ea-for-sf-desc x))))
(define-move-vop move-to-single :move (descriptor-reg) (single-reg))
(define-vop (move-to-double)
(:args (x :scs (descriptor-reg)))
(:results (y :scs (double-reg)))
(:note _N"pointer to float coercion")
(:generator 2
(inst movsd y (ea-for-df-desc x))))
(define-move-vop move-to-double :move (descriptor-reg) (double-reg))
#+long-float
(define-vop (move-to-long)
(:args (x :scs (descriptor-reg)))
(:results (y :scs (long-reg)))
(:note _N"pointer to float coercion")
(:generator 2
(with-empty-tn@fp-top(y)
(inst fldl (ea-for-lf-desc x)))))
#+long-float
(define-move-vop move-to-long :move (descriptor-reg) (long-reg))
;;;
;;; Move from complex float to a descriptor reg. allocating a new
;;; complex float object in the process.
;;;
(define-vop (move-from-complex-single)
(:args (x :scs (complex-single-reg) :to :save))
(:results (y :scs (descriptor-reg)))
(:node-var node)
(:note _N"complex float to pointer coercion")
(:generator 13
(with-fixed-allocation (y vm:complex-single-float-type
vm:complex-single-float-size node)
(inst movlps (ea-for-csf-real-desc y) x))))
(define-move-vop move-from-complex-single :move
(complex-single-reg) (descriptor-reg))
(define-vop (move-from-complex-double)
(:args (x :scs (complex-double-reg) :to :save))
(:results (y :scs (descriptor-reg)))
(:node-var node)
(:note _N"complex float to pointer coercion")
(:generator 13
(with-fixed-allocation (y vm:complex-double-float-type
vm:complex-double-float-size node)
(inst movupd (ea-for-cdf-real-desc y) x))))
(define-move-vop move-from-complex-double :move
(complex-double-reg) (descriptor-reg))
#+long-float
(define-vop (move-from-complex-long)
(:args (x :scs (complex-long-reg) :to :save))
(:results (y :scs (descriptor-reg)))
(:node-var node)
(:note _N"complex float to pointer coercion")
(:generator 13
(with-fixed-allocation (y vm:complex-long-float-type
vm:complex-long-float-size node)
(let ((real-tn (complex-long-reg-real-tn x)))
(with-tn@fp-top(real-tn)
(store-long-float (ea-for-clf-real-desc y))))
(let ((imag-tn (complex-long-reg-imag-tn x)))
(with-tn@fp-top(imag-tn)
(store-long-float (ea-for-clf-imag-desc y)))))))
#+long-float
(define-move-vop move-from-complex-long :move
(complex-long-reg) (descriptor-reg))
#+double-double
(define-vop (move-from-complex-double-double)
(:args (x :scs (complex-double-double-reg) :to :save))
(:results (y :scs (descriptor-reg)))
(:node-var node)
(:note _N"complex double-double float to pointer coercion")
(:generator 13
(with-fixed-allocation (y vm::complex-double-double-float-type
vm::complex-double-double-float-size node)
(let ((real-tn (complex-double-double-reg-real-hi-tn x)))
(inst movsd (ea-for-cddf-real-hi-desc y) real-tn))
(let ((real-tn (complex-double-double-reg-real-lo-tn x)))
(inst movsd (ea-for-cddf-real-lo-desc y) real-tn))
(let ((imag-tn (complex-double-double-reg-imag-hi-tn x)))
(inst movsd (ea-for-cddf-imag-hi-desc y) imag-tn))
(let ((imag-tn (complex-double-double-reg-imag-lo-tn x)))
(inst movsd (ea-for-cddf-imag-lo-desc y) imag-tn)))))
;;;
#+double-double
(define-move-vop move-from-complex-double-double :move
(complex-double-double-reg) (descriptor-reg))
;;;
;;; Move from a descriptor to a complex float register
;;;
(define-vop (move-to-complex-single)
(:args (x :scs (descriptor-reg)))
(:results (y :scs (complex-single-reg)))
(:note _N"pointer to complex float coercion")
(:generator 2
(inst movlps y (ea-for-csf-real-desc x))))
(define-move-vop move-to-complex-single :move
(descriptor-reg) (complex-single-reg))
(define-vop (move-to-complex-double)
(:args (x :scs (descriptor-reg)))
(:results (y :scs (complex-double-reg)))
(:note _N"pointer to complex float coercion")
(:generator 2
(inst movupd y (ea-for-cdf-real-desc x))))
(define-move-vop move-to-complex-double :move
(descriptor-reg) (complex-double-reg))
;;;
;;; The move argument vops.
;;;
;;; Note these are also used to stuff fp numbers onto the c-call stack
;;; so the order is different than the lisp-stack.
;;; The general move-argument vop
(macrolet ((frob (name sc stack-sc format)
`(progn
(define-vop (,name)
(:args (x :scs (,sc) :target y)
(fp :scs (any-reg)
:load-if (not (sc-is y ,sc))))
(:results (y))
(:note _N"float argument move")
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
(:generator ,(case format (:single 2) (:double 3) (:long 4))
(sc-case y
(,sc
(unless (location= x y)
(inst movq y x)))
(,stack-sc
(if (= (tn-offset fp) esp-offset)
(let* ((offset (* (tn-offset y) word-bytes))
(ea (make-ea :dword :base fp :disp offset)))
,@(ecase format
(:single '((inst movss ea x)))
(:double '((inst movsd ea x)))))
(let ((ea (make-ea
:dword :base fp
:disp (- (* (+ (tn-offset y)
,(case format
(:single 1)
(:double 2)
(:long 3)))
vm:word-bytes)))))
,@(ecase format
(:single '((inst movss ea x)))
(:double '((inst movsd ea x))))))))))
(define-move-vop ,name :move-argument
(,sc descriptor-reg) (,sc)))))
(frob move-single-float-argument single-reg single-stack :single)
(frob move-double-float-argument double-reg double-stack :double))
;;;; Complex float move-argument vop
(define-vop (move-complex-single-float-argument)
(:args (x :scs (complex-single-reg) :target y)
(fp :scs (any-reg)
:load-if (not (sc-is y complex-single-reg))))
(:results (y))
(:note _N"complex float argument move")
(:generator 3
(sc-case y
(complex-single-reg
(unless (location= x y)
(inst movaps y x)))
(complex-single-stack
(inst movlps (ea-for-csf-real-stack y fp) x)))))
(define-move-vop move-complex-single-float-argument :move-argument
(complex-single-reg descriptor-reg) (complex-single-reg))
(define-vop (move-complex-double-float-argument)
(:args (x :scs (complex-double-reg) :target y)
(fp :scs (any-reg)
:load-if (not (sc-is y complex-double-reg))))
(:results (y))
(:note _N"complex float argument move")
(:generator 3
(sc-case y
(complex-double-reg
(unless (location= x y)
(inst movapd y x)))
(complex-double-stack
(inst movupd (ea-for-cdf-real-stack y fp) x)))))
(define-move-vop move-complex-double-float-argument :move-argument
(complex-double-reg descriptor-reg) (complex-double-reg))
#+double-double
(define-vop (move-complex-double-double-float-argument)
(:args (x :scs (complex-double-double-reg) :target y)
(fp :scs (any-reg) :load-if (not (sc-is y complex-double-double-reg))))
(:results (y))
(:note _N"complex double-double-float argument move")
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
(:generator 2
(sc-case y
(complex-double-double-reg
(unless (location= x y)
(let ((x-real (complex-double-double-reg-real-hi-tn x))
(y-real (complex-double-double-reg-real-hi-tn y)))
(inst movsd y-real x-real))
(let ((x-real (complex-double-double-reg-real-lo-tn x))
(y-real (complex-double-double-reg-real-lo-tn y)))
(inst movsd y-real x-real))
(let ((x-imag (complex-double-double-reg-imag-hi-tn x))
(y-imag (complex-double-double-reg-imag-hi-tn y)))
(inst movsd y-imag x-imag))
(let ((x-imag (complex-double-double-reg-imag-lo-tn x))
(y-imag (complex-double-double-reg-imag-lo-tn y)))
(inst movsd y-imag x-imag))))
(complex-double-double-stack
(let ((real-tn (complex-double-double-reg-real-hi-tn x)))
(inst movsd (ea-for-cddf-real-hi-stack y fp) real-tn))
(let ((real-tn (complex-double-double-reg-real-lo-tn x)))
(inst movsd (ea-for-cddf-real-lo-stack y fp) real-tn))
(let ((imag-tn (complex-double-double-reg-imag-hi-tn x)))
(inst movsd (ea-for-cddf-imag-hi-stack y fp) imag-tn))
(let ((imag-tn (complex-double-double-reg-imag-lo-tn x)))
(inst movsd (ea-for-cddf-imag-lo-stack y fp) imag-tn))))
))
#+double-double
(define-move-vop move-complex-double-double-float-argument :move-argument
(complex-double-double-reg descriptor-reg) (complex-double-double-reg))
(define-move-vop move-argument :move-argument
(single-reg double-reg #+long-float long-reg
#+double-double double-double-reg
complex-single-reg complex-double-reg #+long-float complex-long-reg
#+double-double complex-double-double-reg)
(descriptor-reg))
;;;; Arithmetic VOPs:
;;; dtc: The floating point arithmetic vops.
;;;
;;; Note: Although these can accept x and y on the stack or pointed to
;;; from a descriptor register, they will work with register loading
;;; without these. Same deal with the result - it need only be a
;;; register. When load-tns are needed they will probably be in ST0
;;; and the code below should be able to correctly handle all cases.
;;;
;;; However it seems to produce better code if all arg. and result
;;; options are used; on the P86 there is no extra cost in using a
;;; memory operand to the FP instructions - not so on the PPro.
;;;
;;; It may also be useful to handle constant args?
;;;
;;; 22-Jul-97: descriptor args lose in some simple cases when
;;; a function result computed in a loop. Then Python insists
;;; on consing the intermediate values! For example
#|
(defun test(a n)
(declare (type (simple-array double-float (*)) a)
(fixnum n))
(let ((sum 0d0))
(declare (type double-float sum))
(dotimes (i n)
(incf sum (* (aref a i)(aref a i))))
sum))
|#
;;; So, disabling descriptor args until this can be fixed elsewhere.
;;;
(define-vop (float-op)
(:args (x) (y))
(:results (r))
(:policy :fast-safe)
(:note _N"inline float arithmetic")
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
(:vop-var vop)
(:save-p :compute-only))
(macrolet ((frob (name sc ptype)
`(define-vop (,name float-op)
(:args (x :scs (,sc) :target r)
(y :scs (,sc)))
(:results (r :scs (,sc)))
(:arg-types ,ptype ,ptype)
(:result-types ,ptype))))
(frob single-float-op single-reg single-float)
(frob double-float-op double-reg double-float))
(macrolet ((generate (movinst opinst commutative)
`(progn
(cond
((location= x r)
(inst ,opinst x y))
((and ,commutative (location= y r))
(inst ,opinst y x))
((not (location= r y))
(inst ,movinst r x)
(inst ,opinst r y))
(t
(inst ,movinst tmp x)
(inst ,opinst tmp y)
(inst ,movinst r tmp)))))
(frob (op sinst sname scost dinst dname dcost commutative)
`(progn
(define-vop (,sname single-float-op)
(:translate ,op)
(:temporary (:sc single-reg) tmp)
(:generator ,scost
(generate movss ,sinst ,commutative)))
(define-vop (,dname double-float-op)
(:translate ,op)
(:temporary (:sc single-reg) tmp)
(:generator ,dcost
(generate movsd ,dinst ,commutative))))))
(frob + addss +/single-float 2 addsd +/double-float 2 t)
(frob - subss -/single-float 2 subsd -/double-float 2 nil)
(frob * mulss */single-float 4 mulsd */double-float 5 t)
(frob / divss //single-float 12 divsd //double-float 19 nil))
(define-vop (fsqrt)
(:args (x :scs (double-reg)))
(:results (y :scs (double-reg)))
(:translate %sqrt)
(:policy :fast-safe)
(:arg-types double-float)
(:result-types double-float)
(:note _N"inline float arithmetic")
(:vop-var vop)
(:save-p :compute-only)
(:generator 1
(note-this-location vop :internal-error)
(inst sqrtsd y x)))
(macrolet ((frob ((name translate mov sc type) &body body)
`(define-vop (,name)
(:args (x :scs (,sc)))
(:results (y :scs (,sc)))
(:translate ,translate)
(:policy :fast-safe)
(:arg-types ,type)
(:result-types ,type)
(:temporary (:sc ,sc) tmp)
(:note _N"inline float arithmetic")
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
(:vop-var vop)
(:save-p :compute-only)
(:generator 1
(note-this-location vop :internal-error)
(inst pcmpeqd tmp tmp) ; all 1's
;; we should be able to do this better. what we
;; really would like to do is use the target as the
;; temp whenever it's not also the source
(unless (location= x y)
(inst ,mov y x))
,@body))))
(frob (%negate/double-float %negate movsd double-reg double-float)
(inst psllq tmp 63) ; tmp = #x8000000000000000
(inst xorpd y tmp))
(frob (%negate/single-float %negate movss single-reg single-float)
(inst pslld tmp 31) ; tmp = #x80000000
(inst xorps y tmp))
(frob (abs/double-float abs movsd double-reg double-float)
(inst psrlq tmp 1) ; tmp = #x7fffffffffffffff
(inst andpd y tmp))
(frob (abs/single-float abs movss single-reg single-float)
(inst psrld tmp 1) ; tmp = #x7fffffff
(inst andps y tmp)))
;;;; Comparison:
#+long-float
(deftransform eql ((x y) (long-float long-float))
`(and (= (long-float-low-bits x) (long-float-low-bits y))
(= (long-float-high-bits x) (long-float-high-bits y))
(= (long-float-exp-bits x) (long-float-exp-bits y))))
#+double-double
(deftransform eql ((x y) (double-double-float double-double-float))
'(and (eql (double-double-hi x) (double-double-hi y))
(eql (double-double-lo x) (double-double-lo y))))
;;;; comparison
(define-vop (float-compare)
(:conditional)
(:info target not-p)
(:policy :fast-safe)
(:vop-var vop)
(:save-p :compute-only)
(:note _N"inline float comparison"))
;;; comiss and comisd can cope with one or other arg in memory: we
;;; could (should, indeed) extend these to cope with descriptor args
;;; and stack args
(define-vop (single-float-compare float-compare)
(:args (x :scs (single-reg)) (y :scs (single-reg)))
(:conditional)
(:arg-types single-float single-float))
(define-vop (double-float-compare float-compare)
(:args (x :scs (double-reg)) (y :scs (double-reg)))
(:conditional)
(:arg-types double-float double-float))
(define-vop (=/single-float single-float-compare)
(:translate =)
(:info target not-p)
(:vop-var vop)
(:generator 3
(note-this-location vop :internal-error)
;; if PF&CF, there was a NaN involved => not equal
;; otherwise, ZF => equal
(cond (not-p
(inst jmp :p target)
(inst jmp :ne target))
(t
(let ((not-lab (gen-label)))
(inst jmp :p not-lab)
(inst jmp :e target)
(emit-label not-lab))))))
(define-vop (=/double-float double-float-compare)
(:translate =)
(:info target not-p)
(:vop-var vop)
(:generator 3
(note-this-location vop :internal-error)
(cond (not-p
(inst jmp :p target)
(inst jmp :ne target))
(t
(let ((not-lab (gen-label)))
(inst jmp :p not-lab)
(inst jmp :e target)
(emit-label not-lab))))))
(:translate <)
(:info target not-p)
(:generator 3
(inst comisd x y)
(cond (not-p
(inst jmp :p target)
(inst jmp :nc target))
(t
(let ((not-lab (gen-label)))
(inst jmp :p not-lab)
(inst jmp :c target)
(emit-label not-lab))))))
(:translate <)
(:info target not-p)
(:generator 3
(inst comiss x y)
(cond (not-p
(inst jmp :p target)
(inst jmp :nc target))
(t
(let ((not-lab (gen-label)))
(inst jmp :p not-lab)
(inst jmp :c target)
(emit-label not-lab))))))
(:translate >)
(:info target not-p)
(:generator 3
(inst comisd x y)
(cond (not-p
(inst jmp :p target)
(inst jmp :na target))
(t
(let ((not-lab (gen-label)))
(inst jmp :p not-lab)
(inst jmp :a target)
(emit-label not-lab))))))
(:translate >)
(:info target not-p)
(:generator 3
(inst comiss x y)
(cond (not-p
(inst jmp :p target)
(inst jmp :na target))
(t
(let ((not-lab (gen-label)))
(inst jmp :p not-lab)
(inst jmp :a target)
(emit-label not-lab))))))
;;;; Conversion:
(macrolet ((frob (name translate inst to-sc to-type)
`(define-vop (,name)
(:args (x :scs (signed-stack signed-reg) :target temp))
(:temporary (:sc signed-stack) temp)
(:results (y :scs (,to-sc)))
(:arg-types signed-num)
(:result-types ,to-type)
(:policy :fast-safe)
(:note _N"inline float coercion")
(:translate ,translate)
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(sc-case x
(signed-reg
(inst mov temp x)
(note-this-location vop :internal-error)
(inst ,inst y temp))
(signed-stack
(note-this-location vop :internal-error)
(inst ,inst y x)))))))
(frob %single-float/signed %single-float cvtsi2ss single-reg single-float)
(frob %double-float/signed %double-float cvtsi2sd double-reg double-float))
(macrolet ((frob (name translate inst from-sc from-type to-sc to-type)
`(define-vop (,name)
(:args (x :scs (,from-sc) :target y))
(:results (y :scs (,to-sc)))
(:arg-types ,from-type)
(:result-types ,to-type)
(:policy :fast-safe)