12 /* The abstracted result of an CU21 insn */
14 uint64_t addr1
; // target
16 uint64_t addr2
; // source
21 /* Define various input buffers. */
23 /* U+0000 to U+007f: Result is 1 byte for each uint16_t */
24 uint16_t pattern1
[] = {
25 0x0000, 0x007f, /* corner cases */
26 0x0047, 0x0056, 0x0045, 0x0021, 0x007b, 0x003a /* misc */
29 /* U+0080 to U+07ff: Result is 2 bytes for each uint16_t */
30 uint16_t pattern2
[] = {
31 0x0080, 0x07ff, /* corner cases */
32 0x07df, 0x008f, 0x0100, 0x017f, 0x052f, 0x0600, 0x06ff /* misc */
35 /* U+0800 to U+d7ff: Result is 3 bytes for each uint16_t
36 U+dc00 to U+ffff: Result is 3 bytes for each uint16_t */
37 uint16_t pattern3
[] = {
38 0x0800, 0xd7ff, /* corner cases */
39 0xdc00, 0xffff, /* corner cases */
40 0x083f, 0x1a21, 0x1b10, 0x2200, 0x225e, 0x22c9, 0xe001 /* misc */
43 /* U+d800 to U+dbff: Result is 4 bytes for each uint16_t pair */
44 uint16_t pattern4
[] = {
45 0xd800, 0xdc00, /* left corner case */
46 0xdbff, 0xdfff, /* right corner case */
47 0xdada, 0xdddd, 0xdeaf, 0xdcdc /* misc */
50 /* Invalid low surrogate */
51 uint16_t invalid
[] = { 0xd801, 0x0098 };
58 0xd800, 0xdc01 /* 4 bytes */
61 /* This is the buffer for the converted bytes. */
62 uint8_t buff
[1000]; /* Large so we con'don't have to worry about it */
64 void write_and_check(uint16_t *, unsigned, unsigned);
68 do_cu21(uint8_t *dst
, uint64_t dst_len
, uint16_t *src
, uint64_t src_len
)
73 /* build up the register pairs */
74 register uint16_t *source
asm("4") = src
;
75 register uint64_t source_len
asm("5") = src_len
;
76 register uint8_t *dest
asm("2") = dst
;
77 register uint64_t dest_len
asm("3") = dst_len
;
83 : "+d"(dest
), "+d"(source
), "=d"(cc
),
84 "+d"(source_len
), "+d"(dest_len
)
88 /* Capture register contents at end of cu21 */
89 regs
.addr1
= (uint64_t)dest
;
91 regs
.addr2
= (uint64_t)source
;
92 regs
.len2
= source_len
;
99 run_test(uint8_t *dst
, uint64_t dst_len
, uint16_t *src
, uint64_t src_len
)
104 result
= do_cu21(dst
, dst_len
, src
, src_len
);
106 // Write out the converted bytes, if any
108 if (dst_len
- result
.len1
== 0)
111 for (i
= 0; i
< dst_len
- result
.len1
; i
++) {
112 printf(" %02x", dst
[i
]);
116 printf(" cc = %d\n", result
.cc
);
118 printf(" dst address difference: %"PRId64
, result
.addr1
- (uint64_t)dst
);
119 printf(" dst len: %"PRId64
"\n", result
.len1
);
122 printf(" src address difference: %"PRId64
, result
.addr2
- (uint64_t)src
);
123 printf(" src len: %"PRId64
"\n", result
.len2
);
128 /* Length == 0, no memory should be read or written */
129 printf("\n------------- test1 ----------------\n");
130 run_test(NULL
, 0, NULL
, 0);
132 /* Test exhaustion of source length (source bytes are valid) */
133 printf("\n------------- test2.1 ----------------\n");
135 /* No character will be written to BUFF, i.e. loop in jitted code
137 run_test(buff
, sizeof buff
, NULL
, 1);
138 run_test(buff
, sizeof buff
, pattern1
, 1);
139 run_test(buff
, sizeof buff
, pattern2
, 1);
140 run_test(buff
, sizeof buff
, pattern3
, 1);
141 run_test(buff
, sizeof buff
, pattern4
, 1);
142 run_test(buff
, sizeof buff
, pattern4
, 2);
143 run_test(buff
, sizeof buff
, pattern4
, 3);
145 printf("\n------------- test2.2 ----------------\n");
146 /* At least one character will be written to BUFF, i.e. loop in jitted
148 run_test(buff
, sizeof buff
, pattern1
, 3);
149 run_test(buff
, sizeof buff
, pattern2
, 5);
150 run_test(buff
, sizeof buff
, pattern3
, 7);
151 run_test(buff
, sizeof buff
, pattern4
, 9);
152 run_test(buff
, sizeof buff
, pattern4
, 10);
153 run_test(buff
, sizeof buff
, pattern4
, 11);
155 /* Test exhaustion of destination length (source bytes are valid) */
156 printf("\n------------- test3.1 ----------------\n");
158 /* No character will be written to BUFF, i.e. loop in jitted code
161 /* Want to write a single byte */
162 run_test(NULL
, 0, pattern1
, sizeof pattern1
);
164 /* Want to write two bytes */
165 run_test(NULL
, 0, pattern2
, sizeof pattern2
);
166 run_test(NULL
, 1, pattern2
, sizeof pattern2
);
168 /* Want to write three bytes */
169 run_test(NULL
, 0, pattern3
, sizeof pattern3
);
170 run_test(NULL
, 1, pattern3
, sizeof pattern3
);
171 run_test(NULL
, 2, pattern3
, sizeof pattern3
);
173 /* Want to write four bytes */
174 run_test(NULL
, 0, pattern4
, sizeof pattern4
);
175 run_test(NULL
, 1, pattern4
, sizeof pattern4
);
176 run_test(NULL
, 2, pattern4
, sizeof pattern4
);
177 run_test(NULL
, 3, pattern4
, sizeof pattern4
);
179 printf("\n------------- test3.2 ----------------\n");
180 /* At least one character will be written to BUFF, i.e. loop in jitted
182 run_test(buff
, 3, pattern1
, sizeof pattern1
);
184 run_test(buff
, 5, pattern2
, sizeof pattern2
);
186 run_test(buff
, 7, pattern3
, sizeof pattern3
);
187 run_test(buff
, 8, pattern3
, sizeof pattern3
);
189 run_test(buff
, 9, pattern4
, sizeof pattern4
);
190 run_test(buff
, 10, pattern4
, sizeof pattern4
);
191 run_test(buff
, 11, pattern4
, sizeof pattern4
);
193 /* When both operands are exhausted, cc=0 takes precedence.
194 (test1 tests this for len == 0) */
195 printf("\n------------- test4 ----------------\n");
196 run_test(buff
, 6, pattern1
, 6);
198 /* Input has invalid low surrogate. */
199 printf("\n------------- test5 ----------------\n");
200 run_test(buff
, sizeof buff
, invalid
, sizeof invalid
);
201 run_test(buff
, 0, invalid
, sizeof invalid
);
203 /* Convert all pattern buffers */
204 printf("\n------------- test6 ----------------\n");
205 run_test(buff
, sizeof buff
, pattern1
, sizeof pattern1
);
206 run_test(buff
, sizeof buff
, pattern2
, sizeof pattern2
);
207 run_test(buff
, sizeof buff
, pattern3
, sizeof pattern3
);
208 run_test(buff
, sizeof buff
, pattern4
, sizeof pattern4
);
209 run_test(buff
, sizeof buff
, mixed
, sizeof mixed
);
211 /* Make sure we only write the exact number of bytes (and not more) */
215 printf("\n------------- test7.1 ----------------\n");
217 write_and_check(pat
, 2, 1);
220 printf("\n------------- test7.2 ----------------\n");
222 write_and_check(pat
, 2, 2);
225 printf("\n------------- test7.3 ----------------\n");
227 write_and_check(pat
, 2, 3);
230 printf("\n------------- test7.4 ----------------\n");
233 write_and_check(pat
, 2, 4);
240 write_and_check_aux(uint16_t *input
, unsigned num_input_bytes
,
241 unsigned num_expected_output_bytes
,
246 /* Fill output buffer with FILL_BYTE */
247 memset(buff
, fill_byte
, sizeof buff
);
250 run_test(buff
, sizeof buff
, input
, num_input_bytes
);
252 /* Make sure the rest of the buffer is unmodified. */
254 for (i
= num_expected_output_bytes
; i
< sizeof buff
; ++i
)
255 if (buff
[i
] != fill_byte
) ++num_errors
;
257 fprintf(stderr
, "*** wrote more than %u bytes\n",
258 num_expected_output_bytes
);
262 write_and_check(uint16_t *input
, unsigned num_input_bytes
,
263 unsigned num_expected_output_bytes
)
265 write_and_check_aux(input
, num_input_bytes
, num_expected_output_bytes
, 0x0);
267 /* Run again with different fill pattern to make sure we did not write
269 write_and_check_aux(input
, num_input_bytes
, num_expected_output_bytes
, 0xFF);