-
Notifications
You must be signed in to change notification settings - Fork 313
/
float.lisp
1660 lines (1557 loc) · 68.6 KB
/
float.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
187
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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
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
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
741
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
794
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
849
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
902
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
;;;; floating point support for x86-64
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; This software is derived from the CMU CL system, which was
;;;; written at Carnegie Mellon University and released into the
;;;; public domain. The software is in the public domain and is
;;;; provided with absolutely no warranty. See the COPYING and CREDITS
;;;; files for more information.
(in-package "SB-VM")
(macrolet ((ea-for-xf-desc (tn slot)
`(ea (- (* ,slot n-word-bytes) other-pointer-lowtag) ,tn)))
(defun ea-for-df-desc (tn)
(ea-for-xf-desc tn double-float-value-slot))
;; complex floats
(defun ea-for-csf-data-desc (tn)
(ea-for-xf-desc tn complex-single-float-data-slot))
(defun ea-for-csf-real-desc (tn)
(ea-for-xf-desc tn complex-single-float-data-slot))
(defun ea-for-csf-imag-desc (tn)
(ea-for-xf-desc tn (+ complex-single-float-data-slot 1/2)))
(defun ea-for-cdf-data-desc (tn)
(ea-for-xf-desc tn complex-double-float-real-slot))
(defun ea-for-cdf-real-desc (tn)
(ea-for-xf-desc tn complex-double-float-real-slot))
(defun ea-for-cdf-imag-desc (tn)
(ea-for-xf-desc tn complex-double-float-imag-slot)))
(macrolet ((ea-for-xf-stack (tn kind)
(declare (ignore kind))
`(ea (frame-byte-offset (tn-offset ,tn)) rbp-tn)))
(defun ea-for-sf-stack (tn)
(ea-for-xf-stack tn :single))
(defun ea-for-df-stack (tn)
(ea-for-xf-stack tn :double)))
;;; complex float stack EAs
(macrolet ((ea-for-cxf-stack (tn kind slot &optional base)
`(ea (frame-byte-offset
(+ (tn-offset ,tn)
(cond ((= (tn-offset ,base) rsp-offset)
sp->fp-offset)
(t 0))
(ecase ,kind
(:single
(ecase ,slot
(:real 0)
(:imag -1/2)))
(:double
(ecase ,slot
(:real 1)
(:imag 0))))))
,base)))
(defun ea-for-csf-data-stack (tn &optional (base rbp-tn))
(ea-for-cxf-stack tn :single :real base))
(defun ea-for-csf-real-stack (tn &optional (base rbp-tn))
(ea-for-cxf-stack tn :single :real base))
(defun ea-for-csf-imag-stack (tn &optional (base rbp-tn))
(ea-for-cxf-stack tn :single :imag base))
(defun ea-for-cdf-data-stack (tn &optional (base rbp-tn))
(ea-for-cxf-stack tn :double :real base))
(defun ea-for-cdf-real-stack (tn &optional (base rbp-tn))
(ea-for-cxf-stack tn :double :real base))
(defun ea-for-cdf-imag-stack (tn &optional (base rbp-tn))
(ea-for-cxf-stack tn :double :imag base)))
;;;; move functions
;;; X is source, Y is destination.
(define-move-fun (load-fp-zero 1) (vop x y)
((fp-single-zero) (single-reg)
(fp-double-zero) (double-reg)
(fp-complex-single-zero) (complex-single-reg)
(fp-complex-double-zero) (complex-double-reg))
(identity x)
(sc-case y
((single-reg complex-single-reg) (inst xorps y y))
((double-reg complex-double-reg) (inst xorpd y y))))
(define-move-fun (load-fp-immediate 1) (vop x y)
((fp-single-immediate) (single-reg)
(fp-double-immediate) (double-reg)
(fp-complex-single-immediate) (complex-single-reg)
(fp-complex-double-immediate) (complex-double-reg))
(let ((x (register-inline-constant (tn-value x))))
(sc-case y
(single-reg (inst movss y x))
(double-reg (inst movsd y x))
(complex-single-reg (inst movq y x))
(complex-double-reg (inst movapd y x)))))
(define-move-fun (load-single 2) (vop x y)
((single-stack) (single-reg))
(inst movss y (ea-for-sf-stack x)))
(define-move-fun (store-single 2) (vop x y)
((single-reg) (single-stack))
(inst movss (ea-for-sf-stack y) x))
(define-move-fun (load-double 2) (vop x y)
((double-stack) (double-reg))
(inst movsd y (ea-for-df-stack x)))
(define-move-fun (store-double 2) (vop x y)
((double-reg) (double-stack))
(inst movsd (ea-for-df-stack y) x))
;;;; complex float move functions
;;; X is source, Y is destination.
(define-move-fun (load-complex-single 2) (vop x y)
((complex-single-stack) (complex-single-reg))
(inst movq y (ea-for-csf-data-stack x)))
(define-move-fun (store-complex-single 2) (vop x y)
((complex-single-reg) (complex-single-stack))
(inst movq (ea-for-csf-data-stack y) x))
(define-move-fun (load-complex-double 2) (vop x y)
((complex-double-stack) (complex-double-reg))
(inst movupd y (ea-for-cdf-data-stack x)))
(define-move-fun (store-complex-double 2) (vop x y)
((complex-double-reg) (complex-double-stack))
(inst movupd (ea-for-cdf-data-stack y) x))
;;;; move VOPs
;;; float register to register moves
(macrolet ((frob (vop sc)
`(progn
(define-vop (,vop)
(:args (x :scs (,sc)
:target y
:load-if (not (location= x y))))
(:results (y :scs (,sc)
:load-if (not (location= x y))))
(:note "float move")
(:generator 0
(move y x)))
(define-move-vop ,vop :move (,sc) (,sc)))))
(frob single-move single-reg)
(frob double-move double-reg)
(frob complex-single-move complex-single-reg)
(frob complex-double-move complex-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))
(:results (y :scs (descriptor-reg)))
(:note "float to pointer coercion")
(:generator 4
(inst movd y x)
(inst shl y 32)
(inst or :byte y single-float-widetag)))
(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 "float to pointer coercion")
#+gs-seg (:temporary (:sc unsigned-reg :offset 15) thread-tn)
(:generator 13
(alloc-other double-float-widetag double-float-size y node nil thread-tn)
(inst movsd (ea-for-df-desc y) x)))
(define-move-vop move-from-double :move
(double-reg) (descriptor-reg))
(define-vop (!copy-dfloat)
(:args (x :scs (descriptor-reg) :to :save))
(:results (copy :scs (descriptor-reg)))
(:temporary (:sc unsigned-reg) bits)
(:node-var node)
#+gs-seg (:temporary (:sc unsigned-reg :offset 15) thread-tn)
(:generator 3
(alloc-other double-float-widetag double-float-size copy node nil thread-tn)
(loadw bits x double-float-value-slot other-pointer-lowtag)
(storew bits copy double-float-value-slot other-pointer-lowtag)))
;;; Move from a descriptor to a float register.
(define-vop (move-to-single-reg)
(:args (x :scs (descriptor-reg)
:load-if (not (sc-is x control-stack))))
(:results (y :scs (single-reg)))
(:note "descriptor to float coercion")
(:generator 2
(sc-case x
(descriptor-reg
;; After MOVQ, vector element 0 holds a single-float whose bits are
;; SINGLE-FLOAT-WIDETAG. That's fine, it's an ordinary denormal float.
(inst movq y x)
;; Move bits [63:32] into [31:0] and move bits [127:96]
;; into the other 3 vector elements so that [63:32] is zeroed.
(inst shufps y y #4r3331))
(control-stack
;; Directly load high 4 bytes of descriptor from its stack address
(inst movss y (ea (+ (frame-byte-offset (tn-offset x)) 4) rbp-tn))))))
(define-move-vop move-to-single-reg :move (descriptor-reg) (single-reg))
;;; Move from a descriptor to a float stack.
(define-vop (move-to-single-stack)
(:args (x :scs (descriptor-reg) :target tmp))
(:temporary (:sc unsigned-reg :from :argument :to :result) tmp)
(:results (y :scs (single-stack)))
(:note "pointer to float coercion")
(:generator 2
(move tmp x)
(inst shr tmp 32)
(inst mov :dword (ea (frame-byte-offset (tn-offset y)) rbp-tn) tmp)))
(define-move-vop move-to-single-stack :move (descriptor-reg) (single-stack))
(define-vop (move-to-double)
(:args (x :scs (descriptor-reg)))
(:results (y :scs (double-reg)))
(:note "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))
;;; 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)))
#+gs-seg (:temporary (:sc unsigned-reg :offset 15) thread-tn)
(:node-var node)
(:note "complex float to pointer coercion")
(:generator 13
(alloc-other complex-single-float-widetag complex-single-float-size y node nil thread-tn)
(inst movlps (ea-for-csf-data-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)))
#+gs-seg (:temporary (:sc unsigned-reg :offset 15) thread-tn)
(:node-var node)
(:note "complex float to pointer coercion")
(:generator 13
(alloc-other complex-double-float-widetag complex-double-float-size y node nil thread-tn)
(inst movapd (ea-for-cdf-data-desc y) x)))
(define-move-vop move-from-complex-double :move
(complex-double-reg) (descriptor-reg))
;;; Move from a descriptor to a complex float register.
(macrolet ((frob (name sc format)
`(progn
(define-vop (,name)
(:args (x :scs (descriptor-reg)))
(:results (y :scs (,sc)))
(:note "pointer to complex float coercion")
(:generator 2
,(ecase format
(:single
;; Use an integer move since there's no better choice.
;; - movaps moves 128 bits of data, which is wrong.
;; - movsd moves 64 bits as one double-float
;; - movlps moves 64 bits, but doesn't zero the upper bits
'(inst movq y (ea-for-csf-data-desc x)))
(:double
'(inst movapd y (ea-for-cdf-data-desc x))))))
(define-move-vop ,name :move (descriptor-reg) (,sc)))))
(frob move-to-complex-single complex-single-reg :single)
(frob move-to-complex-double complex-double-reg :double))
;;;; 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-ARG 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 "float argument move")
(:generator ,(case format (:single 2) (:double 3) )
(sc-case y
(,sc
(move y x))
(,stack-sc
(if (= (tn-offset fp) rsp-offset)
(let* ((offset (tn-byte-offset y))
(ea (ea offset fp)))
,@(ecase format
(:single '((inst movss ea x)))
(:double '((inst movsd ea x)))))
(let ((ea (ea (frame-byte-offset (tn-offset y)) fp)))
,@(ecase format
(:single '((inst movss ea x)))
(:double '((inst movsd ea x))))))))))
(define-move-vop ,name :move-arg
(,sc descriptor-reg) (,sc)))))
(frob move-single-float-arg single-reg single-stack :single)
(frob move-double-float-arg double-reg double-stack :double))
;;;; complex float MOVE-ARG 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 "complex float argument move")
(:generator ,(ecase format (:single 2) (:double 3))
(sc-case y
(,sc
(move y x))
(,stack-sc
,(ecase format
(:single
'(inst movq (ea-for-csf-data-stack y fp) x))
(:double
'(inst movupd (ea-for-cdf-data-stack y fp) x)))))))
(define-move-vop ,name :move-arg
(,sc descriptor-reg) (,sc)))))
(frob move-complex-single-float-arg
complex-single-reg complex-single-stack :single)
(frob move-complex-double-float-arg
complex-double-reg complex-double-stack :double))
(define-move-vop move-arg :move-arg
(single-reg double-reg
complex-single-reg complex-double-reg)
(descriptor-reg))
;;;; arithmetic VOPs
(define-vop (float-op)
(:args (x) (y))
(:results (r))
(:policy :fast-safe)
(:note "inline float arithmetic")
(:vop-var vop)
(:save-p :compute-only))
(macrolet ((frob (name comm-name sc constant-sc ptype)
`(progn
(define-vop (,name float-op)
(:args (x :scs (,sc ,constant-sc)
:target r
:load-if (not (sc-is x ,constant-sc)))
(y :scs (,sc ,constant-sc)
:load-if (not (sc-is y ,constant-sc))))
(:results (r :scs (,sc)))
(:arg-types ,ptype ,ptype)
(:result-types ,ptype))
(define-vop (,comm-name float-op)
(:args (x :scs (,sc ,constant-sc)
:target r
:load-if (not (sc-is x ,constant-sc)))
(y :scs (,sc ,constant-sc)
:target r
:load-if (not (sc-is y ,constant-sc))))
(:results (r :scs (,sc)))
(:arg-types ,ptype ,ptype)
(:result-types ,ptype)))))
(frob single-float-op single-float-comm-op
single-reg fp-single-immediate single-float)
(frob double-float-op double-float-comm-op
double-reg fp-double-immediate double-float)
(frob complex-single-float-op complex-single-float-comm-op
complex-single-reg fp-complex-single-immediate
complex-single-float)
(frob complex-double-float-op complex-double-float-comm-op
complex-double-reg fp-complex-double-immediate
complex-double-float))
(defun note-float-location (op vop &rest args)
(let ((*location-context*
(list* op
(loop for arg in args
collect
(cond ((or (symbolp arg)
(floatp arg)
(complexp arg)) arg)
((eq (tn-kind arg) :constant)
(tn-value arg))
(t
(make-sc+offset (sc-number (tn-sc arg))
(or (tn-offset arg) 0))))))))
(note-this-location vop :internal-error)))
(macrolet ((generate (op opinst commutative constant-sc load-inst)
`(flet ((get-constant (tn &optional maybe-aligned)
(declare (ignorable maybe-aligned))
(let ((value (tn-value tn)))
,(if (eq constant-sc 'fp-complex-single-immediate)
`(if maybe-aligned
(register-inline-constant
:aligned value)
(register-inline-constant value))
`(register-inline-constant value))))
(note-location (x y)
(note-float-location ',op vop x y)))
(declare (ignorable #'get-constant))
(cond
((location= x r)
(note-location x y)
(when (sc-is y ,constant-sc)
(setf y (get-constant y t)))
(inst ,opinst x y))
((and ,commutative (location= y r))
(note-location y x)
(when (sc-is x ,constant-sc)
(setf x (get-constant x t)))
(inst ,opinst y x))
((not (location= r y))
(if (sc-is x ,constant-sc)
(inst ,load-inst r (get-constant x))
(move r x))
(note-location r y)
(when (sc-is y ,constant-sc)
(setf y (get-constant y t)))
(inst ,opinst r y))
(t
(if (sc-is x ,constant-sc)
(inst ,load-inst tmp (get-constant x))
(move tmp x))
(note-location tmp y)
(inst ,opinst tmp y)
(move r tmp)))))
(frob (op sinst sname scost dinst dname dcost commutative
&optional csinst csname cscost cdinst cdname cdcost)
`(progn
(define-vop (,sname ,(if commutative
'single-float-comm-op
'single-float-op))
(:translate ,op)
(:temporary (:sc single-reg) tmp)
(:vop-var vop)
(:generator ,scost
(generate ,op ,sinst ,commutative fp-single-immediate movss)))
(define-vop (,dname ,(if commutative
'double-float-comm-op
'double-float-op))
(:translate ,op)
(:temporary (:sc double-reg) tmp)
(:vop-var vop)
(:generator ,dcost
(generate ,op ,dinst ,commutative fp-double-immediate movsd)))
,(when csinst
`(define-vop (,csname
,(if commutative
'complex-single-float-comm-op
'complex-single-float-op))
(:translate ,op)
(:temporary (:sc complex-single-reg) tmp)
(:vop-var vop)
(:generator ,cscost
(generate ,op ,csinst ,commutative
fp-complex-single-immediate movq))))
,(when cdinst
`(define-vop (,cdname
,(if commutative
'complex-double-float-comm-op
'complex-double-float-op))
(:translate ,op)
(:temporary (:sc complex-double-reg) tmp)
(:vop-var vop)
(:generator ,cdcost
(generate ,op ,cdinst ,commutative
fp-complex-double-immediate movapd)))))))
(frob + addss +/single-float 2 addsd +/double-float 2 t
addps +/complex-single-float 3 addpd +/complex-double-float 3)
(frob - subss -/single-float 2 subsd -/double-float 2 nil
subps -/complex-single-float 3 subpd -/complex-double-float 3)
(frob * mulss */single-float 4 mulsd */double-float 5 t)
(frob / divss //single-float 12 divsd //double-float 19 nil))
(macrolet ((frob (op cost commutativep
duplicate-inst op-inst real-move-inst complex-move-inst
real-sc real-constant-sc real-type
complex-sc complex-constant-sc complex-type
real-complex-name complex-real-name)
(cond ((not duplicate-inst) ; simple case
`(flet ((load-into (r x)
(sc-case x
(,real-constant-sc
(inst ,real-move-inst r
(register-inline-constant (tn-value x))))
(,complex-constant-sc
(inst ,complex-move-inst r
(register-inline-constant (tn-value x))))
(t (move r x)))))
,(when real-complex-name
`(define-vop (,real-complex-name float-op)
(:translate ,op)
(:args (x :scs (,real-sc ,real-constant-sc)
:target r
:load-if (not (sc-is x ,real-constant-sc)))
(y :scs (,complex-sc ,complex-constant-sc)
,@(when commutativep '(:target r))
:load-if (not (sc-is y ,complex-constant-sc))))
(:arg-types ,real-type ,complex-type)
(:results (r :scs (,complex-sc)
,@(unless commutativep '(:from (:argument 0)))))
(:result-types ,complex-type)
(:vop-var vop)
(:generator ,cost
,(when commutativep
`(when (location= y r)
(rotatef x y)))
(load-into r x)
(note-float-location ',op vop r y)
(when (sc-is y ,real-constant-sc ,complex-constant-sc)
(setf y (register-inline-constant
:aligned (tn-value y))))
(inst ,op-inst r y))))
,(when complex-real-name
`(define-vop (,complex-real-name float-op)
(:translate ,op)
(:args (x :scs (,complex-sc ,complex-constant-sc)
:target r
:load-if (not (sc-is x ,complex-constant-sc)))
(y :scs (,real-sc ,real-constant-sc)
,@(when commutativep '(:target r))
:load-if (not (sc-is y ,real-constant-sc))))
(:arg-types ,complex-type ,real-type)
(:results (r :scs (,complex-sc)
,@(unless commutativep '(:from (:argument 0)))))
(:result-types ,complex-type)
(:vop-var vop)
(:generator ,cost
,(when commutativep
`(when (location= y r)
(rotatef x y)))
(load-into r x)
(note-float-location ',op vop r y)
(when (sc-is y ,real-constant-sc ,complex-constant-sc)
(setf y (register-inline-constant
:aligned (tn-value y))))
(inst ,op-inst r y))))))
(commutativep ; must duplicate, but commutative
`(progn
,(when real-complex-name
`(define-vop (,real-complex-name float-op)
(:translate ,op)
(:args (x :scs (,real-sc ,real-constant-sc)
:target dup
:load-if (not (sc-is x ,real-constant-sc)))
(y :scs (,complex-sc ,complex-constant-sc)
:target r
:to :result
:load-if (not (sc-is y ,complex-constant-sc))))
(:arg-types ,real-type ,complex-type)
(:temporary (:sc ,complex-sc :target r
:from (:argument 0)
:to :result)
dup)
(:results (r :scs (,complex-sc)))
(:result-types ,complex-type)
(:vop-var vop)
(:generator ,cost
(let (first-value
(second-value r))
(if (sc-is x ,real-constant-sc)
(inst ,complex-move-inst dup
(register-inline-constant
(complex (setf first-value (tn-value x)) (tn-value x))))
(let ((real x))
(setf first-value x)
,duplicate-inst))
;; safe: dup /= y
(when (location= dup r)
(rotatef dup y)
(setf second-value dup))
(if (sc-is y ,complex-constant-sc)
(inst ,complex-move-inst r
(register-inline-constant (tn-value y)))
(move r y))
(note-float-location ',op vop first-value second-value)
(when (sc-is dup ,complex-constant-sc)
(setf dup (register-inline-constant
:aligned (tn-value dup))))
(inst ,op-inst r dup)))))
,(when complex-real-name
`(define-vop (,complex-real-name float-op)
(:translate ,op)
(:args (x :scs (,complex-sc ,complex-constant-sc)
:target r
:to :result
:load-if (not (sc-is x ,complex-constant-sc)))
(y :scs (,real-sc ,real-constant-sc)
:target dup
:load-if (not (sc-is y ,real-constant-sc))))
(:arg-types ,complex-type ,real-type)
(:temporary (:sc ,complex-sc :target r
:from (:argument 1)
:to :result)
dup)
(:results (r :scs (,complex-sc)))
(:result-types ,complex-type)
(:vop-var vop)
(:generator ,cost
(let ((first-value r)
second-value)
(if (sc-is y ,real-constant-sc)
(inst ,complex-move-inst dup
(register-inline-constant
(complex (setf second-value (tn-value y))
(tn-value y))))
(let ((real y))
(setf second-value y)
,duplicate-inst))
(when (location= dup r)
(rotatef x dup)
(setf first-value dup))
(if (sc-is x ,complex-constant-sc)
(inst ,complex-move-inst r
(register-inline-constant (tn-value x)))
(move r x))
(note-float-location ',op vop first-value second-value)
(when (sc-is dup ,complex-constant-sc)
(setf dup (register-inline-constant
:aligned (tn-value dup))))
(inst ,op-inst r dup)))))))
(t ; duplicate, not commutative
`(progn
,(when real-complex-name
`(define-vop (,real-complex-name float-op)
(:translate ,op)
(:args (x :scs (,real-sc ,real-constant-sc)
:target r
:load-if (not (sc-is x ,real-constant-sc)))
(y :scs (,complex-sc ,complex-constant-sc)
:to :result
:load-if (not (sc-is y ,complex-constant-sc))))
(:arg-types ,real-type ,complex-type)
(:results (r :scs (,complex-sc) :from (:argument 0)))
(:result-types ,complex-type)
(:vop-var vop)
(:generator ,cost
(if (sc-is x ,real-constant-sc)
(inst ,complex-move-inst dup
(register-inline-constant
(complex (tn-value x) (tn-value x))))
(let ((real x)
(dup r))
,duplicate-inst))
(note-float-location ',op vop r y)
(when (sc-is y ,complex-constant-sc)
(setf y (register-inline-constant
:aligned (tn-value y))))
(inst ,op-inst r y))))
,(when complex-real-name
`(define-vop (,complex-real-name float-op)
(:translate ,op)
(:args (x :scs (,complex-sc)
:target r
:to :eval)
(y :scs (,real-sc ,real-constant-sc)
:target dup
:load-if (not (sc-is y ,complex-constant-sc))))
(:arg-types ,complex-type ,real-type)
(:temporary (:sc ,complex-sc :from (:argument 1))
dup)
(:results (r :scs (,complex-sc) :from :eval))
(:result-types ,complex-type)
(:vop-var vop)
(:generator ,cost
(let (second-value)
(if (sc-is y ,real-constant-sc)
(setf dup (register-inline-constant
:aligned (complex (setf second-value (tn-value y))
(tn-value y))))
(let ((real y))
(setf second-value y)
,duplicate-inst))
(move r x)
(note-float-location ',op vop r second-value)
(inst ,op-inst r dup)))))))))
(def-real-complex-op (op commutativep duplicatep
single-inst single-real-complex-name single-complex-real-name single-cost
double-inst double-real-complex-name double-complex-real-name double-cost)
`(progn
(frob ,op ,single-cost ,commutativep
,(and duplicatep
`(progn
(move dup real)
(inst unpcklps dup dup)))
,single-inst movss movq
single-reg fp-single-immediate single-float
complex-single-reg fp-complex-single-immediate complex-single-float
,single-real-complex-name ,single-complex-real-name)
(frob ,op ,double-cost ,commutativep
,(and duplicatep
`(progn
(move dup real)
(inst unpcklpd dup dup)))
,double-inst movsd movapd
double-reg fp-double-immediate double-float
complex-double-reg fp-complex-double-immediate complex-double-float
,double-real-complex-name ,double-complex-real-name))))
(def-real-complex-op + t nil
addps +/real-complex-single-float +/complex-real-single-float 3
addpd +/real-complex-double-float +/complex-real-double-float 4)
(def-real-complex-op - nil nil
subps -/real-complex-single-float -/complex-real-single-float 3
subpd -/real-complex-double-float -/complex-real-double-float 4)
(def-real-complex-op * t t
mulps */real-complex-single-float */complex-real-single-float 4
mulpd */real-complex-double-float */complex-real-double-float 5)
(def-real-complex-op / nil t
nil nil nil nil
divpd nil //complex-real-double-float 19))
(define-vop (//complex-real-single-float float-op)
(:translate /)
(:args (x :scs (complex-single-reg fp-complex-single-immediate fp-complex-single-zero)
:to (:result 0)
:target r
:load-if (not (sc-is x fp-complex-single-immediate fp-complex-single-zero)))
(y :scs (single-reg fp-single-immediate fp-single-zero)
:target dup
:load-if (not (sc-is y fp-single-immediate fp-single-zero))))
(:arg-types complex-single-float single-float)
(:temporary (:sc complex-single-reg :from (:argument 1)) dup)
(:results (r :scs (complex-single-reg)))
(:result-types complex-single-float)
(:vop-var vop)
(:generator 12
(let ((second-value dup)
(first-value r))
(flet ((duplicate (x)
(let ((word (ldb (byte 64 0)
(logior (ash (single-float-bits (imagpart x)) 32)
(ldb (byte 32 0)
(single-float-bits (realpart x)))))))
(register-inline-constant :oword (logior (ash word 64) word)))))
(sc-case y
(fp-single-immediate
(setf dup (duplicate (complex (setf second-value (tn-value y))
(tn-value y)))))
(fp-single-zero
(inst xorps dup dup))
(t (move dup y)
(setf second-value y)
(inst shufps dup dup #b00000000)))
(sc-case x
(fp-complex-single-immediate
(inst movaps r (duplicate (setf first-value (tn-value x)))))
(fp-complex-single-zero
(inst xorps r r))
(t
(move r x)
(setf first-value x)
(inst unpcklpd r r)))
(note-float-location '/ vop first-value second-value)
(inst divps r dup)
(inst movq r r)))))
;; Complex multiplication
;; r := rx * ry - ix * iy
;; i := rx * iy + ix * ry
;;
;; Transpose for SIMDness
;; rx*ry rx*iy
;; -ix*iy +ix*ry
;;
;; [rx rx] * [ry iy]
;;+ [ix ix] * [-iy ry]
;; [r i]
(macrolet ((define-complex-* (name cost type sc tmp-p &body body)
`(define-vop (,name float-op)
(:translate *)
(:args (x :scs (,sc) :target r)
(y :scs (,sc) :target copy-y))
(:arg-types ,type ,type)
(:temporary (:sc ,sc) imag)
(:temporary (:sc ,sc :from :eval) copy-y)
,@(when tmp-p
`((:temporary (:sc ,sc) xmm)))
(:results (r :scs (,sc) :from :eval))
(:result-types ,type)
(:vop-var vop)
(:generator ,cost
(when (or (location= x copy-y)
(location= y r))
(rotatef x y))
,@body))))
(define-complex-* */complex-single-float 20
complex-single-float complex-single-reg t
(inst xorps xmm xmm)
(move r x)
(move copy-y y) ; y == r only if y == x == r
(setf y copy-y)
(inst unpcklps r r)
(move imag r)
(inst unpckhpd imag xmm)
(inst unpcklpd r xmm)
(note-float-location '* vop r y)
(inst mulps r y)
(inst shufps y y #b11110001)
(inst xorps y (register-inline-constant :oword (ash 1 31)))
(inst mulps imag y)
(inst addps r imag))
(define-complex-* */complex-double-float 25
complex-double-float complex-double-reg nil
(move imag x)
(move r x)
(move copy-y y)
(setf y copy-y)
(inst unpcklpd r r)
(inst unpckhpd imag imag)
(note-float-location '* vop r y)
(inst mulpd r y)
(inst shufpd y y #b01)
(inst xorpd y (register-inline-constant :oword (ash 1 63)))
(inst mulpd imag y)
(inst addpd r imag)))
(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 "inline float arithmetic")
(:vop-var vop)
(:save-p :compute-only)
(:generator 1
(unless (location= x y)
(inst xorpd y y))
(note-float-location 'sqrt vop x)
(inst sqrtsd y x)))
(macrolet ((frob ((name translate sc type) &body body)
`(define-vop (,name)
(:args (x :scs (,sc) :target y))
(:results (y :scs (,sc)))
(:translate ,translate)
(:policy :fast-safe)
(:arg-types ,type)
(:result-types ,type)
(:note "inline float arithmetic")
(:vop-var vop)
(:save-p :compute-only)
(:generator 1
(move y x)
(note-float-location ',translate vop y)
,@body))))
(frob (%negate/double-float %negate double-reg double-float)
(inst xorpd y (register-inline-constant :oword (ash 1 63))))
(frob (%negate/complex-double-float %negate complex-double-reg complex-double-float)
(inst xorpd y (register-inline-constant
:oword (logior (ash 1 127) (ash 1 63)))))
(frob (conjugate/complex-double-float conjugate complex-double-reg complex-double-float)
(inst xorpd y (register-inline-constant :oword (ash 1 127))))
(frob (%negate/single-float %negate single-reg single-float)
(inst xorps y (register-inline-constant :oword (ash 1 31))))
(frob (%negate/complex-single-float %negate complex-single-reg complex-single-float)
(inst xorps y (register-inline-constant
:oword (logior (ash 1 31) (ash 1 63)))))
(frob (conjugate/complex-single-float conjugate complex-single-reg complex-single-float)
(inst xorpd y (register-inline-constant :oword (ash 1 63))))
(frob (abs/double-float abs double-reg double-float)
(inst andpd y (register-inline-constant :oword (ldb (byte 63 0) -1))))
(frob (abs/single-float abs single-reg single-float)
(inst andps y (register-inline-constant :oword (ldb (byte 31 0) -1)))))
;;;; comparison
(define-vop (float-compare)
(:policy :fast-safe)
(:vop-var vop)
(:save-p :compute-only)
(:note "inline float comparison"))
;;; EQL
(macrolet ((define-float-eql (name cost sc constant-sc type)
`(define-vop (,name float-compare)
(:translate eql)
(:args (x :scs (,sc)
:target mask)
(y :scs (,sc ,constant-sc)
:target mask))
(:arg-types ,type ,type)
(:temporary (:sc ,sc :from :eval) mask)
(:temporary (:sc unsigned-reg) bits)
(:conditional :e)
(:generator ,cost
(when (location= y mask)
(rotatef x y))
(move mask x)
(when (sc-is y ,constant-sc)
(setf y (register-inline-constant :aligned (tn-value y))))
(inst pcmpeqd mask y)
(inst movmskps bits mask)
(inst cmp :byte bits #b1111)))))
(define-float-eql eql/single-float 4
single-reg fp-single-immediate single-float)
(define-float-eql eql/double-float 4
double-reg fp-double-immediate double-float)
(define-float-eql eql/complex-single-float 5
complex-single-reg fp-complex-single-immediate complex-single-float)
(define-float-eql eql/complex-double-float 5
complex-double-reg fp-complex-double-immediate complex-double-float))
(define-vop (generic-eq/single-float/c float-compare)
(:translate eq)
(:args (x :scs (any-reg descriptor-reg)))
(:info y)
(:arg-types * (:constant single-float))
(:conditional :e)
(:generator 3
(inst cmp x (constantize (dpb (single-float-bits y) (byte 32 32)
single-float-widetag)))))
;;; 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 single-stack fp-single-immediate)
:load-if (not (sc-is y single-stack fp-single-immediate))))
(:arg-types single-float single-float))
(define-vop (double-float-compare float-compare)
(:args (x :scs (double-reg))
(y :scs (double-reg double-stack descriptor-reg fp-double-immediate)
:load-if (not (sc-is y double-stack descriptor-reg fp-double-immediate))))
(:arg-types double-float double-float))
(define-vop (=/single-float single-float-compare)
(:translate =)
(:args (x :scs (single-reg single-stack fp-single-immediate)
:target xmm
:load-if (not (sc-is x single-stack fp-single-immediate)))
(y :scs (single-reg single-stack fp-single-immediate)
:target xmm
:load-if (not (sc-is y single-stack fp-single-immediate))))
(:temporary (:sc single-reg :from :eval) xmm)
(:conditional not :p :ne)
(:vop-var vop)
(:generator 3
(when (or (location= y xmm)
(and (not (xmm-tn-p x)) (xmm-tn-p y)))
(rotatef x y))
(sc-case x
(single-reg (setf xmm x))
(single-stack (inst movss xmm (ea-for-sf-stack x)))
(fp-single-immediate
(inst movss xmm (register-inline-constant (tn-value x)))))
(note-float-location '= vop xmm y)
(sc-case y
(single-stack
(setf y (ea-for-sf-stack y)))
(fp-single-immediate
(setf y (register-inline-constant (tn-value y))))
(t))
(inst comiss xmm y)
;; if PF&CF, there was a NaN involved => not equal
;; otherwise, ZF => equal
))
(define-vop (=/double-float double-float-compare)
(:translate =)
(:args (x :scs (double-reg double-stack fp-double-immediate descriptor-reg)
:target xmm
:load-if (not (sc-is x double-stack fp-double-immediate descriptor-reg)))
(y :scs (double-reg double-stack fp-double-immediate descriptor-reg)
:target xmm
:load-if (not (sc-is y double-stack fp-double-immediate descriptor-reg))))
(:temporary (:sc double-reg :from :eval) xmm)
(:conditional not :p :ne)
(:vop-var vop)
(:generator 3
(when (or (location= y xmm)
(and (not (xmm-tn-p x)) (xmm-tn-p y)))