]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/s390/utf8-utf32-z9.c
S390: Use s390-64 specific ionv-modules on s390-32, too.
[thirdparty/glibc.git] / sysdeps / s390 / utf8-utf32-z9.c
1 /* Conversion between UTF-8 and UTF-32 BE/internal.
2
3 This module uses the Z9-109 variants of the Convert Unicode
4 instructions.
5 Copyright (C) 1997-2016 Free Software Foundation, Inc.
6
7 Author: Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
8 Based on the work by Ulrich Drepper <drepper@cygnus.com>, 1997.
9
10 Thanks to Daniel Appich who covered the relevant performance work
11 in his diploma thesis.
12
13 This is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 2.1 of the License, or (at your option) any later version.
17
18 This is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with the GNU C Library; if not, see
25 <http://www.gnu.org/licenses/>. */
26
27 #include <dlfcn.h>
28 #include <stdint.h>
29 #include <unistd.h>
30 #include <dl-procinfo.h>
31 #include <gconv.h>
32
33 #if defined HAVE_S390_VX_GCC_SUPPORT
34 # define ASM_CLOBBER_VR(NR) , NR
35 #else
36 # define ASM_CLOBBER_VR(NR)
37 #endif
38
39 #if defined __s390x__
40 # define CONVERT_32BIT_SIZE_T(REG)
41 #else
42 # define CONVERT_32BIT_SIZE_T(REG) "llgfr %" #REG ",%" #REG "\n\t"
43 #endif
44
45 /* Defines for skeleton.c. */
46 #define DEFINE_INIT 0
47 #define DEFINE_FINI 0
48 #define MIN_NEEDED_FROM 1
49 #define MAX_NEEDED_FROM 6
50 #define MIN_NEEDED_TO 4
51 #define FROM_LOOP __from_utf8_loop
52 #define TO_LOOP __to_utf8_loop
53 #define FROM_DIRECTION (dir == from_utf8)
54 #define ONE_DIRECTION 0
55
56 /* UTF-32 big endian byte order mark. */
57 #define BOM 0x0000feffu
58
59 /* Direction of the transformation. */
60 enum direction
61 {
62 illegal_dir,
63 to_utf8,
64 from_utf8
65 };
66
67 struct utf8_data
68 {
69 enum direction dir;
70 int emit_bom;
71 };
72
73
74 extern int gconv_init (struct __gconv_step *step);
75 int
76 gconv_init (struct __gconv_step *step)
77 {
78 /* Determine which direction. */
79 struct utf8_data *new_data;
80 enum direction dir = illegal_dir;
81 int emit_bom;
82 int result;
83
84 emit_bom = (__strcasecmp (step->__to_name, "UTF-32//") == 0);
85
86 if (__strcasecmp (step->__from_name, "ISO-10646/UTF8/") == 0
87 && (__strcasecmp (step->__to_name, "UTF-32//") == 0
88 || __strcasecmp (step->__to_name, "UTF-32BE//") == 0
89 || __strcasecmp (step->__to_name, "INTERNAL") == 0))
90 {
91 dir = from_utf8;
92 }
93 else if (__strcasecmp (step->__to_name, "ISO-10646/UTF8/") == 0
94 && (__strcasecmp (step->__from_name, "UTF-32BE//") == 0
95 || __strcasecmp (step->__from_name, "INTERNAL") == 0))
96 {
97 dir = to_utf8;
98 }
99
100 result = __GCONV_NOCONV;
101 if (dir != illegal_dir)
102 {
103 new_data = (struct utf8_data *) malloc (sizeof (struct utf8_data));
104
105 result = __GCONV_NOMEM;
106 if (new_data != NULL)
107 {
108 new_data->dir = dir;
109 new_data->emit_bom = emit_bom;
110 step->__data = new_data;
111
112 if (dir == from_utf8)
113 {
114 step->__min_needed_from = MIN_NEEDED_FROM;
115 step->__max_needed_from = MIN_NEEDED_FROM;
116 step->__min_needed_to = MIN_NEEDED_TO;
117 step->__max_needed_to = MIN_NEEDED_TO;
118 }
119 else
120 {
121 step->__min_needed_from = MIN_NEEDED_TO;
122 step->__max_needed_from = MIN_NEEDED_TO;
123 step->__min_needed_to = MIN_NEEDED_FROM;
124 step->__max_needed_to = MIN_NEEDED_FROM;
125 }
126
127 step->__stateful = 0;
128
129 result = __GCONV_OK;
130 }
131 }
132
133 return result;
134 }
135
136
137 extern void gconv_end (struct __gconv_step *data);
138 void
139 gconv_end (struct __gconv_step *data)
140 {
141 free (data->__data);
142 }
143
144 /* The macro for the hardware loop. This is used for both
145 directions. */
146 #define HARDWARE_CONVERT(INSTRUCTION) \
147 { \
148 register const unsigned char* pInput __asm__ ("8") = inptr; \
149 register size_t inlen __asm__ ("9") = inend - inptr; \
150 register unsigned char* pOutput __asm__ ("10") = outptr; \
151 register size_t outlen __asm__("11") = outend - outptr; \
152 unsigned long cc = 0; \
153 \
154 __asm__ __volatile__ (".machine push \n\t" \
155 ".machine \"z9-109\" \n\t" \
156 ".machinemode \"zarch_nohighgprs\"\n\t" \
157 "0: " INSTRUCTION " \n\t" \
158 ".machine pop \n\t" \
159 " jo 0b \n\t" \
160 " ipm %2 \n" \
161 : "+a" (pOutput), "+a" (pInput), "+d" (cc), \
162 "+d" (outlen), "+d" (inlen) \
163 : \
164 : "cc", "memory"); \
165 \
166 inptr = pInput; \
167 outptr = pOutput; \
168 cc >>= 28; \
169 \
170 if (cc == 1) \
171 { \
172 result = __GCONV_FULL_OUTPUT; \
173 } \
174 else if (cc == 2) \
175 { \
176 result = __GCONV_ILLEGAL_INPUT; \
177 } \
178 }
179
180 #define PREPARE_LOOP \
181 enum direction dir = ((struct utf8_data *) step->__data)->dir; \
182 int emit_bom = ((struct utf8_data *) step->__data)->emit_bom; \
183 \
184 if (emit_bom && !data->__internal_use \
185 && data->__invocation_counter == 0) \
186 { \
187 /* Emit the Byte Order Mark. */ \
188 if (__glibc_unlikely (outbuf + 4 > outend)) \
189 return __GCONV_FULL_OUTPUT; \
190 \
191 put32u (outbuf, BOM); \
192 outbuf += 4; \
193 }
194
195 /* Conversion function from UTF-8 to UTF-32 internal/BE. */
196
197 #define STORE_REST_COMMON \
198 { \
199 /* We store the remaining bytes while converting them into the UCS4 \
200 format. We can assume that the first byte in the buffer is \
201 correct and that it requires a larger number of bytes than there \
202 are in the input buffer. */ \
203 wint_t ch = **inptrp; \
204 size_t cnt, r; \
205 \
206 state->__count = inend - *inptrp; \
207 \
208 assert (ch != 0xc0 && ch != 0xc1); \
209 if (ch >= 0xc2 && ch < 0xe0) \
210 { \
211 /* We expect two bytes. The first byte cannot be 0xc0 or \
212 0xc1, otherwise the wide character could have been \
213 represented using a single byte. */ \
214 cnt = 2; \
215 ch &= 0x1f; \
216 } \
217 else if (__glibc_likely ((ch & 0xf0) == 0xe0)) \
218 { \
219 /* We expect three bytes. */ \
220 cnt = 3; \
221 ch &= 0x0f; \
222 } \
223 else if (__glibc_likely ((ch & 0xf8) == 0xf0)) \
224 { \
225 /* We expect four bytes. */ \
226 cnt = 4; \
227 ch &= 0x07; \
228 } \
229 else if (__glibc_likely ((ch & 0xfc) == 0xf8)) \
230 { \
231 /* We expect five bytes. */ \
232 cnt = 5; \
233 ch &= 0x03; \
234 } \
235 else \
236 { \
237 /* We expect six bytes. */ \
238 cnt = 6; \
239 ch &= 0x01; \
240 } \
241 \
242 /* The first byte is already consumed. */ \
243 r = cnt - 1; \
244 while (++(*inptrp) < inend) \
245 { \
246 ch <<= 6; \
247 ch |= **inptrp & 0x3f; \
248 --r; \
249 } \
250 \
251 /* Shift for the so far missing bytes. */ \
252 ch <<= r * 6; \
253 \
254 /* Store the number of bytes expected for the entire sequence. */ \
255 state->__count |= cnt << 8; \
256 \
257 /* Store the value. */ \
258 state->__value.__wch = ch; \
259 }
260
261 #define UNPACK_BYTES_COMMON \
262 { \
263 static const unsigned char inmask[5] = { 0xc0, 0xe0, 0xf0, 0xf8, 0xfc }; \
264 wint_t wch = state->__value.__wch; \
265 size_t ntotal = state->__count >> 8; \
266 \
267 inlen = state->__count & 255; \
268 \
269 bytebuf[0] = inmask[ntotal - 2]; \
270 \
271 do \
272 { \
273 if (--ntotal < inlen) \
274 bytebuf[ntotal] = 0x80 | (wch & 0x3f); \
275 wch >>= 6; \
276 } \
277 while (ntotal > 1); \
278 \
279 bytebuf[0] |= wch; \
280 }
281
282 #define CLEAR_STATE_COMMON \
283 state->__count = 0
284
285 #define BODY_FROM_HW(ASM) \
286 { \
287 ASM; \
288 if (__glibc_likely (inptr == inend) \
289 || result == __GCONV_FULL_OUTPUT) \
290 break; \
291 \
292 int i; \
293 for (i = 1; inptr + i < inend && i < 5; ++i) \
294 if ((inptr[i] & 0xc0) != 0x80) \
295 break; \
296 \
297 if (__glibc_likely (inptr + i == inend \
298 && result == __GCONV_EMPTY_INPUT)) \
299 { \
300 result = __GCONV_INCOMPLETE_INPUT; \
301 break; \
302 } \
303 STANDARD_FROM_LOOP_ERR_HANDLER (i); \
304 }
305
306 /* This hardware routine uses the Convert UTF8 to UTF32 (cu14) instruction. */
307 #define BODY_FROM_ETF3EH BODY_FROM_HW (HARDWARE_CONVERT ("cu14 %0, %1, 1"))
308
309
310 /* The software routine is copied from gconv_simple.c. */
311 #define BODY_FROM_C \
312 { \
313 /* Next input byte. */ \
314 uint32_t ch = *inptr; \
315 \
316 if (__glibc_likely (ch < 0x80)) \
317 { \
318 /* One byte sequence. */ \
319 ++inptr; \
320 } \
321 else \
322 { \
323 uint_fast32_t cnt; \
324 uint_fast32_t i; \
325 \
326 if (ch >= 0xc2 && ch < 0xe0) \
327 { \
328 /* We expect two bytes. The first byte cannot be 0xc0 or \
329 0xc1, otherwise the wide character could have been \
330 represented using a single byte. */ \
331 cnt = 2; \
332 ch &= 0x1f; \
333 } \
334 else if (__glibc_likely ((ch & 0xf0) == 0xe0)) \
335 { \
336 /* We expect three bytes. */ \
337 cnt = 3; \
338 ch &= 0x0f; \
339 } \
340 else if (__glibc_likely ((ch & 0xf8) == 0xf0)) \
341 { \
342 /* We expect four bytes. */ \
343 cnt = 4; \
344 ch &= 0x07; \
345 } \
346 else \
347 { \
348 /* Search the end of this ill-formed UTF-8 character. This \
349 is the next byte with (x & 0xc0) != 0x80. */ \
350 i = 0; \
351 do \
352 ++i; \
353 while (inptr + i < inend \
354 && (*(inptr + i) & 0xc0) == 0x80 \
355 && i < 5); \
356 \
357 errout: \
358 STANDARD_FROM_LOOP_ERR_HANDLER (i); \
359 } \
360 \
361 if (__glibc_unlikely (inptr + cnt > inend)) \
362 { \
363 /* We don't have enough input. But before we report \
364 that check that all the bytes are correct. */ \
365 for (i = 1; inptr + i < inend; ++i) \
366 if ((inptr[i] & 0xc0) != 0x80) \
367 break; \
368 \
369 if (__glibc_likely (inptr + i == inend)) \
370 { \
371 result = __GCONV_INCOMPLETE_INPUT; \
372 break; \
373 } \
374 \
375 goto errout; \
376 } \
377 \
378 /* Read the possible remaining bytes. */ \
379 for (i = 1; i < cnt; ++i) \
380 { \
381 uint32_t byte = inptr[i]; \
382 \
383 if ((byte & 0xc0) != 0x80) \
384 /* This is an illegal encoding. */ \
385 break; \
386 \
387 ch <<= 6; \
388 ch |= byte & 0x3f; \
389 } \
390 \
391 /* If i < cnt, some trail byte was not >= 0x80, < 0xc0. \
392 If cnt > 2 and ch < 2^(5*cnt-4), the wide character ch could \
393 have been represented with fewer than cnt bytes. */ \
394 if (i < cnt || (cnt > 2 && (ch >> (5 * cnt - 4)) == 0) \
395 /* Do not accept UTF-16 surrogates. */ \
396 || (ch >= 0xd800 && ch <= 0xdfff) \
397 || (ch > 0x10ffff)) \
398 { \
399 /* This is an illegal encoding. */ \
400 goto errout; \
401 } \
402 \
403 inptr += cnt; \
404 } \
405 \
406 /* Now adjust the pointers and store the result. */ \
407 *((uint32_t *) outptr) = ch; \
408 outptr += sizeof (uint32_t); \
409 }
410
411 #define HW_FROM_VX \
412 { \
413 register const unsigned char* pInput asm ("8") = inptr; \
414 register size_t inlen asm ("9") = inend - inptr; \
415 register unsigned char* pOutput asm ("10") = outptr; \
416 register size_t outlen asm("11") = outend - outptr; \
417 unsigned long tmp, tmp2, tmp3; \
418 asm volatile (".machine push\n\t" \
419 ".machine \"z13\"\n\t" \
420 ".machinemode \"zarch_nohighgprs\"\n\t" \
421 " vrepib %%v30,0x7f\n\t" /* For compare > 0x7f. */ \
422 " vrepib %%v31,0x20\n\t" \
423 CONVERT_32BIT_SIZE_T ([R_INLEN]) \
424 CONVERT_32BIT_SIZE_T ([R_OUTLEN]) \
425 /* Loop which handles UTF-8 chars <=0x7f. */ \
426 "0: clgijl %[R_INLEN],16,20f\n\t" \
427 " clgijl %[R_OUTLEN],64,20f\n\t" \
428 "1: vl %%v16,0(%[R_IN])\n\t" \
429 " vstrcbs %%v17,%%v16,%%v30,%%v31\n\t" \
430 " jno 10f\n\t" /* Jump away if not all bytes are 1byte \
431 UTF8 chars. */ \
432 /* Enlarge to UCS4. */ \
433 " vuplhb %%v18,%%v16\n\t" \
434 " vupllb %%v19,%%v16\n\t" \
435 " la %[R_IN],16(%[R_IN])\n\t" \
436 " vuplhh %%v20,%%v18\n\t" \
437 " aghi %[R_INLEN],-16\n\t" \
438 " vupllh %%v21,%%v18\n\t" \
439 " aghi %[R_OUTLEN],-64\n\t" \
440 " vuplhh %%v22,%%v19\n\t" \
441 " vupllh %%v23,%%v19\n\t" \
442 /* Store 64 bytes to buf_out. */ \
443 " vstm %%v20,%%v23,0(%[R_OUT])\n\t" \
444 " la %[R_OUT],64(%[R_OUT])\n\t" \
445 " clgijl %[R_INLEN],16,20f\n\t" \
446 " clgijl %[R_OUTLEN],64,20f\n\t" \
447 " j 1b\n\t" \
448 "10: \n\t" \
449 /* At least one byte is > 0x7f. \
450 Store the preceding 1-byte chars. */ \
451 " vlgvb %[R_TMP],%%v17,7\n\t" \
452 " sllk %[R_TMP2],%[R_TMP],2\n\t" /* Compute highest \
453 index to store. */ \
454 " llgfr %[R_TMP3],%[R_TMP2]\n\t" \
455 " ahi %[R_TMP2],-1\n\t" \
456 " jl 20f\n\t" \
457 " vuplhb %%v18,%%v16\n\t" \
458 " vuplhh %%v20,%%v18\n\t" \
459 " vstl %%v20,%[R_TMP2],0(%[R_OUT])\n\t" \
460 " ahi %[R_TMP2],-16\n\t" \
461 " jl 11f\n\t" \
462 " vupllh %%v21,%%v18\n\t" \
463 " vstl %%v21,%[R_TMP2],16(%[R_OUT])\n\t" \
464 " ahi %[R_TMP2],-16\n\t" \
465 " jl 11f\n\t" \
466 " vupllb %%v19,%%v16\n\t" \
467 " vuplhh %%v22,%%v19\n\t" \
468 " vstl %%v22,%[R_TMP2],32(%[R_OUT])\n\t" \
469 " ahi %[R_TMP2],-16\n\t" \
470 " jl 11f\n\t" \
471 " vupllh %%v23,%%v19\n\t" \
472 " vstl %%v23,%[R_TMP2],48(%[R_OUT])\n\t" \
473 "11: \n\t" \
474 /* Update pointers. */ \
475 " la %[R_IN],0(%[R_TMP],%[R_IN])\n\t" \
476 " slgr %[R_INLEN],%[R_TMP]\n\t" \
477 " la %[R_OUT],0(%[R_TMP3],%[R_OUT])\n\t" \
478 " slgr %[R_OUTLEN],%[R_TMP3]\n\t" \
479 /* Handle multibyte utf8-char with convert instruction. */ \
480 "20: cu14 %[R_OUT],%[R_IN],1\n\t" \
481 " jo 0b\n\t" /* Try vector implemenation again. */ \
482 " lochil %[R_RES],%[RES_OUT_FULL]\n\t" /* cc == 1. */ \
483 " lochih %[R_RES],%[RES_IN_ILL]\n\t" /* cc == 2. */ \
484 ".machine pop" \
485 : /* outputs */ [R_IN] "+a" (pInput) \
486 , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (pOutput) \
487 , [R_OUTLEN] "+d" (outlen), [R_TMP] "=a" (tmp) \
488 , [R_TMP2] "=d" (tmp2), [R_TMP3] "=a" (tmp3) \
489 , [R_RES] "+d" (result) \
490 : /* inputs */ \
491 [RES_OUT_FULL] "i" (__GCONV_FULL_OUTPUT) \
492 , [RES_IN_ILL] "i" (__GCONV_ILLEGAL_INPUT) \
493 : /* clobber list */ "memory", "cc" \
494 ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
495 ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
496 ASM_CLOBBER_VR ("v20") ASM_CLOBBER_VR ("v21") \
497 ASM_CLOBBER_VR ("v22") ASM_CLOBBER_VR ("v30") \
498 ASM_CLOBBER_VR ("v31") \
499 ); \
500 inptr = pInput; \
501 outptr = pOutput; \
502 }
503 #define BODY_FROM_VX BODY_FROM_HW (HW_FROM_VX)
504
505 /* These definitions apply to the UTF-8 to UTF-32 direction. The
506 software implementation for UTF-8 still supports multibyte
507 characters up to 6 bytes whereas the hardware variant does not. */
508 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
509 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
510 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
511 #define LOOPFCT __from_utf8_loop_c
512
513 #define LOOP_NEED_FLAGS
514
515 #define STORE_REST STORE_REST_COMMON
516 #define UNPACK_BYTES UNPACK_BYTES_COMMON
517 #define CLEAR_STATE CLEAR_STATE_COMMON
518 #define BODY BODY_FROM_C
519 #include <iconv/loop.c>
520
521
522 /* Generate loop-function with hardware utf-convert instruction. */
523 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
524 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
525 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
526 #define LOOPFCT __from_utf8_loop_etf3eh
527
528 #define LOOP_NEED_FLAGS
529
530 #define STORE_REST STORE_REST_COMMON
531 #define UNPACK_BYTES UNPACK_BYTES_COMMON
532 #define CLEAR_STATE CLEAR_STATE_COMMON
533 #define BODY BODY_FROM_ETF3EH
534 #include <iconv/loop.c>
535
536 #if defined HAVE_S390_VX_ASM_SUPPORT
537 /* Generate loop-function with hardware vector instructions. */
538 # define MIN_NEEDED_INPUT MIN_NEEDED_FROM
539 # define MAX_NEEDED_INPUT MAX_NEEDED_FROM
540 # define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
541 # define LOOPFCT __from_utf8_loop_vx
542
543 # define LOOP_NEED_FLAGS
544
545 # define STORE_REST STORE_REST_COMMON
546 # define UNPACK_BYTES UNPACK_BYTES_COMMON
547 # define CLEAR_STATE CLEAR_STATE_COMMON
548 # define BODY BODY_FROM_VX
549 # include <iconv/loop.c>
550 #endif
551
552
553 /* Generate ifunc'ed loop function. */
554 __typeof(__from_utf8_loop_c)
555 __attribute__ ((ifunc ("__from_utf8_loop_resolver")))
556 __from_utf8_loop;
557
558 static void *
559 __from_utf8_loop_resolver (unsigned long int dl_hwcap)
560 {
561 #if defined HAVE_S390_VX_ASM_SUPPORT
562 if (dl_hwcap & HWCAP_S390_VX)
563 return __from_utf8_loop_vx;
564 else
565 #endif
566 if (dl_hwcap & HWCAP_S390_ZARCH && dl_hwcap & HWCAP_S390_HIGH_GPRS
567 && dl_hwcap & HWCAP_S390_ETF3EH)
568 return __from_utf8_loop_etf3eh;
569 else
570 return __from_utf8_loop_c;
571 }
572
573 strong_alias (__from_utf8_loop_c_single, __from_utf8_loop_single)
574
575
576 /* Conversion from UTF-32 internal/BE to UTF-8. */
577 #define BODY_TO_HW(ASM) \
578 { \
579 ASM; \
580 if (__glibc_likely (inptr == inend) \
581 || result == __GCONV_FULL_OUTPUT) \
582 break; \
583 if (inptr + 4 > inend) \
584 { \
585 result = __GCONV_INCOMPLETE_INPUT; \
586 break; \
587 } \
588 STANDARD_TO_LOOP_ERR_HANDLER (4); \
589 }
590
591 /* The hardware routine uses the S/390 cu41 instruction. */
592 #define BODY_TO_ETF3EH BODY_TO_HW (HARDWARE_CONVERT ("cu41 %0, %1"))
593
594 /* The hardware routine uses the S/390 vector and cu41 instructions. */
595 #define BODY_TO_VX BODY_TO_HW (HW_TO_VX)
596
597 /* The software routine mimics the S/390 cu41 instruction. */
598 #define BODY_TO_C \
599 { \
600 uint32_t wc = *((const uint32_t *) inptr); \
601 \
602 if (__glibc_likely (wc <= 0x7f)) \
603 { \
604 /* Single UTF-8 char. */ \
605 *outptr = (uint8_t)wc; \
606 outptr++; \
607 } \
608 else if (wc <= 0x7ff) \
609 { \
610 /* Two UTF-8 chars. */ \
611 if (__glibc_unlikely (outptr + 2 > outend)) \
612 { \
613 /* Overflow in the output buffer. */ \
614 result = __GCONV_FULL_OUTPUT; \
615 break; \
616 } \
617 \
618 outptr[0] = 0xc0; \
619 outptr[0] |= wc >> 6; \
620 \
621 outptr[1] = 0x80; \
622 outptr[1] |= wc & 0x3f; \
623 \
624 outptr += 2; \
625 } \
626 else if (wc <= 0xffff) \
627 { \
628 /* Three UTF-8 chars. */ \
629 if (__glibc_unlikely (outptr + 3 > outend)) \
630 { \
631 /* Overflow in the output buffer. */ \
632 result = __GCONV_FULL_OUTPUT; \
633 break; \
634 } \
635 if (wc >= 0xd800 && wc < 0xdc00) \
636 { \
637 /* Do not accept UTF-16 surrogates. */ \
638 result = __GCONV_ILLEGAL_INPUT; \
639 STANDARD_TO_LOOP_ERR_HANDLER (4); \
640 } \
641 outptr[0] = 0xe0; \
642 outptr[0] |= wc >> 12; \
643 \
644 outptr[1] = 0x80; \
645 outptr[1] |= (wc >> 6) & 0x3f; \
646 \
647 outptr[2] = 0x80; \
648 outptr[2] |= wc & 0x3f; \
649 \
650 outptr += 3; \
651 } \
652 else if (wc <= 0x10ffff) \
653 { \
654 /* Four UTF-8 chars. */ \
655 if (__glibc_unlikely (outptr + 4 > outend)) \
656 { \
657 /* Overflow in the output buffer. */ \
658 result = __GCONV_FULL_OUTPUT; \
659 break; \
660 } \
661 outptr[0] = 0xf0; \
662 outptr[0] |= wc >> 18; \
663 \
664 outptr[1] = 0x80; \
665 outptr[1] |= (wc >> 12) & 0x3f; \
666 \
667 outptr[2] = 0x80; \
668 outptr[2] |= (wc >> 6) & 0x3f; \
669 \
670 outptr[3] = 0x80; \
671 outptr[3] |= wc & 0x3f; \
672 \
673 outptr += 4; \
674 } \
675 else \
676 { \
677 STANDARD_TO_LOOP_ERR_HANDLER (4); \
678 } \
679 inptr += 4; \
680 }
681
682 #define HW_TO_VX \
683 { \
684 register const unsigned char* pInput asm ("8") = inptr; \
685 register size_t inlen asm ("9") = inend - inptr; \
686 register unsigned char* pOutput asm ("10") = outptr; \
687 register size_t outlen asm("11") = outend - outptr; \
688 unsigned long tmp, tmp2; \
689 asm volatile (".machine push\n\t" \
690 ".machine \"z13\"\n\t" \
691 ".machinemode \"zarch_nohighgprs\"\n\t" \
692 " vleif %%v20,127,0\n\t" /* element 0: 127 */ \
693 " vzero %%v21\n\t" \
694 " vleih %%v21,8192,0\n\t" /* element 0: > */ \
695 " vleih %%v21,-8192,2\n\t" /* element 1: =<> */ \
696 CONVERT_32BIT_SIZE_T ([R_INLEN]) \
697 CONVERT_32BIT_SIZE_T ([R_OUTLEN]) \
698 /* Loop which handles UTF-32 chars <=0x7f. */ \
699 "0: clgijl %[R_INLEN],64,20f\n\t" \
700 " clgijl %[R_OUTLEN],16,20f\n\t" \
701 "1: vlm %%v16,%%v19,0(%[R_IN])\n\t" \
702 " lghi %[R_TMP],0\n\t" \
703 /* Shorten to byte values. */ \
704 " vpkf %%v23,%%v16,%%v17\n\t" \
705 " vpkf %%v24,%%v18,%%v19\n\t" \
706 " vpkh %%v23,%%v23,%%v24\n\t" \
707 /* Checking for values > 0x7f. */ \
708 " vstrcfs %%v22,%%v16,%%v20,%%v21\n\t" \
709 " jno 10f\n\t" \
710 " vstrcfs %%v22,%%v17,%%v20,%%v21\n\t" \
711 " jno 11f\n\t" \
712 " vstrcfs %%v22,%%v18,%%v20,%%v21\n\t" \
713 " jno 12f\n\t" \
714 " vstrcfs %%v22,%%v19,%%v20,%%v21\n\t" \
715 " jno 13f\n\t" \
716 /* Store 16bytes to outptr. */ \
717 " vst %%v23,0(%[R_OUT])\n\t" \
718 " aghi %[R_INLEN],-64\n\t" \
719 " aghi %[R_OUTLEN],-16\n\t" \
720 " la %[R_IN],64(%[R_IN])\n\t" \
721 " la %[R_OUT],16(%[R_OUT])\n\t" \
722 " clgijl %[R_INLEN],64,20f\n\t" \
723 " clgijl %[R_OUTLEN],16,20f\n\t" \
724 " j 1b\n\t" \
725 /* Found a value > 0x7f. */ \
726 "13: ahi %[R_TMP],4\n\t" \
727 "12: ahi %[R_TMP],4\n\t" \
728 "11: ahi %[R_TMP],4\n\t" \
729 "10: vlgvb %[R_I],%%v22,7\n\t" \
730 " srlg %[R_I],%[R_I],2\n\t" \
731 " agr %[R_I],%[R_TMP]\n\t" \
732 " je 20f\n\t" \
733 /* Store characters before invalid one... */ \
734 " slgr %[R_OUTLEN],%[R_I]\n\t" \
735 "15: aghi %[R_I],-1\n\t" \
736 " vstl %%v23,%[R_I],0(%[R_OUT])\n\t" \
737 /* ... and update pointers. */ \
738 " aghi %[R_I],1\n\t" \
739 " la %[R_OUT],0(%[R_I],%[R_OUT])\n\t" \
740 " sllg %[R_I],%[R_I],2\n\t" \
741 " la %[R_IN],0(%[R_I],%[R_IN])\n\t" \
742 " slgr %[R_INLEN],%[R_I]\n\t" \
743 /* Handle multibyte utf8-char with convert instruction. */ \
744 "20: cu41 %[R_OUT],%[R_IN]\n\t" \
745 " jo 0b\n\t" /* Try vector implemenation again. */ \
746 " lochil %[R_RES],%[RES_OUT_FULL]\n\t" /* cc == 1. */ \
747 " lochih %[R_RES],%[RES_IN_ILL]\n\t" /* cc == 2. */ \
748 ".machine pop" \
749 : /* outputs */ [R_IN] "+a" (pInput) \
750 , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (pOutput) \
751 , [R_OUTLEN] "+d" (outlen), [R_TMP] "=d" (tmp) \
752 , [R_I] "=a" (tmp2) \
753 , [R_RES] "+d" (result) \
754 : /* inputs */ \
755 [RES_OUT_FULL] "i" (__GCONV_FULL_OUTPUT) \
756 , [RES_IN_ILL] "i" (__GCONV_ILLEGAL_INPUT) \
757 : /* clobber list */ "memory", "cc" \
758 ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
759 ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
760 ASM_CLOBBER_VR ("v20") ASM_CLOBBER_VR ("v21") \
761 ASM_CLOBBER_VR ("v22") ASM_CLOBBER_VR ("v23") \
762 ASM_CLOBBER_VR ("v24") \
763 ); \
764 inptr = pInput; \
765 outptr = pOutput; \
766 }
767
768 /* Generate loop-function with software routing. */
769 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
770 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
771 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
772 #define LOOPFCT __to_utf8_loop_c
773 #define BODY BODY_TO_C
774 #define LOOP_NEED_FLAGS
775 #include <iconv/loop.c>
776
777 /* Generate loop-function with hardware utf-convert instruction. */
778 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
779 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
780 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
781 #define LOOPFCT __to_utf8_loop_etf3eh
782 #define LOOP_NEED_FLAGS
783 #define BODY BODY_TO_ETF3EH
784 #include <iconv/loop.c>
785
786 #if defined HAVE_S390_VX_ASM_SUPPORT
787 /* Generate loop-function with hardware vector and utf-convert instructions. */
788 # define MIN_NEEDED_INPUT MIN_NEEDED_TO
789 # define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
790 # define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
791 # define LOOPFCT __to_utf8_loop_vx
792 # define BODY BODY_TO_VX
793 # define LOOP_NEED_FLAGS
794 # include <iconv/loop.c>
795 #endif
796
797 /* Generate ifunc'ed loop function. */
798 __typeof(__to_utf8_loop_c)
799 __attribute__ ((ifunc ("__to_utf8_loop_resolver")))
800 __to_utf8_loop;
801
802 static void *
803 __to_utf8_loop_resolver (unsigned long int dl_hwcap)
804 {
805 #if defined HAVE_S390_VX_ASM_SUPPORT
806 if (dl_hwcap & HWCAP_S390_VX)
807 return __to_utf8_loop_vx;
808 else
809 #endif
810 if (dl_hwcap & HWCAP_S390_ZARCH && dl_hwcap & HWCAP_S390_HIGH_GPRS
811 && dl_hwcap & HWCAP_S390_ETF3EH)
812 return __to_utf8_loop_etf3eh;
813 else
814 return __to_utf8_loop_c;
815 }
816
817 strong_alias (__to_utf8_loop_c_single, __to_utf8_loop_single)
818
819
820 #include <iconv/skeleton.c>