Clone of PhatAC @ https://github.com/floaterxk/PhatAC

big_endian.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (c) 2012, 2014, 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 02110-1301 USA */
  12. #include <string.h>
  13. /*
  14. Data in big-endian format.
  15. */
  16. static inline void float4store(uchar *T, float A)
  17. { *(T)= ((uchar *) &A)[3];
  18. *((T)+1)=(char) ((uchar *) &A)[2];
  19. *((T)+2)=(char) ((uchar *) &A)[1];
  20. *((T)+3)=(char) ((uchar *) &A)[0]; }
  21. static inline void float4get (float *V, const uchar *M)
  22. { float def_temp;
  23. ((uchar*) &def_temp)[0]=(M)[3];
  24. ((uchar*) &def_temp)[1]=(M)[2];
  25. ((uchar*) &def_temp)[2]=(M)[1];
  26. ((uchar*) &def_temp)[3]=(M)[0];
  27. (*V)=def_temp; }
  28. static inline void float8store(uchar *T, double V)
  29. { *(T)= ((uchar *) &V)[7];
  30. *((T)+1)=(char) ((uchar *) &V)[6];
  31. *((T)+2)=(char) ((uchar *) &V)[5];
  32. *((T)+3)=(char) ((uchar *) &V)[4];
  33. *((T)+4)=(char) ((uchar *) &V)[3];
  34. *((T)+5)=(char) ((uchar *) &V)[2];
  35. *((T)+6)=(char) ((uchar *) &V)[1];
  36. *((T)+7)=(char) ((uchar *) &V)[0]; }
  37. static inline void float8get (double *V, const uchar *M)
  38. { double def_temp;
  39. ((uchar*) &def_temp)[0]=(M)[7];
  40. ((uchar*) &def_temp)[1]=(M)[6];
  41. ((uchar*) &def_temp)[2]=(M)[5];
  42. ((uchar*) &def_temp)[3]=(M)[4];
  43. ((uchar*) &def_temp)[4]=(M)[3];
  44. ((uchar*) &def_temp)[5]=(M)[2];
  45. ((uchar*) &def_temp)[6]=(M)[1];
  46. ((uchar*) &def_temp)[7]=(M)[0];
  47. (*V) = def_temp; }
  48. static inline void ushortget(uint16 *V, const uchar *pM)
  49. { *V = (uint16) (((uint16) ((uchar) (pM)[1]))+
  50. ((uint16) ((uint16) (pM)[0]) << 8)); }
  51. static inline void shortget (int16 *V, const uchar *pM)
  52. { *V = (short) (((short) ((uchar) (pM)[1]))+
  53. ((short) ((short) (pM)[0]) << 8)); }
  54. static inline void longget (int32 *V, const uchar *pM)
  55. { int32 def_temp;
  56. ((uchar*) &def_temp)[0]=(pM)[0];
  57. ((uchar*) &def_temp)[1]=(pM)[1];
  58. ((uchar*) &def_temp)[2]=(pM)[2];
  59. ((uchar*) &def_temp)[3]=(pM)[3];
  60. (*V)=def_temp; }
  61. static inline void ulongget (uint32 *V, const uchar *pM)
  62. { uint32 def_temp;
  63. ((uchar*) &def_temp)[0]=(pM)[0];
  64. ((uchar*) &def_temp)[1]=(pM)[1];
  65. ((uchar*) &def_temp)[2]=(pM)[2];
  66. ((uchar*) &def_temp)[3]=(pM)[3];
  67. (*V)=def_temp; }
  68. static inline void shortstore(uchar *T, int16 A)
  69. { uint def_temp=(uint) (A) ;
  70. *(((char*)T)+1)=(char)(def_temp);
  71. *(((char*)T)+0)=(char)(def_temp >> 8); }
  72. static inline void longstore (uchar *T, int32 A)
  73. { *(((char*)T)+3)=((A));
  74. *(((char*)T)+2)=(((A) >> 8));
  75. *(((char*)T)+1)=(((A) >> 16));
  76. *(((char*)T)+0)=(((A) >> 24)); }
  77. static inline void floatget(float *V, const uchar *M)
  78. {
  79. memcpy(V, (M), sizeof(float));
  80. }
  81. static inline void floatstore(uchar *T, float V)
  82. {
  83. memcpy((T), (&V), sizeof(float));
  84. }
  85. static inline void doubleget(double *V, const uchar *M)
  86. {
  87. memcpy(V, (M), sizeof(double));
  88. }
  89. static inline void doublestore(uchar *T, double V)
  90. {
  91. memcpy((T), &V, sizeof(double));
  92. }
  93. static inline void longlongget(longlong *V, const uchar *M)
  94. {
  95. memcpy(V, (M), sizeof(ulonglong));
  96. }
  97. static inline void longlongstore(uchar *T, longlong V)
  98. {
  99. memcpy((T), &V, sizeof(ulonglong));
  100. }