rawint.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef _RAR_RAWINT_
  2. #define _RAR_RAWINT_
  3. #define rotls(x,n,xsize) (((x)<<(n)) | ((x)>>(xsize-(n))))
  4. #define rotrs(x,n,xsize) (((x)>>(n)) | ((x)<<(xsize-(n))))
  5. #define rotl32(x,n) rotls(x,n,32)
  6. #define rotr32(x,n) rotrs(x,n,32)
  7. inline uint RawGet2(const void *Data)
  8. {
  9. byte *D=(byte *)Data;
  10. return D[0]+(D[1]<<8);
  11. }
  12. inline uint32 RawGet4(const void *Data)
  13. {
  14. #if defined(BIG_ENDIAN) || !defined(ALLOW_MISALIGNED)
  15. byte *D=(byte *)Data;
  16. return D[0]+(D[1]<<8)+(D[2]<<16)+(D[3]<<24);
  17. #else
  18. return *(uint32 *)Data;
  19. #endif
  20. }
  21. inline uint64 RawGet8(const void *Data)
  22. {
  23. #if defined(BIG_ENDIAN) || !defined(ALLOW_MISALIGNED)
  24. byte *D=(byte *)Data;
  25. return INT32TO64(RawGet4(D+4),RawGet4(D));
  26. #else
  27. return *(uint64 *)Data;
  28. #endif
  29. }
  30. inline void RawPut2(uint Field,void *Data)
  31. {
  32. byte *D=(byte *)Data;
  33. D[0]=(byte)(Field);
  34. D[1]=(byte)(Field>>8);
  35. }
  36. inline void RawPut4(uint32 Field,void *Data)
  37. {
  38. #if defined(BIG_ENDIAN) || !defined(ALLOW_MISALIGNED)
  39. byte *D=(byte *)Data;
  40. D[0]=(byte)(Field);
  41. D[1]=(byte)(Field>>8);
  42. D[2]=(byte)(Field>>16);
  43. D[3]=(byte)(Field>>24);
  44. #else
  45. *(uint32 *)Data=Field;
  46. #endif
  47. }
  48. inline void RawPut8(uint64 Field,void *Data)
  49. {
  50. #if defined(BIG_ENDIAN) || !defined(ALLOW_MISALIGNED)
  51. byte *D=(byte *)Data;
  52. D[0]=(byte)(Field);
  53. D[1]=(byte)(Field>>8);
  54. D[2]=(byte)(Field>>16);
  55. D[3]=(byte)(Field>>24);
  56. D[4]=(byte)(Field>>32);
  57. D[5]=(byte)(Field>>40);
  58. D[6]=(byte)(Field>>48);
  59. D[7]=(byte)(Field>>56);
  60. #else
  61. *(uint64 *)Data=Field;
  62. #endif
  63. }
  64. #if defined(LITTLE_ENDIAN) && defined(ALLOW_MISALIGNED)
  65. #define USE_MEM_BYTESWAP
  66. #endif
  67. // Load 4 big endian bytes from memory and return uint32.
  68. inline uint32 RawGetBE4(const byte *m)
  69. {
  70. #if defined(USE_MEM_BYTESWAP) && defined(_MSC_VER)
  71. return _byteswap_ulong(*(uint32 *)m);
  72. #elif defined(USE_MEM_BYTESWAP) && (__GNUC__ > 3) && (__GNUC_MINOR__ > 2)
  73. return __builtin_bswap32(*(uint32 *)m);
  74. #else
  75. return uint32(m[0]<<24) | uint32(m[1]<<16) | uint32(m[2]<<8) | m[3];
  76. #endif
  77. }
  78. // Save integer to memory as big endian.
  79. inline void RawPutBE4(uint32 i,byte *mem)
  80. {
  81. #if defined(USE_MEM_BYTESWAP) && defined(_MSC_VER)
  82. *(uint32*)mem = _byteswap_ulong(i);
  83. #elif defined(USE_MEM_BYTESWAP) && (__GNUC__ > 3) && (__GNUC_MINOR__ > 2)
  84. *(uint32*)mem = __builtin_bswap32(i);
  85. #else
  86. mem[0]=byte(i>>24);
  87. mem[1]=byte(i>>16);
  88. mem[2]=byte(i>>8);
  89. mem[3]=byte(i);
  90. #endif
  91. }
  92. inline uint32 ByteSwap32(uint32 i)
  93. {
  94. #ifdef _MSC_VER
  95. return _byteswap_ulong(i);
  96. #elif (__GNUC__ > 3) && (__GNUC_MINOR__ > 2)
  97. return __builtin_bswap32(i);
  98. #else
  99. return (rotl32(i,24)&0xFF00FF00)|(rotl32(i,8)&0x00FF00FF);
  100. #endif
  101. }
  102. #endif