big_endian.h 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 big-endian format.
  14. */
  15. #define float4store(T,A) do { *(T)= ((uchar *) &A)[3];\
  16. *((T)+1)=(char) ((uchar *) &A)[2];\
  17. *((T)+2)=(char) ((uchar *) &A)[1];\
  18. *((T)+3)=(char) ((uchar *) &A)[0]; } while(0)
  19. #define float4get(V,M) do { float def_temp;\
  20. ((uchar*) &def_temp)[0]=(M)[3];\
  21. ((uchar*) &def_temp)[1]=(M)[2];\
  22. ((uchar*) &def_temp)[2]=(M)[1];\
  23. ((uchar*) &def_temp)[3]=(M)[0];\
  24. (V)=def_temp; } while(0)
  25. #define float8store(T,V) do { *(T)= ((uchar *) &V)[7];\
  26. *((T)+1)=(char) ((uchar *) &V)[6];\
  27. *((T)+2)=(char) ((uchar *) &V)[5];\
  28. *((T)+3)=(char) ((uchar *) &V)[4];\
  29. *((T)+4)=(char) ((uchar *) &V)[3];\
  30. *((T)+5)=(char) ((uchar *) &V)[2];\
  31. *((T)+6)=(char) ((uchar *) &V)[1];\
  32. *((T)+7)=(char) ((uchar *) &V)[0]; } while(0)
  33. #define float8get(V,M) do { double def_temp;\
  34. ((uchar*) &def_temp)[0]=(M)[7];\
  35. ((uchar*) &def_temp)[1]=(M)[6];\
  36. ((uchar*) &def_temp)[2]=(M)[5];\
  37. ((uchar*) &def_temp)[3]=(M)[4];\
  38. ((uchar*) &def_temp)[4]=(M)[3];\
  39. ((uchar*) &def_temp)[5]=(M)[2];\
  40. ((uchar*) &def_temp)[6]=(M)[1];\
  41. ((uchar*) &def_temp)[7]=(M)[0];\
  42. (V) = def_temp; } while(0)
  43. #define ushortget(V,M) do { V = (uint16) (((uint16) ((uchar) (M)[1]))+\
  44. ((uint16) ((uint16) (M)[0]) << 8)); } while(0)
  45. #define shortget(V,M) do { V = (short) (((short) ((uchar) (M)[1]))+\
  46. ((short) ((short) (M)[0]) << 8)); } while(0)
  47. #define longget(V,M) do { int32 def_temp;\
  48. ((uchar*) &def_temp)[0]=(M)[0];\
  49. ((uchar*) &def_temp)[1]=(M)[1];\
  50. ((uchar*) &def_temp)[2]=(M)[2];\
  51. ((uchar*) &def_temp)[3]=(M)[3];\
  52. (V)=def_temp; } while(0)
  53. #define ulongget(V,M) do { uint32 def_temp;\
  54. ((uchar*) &def_temp)[0]=(M)[0];\
  55. ((uchar*) &def_temp)[1]=(M)[1];\
  56. ((uchar*) &def_temp)[2]=(M)[2];\
  57. ((uchar*) &def_temp)[3]=(M)[3];\
  58. (V)=def_temp; } while(0)
  59. #define shortstore(T,A) do { uint def_temp=(uint) (A) ;\
  60. *(((char*)T)+1)=(char)(def_temp); \
  61. *(((char*)T)+0)=(char)(def_temp >> 8); } while(0)
  62. #define longstore(T,A) do { *(((char*)T)+3)=((A));\
  63. *(((char*)T)+2)=(((A) >> 8));\
  64. *(((char*)T)+1)=(((A) >> 16));\
  65. *(((char*)T)+0)=(((A) >> 24)); } while(0)
  66. #define floatget(V,M) memcpy(&V, (M), sizeof(float))
  67. /* Cast away type qualifiers (necessary as macro takes argument by value). */
  68. #define floatstore(T,V) memcpy((T), (void*) (&V), sizeof(float))
  69. #define doubleget(V,M) memcpy(&V, (M), sizeof(double))
  70. /* Cast away type qualifiers (necessary as macro takes argument by value). */
  71. #define doublestore(T,V) memcpy((T), (void*) &V, sizeof(double))
  72. #define longlongget(V,M) memcpy(&V, (M), sizeof(ulonglong))
  73. #define longlongstore(T,V) memcpy((T), &V, sizeof(ulonglong))