Re: I wonder if I'm missing something...
"Syed Zaeem Hosain (as Syed dot Hosain at aeris dot net)" <[email protected]>
| Newsgroups | gmane.lisp.lispworks.general |
|---|---|
| Message-ID | <SA9PR18MB377431211A101982E94FB497F0C1A@SA9PR18MB3774.namprd18.prod.outlook.com> |
At my age, I find that worrying about the "best", or "most-efficient", or "fastest", or "the <name-your-language> way" to code can simply just slow down getting the job done!
For example, more than four decades ago, I wrote code on a Symbolics 3600 to read magnetic reel tapes (remember those?) with Data General (remember those?) binary numbers in 2048-byte streaming records.
Given the following simplifying factors, I brute-forced it ... and moved on to getting the rest of the application working! Our team was too small to waste time.
1.
I knew what the DG floating-point formats were, but did not bother to convert values perfectly ... from a floating point perspective, that is.
2.
The DG systems did not use IEEE floating point format (hmmm ... did that even exist at the time? I do not recall ... am getting old!)
3.
My code knew just enough to expect which one of four types of numbers in the buffered data would be next: unsigned short int, unsigned long int, single float, or double float.
4.
Was only going to be able to read/buffer 1 byte at a time from a 2048-byte reel tape record, extracting what I needed from the streaming data buffered in.
5.
Did not need to worry about performance or speed - just read the dang file in, convert, and use the results in the Lisp application from that point on.
6.
Did not even concern myself that the two integer formats (see below) might probably have lined up and read/converted easily anyway.
Hence, the following very simplistic LISP (Yeah! Hard-coded numbers! Eek! π), which worked perfectly fine the very first time I ran it, so I did not bother to try to optimize in any way.
KISS principle ... even if it is terrible-looking code.
(defmacro dg-short (buffer index)
"Extract a DG unsigned short integer from two successive bytes."
`(logior (lsh (aref ,buffer ,index) 8)
ββββ ββ (aref ,buffer (1+ ,index))))
(defmacro dg-long (buffer index)
"Extract a DG unsigned long integer from four successive bytes."
`(logior (lsh (aref ,buffer ,index) 24)
ββ ββββ (lsh (aref ,buffer (+ 1 ,index)) 16)
βββββ β (lsh (aref ,buffer (+ 2 ,index)) 8)
ββββββ (aref ,buffer (+ 3 ,index))))
(defmacro dg-single (buffer index)
"Extract a DG single precision number from four successive bytes."
`(let (mantissa)
(setq mantissa (* (// (float (logior (ash (aref ,buffer (+ 1 ,index)) 16)
ββββββββββββββββββββββββββββββ (ash (aref ,buffer (+ 2 ,index)) 8)
ββββββββββββββββββββββββββββββ β (aref ,buffer (+ 3 ,index))))
ββββββββββββββββββ 16777216.0e0)
ββββββββββββ (expt 16.0 (float (- (logand (aref ,buffer ,index) 127) 64)))))
(and (bit-test 128 (aref ,buffer ,index)) (setq mantissa (minus mantissa)))
mantissa))
(defmacro dg-double (buffer index)
"Extract a DG double precision number from eight successive bytes."
`(let (mantissa)
(setq mantissa (* (// (dfloat (logior (ash (aref ,buffer (+ 1 ,index)) 48)
ββββββββββββββββββββββββββββββ (ash (aref ,buffer (+ 2 ,index)) 40)
ββββββββββββββββββββββββββββββ (ash (aref ,buffer (+ 3 ,index)) 32)
ββββββββββββββββββββββββββββββ (ash (aref ,buffer (+ 4 ,index)) 24)
ββββββββββββββββββββββββββββββ (ash (aref ,buffer (+ 5 ,index)) 16)
ββββββββββββββββββββββββββββββ (ash (aref ,buffer (+ 6 ,index)) 8)
ββββββββββββββββββββββββββββββ (aref ,buffer (+ 7 ,index))))
ββββββββββββββββββ 72057594037927936.0d0)
ββββββββββββ (expt 16.0d0 (dfloat (- (logand (aref ,buffer ,index) 127) 64)))))
(and (bit-test 128 (aref ,buffer ,index)) (setq mantissa (minus mantissa)))
mantissa))
Syed Zaeem Hosain | Founder, Chief Evangelist
Aeris Communications, Inc.<http://www.aeris.com/>
[email protected]
a: 1731 Technology Drive, Suite 800, San Jose, CA 95110-1360, USA.
o: +1 408 557-1905 | f: +1 408 557-1925 | twitter: @AerisCTO
This communication contains Aeris Confidential Information. If it has been sent to you in error, please reply to advise the sender of the error and then immediately delete this message.