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

source code of glibc/string/tst-endian.c