Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
posh.cpp File Reference
#include "posh.h"

Go to the source code of this file.

Macros

#define POSH_FLOAT_STRING   "enabled"
 
#define POSH_64BIT_INTEGER_STRING   "no"
 
#define POSH_POINTER_STRING   "32-bits"
 
#define IS_BIG_ENDIAN   1
 
#define NATIVE16   POSH_BigU16
 
#define NATIVE32   POSH_BigU32
 
#define NATIVE64   POSH_BigU64
 
#define FOREIGN16   POSH_LittleU16
 
#define FOREIGN32   POSH_LittleU32
 
#define FOREIGN64   POSH_LittleU64
 

Functions

static int s_testBigEndian (void)
 
static const char * s_testSerialization (void)
 
static const char * s_testFloatingPoint (void)
 
static const char * s_testEndianess (void)
 
const char * POSH_GetArchString (void)
 
posh_u16_t POSH_SwapU16 (posh_u16_t v)
 
posh_s16_t POSH_SwapS16 (posh_s16_t v)
 
posh_u32_t POSH_SwapU32 (posh_u32_t v)
 
posh_s32_t POSH_SwapS32 (posh_s32_t v)
 
posh_u16_tPOSH_WriteU16ToLittle (void *dst, posh_u16_t value)
 
posh_s16_tPOSH_WriteS16ToLittle (void *dst, posh_s16_t value)
 
posh_u32_tPOSH_WriteU32ToLittle (void *dst, posh_u32_t value)
 
posh_s32_tPOSH_WriteS32ToLittle (void *dst, posh_s32_t value)
 
posh_u16_tPOSH_WriteU16ToBig (void *dst, posh_u16_t value)
 
posh_s16_tPOSH_WriteS16ToBig (void *dst, posh_s16_t value)
 
posh_u32_tPOSH_WriteU32ToBig (void *dst, posh_u32_t value)
 
posh_s32_tPOSH_WriteS32ToBig (void *dst, posh_s32_t value)
 
posh_u16_t POSH_ReadU16FromLittle (const void *src)
 
posh_s16_t POSH_ReadS16FromLittle (const void *src)
 
posh_u32_t POSH_ReadU32FromLittle (const void *src)
 
posh_s32_t POSH_ReadS32FromLittle (const void *src)
 
posh_u16_t POSH_ReadU16FromBig (const void *src)
 
posh_s16_t POSH_ReadS16FromBig (const void *src)
 
posh_u32_t POSH_ReadU32FromBig (const void *src)
 
posh_s32_t POSH_ReadS32FromBig (const void *src)
 
posh_u32_t POSH_LittleFloatBits (float f)
 
posh_u32_t POSH_BigFloatBits (float f)
 
void POSH_DoubleBits (double d, posh_byte_t dst[8])
 
double POSH_DoubleFromBits (const posh_byte_t src[8])
 
float POSH_FloatFromLittleBits (posh_u32_t bits)
 
float POSH_FloatFromBigBits (posh_u32_t bits)
 

Macro Definition Documentation

#define FOREIGN16   POSH_LittleU16

Definition at line 43 of file posh.cpp.

Referenced by s_testEndianess().

#define FOREIGN32   POSH_LittleU32

Definition at line 44 of file posh.cpp.

Referenced by s_testEndianess().

#define FOREIGN64   POSH_LittleU64

Definition at line 45 of file posh.cpp.

#define IS_BIG_ENDIAN   1

Definition at line 38 of file posh.cpp.

Referenced by s_testEndianess().

#define NATIVE16   POSH_BigU16

Definition at line 40 of file posh.cpp.

Referenced by s_testEndianess().

#define NATIVE32   POSH_BigU32

Definition at line 41 of file posh.cpp.

Referenced by s_testEndianess().

#define NATIVE64   POSH_BigU64

Definition at line 42 of file posh.cpp.

#define POSH_64BIT_INTEGER_STRING   "no"

Definition at line 19 of file posh.cpp.

Referenced by POSH_GetArchString().

#define POSH_FLOAT_STRING   "enabled"

Definition at line 11 of file posh.cpp.

Referenced by POSH_GetArchString().

#define POSH_POINTER_STRING   "32-bits"

Definition at line 25 of file posh.cpp.

Referenced by POSH_GetArchString().

Function Documentation

static int s_testBigEndian ( void  )
static

Definition at line 49 of file posh.cpp.

References c, and i.

Referenced by s_testEndianess().

50 {
51  union
52  {
53  posh_byte_t c[4];
54  posh_u32_t i;
55  }
56  u;
57 
58  u.i = 1;
59  if (u.c[0] == 1)
60  return 0;
61  return 1;
62 }
static const char* s_testEndianess ( void  )
static

