[Git][cmucl/cmucl][issue-242-c-call-char-result-wrong] Handle the signed integer case for alien integers

Raymond Toy (@rtoy) gitlab at common-lisp.net
Thu Jun 15 22:54:38 UTC 2023



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/d5391d9a0e25c687937e86fc16e67127671896e1

-- 
View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/commit/d5391d9a0e25c687937e86fc16e67127671896e1
You're receiving this email because of your account on gitlab.common-lisp.net.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cmucl-cvs/attachments/20230615/b073a6e8/attachment-0001.html>


More information about the cmucl-cvs mailing list