il_endian.h
Upload User: wmy0603
Upload Date: 2022-05-02
Package Size: 1808k
Code Size: 7k
Development Platform:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib Sources
  4. // Copyright (C) 2000-2009 by Denton Woods
  5. // Last modified: 01/29/2009
  6. //
  7. // Filename: src-IL/include/il_endian.h
  8. //
  9. // Description: Handles Endian-ness
  10. //
  11. //-----------------------------------------------------------------------------
  12. #ifndef IL_ENDIAN_H
  13. #define IL_ENDIAN_H
  14. #include "il_internal.h"
  15. #ifdef WORDS_BIGENDIAN  // This is defined by ./configure.
  16. #ifndef __BIG_ENDIAN__
  17. #define __BIG_ENDIAN__ 1
  18. #endif
  19. #endif
  20. #if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __BIG_ENDIAN__) 
  21.   || (defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__))
  22.   #undef __LITTLE_ENDIAN__
  23. #define Short(s) iSwapShort(s)
  24. #define UShort(s) iSwapUShort(s)
  25. #define Int(i) iSwapInt(i)
  26. #define UInt(i) iSwapUInt(i)
  27. #define Float(f) iSwapFloat(f)
  28. #define Double(d) iSwapDouble(d)
  29.  
  30. #define BigShort(s)  
  31. #define BigUShort(s)  
  32. #define BigInt(i)  
  33. #define BigUInt(i)  
  34. #define BigFloat(f)  
  35. #define BigDouble(d)  
  36. #else
  37. #undef __BIG_ENDIAN__
  38. #undef __LITTLE_ENDIAN__  // Not sure if it's defined by any compiler...
  39. #define __LITTLE_ENDIAN__
  40. #define Short(s)  
  41. #define UShort(s)  
  42. #define Int(i)  
  43. #define UInt(i)  
  44. #define Float(f)  
  45. #define Double(d)  
  46. #define BigShort(s) iSwapShort(s)
  47. #define BigUShort(s) iSwapUShort(s)
  48. #define BigInt(i) iSwapInt(i)
  49. #define BigUInt(i) iSwapUInt(i)
  50. #define BigFloat(f) iSwapFloat(f)
  51. #define BigDouble(d) iSwapDouble(d)
  52. #endif
  53. void   iSwapUShort(ILushort *s);
  54. void   iSwapShort(ILshort *s);
  55. void   iSwapUInt(ILuint *i);
  56. void   iSwapInt(ILint *i);
  57. void   iSwapFloat(ILfloat *f);
  58. void   iSwapDouble(ILdouble *d);
  59. ILushort GetLittleUShort();
  60. ILshort  GetLittleShort();
  61. ILuint   GetLittleUInt();
  62. ILint    GetLittleInt();
  63. ILfloat  GetLittleFloat();
  64. ILdouble GetLittleDouble();
  65. ILushort GetBigUShort();
  66. ILshort  GetBigShort();
  67. ILuint   GetBigUInt();
  68. ILint    GetBigInt();
  69. ILfloat  GetBigFloat();
  70. ILdouble GetBigDouble();
  71. ILubyte SaveLittleUShort(ILushort s);
  72. ILubyte SaveLittleShort(ILshort s);
  73. ILubyte SaveLittleUInt(ILuint i);
  74. ILubyte SaveLittleInt(ILint i);
  75. ILubyte SaveLittleFloat(ILfloat f);
  76. ILubyte SaveLittleDouble(ILdouble d);
  77. ILubyte SaveBigUShort(ILushort s);
  78. ILubyte SaveBigShort(ILshort s);
  79. ILubyte SaveBigUInt(ILuint i);
  80. ILubyte SaveBigInt(ILint i);
  81. ILubyte SaveBigFloat(ILfloat f);
  82. ILubyte SaveBigDouble(ILdouble d);
  83. #ifdef IL_ENDIAN_C
  84. #undef NOINLINE
  85. #undef INLINE
  86. #define INLINE
  87. #endif
  88. #ifndef NOINLINE
  89. INLINE void iSwapUShort(ILushort *s)  {
  90. #ifdef USE_WIN32_ASM
  91. __asm {
  92. mov ebx, s
  93. mov al, [ebx+1]
  94. mov ah, [ebx  ]
  95. mov [ebx], ax
  96. }
  97. #else
  98. #ifdef GCC_X86_ASM
  99. asm("ror $8,%0"
  100. : "=r" (*s)
  101. : "0" (*s));
  102. #else
  103. *s = ((*s)>>8) | ((*s)<<8);
  104. #endif //GCC_X86_ASM
  105. #endif //USE_WIN32_ASM
  106. }
  107. INLINE void iSwapShort(ILshort *s) {
  108. iSwapUShort((ILushort*)s);
  109. }
  110. INLINE void iSwapUInt(ILuint *i) {
  111. #ifdef USE_WIN32_ASM
  112. __asm {
  113. mov ebx, i
  114. mov eax, [ebx]
  115. bswap eax
  116. mov [ebx], eax
  117. }
  118. #else
  119. #ifdef GCC_X86_ASM
  120. asm("bswap %0;"
  121. : "+r" (*i));
  122. #else
  123. *i = ((*i)>>24) | (((*i)>>8) & 0xff00) | (((*i)<<8) & 0xff0000) | ((*i)<<24);
  124. #endif //GCC_X86_ASM
  125. #endif //USE_WIN32_ASM
  126. }
  127. INLINE void iSwapInt(ILint *i) {
  128. iSwapUInt((ILuint*)i);
  129. }
  130. INLINE void iSwapFloat(ILfloat *f) {
  131. iSwapUInt((ILuint*)f);
  132. }
  133. INLINE void iSwapDouble(ILdouble *d) {
  134. #ifdef GCC_X86_ASM
  135. int *t = (int*)d;
  136. asm("bswap %2    n"
  137. "bswap %3    n"
  138. "movl  %2,%1 n"
  139. "movl  %3,%0 n"
  140. : "=g" (t[0]), "=g" (t[1])
  141. : "r"  (t[0]), "r"  (t[1]));
  142. #else
  143. ILubyte t,*b = (ILubyte*)d;
  144. #define dswap(x,y) t=b[x];b[x]=b[y];b[y]=b[x];
  145. dswap(0,7);
  146. dswap(1,6);
  147. dswap(2,5);
  148. dswap(3,4);
  149. #undef dswap
  150. #endif
  151. }
  152. INLINE ILushort GetLittleUShort() {
  153. ILushort s;
  154. iread(&s, sizeof(ILushort), 1);
  155. #ifdef __BIG_ENDIAN__
  156. iSwapUShort(&s);
  157. #endif
  158. return s;
  159. }
  160. INLINE ILshort GetLittleShort() {
  161. ILshort s;
  162. iread(&s, sizeof(ILshort), 1);
  163. #ifdef __BIG_ENDIAN__
  164. iSwapShort(&s);
  165. #endif
  166. return s;
  167. }
  168. INLINE ILuint GetLittleUInt() {
  169. ILuint i;
  170. iread(&i, sizeof(ILuint), 1);
  171. #ifdef __BIG_ENDIAN__
  172. iSwapUInt(&i);
  173. #endif
  174. return i;
  175. }
  176. INLINE ILint GetLittleInt() {
  177. ILint i;
  178. iread(&i, sizeof(ILint), 1);
  179. #ifdef __BIG_ENDIAN__
  180. iSwapInt(&i);
  181. #endif
  182. return i;
  183. }
  184. INLINE ILfloat GetLittleFloat() {
  185. ILfloat f;
  186. iread(&f, sizeof(ILfloat), 1);
  187. #ifdef __BIG_ENDIAN__
  188. iSwapFloat(&f);
  189. #endif
  190. return f;
  191. }
  192. INLINE ILdouble GetLittleDouble() {
  193. ILdouble d;
  194. iread(&d, sizeof(ILdouble), 1);
  195. #ifdef __BIG_ENDIAN__
  196. iSwapDouble(&d);
  197. #endif
  198. return d;
  199. }
  200. INLINE ILushort GetBigUShort() {
  201. ILushort s;
  202. iread(&s, sizeof(ILushort), 1);
  203. #ifdef __LITTLE_ENDIAN__
  204. iSwapUShort(&s);
  205. #endif
  206. return s;
  207. }
  208. INLINE ILshort GetBigShort() {
  209. ILshort s;
  210. iread(&s, sizeof(ILshort), 1);
  211. #ifdef __LITTLE_ENDIAN__
  212. iSwapShort(&s);
  213. #endif
  214. return s;
  215. }
  216. INLINE ILuint GetBigUInt() {
  217. ILuint i;
  218. iread(&i, sizeof(ILuint), 1);
  219. #ifdef __LITTLE_ENDIAN__
  220. iSwapUInt(&i);
  221. #endif
  222. return i;
  223. }
  224. INLINE ILint GetBigInt() {
  225. ILint i;
  226. iread(&i, sizeof(ILint), 1);
  227. #ifdef __LITTLE_ENDIAN__
  228. iSwapInt(&i);
  229. #endif
  230. return i;
  231. }
  232. INLINE ILfloat GetBigFloat() {
  233. ILfloat f;
  234. iread(&f, sizeof(ILfloat), 1);
  235. #ifdef __LITTLE_ENDIAN__
  236. iSwapFloat(&f);
  237. #endif
  238. return f;
  239. }
  240. INLINE ILdouble GetBigDouble() {
  241. ILdouble d;
  242. iread(&d, sizeof(ILdouble), 1);
  243. #ifdef __LITTLE_ENDIAN__
  244. iSwapDouble(&d);
  245. #endif
  246. return d;
  247. }
  248. INLINE ILubyte SaveLittleUShort(ILushort s) {
  249. #ifdef __BIG_ENDIAN__
  250. iSwapUShort(&s);
  251. #endif
  252. return iwrite(&s, sizeof(ILushort), 1);
  253. }
  254. INLINE ILubyte SaveLittleShort(ILshort s) {
  255. #ifdef __BIG_ENDIAN__
  256. iSwapShort(&s);
  257. #endif
  258. return iwrite(&s, sizeof(ILshort), 1);
  259. }
  260. INLINE ILubyte SaveLittleUInt(ILuint i) {
  261. #ifdef __BIG_ENDIAN__
  262. iSwapUInt(&i);
  263. #endif
  264. return iwrite(&i, sizeof(ILuint), 1);
  265. }
  266. INLINE ILubyte SaveLittleInt(ILint i) {
  267. #ifdef __BIG_ENDIAN__
  268. iSwapInt(&i);
  269. #endif
  270. return iwrite(&i, sizeof(ILint), 1);
  271. }
  272. INLINE ILubyte SaveLittleFloat(ILfloat f) {
  273. #ifdef __BIG_ENDIAN__
  274. iSwapFloat(&f);
  275. #endif
  276. return iwrite(&f, sizeof(ILfloat), 1);
  277. }
  278. INLINE ILubyte SaveLittleDouble(ILdouble d) {
  279. #ifdef __BIG_ENDIAN__
  280. iSwapDouble(&d);
  281. #endif
  282. return iwrite(&d, sizeof(ILdouble), 1);
  283. }
  284. INLINE ILubyte SaveBigUShort(ILushort s) {
  285. #ifdef __LITTLE_ENDIAN__
  286. iSwapUShort(&s);
  287. #endif
  288. return iwrite(&s, sizeof(ILushort), 1);
  289. }
  290. INLINE ILubyte SaveBigShort(ILshort s) {
  291. #ifdef __LITTLE_ENDIAN__
  292. iSwapShort(&s);
  293. #endif
  294. return iwrite(&s, sizeof(ILshort), 1);
  295. }
  296. INLINE ILubyte SaveBigUInt(ILuint i) {
  297. #ifdef __LITTLE_ENDIAN__
  298. iSwapUInt(&i);
  299. #endif
  300. return iwrite(&i, sizeof(ILuint), 1);
  301. }
  302. INLINE ILubyte SaveBigInt(ILint i) {
  303. #ifdef __LITTLE_ENDIAN__
  304. iSwapInt(&i);
  305. #endif
  306. return iwrite(&i, sizeof(ILint), 1);
  307. }
  308. INLINE ILubyte SaveBigFloat(ILfloat f) {
  309. #ifdef __LITTLE_ENDIAN__
  310. iSwapFloat(&f);
  311. #endif
  312. return iwrite(&f, sizeof(ILfloat), 1);
  313. }
  314. INLINE ILubyte SaveBigDouble(ILdouble d) {
  315. #ifdef __LITTLE_ENDIAN__
  316. iSwapDouble(&d);
  317. #endif
  318. return iwrite(&d, sizeof(ILdouble), 1);
  319. }
  320. #endif//NOINLINE
  321. void EndianSwapData(void *_Image);
  322. #endif//ENDIAN_H