* IEEE single precision float values
@ 2005-02-11 9:34 Mario Lang
2005-02-12 19:16 ` Richard Stallman
0 siblings, 1 reply; 3+ messages in thread
From: Mario Lang @ 2005-02-11 9:34 UTC (permalink / raw)
Hi.
While trying to implement a elisp library for a binary
network protocol which uses IEEE 32-bit float data types
I realized Emacs Lisp does not have any way to generate or interpret those.
I came up with the following two functions below. I think they
are generically useable for network IO and possibly reading
certain data records from files directly. I am fairly sure
my version is very bad regarding performance though.
Either way, I'd like to contribute this for eventual inclusion
in a suitable file in lisp/. If anyone wants to take the
job of writing C level primitives which do the same thing,
that would be even nicer.
(defun ieeefloat-to-emacs (string)
"Convert a 32bit IEEE floating point value to Emacs floating point
representation."
(assert (length string) 4)
(let ((s (lsh (logand (aref string 3) #X80) -7))
(e (+ (lsh (logand (aref string 3) #X7F) 1)
(lsh (logand (aref string 2) #X80) -7)))
(f (+ (aref string 0)
(lsh (aref string 1) 8)
(lsh (logand (aref string 2) #X7F) 16))))
(cond
((and (= e 0) (= f 0))
(* 0.0 (expt -1 s)))
((and (= e 255) (or (= f (1- (expt 2 23))) (= f 0)))
(* 1.0e+INF (expt -1 s)))
((and (= e 255) (not (or (= f 0) (= f (1- (expt 2 23))))))
0.0e+NaN)
(t
(* (round (* (* (expt -1 s)
(expt 2.0 (- e 127))
(1+ (/ f (expt 2.0 23))))
(expt 10.0 7.0)))
(expt 10.0 -7.0))))))
(defun emacs-to-ieeefloat (value)
"Convert Emacs floating point numers to 32bit IEEE floating
point representation."
(let (s (e 0) f)
(cond
((string= (format "%f" value) (format "%f" -0.0))
(setq s 1 f 0))
((string= (format "%f" value) (format "%f" 0.0))
(setq s 0 f 0))
((= value 1.0e+INF)
(setq s 0 e 255 f (1- (expt 2 23))))
((= value -1.0e+INF)
(setq s 1 e 255 f (1- (expt 2 23))))
((string= (format "%f" value) (format "%f" 0.0e+NaN))
(setq s 0 e 255 f 1))
(t
(setq s (if (>= value 0.0)
(progn (setq f value) 0)
(setq f (* -1 value)) 1))
(while (>= (* f (expt 2.0 e)) 2.0) (setq e (1- e)))
(unless (= e 0) (while (< (* f (expt 2.0 e)) 1.0) (setq e (1+ e))))
(setq f (round (* (1- (* f (expt 2.0 e))) (expt 2 23)))
e (+ (* -1 e) 127))))
(concat (vector (logand f #XFF) (lsh (logand f #XFF00) -8)
(+ (lsh (logand e #X01) 7) (lsh (logand f #X7F0000) -16))
(+ (lsh s 7) (lsh (logand e #XFE) -1))))))
--
CYa,
Mario
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: IEEE single precision float values
2005-02-11 9:34 IEEE single precision float values Mario Lang
@ 2005-02-12 19:16 ` Richard Stallman
2005-02-12 19:53 ` David Kastrup
0 siblings, 1 reply; 3+ messages in thread
From: Richard Stallman @ 2005-02-12 19:16 UTC (permalink / raw)
Cc: emacs-devel
I came up with the following two functions below. I think they
are generically useable for network IO and possibly reading
certain data records from files directly. I am fairly sure
my version is very bad regarding performance though.
Whether that is a problem depends on what the application is.
Anyway, if this is generally useful we could install it.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: IEEE single precision float values
2005-02-12 19:16 ` Richard Stallman
@ 2005-02-12 19:53 ` David Kastrup
0 siblings, 0 replies; 3+ messages in thread
From: David Kastrup @ 2005-02-12 19:53 UTC (permalink / raw)
Cc: Mario Lang, emacs-devel
Richard Stallman <rms@gnu.org> writes:
> I came up with the following two functions below. I think they
> are generically useable for network IO and possibly reading
> certain data records from files directly. I am fairly sure
> my version is very bad regarding performance though.
>
> Whether that is a problem depends on what the application is.
> Anyway, if this is generally useful we could install it.
I think this is something that should quite likely rather be done in C
on platforms with IEEE floating point arithmetic. It seems rather
pointless (not to mention bug-prone) to go through all that Elisp when
just typecasting the bit patterns should be enough.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-02-12 19:53 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-02-11 9:34 IEEE single precision float values Mario Lang
2005-02-12 19:16 ` Richard Stallman
2005-02-12 19:53 ` David Kastrup
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.