swap.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. /*
  11. * big-endian short
  12. */
  13. uint16_t
  14. beswab(uint16_t s)
  15. {
  16. uint8_t *p;
  17. p = (uint8_t*)&s;
  18. return (p[0]<<8) | p[1];
  19. }
  20. /*
  21. * big-endian long
  22. */
  23. uint32_t
  24. beswal(uint32_t l)
  25. {
  26. uint8_t *p;
  27. p = (uint8_t*)&l;
  28. return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  29. }
  30. /*
  31. * big-endian vlong
  32. */
  33. uint64_t
  34. beswav(uint64_t v)
  35. {
  36. uint8_t *p;
  37. p = (uint8_t*)&v;
  38. return ((uint64_t)p[0]<<56) | ((uint64_t)p[1]<<48) | ((uint64_t)p[2]<<40)
  39. | ((uint64_t)p[3]<<32) | ((uint64_t)p[4]<<24)
  40. | ((uint64_t)p[5]<<16) | ((uint64_t)p[6]<<8)
  41. | (uint64_t)p[7];
  42. }
  43. /*
  44. * little-endian short
  45. */
  46. uint16_t
  47. leswab(uint16_t s)
  48. {
  49. uint8_t *p;
  50. p = (uint8_t*)&s;
  51. return (p[1]<<8) | p[0];
  52. }
  53. /*
  54. * little-endian long
  55. */
  56. uint32_t
  57. leswal(uint32_t l)
  58. {
  59. uint8_t *p;
  60. p = (uint8_t*)&l;
  61. return (p[3]<<24) | (p[2]<<16) | (p[1]<<8) | p[0];
  62. }
  63. /*
  64. * little-endian vlong
  65. */
  66. uint64_t
  67. leswav(uint64_t v)
  68. {
  69. uint8_t *p;
  70. p = (uint8_t*)&v;
  71. return ((uint64_t)p[7]<<56) | ((uint64_t)p[6]<<48) | ((uint64_t)p[5]<<40)
  72. | ((uint64_t)p[4]<<32) | ((uint64_t)p[3]<<24)
  73. | ((uint64_t)p[2]<<16) | ((uint64_t)p[1]<<8)
  74. | (uint64_t)p[0];
  75. }