byteswap.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /* Macros and inline functions to swap the order of bytes in integer values.
  17. Copyright (C) 1997-2026 Free Software Foundation, Inc.
  18. This file is part of the GNU C Library.
  19. The GNU C Library is free software; you can redistribute it and/or
  20. modify it under the terms of the GNU Lesser General Public
  21. License as published by the Free Software Foundation; either
  22. version 2.1 of the License, or (at your option) any later version.
  23. The GNU C Library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with the GNU C Library; if not, see
  29. <https://www.gnu.org/licenses/>. */
  30. #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H
  31. # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
  32. #endif
  33. #ifndef _BITS_BYTESWAP_H
  34. #define _BITS_BYTESWAP_H 1
  35. #include <features.h>
  36. #include <bits/types.h>
  37. /* Swap bytes in 16-bit value. */
  38. #define __bswap_constant_16(x) \
  39. ((__uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
  40. static __inline __uint16_t
  41. __bswap_16 (__uint16_t __bsx)
  42. {
  43. #if __GNUC_PREREQ (4, 8)
  44. return __builtin_bswap16 (__bsx);
  45. #else
  46. return __bswap_constant_16 (__bsx);
  47. #endif
  48. }
  49. /* Swap bytes in 32-bit value. */
  50. #define __bswap_constant_32(x) \
  51. ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) \
  52. | (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
  53. static __inline __uint32_t
  54. __bswap_32 (__uint32_t __bsx)
  55. {
  56. #if __GNUC_PREREQ (4, 3)
  57. return __builtin_bswap32 (__bsx);
  58. #else
  59. return __bswap_constant_32 (__bsx);
  60. #endif
  61. }
  62. /* Swap bytes in 64-bit value. */
  63. #define __bswap_constant_64(x) \
  64. ((((x) & 0xff00000000000000ull) >> 56) \
  65. | (((x) & 0x00ff000000000000ull) >> 40) \
  66. | (((x) & 0x0000ff0000000000ull) >> 24) \
  67. | (((x) & 0x000000ff00000000ull) >> 8) \
  68. | (((x) & 0x00000000ff000000ull) << 8) \
  69. | (((x) & 0x0000000000ff0000ull) << 24) \
  70. | (((x) & 0x000000000000ff00ull) << 40) \
  71. | (((x) & 0x00000000000000ffull) << 56))
  72. __extension__ static __inline __uint64_t
  73. __bswap_64 (__uint64_t __bsx)
  74. {
  75. #if __GNUC_PREREQ (4, 3)
  76. return __builtin_bswap64 (__bsx);
  77. #else
  78. return __bswap_constant_64 (__bsx);
  79. #endif
  80. }
  81. #endif /* _BITS_BYTESWAP_H */