tst-endian.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <byteswap.h>
  2. #include <endian.h>
  3. #include <inttypes.h>
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <libc-diag.h>
  7. #if __GNUC_PREREQ (6, 0)
  8. /* GCC 6.0 warns on big endian systems about:
  9. htobeXX (beXXtoh (i)) != i
  10. warning: self-comparison always evaluates to false [-Wtautological-compare]
  11. because htobeXX(x) and beXXtoh(x) is defined to (x)
  12. in string/endian.h on big endian systems.
  13. The same applies to htoleXX/leXXtoh on little endian systems. */
  14. # define DIAG_IGNORE_NEEDS_COMMENT_TAUTOLOGICAL_COMPARE() \
  15. DIAG_IGNORE_NEEDS_COMMENT (6, "-Wtautological-compare")
  16. #else
  17. # define DIAG_IGNORE_NEEDS_COMMENT_TAUTOLOGICAL_COMPARE()
  18. #endif
  19. int
  20. do_test (void)
  21. {
  22. int result = 0;
  23. for (uint64_t i = 0; i < (~UINT64_C (0)) >> 2; i = (i << 1) + 3)
  24. {
  25. if (i < UINT64_C (65536))
  26. {
  27. DIAG_PUSH_NEEDS_COMMENT;
  28. DIAG_IGNORE_NEEDS_COMMENT_TAUTOLOGICAL_COMPARE ();
  29. if (htobe16 (be16toh (i)) != i)
  30. {
  31. printf ("htobe16 (be16toh (%" PRIx64 ")) == %" PRIx16 "\n",
  32. i, (uint16_t) htobe16 (be16toh (i)));
  33. result = 1;
  34. }
  35. if (htole16 (le16toh (i)) != i)
  36. {
  37. printf ("htole16 (le16toh (%" PRIx64 ")) == %" PRIx16 "\n",
  38. i, (uint16_t) htole16 (le16toh (i)));
  39. result = 1;
  40. }
  41. DIAG_POP_NEEDS_COMMENT;
  42. uint16_t n[2];
  43. n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_16 (i);
  44. n[__BYTE_ORDER == __BIG_ENDIAN] = i;
  45. if (htole16 (i) != n[0])
  46. {
  47. printf ("htole16 (%" PRIx64 ") == %" PRIx16 " != %" PRIx16 "\n",
  48. i, (uint16_t) htole16 (i), n[0]);
  49. result = 1;
  50. }
  51. if (htobe16 (i) != n[1])
  52. {
  53. printf ("htobe16 (%" PRIx64 ") == %" PRIx16 " != %" PRIx16 "\n",
  54. i, (uint16_t) htobe16 (i), n[1]);
  55. result = 1;
  56. }
  57. }
  58. if (i < UINT64_C (4294967296))
  59. {
  60. DIAG_PUSH_NEEDS_COMMENT;
  61. DIAG_IGNORE_NEEDS_COMMENT_TAUTOLOGICAL_COMPARE ();
  62. if (htobe32 (be32toh (i)) != i)
  63. {
  64. printf ("htobe32 (be32toh (%" PRIx64 ")) == %" PRIx32 "\n",
  65. i, (uint32_t) htobe32 (be32toh (i)));
  66. result = 1;
  67. }
  68. if (htole32 (le32toh (i)) != i)
  69. {
  70. printf ("htole32 (le32toh (%" PRIx64 ")) == %" PRIx32 "\n",
  71. i, (uint32_t) htole32 (le32toh (i)));
  72. result = 1;
  73. }
  74. DIAG_POP_NEEDS_COMMENT;
  75. uint32_t n[2];
  76. n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_32 (i);
  77. n[__BYTE_ORDER == __BIG_ENDIAN] = i;
  78. if (htole32 (i) != n[0])
  79. {
  80. printf ("htole32 (%" PRIx64 ") == %" PRIx32 " != %" PRIx32 "\n",
  81. i, (uint32_t) htole32 (i), n[0]);
  82. result = 1;
  83. }
  84. if (htobe32 (i) != n[1])
  85. {
  86. printf ("htobe32 (%" PRIx64 ") == %" PRIx32 " != %" PRIx32 "\n",
  87. i, (uint32_t) htobe32 (i), n[1]);
  88. result = 1;
  89. }
  90. }
  91. DIAG_PUSH_NEEDS_COMMENT;
  92. DIAG_IGNORE_NEEDS_COMMENT_TAUTOLOGICAL_COMPARE ();
  93. if (htobe64 (be64toh (i)) != i)
  94. {
  95. printf ("htobe64 (be64toh (%" PRIx64 ")) == %" PRIx64 "\n",
  96. i, htobe64 (be64toh (i)));
  97. result = 1;
  98. }
  99. if (htole64 (le64toh (i)) != i)
  100. {
  101. printf ("htole64 (le64toh (%" PRIx64 ")) == %" PRIx64 "\n",
  102. i, htole64 (le64toh (i)));
  103. result = 1;
  104. }
  105. DIAG_POP_NEEDS_COMMENT;
  106. uint64_t n[2];
  107. n[__BYTE_ORDER == __LITTLE_ENDIAN] = bswap_64 (i);
  108. n[__BYTE_ORDER == __BIG_ENDIAN] = i;
  109. if (htole64 (i) != n[0])
  110. {
  111. printf ("htole64 (%" PRIx64 ") == %" PRIx64 " != %" PRIx64 "\n",
  112. i, htole64 (i), n[0]);
  113. result = 1;
  114. }
  115. if (htobe64 (i) != n[1])
  116. {
  117. printf ("htobe64 (%" PRIx64 ") == %" PRIx64 " != %" PRIx64 "\n",
  118. i, htobe64 (i), n[1]);
  119. result = 1;
  120. }
  121. }
  122. return result;
  123. }
  124. #include <support/test-driver.c>