endian.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright (C) 1992, 1996, 1997, 2000 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library 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 GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifndef _ENDIAN_H
  15. #define _ENDIAN_H 1
  16. #include <features.h>
  17. /* Definitions for byte order, according to significance of bytes,
  18. from low addresses to high addresses. The value is what you get by
  19. putting '4' in the most significant byte, '3' in the second most
  20. significant byte, '2' in the second least significant byte, and '1'
  21. in the least significant byte, and then writing down one digit for
  22. each byte, starting with the byte at the lowest address at the left,
  23. and proceeding to the byte with the highest address at the right. */
  24. #define __LITTLE_ENDIAN 1234
  25. #define __BIG_ENDIAN 4321
  26. #define __PDP_ENDIAN 3412
  27. /* This file defines `__BYTE_ORDER' for the particular machine. */
  28. #include <bits/endian.h>
  29. /* Some machines may need to use a different endianness for floating point
  30. values. */
  31. #ifndef __FLOAT_WORD_ORDER
  32. # define __FLOAT_WORD_ORDER __BYTE_ORDER
  33. #endif
  34. #ifdef __USE_BSD
  35. # define LITTLE_ENDIAN __LITTLE_ENDIAN
  36. # define BIG_ENDIAN __BIG_ENDIAN
  37. # define PDP_ENDIAN __PDP_ENDIAN
  38. # define BYTE_ORDER __BYTE_ORDER
  39. #endif
  40. #if __BYTE_ORDER == __LITTLE_ENDIAN
  41. # define __LONG_LONG_PAIR(HI, LO) LO, HI
  42. #elif __BYTE_ORDER == __BIG_ENDIAN
  43. # define __LONG_LONG_PAIR(HI, LO) HI, LO
  44. #endif
  45. #ifdef _LIBC
  46. # ifndef __ASSEMBLER__
  47. # include <stdint.h>
  48. # define OFF_HI(offset) (offset >> 31)
  49. # define OFF_LO(offset) (offset)
  50. # define OFF64_HI(offset) (uint32_t)(offset >> 32)
  51. # define OFF64_LO(offset) (uint32_t)(offset & 0xffffffff)
  52. # define OFF_HI_LO(offset) __LONG_LONG_PAIR(OFF_HI(offset), OFF_LO(offset))
  53. # define OFF64_HI_LO(offset) __LONG_LONG_PAIR(OFF64_HI(offset), OFF64_LO(offset))
  54. # endif
  55. #endif
  56. #if defined(__USE_BSD) && !defined(__ASSEMBLER__)
  57. /* Conversion interfaces. */
  58. # include <byteswap.h>
  59. # if __BYTE_ORDER == __LITTLE_ENDIAN
  60. # define htobe16(x) __bswap_16 (x)
  61. # define htole16(x) (x)
  62. # define be16toh(x) __bswap_16 (x)
  63. # define le16toh(x) (x)
  64. # define htobe32(x) __bswap_32 (x)
  65. # define htole32(x) (x)
  66. # define be32toh(x) __bswap_32 (x)
  67. # define le32toh(x) (x)
  68. # define htobe64(x) __bswap_64 (x)
  69. # define htole64(x) (x)
  70. # define be64toh(x) __bswap_64 (x)
  71. # define le64toh(x) (x)
  72. # else
  73. # define htobe16(x) (x)
  74. # define htole16(x) __bswap_16 (x)
  75. # define be16toh(x) (x)
  76. # define le16toh(x) __bswap_16 (x)
  77. # define htobe32(x) (x)
  78. # define htole32(x) __bswap_32 (x)
  79. # define be32toh(x) (x)
  80. # define le32toh(x) __bswap_32 (x)
  81. # define htobe64(x) (x)
  82. # define htole64(x) __bswap_64 (x)
  83. # define be64toh(x) (x)
  84. # define le64toh(x) __bswap_64 (x)
  85. # endif
  86. #endif
  87. #endif /* endian.h */