/
arith.lisp
370 lines (318 loc) · 13.1 KB
/
arith.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
;;;; simple cases for generic arithmetic
;;;; 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")
;;;; addition, subtraction, and multiplication
(macrolet ((define-generic-arith-routine ((fun cost) &body body)
`(define-assembly-routine (,(symbolicate "GENERIC-" fun)
(:cost ,cost)
(:return-style :full-call-no-return)
(:translate ,fun)
(:policy :safe)
(:save-p t))
((:arg x (descriptor-reg any-reg) edx-offset)
(:arg y (descriptor-reg any-reg) edi-offset)
(:res res (descriptor-reg any-reg) edx-offset)
,@(if (eq fun '*)
'((:temp eax unsigned-reg eax-offset)))
(:temp ecx unsigned-reg ecx-offset))
,@(multiple-value-bind (reg byte)
(if (eq fun '*) (values 'eax 'al-tn) (values 'ecx 'cl-tn))
`((inst mov ,reg x)
(inst or ,reg y)
(inst test ,byte fixnum-tag-mask))) ; both fixnums?
(inst jmp :nz DO-STATIC-FUN) ; no - do generic
,@body
(inst clc) ; single-value return
(inst ret)
DO-STATIC-FUN
;; Same as: (inst enter (fixnumize 1))
(inst push ebp-tn)
(inst mov ebp-tn esp-tn)
(inst sub esp-tn (fixnumize 1))
(inst push (make-ea :dword :base ebp-tn
:disp (frame-byte-offset return-pc-save-offset)))
(inst mov ecx (fixnumize 2)) ; arg count
(inst jmp
(make-ea :dword
:disp (+ nil-value
(static-fun-offset
',(symbolicate "TWO-ARG-" fun))))))))
(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 ecx res)
(alloc-other res bignum-widetag (1+ bignum-digits-offset) nil)
(storew ecx res bignum-digits-offset other-pointer-lowtag)
OKAY)
(define-generic-arith-routine (- 10)
(move res x)
(inst sub res y)
(inst jmp :no OKAY)
(inst cmc) ; carry has correct sign now
(inst rcr res 1)
(inst sar res 1) ; remove type bits
(move ecx res)
(alloc-other res bignum-widetag (1+ bignum-digits-offset) nil)
(storew ecx res bignum-digits-offset other-pointer-lowtag)
OKAY)
(define-generic-arith-routine (* 30)
(move eax x) ; must use eax for 64-bit result
(inst sar eax n-fixnum-tag-bits) ; remove *4 fixnum bias
(inst imul y) ; result in edx:eax
(inst jmp :no OKAY) ; still fixnum
;; zzz jrd changed edx to ebx in here, as edx isn't listed as a temp, above
;; pfw says that loses big -- edx is target for arg x and result res
;; note that 'edx' is not defined -- using x
(inst shrd eax x n-fixnum-tag-bits) ; high bits from edx
(inst sar x n-fixnum-tag-bits) ; now shift edx too
(move ecx x) ; save high bits from cdq
(inst cdq) ; edx:eax <- sign-extend of eax
(inst cmp x ecx)
(inst jmp :e SINGLE-WORD-BIGNUM)
(alloc-other res bignum-widetag (+ bignum-digits-offset 2) nil)
(storew eax res bignum-digits-offset other-pointer-lowtag)
(storew ecx res (1+ bignum-digits-offset) other-pointer-lowtag)
(inst jmp DONE)
SINGLE-WORD-BIGNUM
(alloc-other res bignum-widetag (1+ bignum-digits-offset) nil)
(storew eax res bignum-digits-offset other-pointer-lowtag)
(inst jmp DONE)
OKAY
(move res eax)
DONE))
;;;; negation
(define-assembly-routine (generic-negate
(:cost 10)
(:return-style :full-call)
(:policy :safe)
(:translate %negate)
(:save-p t))
((:arg x (descriptor-reg any-reg) edx-offset)
(:res res (descriptor-reg any-reg) edx-offset)
(:temp ecx unsigned-reg ecx-offset))
(inst test x fixnum-tag-mask)
(inst jmp :z FIXNUM)
(inst push ebp-tn)
(inst mov ebp-tn esp-tn)
(inst sub esp-tn (fixnumize 1))
(inst push (make-ea :dword :base ebp-tn
:disp (frame-byte-offset return-pc-save-offset)))
(inst mov ecx (fixnumize 1)) ; arg count
(inst jmp (make-ea :dword
:disp (+ nil-value (static-fun-offset '%negate))))
FIXNUM
(move res x)
(inst neg res) ; (- most-negative-fixnum) is BIGNUM
(inst jmp :no OKAY)
(inst shr res n-fixnum-tag-bits) ; sign bit is data - remove type bits
(move ecx res)
(alloc-other res bignum-widetag (1+ bignum-digits-offset) nil)
(storew ecx res bignum-digits-offset other-pointer-lowtag)
OKAY)
;;;; comparison
(macrolet ((define-cond-assem-rtn (name translate static-fn test)
`(define-assembly-routine (,name
(:translate ,translate)
(:policy :safe)
(:save-p t)
(:conditional ,test)
(:cost 10))
((:arg x (descriptor-reg any-reg) edx-offset)
(:arg y (descriptor-reg any-reg) edi-offset)
(:temp ecx unsigned-reg ecx-offset))
(inst mov ecx x)
(inst or ecx y)
(inst test ecx fixnum-tag-mask)
(inst jmp :nz DO-STATIC-FUN) ; are both fixnums?
(inst cmp x y)
(inst ret)
DO-STATIC-FUN
(inst push ebp-tn)
(inst mov ebp-tn esp-tn)
(inst sub esp-tn (fixnumize 3))
(inst mov (make-ea :dword :base esp-tn
:disp (frame-byte-offset
(+ sp->fp-offset
-3
ocfp-save-offset)))
ebp-tn)
(inst lea ebp-tn (make-ea :dword :base esp-tn
:disp (frame-byte-offset
(+ sp->fp-offset
-3
ocfp-save-offset))))
(inst mov ecx (fixnumize 2))
(inst call (make-ea :dword
:disp (+ nil-value
(static-fun-offset ',static-fn))))
;; HACK: We depend on NIL having the lowest address of all
;; static symbols (including T)
,@(ecase test
(:l `((inst mov y (1+ nil-value))
(inst cmp y x)))
(:g `((inst cmp x (1+ nil-value)))))
(inst pop ebp-tn))))
(define-cond-assem-rtn generic-< < two-arg-< :l)
(define-cond-assem-rtn generic-> > two-arg-> :g))
(define-assembly-routine (generic-eql
(:translate eql)
(:policy :safe)
(:save-p t)
(:conditional :e)
(:cost 10))
((:arg x (descriptor-reg any-reg) edx-offset)
(:arg y (descriptor-reg any-reg) edi-offset)
(:temp ecx unsigned-reg ecx-offset))
(inst mov ecx x)
(inst and ecx y)
(inst and ecx lowtag-mask)
(inst cmp ecx other-pointer-lowtag)
(inst jmp :e DO-STATIC-FUN)
;; At least one fixnum
(inst cmp x y)
RET
(inst ret)
DO-STATIC-FUN
;; Might as well fast path that...
(inst cmp x y)
(inst jmp :e RET)
(inst push ebp-tn)
(inst mov ebp-tn esp-tn)
(inst sub esp-tn (fixnumize 3))
(inst mov (make-ea :dword :base esp-tn
:disp (frame-byte-offset
(+ sp->fp-offset
-3
ocfp-save-offset)))
ebp-tn)
(inst lea ebp-tn (make-ea :dword :base esp-tn
:disp (frame-byte-offset
(+ sp->fp-offset
-3
ocfp-save-offset))))
(inst mov ecx (fixnumize 2))
(inst call (make-ea :dword
:disp (+ nil-value (static-fun-offset 'eql))))
(load-symbol y t)
(inst cmp x y)
(inst pop ebp-tn))
(define-assembly-routine (generic-=
(:translate =)
(:policy :safe)
(:save-p t)
(:conditional :e)
(:cost 10))
((:arg x (descriptor-reg any-reg) edx-offset)
(:arg y (descriptor-reg any-reg) edi-offset)
(:temp ecx unsigned-reg ecx-offset))
(inst mov ecx x)
(inst or ecx y)
(inst test ecx fixnum-tag-mask)
(inst jmp :nz DO-STATIC-FUN)
;; Both fixnums
(inst cmp x y)
(inst ret)
DO-STATIC-FUN
(inst push ebp-tn)
(inst mov ebp-tn esp-tn)
(inst sub esp-tn (fixnumize 3))
(inst mov (make-ea :dword :base esp-tn
:disp (frame-byte-offset
(+ sp->fp-offset
-3
ocfp-save-offset)))
ebp-tn)
(inst lea ebp-tn (make-ea :dword :base esp-tn
:disp (frame-byte-offset
(+ sp->fp-offset
-3
ocfp-save-offset))))
(inst mov ecx (fixnumize 2))
(inst call (make-ea :dword
:disp (+ nil-value (static-fun-offset 'two-arg-=))))
(load-symbol y t)
(inst cmp x y)
(inst pop ebp-tn))
;;; 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 EAX register.
#+sb-assembling ; We don't want a vop for this one.
(define-assembly-routine
(random-mt19937-update)
((:temp state unsigned-reg eax-offset)
(:temp k unsigned-reg ebx-offset)
(:temp y unsigned-reg ecx-offset)
(:temp tmp unsigned-reg edx-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-for-vector-data state :index k :offset 3))
(inst mov tmp (make-ea-for-vector-data state :index k :offset (+ 1 3)))
(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-for-vector-data state :index k :offset (+ 397 3)))
(inst mov (make-ea-for-vector-data state :index k :offset 3) y)
(inst inc k)
(inst cmp k (- 624 397))
(inst jmp :b loop1)
LOOP2
(inst mov y (make-ea-for-vector-data state :index k :offset 3))
(inst mov tmp (make-ea-for-vector-data state :index k :offset (+ 1 3)))
(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-for-vector-data state :index k :offset (+ (- 397 624) 3)))
(inst mov (make-ea-for-vector-data state :index k :offset 3) y)
(inst inc k)
(inst cmp k (- 624 1))
(inst jmp :b loop2)
(inst mov y (make-ea-for-vector-data state :offset (+ (- 624 1) 3)))
(inst mov tmp (make-ea-for-vector-data state :offset (+ 0 3)))
(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-for-vector-data state :offset (+ (- 397 1) 3)))
(inst mov (make-ea-for-vector-data state :offset (+ (- 624 1) 3)) y)
;; Restore the temporary registers and return.
(inst pop tmp)
(inst pop y)
(inst pop k))