diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2006-08-19 03:12:28 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2006-08-19 03:12:28 +0000 |
commit | 1fd361a1ea06e44286c213ca1f814f49306fdc43 (patch) | |
tree | 64c8c96cf54d8718847339a403e5e67b922e8c3f /lib/util/intcode.h | |
download | netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.gz netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.xz netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.zip |
Create Subversion repository
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/util/intcode.h')
-rw-r--r-- | lib/util/intcode.h | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/util/intcode.h b/lib/util/intcode.h new file mode 100644 index 00000000..4d9c83aa --- /dev/null +++ b/lib/util/intcode.h @@ -0,0 +1,86 @@ +#ifndef INTCODE_H_INCLUDED +#define INTCODE_H_INCLUDED + +#include "pm_config.h" /* For uint32_t, BYTE_ORDER */ + +typedef struct { +/*---------------------------------------------------------------------------- + This is a big-endian representation of a 32 bit integer. I.e. + bytes[0] is the most significant 8 bits; bytes[3] is the least + significant 8 bits of the number in pure binary. + + On a big-endian machines, this is bit for bit identical to uint32_t. + On a little-endian machine, it isn't. + + This is an important data type because decent file formats use + big-endian -- they don't care if some CPU happens to use some other + code for its own work. +-----------------------------------------------------------------------------*/ + unsigned char bytes[4]; +} bigend32; + + +unsigned int const pm_byteOrder = BYTE_ORDER; + + +static __inline__ uint32_t +pm_uintFromBigend32(bigend32 const arg) { + + uint32_t retval; + + switch (pm_byteOrder) { + case BIG_ENDIAN: { + union { + bigend32 bigend; + uint32_t native; + } converter; + + converter.bigend = arg; + + retval = converter.native; + }; break; + case LITTLE_ENDIAN: { + retval = + (arg.bytes[0] << 24) | + (arg.bytes[1] << 16) | + (arg.bytes[2] << 8) | + (arg.bytes[3] << 0); + } break; + } + return retval; +} + + + +static __inline__ bigend32 +pm_bigendFromUint32(uint32_t const arg) { + + bigend32 retval; + + switch (pm_byteOrder) { + case BIG_ENDIAN: { + union { + bigend32 bigend; + uint32_t native; + } converter; + + converter.native = arg; + + retval = converter.bigend; + } break; + case LITTLE_ENDIAN: { + uint32_t shift; + shift = arg; + retval.bytes[3] = shift; /* Takes lower 8 bits */ + shift >>= 8; + retval.bytes[2] = shift; /* Takes lower 8 bits */ + shift >>= 8; + retval.bytes[1] = shift; /* Takes lower 8 bits */ + shift >>= 8; + retval.bytes[0] = shift; /* Takes lower 8 bits */ + } break; + } + return retval; +} + +#endif |