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

little_endian.h 3.2KB

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