1 /* Conversion between UTF-16 and UTF-32 BE/internal.
3 This module uses the Z9-109 variants of the Convert Unicode
5 Copyright (C) 1997-2018 Free Software Foundation, Inc.
7 Author: Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
8 Based on the work by Ulrich Drepper <drepper@cygnus.com>, 1997.
10 Thanks to Daniel Appich who covered the relevant performance work
11 in his diploma thesis.
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.
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.
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/>. */
33 /* Select which versions should be defined depending on support
34 for multiarch, vector and used minimum architecture level. */
36 #define FROM_LOOP_DEFAULT FROM_LOOP_C
38 #define TO_LOOP_DEFAULT TO_LOOP_C
40 #if defined HAVE_S390_VX_ASM_SUPPORT && defined USE_MULTIARCH
41 # define HAVE_FROM_VX 1
42 # define HAVE_FROM_VX_CU 1
44 # define HAVE_TO_VX_CU 1
46 # define HAVE_FROM_VX 0
47 # define HAVE_FROM_VX_CU 0
49 # define HAVE_TO_VX_CU 0
52 #if defined HAVE_S390_VX_GCC_SUPPORT
53 # define ASM_CLOBBER_VR(NR) , NR
55 # define ASM_CLOBBER_VR(NR)
59 # define CONVERT_32BIT_SIZE_T(REG)
61 # define CONVERT_32BIT_SIZE_T(REG) "llgfr %" #REG ",%" #REG "\n\t"
64 /* UTF-32 big endian byte order mark. */
65 #define BOM_UTF32 0x0000feffu
67 /* UTF-16 big endian byte order mark. */
68 #define BOM_UTF16 0xfeff
72 #define MIN_NEEDED_FROM 2
73 #define MAX_NEEDED_FROM 4
74 #define MIN_NEEDED_TO 4
75 #define FROM_LOOP FROM_LOOP_DEFAULT
76 #define TO_LOOP TO_LOOP_DEFAULT
77 #define FROM_DIRECTION (dir == from_utf16)
78 #define ONE_DIRECTION 0
80 /* Direction of the transformation. */
95 extern int gconv_init (struct __gconv_step
*step
);
97 gconv_init (struct __gconv_step
*step
)
99 /* Determine which direction. */
100 struct utf16_data
*new_data
;
101 enum direction dir
= illegal_dir
;
105 emit_bom
= (__strcasecmp (step
->__to_name
, "UTF-32//") == 0
106 || __strcasecmp (step
->__to_name
, "UTF-16//") == 0);
108 if (__strcasecmp (step
->__from_name
, "UTF-16BE//") == 0
109 && (__strcasecmp (step
->__to_name
, "UTF-32//") == 0
110 || __strcasecmp (step
->__to_name
, "UTF-32BE//") == 0
111 || __strcasecmp (step
->__to_name
, "INTERNAL") == 0))
115 else if ((__strcasecmp (step
->__to_name
, "UTF-16//") == 0
116 || __strcasecmp (step
->__to_name
, "UTF-16BE//") == 0)
117 && (__strcasecmp (step
->__from_name
, "UTF-32BE//") == 0
118 || __strcasecmp (step
->__from_name
, "INTERNAL") == 0))
123 result
= __GCONV_NOCONV
;
124 if (dir
!= illegal_dir
)
126 new_data
= (struct utf16_data
*) malloc (sizeof (struct utf16_data
));
128 result
= __GCONV_NOMEM
;
129 if (new_data
!= NULL
)
132 new_data
->emit_bom
= emit_bom
;
133 step
->__data
= new_data
;
135 if (dir
== from_utf16
)
137 step
->__min_needed_from
= MIN_NEEDED_FROM
;
138 step
->__max_needed_from
= MIN_NEEDED_FROM
;
139 step
->__min_needed_to
= MIN_NEEDED_TO
;
140 step
->__max_needed_to
= MIN_NEEDED_TO
;
144 step
->__min_needed_from
= MIN_NEEDED_TO
;
145 step
->__max_needed_from
= MIN_NEEDED_TO
;
146 step
->__min_needed_to
= MIN_NEEDED_FROM
;
147 step
->__max_needed_to
= MIN_NEEDED_FROM
;
150 step
->__stateful
= 0;
160 extern void gconv_end (struct __gconv_step
*data
);
162 gconv_end (struct __gconv_step
*data
)
167 #define PREPARE_LOOP \
168 enum direction dir = ((struct utf16_data *) step->__data)->dir; \
169 int emit_bom = ((struct utf16_data *) step->__data)->emit_bom; \
171 if (emit_bom && !data->__internal_use \
172 && data->__invocation_counter == 0) \
174 if (dir == to_utf16) \
176 /* Emit the UTF-16 Byte Order Mark. */ \
177 if (__glibc_unlikely (outbuf + 2 > outend)) \
178 return __GCONV_FULL_OUTPUT; \
180 put16u (outbuf, BOM_UTF16); \
185 /* Emit the UTF-32 Byte Order Mark. */ \
186 if (__glibc_unlikely (outbuf + 4 > outend)) \
187 return __GCONV_FULL_OUTPUT; \
189 put32u (outbuf, BOM_UTF32); \
194 /* Conversion function from UTF-16 to UTF-32 internal/BE. */
197 /* The software routine is copied from utf-16.c (minus bytes
199 # define BODY_FROM_C \
201 uint16_t u1 = get16 (inptr); \
203 if (__builtin_expect (u1 < 0xd800, 1) || u1 > 0xdfff) \
205 /* No surrogate. */ \
206 put32 (outptr, u1); \
211 /* An isolated low-surrogate was found. This has to be \
212 considered ill-formed. */ \
213 if (__glibc_unlikely (u1 >= 0xdc00)) \
215 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
217 /* It's a surrogate character. At least the first word says \
219 if (__glibc_unlikely (inptr + 4 > inend)) \
221 /* We don't have enough input for another complete input \
223 result = __GCONV_INCOMPLETE_INPUT; \
228 uint16_t u2 = get16 (inptr); \
229 if (__builtin_expect (u2 < 0xdc00, 0) \
230 || __builtin_expect (u2 > 0xdfff, 0)) \
232 /* This is no valid second word for a surrogate. */ \
234 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
237 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
244 /* Generate loop-function with software routing. */
245 # define MIN_NEEDED_INPUT MIN_NEEDED_FROM
246 # define MAX_NEEDED_INPUT MAX_NEEDED_FROM
247 # define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
248 # define FROM_LOOP_C __from_utf16_loop_c
249 # define LOOPFCT FROM_LOOP_C
250 # define LOOP_NEED_FLAGS
251 # define BODY BODY_FROM_C
252 # include <iconv/loop.c>
254 # define FROM_LOOP_C NULL
255 #endif /* HAVE_FROM_C != 1 */
257 #if HAVE_FROM_VX == 1
258 # define BODY_FROM_VX \
260 size_t inlen = inend - inptr; \
261 size_t outlen = outend - outptr; \
262 unsigned long tmp, tmp2, tmp3; \
263 asm volatile (".machine push\n\t" \
264 ".machine \"z13\"\n\t" \
265 ".machinemode \"zarch_nohighgprs\"\n\t" \
266 /* Setup to check for surrogates. */ \
267 " larl %[R_TMP],9f\n\t" \
268 " vlm %%v30,%%v31,0(%[R_TMP])\n\t" \
269 CONVERT_32BIT_SIZE_T ([R_INLEN]) \
270 CONVERT_32BIT_SIZE_T ([R_OUTLEN]) \
271 /* Loop which handles UTF-16 chars <0xd800, >0xdfff. */ \
272 "0: clgijl %[R_INLEN],16,2f\n\t" \
273 " clgijl %[R_OUTLEN],32,2f\n\t" \
274 "1: vl %%v16,0(%[R_IN])\n\t" \
275 /* Check for surrogate chars. */ \
276 " vstrchs %%v19,%%v16,%%v30,%%v31\n\t" \
278 /* Enlarge to UTF-32. */ \
279 " vuplhh %%v17,%%v16\n\t" \
280 " la %[R_IN],16(%[R_IN])\n\t" \
281 " vupllh %%v18,%%v16\n\t" \
282 " aghi %[R_INLEN],-16\n\t" \
283 /* Store 32 bytes to buf_out. */ \
284 " vstm %%v17,%%v18,0(%[R_OUT])\n\t" \
285 " aghi %[R_OUTLEN],-32\n\t" \
286 " la %[R_OUT],32(%[R_OUT])\n\t" \
287 " clgijl %[R_INLEN],16,2f\n\t" \
288 " clgijl %[R_OUTLEN],32,2f\n\t" \
290 /* Setup to check for ch >= 0xd800 && ch <= 0xdfff. (v30, v31) */ \
291 "9: .short 0xd800,0xdfff,0x0,0x0,0x0,0x0,0x0,0x0\n\t" \
292 " .short 0xa000,0xc000,0x0,0x0,0x0,0x0,0x0,0x0\n\t" \
293 /* At least one uint16_t is in range of surrogates. \
294 Store the preceding chars. */ \
295 "10: vlgvb %[R_TMP],%%v19,7\n\t" \
296 " vuplhh %%v17,%%v16\n\t" \
297 " sllg %[R_TMP3],%[R_TMP],1\n\t" /* Number of out bytes. */ \
298 " ahik %[R_TMP2],%[R_TMP3],-1\n\t" /* Highest index to store. */ \
300 " vstl %%v17,%[R_TMP2],0(%[R_OUT])\n\t" \
301 " vupllh %%v18,%%v16\n\t" \
302 " ahi %[R_TMP2],-16\n\t" \
304 " vstl %%v18,%[R_TMP2],16(%[R_OUT])\n\t" \
305 "11: \n\t" /* Update pointers. */ \
306 " la %[R_IN],0(%[R_TMP],%[R_IN])\n\t" \
307 " slgr %[R_INLEN],%[R_TMP]\n\t" \
308 " la %[R_OUT],0(%[R_TMP3],%[R_OUT])\n\t" \
309 " slgr %[R_OUTLEN],%[R_TMP3]\n\t" \
310 /* Calculate remaining uint16_t values in loaded vrs. */ \
311 "12: lghi %[R_TMP2],16\n\t" \
312 " slgr %[R_TMP2],%[R_TMP]\n\t" \
313 " srl %[R_TMP2],1\n\t" \
314 " llh %[R_TMP],0(%[R_IN])\n\t" \
315 " aghi %[R_OUTLEN],-4\n\t" \
317 /* Handle remaining bytes. */ \
319 /* Zero, one or more bytes available? */ \
320 " clgfi %[R_INLEN],1\n\t" \
321 " je 97f\n\t" /* Only one byte available. */ \
322 " jl 99f\n\t" /* End if no bytes available. */ \
323 /* Calculate remaining uint16_t values in inptr. */ \
324 " srlg %[R_TMP2],%[R_INLEN],1\n\t" \
325 /* Handle remaining uint16_t values. */ \
326 "13: llh %[R_TMP],0(%[R_IN])\n\t" \
327 " slgfi %[R_OUTLEN],4\n\t" \
329 " clfi %[R_TMP],0xd800\n\t" \
331 "14: st %[R_TMP],0(%[R_OUT])\n\t" \
332 " la %[R_IN],2(%[R_IN])\n\t" \
333 " aghi %[R_INLEN],-2\n\t" \
334 " la %[R_OUT],4(%[R_OUT])\n\t" \
335 " brctg %[R_TMP2],13b\n\t" \
336 " j 0b\n\t" /* Switch to vx-loop. */ \
337 /* Handle UTF-16 surrogate pair. */ \
338 "15: clfi %[R_TMP],0xdfff\n\t" \
339 " jh 14b\n\t" /* Jump away if ch > 0xdfff. */ \
340 "16: clfi %[R_TMP],0xdc00\n\t" \
341 " jhe 98f\n\t" /* Jump away in case of low-surrogate. */ \
342 " slgfi %[R_INLEN],4\n\t" \
343 " jl 97f\n\t" /* Big enough input? */ \
344 " llh %[R_TMP3],2(%[R_IN])\n\t" /* Load low surrogate. */ \
345 " slfi %[R_TMP],0xd7c0\n\t" \
346 " sll %[R_TMP],10\n\t" \
347 " risbgn %[R_TMP],%[R_TMP3],54,63,0\n\t" /* Insert klmnopqrst. */ \
348 " nilf %[R_TMP3],0xfc00\n\t" \
349 " clfi %[R_TMP3],0xdc00\n\t" /* Check if it starts with 0xdc00. */ \
351 " st %[R_TMP],0(%[R_OUT])\n\t" \
352 " la %[R_IN],4(%[R_IN])\n\t" \
353 " la %[R_OUT],4(%[R_OUT])\n\t" \
354 " aghi %[R_TMP2],-2\n\t" \
355 " jh 13b\n\t" /* Handle remaining uint16_t values. */ \
356 " j 0b\n\t" /* Switch to vx-loop. */ \
357 "96: \n\t" /* Return full output. */ \
358 " lghi %[R_RES],%[RES_OUT_FULL]\n\t" \
360 "97: \n\t" /* Return incomplete input. */ \
361 " lghi %[R_RES],%[RES_IN_FULL]\n\t" \
363 "98:\n\t" /* Return Illegal character. */ \
364 " lghi %[R_RES],%[RES_IN_ILL]\n\t" \
367 : /* outputs */ [R_IN] "+a" (inptr) \
368 , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (outptr) \
369 , [R_OUTLEN] "+d" (outlen), [R_TMP] "=a" (tmp) \
370 , [R_TMP2] "=d" (tmp2), [R_TMP3] "=a" (tmp3) \
371 , [R_RES] "+d" (result) \
373 [RES_OUT_FULL] "i" (__GCONV_FULL_OUTPUT) \
374 , [RES_IN_ILL] "i" (__GCONV_ILLEGAL_INPUT) \
375 , [RES_IN_FULL] "i" (__GCONV_INCOMPLETE_INPUT) \
376 : /* clobber list */ "memory", "cc" \
377 ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
378 ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
379 ASM_CLOBBER_VR ("v30") ASM_CLOBBER_VR ("v31") \
381 if (__glibc_likely (inptr == inend) \
382 || result != __GCONV_ILLEGAL_INPUT) \
385 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
388 /* Generate loop-function with hardware vector instructions. */
389 # define MIN_NEEDED_INPUT MIN_NEEDED_FROM
390 # define MAX_NEEDED_INPUT MAX_NEEDED_FROM
391 # define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
392 # define FROM_LOOP_VX __from_utf16_loop_vx
393 # define LOOPFCT FROM_LOOP_VX
394 # define LOOP_NEED_FLAGS
395 # define BODY BODY_FROM_VX
396 # include <iconv/loop.c>
398 # define FROM_LOOP_VX NULL
399 #endif /* HAVE_FROM_VX != 1 */
401 #if HAVE_FROM_VX_CU == 1
402 #define BODY_FROM_VX_CU \
404 register const unsigned char* pInput asm ("8") = inptr; \
405 register size_t inlen asm ("9") = inend - inptr; \
406 register unsigned char* pOutput asm ("10") = outptr; \
407 register size_t outlen asm ("11") = outend - outptr; \
408 unsigned long tmp, tmp2, tmp3; \
409 asm volatile (".machine push\n\t" \
410 ".machine \"z13\"\n\t" \
411 ".machinemode \"zarch_nohighgprs\"\n\t" \
412 /* Setup to check for surrogates. */ \
413 " larl %[R_TMP],9f\n\t" \
414 " vlm %%v30,%%v31,0(%[R_TMP])\n\t" \
415 CONVERT_32BIT_SIZE_T ([R_INLEN]) \
416 CONVERT_32BIT_SIZE_T ([R_OUTLEN]) \
417 /* Loop which handles UTF-16 chars <0xd800, >0xdfff. */ \
418 "0: clgijl %[R_INLEN],16,20f\n\t" \
419 " clgijl %[R_OUTLEN],32,20f\n\t" \
420 "1: vl %%v16,0(%[R_IN])\n\t" \
421 /* Check for surrogate chars. */ \
422 " vstrchs %%v19,%%v16,%%v30,%%v31\n\t" \
424 /* Enlarge to UTF-32. */ \
425 " vuplhh %%v17,%%v16\n\t" \
426 " la %[R_IN],16(%[R_IN])\n\t" \
427 " vupllh %%v18,%%v16\n\t" \
428 " aghi %[R_INLEN],-16\n\t" \
429 /* Store 32 bytes to buf_out. */ \
430 " vstm %%v17,%%v18,0(%[R_OUT])\n\t" \
431 " aghi %[R_OUTLEN],-32\n\t" \
432 " la %[R_OUT],32(%[R_OUT])\n\t" \
433 " clgijl %[R_INLEN],16,20f\n\t" \
434 " clgijl %[R_OUTLEN],32,20f\n\t" \
436 /* Setup to check for ch >= 0xd800 && ch <= 0xdfff. (v30, v31) */ \
437 "9: .short 0xd800,0xdfff,0x0,0x0,0x0,0x0,0x0,0x0\n\t" \
438 " .short 0xa000,0xc000,0x0,0x0,0x0,0x0,0x0,0x0\n\t" \
439 /* At least one uint16_t is in range of surrogates. \
440 Store the preceding chars. */ \
441 "10: vlgvb %[R_TMP],%%v19,7\n\t" \
442 " vuplhh %%v17,%%v16\n\t" \
443 " sllg %[R_TMP3],%[R_TMP],1\n\t" /* Number of out bytes. */ \
444 " ahik %[R_TMP2],%[R_TMP3],-1\n\t" /* Highest index to store. */ \
446 " vstl %%v17,%[R_TMP2],0(%[R_OUT])\n\t" \
447 " vupllh %%v18,%%v16\n\t" \
448 " ahi %[R_TMP2],-16\n\t" \
450 " vstl %%v18,%[R_TMP2],16(%[R_OUT])\n\t" \
451 "11: \n\t" /* Update pointers. */ \
452 " la %[R_IN],0(%[R_TMP],%[R_IN])\n\t" \
453 " slgr %[R_INLEN],%[R_TMP]\n\t" \
454 " la %[R_OUT],0(%[R_TMP3],%[R_OUT])\n\t" \
455 " slgr %[R_OUTLEN],%[R_TMP3]\n\t" \
456 /* Handles UTF16 surrogates with convert instruction. */ \
457 "20: cu24 %[R_OUT],%[R_IN],1\n\t" \
458 " jo 0b\n\t" /* Try vector implemenation again. */ \
459 " lochil %[R_RES],%[RES_OUT_FULL]\n\t" /* cc == 1. */ \
460 " lochih %[R_RES],%[RES_IN_ILL]\n\t" /* cc == 2. */ \
462 : /* outputs */ [R_IN] "+a" (pInput) \
463 , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (pOutput) \
464 , [R_OUTLEN] "+d" (outlen), [R_TMP] "=a" (tmp) \
465 , [R_TMP2] "=d" (tmp2), [R_TMP3] "=a" (tmp3) \
466 , [R_RES] "+d" (result) \
468 [RES_OUT_FULL] "i" (__GCONV_FULL_OUTPUT) \
469 , [RES_IN_ILL] "i" (__GCONV_ILLEGAL_INPUT) \
470 : /* clobber list */ "memory", "cc" \
471 ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
472 ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
473 ASM_CLOBBER_VR ("v30") ASM_CLOBBER_VR ("v31") \
478 if (__glibc_likely (inlen == 0) \
479 || result == __GCONV_FULL_OUTPUT) \
483 /* Input does not contain a complete utf16 character. */ \
484 result = __GCONV_INCOMPLETE_INPUT; \
487 else if (result != __GCONV_ILLEGAL_INPUT) \
489 /* Input is >= 2 and < 4 bytes (as cu24 would have processed \
490 a possible next utf16 character) and not illegal. \
491 => we have a single high surrogate at end of input. */ \
492 result = __GCONV_INCOMPLETE_INPUT; \
496 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
499 /* Generate loop-function with hardware vector and utf-convert instructions. */
500 # define MIN_NEEDED_INPUT MIN_NEEDED_FROM
501 # define MAX_NEEDED_INPUT MAX_NEEDED_FROM
502 # define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
503 # define FROM_LOOP_VX_CU __from_utf16_loop_vx_cu
504 # define LOOPFCT FROM_LOOP_VX_CU
505 # define LOOP_NEED_FLAGS
506 # define BODY BODY_FROM_VX_CU
507 # include <iconv/loop.c>
509 # define FROM_LOOP_VX_CU NULL
510 #endif /* HAVE_FROM_VX_CU != 1 */
512 /* Conversion from UTF-32 internal/BE to UTF-16. */
515 /* The software routine is copied from utf-16.c (minus bytes
519 uint32_t c = get32 (inptr); \
521 if (__builtin_expect (c <= 0xd7ff, 1) \
522 || (c > 0xdfff && c <= 0xffff)) \
524 /* Two UTF-16 chars. */ \
527 else if (__builtin_expect (c >= 0x10000, 1) \
528 && __builtin_expect (c <= 0x10ffff, 1)) \
530 /* Four UTF-16 chars. */ \
531 uint16_t zabcd = ((c & 0x1f0000) >> 16) - 1; \
534 /* Generate a surrogate character. */ \
535 if (__glibc_unlikely (outptr + 4 > outend)) \
537 /* Overflow in the output buffer. */ \
538 result = __GCONV_FULL_OUTPUT; \
543 out |= (zabcd & 0xff) << 6; \
544 out |= (c >> 10) & 0x3f; \
545 put16 (outptr, out); \
550 put16 (outptr, out); \
554 STANDARD_TO_LOOP_ERR_HANDLER (4); \
560 /* Generate loop-function with software routing. */
561 # define MIN_NEEDED_INPUT MIN_NEEDED_TO
562 # define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
563 # define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
564 # define TO_LOOP_C __to_utf16_loop_c
565 # define LOOPFCT TO_LOOP_C
566 # define LOOP_NEED_FLAGS
567 # define BODY BODY_TO_C
568 # include <iconv/loop.c>
570 # define TO_LOOP_C NULL
571 #endif /* HAVE_TO_C != 1 */
574 # define BODY_TO_VX \
576 size_t inlen = inend - inptr; \
577 size_t outlen = outend - outptr; \
578 unsigned long tmp, tmp2, tmp3; \
579 asm volatile (".machine push\n\t" \
580 ".machine \"z13\"\n\t" \
581 ".machinemode \"zarch_nohighgprs\"\n\t" \
582 /* Setup to check for surrogates. */ \
583 " larl %[R_TMP],9f\n\t" \
584 " vlm %%v30,%%v31,0(%[R_TMP])\n\t" \
585 CONVERT_32BIT_SIZE_T ([R_INLEN]) \
586 CONVERT_32BIT_SIZE_T ([R_OUTLEN]) \
587 /* Loop which handles UTF-32 chars \
588 ch < 0xd800 || (ch > 0xdfff && ch < 0x10000). */ \
589 "0: clgijl %[R_INLEN],32,2f\n\t" \
590 " clgijl %[R_OUTLEN],16,2f\n\t" \
591 "1: vlm %%v16,%%v17,0(%[R_IN])\n\t" \
592 " lghi %[R_TMP2],0\n\t" \
593 /* Shorten to UTF-16. */ \
594 " vpkf %%v18,%%v16,%%v17\n\t" \
595 /* Check for surrogate chars. */ \
596 " vstrcfs %%v19,%%v16,%%v30,%%v31\n\t" \
598 " vstrcfs %%v19,%%v17,%%v30,%%v31\n\t" \
600 /* Store 16 bytes to buf_out. */ \
601 " vst %%v18,0(%[R_OUT])\n\t" \
602 " la %[R_IN],32(%[R_IN])\n\t" \
603 " aghi %[R_INLEN],-32\n\t" \
604 " aghi %[R_OUTLEN],-16\n\t" \
605 " la %[R_OUT],16(%[R_OUT])\n\t" \
606 " clgijl %[R_INLEN],32,2f\n\t" \
607 " clgijl %[R_OUTLEN],16,2f\n\t" \
609 /* Calculate remaining uint32_t values in inptr. */ \
611 " clgije %[R_INLEN],0,99f\n\t" \
612 " clgijl %[R_INLEN],4,92f\n\t" \
613 " srlg %[R_TMP2],%[R_INLEN],2\n\t" \
615 /* Setup to check for ch >= 0xd800 && ch <= 0xdfff \
616 and check for ch >= 0x10000. (v30, v31) */ \
617 "9: .long 0xd800,0xdfff,0x10000,0x10000\n\t" \
618 " .long 0xa0000000,0xc0000000, 0xa0000000,0xa0000000\n\t" \
619 /* At least on UTF32 char is in range of surrogates. \
620 Store the preceding characters. */ \
621 "11: ahi %[R_TMP2],16\n\t" \
622 "10: vlgvb %[R_TMP],%%v19,7\n\t" \
623 " agr %[R_TMP],%[R_TMP2]\n\t" \
624 " srlg %[R_TMP3],%[R_TMP],1\n\t" /* Number of out bytes. */ \
625 " ahik %[R_TMP2],%[R_TMP3],-1\n\t" /* Highest index to store. */ \
627 " vstl %%v18,%[R_TMP2],0(%[R_OUT])\n\t" \
628 /* Update pointers. */ \
629 " la %[R_IN],0(%[R_TMP],%[R_IN])\n\t" \
630 " slgr %[R_INLEN],%[R_TMP]\n\t" \
631 " la %[R_OUT],0(%[R_TMP3],%[R_OUT])\n\t" \
632 " slgr %[R_OUTLEN],%[R_TMP3]\n\t" \
633 /* Calculate remaining uint32_t values in vrs. */ \
634 "12: lghi %[R_TMP2],8\n\t" \
635 " srlg %[R_TMP3],%[R_TMP3],1\n\t" \
636 " slgr %[R_TMP2],%[R_TMP3]\n\t" \
637 /* Handle remaining UTF-32 characters. */ \
638 "20: l %[R_TMP],0(%[R_IN])\n\t" \
639 " aghi %[R_INLEN],-4\n\t" \
640 /* Test if ch is 2byte UTF-16 char. */ \
641 " clfi %[R_TMP],0xffff\n\t" \
643 /* Handle 2 byte UTF16 char. */ \
644 " lgr %[R_TMP3],%[R_TMP]\n\t" \
645 " nilf %[R_TMP],0xf800\n\t" \
646 " clfi %[R_TMP],0xd800\n\t" \
647 " je 91f\n\t" /* Do not accept UTF-16 surrogates. */ \
648 " slgfi %[R_OUTLEN],2\n\t" \
650 " sth %[R_TMP3],0(%[R_OUT])\n\t" \
651 " la %[R_IN],4(%[R_IN])\n\t" \
652 " la %[R_OUT],2(%[R_OUT])\n\t" \
653 " brctg %[R_TMP2],20b\n\t" \
654 " j 0b\n\t" /* Switch to vx-loop. */ \
655 /* Test if ch is 4byte UTF-16 char. */ \
656 "21: clfi %[R_TMP],0x10ffff\n\t" \
657 " jh 91f\n\t" /* ch > 0x10ffff is not allowed! */ \
658 /* Handle 4 byte UTF16 char. */ \
659 " slgfi %[R_OUTLEN],4\n\t" \
661 " slfi %[R_TMP],0x10000\n\t" /* zabcd = uvwxy - 1. */ \
662 " llilf %[R_TMP3],0xd800dc00\n\t" \
663 " la %[R_IN],4(%[R_IN])\n\t" \
664 " risbgn %[R_TMP3],%[R_TMP],38,47,6\n\t" /* High surrogate. */ \
665 " risbgn %[R_TMP3],%[R_TMP],54,63,0\n\t" /* Low surrogate. */ \
666 " st %[R_TMP3],0(%[R_OUT])\n\t" \
667 " la %[R_OUT],4(%[R_OUT])\n\t" \
668 " brctg %[R_TMP2],20b\n\t" \
669 " j 0b\n\t" /* Switch to vx-loop. */ \
670 "92: lghi %[R_RES],%[RES_IN_FULL]\n\t" \
672 "91: lghi %[R_RES],%[RES_IN_ILL]\n\t" \
674 "90: lghi %[R_RES],%[RES_OUT_FULL]\n\t" \
677 : /* outputs */ [R_IN] "+a" (inptr) \
678 , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (outptr) \
679 , [R_OUTLEN] "+d" (outlen), [R_TMP] "=a" (tmp) \
680 , [R_TMP2] "=d" (tmp2), [R_TMP3] "=a" (tmp3) \
681 , [R_RES] "+d" (result) \
683 [RES_OUT_FULL] "i" (__GCONV_FULL_OUTPUT) \
684 , [RES_IN_ILL] "i" (__GCONV_ILLEGAL_INPUT) \
685 , [RES_IN_FULL] "i" (__GCONV_INCOMPLETE_INPUT) \
686 : /* clobber list */ "memory", "cc" \
687 ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
688 ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
689 ASM_CLOBBER_VR ("v30") ASM_CLOBBER_VR ("v31") \
691 if (__glibc_likely (inptr == inend) \
692 || result != __GCONV_ILLEGAL_INPUT) \
695 STANDARD_TO_LOOP_ERR_HANDLER (4); \
698 /* Generate loop-function with hardware vector instructions. */
699 # define MIN_NEEDED_INPUT MIN_NEEDED_TO
700 # define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
701 # define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
702 # define TO_LOOP_VX __to_utf16_loop_vx
703 # define LOOPFCT TO_LOOP_VX
704 # define LOOP_NEED_FLAGS
705 # define BODY BODY_TO_VX
706 # include <iconv/loop.c>
708 # define TO_LOOP_VX NULL
709 #endif /* HAVE_TO_VX != 1 */
711 #if HAVE_TO_VX_CU == 1
712 #define BODY_TO_VX_CU \
714 register const unsigned char* pInput asm ("8") = inptr; \
715 register size_t inlen asm ("9") = inend - inptr; \
716 register unsigned char* pOutput asm ("10") = outptr; \
717 register size_t outlen asm ("11") = outend - outptr; \
718 unsigned long tmp, tmp2, tmp3; \
719 asm volatile (".machine push\n\t" \
720 ".machine \"z13\"\n\t" \
721 ".machinemode \"zarch_nohighgprs\"\n\t" \
722 /* Setup to check for surrogates. */ \
723 " larl %[R_TMP],9f\n\t" \
724 " vlm %%v30,%%v31,0(%[R_TMP])\n\t" \
725 CONVERT_32BIT_SIZE_T ([R_INLEN]) \
726 CONVERT_32BIT_SIZE_T ([R_OUTLEN]) \
727 /* Loop which handles UTF-32 chars \
728 ch < 0xd800 || (ch > 0xdfff && ch < 0x10000). */ \
729 "0: clgijl %[R_INLEN],32,20f\n\t" \
730 " clgijl %[R_OUTLEN],16,20f\n\t" \
731 "1: vlm %%v16,%%v17,0(%[R_IN])\n\t" \
732 " lghi %[R_TMP2],0\n\t" \
733 /* Shorten to UTF-16. */ \
734 " vpkf %%v18,%%v16,%%v17\n\t" \
735 /* Check for surrogate chars. */ \
736 " vstrcfs %%v19,%%v16,%%v30,%%v31\n\t" \
738 " vstrcfs %%v19,%%v17,%%v30,%%v31\n\t" \
740 /* Store 16 bytes to buf_out. */ \
741 " vst %%v18,0(%[R_OUT])\n\t" \
742 " la %[R_IN],32(%[R_IN])\n\t" \
743 " aghi %[R_INLEN],-32\n\t" \
744 " aghi %[R_OUTLEN],-16\n\t" \
745 " la %[R_OUT],16(%[R_OUT])\n\t" \
746 " clgijl %[R_INLEN],32,20f\n\t" \
747 " clgijl %[R_OUTLEN],16,20f\n\t" \
749 /* Setup to check for ch >= 0xd800 && ch <= 0xdfff \
750 and check for ch >= 0x10000. (v30, v31) */ \
751 "9: .long 0xd800,0xdfff,0x10000,0x10000\n\t" \
752 " .long 0xa0000000,0xc0000000, 0xa0000000,0xa0000000\n\t" \
753 /* At least one UTF32 char is in range of surrogates. \
754 Store the preceding characters. */ \
755 "11: ahi %[R_TMP2],16\n\t" \
756 "10: vlgvb %[R_TMP],%%v19,7\n\t" \
757 " agr %[R_TMP],%[R_TMP2]\n\t" \
758 " srlg %[R_TMP3],%[R_TMP],1\n\t" /* Number of out bytes. */ \
759 " ahik %[R_TMP2],%[R_TMP3],-1\n\t" /* Highest index to store. */ \
761 " vstl %%v18,%[R_TMP2],0(%[R_OUT])\n\t" \
762 /* Update pointers. */ \
763 " la %[R_IN],0(%[R_TMP],%[R_IN])\n\t" \
764 " slgr %[R_INLEN],%[R_TMP]\n\t" \
765 " la %[R_OUT],0(%[R_TMP3],%[R_OUT])\n\t" \
766 " slgr %[R_OUTLEN],%[R_TMP3]\n\t" \
767 /* Handles UTF16 surrogates with convert instruction. */ \
768 "20: cu42 %[R_OUT],%[R_IN]\n\t" \
769 " jo 0b\n\t" /* Try vector implemenation again. */ \
770 " lochil %[R_RES],%[RES_OUT_FULL]\n\t" /* cc == 1. */ \
771 " lochih %[R_RES],%[RES_IN_ILL]\n\t" /* cc == 2. */ \
773 : /* outputs */ [R_IN] "+a" (pInput) \
774 , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (pOutput) \
775 , [R_OUTLEN] "+d" (outlen), [R_TMP] "=a" (tmp) \
776 , [R_TMP2] "=d" (tmp2), [R_TMP3] "=a" (tmp3) \
777 , [R_RES] "+d" (result) \
779 [RES_OUT_FULL] "i" (__GCONV_FULL_OUTPUT) \
780 , [RES_IN_ILL] "i" (__GCONV_ILLEGAL_INPUT) \
781 : /* clobber list */ "memory", "cc" \
782 ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
783 ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
784 ASM_CLOBBER_VR ("v30") ASM_CLOBBER_VR ("v31") \
789 if (__glibc_likely (inlen == 0) \
790 || result == __GCONV_FULL_OUTPUT) \
794 result = __GCONV_INCOMPLETE_INPUT; \
798 STANDARD_TO_LOOP_ERR_HANDLER (4); \
801 /* Generate loop-function with hardware vector and utf-convert instructions. */
802 # define MIN_NEEDED_INPUT MIN_NEEDED_TO
803 # define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
804 # define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
805 # define TO_LOOP_VX_CU __to_utf16_loop_vx_cu
806 # define LOOPFCT TO_LOOP_VX_CU
807 # define LOOP_NEED_FLAGS
808 # define BODY BODY_TO_VX_CU
809 # include <iconv/loop.c>
811 # define TO_LOOP_VX_CU NULL
812 #endif /* HAVE_TO_VX_CU != 1 */
814 /* This file also exists in sysdeps/s390/multiarch/ which
815 generates ifunc resolvers for FROM/TO_LOOP functions
816 and includes iconv/skeleton.c afterwards. */
817 #if ! defined USE_MULTIARCH
818 # include <iconv/skeleton.c>