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: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/compiler/x86/float.lisp,v 1.3 1997/03/26 20:29:14 dtc Exp $")
;;;
;;; **********************************************************************
;;;
;;; This file contains floating point support for the x86.
;;;
;;; Written by William Lott.
;;;
;;; Debugged by Paul F. Werkowski Spring/Summer 1995.
;;; Re-written and enhanced by Douglas Crosher, 1996, 1997.
(in-package :x86)
(defmacro 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))
(defmacro ea-for-xf-stack(tn kind)
`(make-ea
:dword :base ebp-tn
:disp (- (* (+ (tn-offset ,tn) (case ,kind (:single 1) (:double 2)))
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))
;;; Abstract out the copying of a FP register to the FP stack top, and
;;; provide two alternatives for its implementation. Note: it's not
;;; necessary to distinguish between a single or double register move
;;; here.
;;;
;;; Using a Pop then load.
(defmacro copy-fp-reg-to-fr0 (reg)
`(progn
(assert (not (zerop (tn-offset ,reg))))
(inst fstp fr0-tn)
(inst fld (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (1- (tn-offset ,reg))))))
;;;
;;; Using Fxch then Fst to restore the original reg contents.
#+nil
(defmacro copy-fp-reg-to-fr0 (reg)
`(progn
(assert (not (zerop (tn-offset ,reg))))
(inst fxch ,reg)
(inst fst ,reg)))
;;;; Move functions:
;;; x is source, y is destination
(define-move-function (load-single 6) (vop x y)
((single-stack) (single-reg))
(with-empty-tn@fp-top(y)
(inst fld (ea-for-sf-stack x))))
(define-move-function (store-single 6) (vop x y)
((single-reg) (single-stack))
(cond ((zerop (tn-offset x))
(inst fst (ea-for-sf-stack y)))
(t
(inst fxch x)
(inst fst (ea-for-sf-stack y))
;; This may not be necessary as ST0 is likely invalid now.
(inst fxch x))))
(define-move-function (load-double 6) (vop x y)
((double-stack) (double-reg))
(with-empty-tn@fp-top(y)
(inst fldd (ea-for-df-stack x))))
(define-move-function (store-double 6) (vop x y)
((double-reg) (double-stack))
(cond ((zerop (tn-offset x))
(inst fstd (ea-for-df-stack y)))
(t
(inst fxch x)
(inst fstd (ea-for-df-stack y))
;; This may not be necessary as ST0 is likely invalid now.
(inst fxch x))))
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
;;; The i387 has instructions to load some useful constants.
;;; This doesn't save much time but might cut down on memory
;;; access and reduce the size of the constant vector (CV).
;;; Intel claims they are stored in a more precise form on chip.
;;; Anyhow, might as well use the feature. It can be turned
;;; off by hacking the "immediate-constant-sc" in vm.lisp.
(define-move-function (load-fp-constant 6) (vop x y)
((fp-single-constant)(single-reg)
(fp-double-constant)(double-reg))
(let ((value (c::constant-value (c::tn-leaf x))))
(with-empty-tn@fp-top(y)
(cond ((zerop value)
(inst fldz))
((or (= value 1s0)(= value 1d0))
(inst fld1))
((= value #.pi)
(inst fldpi))
((= value #.i387-l2t)
(inst fldl2t))
((= value #.i387-l2e)
(inst fldl2e))
((= value #.i387-lg2)
(inst fldlg2))
((= value #.i387-ln2)
(inst fldln2))
(t (warn "Ignoring bogus i387 Constant ~a" value))))))
;;;; Move VOPs:
;;;
;;; Float register to register moves.
;;;
(define-vop (single-move)
(:args (x :scs (single-reg) :target y
:load-if (not (and (sc-is x single-stack)
(sc-is y single-stack)
(location= x y)))))
(:results (y :scs (single-reg)
:load-if (not (and (sc-is x single-stack)
(sc-is y single-stack)
(location= x y)))))
(:note "float move")
(:generator 0
(unless (location= x y)
(cond ((zerop (tn-offset y))
(copy-fp-reg-to-fr0 x))
((zerop (tn-offset x))
(inst fst y))
(t
(inst fxch x)
(inst fst y)
(inst fxch x))))))
(define-move-vop single-move :move (single-reg) (single-reg))
(define-vop (double-move)
(:args (x :scs (double-reg) :target y
:load-if (not (and (sc-is x double-stack)
(sc-is y double-stack)
(location= x y)))))
(:results (y :scs (double-reg)
:load-if (not (and (sc-is x double-stack)
(sc-is y double-stack)
(location= x y)))))
(:note "float move")
(:generator 0
(unless (location= x y)
(cond ((zerop (tn-offset y))
(copy-fp-reg-to-fr0 x))
((zerop (tn-offset x))
(inst fstd y))
(t
(inst fxch x)
(inst fstd y)
(inst fxch x))))))
(define-move-vop double-move :move (double-reg) (double-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
:load-if (not (sc-is x fp-single-constant))))
(:results (y :scs (descriptor-reg)))
(:temporary (:sc dword-reg) ndescr)
(:note "float to pointer coercion")
(:generator 13
(if (sc-is x fp-single-constant)
(ecase (c::constant-value (c::tn-leaf x))
(0s0 (load-symbol-value y *fp-constant-0s0*))
(1s0 (load-symbol-value y *fp-constant-1s0*)))
(with-fixed-allocation (y ndescr vm:single-float-type
vm:single-float-size)
(with-tn@fp-top(x)
(inst fst (make-ea :dword :base y
:disp (- (* vm:single-float-value-slot
vm:word-bytes)
vm:other-pointer-type))))))))
(define-move-vop move-from-single :move
(single-reg) (descriptor-reg))
(define-vop (move-from-double)
(:args (x :scs (double-reg) :to :save
:load-if (not (sc-is x fp-double-constant))))
(:results (y :scs (descriptor-reg)))
(:temporary (:sc dword-reg) ndescr)
(:note "float to pointer coercion")
(:generator 13
(if (sc-is x fp-double-constant)
(ecase (c::constant-value (c::tn-leaf x))
(0d0 (load-symbol-value y *fp-constant-0d0*))
(1d0 (load-symbol-value y *fp-constant-1d0*)))
(with-fixed-allocation (y ndescr vm:double-float-type
vm:double-float-size)
(with-tn@fp-top(x)
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
(inst fstd (make-ea :dword :base y
:disp (- (* vm:double-float-value-slot
vm:word-bytes)
vm:other-pointer-type))))))))
(define-move-vop move-from-double :move
(double-reg) (descriptor-reg))
;;;
;;; Move from a descriptor to a float register
;;;
(macrolet ((frob (name sc double-p)
`(progn
(define-vop (,name)
(:args (x :scs (descriptor-reg)))
(:results (y :scs (,sc)))
(:note "pointer to float coercion")
(:generator 2
(with-empty-tn@fp-top(y)
,@(if double-p
'((inst fldd (ea-for-df-desc x)))
'((inst fld (ea-for-sf-desc x)))))))
(define-move-vop ,name :move (descriptor-reg) (,sc)))))
(frob move-to-single single-reg nil)
(frob move-to-double double-reg t))
;;;
;;; The move argument vops.
;;;
;;; Note these are also used to stuffs 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 double-p)
`(progn
(define-vop (,name)
(:args (x :scs (,sc) :target y)
(fp :scs (any-reg)
:load-if (not (sc-is y ,sc))))
(:results (y))
(:note "float argument move")
(:generator ,(if double-p 3 2)
(sc-case y
(,sc
(unless (location= x y)
(cond ((zerop (tn-offset y))
(copy-fp-reg-to-fr0 x))
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
304
305
306
307
308
309
310
311
312
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
((zerop (tn-offset x))
(inst fstd y))
(t
(inst fxch x)
(inst fstd y)
(inst fxch x)))))
(,stack-sc
(if (= (tn-offset fp) esp-offset)
(let* ((offset (* (tn-offset y) word-bytes))
(ea (make-ea :dword :base fp :disp offset)))
(with-tn@fp-top(x)
,@(if double-p
'((inst fstd ea))
'((inst fst ea)))))
(let ((ea (make-ea
:dword :base fp
:disp (- (* (+ (tn-offset y)
,(if double-p 2 1))
vm:word-bytes)))))
(with-tn@fp-top(x)
,@(if double-p
'((inst fstd ea))
'((inst fst ea))))))))))
(define-move-vop ,name :move-argument
(,sc descriptor-reg) (,sc)))))
(frob move-single-float-argument single-reg single-stack nil)
(frob move-double-float-argument double-reg double-stack t))
;;;
(define-move-vop move-argument :move-argument
(single-reg double-reg) (descriptor-reg any-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?
(macrolet
((frob (op fop-sti fopr-sti
fop fopr sname scost
fopd foprd dname dcost)
`(progn
(define-vop (,sname)
(:translate ,op)
(:args (x :scs (single-reg single-stack descriptor-reg)
:to :eval)
(y :scs (single-reg single-stack descriptor-reg)
:to :eval))
(:temporary (:sc single-reg :offset fr0-offset
:from :eval :to :result) fr0)
(:results (r :scs (single-reg single-stack)))
(:arg-types single-float single-float)
(:result-types single-float)
(:policy :fast-safe)
(:note "inline float arithmetic")
(:vop-var vop)
(:save-p :compute-only)
(:node-var node)
(:generator ,scost
;; Handle a few special cases
(cond
;; x, y, and r are the same register.
((and (sc-is x single-reg) (location= x r) (location= y r))
(cond ((zerop (tn-offset r))
(inst ,fop fr0))
(t
(inst fxch r)
(inst ,fop fr0)
;; XX the source register will not be valid.
(note-next-instruction vop :internal-error)
(inst fxch r))))
;; x and r are the same register.
((and (sc-is x single-reg) (location= x r))
(cond ((zerop (tn-offset r))
(sc-case y
(single-reg
;; ST(0) = ST(0) op ST(y)
(inst ,fop y))
(single-stack
;; ST(0) = ST(0) op Mem
(inst ,fop (ea-for-sf-stack y)))
(descriptor-reg
(inst ,fop (ea-for-sf-desc y)))))
(t
(single-reg
(unless (zerop (tn-offset y))
(copy-fp-reg-to-fr0 y)))
((single-stack descriptor-reg)
(inst fstp fr0)
(if (sc-is y single-stack)
(inst fld (ea-for-sf-stack y))
(inst fld (ea-for-sf-desc y)))))
;; ST(i) = ST(i) op ST0
(inst ,fop-sti r)))
(when (policy node (or (= debug 3) (> safety speed)))
(note-next-instruction vop :internal-error)
(inst wait)))
;; y and r are the same register.
((and (sc-is y single-reg) (location= y r))
(cond ((zerop (tn-offset r))
(sc-case x
(single-reg
;; ST(0) = ST(x) op ST(0)
(inst ,fopr x))
(single-stack
;; ST(0) = Mem op ST(0)
(inst ,fopr (ea-for-sf-stack x)))
(descriptor-reg
(t
;; x to ST0
(sc-case x
(single-reg
(unless (zerop (tn-offset x))
(copy-fp-reg-to-fr0 x)))
((single-stack descriptor-reg)
(inst fstp fr0)
(if (sc-is x single-stack)
(inst fld (ea-for-sf-stack x))
(inst fld (ea-for-sf-desc x)))))
;; ST(i) = ST(0) op ST(i)
(inst ,fopr-sti r)))
(when (policy node (or (= debug 3) (> safety speed)))
(note-next-instruction vop :internal-error)
(inst wait)))
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
;; The default case
(t
;; Get the result to ST0.
;; Special handling is needed if x or y are in ST0, and
;; simpler code is generated.
(cond
;; x is in ST0
((and (sc-is x single-reg) (zerop (tn-offset x)))
;; ST0 = ST0 op y
(sc-case y
(single-reg
(inst ,fop y))
(single-stack
(inst ,fop (ea-for-sf-stack y)))
(descriptor-reg
(inst ,fop (ea-for-sf-desc y)))))
;; y is in ST0
((and (sc-is y single-reg) (zerop (tn-offset y)))
;; ST0 = x op ST0
(sc-case x
(single-reg
(inst ,fopr x))
(single-stack
(inst ,fopr (ea-for-sf-stack x)))
(descriptor-reg
(inst ,fopr (ea-for-sf-desc x)))))
(t
;; x to ST0
(sc-case x
(single-reg
(copy-fp-reg-to-fr0 x))
(single-stack
(inst fld (ea-for-sf-stack x)))
(descriptor-reg
(inst fstp fr0)
(inst fld (ea-for-sf-desc x))))
;; ST0 = ST0 op y
(sc-case y
(single-reg
(inst ,fop y))
(single-stack
(inst ,fop (ea-for-sf-stack y)))
(descriptor-reg
(inst ,fop (ea-for-sf-desc y))))))
(note-next-instruction vop :internal-error)
;; Finally save the result
(sc-case r
(single-reg
(cond ((zerop (tn-offset r))
(when (policy node (or (= debug 3) (> safety speed)))
(inst wait)))
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
(t
(inst fst r))))
(single-stack
(inst fst (ea-for-sf-stack r))))))))
(define-vop (,dname)
(:translate ,op)
(:args (x :scs (double-reg double-stack descriptor-reg)
:to :eval)
(y :scs (double-reg double-stack descriptor-reg)
:to :eval))
(:temporary (:sc double-reg :offset fr0-offset
:from :eval :to :result) fr0)
(:results (r :scs (double-reg double-stack)))
(:arg-types double-float double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline float arithmetic")
(:vop-var vop)
(:save-p :compute-only)
(:node-var node)
(:generator ,dcost
;; Handle a few special cases
(cond
;; x, y, and r are the same register.
((and (sc-is x double-reg) (location= x r) (location= y r))
(cond ((zerop (tn-offset r))
(inst ,fop fr0))
(t
(inst fxch x)
(inst ,fopd fr0)
;; XX the source register will not be valid.
(note-next-instruction vop :internal-error)
(inst fxch r))))
;; x and r are the same register.
((and (sc-is x double-reg) (location= x r))
(cond ((zerop (tn-offset r))
(sc-case y
(double-reg
;; ST(0) = ST(0) op ST(y)
(inst ,fopd y))
(double-stack
;; ST(0) = ST(0) op Mem
(inst ,fopd (ea-for-df-stack y)))
(descriptor-reg
(t
;; y to ST0
(sc-case y
(double-reg
(unless (zerop (tn-offset y))
(copy-fp-reg-to-fr0 y)))
((double-stack descriptor-reg)
(inst fstp fr0)
(if (sc-is y double-stack)
(inst fldd (ea-for-df-stack y))
(inst fldd (ea-for-df-desc y)))))
;; ST(i) = ST(i) op ST0
(inst ,fop-sti r)))
(when (policy node (or (= debug 3) (> safety speed)))
(note-next-instruction vop :internal-error)
(inst wait)))
;; y and r are the same register.
((and (sc-is y double-reg) (location= y r))
(cond ((zerop (tn-offset r))
(sc-case x
(double-reg
;; ST(0) = ST(x) op ST(0)
(inst ,foprd x))
(double-stack
;; ST(0) = Mem op ST(0)
(inst ,foprd (ea-for-df-stack x)))
(descriptor-reg
(t
;; x to ST0
(sc-case x
(double-reg
(unless (zerop (tn-offset x))
(copy-fp-reg-to-fr0 x)))
((double-stack descriptor-reg)
(inst fstp fr0)
(if (sc-is x double-stack)
(inst fldd (ea-for-df-stack x))
(inst fldd (ea-for-df-desc x)))))
;; ST(i) = ST(0) op ST(i)
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
(inst ,fopr-sti r)))
(when (policy node (or (= debug 3) (> safety speed)))
(note-next-instruction vop :internal-error)
(inst wait)))
;; The default case
(t
;; Get the result to ST0.
;; Special handling is needed if x or y are in ST0, and
;; simpler code is generated.
(cond
;; x is in ST0
((and (sc-is x double-reg) (zerop (tn-offset x)))
;; ST0 = ST0 op y
(sc-case y
(double-reg
(inst ,fopd y))
(double-stack
(inst ,fopd (ea-for-df-stack y)))
(descriptor-reg
(inst ,fopd (ea-for-df-desc y)))))
;; y is in ST0
((and (sc-is y double-reg) (zerop (tn-offset y)))
;; ST0 = x op ST0
(sc-case x
(double-reg
(inst ,foprd x))
(double-stack
(inst ,foprd (ea-for-df-stack x)))
(descriptor-reg
(inst ,foprd (ea-for-df-desc x)))))
(t
;; x to ST0
(sc-case x
(double-reg
(copy-fp-reg-to-fr0 x))
(double-stack
(inst fstp fr0)
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fldd (ea-for-df-desc x))))
;; ST0 = ST0 op y
(sc-case y
(double-reg
(inst ,fopd y))
(double-stack
(inst ,fopd (ea-for-df-stack y)))
(descriptor-reg
(inst ,fopd (ea-for-df-desc y))))))
(note-next-instruction vop :internal-error)
;; Finally save the result
(sc-case r
(double-reg
(cond ((zerop (tn-offset r))
(when (policy node (or (= debug 3) (> safety speed)))
(inst wait)))
(t
(inst fst r))))
(double-stack
(inst fstd (ea-for-df-stack r)))))))))))
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
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
(frob + fadd-sti fadd-sti
fadd fadd +/single-float 2
faddd faddd +/double-float 2)
(frob - fsub-sti fsubr-sti
fsub fsubr -/single-float 2
fsubd fsubrd -/double-float 2)
(frob * fmul-sti fmul-sti
fmul fmul */single-float 3
fmuld fmuld */double-float 3)
(frob / fdiv-sti fdivr-sti
fdiv fdivr //single-float 12
fdivd fdivrd //double-float 12))
(macrolet ((frob (name inst translate sc type)
`(define-vop (,name)
(:args (x :scs (,sc) :target fr0))
(:results (y :scs (,sc)))
(:translate ,translate)
(:policy :fast-safe)
(:arg-types ,type)
(:result-types ,type)
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
(:ignore fr0)
(:note "inline float arithmetic")
(:vop-var vop)
(:save-p :compute-only)
(:generator 1
(note-this-location vop :internal-error)
(unless (zerop (tn-offset x))
(inst fxch x) ; x to top of stack
(unless (location= x y)
(inst fst x))) ; maybe save it
(inst ,inst) ; clobber st0
(unless (zerop (tn-offset y))
(inst fst y))))))
(frob abs/single-float fabs abs single-reg single-float)
(frob abs/double-float fabs abs double-reg double-float)
(frob %negate/single-float fchs %negate single-reg single-float)
(frob %negate/double-float fchs %negate double-reg double-float))
;;;; Comparison:
(define-vop (=/float)
(:args (x) (y))
(:temporary (:sc word-reg :offset eax-offset :from :eval) temp)
(:conditional)
(:info target not-p)
(:policy :fast-safe)
(:vop-var vop)
(:save-p :compute-only)
(:note "inline float comparison")
(:ignore temp)
(:generator 3
(note-this-location vop :internal-error)
(cond
;; x is in ST0; y is in any reg.
((zerop (tn-offset x))
(inst fucom y))
;; y is in ST0; x is in another reg.
((zerop (tn-offset y))
(inst fucom x))
;; x and y are the same register, not ST0
((location= x y)
(inst fxch x)
(inst fucom fr0-tn)
(inst fxch x))
;; x and y are different registers, neither ST0.
(t
(inst fxch x)
(inst fucom y)
(inst fxch x)))
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x45) ; C3 C2 C0
(inst cmp ah-tn #x40)
(inst jmp (if not-p :ne :e) target)))
(define-vop (=/single-float =/float)
(:translate =)
(:args (x :scs (single-reg))
(y :scs (single-reg)))
(:arg-types single-float single-float))
(define-vop (=/double-float =/float)
(:translate =)
(:args (x :scs (double-reg))
(y :scs (double-reg)))
(:arg-types double-float double-float))
(define-vop (<single-float)
(:translate <)
(:args (x :scs (single-reg single-stack descriptor-reg))
(y :scs (single-reg single-stack descriptor-reg)))
(:arg-types single-float single-float)
(:temporary (:sc single-reg :offset fr0-offset :from :eval) fr0)
(:temporary (:sc word-reg :offset eax-offset :from :eval) temp)
(:conditional)
(:info target not-p)
(:policy :fast-safe)
(:vop-var vop)
(:save-p :compute-only)
(:note "inline float comparison")
(:ignore temp)
(:generator 3
;; Handle a few special cases
(cond
;; y is ST0.
((and (sc-is y single-reg) (zerop (tn-offset y)))
(sc-case x
(single-reg
(inst fcom x))
((single-stack descriptor-reg)
(if (sc-is x single-stack)
(inst fcom (ea-for-sf-stack x))
(inst fcom (ea-for-sf-desc x)))))
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x45))
;; General case when y is not in ST0.
(t
;; x to ST0
(sc-case x
(single-reg
(unless (zerop (tn-offset x))
(copy-fp-reg-to-fr0 x)))
742
743
744
745
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
((single-stack descriptor-reg)
(inst fstp fr0)
(if (sc-is x single-stack)
(inst fld (ea-for-sf-stack x))
(inst fld (ea-for-sf-desc x)))))
(sc-case y
(single-reg
(inst fcom y))
((single-stack descriptor-reg)
(if (sc-is y single-stack)
(inst fcom (ea-for-sf-stack y))
(inst fcom (ea-for-sf-desc y)))))
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x45) ; C3 C2 C0
(inst cmp ah-tn #x01)))
(inst jmp (if not-p :ne :e) target)))
(define-vop (<double-float)
(:translate <)
(:args (x :scs (double-reg double-stack descriptor-reg))
(y :scs (double-reg double-stack descriptor-reg)))
(:arg-types double-float double-float)
(:temporary (:sc double-reg :offset fr0-offset :from :eval) fr0)
(:temporary (:sc word-reg :offset eax-offset :from :eval) temp)
(:conditional)
(:info target not-p)
(:policy :fast-safe)
(:vop-var vop)
(:save-p :compute-only)
(:note "inline float comparison")
(:ignore temp)
(:generator 3
;; Handle a few special cases
(cond
;; y is ST0.
((and (sc-is y double-reg) (zerop (tn-offset y)))
(sc-case x
(double-reg
(inst fcomd x))
((double-stack descriptor-reg)
(if (sc-is x double-stack)
(inst fcomd (ea-for-df-stack x))
(inst fcomd (ea-for-df-desc x)))))
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x45))
;; General case when y is not in ST0.
(t
;; x to ST0
(sc-case x
(double-reg
(unless (zerop (tn-offset x))
(copy-fp-reg-to-fr0 x)))
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
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
((double-stack descriptor-reg)
(inst fstp fr0)
(if (sc-is x double-stack)
(inst fldd (ea-for-df-stack x))
(inst fldd (ea-for-df-desc x)))))
(sc-case y
(double-reg
(inst fcomd y))
((double-stack descriptor-reg)
(if (sc-is y double-stack)
(inst fcomd (ea-for-df-stack y))
(inst fcomd (ea-for-df-desc y)))))
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x45) ; C3 C2 C0
(inst cmp ah-tn #x01)))
(inst jmp (if not-p :ne :e) target)))
(define-vop (>single-float)
(:translate >)
(:args (x :scs (single-reg single-stack descriptor-reg))
(y :scs (single-reg single-stack descriptor-reg)))
(:arg-types single-float single-float)
(:temporary (:sc single-reg :offset fr0-offset :from :eval) fr0)
(:temporary (:sc word-reg :offset eax-offset :from :eval) temp)
(:conditional)
(:info target not-p)
(:policy :fast-safe)
(:vop-var vop)
(:save-p :compute-only)
(:note "inline float comparison")
(:ignore temp)
(:generator 3
;; Handle a few special cases
(cond
;; y is ST0.
((and (sc-is y single-reg) (zerop (tn-offset y)))
(sc-case x
(single-reg
(inst fcom x))
((single-stack descriptor-reg)
(if (sc-is x single-stack)
(inst fcom (ea-for-sf-stack x))
(inst fcom (ea-for-sf-desc x)))))
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x45)
(inst cmp ah-tn #x01))
;; General case when y is not in ST0.
(t
;; x to ST0
(sc-case x
(single-reg
(unless (zerop (tn-offset x))
(copy-fp-reg-to-fr0 x)))
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
((single-stack descriptor-reg)
(inst fstp fr0)
(if (sc-is x single-stack)
(inst fld (ea-for-sf-stack x))
(inst fld (ea-for-sf-desc x)))))
(sc-case y
(single-reg
(inst fcom y))
((single-stack descriptor-reg)
(if (sc-is y single-stack)
(inst fcom (ea-for-sf-stack y))
(inst fcom (ea-for-sf-desc y)))))
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x45)))
(inst jmp (if not-p :ne :e) target)))
(define-vop (>double-float)
(:translate >)
(:args (x :scs (double-reg double-stack descriptor-reg))
(y :scs (double-reg double-stack descriptor-reg)))
(:arg-types double-float double-float)
(:temporary (:sc double-reg :offset fr0-offset :from :eval) fr0)
(:temporary (:sc word-reg :offset eax-offset :from :eval) temp)
(:conditional)
(:info target not-p)
(:policy :fast-safe)
(:vop-var vop)
(:save-p :compute-only)
(:note "inline float comparison")
(:ignore temp)
(:generator 3
;; Handle a few special cases
(cond
;; y is ST0.
((and (sc-is y double-reg) (zerop (tn-offset y)))
(sc-case x
(double-reg
(inst fcomd x))
((double-stack descriptor-reg)
(if (sc-is x double-stack)
(inst fcomd (ea-for-df-stack x))
(inst fcomd (ea-for-df-desc x)))))
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x45)
(inst cmp ah-tn #x01))
;; General case when y is not in ST0.
(t
;; x to ST0
(sc-case x
(double-reg
(unless (zerop (tn-offset x))
(copy-fp-reg-to-fr0 x)))
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
((double-stack descriptor-reg)
(inst fstp fr0)
(if (sc-is x double-stack)
(inst fldd (ea-for-df-stack x))
(inst fldd (ea-for-df-desc x)))))
(sc-case y
(double-reg
(inst fcomd y))
((double-stack descriptor-reg)
(if (sc-is y double-stack)
(inst fcomd (ea-for-df-stack y))
(inst fcomd (ea-for-df-desc y)))))
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x45)))
(inst jmp (if not-p :ne :e) target)))
;;; Comparisons with 0 can use the FTST instruction.
(define-vop (float-test)
(:args (x))
(:temporary (:sc word-reg :offset eax-offset :from :eval) temp)
(:conditional)
(:info target not-p y)
(:variant-vars code)
(:policy :fast-safe)
(:vop-var vop)
(:save-p :compute-only)
(:note "inline float comparison")
(:ignore temp y)
(:generator 2
(note-this-location vop :internal-error)
(cond
;; x is in ST0
((zerop (tn-offset x))
(inst ftst))
;; x not ST0
(t
(inst fxch x)
(inst ftst)
(inst fxch x)))
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x45) ; C3 C2 C0
(unless (zerop code)
(inst cmp ah-tn code))
(inst jmp (if not-p :ne :e) target)))
(define-vop (=0/single-float float-test)
(:translate =)
(:args (x :scs (single-reg)))
(:arg-types single-float (:constant (single-float 0s0 0s0)))
(:variant #x40))
(define-vop (=0/double-float float-test)
(:translate =)
(:args (x :scs (double-reg)))
(:arg-types double-float (:constant (double-float 0d0 0d0)))
(:variant #x40))
(define-vop (<0/single-float float-test)
(:translate <)
(:args (x :scs (single-reg)))
(:arg-types single-float (:constant (single-float 0s0 0s0)))
(:variant #x01))
(define-vop (<0/double-float float-test)
(:translate <)
(:args (x :scs (double-reg)))
(:arg-types double-float (:constant (double-float 0d0 0d0)))
(:variant #x01))
(define-vop (>0/single-float float-test)
(:translate >)
(:args (x :scs (single-reg)))
(:arg-types single-float (:constant (single-float 0s0 0s0)))
(:variant #x00))
(define-vop (>0/double-float float-test)
(:translate >)
(:args (x :scs (double-reg)))
(:arg-types double-float (:constant (double-float 0d0 0d0)))
(:variant #x00))
;;;; Conversion:
(macrolet ((frob (name translate 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 "inline float coercion")
(:translate ,translate)
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(sc-case x
(signed-reg
(inst mov temp x)
(with-empty-tn@fp-top(y)
(note-this-location vop :internal-error)
(inst fild temp)))
(signed-stack
(with-empty-tn@fp-top(y)
(note-this-location vop :internal-error)
(inst fild x))))))))
(frob %single-float/signed %single-float single-reg single-float)
(frob %double-float/signed %double-float double-reg double-float))
(macrolet ((frob (name translate to-sc to-type)
`(define-vop (,name)
(:args (x :scs (unsigned-reg)))
(:results (y :scs (,to-sc)))
(:arg-types unsigned-num)
(:result-types ,to-type)
(:policy :fast-safe)
(:note "inline float coercion")
(:translate ,translate)
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(inst push 0)
(inst push x)
(with-empty-tn@fp-top(y)
(note-this-location vop :internal-error)
(inst fildl (make-ea :dword :base esp-tn)))
(inst add esp-tn 8)))))
(frob %single-float/unsigned %single-float single-reg single-float)
(frob %double-float/unsigned %double-float double-reg double-float))
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
;;; These should be no-ops but the compiler might want to move
;;; some things around
(macrolet ((frob (name translate 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)
(:note "inline float coercion")
(:translate ,translate)
(:vop-var vop)
(:save-p :compute-only)
(:generator 2
(note-this-location vop :internal-error)
(unless (location= x y)
(cond
((zerop (tn-offset x))
;; x is in ST0, y is in another reg. not ST0
(inst fst y))
((zerop (tn-offset y))
;; y is in ST0, x is in another reg. not ST0
(copy-fp-reg-to-fr0 x))
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
(t
;; Neither x or y are in ST0, and they are not in
;; the same reg.
(inst fxch x)
(inst fst y)
(inst fxch x))))))))
(frob %single-float/double-float %single-float double-reg
double-float single-reg single-float)
(frob %double-float/single-float %double-float single-reg single-float
double-reg double-float))
(macrolet ((frob (trans from-sc from-type round-p)
`(define-vop (,(symbolicate trans "/" from-type))
(:args (x :scs (,from-sc)))
(:temporary (:sc signed-stack) stack-temp)
,@(unless round-p
'((:temporary (:sc unsigned-stack) scw)
(:temporary (:sc any-reg) rcw)))
(:results (y :scs (signed-reg)))
(:arg-types ,from-type)
(:result-types signed-num)
(:translate ,trans)
(:policy :fast-safe)
(:note "inline float truncate")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
(inst wait) ; Catch any pending FPE exceptions
;; normal mode (for now) is "round to best"
(with-tn@fp-top(x)
,@(unless round-p
'((inst fnstcw scw) ; save current control word
(move rcw scw) ; into 16-bit register
(inst or rcw (ash #b11 10)) ; CHOP
(move stack-temp rcw)
(inst fldcw stack-temp)))
(sc-case y
(signed-stack
(inst fist y))
(signed-reg
(inst fist stack-temp)
(inst mov y stack-temp)))
,@(unless round-p
'((inst fldcw scw))))))))
(frob %unary-truncate single-reg single-float nil)
(frob %unary-truncate double-reg double-float nil)
#-sun4
(frob %unary-round single-reg single-float t)
#-sun4
(frob %unary-round double-reg double-float t))
#+sun4
(deftransform %unary-round ((x) (float) (signed-byte 32))
'(let* ((trunc (truly-the (signed-byte 32) (%unary-truncate x)))
(extra (- x trunc))
(absx (abs extra))
(one-half (float 1/2 x)))
(if (if (oddp trunc)
(>= absx one-half)
(> absx one-half))
(truly-the (signed-byte 32) (%unary-truncate (+ x extra)))
trunc)))
(define-vop (make-single-float)
(:args (bits :scs (signed-reg) :target res
:load-if (not (or (and (sc-is bits signed-stack)
(sc-is res single-reg))
(and (sc-is bits signed-stack)
(sc-is res single-stack)
(location= bits res))))))
(:results (res :scs (single-reg single-stack)))
(:temporary (:sc signed-stack) stack-temp)
(:arg-types signed-num)
(:result-types single-float)
(:translate make-single-float)
(:policy :fast-safe)
(:vop-var vop)
(:generator 4
(sc-case res
(single-stack
(sc-case bits
(signed-reg
(inst mov res bits))
(signed-stack
(assert (location= bits res)))))
(single-reg
(sc-case bits
(signed-reg
;; source must be in memory
(inst mov stack-temp bits)
(with-empty-tn@fp-top(res)
(inst fld stack-temp)))
(signed-stack
(with-empty-tn@fp-top(res)
(inst fld bits))))))))
(define-vop (make-double-float)
(:args (hi-bits :scs (signed-reg))
(lo-bits :scs (unsigned-reg)))
(:results (res :scs (double-reg)))
(:temporary (:sc double-stack) temp)
(:arg-types signed-num unsigned-num)
(:result-types double-float)
(:translate make-double-float)
(:policy :fast-safe)
(:vop-var vop)
(:generator 2
(let ((offset (1+ (tn-offset temp))))
(storew hi-bits ebp-tn (- offset))
(storew lo-bits ebp-tn (- (1+ offset)))
(with-empty-tn@fp-top(res)
(inst fldd (make-ea :dword :base ebp-tn
:disp (- (* (1+ offset) word-bytes))))))))
(define-vop (single-float-bits)
(:args (float :scs (single-reg descriptor-reg)))
(:results (bits :scs (signed-reg)))
(:temporary (:sc signed-stack :from :argument :to :result) stack-temp)
(:arg-types single-float)
(:result-types signed-num)
(:translate single-float-bits)
(:policy :fast-safe)
(:vop-var vop)
(:generator 4
(sc-case bits
(signed-reg
(sc-case float
(single-reg
(with-tn@fp-top(float)
(inst fst stack-temp)
(inst mov bits stack-temp)))
(single-stack
(inst mov bits float))
(descriptor-reg
(loadw
bits float vm:single-float-value-slot vm:other-pointer-type))))
(signed-stack
(sc-case float
(single-reg
(with-tn@fp-top(float)
(inst fst bits))))))))
;; must test
(define-vop (double-float-high-bits)
(:args (float :scs (double-reg descriptor-reg)))
(:results (hi-bits :scs (signed-reg)))
(:temporary (:sc double-stack) temp)
(:arg-types double-float)
(:result-types signed-num)
(:translate double-float-high-bits)
(:policy :fast-safe)
(:vop-var vop)
(:generator 5
(sc-case hi-bits
(signed-reg
(sc-case float
(double-reg
(with-tn@fp-top(float)
(let ((where (make-ea :dword :base ebp-tn
:disp (- (* (+ 2 (tn-offset temp))
word-bytes)))))
(inst fstd where)))
(loadw hi-bits ebp-tn (- (1+ (tn-offset temp)))))
(double-stack
(loadw hi-bits ebp-tn (- (1+ (tn-offset float)))))
(descriptor-reg
(loadw hi-bits float (1+ vm:double-float-value-slot)
vm:other-pointer-type))))
#+nil ;; should not happen
(signed-stack
(sc-case float
(double-reg
(inst stf float (current-nfp-tn vop)
(* (tn-offset hi-bits) vm:word-bytes))))))))
;;needs testing
(define-vop (double-float-low-bits)
(:args (float :scs (double-reg descriptor-reg)))
(:results (lo-bits :scs (unsigned-reg)))
(:temporary (:sc double-stack) temp)
(:arg-types double-float)
(:result-types unsigned-num)
(:translate double-float-low-bits)
(:policy :fast-safe)
(:vop-var vop)
(:generator 5
(sc-case lo-bits
(unsigned-reg
(sc-case float
(double-reg
(with-tn@fp-top(float)
(let ((where (make-ea :dword :base ebp-tn
:disp (- (* (+ 2 (tn-offset temp))
word-bytes)))))
(inst fstd where)))
(loadw lo-bits ebp-tn (- (+ 2 (tn-offset temp)))))
(double-stack
(loadw lo-bits ebp-tn (- (+ 2 (tn-offset float)))))
(descriptor-reg
(loadw lo-bits float vm:double-float-value-slot
vm:other-pointer-type))))
#+nil ;; should not happen
(unsigned-stack
(sc-case float
(double-reg
(inst stf-odd float (current-nfp-tn vop)
(* (tn-offset lo-bits) vm:word-bytes))))))))
;;;; Float mode hackery:
(deftype float-modes () '(unsigned-byte 32)) ; really only 16
(defknown floating-point-modes () float-modes (flushable))
(defknown ((setf floating-point-modes)) (float-modes)
float-modes)
(defconstant npx-env-size (* 7 vm:word-bytes))
(defconstant npx-cw-offset 0)
(defconstant npx-sw-offset 4)
(define-vop (floating-point-modes)
(:results (res :scs (unsigned-reg)))
(:result-types unsigned-num)
(:translate floating-point-modes)
(:policy :fast-safe)
(:vop-var vop)
(:temporary (:sc dword-reg :offset eax-offset :target res) eax)
(:temporary (:sc dword-reg) tmp)
(:generator 8
(inst mov tmp esp-tn) ; save stack pointer
(inst sub esp-tn npx-env-size) ; make space on stack
(inst wait) ; Catch any pending FPE exceptions
(inst fstenv (make-ea :dword :base esp-tn)) ; masks all exceptions
(inst fldenv (make-ea :dword :base esp-tn)) ; restore previous state
(inst mov ax-tn (make-ea :word :base esp-tn :disp npx-sw-offset))
(inst shl eax 16) ; current status to high word
(inst mov ax-tn (make-ea :word :base esp-tn :disp npx-cw-offset))
(inst xor ax-tn #x3f) ; turn exception mask to trap enable bits
(inst mov esp-tn tmp) ; reset stack
(move res eax)))
(define-vop (set-floating-point-modes)
(:args (new :scs (unsigned-reg) :target res))
(:results (res :scs (unsigned-reg)))
(:arg-types unsigned-num)
(:result-types unsigned-num)
(:translate (setf floating-point-modes))
(:policy :fast-safe)
(:vop-var vop)
(:temporary (:sc dword-reg :offset eax-offset) eax)
(:temporary (:sc dword-reg) stmp)
(:temporary (:sc dword-reg) wtmp)
(:generator 3
(inst mov stmp esp-tn) ; save stack pointer
(inst sub esp-tn npx-env-size) ; make space on stack
(inst wait) ; Catch any pending FPE exceptions
(inst fstenv (make-ea :dword :base esp-tn))
(inst mov ax-tn (make-ea :word :base esp-tn :disp npx-sw-offset))
(move wtmp new)
(inst shr wtmp 16) ; position sticky bits
(inst and wtmp #x3f)
(inst and eax wtmp) ; maybe new sticky bits
(inst mov (make-ea :word :base esp-tn :disp npx-sw-offset) ax-tn)
(move eax new)
(inst xor eax #x3f) ; turn trap enable bits into exception mask
(inst mov (make-ea :word :base esp-tn :disp npx-cw-offset) ax-tn)
(inst fldenv (make-ea :dword :base esp-tn))
(move esp-tn stmp) ; reset stack
(move res new)))
;;; Lets use some of the 80387 special functions.
;;;
;;; These defs will not take effect unless code/irrat.lisp is modified
;;; to remove the inlined alien routine def.
(macrolet ((frob (func trans op)
`(define-vop (,func)
(:args (x :scs (double-reg) :target fr0))
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
(:ignore fr0)
(:results (y :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:translate ,trans)
(:policy :fast-safe)
(:note "inline NPX function")
(:vop-var vop)
(:save-p :compute-only)
(:node-var node)
(:generator 5
(note-this-location vop :internal-error)
(unless (zerop (tn-offset x))
(inst fxch x) ; x to top of stack
(unless (location= x y)
(inst fst x))) ; maybe save it
(inst ,op) ; clobber st0
(cond ((zerop (tn-offset y))
(when (policy node (or (= debug 3) (> safety speed)))
(inst wait)))
(t
(inst fst y)))))))
(frob fsin-quick %sin-quick fsin) ; arg range is 2^63
(frob fcos-quick %cos-quick fcos) ; so may need frem1
(frob fsqrt %sqrt fsqrt))
;;; Versions of fsin and fcos which handle a larger arg. range.
(macrolet ((frob (func trans op)
`(define-vop (,func)
(:translate ,trans)
(:args (x :scs (double-reg) :target fr0))
#+nil(:temporary (:sc word-reg :offset eax-offset
:from :eval :to :result) temp)
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
#+nil(:temporary (:sc double-reg :offset fr1-offset
:from :argument :to :result) fr1)
(:results (y :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline sin/cos function")
(:vop-var vop)
(:save-p :compute-only)
#+nil(:ignore temp)
(:generator 5
(note-this-location vop :internal-error)
(unless (zerop (tn-offset x))
(inst fxch x) ; x to top of stack
(unless (location= x y)
(inst fst x))) ; maybe save it
(inst ,op)
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x04) ; C2
(inst jmp :z DONE)
;; Else x was out of range so reduce it; ST0 is unchanged.
#+nil ; @@@
(progn ; Arg reduction is errorful
(inst fstp fr1) ; Load 2*PI
(inst fldpi)
(inst fadd fr0)
(inst fxch fr1)
LOOP
(inst fprem1)
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x04) ; C2
(inst jmp :nz LOOP)
(inst ,op))
(progn ; @@@
(inst fstp fr0) ; Else Load 0.0
(inst fldz)) ; on too big args
DONE
(unless (zerop (tn-offset y))
(inst fstd y))))))
(frob fsin %sin fsin)
(frob fcos %cos fcos))
(define-vop (ftan)
(:translate %tan)
(:args (x :scs (double-reg) :target fr0))
#+nil(:temporary (:sc word-reg :offset eax-offset
:from :eval :to :result) temp)
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from :argument :to :result) fr1)
(:results (y :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline tan function")
(:vop-var vop)
(:save-p :compute-only)
#+nil(:ignore temp)
(:generator 5
(note-this-location vop :internal-error)
(case (tn-offset x)
(0
(inst fstp fr1))
(1
(inst fstp fr0))
(t
(inst fstp fr0)
(inst fstp fr0)
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (- (tn-offset x) 2)))))
(inst fptan)
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x04) ; C2
(inst jmp :z DONE)
;; Else x was out of range so reduce it; ST0 is unchanged.
#+nil
(progn ; @@@ Reduction doesn't work well.
(inst fldpi) ; Load 2*PI
(inst fadd fr0)
(inst fxch fr1)
LOOP
(inst fprem1)
(inst fnstsw) ; status word to %ea
(inst and ah-tn #x04) ; C2
(inst jmp :nz LOOP)
(inst fstp fr1)
(inst fptan))
(progn ; @@@ just load 0.0
(inst fldz)
(inst fxch fr1))
DONE
;; Result is in fr1
(case (tn-offset y)
(0
(inst fxch fr1))
(1)
(t
(inst fxch fr1)
(inst fstd y)))))
(define-vop (ftan-quick)
(:translate %tan-quick)
(:args (x :scs (double-reg) :target fr0))
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from :argument :to :result) fr1)
(:results (y :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline tan function")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
(case (tn-offset x)
(0
(inst fstp fr1))
(1
(inst fstp fr0))
(t
(inst fstp fr0)
(inst fstp fr0)
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (- (tn-offset x) 2)))))
(inst fptan)
;; Result is in fr1
(case (tn-offset y)
(0
(inst fxch fr1))
(1)
(t
(inst fxch fr1)
(inst fstd y)))))
#+nil
(define-vop (fexp)
(:translate %exp)
(:args (x :scs (double-reg double-stack descriptor-reg) :target fr0))
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from :argument :to :result) fr1)
(:temporary (:sc double-reg :offset fr2-offset
:from :argument :to :result) fr2)
(:results (y :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline exp function")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
(sc-case x
(double-reg
(cond ((zerop (tn-offset x))
;; x is in fr0
(inst fstp fr1)
(inst fldl2e)
(inst fmul fr1))
(t
;; x is in a FP reg, not fr0
(inst fstp fr0)
(inst fldl2e)
(inst fmul x))))
((double-stack descriptor-reg)
(inst fstp fr0)
(inst fldl2e)
(if (sc-is x double-stack)
(inst fmuld (ea-for-df-stack x))
(inst fmuld (ea-for-df-desc x)))))
;; Now fr0=x log2(e)
(inst fst fr1)
(inst frndint)
(inst fst fr2)
(inst fsubp-sti fr1)
(inst f2xm1)
(inst fld1)
(inst faddp-sti fr1)
(inst fscale)
(inst fld fr0)
(case (tn-offset y)
((0 1))
(t (inst fstd y)))))
;;; Modified exp that handles the following special cases:
;;; exp(+Inf) is +Inf; exp(-Inf) is 0; exp(NaN) is NaN.
(define-vop (fexp)
(:translate %exp)
(:args (x :scs (double-reg) :target fr0))
(:temporary (:sc word-reg :offset eax-offset :from :eval :to :result) temp)
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from :argument :to :result) fr1)
(:temporary (:sc double-reg :offset fr2-offset
:from :argument :to :result) fr2)
(:results (y :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline exp function")
(:vop-var vop)
(:save-p :compute-only)
(:ignore temp)
(:generator 5
(note-this-location vop :internal-error)
(unless (zerop (tn-offset x))
(inst fxch x) ; x to top of stack
(unless (location= x y)
(inst fst x))) ; maybe save it
;; Check for Inf or NaN
(inst fxam)
(inst fnstsw)
(inst sahf)
(inst jmp :nc NOINFNAN) ; Neither Inf or NaN.
(inst jmp :np NOINFNAN) ; NaN gives NaN? Continue.
(inst and ah-tn #x02) ; Test sign of Inf.
(inst jmp :z DONE) ; +Inf gives +Inf.
(inst fstp fr0) ; -Inf gives 0
(inst fldz)
(inst jmp-short DONE)
NOINFNAN
(inst fstp fr1)
(inst fldl2e)
(inst fmul fr1)
;; Now fr0=x log2(e)
(inst fst fr1)
(inst frndint)
(inst fst fr2)
(inst fsubp-sti fr1)
(inst f2xm1)
(inst fld1)
(inst faddp-sti fr1)
(inst fscale)
(inst fld fr0)
DONE
(unless (zerop (tn-offset y))
(inst fstd y))))
(define-vop (flog)
(:translate %log)
(:args (x :scs (double-reg double-stack descriptor-reg) :target fr0))
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from :argument :to :result) fr1)
(:results (y :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline log function")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
(sc-case x
(double-reg
(case (tn-offset x)
(0
;; x is in fr0
(inst fstp fr1)
(inst fldln2)
(inst fxch fr1))
(1
;; x is in fr1
(inst fstp fr0)
(inst fldln2)
(inst fxch fr1))
(t
;; x is in a FP reg, not fr0 or fr1
(inst fstp fr0)
(inst fstp fr0)
(inst fldln2)
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (1- (tn-offset x))))))
(inst fyl2x))
((double-stack descriptor-reg)
(inst fstp fr0)
(inst fstp fr0)
(inst fldln2)
(if (sc-is x double-stack)
(inst fldd (ea-for-df-stack x))
(inst fldd (ea-for-df-desc x)))
(inst fyl2x)))
(inst fld fr0)
(case (tn-offset y)
((0 1))
(t (inst fstd y)))))
(define-vop (flog10)
(:translate %log10)
(:args (x :scs (double-reg double-stack descriptor-reg) :target fr0))
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from :argument :to :result) fr1)
(:results (y :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline log10 function")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
(sc-case x
(double-reg
(case (tn-offset x)
(0
;; x is in fr0
(inst fstp fr1)
(inst fldlg2)
(inst fxch fr1))
(1
;; x is in fr1
(inst fstp fr0)
(inst fldlg2)
(inst fxch fr1))
(t
;; x is in a FP reg, not fr0 or fr1
(inst fstp fr0)
(inst fstp fr0)
(inst fldlg2)
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (1- (tn-offset x))))))
(inst fyl2x))
((double-stack descriptor-reg)
(inst fstp fr0)
(inst fstp fr0)
(inst fldlg2)
(if (sc-is x double-stack)
(inst fldd (ea-for-df-stack x))
(inst fldd (ea-for-df-desc x)))
(inst fyl2x)))
(inst fld fr0)
(case (tn-offset y)
((0 1))
(t (inst fstd y)))))
(define-vop (fpow)
(:translate %pow)
(:args (x :scs (double-reg double-stack descriptor-reg) :target fr0)
(y :scs (double-reg double-stack descriptor-reg) :target fr1))
(:temporary (:sc double-reg :offset fr0-offset
:from (:argument 0) :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from (:argument 1) :to :result) fr1)
(:temporary (:sc double-reg :offset fr2-offset
:from :load :to :result) fr2)
(:results (r :scs (double-reg)))
(:arg-types double-float double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline pow function")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
;; Setup x in fr0 and y in fr1
(cond
;; x in fr0; y in fr1
((and (sc-is x double-reg) (zerop (tn-offset x))
(sc-is y double-reg) (= 1 (tn-offset y))))
;; y in fr1; x not in fr0
((and (sc-is y double-reg) (= 1 (tn-offset y)))
;; Load x to fr0
(sc-case x
(double-reg
(copy-fp-reg-to-fr0 x))
(inst fstp fr0)
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fstp fr0)
(inst fldd (ea-for-df-desc x)))))
;; x in fr0; y not in fr1
((and (sc-is x double-reg) (zerop (tn-offset x)))
(inst fxch fr1)
;; Now load y to fr0
(sc-case y
(double-reg
(copy-fp-reg-to-fr0 y))
(inst fstp fr0)
(inst fldd (ea-for-df-stack y)))
(descriptor-reg
(inst fstp fr0)
(inst fldd (ea-for-df-desc y))))
(inst fxch fr1))
;; x in fr1; y not in fr1
((and (sc-is x double-reg) (= 1 (tn-offset x)))
;; Load y to fr0
(sc-case y
(double-reg
(copy-fp-reg-to-fr0 y))
(inst fstp fr0)
(inst fldd (ea-for-df-stack y)))
(descriptor-reg
(inst fstp fr0)
(inst fldd (ea-for-df-desc y))))
(inst fxch fr1))
;; y in fr0;
((and (sc-is y double-reg) (zerop (tn-offset y)))
(inst fxch fr1)
;; Now load x to fr0
(sc-case x
(double-reg
(copy-fp-reg-to-fr0 x))
(inst fstp fr0)
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fstp fr0)
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
(inst fldd (ea-for-df-desc x)))))
;; Neither x or y are in either fr0 or fr1
(t
;; Load y then x
(inst fstp fr0)
(inst fstp fr0)
(sc-case y
(double-reg
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (- (tn-offset y) 2))))
(double-stack
(inst fldd (ea-for-df-stack y)))
(descriptor-reg
(inst fldd (ea-for-df-desc y))))
;; Load x to fr0
(sc-case x
(double-reg
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (1- (tn-offset x)))))
(double-stack
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fldd (ea-for-df-desc x))))))
;; Now have x at fr0; and y at fr1
(inst fyl2x)
;; Now fr0=y log2(x)
(inst fld fr0)
(inst frndint)
(inst fst fr2)
(inst fsubp-sti fr1)
(inst f2xm1)
(inst fld1)
(inst faddp-sti fr1)
(inst fscale)
(inst fld fr0)
(case (tn-offset r)
((0 1))
(t (inst fstd r)))))
(define-vop (fscalen)
(:translate %scalbn)
(:args (x :scs (double-reg double-stack descriptor-reg) :target fr0)
(y :scs (signed-stack signed-reg) :target temp))
(:temporary (:sc double-reg :offset fr0-offset
:from (:argument 0) :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset :from :eval :to :result) fr1)
(:temporary (:sc signed-stack :from (:argument 1) :to :result) temp)
(:results (r :scs (double-reg)))
(:arg-types double-float signed-num)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline scalbn function")
(:generator 5
;; Setup x in fr0 and y in fr1
(sc-case x
(double-reg
(case (tn-offset x)
(0
(inst fstp fr1)
(sc-case y
(signed-reg
(inst mov temp y)
(inst fild temp))
(signed-stack
(inst fild y)))
(inst fxch fr1))
(1
(inst fstp fr0)
(sc-case y
(signed-reg
(inst mov temp y)
(inst fild temp))
(signed-stack
(inst fild y)))
(inst fxch fr1))
(t
(inst fstp fr0)
(inst fstp fr0)
(sc-case y
(signed-reg
(inst mov temp y)
(inst fild temp))
(signed-stack
(inst fild y)))
(inst fld (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (1- (tn-offset x)))))))
((double-stack descriptor-reg)
(inst fstp fr0)
(inst fstp fr0)
(sc-case y
(signed-reg
(inst mov temp y)
(inst fild temp))
(signed-stack
(inst fild y)))
(if (sc-is x double-stack)
(inst fldd (ea-for-df-stack x))
(inst fldd (ea-for-df-desc x)))))
(inst fscale)
(unless (zerop (tn-offset r))
(inst fstd r))))
(define-vop (fscale)
(:translate %scalb)
(:args (x :scs (double-reg double-stack descriptor-reg) :target fr0)
(y :scs (double-reg double-stack descriptor-reg) :target fr1))
(:temporary (:sc double-reg :offset fr0-offset
:from (:argument 0) :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from (:argument 1) :to :result) fr1)
(:results (r :scs (double-reg)))
(:arg-types double-float double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline scalb function")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
;; Setup x in fr0 and y in fr1
(cond
;; x in fr0; y in fr1
((and (sc-is x double-reg) (zerop (tn-offset x))
(sc-is y double-reg) (= 1 (tn-offset y))))
;; y in fr1; x not in fr0
((and (sc-is y double-reg) (= 1 (tn-offset y)))
;; Load x to fr0
(sc-case x
(double-reg
(copy-fp-reg-to-fr0 x))
(inst fstp fr0)
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fstp fr0)
(inst fldd (ea-for-df-desc x)))))
;; x in fr0; y not in fr1
((and (sc-is x double-reg) (zerop (tn-offset x)))
(inst fxch fr1)
;; Now load y to fr0
(sc-case y
(double-reg
(copy-fp-reg-to-fr0 y))
(inst fstp fr0)
(inst fldd (ea-for-df-stack y)))
(descriptor-reg
(inst fstp fr0)
(inst fldd (ea-for-df-desc y))))
(inst fxch fr1))
;; x in fr1; y not in fr1
((and (sc-is x double-reg) (= 1 (tn-offset x)))
;; Load y to fr0
(sc-case y
(double-reg
(copy-fp-reg-to-fr0 y))
(inst fstp fr0)
(inst fldd (ea-for-df-stack y)))
(descriptor-reg
(inst fstp fr0)
(inst fldd (ea-for-df-desc y))))
(inst fxch fr1))
;; y in fr0;
((and (sc-is y double-reg) (zerop (tn-offset y)))
(inst fxch fr1)
;; Now load x to fr0
(sc-case x
(double-reg
(copy-fp-reg-to-fr0 x))
(inst fstp fr0)
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fstp fr0)
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
(inst fldd (ea-for-df-desc x)))))
;; Neither x or y are in either fr0 or fr1
(t
;; Load y then x
(inst fstp fr0)
(inst fstp fr0)
(sc-case y
(double-reg
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (- (tn-offset y) 2))))
(double-stack
(inst fldd (ea-for-df-stack y)))
(descriptor-reg
(inst fldd (ea-for-df-desc y))))
;; Load x to fr0
(sc-case x
(double-reg
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (1- (tn-offset x)))))
(double-stack
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fldd (ea-for-df-desc x))))))
;; Now have x at fr0; and y at fr1
(inst fscale)
(unless (zerop (tn-offset r))
(inst fstd r))))
;;; Seems to work fine on a Pentium, but the range of the x argument
;;; is limited on a 386/486.
(define-vop (flog1p-limited)
(:translate %log1p-limited)
(:args (x :scs (double-reg double-stack descriptor-reg) :target fr0))
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from :argument :to :result) fr1)
(:results (y :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline log1p with limited x range function")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
(sc-case x
(double-reg
(case (tn-offset x)
(0
;; x is in fr0
(inst fstp fr1)
(inst fldln2)
(inst fxch fr1))
(1
;; x is in fr1
(inst fstp fr0)
(inst fldln2)
(inst fxch fr1))
(t
;; x is in a FP reg, not fr0 or fr1
(inst fstp fr0)
(inst fstp fr0)
(inst fldln2)
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (- (tn-offset x) 2)))))
(inst fyl2xp1))
((double-stack descriptor-reg)
(inst fstp fr0)
(inst fstp fr0)
(inst fldln2)
(if (sc-is x double-stack)
(inst fldd (ea-for-df-stack x))
(inst fldd (ea-for-df-desc x)))
(inst fyl2xp1)))
(inst fld fr0)
(case (tn-offset y)
((0 1))
(t (inst fstd y)))))
(define-vop (flogb)
(:translate %logb)
(:args (x :scs (double-reg double-stack descriptor-reg) :target fr0))
(:temporary (:sc double-reg :offset fr0-offset
:from :argument :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from :argument :to :result) fr1)
(:results (y :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline logb function")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
(sc-case x
(double-reg
(case (tn-offset x)
(0
;; x is in fr0
(inst fstp fr1))
(1
;; x is in fr1
(inst fstp fr0))
(t
;; x is in a FP reg, not fr0 or fr1
(inst fstp fr0)
(inst fstp fr0)
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (- (tn-offset x) 2))))))
((double-stack descriptor-reg)
(inst fstp fr0)
(inst fstp fr0)
(if (sc-is x double-stack)
(inst fldd (ea-for-df-stack x))
(inst fldd (ea-for-df-desc x)))))
(inst fxtract)
(case (tn-offset y)
(0
(inst fxch fr1))
(1)
(t (inst fxch fr1)
(inst fstd y)))))
(define-vop (fatan)
(:translate %atan)
(:args (x :scs (double-reg double-stack descriptor-reg) :target fr0))
(:temporary (:sc double-reg :offset fr0-offset
:from (:argument 0) :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from (:argument 0) :to :result) fr1)
(:results (r :scs (double-reg)))
(:arg-types double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline atan function")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
;; Setup x in fr1 and 1.0 in fr0
(cond
;; x in fr0
((and (sc-is x double-reg) (zerop (tn-offset x)))
(inst fstp fr1))
;; x in fr1
((and (sc-is x double-reg) (= 1 (tn-offset x)))
(inst fstp fr0))
;; x not in fr0 or fr1
(t
;; Load x then 1.0
(inst fstp fr0)
(inst fstp fr0)
(sc-case x
(double-reg
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (- (tn-offset x) 2))))
(double-stack
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fldd (ea-for-df-desc x))))))
(inst fld1)
;; Now have x at fr1; and 1.0 at fr0
(inst fpatan)
(inst fld fr0)
(case (tn-offset r)
((0 1))
(t (inst fstd r)))))
(define-vop (fatan2)
(:translate %atan2)
(:args (x :scs (double-reg double-stack descriptor-reg) :target fr1)
(y :scs (double-reg double-stack descriptor-reg) :target fr0))
(:temporary (:sc double-reg :offset fr0-offset
:from (:argument 1) :to :result) fr0)
(:temporary (:sc double-reg :offset fr1-offset
:from (:argument 0) :to :result) fr1)
(:results (r :scs (double-reg)))
(:arg-types double-float double-float)
(:result-types double-float)
(:policy :fast-safe)
(:note "inline atan2 function")
(:vop-var vop)
(:save-p :compute-only)
(:generator 5
(note-this-location vop :internal-error)
;; Setup x in fr1 and y in fr0
(cond
;; y in fr0; x in fr1
((and (sc-is y double-reg) (zerop (tn-offset y))
(sc-is x double-reg) (= 1 (tn-offset x))))
;; x in fr1; y not in fr0
((and (sc-is x double-reg) (= 1 (tn-offset x)))
;; Load y to fr0
(sc-case y
(double-reg
(copy-fp-reg-to-fr0 y))
(inst fstp fr0)
(inst fldd (ea-for-df-stack y)))
(descriptor-reg
(inst fstp fr0)
(inst fldd (ea-for-df-desc y)))))
;; y in fr0; x not in fr1
((and (sc-is y double-reg) (zerop (tn-offset y)))
(inst fxch fr1)
;; Now load x to fr0
(sc-case x
(double-reg
(copy-fp-reg-to-fr0 x))
(inst fstp fr0)
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fstp fr0)
(inst fldd (ea-for-df-desc x))))
(inst fxch fr1))
;; y in fr1; x not in fr1
((and (sc-is y double-reg) (= 1 (tn-offset y)))
;; Load x to fr0
(sc-case x
(double-reg
(copy-fp-reg-to-fr0 x))
(inst fstp fr0)
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fstp fr0)
(inst fldd (ea-for-df-desc x))))
(inst fxch fr1))
;; x in fr0;
((and (sc-is x double-reg) (zerop (tn-offset x)))
(inst fxch fr1)
;; Now load y to fr0
(sc-case y
(double-reg
(copy-fp-reg-to-fr0 y))
(inst fstp fr0)
(inst fldd (ea-for-df-stack y)))
(descriptor-reg
(inst fstp fr0)
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
(inst fldd (ea-for-df-desc y)))))
;; Neither y or x are in either fr0 or fr1
(t
;; Load x then y
(inst fstp fr0)
(inst fstp fr0)
(sc-case x
(double-reg
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (- (tn-offset x) 2))))
(double-stack
(inst fldd (ea-for-df-stack x)))
(descriptor-reg
(inst fldd (ea-for-df-desc x))))
;; Load y to fr0
(sc-case y
(double-reg
(inst fldd (make-random-tn :kind :normal
:sc (sc-or-lose 'double-reg)
:offset (1- (tn-offset y)))))
(double-stack
(inst fldd (ea-for-df-stack y)))
(descriptor-reg
(inst fldd (ea-for-df-desc y))))))
;; Now have y at fr0; and x at fr1
(inst fpatan)
(inst fld fr0)
(case (tn-offset r)
((0 1))
(t (inst fstd r)))))