Re: I wonder if I'm missing something...
"David McClain (as dbm at refined-audiometrics dot com)" <[email protected]>
| Newsgroups | gmane.lisp.lispworks.general |
|---|---|
| Message-ID | <[email protected]> |
2 Thumbs up to Martin, of course… His solution was much simpler than what I had been using. > On Nov 7, 2025, at 11:10, David McClain (as dbm at refined-audiometrics dot com) <[email protected]> wrote: > > Ooooh !! No…. (but thanks for all that effort) > > That is tantamount to doing a “proper” encoding/decoding via serialization. I already have that and more. > > But unfortunately, the C application running at the remote end of the network pipe does not understand Lisp serialization. It simply writes arrays of raw Single-Float values as though they are octets (using a typecast on the array). > > And so I have to receive raw floating point values, sent in (probably) little-endian order, since both machines are either ARM64 or X64. > > Here is what Martin just suggested (but again, this uses gratuitous copying) > > (defun copy-byte-vector-to-float-vector (ubvec fpvec) > (fli:with-dynamic-foreign-objects ((fp :float :nelems (length fpvec))) > (fli:with-coerced-pointer (ubp :type :uint8) fp > (fli:replace-foreign-array ubp ubvec :end1 (* (length ubvec) #.(fli:size-of :float))) > (fli:replace-foreign-array fpvec fp :end2 (length fpvec)) > fpvec))) > > > >> On Nov 7, 2025, at 11:01, Paul Werkowski <[email protected]> wrote: >> >> Would this help? Convert bits <-> float. >> >> (defun single-float-bits (fpn) >> (declare (type single-float fpn)) >> ;; 1.5 == 0x3fc00000 >> ;; signif #x0c00000 >> ;; expon -23 >> (multiple-value-bind (signif expon sign) >> (integer-decode-float fpn) >> (let* ((sgn (if (minusp sign) 1 0)) >> (exp (+ 127 expon 23)) >> (man (ldb (byte 23 0) signif)) ; 0x400000 >> (bits exp)) >> (setf bits (dpb sgn (byte 1 31) bits) >> bits (dpb exp (byte 8 23) bits) >> bits (dpb man (byte 23 0) bits)) >> bits))) >> >> (defun make-single-float (bits) >> (declare (type (unsigned-byte 32) bits)) >> (let* ((sgn (ldb (byte 1 31) bits)) >> (exp (ldb (byte 8 23) bits)) >> (mant (ldb (byte 23 0) bits)) >> (signif (logior mant #x800000)) ; (ash 1 23) >> (sign (if (= sgn 1) -1f0 1f0)) >> (expon(- exp 150))) >> (float-sign sign (scale-float (float signif) expon)))) >> >> (defun double-float-bits (dfn) >> (declare (type double-float dfn) >> (optimize (fixnum-safety 0))) >> ;; sign 1 bit (byte 1 63) >> ;; expn 11 bits (byte 11 52) bias 1023 >> ;; mant 52 (byte 52 0) >> (multiple-value-bind (signif expon sign) >> (integer-decode-float dfn) >> (let* ((sgn (if (minusp sign) 1 0)) >> (exp (+ 1023 expon 51 )) >> (man (ldb (byte 52 0) signif)) >> (bits exp)) >> (setf bits (dpb sgn (byte 1 63) bits) >> bits (dpb exp (byte 11 52) bits) >> bits (dpb man (byte 52 0) bits)) >> bits))) >> >> (defun make-double-float (bits) >> (declare (type (unsigned-byte 64) bits) >> #+() >> (optimize (float 0)(safety 0)(hcl:fixnum-safety 0))) >> (let* ((sgn (ldb (byte 1 63) bits)) >> (exp (ldb (byte 11 52) bits)) >> (mant (ldb (byte 52 0) bits)) >> (signif (logior mant (ash 1 52))) >> (sign (if (= sgn 1) -1d0 1d0)) >> (expon(- exp 1023 51))) >> (float-sign sign (scale-float (float signif 1d0) expon)))) >> >> >> On 11/7/2025 12:44 PM, Yuri Davidovsky (as work at disclosure dot ie) wrote: >>> >>>> On 7 Nov 2025, at 16:41, David McClain <[email protected]> wrote: >>>> >>>> So I’m wanting to inhale bytes from a stream and read out into the app layer as single-floats. All without gratuitous copying of information. >>> Don’t we all? >>> >>> Have you ever realised that there is absolutely no way in Common Lisp to read binary floats from disk? Has it ever occurred to you that Common Lisp has close to zero file system operation utilities? (we do have the Taj Mahal of pathnames to admire though. but why?). Have you noticed that Common Lisp does not have even a concept of sockets? Multithreading model? Do I need to say more, or does it hurt enough already? >>> >>> The specification for Common Lisp is that of an abstract spherical lisp in vacuum with boring but essential everyday programming stuff sent to walk the plank. As the result Common Lisp failed to unify all the implementations in a meaningful manner since all the practical stuff that you deal with on daily basis was not specced and left out for implementations to decide, leaving them as incompartible as they were prior because those things are the very foundations of general purpose programming language. >>> >>> _______________________________________________ >>> Lisp Hug - the mailing list for LispWorks users >>> [email protected] >>> http://www.lispworks.com/support/lisp-hug.html >> >