1 ;;;; stuff to handle simple cases for generic arithmetic
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 (define-assembly-routine (generic-+
16 (:return-style :full-call)
20 ((:arg x (descriptor-reg any-reg) a0-offset)
21 (:arg y (descriptor-reg any-reg) a1-offset)
23 (:res res (descriptor-reg any-reg) a0-offset)
25 (:temp temp non-descriptor-reg nl0-offset)
26 (:temp temp2 non-descriptor-reg nl1-offset)
27 (:temp temp3 non-descriptor-reg nl2-offset)
28 (:temp lip interior-reg lip-offset)
29 (:temp lra descriptor-reg lra-offset)
30 (:temp nargs any-reg nargs-offset)
31 (:temp ocfp any-reg ocfp-offset))
33 (inst bne temp DO-STATIC-FUN)
35 (inst bne temp DO-STATIC-FUN)
38 ; Check whether we need a bignum.
39 (inst sra res 31 temp)
43 (inst sra res 2 temp3)
45 ; from move-from-signed
47 (inst sra temp3 31 temp)
48 (inst cmoveq temp 1 temp2)
50 (inst cmoveq temp 1 temp2)
51 (inst sll temp2 type-bits temp2)
52 (inst bis temp2 bignum-type temp2)
54 (pseudo-atomic (:extra (pad-data-block (+ bignum-digits-offset 3)))
55 (inst bis alloc-tn other-pointer-type res)
56 (storew temp2 res 0 other-pointer-type)
57 (storew temp3 res bignum-digits-offset other-pointer-type)
58 (inst srl temp3 32 temp)
59 (storew temp res (1+ bignum-digits-offset) other-pointer-type))
61 (lisp-return lra lip :offset 2)
64 (inst ldl lip (static-function-offset 'two-arg-+) null-tn)
65 (inst li (fixnumize 2) nargs)
66 (inst move cfp-tn ocfp)
67 (inst move csp-tn cfp-tn)
68 (inst jmp zero-tn lip))
71 (define-assembly-routine (generic--
73 (:return-style :full-call)
77 ((:arg x (descriptor-reg any-reg) a0-offset)
78 (:arg y (descriptor-reg any-reg) a1-offset)
80 (:res res (descriptor-reg any-reg) a0-offset)
82 (:temp temp non-descriptor-reg nl0-offset)
83 (:temp temp2 non-descriptor-reg nl1-offset)
84 (:temp temp3 non-descriptor-reg nl2-offset)
85 (:temp lip interior-reg lip-offset)
86 (:temp lra descriptor-reg lra-offset)
87 (:temp nargs any-reg nargs-offset)
88 (:temp ocfp any-reg ocfp-offset))
90 (inst bne temp DO-STATIC-FUN)
92 (inst bne temp DO-STATIC-FUN)
95 ; Check whether we need a bignum.
96 (inst sra res 31 temp)
100 (inst sra res 2 temp3)
102 ; from move-from-signed
104 (inst sra temp3 31 temp)
105 (inst cmoveq temp 1 temp2)
107 (inst cmoveq temp 1 temp2)
108 (inst sll temp2 type-bits temp2)
109 (inst bis temp2 bignum-type temp2)
111 (pseudo-atomic (:extra (pad-data-block (+ bignum-digits-offset 3)))
112 (inst bis alloc-tn other-pointer-type res)
113 (storew temp2 res 0 other-pointer-type)
114 (storew temp3 res bignum-digits-offset other-pointer-type)
115 (inst srl temp3 32 temp)
116 (storew temp res (1+ bignum-digits-offset) other-pointer-type))
118 (lisp-return lra lip :offset 2)
121 (inst ldl lip (static-function-offset 'two-arg--) null-tn)
122 (inst li (fixnumize 2) nargs)
123 (inst move cfp-tn ocfp)
124 (inst move csp-tn cfp-tn)
125 (inst jmp zero-tn lip))
128 (define-assembly-routine (generic-*
130 (:return-style :full-call)
134 ((:arg x (descriptor-reg any-reg) a0-offset)
135 (:arg y (descriptor-reg any-reg) a1-offset)
137 (:res res (descriptor-reg any-reg) a0-offset)
139 (:temp temp non-descriptor-reg nl0-offset)
140 (:temp lo non-descriptor-reg nl1-offset)
141 (:temp hi non-descriptor-reg nl2-offset)
142 (:temp temp2 non-descriptor-reg nl3-offset)
143 (:temp lip interior-reg lip-offset)
144 (:temp lra descriptor-reg lra-offset)
145 (:temp nargs any-reg nargs-offset)
146 (:temp ocfp any-reg ocfp-offset))
147 ;; If either arg is not a fixnum, call the static function.
149 (inst bne temp DO-STATIC-FUN)
151 (inst bne temp DO-STATIC-FUN)
153 ;; Remove the tag from one arg so that the result will have the
154 ;; correct fixnum tag.
156 (inst mulq temp y lo)
159 (inst sra res 32 res)
160 ;; Check to see if the result will fit in a fixnum. (I.e. the high
161 ;; word is just 32 copies of the sign bit of the low word).
162 (inst sra res 31 temp)
163 (inst xor hi temp temp)
165 ;; Shift the double word hi:res down two bits into hi:low to get rid
166 ;; of the fixnum tag.
170 ;; Do we need one word or two? Assume two.
171 (inst li (logior (ash 2 type-bits) bignum-type) temp2)
172 (inst sra lo 31 temp)
173 (inst xor temp hi temp)
174 (inst bne temp two-words)
176 ;; Only need one word, fix the header.
177 (inst li (logior (ash 1 type-bits) bignum-type) temp2)
178 ;; Allocate one word.
179 (pseudo-atomic (:extra (pad-data-block (1+ bignum-digits-offset)))
180 (inst bis alloc-tn other-pointer-type res)
181 (storew temp2 res 0 other-pointer-type))
183 (storew lo res bignum-digits-offset other-pointer-type)
185 (lisp-return lra lip :offset 2)
188 ;; Allocate two words.
189 (pseudo-atomic (:extra (pad-data-block (+ 2 bignum-digits-offset)))
190 (inst bis alloc-tn other-pointer-type res)
191 (storew temp2 res 0 other-pointer-type))
193 (storew lo res bignum-digits-offset other-pointer-type)
194 (storew hi res (1+ bignum-digits-offset) other-pointer-type)
196 (lisp-return lra lip :offset 2)
199 (inst ldl lip (static-function-offset 'two-arg-*) null-tn)
200 (inst li (fixnumize 2) nargs)
201 (inst move cfp-tn ocfp)
202 (inst move csp-tn cfp-tn)
203 (inst jmp zero-tn lip)
210 (define-assembly-routine (signed-truncate
211 (:note "(signed-byte 32) truncate")
214 (:translate truncate)
215 (:arg-types signed-num signed-num)
216 (:result-types signed-num signed-num))
218 ((:arg dividend signed-reg nl0-offset)
219 (:arg divisor signed-reg nl1-offset)
221 (:res quo signed-reg nl2-offset)
222 (:res rem signed-reg nl3-offset)
224 (:temp quo-sign signed-reg nl5-offset)
225 (:temp rem-sign signed-reg nargs-offset)
226 (:temp temp1 non-descriptor-reg nl4-offset))
228 (let ((error (generate-error-code nil division-by-zero-error
230 (inst beq divisor error))
232 (inst xor dividend divisor quo-sign)
233 (inst move dividend rem-sign)
234 (let ((label (gen-label)))
235 (inst bge dividend label)
236 (inst subq zero-tn dividend dividend)
238 (let ((label (gen-label)))
239 (inst bge divisor label)
240 (inst subq zero-tn divisor divisor)
242 (inst move zero-tn rem)
243 (inst move zero-tn quo)
244 (inst sll dividend 32 dividend)
247 (inst srl dividend 63 temp1)
249 (inst bis temp1 rem rem)
250 (inst cmple divisor rem temp1)
252 (inst bis temp1 quo quo)
253 (inst sll dividend 1 dividend)
254 (inst subq temp1 1 temp1)
255 (inst zap divisor temp1 temp1)
256 (inst subq rem temp1 rem))
258 (let ((label (gen-label)))
259 ;; If the quo-sign is negative, we need to negate quo.
260 (inst bge quo-sign label)
261 (inst subq zero-tn quo quo)
263 (let ((label (gen-label)))
264 ;; If the rem-sign is negative, we need to negate rem.
265 (inst bge rem-sign label)
266 (inst subq zero-tn rem rem)
270 ;;;; comparison routines
273 ((define-cond-assem-rtn (name translate static-fn cmp not-p)
274 `(define-assembly-routine (,name
276 (:return-style :full-call)
278 (:translate ,translate)
280 ((:arg x (descriptor-reg any-reg) a0-offset)
281 (:arg y (descriptor-reg any-reg) a1-offset)
283 (:res res descriptor-reg a0-offset)
285 (:temp temp non-descriptor-reg nl0-offset)
286 (:temp lip interior-reg lip-offset)
287 (:temp nargs any-reg nargs-offset)
288 (:temp ocfp any-reg ocfp-offset))
290 (inst bne temp DO-STATIC-FN)
292 (inst beq temp DO-COMPARE)
295 (inst ldl lip (static-function-offset ',static-fn) null-tn)
296 (inst li (fixnumize 2) nargs)
297 (inst move cfp-tn ocfp)
298 (inst move csp-tn cfp-tn)
299 (inst jmp zero-tn lip)
303 (inst move null-tn res)
304 (inst ,(if not-p 'bne 'beq) temp done)
308 (define-cond-assem-rtn generic-< < two-arg-< (inst cmplt x y temp) nil)
309 (define-cond-assem-rtn generic-> > two-arg-> (inst cmplt y x temp) nil))
312 (define-assembly-routine (generic-eql
314 (:return-style :full-call)
318 ((:arg x (descriptor-reg any-reg) a0-offset)
319 (:arg y (descriptor-reg any-reg) a1-offset)
321 (:res res descriptor-reg a0-offset)
323 (:temp temp non-descriptor-reg nl0-offset)
324 (:temp lip interior-reg lip-offset)
325 (:temp lra descriptor-reg lra-offset)
326 (:temp nargs any-reg nargs-offset)
327 (:temp ocfp any-reg ocfp-offset))
328 (inst cmpeq x y temp)
329 (inst bne temp RETURN-T)
331 (inst beq temp RETURN-NIL)
333 (inst bne temp DO-STATIC-FN)
336 (inst move null-tn res)
337 (lisp-return lra lip :offset 2)
340 (inst ldl lip (static-function-offset 'eql) null-tn)
341 (inst li (fixnumize 2) nargs)
342 (inst move cfp-tn ocfp)
343 (inst move csp-tn cfp-tn)
344 (inst jmp zero-tn lip)
349 (define-assembly-routine (generic-=
351 (:return-style :full-call)
355 ((:arg x (descriptor-reg any-reg) a0-offset)
356 (:arg y (descriptor-reg any-reg) a1-offset)
358 (:res res descriptor-reg a0-offset)
360 (:temp temp non-descriptor-reg nl0-offset)
361 (:temp lip interior-reg lip-offset)
362 (:temp lra descriptor-reg lra-offset)
363 (:temp nargs any-reg nargs-offset)
364 (:temp ocfp any-reg ocfp-offset))
366 (inst bne temp DO-STATIC-FN)
368 (inst bne temp DO-STATIC-FN)
369 (inst cmpeq x y temp)
370 (inst bne temp RETURN-T)
372 (inst move null-tn res)
373 (lisp-return lra lip :offset 2)
376 (inst ldl lip (static-function-offset 'two-arg-=) null-tn)
377 (inst li (fixnumize 2) nargs)
378 (inst move cfp-tn ocfp)
379 (inst move csp-tn cfp-tn)
380 (inst jmp zero-tn lip)
385 (define-assembly-routine (generic-/=
387 (:return-style :full-call)
391 ((:arg x (descriptor-reg any-reg) a0-offset)
392 (:arg y (descriptor-reg any-reg) a1-offset)
394 (:res res descriptor-reg a0-offset)
396 (:temp temp non-descriptor-reg nl0-offset)
397 (:temp lip interior-reg lip-offset)
398 (:temp lra descriptor-reg lra-offset)
399 (:temp nargs any-reg nargs-offset)
400 (:temp ocfp any-reg ocfp-offset))
402 (inst bne temp DO-STATIC-FN)
404 (inst bne temp DO-STATIC-FN)
405 (inst cmpeq x y temp)
406 (inst bne temp RETURN-NIL)
409 (lisp-return lra lip :offset 2)
412 (inst ldl lip (static-function-offset 'two-arg-=) null-tn)
413 (inst li (fixnumize 2) nargs)
414 (inst move cfp-tn ocfp)
415 (inst move csp-tn cfp-tn)
416 (inst jmp zero-tn lip)
419 (inst move null-tn res))