9
3

little_endian.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA */
  12. /*
  13. Data in little-endian format.
  14. */
  15. #ifndef MY_BYTE_ORDER_ARCH_OPTIMIZED
  16. #define float4get(V,M) memcpy(&V, (M), sizeof(float))
  17. #define float4store(V,M) memcpy(V, (&M), sizeof(float))
  18. #define float8get(V,M) doubleget((V),(M))
  19. #define float8store(V,M) doublestore((V),(M))
  20. /* Bi-endian hardware.... */
  21. #if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
  22. #define doublestore(T,V) do { *(((char*)T)+0)=(char) ((uchar *) &V)[4];\
  23. *(((char*)T)+1)=(char) ((uchar *) &V)[5];\
  24. *(((char*)T)+2)=(char) ((uchar *) &V)[6];\
  25. *(((char*)T)+3)=(char) ((uchar *) &V)[7];\
  26. *(((char*)T)+4)=(char) ((uchar *) &V)[0];\
  27. *(((char*)T)+5)=(char) ((uchar *) &V)[1];\
  28. *(((char*)T)+6)=(char) ((uchar *) &V)[2];\
  29. *(((char*)T)+7)=(char) ((uchar *) &V)[3]; }\
  30. while(0)
  31. #define doubleget(V,M) do { double def_temp;\
  32. ((uchar*) &def_temp)[0]=(M)[4];\
  33. ((uchar*) &def_temp)[1]=(M)[5];\
  34. ((uchar*) &def_temp)[2]=(M)[6];\
  35. ((uchar*) &def_temp)[3]=(M)[7];\
  36. ((uchar*) &def_temp)[4]=(M)[0];\
  37. ((uchar*) &def_temp)[5]=(M)[1];\
  38. ((uchar*) &def_temp)[6]=(M)[2];\
  39. ((uchar*) &def_temp)[7]=(M)[3];\
  40. (V) = def_temp; } while(0)
  41. #else /* Bi-endian hardware.... */
  42. /* Cast away type qualifiers (necessary as macro takes argument by value). */
  43. #define doublestore(T,V) memcpy((T), (void*) &V, sizeof(double))
  44. #define doubleget(V,M) memcpy(&V, (M), sizeof(double))
  45. #endif /* Bi-endian hardware.... */
  46. #endif /* !MY_BYTE_ORDER_ARCH_OPTIMIZED */
  47. #define ushortget(V,M) do { uchar *pM= (uchar*)(M);V = uint2korr(pM);} while(0)
  48. #define shortget(V,M) do { uchar *pM= (uchar*)(M);V = sint2korr(pM);} while(0)
  49. #define longget(V,M) do { uchar *pM= (uchar*)(M);V = sint4korr(pM);} while(0)
  50. #define ulongget(V,M) do { uchar *pM= (uchar*)(M);V = uint4korr(pM);} while(0)
  51. #define shortstore(T,V) int2store(T,V)
  52. #define longstore(T,V) int4store(T,V)
  53. #ifndef floatstore
  54. /* Cast away type qualifiers (necessary as macro takes argument by value). */
  55. #define floatstore(T,V) memcpy((T), (void*) (&V), sizeof(float))
  56. #define floatget(V,M) memcpy(&V, (M), sizeof(float))
  57. #endif
  58. #ifndef doubleget
  59. #define doubleget(V,M) memcpy(&V, (M), sizeof(double))
  60. #define doublestore(T,V) memcpy((T), (void *) &V, sizeof(double))
  61. #endif /* doubleget */
  62. #define longlongget(V,M) memcpy(&V, (M), sizeof(ulonglong))
  63. #define longlongstore(T,V) memcpy((T), &V, sizeof(ulonglong))