about summary refs log tree commit diff
path: root/lib/util/intcode.h
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2009-01-24 20:22:00 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2009-01-24 20:22:00 +0000
commit568478d03036fa0dd8a09b50104917463e82ff42 (patch)
tree1e703517985c8abc9161aecfbd1d6e7e4f54813f /lib/util/intcode.h
parent89d95baa4ba5d1ed74baf4a3556f31e7541604d8 (diff)
downloadnetpbm-mirror-568478d03036fa0dd8a09b50104917463e82ff42.tar.gz
netpbm-mirror-568478d03036fa0dd8a09b50104917463e82ff42.tar.xz
netpbm-mirror-568478d03036fa0dd8a09b50104917463e82ff42.zip
Add 16 bit bigendian integer utilities
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@828 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/util/intcode.h')
-rw-r--r--lib/util/intcode.h77
1 files changed, 74 insertions, 3 deletions
diff --git a/lib/util/intcode.h b/lib/util/intcode.h
index 4d9c83aa..a171d4e0 100644
--- a/lib/util/intcode.h
+++ b/lib/util/intcode.h
@@ -3,6 +3,9 @@
 
 #include "pm_config.h"  /* For uint32_t, BYTE_ORDER */
 
+unsigned int const pm_byteOrder = BYTE_ORDER;
+
+
 typedef struct {
 /*----------------------------------------------------------------------------
    This is a big-endian representation of a 32 bit integer.  I.e.
@@ -20,9 +23,6 @@ typedef struct {
 } bigend32;
 
 
-unsigned int const pm_byteOrder = BYTE_ORDER;
-
-
 static __inline__ uint32_t
 pm_uintFromBigend32(bigend32 const arg) {
 
@@ -83,4 +83,75 @@ pm_bigendFromUint32(uint32_t const arg) {
     return retval;
 }
 
+typedef struct {
+/*----------------------------------------------------------------------------
+   This is a big-endian representation of a 16 bit integer.  I.e.
+   bytes[0] is the most significant 8 bits; bytes[1] is the least
+   significant 8 bits of the number in pure binary.
+
+   On a big-endian machines, this is bit for bit identical to uint16_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[2];
+} bigend16;
+
+
+static __inline__ uint16_t
+pm_uintFromBigend16(bigend16 const arg) {
+
+    uint16_t retval;
+
+    switch (pm_byteOrder) {
+    case BIG_ENDIAN: {
+        union {
+            bigend16 bigend;
+            uint16_t native;
+        } converter;
+        
+        converter.bigend = arg;
+        
+        retval = converter.native;
+    }; break;
+    case LITTLE_ENDIAN: {
+        retval =
+            (arg.bytes[0] << 16) |
+            (arg.bytes[1] <<  8);
+    } break;
+    }
+    return retval;
+}
+
+
+
+static __inline__ bigend16
+pm_bigendFromUint16(uint16_t const arg) {
+
+    bigend16 retval;
+
+    switch (pm_byteOrder) {
+    case BIG_ENDIAN: {
+        union {
+            bigend16 bigend;
+            uint16_t native;
+        } converter;
+        
+        converter.native = arg;
+
+        retval = converter.bigend;
+    } break;
+    case LITTLE_ENDIAN: {
+        uint16_t shift;
+        shift = arg;
+        retval.bytes[1] = shift;  /* Takes lower 8 bits */
+        shift >>= 8;
+        retval.bytes[0] = shift;  /* Takes lower 8 bits */
+    } break;
+    }
+    return retval;
+}
+
 #endif