x86: Add _CET_ENDBR to functions in crti.S
[glibc.git] / sysdeps / s390 / utf16-utf32-z9.c
blob27086d3e8a1f44ed9289e87f6bb636a171497c07
1 /* Conversion between UTF-16 and UTF-32 BE/internal.
3 This module uses the Z9-109 variants of the Convert Unicode
4 instructions.
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/>. */
27 #include <dlfcn.h>
28 #include <stdint.h>
29 #include <unistd.h>
30 #include <gconv.h>
31 #include <string.h>
33 /* Select which versions should be defined depending on support
34 for multiarch, vector and used minimum architecture level. */
35 #define HAVE_FROM_C 1
36 #define FROM_LOOP_DEFAULT FROM_LOOP_C
37 #define HAVE_TO_C 1
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
43 # define HAVE_TO_VX 1
44 # define HAVE_TO_VX_CU 1
45 #else
46 # define HAVE_FROM_VX 0
47 # define HAVE_FROM_VX_CU 0
48 # define HAVE_TO_VX 0
49 # define HAVE_TO_VX_CU 0
50 #endif
52 #if defined HAVE_S390_VX_GCC_SUPPORT
53 # define ASM_CLOBBER_VR(NR) , NR
54 #else
55 # define ASM_CLOBBER_VR(NR)
56 #endif
58 #if defined __s390x__
59 # define CONVERT_32BIT_SIZE_T(REG)
60 #else
61 # define CONVERT_32BIT_SIZE_T(REG) "llgfr %" #REG ",%" #REG "\n\t"
62 #endif
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
70 #define DEFINE_INIT 0
71 #define DEFINE_FINI 0
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. */
81 enum direction
83 illegal_dir,
84 to_utf16,
85 from_utf16
88 struct utf16_data
90 enum direction dir;
91 int emit_bom;
95 extern int gconv_init (struct __gconv_step *step);
96 int
97 gconv_init (struct __gconv_step *step)
99 /* Determine which direction. */
100 struct utf16_data *new_data;
101 enum direction dir = illegal_dir;
102 int emit_bom;
103 int result;
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))
113 dir = from_utf16;
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))
120 dir = to_utf16;
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)
131 new_data->dir = dir;
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;
142 else
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;
152 result = __GCONV_OK;
156 return result;
160 extern void gconv_end (struct __gconv_step *data);
161 void
162 gconv_end (struct __gconv_step *data)
164 free (data->__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); \
181 outbuf += 2; \
183 else \
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); \
190 outbuf += 4; \
194 /* Conversion function from UTF-16 to UTF-32 internal/BE. */
196 #if HAVE_FROM_C == 1
197 /* The software routine is copied from utf-16.c (minus bytes
198 swapping). */
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); \
207 inptr += 2; \
209 else \
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 \
218 it is. */ \
219 if (__glibc_unlikely (inptr + 4 > inend)) \
221 /* We don't have enough input for another complete input \
222 character. */ \
223 result = __GCONV_INCOMPLETE_INPUT; \
224 break; \
227 inptr += 2; \
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. */ \
233 inptr -= 2; \
234 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
237 put32 (outptr, ((u1 - 0xd7c0) << 10) + (u2 - 0xdc00)); \
238 inptr += 2; \
240 outptr += 4; \
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>
253 #else
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" \
277 " jno 10f\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" \
289 " j 1b\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. */ \
299 " jl 12f\n\t" \
300 " vstl %%v17,%[R_TMP2],0(%[R_OUT])\n\t" \
301 " vupllh %%v18,%%v16\n\t" \
302 " ahi %[R_TMP2],-16\n\t" \
303 " jl 11f\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" \
316 " j 16f\n\t" \
317 /* Handle remaining bytes. */ \
318 "2: \n\t" \
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" \
328 " jl 96f \n\t" \
329 " clfi %[R_TMP],0xd800\n\t" \
330 " jhe 15f\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. */ \
350 " jne 98f\n\t" \
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" \
359 " j 99f\n\t" \
360 "97: \n\t" /* Return incomplete input. */ \
361 " lghi %[R_RES],%[RES_IN_FULL]\n\t" \
362 " j 99f\n\t" \
363 "98:\n\t" /* Return Illegal character. */ \
364 " lghi %[R_RES],%[RES_IN_ILL]\n\t" \
365 "99:\n\t" \
366 ".machine pop" \
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) \
372 : /* inputs */ \
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") \
380 ); \
381 if (__glibc_likely (inptr == inend) \
382 || result != __GCONV_ILLEGAL_INPUT) \
383 break; \
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>
397 #else
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" \
423 " jno 10f\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" \
435 " j 1b\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. */ \
445 " jl 20f\n\t" \
446 " vstl %%v17,%[R_TMP2],0(%[R_OUT])\n\t" \
447 " vupllh %%v18,%%v16\n\t" \
448 " ahi %[R_TMP2],-16\n\t" \
449 " jl 11f\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. */ \
461 ".machine pop" \
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) \
467 : /* inputs */ \
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") \
474 ); \
475 inptr = pInput; \
476 outptr = pOutput; \
478 if (__glibc_likely (inlen == 0) \
479 || result == __GCONV_FULL_OUTPUT) \
480 break; \
481 if (inlen == 1) \
483 /* Input does not contain a complete utf16 character. */ \
484 result = __GCONV_INCOMPLETE_INPUT; \
485 break; \
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; \
493 break; \
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>
508 #else
509 # define FROM_LOOP_VX_CU NULL
510 #endif /* HAVE_FROM_VX_CU != 1 */
512 /* Conversion from UTF-32 internal/BE to UTF-16. */
514 #if HAVE_TO_C == 1
515 /* The software routine is copied from utf-16.c (minus bytes
516 swapping). */
517 # define BODY_TO_C \
519 uint32_t c = get32 (inptr); \
521 if (__builtin_expect (c <= 0xd7ff, 1) \
522 || (c > 0xdfff && c <= 0xffff)) \
524 /* Two UTF-16 chars. */ \
525 put16 (outptr, c); \
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; \
532 uint16_t out; \
534 /* Generate a surrogate character. */ \
535 if (__glibc_unlikely (outptr + 4 > outend)) \
537 /* Overflow in the output buffer. */ \
538 result = __GCONV_FULL_OUTPUT; \
539 break; \
542 out = 0xd800; \
543 out |= (zabcd & 0xff) << 6; \
544 out |= (c >> 10) & 0x3f; \
545 put16 (outptr, out); \
546 outptr += 2; \
548 out = 0xdc00; \
549 out |= c & 0x3ff; \
550 put16 (outptr, out); \
552 else \
554 STANDARD_TO_LOOP_ERR_HANDLER (4); \
556 outptr += 2; \
557 inptr += 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>
569 #else
570 # define TO_LOOP_C NULL
571 #endif /* HAVE_TO_C != 1 */
573 #if HAVE_TO_VX == 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" \
597 " jno 10f\n\t" \
598 " vstrcfs %%v19,%%v17,%%v30,%%v31\n\t" \
599 " jno 11f\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" \
608 " j 1b\n\t" \
609 /* Calculate remaining uint32_t values in inptr. */ \
610 "2: \n\t" \
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" \
614 " j 20f\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. */ \
626 " jl 12f\n\t" \
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" \
642 " jh 21f\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" \
649 " jl 90f \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" \
660 " jl 90f \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" \
671 " j 99f\n\t" \
672 "91: lghi %[R_RES],%[RES_IN_ILL]\n\t" \
673 " j 99f\n\t" \
674 "90: lghi %[R_RES],%[RES_OUT_FULL]\n\t" \
675 "99: \n\t" \
676 ".machine pop" \
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) \
682 : /* inputs */ \
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") \
690 ); \
691 if (__glibc_likely (inptr == inend) \
692 || result != __GCONV_ILLEGAL_INPUT) \
693 break; \
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>
707 #else
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" \
737 " jno 10f\n\t" \
738 " vstrcfs %%v19,%%v17,%%v30,%%v31\n\t" \
739 " jno 11f\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" \
748 " j 1b\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. */ \
760 " jl 20f\n\t" \
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. */ \
772 ".machine pop" \
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) \
778 : /* inputs */ \
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") \
785 ); \
786 inptr = pInput; \
787 outptr = pOutput; \
789 if (__glibc_likely (inlen == 0) \
790 || result == __GCONV_FULL_OUTPUT) \
791 break; \
792 if (inlen < 4) \
794 result = __GCONV_INCOMPLETE_INPUT; \
795 break; \
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>
810 #else
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>
819 #endif