Lisp HUG Maillist Archive

Where can I find lispworks-float:single-float-bits?

Hello,

I am trying to load the CL-PROTOBUFS library from Quicklisp.
On LispWorks 7.1.2, compilation fails complaining that the lispworks-float package does not exist.
The function in question is the following:

(defun single-float-bits (x)
  (declare (type single-float x))
  #+abcl    (system:single-float-bits x)
  #+allegro (multiple-value-bind (high low)
                (excl:single-float-to-shorts x)
              (declare (type (unsigned-byte 16) high low))
              (logior (ash high 16) low))
  #+ccl (ccl::single-float-bits x)
  #+cmu  (kernel:single-float-bits x)
  #+sbcl (sb-kernel:single-float-bits x)
  #+lispworks (lispworks-float:single-float-bits x))


Does anyone knows of a compatible substitution?


Thanks in advance,
Cam


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Re: Where can I find lispworks-float:single-float-bits?

I use the code below, which assumes IEEE single- and double-precision floats. No guarantees that it's 100% correct, and there may well be much more efficient ways of coding this.

Tested (lightly) against  http://weitz.de/ieee/, as well as a few examples of IEEE encoding found around the internet. 

(defun float-bits-to-int (val)
  (check-type val single-float)
  (multiple-value-bind (significand exponent signum)
      (integer-decode-float val)
    (flet ((encode (sign exponent significand)
             (dpb sign (byte 1 31)
                  (dpb exponent (byte 8 23)
                       significand))))
      (cond ((and (zerop significand) (zerop exponent))
             0)
            ((< (float-precision val) (float-digits val))
             (encode (if (minusp signum) 1 0)
                     0
                     significand))
            (t
             (encode (if (minusp signum) 1 0)
                     (+ 127 23 exponent)
                     (ldb (byte 23 0) significand)))))))

(defun int-to-float-bits (val)
  (if (zerop val)
    0.0f0
    (let ((signum (if (zerop (ldb (byte 1 31) val)) 1 -1))
          (exponent (ldb (byte 8 23) val))
          (significand (ldb (byte 23 0) val)))
      (if (zerop exponent)
        (scale-float (* signum (float significand 1.0f0)) (- 1 127 23))
        (scale-float (* signum (float (dpb 1 (byte 1 23) significand) 1.0f0))
                     (- exponent 127 23))))))

#||
(int-to-float-bits (float-bits-to-int (float pi 1.0f0)))

(list least-positive-single-float (int-to-float-bits (float-bits-to-int least-positive-single-float)))
||#

(defun double-bits-to-int (val)
  (check-type val double-float)
  (multiple-value-bind (significand exponent signum)
      (integer-decode-float val)
    (flet ((encode (sign exponent significand)
             (dpb sign (byte 1 63)
                  (dpb exponent (byte 11 52)
                       significand))))
      (cond ((and (zerop significand) (zerop exponent))
             0)
            ((< (float-precision val) (float-digits val))
             (encode (if (minusp signum) 1 0)
                     0
                     significand))
            (t
             (encode (if (minusp signum) 1 0)
                     (+ 1023 52 exponent)
                     (ldb (byte 52 0) significand)))))))

(defun int-to-double-bits (val)
  (if (zerop val)
    0.0d0
    (let ((signum (if (zerop (ldb (byte 1 63) val)) 1 -1))
          (exponent (ldb (byte 11 52) val))
          (significand (ldb (byte 52 0) val)))
      (if (zerop exponent)
        (scale-float (* signum (float significand 1.0d0)) (- 1 1023 52))
        (scale-float (* signum (float (dpb 1 (byte 1 52) significand) 1.0d0))
                     (- exponent 1023 52))))))

#||
(int-to-double-bits (double-bits-to-int pi))

(list least-positive-double-float (int-to-double-bits (double-bits-to-int least-positive-double-float)))
||#

On Thu, Sep 12, 2019 at 11:51 AM Camille Troillard <camille.troillard@icloud.com> wrote:
Hello,

I am trying to load the CL-PROTOBUFS library from Quicklisp.
On LispWorks 7.1.2, compilation fails complaining that the lispworks-float package does not exist.
The function in question is the following:

