FROM : Wade Tregaskis
DATE : Tue Nov 02 01:43:10 2004
> Is there a function to convert 64 bit words to network byte order (and
> back again)? ntohl convert uint_32 and ntohs converts uint_16. uint_64
> anyone?
There's always <architecture/byte_order.h>, which is what ntohl gets
mapped to anyway.
Also, while the man page for ntohl says it takes a uint32_t argument,
the actual definition in the header files takes an unsigned long, and
maps to the appropriate NXSwap... function, taking a long. So, on a
64-bit architecture, ntohl will swap 64-bit longs. Of course, you
probably want behaviour that's the same on 32-bit and 64-bit systems...
The code for doing so is pretty trivial; define a union containing a
uint64_t and an eight-byte character array, then swap the bytes. There
are more efficient ways of doing so on 32-bit machines (you can swap
the high and low 32-bits, then swap the bytes in them, allowing some
minor level of parallelism), although you'll be hard pressed to find
any open source* assembly implementations. I have written some, but
the performance improvements in most cases are negligible, and not
worth the associated issues (such as using inline assembly with gcc,
which appears dangerous in 3.3).
As a special case you could probably optimise loads and stores to
memory by using the appropriate byte-swapping load/store PPC
instructions, but I don't think these are present on the G5 (re. the
Virtual PC "fiasco"), so here be compatibility dragons...
Wade Tregaskis (AIM, Yahoo & Skype: wadetregaskis, ICQ: 40056898, MSN &
email: <email_removed>, Jabber:
<email_removed>
-- Sed quis custodiet ipsos custodes?
* = Open source is this context being "open" in the truer sense of the
word, so no APSL/GPL/etc.
DATE : Tue Nov 02 01:43:10 2004
> Is there a function to convert 64 bit words to network byte order (and
> back again)? ntohl convert uint_32 and ntohs converts uint_16. uint_64
> anyone?
There's always <architecture/byte_order.h>, which is what ntohl gets
mapped to anyway.
Also, while the man page for ntohl says it takes a uint32_t argument,
the actual definition in the header files takes an unsigned long, and
maps to the appropriate NXSwap... function, taking a long. So, on a
64-bit architecture, ntohl will swap 64-bit longs. Of course, you
probably want behaviour that's the same on 32-bit and 64-bit systems...
The code for doing so is pretty trivial; define a union containing a
uint64_t and an eight-byte character array, then swap the bytes. There
are more efficient ways of doing so on 32-bit machines (you can swap
the high and low 32-bits, then swap the bytes in them, allowing some
minor level of parallelism), although you'll be hard pressed to find
any open source* assembly implementations. I have written some, but
the performance improvements in most cases are negligible, and not
worth the associated issues (such as using inline assembly with gcc,
which appears dangerous in 3.3).
As a special case you could probably optimise loads and stores to
memory by using the appropriate byte-swapping load/store PPC
instructions, but I don't think these are present on the G5 (re. the
Virtual PC "fiasco"), so here be compatibility dragons...
Wade Tregaskis (AIM, Yahoo & Skype: wadetregaskis, ICQ: 40056898, MSN &
email: <email_removed>, Jabber:
<email_removed>
-- Sed quis custodiet ipsos custodes?
* = Open source is this context being "open" in the truer sense of the
word, so no APSL/GPL/etc.
| Related mails | Author | Date |
|---|---|---|
| Hall, Warwick D | Nov 2, 01:22 | |
| Wade Tregaskis | Nov 2, 01:43 |






Cocoa mail archive

