$search
00001 #ifndef TRIMESH_ENDIAN_H 00002 #define TRIMESH_ENDIAN_H 00003 /* 00004 Szymon Rusinkiewicz 00005 Stanford Graphics Lab 00006 00007 trimesh_endian.h 00008 Byte swapping for big/little-endian architectures 00009 */ 00010 00011 //#if defined(i386) || defined(WIN32) 00012 //# define we_are_little_endian 0 00013 //#else 00014 //# define we_are_little_endian 1 00015 //#endif 00016 // 00017 //#define we_are_big_endian (!we_are_little_endian) 00018 // 00019 00020 namespace trimesh { 00021 00022 // Figure out whether this machine is little- or big-endian 00023 static inline bool we_are_little_endian() 00024 { 00025 char buf[4]; 00026 *(int *)(&buf[0]) = 1; 00027 return (buf[0] == 1); 00028 } 00029 00030 static inline bool we_are_big_endian() 00031 { 00032 return !we_are_little_endian(); 00033 } 00034 00035 static inline void swap_2(unsigned short &x) 00036 { 00037 x = ((x & 0xff) << 8) | (x >> 8); 00038 } 00039 00040 static inline void swap_2(short &x) 00041 { 00042 swap_2(* (unsigned short *)(&x)); 00043 } 00044 00045 static inline void swap_4(unsigned &x) 00046 { 00047 x = (x << 24) | 00048 ((x << 8) & 0x00ff0000u) | 00049 ((x >> 8) & 0x0000ff00u) | 00050 (x >> 24); 00051 } 00052 00053 static inline void swap_4(int &x) 00054 { 00055 swap_4(* (unsigned *)(&x)); 00056 } 00057 00058 static inline void swap_float(float &x) 00059 { 00060 swap_4(* (unsigned *)(&x)); 00061 } 00062 00063 } // namespace trimesh 00064 00065 #endif