(defun single-float-bits (x)
  (declare (type single-float x))
  #+abcl    (system:single-float-bits x)
  #+allegro (multiple-value-bind (high low)
                (excl:single-float-to-shorts x)
              (declare (type (unsigned-byte 16) high low))
              (logior (ash high 16) low))
  #+ccl (ccl::single-float-bits x)
  #+cmu  (kernel:single-float-bits x)
  #+sbcl (sb-kernel:single-float-bits x)
  #+lispworks (lispworks-float:single-float-bits x))


Does anyone knows of a compatible substitution?


Thanks in advance,
Cam


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Re: Where can I find lispworks-float:single-float-bits?

Thank you Raymond.


On 13 Sep 2019, at 08:32, Raymond Wiker <rwiker@gmail.com> wrote:

I use the code below, which assumes IEEE single- and double-precision floats. No guarantees that it's 100% correct, and there may well be much more efficient ways of coding this.

Tested (lightly) against  http://weitz.de/ieee/, as well as a few examples of IEEE encoding found around the internet. 

(defun float-bits-to-int (val)
  (check-type val single-float)
  (multiple-value-bind (significand exponent signum)
      (integer-decode-float val)
    (flet ((encode (sign exponent significand)
             (dpb sign (byte 1 31)
                  (dpb exponent (byte 8 23)
                       significand))))
      (cond ((and (zerop significand) (zerop exponent))
             0)
            ((< (float-precision val) (float-digits val))
             (encode (if (minusp signum) 1 0)
                     0
                     significand))
            (t
             (encode (if (minusp signum) 1 0)
                     (+ 127 23 exponent)
                     (ldb (byte 23 0) significand)))))))

(defun int-to-float-bits (val)
  (if (zerop val)
    0.0f0
    (let ((signum (if (zerop (ldb (byte 1 31) val)) 1 -1))
          (exponent (ldb (byte 8 23) val))
          (significand (ldb (byte 23 0) val)))
      (if (zerop exponent)
        (scale-float (* signum (float significand 1.0f0)) (- 1 127 23))
        (scale-float (* signum (float (dpb 1 (byte 1 23) significand) 1.0f0))
                     (- exponent 127 23))))))

#||
(int-to-float-bits (float-bits-to-int (float pi 1.0f0)))

(list least-positive-single-float (int-to-float-bits (float-bits-to-int least-positive-single-float)))
||#

(defun double-bits-to-int (val)
  (check-type val double-float)
  (multiple-value-bind (significand exponent signum)
      (integer-decode-float val)
    (flet ((encode (sign exponent significand)
             (dpb sign (byte 1 63)
                  (dpb exponent (byte 11 52)
                       significand))))
      (cond ((and (zerop significand) (zerop exponent))
             0)
            ((< (float-precision val) (float-digits val))
             (encode (if (minusp signum) 1 0)
                     0
                     significand))
            (t
             (encode (if (minusp signum) 1 0)
                     (+ 1023 52 exponent)
                     (ldb (byte 52 0) significand)))))))

(defun int-to-double-bits (val)
  (if (zerop val)
    0.0d0
    (let ((signum (if (zerop (ldb (byte 1 63) val)) 1 -1))
          (exponent (ldb (byte 11 52) val))
          (significand (ldb (byte 52 0) val)))
      (if (zerop exponent)
        (scale-float (* signum (float significand 1.0d0)) (- 1 1023 52))
        (scale-float (* signum (float (dpb 1 (byte 1 52) significand) 1.0d0))
                     (- exponent 1023 52))))))

#||
(int-to-double-bits (double-bits-to-int pi))

(list least-positive-double-float (int-to-double-bits (double-bits-to-int least-positive-double-float)))
||#

On Thu, Sep 12, 2019 at 11:51 AM Camille Troillard <camille.troillard@icloud.com> wrote:
Hello,

I am trying to load the CL-PROTOBUFS library from Quicklisp.
On LispWorks 7.1.2, compilation fails complaining that the lispworks-float package does not exist.
The function in question is the following:

(defun single-float-bits (x)
  (declare (type single-float x))
  #+abcl    (system:single-float-bits x)
  #+allegro (multiple-value-bind (high low)
                (excl:single-float-to-shorts x)
              (declare (type (unsigned-byte 16) high low))
              (logior (ash high 16) low))
  #+ccl (ccl::single-float-bits x)
  #+cmu  (kernel:single-float-bits x)
  #+sbcl (sb-kernel:single-float-bits x)
  #+lispworks (lispworks-float:single-float-bits x))


Does anyone knows of a compatible substitution?


Thanks in advance,
Cam


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Updated at: 2020-12-10 08:29 UTC