[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Accessing C floating point data type in MCL
- To: lam@ai-pluto.jpl.nasa.gov (Ray Lam)
- Subject: Re: Accessing C floating point data type in MCL
- From: straz@cambridge.apple.com (Steve Strassmann)
- Date: Mon, 30 Aug 1993 14:02:15 -0400
- Cc: info-mcl
>Date: Mon, 30 Aug 1993 13:59:35 -0600
>To: lam@ai-pluto.jpl.nasa.gov (Ray Lam)
>From: bill@cambridge.apple.com (Bill St. Clair)
>Subject: Re: Accessing C floating point data type in MCL
>Cc: info-mcl
>
>At 4:26 PM 8/26/93 -0800, Ray Lam wrote:
>>Hi,
>> I'm looking for a function simular to "%get-long" but it will work on
>>a ptr to a floating point number in C. I was looking into the
>>Also, Can anyone tell me how a FLOAT datatype in C is represented in a
>>32 and 64 bits? Where is the exponent and where is the mantissa. Thanks in
>>advance.
>
>Don't know. Get the Apple Numerics book or find a specification of
>IEEE floating point.
>
Here's something I did a while back as an exercise in interpreting
a 32-bit FLOAT in C into LISP. I used something similar to
(box-short-float (%get-long float-macptr))
Disclaimer: this seems to work but it is not fully tested.
It's also probably not the most efficient thing in the world;
I was just fooling with the Apple Numerics book in hand.
; X is a 32-bit unboxed short float, as a fixnum.
; This reboxes it to a lisp float.
; From Apple Numerics manual, 2nd ed, p 16.
;
(defun box-short-float (x)
(let ((f (ldb (byte 23 0) x))
(e (ldb (byte 8 23) x))
(s (ldb (byte 1 31) x)))
(cond ((and (zerop e) (zerop f)) 0)
((zerop e)
(* (if (zerop s) 1 -1)
(expt 2 -126)
(decode-binary-fraction f))) ; denormalized f
((and (= e 255) (zerop f))
:infinity)
((= e 255) :NaN)
(t
(* (if (zerop s) 1 -1)
(expt 2 (- e 127))
(+ 1 (decode-binary-fraction f))))))) ; normalized f
; the msb of f = 1/2, next is 1/4, etc.
;
(defun decode-binary-fraction (f &optional (word-size 23))
(let ((acc 0))
(dotimes (i word-size)
(if (= 1 (ldb (byte 1 (- word-size (1+ i))) f))
(incf acc (expt 2 (- -1 i)))))
acc))