swap.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <u.h>
  2. /*
  3. * big-endian short
  4. */
  5. ushort
  6. beswab(ushort s)
  7. {
  8. uchar *p;
  9. p = (uchar*)&s;
  10. return (p[0]<<8) | p[1];
  11. }
  12. /*
  13. * big-endian long
  14. */
  15. ulong
  16. beswal(ulong l)
  17. {
  18. uchar *p;
  19. p = (uchar*)&l;
  20. return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  21. }
  22. /*
  23. * big-endian vlong
  24. */
  25. uvlong
  26. beswav(uvlong v)
  27. {
  28. uchar *p;
  29. p = (uchar*)&v;
  30. return ((uvlong)p[0]<<56) | ((uvlong)p[1]<<48) | ((uvlong)p[2]<<40)
  31. | ((uvlong)p[3]<<32) | ((uvlong)p[4]<<24)
  32. | ((uvlong)p[5]<<16) | ((uvlong)p[6]<<8)
  33. | (uvlong)p[7];
  34. }
  35. /*
  36. * little-endian short
  37. */
  38. ushort
  39. leswab(ushort s)
  40. {
  41. uchar *p;
  42. p = (uchar*)&s;
  43. return (p[1]<<8) | p[0];
  44. }
  45. /*
  46. * little-endian long
  47. */
  48. ulong
  49. leswal(ulong l)
  50. {
  51. uchar *p;
  52. p = (uchar*)&l;
  53. return (p[3]<<24) | (p[2]<<16) | (p[1]<<8) | p[0];
  54. }
  55. /*
  56. * little-endian vlong
  57. */
  58. uvlong
  59. leswav(uvlong v)
  60. {
  61. uchar *p;
  62. p = (uchar*)&v;
  63. return ((uvlong)p[7]<<56) | ((uvlong)p[6]<<48) | ((uvlong)p[5]<<40)
  64. | ((uvlong)p[4]<<32) | ((uvlong)p[3]<<24)
  65. | ((uvlong)p[2]<<16) | ((uvlong)p[1]<<8)
  66. | (uvlong)p[0];
  67. }