il_endian.h 6.8 KB

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