[pro] Looking for Sign-extend elegance

David McClain dbm at refined-audiometrics.com
Wed Sep 8 18:16:50 UTC 2010


I found one way...

(defun read-int64 (ptr)
   (let ((v (read-uint64 ptr)))
     (declare (type (unsigned-byte 64) v))
     (if (logbitp 63 v)
         (dpb v (byte 64 0) -1) ;; <-- better?
       ;; else
       v))

Dr. David McClain
Chief Technical Officer
Refined Audiometrics Laboratory
4391 N. Camino Ferreo
Tucson, AZ  85750

email: dbm at refined-audiometrics.com
phone: 1.520.390.3995
web: http://refined-audiometrics.com



On Sep 8, 2010, at 10:18, David McClain wrote:

> I have a 64-bit integer obtained by the FLI from external storage.  
> It was obtained by reading two 32-bit unsigned values and  
> depositing them into a zero valued integer. Now I want to sign- 
> extend the result so that if it has its MSB set, the value will be  
> a twos-complement negative value.
>
> (defun read-int64 (ptr)
>   (let ((v (read-uint64 ptr)))
>     (if (logbitp 63 v)
>         (- v #.(ash 1 64))
>       v)))
>
> But this looks inelegant to me, requiring the storage of a constant  
> #.(ash 1 64) used in a subtraction operation.
>
> Been racking my brain on the BOOLE operations and the LOGNOT et al,  
> looking for a more elegant solution to this. Not a high-value item,  
> just curious.
>
> Dr. David McClain
> Chief Technical Officer
> Refined Audiometrics Laboratory
> 4391 N. Camino Ferreo
> Tucson, AZ  85750
>
> email: dbm at refined-audiometrics.com
> phone: 1.520.390.3995
> web: http://www.refined-audiometrics.com
>
>
>
> _______________________________________________
> pro mailing list
> pro at common-lisp.net
> http://common-lisp.net/cgi-bin/mailman/listinfo/pro

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/pro/attachments/20100908/31e98e6e/attachment.html>


More information about the pro mailing list