Paste number 25289: Best way to uffi 128-bit integers?

Paste number 25289: Best way to uffi 128-bit integers?
Pasted by: chkno
When:2 years, 9 months ago
Share:Tweet this! | http://paste.lisp.org/+JIH
Channel:#lisp
Paste contents:
Raw Source | XML | Display As
; Is this the best way to pass 128-bit integers through the uffi?

(asdf:oos 'asdf:load-op 'uffi)

(defconstant int128_t '(:array :unsigned-int 4))

(defun bignum->int128 (bignum int128)
  (setf (uffi:deref-array int128 int128_t 0) (ldb (byte 32  0) bignum))
  (setf (uffi:deref-array int128 int128_t 1) (ldb (byte 32 32) bignum))
  (setf (uffi:deref-array int128 int128_t 2) (ldb (byte 32 64) bignum))
  (setf (uffi:deref-array int128 int128_t 3) (ldb (byte 32 96) bignum))
  int128)

(defun int128->bignum (int128)
  (dpb (uffi:deref-array int128 int128_t 3) (byte 32 96)
    (dpb (uffi:deref-array int128 int128_t 2) (byte 32 64)
      (dpb (uffi:deref-array int128 int128_t 1) (byte 32 32)
        (uffi:deref-array int128 int128_t 0)))))

(uffi:with-foreign-object (x int128_t)
  (int128->bignum (bignum->int128 1000000000000000000000000000000000000 x)))
 => 1000000000000000000000000000000000000

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.