Definition at line 130 of file posh.cpp.

References FOREIGN16, FOREIGN32, IS_BIG_ENDIAN, NATIVE16, NATIVE32, and s_testBigEndian().

Referenced by POSH_GetArchString().

131 {
132  /* check endianess */
134  return
135  "*ERROR: POSH compile time endianess does not match run-time endianess verification. Please report this to poshlib@poshlib.org!\n";
136  /* make sure our endian swap routines work */
137  if ( (NATIVE32( 0x11223344L ) != 0x11223344L)
138  || (FOREIGN32( 0x11223344L ) != 0x44332211L)
139  || (NATIVE16( 0x1234 ) != 0x1234)
140  || (FOREIGN16( 0x1234 ) != 0x3412) )
141  return "*ERROR: POSH endianess macro selection failed. Please report this to poshlib@poshlib.org!\n";
142  /* test serialization routines */
143 
144  return 0;
145 }
static const char* s_testFloatingPoint ( void  )
static

Definition at line 105 of file posh.cpp.

References POSH_BigFloatBits(), POSH_DoubleBits(), POSH_DoubleFromBits(), POSH_FloatFromBigBits(), POSH_FloatFromLittleBits(), and POSH_LittleFloatBits().

Referenced by POSH_GetArchString().

106 {
107  float fRef = 10.0f/30.0f;
108  double dRef = 10.0/30.0;
109  posh_byte_t dbuf[8];
110  float fTmp;
111  double dTmp;
112 
114  if (fTmp != fRef)
115  return "*ERROR: POSH little endian floating point conversion failed. Please report this to poshlib@poshlib.org!\n";
116  fTmp = POSH_FloatFromBigBits( POSH_BigFloatBits( fRef ) );
117  if (fTmp != fRef)
118  return "*ERROR: POSH big endian floating point conversion failed. Please report this to poshlib@poshlib.org!\n";
119  POSH_DoubleBits( dRef, dbuf );
120 
121  dTmp = POSH_DoubleFromBits( dbuf );
122  if (dTmp != dRef)
123  return
124  "*ERROR: POSH double precision floating point serialization failed. Please report this to poshlib@poshlib.org!\n";
125  return 0;
126 }
static const char* s_testSerialization ( void  )
static

Definition at line 65 of file posh.cpp.

References POSH_ReadU16FromBig(), POSH_ReadU16FromLittle(), POSH_ReadU32FromBig(), POSH_ReadU32FromLittle(), POSH_WriteU16ToBig(), POSH_WriteU16ToLittle(), POSH_WriteU32ToBig(), and POSH_WriteU32ToLittle().

Referenced by POSH_GetArchString().

66 {
67  posh_byte_t serbuf[8];
68  posh_u16_t tmp16;
69  posh_u32_t tmp32;
70 
71  /* 16-bit serialization */
72  POSH_WriteU16ToLittle( serbuf, 0xABCD );
73  if ( ( tmp16 = POSH_ReadU16FromLittle( serbuf ) ) != 0xABCD )
74  return "*ERROR: failed little-endian 16-bit serialization test";
75  POSH_WriteU16ToBig( serbuf, 0xABCD );
76  if ( ( tmp16 = POSH_ReadU16FromBig( serbuf ) ) != 0xABCD )
77  return "*ERROR: failed big-endian 16-bit serialization test";
78  /* 32-bit serialization */
79  POSH_WriteU32ToLittle( serbuf, 0xABCD1234L );
80  if ( ( tmp32 = POSH_ReadU32FromLittle( serbuf ) ) != 0xABCD1234 )
81  return "*ERROR: failed little-endian 32-bit serialization test";
82  POSH_WriteU32ToBig( serbuf, 0xABCD1234L );
83  if ( ( tmp32 = POSH_ReadU32FromBig( serbuf ) ) != 0xABCD1234 )
84  return "*ERROR: failed big-endian 32-bit serialization test";
85 #if defined POSH_64BIT_INTEGER
86  {
87 #define REF64 POSH_U64( 0xFEDCBA9876543210 )
88 
89  posh_u64_t tmp64;
90 
91  POSH_WriteU64ToLittle( serbuf, REF64 );
92  if ( ( tmp64 = POSH_ReadU64FromLittle( serbuf ) ) != REF64 )
93  return "*ERROR: failed little-endian 64-bit serialization test";
94  POSH_WriteU64ToBig( serbuf, REF64 );
95  if ( ( tmp64 = POSH_ReadU64FromBig( serbuf ) ) != REF64 )
96  return "*ERROR: failed big-endian 64-bit serialization test";
97  }
98 #endif
99 
100  return 0;
101 }