Raymond Toy pushed to branch issue-242-c-call-char-result-wrong at cmucl / cmucl
Commits: d5391d9a by Raymond Toy at 2023-06-15T15:53:13-07:00 Handle the signed integer case for alien integers
Mask out integer bits that we don't care about, and then sign-extend the result, as needed.
- - - - -
1 changed file:
- src/code/alieneval.lisp
Changes:
===================================== src/code/alieneval.lisp ===================================== @@ -648,10 +648,21 @@ (def-alien-type-method (integer :naturalize-gen) (type alien) ;; Mask out any unwanted bits. Important if the C code returns ;; values in %al, or %ax - (case (alien-integer-type-bits type) - (8 `(ldb (byte 8 0) ,alien)) - (16 `(ldb (byte 16 0) ,alien)) - (t alien))) + (if (alien-integer-type-signed type) + (case (alien-integer-type-bits type) + ;; First, get just the low part of the alien and then + ;; sign-extend it appropriately. + (8 `(let ((val (ldb (byte 8 0) ,alien))) + (if (> val #x7f) + (- val #x100)))) + (16 `(let ((val (ldb (byte 16 0) ,alien))) + (if (> val #x7fff) + (- val #x10000)))) + (t alien)) + (case (alien-integer-type-bits type) + (8 `(ldb (byte 8 0) ,alien)) + (16 `(ldb (byte 16 0) ,alien)) + (t alien))))
;; signed numbers <= 32 bits need to be sign extended. ;; I really should use the movsxd instruction, but I don't
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/d5391d9a0e25c687937e86fc...