arith.lisp 11.8 KB
Newer Older
cwang's avatar
cwang committed
1
2
3
4
5
6
7
8
9
;;; -*- 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
10
 "$Header: src/assembly/amd64/arith.lisp $")
cwang's avatar
cwang committed
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
;;;
;;; **********************************************************************
;;;
;;; Stuff to handle simple cases for generic arithmetic.
;;;
;;; Written by William Lott.
;;; Debugged by Paul Werkowski -- Spring/Summer 1995.
;;;
(in-package :amd64)

(eval-when (compile eval)


;;;; Addition, Subtraction, and Multiplication

(defmacro define-generic-arith-routine ((fun cost) &body body)
  `(define-assembly-routine (,(symbolicate "GENERIC-" fun)
			     (:cost ,cost)
			     (:return-style :full-call)
			     (:translate ,fun)
			     (:policy :safe)
			     (:save-p t))
			    ((:arg x (descriptor-reg any-reg) rdx-offset)
			     (:arg y (descriptor-reg any-reg)
			      ;; this seems wrong esi-offset
			      rdi-offset)

			     (:res res (descriptor-reg any-reg) rdx-offset)

			     (:temp rax unsigned-reg rax-offset)
			     (:temp rbx unsigned-reg rbx-offset)
42
43
			     (:temp rcx unsigned-reg rcx-offset)
			     (:temp r11 unsigned-reg r11-offset))
cwang's avatar
cwang committed
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

    (inst test x 3)			; fixnum?
    (inst jmp :nz DO-STATIC-FUN)	; no - do generic
    (inst test y 3)			; fixnum?
    (inst jmp :z DO-BODY)		; yes - doit here

    DO-STATIC-FUN
    (inst pop rax)
    (inst push rbp-tn)
    (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp word-bytes))
    (inst sub rsp-tn (* 2 word-bytes))
    (inst push rax)			; callers return addr
    (inst mov rcx (fixnumize 2))	; arg count
    (inst mov rbx (+ nil-value (static-function-offset
				',(symbolicate "TWO-ARG-" fun))))
    (inst jmp (make-ea :qword :base rbx))
    
    DO-BODY
    ,@body))

); eval-when


(define-generic-arith-routine (+ 10)
  (move res x)
  (inst add res y)
  (inst jmp :no OKAY)
  (inst rcr res 1)			; carry has correct sign
  (inst sar res 1)			; remove type bits

  (move rcx res)

76
  (with-fixed-allocation (res bignum-type (1+ bignum-digits-offset) r11)
cwang's avatar
cwang committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    (storew rcx res bignum-digits-offset other-pointer-type))
  
  OKAY)


(define-generic-arith-routine (- 10)
    ;;; I can't figure out the flags on subtract. Overflow never gets
    ;;; set and carry always does. (- 0 most-negative-fixnum) can't be
    ;;; easily detected so just let the upper level stuff do it.
  (inst jmp DO-STATIC-FUN)

  ;; this is junk code. clean this up!!!
  (move res x)
  (inst sub res y)
  (inst jmp :no OKAY)
  (inst rcr res 1)
  (inst sar res 1)			; remove type bits
  
  (move rcx res)
  
97
  (with-fixed-allocation (res bignum-type (1+ bignum-digits-offset) r11)
cwang's avatar
cwang committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    (storew rcx res bignum-digits-offset other-pointer-type))
  OKAY)

(define-generic-arith-routine (* 30)
  (move rax x)				; must use rax for 64-bit result
  (inst sar rax 2)			; remove *4 fixnum bias
  (inst imul y)				; result in rdx:eax
  (inst jmp :no okay)			; still fixnum

  ;; zzz jrd changed rdx to rbx in here, as rdx isn't listed as a temp, above
  ;;     pfw says that loses big -- rdx is target for arg x and result res
  ;;     note that 'rdx' is not defined -- using x
  (inst shrd rax x 2)			; high bits from rdx
  (inst sar x 2)			; now shift rdx too

  (move rcx x)				; save high bits from cdq
  (inst cdq)				; rdx:eax <- sign-extend of rax
  (inst cmp x rcx)
  (inst jmp :e SINGLE-WORD-BIGNUM)

118
  (with-fixed-allocation (res bignum-type (+ bignum-digits-offset 2) r11)
cwang's avatar
cwang committed
119
120
121
122
123
124
    (storew rax res bignum-digits-offset other-pointer-type)
    (storew rcx res (1+ bignum-digits-offset) other-pointer-type))
  (inst jmp DONE)

  SINGLE-WORD-BIGNUM
  
125
  (with-fixed-allocation (res bignum-type (1+ bignum-digits-offset) r11)
cwang's avatar
cwang committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    (storew rax res bignum-digits-offset other-pointer-type))
  (inst jmp DONE)

  OKAY
  (move res rax)
  DONE)

(define-assembly-routine (generic-negate
			  (:cost 10)
			  (:return-style :full-call)
			  (:policy :safe)
			  (:translate %negate)
			  (:save-p t))
			 ((:arg x (descriptor-reg any-reg) rdx-offset)
			  (:res res (descriptor-reg any-reg) rdx-offset)

			  (:temp rax unsigned-reg rax-offset)
143
144
			  (:temp rcx unsigned-reg rcx-offset)
			  (:temp r11 unsigned-reg r11-offset))
cwang's avatar
cwang committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  (inst test x 3)
  (inst jmp :z FIXNUM)

  (inst pop rax)
  (inst push rbp-tn)
  (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp word-bytes))
  (inst sub rsp-tn (fixnumize 2))
  (inst push rax)
  (inst mov rcx (fixnumize 1))		; arg count
  (inst mov rax (+ nil-value (static-function-offset '%negate)))
  (inst jmp (make-ea :qword :base rax))
  
  FIXNUM
  (move res x)
  (inst neg res)			; (- most-negative-fixnum) is BIGNUM
  (inst jmp :no OKAY)
  (inst shr res 2)			; sign bit is data - remove type bits
  (move rcx res)

164
  (with-fixed-allocation (res bignum-type (1+ bignum-digits-offset) r11)
cwang's avatar
cwang committed
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
    (storew rcx res bignum-digits-offset other-pointer-type))
  
  OKAY)



;;;; Comparison routines.

(eval-when (compile eval)

(defmacro define-cond-assem-rtn (name translate static-fn test)
  `(define-assembly-routine (,name
			     (:cost 10)
			     (:return-style :full-call)
			     (:policy :safe)
			     (:translate ,translate)
			     (:save-p t))
			    ((:arg x (descriptor-reg any-reg) rdx-offset)
			     (:arg y (descriptor-reg any-reg) rdi-offset)
			     
			     (:res res descriptor-reg rdx-offset)

			     (:temp rax unsigned-reg rax-offset)
			     (:temp rcx unsigned-reg rcx-offset))
    (inst test x 3)
    (inst jmp :nz DO-STATIC-FN)
    (inst test y 3)
    (inst jmp :z DO-COMPARE)
    
    DO-STATIC-FN
    (inst pop rax)
    (inst push rbp-tn)
    (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp word-bytes))
    (inst sub rsp-tn (* 2 word-bytes))
    (inst push rax)
    (inst mov rcx (fixnumize 2))
    (inst mov rax (+ nil-value
		   (static-function-offset ',static-fn)))
    (inst jmp (make-ea :qword :base rax))
    
    DO-COMPARE
    (inst cmp x y)
    (inst jmp ,test TRUE)
    (inst mov res nil-value)
    (inst pop rax)
    (inst add rax 3) ; single value return
    (inst jmp rax)
    
    TRUE
    (load-symbol res t)))

); eval-when

(define-cond-assem-rtn generic-< < two-arg-< :l)
(define-cond-assem-rtn generic-> > two-arg-> :g)

(define-assembly-routine (generic-eql
			  (:cost 10)
			  (:return-style :full-call)
			  (:policy :safe)
			  (:translate eql)
			  (:save-p t))
			 ((:arg x (descriptor-reg any-reg) rdx-offset)
			  (:arg y (descriptor-reg any-reg) rdi-offset)
			  
			  (:res res descriptor-reg rdx-offset)
			  
			  (:temp rax unsigned-reg rax-offset)
			  (:temp rcx unsigned-reg rcx-offset))
  (inst cmp x y)
  (inst jmp :e RETURN-T)
  (inst test x 3)
  (inst jmp :z RETURN-NIL)
  (inst test y 3)
  (inst jmp :nz DO-STATIC-FN)

  RETURN-NIL
  (inst mov res nil-value)
  (inst pop rax)
  (inst add rax 3) ; single value return
  (inst jmp rax)

  DO-STATIC-FN
  (inst pop rax)
  (inst push rbp-tn)
  (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp word-bytes))
  (inst sub rsp-tn (fixnumize 2))
  (inst push rax) ; return address
  (inst mov rcx (fixnumize 2)) ; number of arguments
  (inst mov rax (+ nil-value (static-function-offset 'eql)))
  (inst jmp (make-ea :qword :base rax))
  
  RETURN-T
  (load-symbol res t))

(define-assembly-routine (generic-=
			  (:cost 10)
			  (:return-style :full-call)
			  (:policy :safe)
			  (:translate =)
			  (:save-p t))
			 ((:arg x (descriptor-reg any-reg) rdx-offset)
			  (:arg y (descriptor-reg any-reg) rdi-offset)
			  
			  (:res res descriptor-reg rdx-offset)
			  
			  (:temp rax unsigned-reg rax-offset)
			  (:temp rcx unsigned-reg rcx-offset)
			  )
  (inst test x 3)			; descriptor?
  (inst jmp :nz DO-STATIC-FN)		; yes do it here
  (inst test y 3)			; descriptor?
  (inst jmp :nz DO-STATIC-FN)
  (inst cmp x y)
  (inst jmp :e RETURN-T)		; ok

  (inst mov res nil-value)
  (inst pop rax)
  (inst add rax 3)
  (inst jmp rax)

  DO-STATIC-FN
  (inst pop rax)
  (inst push rbp-tn)
  (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp word-bytes))
  (inst sub rsp-tn (* 2 word-bytes))
  (inst push rax)
  (inst mov rcx (fixnumize 2))
  (inst mov rax (+ nil-value (static-function-offset 'two-arg-=)))
  (inst jmp (make-ea :qword :base rax))
  
  RETURN-T
  (load-symbol res t))


;;; Support for the Mersenne Twister, MT19937, random number generator
;;; due to Matsumoto and Nishimura.
;;;
;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
;;; 623-dimensionally equidistributed uniform pseudorandom number
;;; generator.", ACM Transactions on Modeling and Computer Simulation,
;;; 1997, to appear.
;;;
;;; State:
;;;  0-1:   Constant matrix A. [0, #x9908b0df] (not used here)
;;;  2:     Index; init. to 1.
;;;  3-626: State.
;;;

;;; This assembly routine is called from the inline VOP and updates
;;; the state vector with new random numbers. The state vector is
;;; passed in the rAX register.
;;;
#+assembler ; we don't want a vop for this one.
(define-assembly-routine
    (random-mt19937-update)
    ((:temp state unsigned-reg rax-offset)
     (:temp k unsigned-reg rbx-offset)
     (:temp y unsigned-reg rcx-offset)
     (:temp tmp unsigned-reg rdx-offset))

  ;; Save the temporary registers.
  (inst push k)
  (inst push y)
  (inst push tmp)

  ;; Generate a new set of results.
  (inst xor k k)
  LOOP1
  (inst mov y (make-ea :qword :base state :index k :scale 4
		       :disp (- (* (+ 3 vm:vector-data-offset) vm:word-bytes)
				vm:other-pointer-type)))
  (inst mov tmp (make-ea :qword :base state :index k :scale 4
			 :disp (- (* (+ 1 3 vm:vector-data-offset)
				     vm:word-bytes)
				  vm:other-pointer-type)))
  (inst and y #x80000000)
  (inst and tmp #x7fffffff)
  (inst or y tmp)
  (inst shr y 1)
  (inst jmp :nc skip1)
  (inst xor y #x9908b0df)
  SKIP1
  (inst xor y (make-ea :qword :base state :index k :scale 4
		       :disp (- (* (+ 397 3 vm:vector-data-offset)
				   vm:word-bytes)
				vm:other-pointer-type)))
  (inst mov (make-ea :qword :base state :index k :scale 4
		     :disp (- (* (+ 3 vm:vector-data-offset) vm:word-bytes)
			      vm:other-pointer-type))
	y)
  (inst inc k)
  (inst cmp k (- 624 397))
  (inst jmp :b loop1)
  LOOP2
  (inst mov y (make-ea :qword :base state :index k :scale 4
		       :disp (- (* (+ 3 vm:vector-data-offset) vm:word-bytes)
				vm:other-pointer-type)))
  (inst mov tmp (make-ea :qword :base state :index k :scale 4
			 :disp (- (* (+ 1 3 vm:vector-data-offset)
				     vm:word-bytes)
				  vm:other-pointer-type)))
  (inst and y #x80000000)
  (inst and tmp #x7fffffff)
  (inst or y tmp)
  (inst shr y 1)
  (inst jmp :nc skip2)
  (inst xor y #x9908b0df)
  SKIP2
  (inst xor y (make-ea :qword :base state :index k :scale 4
		       :disp (- (* (+ (- 397 624) 3 vm:vector-data-offset)
				   vm:word-bytes)
				vm:other-pointer-type)))
  (inst mov (make-ea :qword :base state :index k :scale 4
		     :disp (- (* (+ 3 vm:vector-data-offset) vm:word-bytes)
			      vm:other-pointer-type))
	y)
  (inst inc k)
  (inst cmp k (- 624 1))
  (inst jmp :b loop2)
  
  (inst mov y (make-ea :qword :base state
		       :disp (- (* (+ (- 624 1) 3 vm:vector-data-offset)
				   vm:word-bytes)
				vm:other-pointer-type)))
  (inst mov tmp (make-ea :qword :base state
			 :disp (- (* (+ 0 3 vm:vector-data-offset)
				     vm:word-bytes)
				  vm:other-pointer-type)))
  (inst and y #x80000000)
  (inst and tmp #x7fffffff)
  (inst or y tmp)
  (inst shr y 1)
  (inst jmp :nc skip3)
  (inst xor y #x9908b0df)
  SKIP3
  (inst xor y (make-ea :qword :base state
		       :disp (- (* (+ (- 397 1) 3 vm:vector-data-offset)
				   vm:word-bytes)
				vm:other-pointer-type)))
  (inst mov (make-ea :qword :base state
		     :disp (- (* (+ (- 624 1) 3 vm:vector-data-offset)
				 vm:word-bytes)
			      vm:other-pointer-type))
	y)

  ;; Restore the temporary registers and return.
  (inst pop tmp)
  (inst pop y)
  (inst pop k)
  (inst ret))