byteswap.in.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* byteswap.h - Byte swapping
  2. Copyright (C) 2005, 2007, 2009-2014 Free Software Foundation, Inc.
  3. Written by Oskar Liljeblad <oskar@osk.mine.nu>, 2005.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef _GL_BYTESWAP_H
  15. #define _GL_BYTESWAP_H
  16. /* Given an unsigned 16-bit argument X, return the value corresponding to
  17. X with reversed byte order. */
  18. #define bswap_16(x) ((((x) & 0x00FF) << 8) | \
  19. (((x) & 0xFF00) >> 8))
  20. /* Given an unsigned 32-bit argument X, return the value corresponding to
  21. X with reversed byte order. */
  22. #define bswap_32(x) ((((x) & 0x000000FF) << 24) | \
  23. (((x) & 0x0000FF00) << 8) | \
  24. (((x) & 0x00FF0000) >> 8) | \
  25. (((x) & 0xFF000000) >> 24))
  26. /* Given an unsigned 64-bit argument X, return the value corresponding to
  27. X with reversed byte order. */
  28. #define bswap_64(x) ((((x) & 0x00000000000000FFULL) << 56) | \
  29. (((x) & 0x000000000000FF00ULL) << 40) | \
  30. (((x) & 0x0000000000FF0000ULL) << 24) | \
  31. (((x) & 0x00000000FF000000ULL) << 8) | \
  32. (((x) & 0x000000FF00000000ULL) >> 8) | \
  33. (((x) & 0x0000FF0000000000ULL) >> 24) | \
  34. (((x) & 0x00FF000000000000ULL) >> 40) | \
  35. (((x) & 0xFF00000000000000ULL) >> 56))
  36. #endif /* _GL_BYTESWAP_H */