1 /* Test memcmp/strncmp builtin expansion for compilation and proper execution. */
3 /* { dg-options "-O2" } */
4 /* { dg-require-effective-target ptr32plus } */
11 int lib_memcmp(const void *a
, const void *b
, size_t n
) asm("memcmp");
12 int lib_strncmp(const char *a
, const char *b
, size_t n
) asm("strncmp");
31 #define DEF_RS(ALIGN) \
32 static void test_memcmp_runtime_size_ ## ALIGN (const char *str1, \
34 size_t sz, int expect) \
36 char three[8192] __attribute__ ((aligned (4096))); \
37 char four[8192] __attribute__ ((aligned (4096))); \
40 for (j = 0; j < 2; j++) \
42 for (i = 0; i < 2; i++) \
44 a = three+i*ALIGN+j*(4096-2*i*ALIGN); \
45 b = four+i*ALIGN+j*(4096-2*i*ALIGN); \
49 if ( r < 0 && !(expect < 0) ) abort(); \
50 if ( r > 0 && !(expect > 0) ) abort(); \
51 if ( r == 0 && !(expect == 0) ) abort(); \
62 static void test_memcmp_runtime_size (const char *str1
, const char *str2
,
63 size_t sz
, int expect
)
65 char three
[8192] __attribute__ ((aligned (4096)));
66 char four
[8192] __attribute__ ((aligned (4096)));
69 test_memcmp_runtime_size_1 (str1
,str2
,sz
,expect
);
70 test_memcmp_runtime_size_2 (str1
,str2
,sz
,expect
);
71 test_memcmp_runtime_size_4 (str1
,str2
,sz
,expect
);
72 test_memcmp_runtime_size_8 (str1
,str2
,sz
,expect
);
73 test_memcmp_runtime_size_16 (str1
,str2
,sz
,expect
);
74 for (j
= 0; j
< 2; j
++)
76 for (i
= 0; i
< 2; i
++)
78 for (a1
=0; a1
< 2*sizeof(void *); a1
++)
80 a
= three
+i
*a1
+j
*(4096-2*i
*a1
);
82 for (a2
=0; a2
< 2*sizeof(void *); a2
++)
84 b
= four
+i
*a2
+j
*(4096-2*i
*a2
);
87 if ( r
< 0 && !(expect
< 0) ) abort();
88 if ( r
> 0 && !(expect
> 0) ) abort();
89 if ( r
== 0 && !(expect
== 0) ) abort();
96 static void test_driver_memcmp (void (test_memcmp
)(const char *, const char *, int),
97 void (test_strncmp
)(const char *, const char *, int),
100 char buf1
[MAX_SZ
*2+TZONE
],buf2
[MAX_SZ
*2+TZONE
];
101 size_t test_sz
= (sz
<MAX_SZ
)?sz
:MAX_SZ
;
102 size_t diff_pos
, zero_pos
;
106 for(i
=0;i
<NRAND
/sz
;i
++) {
108 buf1
[j
] = rand() & 0xff;
112 buf1
[j
] = rand() & 0xff;
113 buf2
[j
] = rand() & 0xff;
115 e
= lib_memcmp(buf1
,buf2
,sz
);
116 (*test_memcmp
)(buf1
,buf2
,e
);
117 test_memcmp_runtime_size (buf1
, buf2
, sz
, e
);
118 e
= lib_strncmp(buf1
,buf2
,sz
);
119 (*test_strncmp
)(buf1
,buf2
,e
);
122 for(diff_pos
= ((test_sz
>TZONE
)?(test_sz
-TZONE
):0); diff_pos
< test_sz
+TZONE
; diff_pos
++)
123 for(zero_pos
= ((test_sz
>TZONE
)?(test_sz
-TZONE
):0); zero_pos
< test_sz
+TZONE
; zero_pos
++)
125 memset(buf1
, 'A', 2*test_sz
);
126 memset(buf2
, 'A', 2*test_sz
);
127 buf2
[diff_pos
] = 'B';
130 e
= lib_memcmp(buf1
,buf2
,sz
);
131 (*test_memcmp
)(buf1
,buf2
,e
);
132 (*test_memcmp
)(buf2
,buf1
,-e
);
133 (*test_memcmp
)(buf2
,buf2
,0);
134 test_memcmp_runtime_size (buf1
, buf2
, sz
, e
);
135 test_memcmp_runtime_size (buf2
, buf1
, sz
, -e
);
136 e
= lib_strncmp(buf1
,buf2
,sz
);
137 (*test_strncmp
)(buf1
,buf2
,e
);
138 (*test_strncmp
)(buf2
,buf1
,-e
);
139 (*test_strncmp
)(buf2
,buf2
,0);
140 /* differing length: */
142 e
= lib_memcmp(buf1
,buf2
,sz
);
143 (*test_memcmp
)(buf1
,buf2
,e
);
144 test_memcmp_runtime_size (buf1
, buf2
, sz
, e
);
145 e
= lib_strncmp(buf1
,buf2
,sz
);
146 (*test_strncmp
)(buf1
,buf2
,e
);
147 memset(buf2
+diff_pos
,'B',sizeof(buf2
)-diff_pos
);
149 e
= lib_memcmp(buf1
,buf2
,sz
);
150 (*test_memcmp
)(buf1
,buf2
,e
);
151 (*test_memcmp
)(buf2
,buf1
,-e
);
152 test_memcmp_runtime_size (buf1
, buf2
, sz
, e
);
153 test_memcmp_runtime_size (buf2
, buf1
, sz
, -e
);
154 e
= lib_strncmp(buf1
,buf2
,sz
);
155 (*test_strncmp
)(buf1
,buf2
,e
);
156 (*test_strncmp
)(buf2
,buf1
,-e
);
160 #define RUN_TEST(SZ, ALIGN) test_driver_memcmp (test_memcmp_ ## SZ ## _ ## ALIGN, test_strncmp_ ## SZ ## _ ## ALIGN, SZ, ALIGN);
162 #define DEF_TEST(SZ, ALIGN) \
163 static void test_memcmp_ ## SZ ## _ ## ALIGN (const char *str1, const char *str2, int expect) \
165 char three[8192] __attribute__ ((aligned (4096))); \
166 char four[8192] __attribute__ ((aligned (4096))); \
169 for (j = 0; j < 2; j++) \
171 for (i = 0; i < 2; i++) \
173 a = three+i*ALIGN+j*(4096-2*i*ALIGN); \
174 b = four+i*ALIGN+j*(4096-2*i*ALIGN); \
177 r = memcmp(a,b,SZ); \
178 if ( r < 0 && !(expect < 0) ) abort(); \
179 if ( r > 0 && !(expect > 0) ) abort(); \
180 if ( r == 0 && !(expect == 0) ) abort(); \
184 static void test_strncmp_ ## SZ ## _ ## ALIGN (const char *str1, const char *str2, int expect) \
186 char three[8192] __attribute__ ((aligned (4096))); \
187 char four[8192] __attribute__ ((aligned (4096))); \
190 for (j = 0; j < 2; j++) \
192 for (i = 0; i < 2; i++) \
194 a = three+i*ALIGN+j*(4096-2*i*ALIGN); \
195 b = four+i*ALIGN+j*(4096-2*i*ALIGN); \
198 r = strncmp(a,b,SZ); \
199 if ( r < 0 && !(expect < 0) ) abort(); \
200 if ( r > 0 && !(expect > 0) ) abort(); \
201 if ( r == 0 && !(expect == 0) ) abort(); \
485 main(int argc
, char **argv
)