9 /* The abstracted result of an CU42 insn */
11 uint64_t addr1
; // target
13 uint64_t addr2
; // source
18 /* Define various input buffers. */
20 /* U+0000 to U+d7ff: Result is 2 bytes for each uint32_t
21 U+dc00 to U+ffff: Result is 2 bytes for each uint32_t */
22 uint32_t pattern2
[] = {
23 0x0000, 0xd7ff, /* corner cases */
24 0xdc00, 0xffff, /* corner cases */
25 0xabba, 0xf00d, 0xd00f, 0x1234 /* misc */
28 /* U+00010000 to U+0010ffff: Result is 4 bytes for each uint32_t */
29 uint32_t pattern4
[] = {
30 0x00010000, 0x0010ffff, /* corner cases */
31 0x00010123, 0x00023456, 0x000789ab, 0x00100000 /* misc */
34 /* Invalid UTF-32 character */
35 uint32_t invalid
[] = {
36 0x0000d800, 0x0000dbff, /* corner cases */
37 0x00110000, 0xffffffff, /* corner cases */
38 0x0000daad, 0x0000d901, 0x0000d8ff, /* misc */
39 0x00110011, 0x01000000, 0x10000000, 0xdeadbeef /* misc */
44 0x00000078 /* 2 bytes */,
45 0x0000d000 /* 2 bytes */,
46 0x00033333 /* 4 bytes */,
47 0x00040404 /* 4 bytes */,
48 0x0000abcd /* 2 bytes */,
51 /* This is the buffer for the converted bytes. */
52 uint16_t buff
[1000]; /* Large so we con'don't have to worry about it */
54 void write_and_check(uint32_t *, unsigned, unsigned);
58 do_cu42(uint16_t *dst
, uint64_t dst_len
, uint32_t *src
, uint64_t src_len
)
63 /* build up the register pairs */
64 register uint32_t *source
asm("4") = src
;
65 register uint64_t source_len
asm("5") = src_len
;
66 register uint16_t *dest
asm("2") = dst
;
67 register uint64_t dest_len
asm("3") = dst_len
;
73 : "+d"(dest
), "+d"(source
), "=d"(cc
),
74 "+d"(source_len
), "+d"(dest_len
)
78 /* Capture register contents at end of cu42 */
79 regs
.addr1
= (uint64_t)dest
;
81 regs
.addr2
= (uint64_t)source
;
82 regs
.len2
= source_len
;
89 run_test(uint16_t *dst
, uint64_t dst_len
, uint32_t *src
, uint64_t src_len
)
94 result
= do_cu42(dst
, dst_len
, src
, src_len
);
96 // Write out the converted values, if any
98 if (dst_len
- result
.len1
== 0)
101 assert((dst_len
- result
.len1
) % 2 == 0);
102 for (i
= 0; i
< (dst_len
- result
.len1
) / 2; ++i
) {
103 printf(" %04x", dst
[i
]);
108 printf(" cc = %d\n", result
.cc
);
110 printf(" dst address difference: %"PRId64
, result
.addr1
- (uint64_t)dst
);
111 printf(" dst len: %"PRId64
"\n", result
.len1
);
114 printf(" src address difference: %"PRId64
, result
.addr2
- (uint64_t)src
);
115 printf(" src len: %"PRId64
"\n", result
.len2
);
122 /* Length == 0, no memory should be read or written */
123 printf("\n------------- test1 ----------------\n");
124 run_test(NULL
, 0, NULL
, 0);
126 /* Test exhaustion of source length (source bytes are valid) */
127 printf("\n------------- test2.1 ----------------\n");
129 /* No character will be written to BUFF, i.e. loop in jitted code
131 run_test(buff
, sizeof buff
, NULL
, 0);
132 run_test(buff
, sizeof buff
, NULL
, 1);
133 run_test(buff
, sizeof buff
, NULL
, 2);
134 run_test(buff
, sizeof buff
, NULL
, 3);
135 run_test(buff
, sizeof buff
, pattern2
, 0);
136 run_test(buff
, sizeof buff
, pattern2
, 1);
137 run_test(buff
, sizeof buff
, pattern2
, 2);
138 run_test(buff
, sizeof buff
, pattern2
, 3);
140 printf("\n------------- test2.2 ----------------\n");
141 /* At least one character will be written to BUFF, i.e. loop in jitted
143 run_test(buff
, sizeof buff
, pattern2
, 4); /* 1 utf32 -> 1 utf16 */
144 run_test(buff
, sizeof buff
, pattern2
, 10); /* 2 utf32 -> 2 utf16 */
145 run_test(buff
, sizeof buff
, pattern4
, 5); /* 1 utf32 -> 2 utf16 */
146 run_test(buff
, sizeof buff
, pattern4
, 11); /* 2 utf32 -> 4 utf16 */
147 run_test(buff
, sizeof buff
, pattern4
, 18); /* 4 utf32 -> 8 utf16 */
149 /* Test exhaustion of destination length (source bytes are valid) */
150 printf("\n------------- test3.1 ----------------\n");
152 /* No character will be written to BUFF, i.e. loop in jitted code
155 /* Want to write at least 1 UTF-16 */
156 run_test(NULL
, 0, pattern2
, sizeof pattern2
);
158 /* Want to write at least 1 UTF-16 */
159 run_test(NULL
, 0, pattern2
, sizeof pattern2
);
160 run_test(NULL
, 1, pattern2
, sizeof pattern2
);
162 /* Want to write at least 2 UTF-16 */
163 run_test(NULL
, 0, pattern4
, sizeof pattern4
);
164 run_test(NULL
, 1, pattern4
, sizeof pattern4
);
165 run_test(NULL
, 2, pattern4
, sizeof pattern4
);
166 run_test(NULL
, 3, pattern4
, sizeof pattern4
);
168 /* When both operands are exhausted, cc=0 takes precedence.
169 (test1 tests this for len == 0) */
170 printf("\n------------- test4 ----------------\n");
171 run_test(buff
, 4, pattern2
, 8);
173 /* Input contains invalid characters */
175 // As conversion stops upon encountering an invalid character, we
176 // need to test each invalid character separately, to make sure it
177 // is recognized as invalid.
179 printf("\n------------- test5 ----------------\n");
180 for (i
= 0; i
< sizeof invalid
/ 4; ++i
) {
181 run_test(buff
, sizeof buff
, invalid
+ i
, 4);
183 run_test(buff
, 0, invalid
, sizeof invalid
); // cc = 2
184 run_test(buff
, 100, invalid
, sizeof invalid
);
186 /* Convert all pattern buffers */
187 printf("\n------------- test6 ----------------\n");
188 run_test(buff
, sizeof buff
, pattern2
, sizeof pattern2
);
189 run_test(buff
, sizeof buff
, pattern4
, sizeof pattern4
);
190 run_test(buff
, sizeof buff
, mixed
, sizeof mixed
);
192 /* Make sure we only write the exact number of bytes (and not more) */
195 printf("\n------------- test7.1 ----------------\n");
196 write_and_check(pattern2
+ 3, 4, 2);
199 printf("\n------------- test7.2 ----------------\n");
200 write_and_check(pattern4
+ 5, 4, 4);
207 write_and_check_aux(uint32_t *input
, unsigned num_input_bytes
,
208 unsigned num_expected_output_bytes
,
213 /* Fill output buffer with FILL_BYTE */
214 memset(buff
, fill_byte
, sizeof buff
);
217 run_test(buff
, sizeof buff
, input
, num_input_bytes
);
219 /* Make sure the rest of the buffer is unmodified. */
221 for (i
= num_expected_output_bytes
; i
< sizeof buff
; ++i
)
222 if (((unsigned char *)buff
)[i
] != fill_byte
) ++num_errors
;
224 fprintf(stderr
, "*** wrote more than %d bytes\n",
225 num_expected_output_bytes
);
229 write_and_check(uint32_t *input
, unsigned num_input_bytes
,
230 unsigned num_expected_output_bytes
)
232 write_and_check_aux(input
, num_input_bytes
, num_expected_output_bytes
, 0x0);
234 /* Run again with different fill pattern to make sure we did not write
236 write_and_check_aux(input
, num_input_bytes
, num_expected_output_bytes
, 0xFF);