* [9fans] endiness and IEEE floats @ 2004-12-23 16:45 Steve Simon 2004-12-23 17:00 ` Nigel Roles 2004-12-23 17:12 ` Russ Cox 0 siblings, 2 replies; 4+ messages in thread From: Steve Simon @ 2004-12-23 16:45 UTC (permalink / raw) To: 9fans I am reading data files, they are always little endian (Microsoft) but they include IEEE floats. How do I read them portably? This is what I came up with, It assumes the endianess of doubles is the same as vlongs, which I think is fair... double gf64(Biobuf *b) // get 64 bit IEEE double { uvlong n; double d; n = gi64(b); // get 64 bit int memcpy(&d, &n, sizeof(n)); return d; } anyone got somthing better? -Steve ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [9fans] endiness and IEEE floats 2004-12-23 16:45 [9fans] endiness and IEEE floats Steve Simon @ 2004-12-23 17:00 ` Nigel Roles 2004-12-23 17:14 ` Russ Cox 2004-12-23 17:12 ` Russ Cox 1 sibling, 1 reply; 4+ messages in thread From: Nigel Roles @ 2004-12-23 17:00 UTC (permalink / raw) To: Fans of the OS Plan 9 from Bell Labs Steve Simon wrote: >I am reading data files, they are always little endian >(Microsoft) but they include IEEE floats. How do I read them >portably? > >This is what I came up with, It assumes the endianess >of doubles is the same as vlongs, which I think is fair... > >double >gf64(Biobuf *b) // get 64 bit IEEE double >{ > uvlong n; > double d; > n = gi64(b); // get 64 bit int > memcpy(&d, &n, sizeof(n)); > return d; >} > >anyone got somthing better? > >-Steve > > > if you are going to assume size and endianness matches, then #define bf64(b) ((double)gi64(b)) would suffice. ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [9fans] endiness and IEEE floats 2004-12-23 17:00 ` Nigel Roles @ 2004-12-23 17:14 ` Russ Cox 0 siblings, 0 replies; 4+ messages in thread From: Russ Cox @ 2004-12-23 17:14 UTC (permalink / raw) To: Fans of the OS Plan 9 from Bell Labs > if you are going to assume size and endianness matches, then > > #define gf64(b) ((double)gi64(b)) > > would suffice. that would be true if gf64 and gi64 were returning pointers, but they are returning the actual values. #define bf64(b) (*(double*)&gi64(b)) would work except that you can't take the address of the return value. ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [9fans] endiness and IEEE floats 2004-12-23 16:45 [9fans] endiness and IEEE floats Steve Simon 2004-12-23 17:00 ` Nigel Roles @ 2004-12-23 17:12 ` Russ Cox 1 sibling, 0 replies; 4+ messages in thread From: Russ Cox @ 2004-12-23 17:12 UTC (permalink / raw) To: Fans of the OS Plan 9 from Bell Labs > I am reading data files, they are always little endian > (Microsoft) but they include IEEE floats. How do I read them > portably? > > This is what I came up with, It assumes the endianess > of doubles is the same as vlongs, which I think is fair... > > double > gf64(Biobuf *b) // get 64 bit IEEE double > { > uvlong n; > double d; > n = gi64(b); // get 64 bit int > memcpy(&d, &n, sizeof(n)); > return d; > } > > anyone got somthing better? That's what I do (well, different problem but same sort of solution) to implement isNaN etc. "portably" in libfmt. You could also fiddle with the bytes, but that requires detecting the byte order. gf64(Biobuf *b) { uchar buf[8]; static uchar big1[4] = { 0, 0, 0, 1 }; Bread(b, buf, 8); if(*(ulong*)big1 == 1) /* running on big endian machine */ reverse(buf, 8); return *(double*)buf; } Ken came up with clever way to do doing integer conversion: gi32(Biobuf *b) { int i; u64int leorder = 0x0706050403020100, to; uchar lebuf[8]; uchar *o, *t, *f; Bread(b, lebuf, 8); o = (uchar*)&leorder; f = lebuf; t = (uchar*)&to; for(i=0; i<8; i++) t[o[i]] = f[i]; } The kernel and user space use this trick to convert to and from the big-endian 64-bit numbers in /dev/bintime. When the 64-bit support was worse, it was a huge win. It's probably still a win but less so. Russ ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2004-12-23 17:14 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2004-12-23 16:45 [9fans] endiness and IEEE floats Steve Simon 2004-12-23 17:00 ` Nigel Roles 2004-12-23 17:14 ` Russ Cox 2004-12-23 17:12 ` Russ Cox
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).