gconv_simple.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /* Simple transformations functions - s390 version.
  2. Copyright (C) 2016-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #if defined HAVE_S390_VX_ASM_SUPPORT
  16. # include <ifunc-resolve.h>
  17. # if defined HAVE_S390_VX_GCC_SUPPORT
  18. # define ASM_CLOBBER_VR(NR) , NR
  19. # else
  20. # define ASM_CLOBBER_VR(NR)
  21. # endif
  22. # define ICONV_C_NAME(NAME) __##NAME##_c
  23. # define ICONV_VX_NAME(NAME) __##NAME##_vx
  24. # ifdef HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT
  25. /* We support z13 instructions by default -> Just use the vector variant. */
  26. # define ICONV_VX_IFUNC(FUNC) strong_alias (ICONV_VX_NAME (FUNC), FUNC)
  27. # else
  28. /* We have to use ifunc to determine if z13 instructions are supported. */
  29. # define ICONV_VX_IFUNC(FUNC) \
  30. s390_libc_ifunc_expr (ICONV_C_NAME (FUNC), FUNC, \
  31. (hwcap & HWCAP_S390_VX) \
  32. ? ICONV_VX_NAME (FUNC) \
  33. : ICONV_C_NAME (FUNC) \
  34. )
  35. # endif
  36. # define ICONV_VX_SINGLE(NAME) \
  37. static __typeof (NAME##_single) __##NAME##_vx_single __attribute__((alias(#NAME "_single")));
  38. /* Generate the transformations which are used, if the target machine does not
  39. support vector instructions. */
  40. # define __gconv_transform_ascii_internal \
  41. ICONV_C_NAME (__gconv_transform_ascii_internal)
  42. # define __gconv_transform_internal_ascii \
  43. ICONV_C_NAME (__gconv_transform_internal_ascii)
  44. # define __gconv_transform_internal_ucs4le \
  45. ICONV_C_NAME (__gconv_transform_internal_ucs4le)
  46. # define __gconv_transform_ucs4_internal \
  47. ICONV_C_NAME (__gconv_transform_ucs4_internal)
  48. # define __gconv_transform_ucs4le_internal \
  49. ICONV_C_NAME (__gconv_transform_ucs4le_internal)
  50. # define __gconv_transform_ucs2_internal \
  51. ICONV_C_NAME (__gconv_transform_ucs2_internal)
  52. # define __gconv_transform_ucs2reverse_internal \
  53. ICONV_C_NAME (__gconv_transform_ucs2reverse_internal)
  54. # define __gconv_transform_internal_ucs2 \
  55. ICONV_C_NAME (__gconv_transform_internal_ucs2)
  56. # define __gconv_transform_internal_ucs2reverse \
  57. ICONV_C_NAME (__gconv_transform_internal_ucs2reverse)
  58. # include <iconv/gconv_simple.c>
  59. # undef __gconv_transform_ascii_internal
  60. # undef __gconv_transform_internal_ascii
  61. # undef __gconv_transform_internal_ucs4le
  62. # undef __gconv_transform_ucs4_internal
  63. # undef __gconv_transform_ucs4le_internal
  64. # undef __gconv_transform_ucs2_internal
  65. # undef __gconv_transform_ucs2reverse_internal
  66. # undef __gconv_transform_internal_ucs2
  67. # undef __gconv_transform_internal_ucs2reverse
  68. /* Now define the functions with vector support. */
  69. # if defined __s390x__
  70. # define CONVERT_32BIT_SIZE_T(REG)
  71. # else
  72. # define CONVERT_32BIT_SIZE_T(REG) "llgfr %" #REG ",%" #REG "\n\t"
  73. # endif
  74. /* Convert from ISO 646-IRV to the internal (UCS4-like) format. */
  75. # define DEFINE_INIT 0
  76. # define DEFINE_FINI 0
  77. # define MIN_NEEDED_FROM 1
  78. # define MIN_NEEDED_TO 4
  79. # define FROM_DIRECTION 1
  80. # define FROM_LOOP ICONV_VX_NAME (ascii_internal_loop)
  81. # define TO_LOOP ICONV_VX_NAME (ascii_internal_loop) /* This is not used. */
  82. # define FUNCTION_NAME ICONV_VX_NAME (__gconv_transform_ascii_internal)
  83. # define ONE_DIRECTION 1
  84. # define MIN_NEEDED_INPUT MIN_NEEDED_FROM
  85. # define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
  86. # define LOOPFCT FROM_LOOP
  87. # define BODY_ORIG_ERROR \
  88. /* The value is too large. We don't try transliteration here since \
  89. this is not an error because of the lack of possibilities to \
  90. represent the result. This is a genuine bug in the input since \
  91. ASCII does not allow such values. */ \
  92. STANDARD_FROM_LOOP_ERR_HANDLER (1);
  93. # define BODY_ORIG \
  94. { \
  95. if (__glibc_unlikely (*inptr > '\x7f')) \
  96. { \
  97. BODY_ORIG_ERROR \
  98. } \
  99. else \
  100. { \
  101. /* It's an one byte sequence. */ \
  102. *((uint32_t *) outptr) = *inptr++; \
  103. outptr += sizeof (uint32_t); \
  104. } \
  105. }
  106. # define BODY \
  107. { \
  108. size_t len = inend - inptr; \
  109. if (len > (outend - outptr) / 4) \
  110. len = (outend - outptr) / 4; \
  111. size_t loop_count, tmp; \
  112. __asm__ volatile (".machine push\n\t" \
  113. ".machine \"z13\"\n\t" \
  114. ".machinemode \"zarch_nohighgprs\"\n\t" \
  115. CONVERT_32BIT_SIZE_T ([R_LEN]) \
  116. " vrepib %%v30,0x7f\n\t" /* For compare > 0x7f. */ \
  117. " srlg %[R_LI],%[R_LEN],4\n\t" \
  118. " vrepib %%v31,0x20\n\t" \
  119. " clgije %[R_LI],0,1f\n\t" \
  120. "0: \n\t" /* Handle 16-byte blocks. */ \
  121. " vl %%v16,0(%[R_IN])\n\t" \
  122. /* Checking for values > 0x7f. */ \
  123. " vstrcbs %%v17,%%v16,%%v30,%%v31\n\t" \
  124. " jno 10f\n\t" \
  125. /* Enlarge to UCS4. */ \
  126. " vuplhb %%v17,%%v16\n\t" \
  127. " vupllb %%v18,%%v16\n\t" \
  128. " vuplhh %%v19,%%v17\n\t" \
  129. " vupllh %%v20,%%v17\n\t" \
  130. " vuplhh %%v21,%%v18\n\t" \
  131. " vupllh %%v22,%%v18\n\t" \
  132. /* Store 64bytes to buf_out. */ \
  133. " vstm %%v19,%%v22,0(%[R_OUT])\n\t" \
  134. " la %[R_IN],16(%[R_IN])\n\t" \
  135. " la %[R_OUT],64(%[R_OUT])\n\t" \
  136. " brctg %[R_LI],0b\n\t" \
  137. " lghi %[R_LI],15\n\t" \
  138. " ngr %[R_LEN],%[R_LI]\n\t" \
  139. " je 20f\n\t" /* Jump away if no remaining bytes. */ \
  140. /* Handle remaining bytes. */ \
  141. "1: aghik %[R_LI],%[R_LEN],-1\n\t" \
  142. " jl 20f\n\t" /* Jump away if no remaining bytes. */ \
  143. " vll %%v16,%[R_LI],0(%[R_IN])\n\t" \
  144. /* Checking for values > 0x7f. */ \
  145. " vstrcbs %%v17,%%v16,%%v30,%%v31\n\t" \
  146. " vlgvb %[R_TMP],%%v17,7\n\t" \
  147. " clr %[R_TMP],%[R_LI]\n\t" \
  148. " locrh %[R_TMP],%[R_LEN]\n\t" \
  149. " locghih %[R_LEN],0\n\t" \
  150. " j 12f\n\t" \
  151. "10:\n\t" \
  152. /* Found a value > 0x7f. \
  153. Store the preceding chars. */ \
  154. " vlgvb %[R_TMP],%%v17,7\n\t" \
  155. "12: la %[R_IN],0(%[R_TMP],%[R_IN])\n\t" \
  156. " sllk %[R_TMP],%[R_TMP],2\n\t" \
  157. " ahi %[R_TMP],-1\n\t" \
  158. " jl 20f\n\t" \
  159. " lgr %[R_LI],%[R_TMP]\n\t" \
  160. " vuplhb %%v17,%%v16\n\t" \
  161. " vuplhh %%v19,%%v17\n\t" \
  162. " vstl %%v19,%[R_LI],0(%[R_OUT])\n\t" \
  163. " ahi %[R_LI],-16\n\t" \
  164. " jl 11f\n\t" \
  165. " vupllh %%v20,%%v17\n\t" \
  166. " vstl %%v20,%[R_LI],16(%[R_OUT])\n\t" \
  167. " ahi %[R_LI],-16\n\t" \
  168. " jl 11f\n\t" \
  169. " vupllb %%v18,%%v16\n\t" \
  170. " vuplhh %%v21,%%v18\n\t" \
  171. " vstl %%v21,%[R_LI],32(%[R_OUT])\n\t" \
  172. " ahi %[R_LI],-16\n\t" \
  173. " jl 11f\n\t" \
  174. " vupllh %%v22,%%v18\n\t" \
  175. " vstl %%v22,%[R_LI],48(%[R_OUT])\n\t" \
  176. "11:\n\t" \
  177. " la %[R_OUT],1(%[R_TMP],%[R_OUT])\n\t" \
  178. "20:\n\t" \
  179. ".machine pop" \
  180. : /* outputs */ [R_OUT] "+a" (outptr) \
  181. , [R_IN] "+a" (inptr) \
  182. , [R_LEN] "+d" (len) \
  183. , [R_LI] "=d" (loop_count) \
  184. , [R_TMP] "=a" (tmp) \
  185. : /* inputs */ \
  186. : /* clobber list*/ "memory", "cc" \
  187. ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
  188. ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
  189. ASM_CLOBBER_VR ("v20") ASM_CLOBBER_VR ("v21") \
  190. ASM_CLOBBER_VR ("v22") ASM_CLOBBER_VR ("v30") \
  191. ASM_CLOBBER_VR ("v31") \
  192. ); \
  193. if (len > 0) \
  194. { \
  195. /* Found an invalid character at the next input byte. */ \
  196. BODY_ORIG_ERROR \
  197. } \
  198. }
  199. # define LOOP_NEED_FLAGS
  200. # include <iconv/loop.c>
  201. # include <iconv/skeleton.c>
  202. # undef BODY_ORIG
  203. # undef BODY_ORIG_ERROR
  204. ICONV_VX_IFUNC (__gconv_transform_ascii_internal)
  205. /* Convert from the internal (UCS4-like) format to ISO 646-IRV. */
  206. # define DEFINE_INIT 0
  207. # define DEFINE_FINI 0
  208. # define MIN_NEEDED_FROM 4
  209. # define MIN_NEEDED_TO 1
  210. # define FROM_DIRECTION 1
  211. # define FROM_LOOP ICONV_VX_NAME (internal_ascii_loop)
  212. # define TO_LOOP ICONV_VX_NAME (internal_ascii_loop) /* This is not used. */
  213. # define FUNCTION_NAME ICONV_VX_NAME (__gconv_transform_internal_ascii)
  214. # define ONE_DIRECTION 1
  215. # define MIN_NEEDED_INPUT MIN_NEEDED_FROM
  216. # define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
  217. # define LOOPFCT FROM_LOOP
  218. # define BODY_ORIG_ERROR \
  219. UNICODE_TAG_HANDLER (*((const uint32_t *) inptr), 4); \
  220. STANDARD_TO_LOOP_ERR_HANDLER (4);
  221. # define BODY_ORIG \
  222. { \
  223. if (__glibc_unlikely (*((const uint32_t *) inptr) > 0x7f)) \
  224. { \
  225. BODY_ORIG_ERROR \
  226. } \
  227. else \
  228. { \
  229. /* It's an one byte sequence. */ \
  230. *outptr++ = *((const uint32_t *) inptr); \
  231. inptr += sizeof (uint32_t); \
  232. } \
  233. }
  234. # define BODY \
  235. { \
  236. size_t len = (inend - inptr) / 4; \
  237. if (len > outend - outptr) \
  238. len = outend - outptr; \
  239. size_t loop_count, tmp, tmp2; \
  240. __asm__ volatile (".machine push\n\t" \
  241. ".machine \"z13\"\n\t" \
  242. ".machinemode \"zarch_nohighgprs\"\n\t" \
  243. CONVERT_32BIT_SIZE_T ([R_LEN]) \
  244. /* Setup to check for ch > 0x7f. */ \
  245. " vzero %%v21\n\t" \
  246. " srlg %[R_LI],%[R_LEN],4\n\t" \
  247. " vleih %%v21,8192,0\n\t" /* element 0: > */ \
  248. " vleih %%v21,-8192,2\n\t" /* element 1: =<> */ \
  249. " vleif %%v20,127,0\n\t" /* element 0: 127 */ \
  250. " lghi %[R_TMP],0\n\t" \
  251. " clgije %[R_LI],0,1f\n\t" \
  252. "0:\n\t" \
  253. " vlm %%v16,%%v19,0(%[R_IN])\n\t" \
  254. /* Shorten to byte values. */ \
  255. " vpkf %%v23,%%v16,%%v17\n\t" \
  256. " vpkf %%v24,%%v18,%%v19\n\t" \
  257. " vpkh %%v23,%%v23,%%v24\n\t" \
  258. /* Checking for values > 0x7f. */ \
  259. " vstrcfs %%v22,%%v16,%%v20,%%v21\n\t" \
  260. " jno 10f\n\t" \
  261. " vstrcfs %%v22,%%v17,%%v20,%%v21\n\t" \
  262. " jno 11f\n\t" \
  263. " vstrcfs %%v22,%%v18,%%v20,%%v21\n\t" \
  264. " jno 12f\n\t" \
  265. " vstrcfs %%v22,%%v19,%%v20,%%v21\n\t" \
  266. " jno 13f\n\t" \
  267. /* Store 16bytes to outptr. */ \
  268. " vst %%v23,0(%[R_OUT])\n\t" \
  269. " la %[R_IN],64(%[R_IN])\n\t" \
  270. " la %[R_OUT],16(%[R_OUT])\n\t" \
  271. " brctg %[R_LI],0b\n\t" \
  272. " lghi %[R_LI],15\n\t" \
  273. " ngr %[R_LEN],%[R_LI]\n\t" \
  274. " je 20f\n\t" /* Jump away if no remaining bytes. */ \
  275. /* Handle remaining bytes. */ \
  276. "1: sllg %[R_LI],%[R_LEN],2\n\t" \
  277. " aghi %[R_LI],-1\n\t" \
  278. " jl 20f\n\t" /* Jump away if no remaining bytes. */ \
  279. /* Load remaining 1...63 bytes. */ \
  280. " vll %%v16,%[R_LI],0(%[R_IN])\n\t" \
  281. " ahi %[R_LI],-16\n\t" \
  282. " jl 2f\n\t" \
  283. " vll %%v17,%[R_LI],16(%[R_IN])\n\t" \
  284. " ahi %[R_LI],-16\n\t" \
  285. " jl 2f\n\t" \
  286. " vll %%v18,%[R_LI],32(%[R_IN])\n\t" \
  287. " ahi %[R_LI],-16\n\t" \
  288. " jl 2f\n\t" \
  289. " vll %%v19,%[R_LI],48(%[R_IN])\n\t" \
  290. "2:\n\t" \
  291. /* Shorten to byte values. */ \
  292. " vpkf %%v23,%%v16,%%v17\n\t" \
  293. " vpkf %%v24,%%v18,%%v19\n\t" \
  294. " vpkh %%v23,%%v23,%%v24\n\t" \
  295. " sllg %[R_LI],%[R_LEN],2\n\t" \
  296. " aghi %[R_LI],-16\n\t" \
  297. " jl 3f\n\t" /* v16 is not fully loaded. */ \
  298. " vstrcfs %%v22,%%v16,%%v20,%%v21\n\t" \
  299. " jno 10f\n\t" \
  300. " aghi %[R_LI],-16\n\t" \
  301. " jl 4f\n\t" /* v17 is not fully loaded. */ \
  302. " vstrcfs %%v22,%%v17,%%v20,%%v21\n\t" \
  303. " jno 11f\n\t" \
  304. " aghi %[R_LI],-16\n\t" \
  305. " jl 5f\n\t" /* v18 is not fully loaded. */ \
  306. " vstrcfs %%v22,%%v18,%%v20,%%v21\n\t" \
  307. " jno 12f\n\t" \
  308. " aghi %[R_LI],-16\n\t" \
  309. /* v19 is not fully loaded. */ \
  310. " lghi %[R_TMP],12\n\t" \
  311. " vstrcfs %%v22,%%v19,%%v20,%%v21\n\t" \
  312. "6: vlgvb %[R_I],%%v22,7\n\t" \
  313. " aghi %[R_LI],16\n\t" \
  314. " clrjl %[R_I],%[R_LI],14f\n\t" \
  315. " lgr %[R_I],%[R_LEN]\n\t" \
  316. " lghi %[R_LEN],0\n\t" \
  317. " j 15f\n\t" \
  318. "3: vstrcfs %%v22,%%v16,%%v20,%%v21\n\t" \
  319. " j 6b\n\t" \
  320. "4: vstrcfs %%v22,%%v17,%%v20,%%v21\n\t" \
  321. " lghi %[R_TMP],4\n\t" \
  322. " j 6b\n\t" \
  323. "5: vstrcfs %%v22,%%v17,%%v20,%%v21\n\t" \
  324. " lghi %[R_TMP],8\n\t" \
  325. " j 6b\n\t" \
  326. /* Found a value > 0x7f. */ \
  327. "13: ahi %[R_TMP],4\n\t" \
  328. "12: ahi %[R_TMP],4\n\t" \
  329. "11: ahi %[R_TMP],4\n\t" \
  330. "10: vlgvb %[R_I],%%v22,7\n\t" \
  331. "14: srlg %[R_I],%[R_I],2\n\t" \
  332. " agr %[R_I],%[R_TMP]\n\t" \
  333. " je 20f\n\t" \
  334. /* Store characters before invalid one... */ \
  335. "15: aghi %[R_I],-1\n\t" \
  336. " vstl %%v23,%[R_I],0(%[R_OUT])\n\t" \
  337. /* ... and update pointers. */ \
  338. " la %[R_OUT],1(%[R_I],%[R_OUT])\n\t" \
  339. " sllg %[R_I],%[R_I],2\n\t" \
  340. " la %[R_IN],4(%[R_I],%[R_IN])\n\t" \
  341. "20:\n\t" \
  342. ".machine pop" \
  343. : /* outputs */ [R_OUT] "+a" (outptr) \
  344. , [R_IN] "+a" (inptr) \
  345. , [R_LEN] "+d" (len) \
  346. , [R_LI] "=d" (loop_count) \
  347. , [R_I] "=a" (tmp2) \
  348. , [R_TMP] "=d" (tmp) \
  349. : /* inputs */ \
  350. : /* clobber list*/ "memory", "cc" \
  351. ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
  352. ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
  353. ASM_CLOBBER_VR ("v20") ASM_CLOBBER_VR ("v21") \
  354. ASM_CLOBBER_VR ("v22") ASM_CLOBBER_VR ("v23") \
  355. ASM_CLOBBER_VR ("v24") \
  356. ); \
  357. if (len > 0) \
  358. { \
  359. /* Found an invalid character > 0x7f at next character. */ \
  360. BODY_ORIG_ERROR \
  361. } \
  362. }
  363. # define LOOP_NEED_FLAGS
  364. # include <iconv/loop.c>
  365. # include <iconv/skeleton.c>
  366. # undef BODY_ORIG
  367. # undef BODY_ORIG_ERROR
  368. ICONV_VX_IFUNC (__gconv_transform_internal_ascii)
  369. /* Convert from internal UCS4 to UCS4 little endian form. */
  370. # define DEFINE_INIT 0
  371. # define DEFINE_FINI 0
  372. # define MIN_NEEDED_FROM 4
  373. # define MIN_NEEDED_TO 4
  374. # define FROM_DIRECTION 1
  375. # define FROM_LOOP ICONV_VX_NAME (internal_ucs4le_loop)
  376. # define TO_LOOP ICONV_VX_NAME (internal_ucs4le_loop) /* This is not used. */
  377. # define FUNCTION_NAME ICONV_VX_NAME (__gconv_transform_internal_ucs4le)
  378. # define ONE_DIRECTION 0
  379. static inline int
  380. __attribute ((always_inline))
  381. ICONV_VX_NAME (internal_ucs4le_loop) (struct __gconv_step *step,
  382. struct __gconv_step_data *step_data,
  383. const unsigned char **inptrp,
  384. const unsigned char *inend,
  385. unsigned char **outptrp,
  386. const unsigned char *outend,
  387. size_t *irreversible)
  388. {
  389. const unsigned char *inptr = *inptrp;
  390. unsigned char *outptr = *outptrp;
  391. int result;
  392. size_t len = MIN (inend - inptr, outend - outptr) / 4;
  393. size_t loop_count;
  394. __asm__ volatile (".machine push\n\t"
  395. ".machine \"z13\"\n\t"
  396. ".machinemode \"zarch_nohighgprs\"\n\t"
  397. CONVERT_32BIT_SIZE_T ([R_LEN])
  398. " bras %[R_LI],1f\n\t"
  399. /* Vector permute mask: */
  400. " .long 0x03020100,0x7060504,0x0B0A0908,0x0F0E0D0C\n\t"
  401. "1: vl %%v20,0(%[R_LI])\n\t"
  402. /* Process 64byte (16char) blocks. */
  403. " srlg %[R_LI],%[R_LEN],4\n\t"
  404. " clgije %[R_LI],0,10f\n\t"
  405. "0: vlm %%v16,%%v19,0(%[R_IN])\n\t"
  406. " vperm %%v16,%%v16,%%v16,%%v20\n\t"
  407. " vperm %%v17,%%v17,%%v17,%%v20\n\t"
  408. " vperm %%v18,%%v18,%%v18,%%v20\n\t"
  409. " vperm %%v19,%%v19,%%v19,%%v20\n\t"
  410. " vstm %%v16,%%v19,0(%[R_OUT])\n\t"
  411. " la %[R_IN],64(%[R_IN])\n\t"
  412. " la %[R_OUT],64(%[R_OUT])\n\t"
  413. " brctg %[R_LI],0b\n\t"
  414. " llgfr %[R_LEN],%[R_LEN]\n\t"
  415. " nilf %[R_LEN],15\n\t"
  416. /* Process 16byte (4char) blocks. */
  417. "10: srlg %[R_LI],%[R_LEN],2\n\t"
  418. " clgije %[R_LI],0,20f\n\t"
  419. "11: vl %%v16,0(%[R_IN])\n\t"
  420. " vperm %%v16,%%v16,%%v16,%%v20\n\t"
  421. " vst %%v16,0(%[R_OUT])\n\t"
  422. " la %[R_IN],16(%[R_IN])\n\t"
  423. " la %[R_OUT],16(%[R_OUT])\n\t"
  424. " brctg %[R_LI],11b\n\t"
  425. " nill %[R_LEN],3\n\t"
  426. /* Process <16bytes. */
  427. "20: sll %[R_LEN],2\n\t"
  428. " ahi %[R_LEN],-1\n\t"
  429. " jl 30f\n\t"
  430. " vll %%v16,%[R_LEN],0(%[R_IN])\n\t"
  431. " vperm %%v16,%%v16,%%v16,%%v20\n\t"
  432. " vstl %%v16,%[R_LEN],0(%[R_OUT])\n\t"
  433. " la %[R_IN],1(%[R_LEN],%[R_IN])\n\t"
  434. " la %[R_OUT],1(%[R_LEN],%[R_OUT])\n\t"
  435. "30: \n\t"
  436. ".machine pop"
  437. : /* outputs */ [R_OUT] "+a" (outptr)
  438. , [R_IN] "+a" (inptr)
  439. , [R_LI] "=a" (loop_count)
  440. , [R_LEN] "+a" (len)
  441. : /* inputs */
  442. : /* clobber list*/ "memory", "cc"
  443. ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17")
  444. ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19")
  445. ASM_CLOBBER_VR ("v20")
  446. );
  447. *inptrp = inptr;
  448. *outptrp = outptr;
  449. /* Determine the status. */
  450. if (*inptrp == inend)
  451. result = __GCONV_EMPTY_INPUT;
  452. else if (*outptrp + 4 > outend)
  453. result = __GCONV_FULL_OUTPUT;
  454. else
  455. result = __GCONV_INCOMPLETE_INPUT;
  456. return result;
  457. }
  458. ICONV_VX_SINGLE (internal_ucs4le_loop)
  459. # include <iconv/skeleton.c>
  460. ICONV_VX_IFUNC (__gconv_transform_internal_ucs4le)
  461. /* Transform from UCS4 to the internal, UCS4-like format. Unlike
  462. for the other direction we have to check for correct values here. */
  463. # define DEFINE_INIT 0
  464. # define DEFINE_FINI 0
  465. # define MIN_NEEDED_FROM 4
  466. # define MIN_NEEDED_TO 4
  467. # define FROM_DIRECTION 1
  468. # define FROM_LOOP ICONV_VX_NAME (ucs4_internal_loop)
  469. # define TO_LOOP ICONV_VX_NAME (ucs4_internal_loop) /* This is not used. */
  470. # define FUNCTION_NAME ICONV_VX_NAME (__gconv_transform_ucs4_internal)
  471. # define ONE_DIRECTION 0
  472. static inline int
  473. __attribute ((always_inline))
  474. ICONV_VX_NAME (ucs4_internal_loop) (struct __gconv_step *step,
  475. struct __gconv_step_data *step_data,
  476. const unsigned char **inptrp,
  477. const unsigned char *inend,
  478. unsigned char **outptrp,
  479. const unsigned char *outend,
  480. size_t *irreversible)
  481. {
  482. int flags = step_data->__flags;
  483. const unsigned char *inptr = *inptrp;
  484. unsigned char *outptr = *outptrp;
  485. int result;
  486. size_t len, loop_count;
  487. do
  488. {
  489. len = MIN (inend - inptr, outend - outptr) / 4;
  490. __asm__ volatile (".machine push\n\t"
  491. ".machine \"z13\"\n\t"
  492. ".machinemode \"zarch_nohighgprs\"\n\t"
  493. CONVERT_32BIT_SIZE_T ([R_LEN])
  494. /* Setup to check for ch > 0x7fffffff. */
  495. " larl %[R_LI],9f\n\t"
  496. " vlm %%v20,%%v21,0(%[R_LI])\n\t"
  497. " srlg %[R_LI],%[R_LEN],2\n\t"
  498. " clgije %[R_LI],0,1f\n\t"
  499. /* Process 16byte (4char) blocks. */
  500. "0: vl %%v16,0(%[R_IN])\n\t"
  501. " vstrcfs %%v22,%%v16,%%v20,%%v21\n\t"
  502. " jno 10f\n\t"
  503. " vst %%v16,0(%[R_OUT])\n\t"
  504. " la %[R_IN],16(%[R_IN])\n\t"
  505. " la %[R_OUT],16(%[R_OUT])\n\t"
  506. " brctg %[R_LI],0b\n\t"
  507. " llgfr %[R_LEN],%[R_LEN]\n\t"
  508. " nilf %[R_LEN],3\n\t"
  509. /* Process <16bytes. */
  510. "1: sll %[R_LEN],2\n\t"
  511. " ahik %[R_LI],%[R_LEN],-1\n\t"
  512. " jl 20f\n\t" /* No further bytes available. */
  513. " vll %%v16,%[R_LI],0(%[R_IN])\n\t"
  514. " vstrcfs %%v22,%%v16,%%v20,%%v21\n\t"
  515. " vlgvb %[R_LI],%%v22,7\n\t"
  516. " clr %[R_LI],%[R_LEN]\n\t"
  517. " locgrhe %[R_LI],%[R_LEN]\n\t"
  518. " locghihe %[R_LEN],0\n\t"
  519. " j 11f\n\t"
  520. /* v20: Vector string range compare values. */
  521. "9: .long 0x7fffffff,0x0,0x0,0x0\n\t"
  522. /* v21: Vector string range compare control-bits.
  523. element 0: >; element 1: =<> (always true) */
  524. " .long 0x20000000,0xE0000000,0x0,0x0\n\t"
  525. /* Found a value > 0x7fffffff. */
  526. "10: vlgvb %[R_LI],%%v22,7\n\t"
  527. /* Store characters before invalid one. */
  528. "11: aghi %[R_LI],-1\n\t"
  529. " jl 20f\n\t"
  530. " vstl %%v16,%[R_LI],0(%[R_OUT])\n\t"
  531. " la %[R_IN],1(%[R_LI],%[R_IN])\n\t"
  532. " la %[R_OUT],1(%[R_LI],%[R_OUT])\n\t"
  533. "20:\n\t"
  534. ".machine pop"
  535. : /* outputs */ [R_OUT] "+a" (outptr)
  536. , [R_IN] "+a" (inptr)
  537. , [R_LI] "=a" (loop_count)
  538. , [R_LEN] "+d" (len)
  539. : /* inputs */
  540. : /* clobber list*/ "memory", "cc"
  541. ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v20")
  542. ASM_CLOBBER_VR ("v21") ASM_CLOBBER_VR ("v22")
  543. );
  544. if (len > 0)
  545. {
  546. /* The value is too large. We don't try transliteration here since
  547. this is not an error because of the lack of possibilities to
  548. represent the result. This is a genuine bug in the input since
  549. UCS4 does not allow such values. */
  550. if (irreversible == NULL)
  551. /* We are transliterating, don't try to correct anything. */
  552. return __GCONV_ILLEGAL_INPUT;
  553. if (flags & __GCONV_IGNORE_ERRORS)
  554. {
  555. /* Just ignore this character. */
  556. ++*irreversible;
  557. inptr += 4;
  558. continue;
  559. }
  560. *inptrp = inptr;
  561. *outptrp = outptr;
  562. return __GCONV_ILLEGAL_INPUT;
  563. }
  564. }
  565. while (len > 0);
  566. *inptrp = inptr;
  567. *outptrp = outptr;
  568. /* Determine the status. */
  569. if (*inptrp == inend)
  570. result = __GCONV_EMPTY_INPUT;
  571. else if (*outptrp + 4 > outend)
  572. result = __GCONV_FULL_OUTPUT;
  573. else
  574. result = __GCONV_INCOMPLETE_INPUT;
  575. return result;
  576. }
  577. ICONV_VX_SINGLE (ucs4_internal_loop)
  578. # include <iconv/skeleton.c>
  579. ICONV_VX_IFUNC (__gconv_transform_ucs4_internal)
  580. /* Transform from UCS4-LE to the internal encoding. */
  581. # define DEFINE_INIT 0
  582. # define DEFINE_FINI 0
  583. # define MIN_NEEDED_FROM 4
  584. # define MIN_NEEDED_TO 4
  585. # define FROM_DIRECTION 1
  586. # define FROM_LOOP ICONV_VX_NAME (ucs4le_internal_loop)
  587. # define TO_LOOP ICONV_VX_NAME (ucs4le_internal_loop) /* This is not used. */
  588. # define FUNCTION_NAME ICONV_VX_NAME (__gconv_transform_ucs4le_internal)
  589. # define ONE_DIRECTION 0
  590. static inline int
  591. __attribute ((always_inline))
  592. ICONV_VX_NAME (ucs4le_internal_loop) (struct __gconv_step *step,
  593. struct __gconv_step_data *step_data,
  594. const unsigned char **inptrp,
  595. const unsigned char *inend,
  596. unsigned char **outptrp,
  597. const unsigned char *outend,
  598. size_t *irreversible)
  599. {
  600. int flags = step_data->__flags;
  601. const unsigned char *inptr = *inptrp;
  602. unsigned char *outptr = *outptrp;
  603. int result;
  604. size_t len, loop_count;
  605. do
  606. {
  607. len = MIN (inend - inptr, outend - outptr) / 4;
  608. __asm__ volatile (".machine push\n\t"
  609. ".machine \"z13\"\n\t"
  610. ".machinemode \"zarch_nohighgprs\"\n\t"
  611. CONVERT_32BIT_SIZE_T ([R_LEN])
  612. /* Setup to check for ch > 0x7fffffff. */
  613. " larl %[R_LI],9f\n\t"
  614. " vlm %%v20,%%v22,0(%[R_LI])\n\t"
  615. " srlg %[R_LI],%[R_LEN],2\n\t"
  616. " clgije %[R_LI],0,1f\n\t"
  617. /* Process 16byte (4char) blocks. */
  618. "0: vl %%v16,0(%[R_IN])\n\t"
  619. " vperm %%v16,%%v16,%%v16,%%v22\n\t"
  620. " vstrcfs %%v23,%%v16,%%v20,%%v21\n\t"
  621. " jno 10f\n\t"
  622. " vst %%v16,0(%[R_OUT])\n\t"
  623. " la %[R_IN],16(%[R_IN])\n\t"
  624. " la %[R_OUT],16(%[R_OUT])\n\t"
  625. " brctg %[R_LI],0b\n\t"
  626. " llgfr %[R_LEN],%[R_LEN]\n\t"
  627. " nilf %[R_LEN],3\n\t"
  628. /* Process <16bytes. */
  629. "1: sll %[R_LEN],2\n\t"
  630. " ahik %[R_LI],%[R_LEN],-1\n\t"
  631. " jl 20f\n\t" /* No further bytes available. */
  632. " vll %%v16,%[R_LI],0(%[R_IN])\n\t"
  633. " vperm %%v16,%%v16,%%v16,%%v22\n\t"
  634. " vstrcfs %%v23,%%v16,%%v20,%%v21\n\t"
  635. " vlgvb %[R_LI],%%v23,7\n\t"
  636. " clr %[R_LI],%[R_LEN]\n\t"
  637. " locgrhe %[R_LI],%[R_LEN]\n\t"
  638. " locghihe %[R_LEN],0\n\t"
  639. " j 11f\n\t"
  640. /* v20: Vector string range compare values. */
  641. "9: .long 0x7fffffff,0x0,0x0,0x0\n\t"
  642. /* v21: Vector string range compare control-bits.
  643. element 0: >; element 1: =<> (always true) */
  644. " .long 0x20000000,0xE0000000,0x0,0x0\n\t"
  645. /* v22: Vector permute mask. */
  646. " .long 0x03020100,0x7060504,0x0B0A0908,0x0F0E0D0C\n\t"
  647. /* Found a value > 0x7fffffff. */
  648. "10: vlgvb %[R_LI],%%v23,7\n\t"
  649. /* Store characters before invalid one. */
  650. "11: aghi %[R_LI],-1\n\t"
  651. " jl 20f\n\t"
  652. " vstl %%v16,%[R_LI],0(%[R_OUT])\n\t"
  653. " la %[R_IN],1(%[R_LI],%[R_IN])\n\t"
  654. " la %[R_OUT],1(%[R_LI],%[R_OUT])\n\t"
  655. "20:\n\t"
  656. ".machine pop"
  657. : /* outputs */ [R_OUT] "+a" (outptr)
  658. , [R_IN] "+a" (inptr)
  659. , [R_LI] "=a" (loop_count)
  660. , [R_LEN] "+d" (len)
  661. : /* inputs */
  662. : /* clobber list*/ "memory", "cc"
  663. ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v20")
  664. ASM_CLOBBER_VR ("v21") ASM_CLOBBER_VR ("v22")
  665. ASM_CLOBBER_VR ("v23")
  666. );
  667. if (len > 0)
  668. {
  669. /* The value is too large. We don't try transliteration here since
  670. this is not an error because of the lack of possibilities to
  671. represent the result. This is a genuine bug in the input since
  672. UCS4 does not allow such values. */
  673. if (irreversible == NULL)
  674. /* We are transliterating, don't try to correct anything. */
  675. return __GCONV_ILLEGAL_INPUT;
  676. if (flags & __GCONV_IGNORE_ERRORS)
  677. {
  678. /* Just ignore this character. */
  679. ++*irreversible;
  680. inptr += 4;
  681. continue;
  682. }
  683. *inptrp = inptr;
  684. *outptrp = outptr;
  685. return __GCONV_ILLEGAL_INPUT;
  686. }
  687. }
  688. while (len > 0);
  689. *inptrp = inptr;
  690. *outptrp = outptr;
  691. /* Determine the status. */
  692. if (*inptrp == inend)
  693. result = __GCONV_EMPTY_INPUT;
  694. else if (*inptrp + 4 > inend)
  695. result = __GCONV_INCOMPLETE_INPUT;
  696. else
  697. {
  698. assert (*outptrp + 4 > outend);
  699. result = __GCONV_FULL_OUTPUT;
  700. }
  701. return result;
  702. }
  703. ICONV_VX_SINGLE (ucs4le_internal_loop)
  704. # include <iconv/skeleton.c>
  705. ICONV_VX_IFUNC (__gconv_transform_ucs4le_internal)
  706. /* Convert from UCS2 to the internal (UCS4-like) format. */
  707. # define DEFINE_INIT 0
  708. # define DEFINE_FINI 0
  709. # define MIN_NEEDED_FROM 2
  710. # define MIN_NEEDED_TO 4
  711. # define FROM_DIRECTION 1
  712. # define FROM_LOOP ICONV_VX_NAME (ucs2_internal_loop)
  713. # define TO_LOOP ICONV_VX_NAME (ucs2_internal_loop) /* This is not used. */
  714. # define FUNCTION_NAME ICONV_VX_NAME (__gconv_transform_ucs2_internal)
  715. # define ONE_DIRECTION 1
  716. # define MIN_NEEDED_INPUT MIN_NEEDED_FROM
  717. # define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
  718. # define LOOPFCT FROM_LOOP
  719. # define BODY_ORIG_ERROR \
  720. /* Surrogate characters in UCS-2 input are not valid. Reject \
  721. them. (Catching this here is not security relevant.) */ \
  722. STANDARD_FROM_LOOP_ERR_HANDLER (2);
  723. # define BODY_ORIG \
  724. { \
  725. uint16_t u1 = get16 (inptr); \
  726. \
  727. if (__glibc_unlikely (u1 >= 0xd800 && u1 < 0xe000)) \
  728. { \
  729. BODY_ORIG_ERROR \
  730. } \
  731. \
  732. *((uint32_t *) outptr) = u1; \
  733. outptr += sizeof (uint32_t); \
  734. inptr += 2; \
  735. }
  736. # define BODY \
  737. { \
  738. size_t len, tmp, tmp2; \
  739. len = MIN ((inend - inptr) / 2, (outend - outptr) / 4); \
  740. __asm__ volatile (".machine push\n\t" \
  741. ".machine \"z13\"\n\t" \
  742. ".machinemode \"zarch_nohighgprs\"\n\t" \
  743. CONVERT_32BIT_SIZE_T ([R_LEN]) \
  744. /* Setup to check for ch >= 0xd800 && ch < 0xe000. */ \
  745. " larl %[R_TMP],9f\n\t" \
  746. " vlm %%v20,%%v21,0(%[R_TMP])\n\t" \
  747. " srlg %[R_TMP],%[R_LEN],3\n\t" \
  748. " clgije %[R_TMP],0,1f\n\t" \
  749. /* Process 16byte (8char) blocks. */ \
  750. "0: vl %%v16,0(%[R_IN])\n\t" \
  751. " vstrchs %%v19,%%v16,%%v20,%%v21\n\t" \
  752. /* Enlarge UCS2 to UCS4. */ \
  753. " vuplhh %%v17,%%v16\n\t" \
  754. " vupllh %%v18,%%v16\n\t" \
  755. " jno 10f\n\t" \
  756. /* Store 32bytes to buf_out. */ \
  757. " vstm %%v17,%%v18,0(%[R_OUT])\n\t" \
  758. " la %[R_IN],16(%[R_IN])\n\t" \
  759. " la %[R_OUT],32(%[R_OUT])\n\t" \
  760. " brctg %[R_TMP],0b\n\t" \
  761. " llgfr %[R_LEN],%[R_LEN]\n\t" \
  762. " nilf %[R_LEN],7\n\t" \
  763. /* Process <16bytes. */ \
  764. "1: sll %[R_LEN],1\n\t" \
  765. " ahik %[R_TMP],%[R_LEN],-1\n\t" \
  766. " jl 20f\n\t" /* No further bytes available. */ \
  767. " vll %%v16,%[R_TMP],0(%[R_IN])\n\t" \
  768. " vstrchs %%v19,%%v16,%%v20,%%v21\n\t" \
  769. /* Enlarge UCS2 to UCS4. */ \
  770. " vuplhh %%v17,%%v16\n\t" \
  771. " vupllh %%v18,%%v16\n\t" \
  772. " vlgvb %[R_TMP],%%v19,7\n\t" \
  773. " clr %[R_TMP],%[R_LEN]\n\t" \
  774. " locgrhe %[R_TMP],%[R_LEN]\n\t" \
  775. " locghihe %[R_LEN],0\n\t" \
  776. " j 11f\n\t" \
  777. /* v20: Vector string range compare values. */ \
  778. "9: .short 0xd800,0xe000,0x0,0x0,0x0,0x0,0x0,0x0\n\t" \
  779. /* v21: Vector string range compare control-bits. \
  780. element 0: =>; element 1: < */ \
  781. " .short 0xa000,0x4000,0x0,0x0,0x0,0x0,0x0,0x0\n\t" \
  782. /* Found an element: ch >= 0xd800 && ch < 0xe000 */ \
  783. "10: vlgvb %[R_TMP],%%v19,7\n\t" \
  784. "11: la %[R_IN],0(%[R_TMP],%[R_IN])\n\t" \
  785. " sll %[R_TMP],1\n\t" \
  786. " lgr %[R_TMP2],%[R_TMP]\n\t" \
  787. " ahi %[R_TMP],-1\n\t" \
  788. " jl 20f\n\t" \
  789. " vstl %%v17,%[R_TMP],0(%[R_OUT])\n\t" \
  790. " ahi %[R_TMP],-16\n\t" \
  791. " jl 19f\n\t" \
  792. " vstl %%v18,%[R_TMP],16(%[R_OUT])\n\t" \
  793. "19: la %[R_OUT],0(%[R_TMP2],%[R_OUT])\n\t" \
  794. "20: \n\t" \
  795. ".machine pop" \
  796. : /* outputs */ [R_OUT] "+a" (outptr) \
  797. , [R_IN] "+a" (inptr) \
  798. , [R_TMP] "=a" (tmp) \
  799. , [R_TMP2] "=a" (tmp2) \
  800. , [R_LEN] "+d" (len) \
  801. : /* inputs */ \
  802. : /* clobber list*/ "memory", "cc" \
  803. ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
  804. ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
  805. ASM_CLOBBER_VR ("v20") ASM_CLOBBER_VR ("v21") \
  806. ); \
  807. if (len > 0) \
  808. { \
  809. /* Found an invalid character at next input-char. */ \
  810. BODY_ORIG_ERROR \
  811. } \
  812. }
  813. # define LOOP_NEED_FLAGS
  814. # include <iconv/loop.c>
  815. # include <iconv/skeleton.c>
  816. # undef BODY_ORIG
  817. # undef BODY_ORIG_ERROR
  818. ICONV_VX_IFUNC (__gconv_transform_ucs2_internal)
  819. /* Convert from UCS2 in other endianness to the internal (UCS4-like) format. */
  820. # define DEFINE_INIT 0
  821. # define DEFINE_FINI 0
  822. # define MIN_NEEDED_FROM 2
  823. # define MIN_NEEDED_TO 4
  824. # define FROM_DIRECTION 1
  825. # define FROM_LOOP ICONV_VX_NAME (ucs2reverse_internal_loop)
  826. # define TO_LOOP ICONV_VX_NAME (ucs2reverse_internal_loop) /* This is not used.*/
  827. # define FUNCTION_NAME ICONV_VX_NAME (__gconv_transform_ucs2reverse_internal)
  828. # define ONE_DIRECTION 1
  829. # define MIN_NEEDED_INPUT MIN_NEEDED_FROM
  830. # define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
  831. # define LOOPFCT FROM_LOOP
  832. # define BODY_ORIG_ERROR \
  833. /* Surrogate characters in UCS-2 input are not valid. Reject \
  834. them. (Catching this here is not security relevant.) */ \
  835. if (! ignore_errors_p ()) \
  836. { \
  837. result = __GCONV_ILLEGAL_INPUT; \
  838. break; \
  839. } \
  840. inptr += 2; \
  841. ++*irreversible; \
  842. continue;
  843. # define BODY_ORIG \
  844. { \
  845. uint16_t u1 = bswap_16 (get16 (inptr)); \
  846. \
  847. if (__glibc_unlikely (u1 >= 0xd800 && u1 < 0xe000)) \
  848. { \
  849. BODY_ORIG_ERROR \
  850. } \
  851. \
  852. *((uint32_t *) outptr) = u1; \
  853. outptr += sizeof (uint32_t); \
  854. inptr += 2; \
  855. }
  856. # define BODY \
  857. { \
  858. size_t len, tmp, tmp2; \
  859. len = MIN ((inend - inptr) / 2, (outend - outptr) / 4); \
  860. __asm__ volatile (".machine push\n\t" \
  861. ".machine \"z13\"\n\t" \
  862. ".machinemode \"zarch_nohighgprs\"\n\t" \
  863. CONVERT_32BIT_SIZE_T ([R_LEN]) \
  864. /* Setup to check for ch >= 0xd800 && ch < 0xe000. */ \
  865. " larl %[R_TMP],9f\n\t" \
  866. " vlm %%v20,%%v22,0(%[R_TMP])\n\t" \
  867. " srlg %[R_TMP],%[R_LEN],3\n\t" \
  868. " clgije %[R_TMP],0,1f\n\t" \
  869. /* Process 16byte (8char) blocks. */ \
  870. "0: vl %%v16,0(%[R_IN])\n\t" \
  871. " vperm %%v16,%%v16,%%v16,%%v22\n\t" \
  872. " vstrchs %%v19,%%v16,%%v20,%%v21\n\t" \
  873. /* Enlarge UCS2 to UCS4. */ \
  874. " vuplhh %%v17,%%v16\n\t" \
  875. " vupllh %%v18,%%v16\n\t" \
  876. " jno 10f\n\t" \
  877. /* Store 32bytes to buf_out. */ \
  878. " vstm %%v17,%%v18,0(%[R_OUT])\n\t" \
  879. " la %[R_IN],16(%[R_IN])\n\t" \
  880. " la %[R_OUT],32(%[R_OUT])\n\t" \
  881. " brctg %[R_TMP],0b\n\t" \
  882. " llgfr %[R_LEN],%[R_LEN]\n\t" \
  883. " nilf %[R_LEN],7\n\t" \
  884. /* Process <16bytes. */ \
  885. "1: sll %[R_LEN],1\n\t" \
  886. " ahik %[R_TMP],%[R_LEN],-1\n\t" \
  887. " jl 20f\n\t" /* No further bytes available. */ \
  888. " vll %%v16,%[R_TMP],0(%[R_IN])\n\t" \
  889. " vperm %%v16,%%v16,%%v16,%%v22\n\t" \
  890. " vstrchs %%v19,%%v16,%%v20,%%v21\n\t" \
  891. /* Enlarge UCS2 to UCS4. */ \
  892. " vuplhh %%v17,%%v16\n\t" \
  893. " vupllh %%v18,%%v16\n\t" \
  894. " vlgvb %[R_TMP],%%v19,7\n\t" \
  895. " clr %[R_TMP],%[R_LEN]\n\t" \
  896. " locgrhe %[R_TMP],%[R_LEN]\n\t" \
  897. " locghihe %[R_LEN],0\n\t" \
  898. " j 11f\n\t" \
  899. /* v20: Vector string range compare values. */ \
  900. "9: .short 0xd800,0xe000,0x0,0x0,0x0,0x0,0x0,0x0\n\t" \
  901. /* v21: Vector string range compare control-bits. \
  902. element 0: =>; element 1: < */ \
  903. " .short 0xa000,0x4000,0x0,0x0,0x0,0x0,0x0,0x0\n\t" \
  904. /* v22: Vector permute mask. */ \
  905. " .short 0x0100,0x0302,0x0504,0x0706\n\t" \
  906. " .short 0x0908,0x0b0a,0x0d0c,0x0f0e\n\t" \
  907. /* Found an element: ch >= 0xd800 && ch < 0xe000 */ \
  908. "10: vlgvb %[R_TMP],%%v19,7\n\t" \
  909. "11: la %[R_IN],0(%[R_TMP],%[R_IN])\n\t" \
  910. " sll %[R_TMP],1\n\t" \
  911. " lgr %[R_TMP2],%[R_TMP]\n\t" \
  912. " ahi %[R_TMP],-1\n\t" \
  913. " jl 20f\n\t" \
  914. " vstl %%v17,%[R_TMP],0(%[R_OUT])\n\t" \
  915. " ahi %[R_TMP],-16\n\t" \
  916. " jl 19f\n\t" \
  917. " vstl %%v18,%[R_TMP],16(%[R_OUT])\n\t" \
  918. "19: la %[R_OUT],0(%[R_TMP2],%[R_OUT])\n\t" \
  919. "20: \n\t" \
  920. ".machine pop" \
  921. : /* outputs */ [R_OUT] "+a" (outptr) \
  922. , [R_IN] "+a" (inptr) \
  923. , [R_TMP] "=a" (tmp) \
  924. , [R_TMP2] "=a" (tmp2) \
  925. , [R_LEN] "+d" (len) \
  926. : /* inputs */ \
  927. : /* clobber list*/ "memory", "cc" \
  928. ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
  929. ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
  930. ASM_CLOBBER_VR ("v20") ASM_CLOBBER_VR ("v21") \
  931. ASM_CLOBBER_VR ("v22") \
  932. ); \
  933. if (len > 0) \
  934. { \
  935. /* Found an invalid character at next input-char. */ \
  936. BODY_ORIG_ERROR \
  937. } \
  938. }
  939. # define LOOP_NEED_FLAGS
  940. # include <iconv/loop.c>
  941. # include <iconv/skeleton.c>
  942. # undef BODY_ORIG
  943. # undef BODY_ORIG_ERROR
  944. ICONV_VX_IFUNC (__gconv_transform_ucs2reverse_internal)
  945. /* Convert from the internal (UCS4-like) format to UCS2. */
  946. #define DEFINE_INIT 0
  947. #define DEFINE_FINI 0
  948. #define MIN_NEEDED_FROM 4
  949. #define MIN_NEEDED_TO 2
  950. #define FROM_DIRECTION 1
  951. #define FROM_LOOP ICONV_VX_NAME (internal_ucs2_loop)
  952. #define TO_LOOP ICONV_VX_NAME (internal_ucs2_loop) /* This is not used. */
  953. #define FUNCTION_NAME ICONV_VX_NAME (__gconv_transform_internal_ucs2)
  954. #define ONE_DIRECTION 1
  955. #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
  956. #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
  957. #define LOOPFCT FROM_LOOP
  958. #define BODY_ORIG \
  959. { \
  960. uint32_t val = *((const uint32_t *) inptr); \
  961. \
  962. if (__glibc_unlikely (val >= 0x10000)) \
  963. { \
  964. UNICODE_TAG_HANDLER (val, 4); \
  965. STANDARD_TO_LOOP_ERR_HANDLER (4); \
  966. } \
  967. else if (__glibc_unlikely (val >= 0xd800 && val < 0xe000)) \
  968. { \
  969. /* Surrogate characters in UCS-4 input are not valid. \
  970. We must catch this, because the UCS-2 output might be \
  971. interpreted as UTF-16 by other programs. If we let \
  972. surrogates pass through, attackers could make a security \
  973. hole exploit by synthesizing any desired plane 1-16 \
  974. character. */ \
  975. result = __GCONV_ILLEGAL_INPUT; \
  976. if (! ignore_errors_p ()) \
  977. break; \
  978. inptr += 4; \
  979. ++*irreversible; \
  980. continue; \
  981. } \
  982. else \
  983. { \
  984. put16 (outptr, val); \
  985. outptr += sizeof (uint16_t); \
  986. inptr += 4; \
  987. } \
  988. }
  989. # define BODY \
  990. { \
  991. if (__builtin_expect (inend - inptr < 32, 1) \
  992. || outend - outptr < 16) \
  993. /* Convert remaining bytes with c code. */ \
  994. BODY_ORIG \
  995. else \
  996. { \
  997. /* Convert in 32 byte blocks. */ \
  998. size_t loop_count = (inend - inptr) / 32; \
  999. size_t tmp, tmp2; \
  1000. if (loop_count > (outend - outptr) / 16) \
  1001. loop_count = (outend - outptr) / 16; \
  1002. __asm__ volatile (".machine push\n\t" \
  1003. ".machine \"z13\"\n\t" \
  1004. ".machinemode \"zarch_nohighgprs\"\n\t" \
  1005. CONVERT_32BIT_SIZE_T ([R_LI]) \
  1006. " larl %[R_I],3f\n\t" \
  1007. " vlm %%v20,%%v23,0(%[R_I])\n\t" \
  1008. "0: \n\t" \
  1009. " vlm %%v16,%%v17,0(%[R_IN])\n\t" \
  1010. /* Shorten UCS4 to UCS2. */ \
  1011. " vpkf %%v18,%%v16,%%v17\n\t" \
  1012. " vstrcfs %%v19,%%v16,%%v20,%%v21\n\t" \
  1013. " jno 11f\n\t" \
  1014. "1: vstrcfs %%v19,%%v17,%%v20,%%v21\n\t" \
  1015. " jno 10f\n\t" \
  1016. /* Store 16bytes to buf_out. */ \
  1017. "2: vst %%v18,0(%[R_OUT])\n\t" \
  1018. " la %[R_IN],32(%[R_IN])\n\t" \
  1019. " la %[R_OUT],16(%[R_OUT])\n\t" \
  1020. " brctg %[R_LI],0b\n\t" \
  1021. " j 20f\n\t" \
  1022. /* Setup to check for ch >= 0xd800. (v20, v21) */ \
  1023. "3: .long 0xd800,0xd800,0x0,0x0\n\t" \
  1024. " .long 0xa0000000,0xa0000000,0x0,0x0\n\t" \
  1025. /* Setup to check for ch >= 0xe000 \
  1026. && ch < 0x10000. (v22,v23) */ \
  1027. " .long 0xe000,0x10000,0x0,0x0\n\t" \
  1028. " .long 0xa0000000,0x40000000,0x0,0x0\n\t" \
  1029. /* v16 contains only valid chars. Check in v17: \
  1030. ch >= 0xe000 && ch <= 0xffff. */ \
  1031. "10: vstrcfs %%v19,%%v17,%%v22,%%v23,8\n\t" \
  1032. " jo 2b\n\t" /* All ch's in this range, proceed. */ \
  1033. " lghi %[R_TMP],16\n\t" \
  1034. " j 12f\n\t" \
  1035. /* Maybe v16 contains invalid chars. \
  1036. Check ch >= 0xe000 && ch <= 0xffff. */ \
  1037. "11: vstrcfs %%v19,%%v16,%%v22,%%v23,8\n\t" \
  1038. " jo 1b\n\t" /* All ch's in this range, proceed. */ \
  1039. " lghi %[R_TMP],0\n\t" \
  1040. "12: vlgvb %[R_I],%%v19,7\n\t" \
  1041. " agr %[R_I],%[R_TMP]\n\t" \
  1042. " la %[R_IN],0(%[R_I],%[R_IN])\n\t" \
  1043. " srl %[R_I],1\n\t" \
  1044. " ahi %[R_I],-1\n\t" \
  1045. " jl 20f\n\t" \
  1046. " vstl %%v18,%[R_I],0(%[R_OUT])\n\t" \
  1047. " la %[R_OUT],1(%[R_I],%[R_OUT])\n\t" \
  1048. "20:\n\t" \
  1049. ".machine pop" \
  1050. : /* outputs */ [R_OUT] "+a" (outptr) \
  1051. , [R_IN] "+a" (inptr) \
  1052. , [R_LI] "+d" (loop_count) \
  1053. , [R_I] "=a" (tmp2) \
  1054. , [R_TMP] "=d" (tmp) \
  1055. : /* inputs */ \
  1056. : /* clobber list*/ "memory", "cc" \
  1057. ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
  1058. ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
  1059. ASM_CLOBBER_VR ("v20") ASM_CLOBBER_VR ("v21") \
  1060. ASM_CLOBBER_VR ("v22") ASM_CLOBBER_VR ("v23") \
  1061. ); \
  1062. if (loop_count > 0) \
  1063. { \
  1064. /* Found an invalid character at next character. */ \
  1065. BODY_ORIG \
  1066. } \
  1067. } \
  1068. }
  1069. #define LOOP_NEED_FLAGS
  1070. #include <iconv/loop.c>
  1071. #include <iconv/skeleton.c>
  1072. # undef BODY_ORIG
  1073. ICONV_VX_IFUNC (__gconv_transform_internal_ucs2)
  1074. /* Convert from the internal (UCS4-like) format to UCS2 in other endianness. */
  1075. #define DEFINE_INIT 0
  1076. #define DEFINE_FINI 0
  1077. #define MIN_NEEDED_FROM 4
  1078. #define MIN_NEEDED_TO 2
  1079. #define FROM_DIRECTION 1
  1080. #define FROM_LOOP ICONV_VX_NAME (internal_ucs2reverse_loop)
  1081. #define TO_LOOP ICONV_VX_NAME (internal_ucs2reverse_loop)/* This is not used.*/
  1082. #define FUNCTION_NAME ICONV_VX_NAME (__gconv_transform_internal_ucs2reverse)
  1083. #define ONE_DIRECTION 1
  1084. #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
  1085. #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
  1086. #define LOOPFCT FROM_LOOP
  1087. #define BODY_ORIG \
  1088. { \
  1089. uint32_t val = *((const uint32_t *) inptr); \
  1090. if (__glibc_unlikely (val >= 0x10000)) \
  1091. { \
  1092. UNICODE_TAG_HANDLER (val, 4); \
  1093. STANDARD_TO_LOOP_ERR_HANDLER (4); \
  1094. } \
  1095. else if (__glibc_unlikely (val >= 0xd800 && val < 0xe000)) \
  1096. { \
  1097. /* Surrogate characters in UCS-4 input are not valid. \
  1098. We must catch this, because the UCS-2 output might be \
  1099. interpreted as UTF-16 by other programs. If we let \
  1100. surrogates pass through, attackers could make a security \
  1101. hole exploit by synthesizing any desired plane 1-16 \
  1102. character. */ \
  1103. if (! ignore_errors_p ()) \
  1104. { \
  1105. result = __GCONV_ILLEGAL_INPUT; \
  1106. break; \
  1107. } \
  1108. inptr += 4; \
  1109. ++*irreversible; \
  1110. continue; \
  1111. } \
  1112. else \
  1113. { \
  1114. put16 (outptr, bswap_16 (val)); \
  1115. outptr += sizeof (uint16_t); \
  1116. inptr += 4; \
  1117. } \
  1118. }
  1119. # define BODY \
  1120. { \
  1121. if (__builtin_expect (inend - inptr < 32, 1) \
  1122. || outend - outptr < 16) \
  1123. /* Convert remaining bytes with c code. */ \
  1124. BODY_ORIG \
  1125. else \
  1126. { \
  1127. /* Convert in 32 byte blocks. */ \
  1128. size_t loop_count = (inend - inptr) / 32; \
  1129. size_t tmp, tmp2; \
  1130. if (loop_count > (outend - outptr) / 16) \
  1131. loop_count = (outend - outptr) / 16; \
  1132. __asm__ volatile (".machine push\n\t" \
  1133. ".machine \"z13\"\n\t" \
  1134. ".machinemode \"zarch_nohighgprs\"\n\t" \
  1135. CONVERT_32BIT_SIZE_T ([R_LI]) \
  1136. " larl %[R_I],3f\n\t" \
  1137. " vlm %%v20,%%v24,0(%[R_I])\n\t" \
  1138. "0: \n\t" \
  1139. " vlm %%v16,%%v17,0(%[R_IN])\n\t" \
  1140. /* Shorten UCS4 to UCS2 and byteswap. */ \
  1141. " vpkf %%v18,%%v16,%%v17\n\t" \
  1142. " vperm %%v18,%%v18,%%v18,%%v24\n\t" \
  1143. " vstrcfs %%v19,%%v16,%%v20,%%v21\n\t" \
  1144. " jno 11f\n\t" \
  1145. "1: vstrcfs %%v19,%%v17,%%v20,%%v21\n\t" \
  1146. " jno 10f\n\t" \
  1147. /* Store 16bytes to buf_out. */ \
  1148. "2: vst %%v18,0(%[R_OUT])\n\t" \
  1149. " la %[R_IN],32(%[R_IN])\n\t" \
  1150. " la %[R_OUT],16(%[R_OUT])\n\t" \
  1151. " brctg %[R_LI],0b\n\t" \
  1152. " j 20f\n\t" \
  1153. /* Setup to check for ch >= 0xd800. (v20, v21) */ \
  1154. "3: .long 0xd800,0xd800,0x0,0x0\n\t" \
  1155. " .long 0xa0000000,0xa0000000,0x0,0x0\n\t" \
  1156. /* Setup to check for ch >= 0xe000 \
  1157. && ch < 0x10000. (v22,v23) */ \
  1158. " .long 0xe000,0x10000,0x0,0x0\n\t" \
  1159. " .long 0xa0000000,0x40000000,0x0,0x0\n\t" \
  1160. /* Vector permute mask (v24) */ \
  1161. " .short 0x0100,0x0302,0x0504,0x0706\n\t" \
  1162. " .short 0x0908,0x0b0a,0x0d0c,0x0f0e\n\t" \
  1163. /* v16 contains only valid chars. Check in v17: \
  1164. ch >= 0xe000 && ch <= 0xffff. */ \
  1165. "10: vstrcfs %%v19,%%v17,%%v22,%%v23,8\n\t" \
  1166. " jo 2b\n\t" /* All ch's in this range, proceed. */ \
  1167. " lghi %[R_TMP],16\n\t" \
  1168. " j 12f\n\t" \
  1169. /* Maybe v16 contains invalid chars. \
  1170. Check ch >= 0xe000 && ch <= 0xffff. */ \
  1171. "11: vstrcfs %%v19,%%v16,%%v22,%%v23,8\n\t" \
  1172. " jo 1b\n\t" /* All ch's in this range, proceed. */ \
  1173. " lghi %[R_TMP],0\n\t" \
  1174. "12: vlgvb %[R_I],%%v19,7\n\t" \
  1175. " agr %[R_I],%[R_TMP]\n\t" \
  1176. " la %[R_IN],0(%[R_I],%[R_IN])\n\t" \
  1177. " srl %[R_I],1\n\t" \
  1178. " ahi %[R_I],-1\n\t" \
  1179. " jl 20f\n\t" \
  1180. " vstl %%v18,%[R_I],0(%[R_OUT])\n\t" \
  1181. " la %[R_OUT],1(%[R_I],%[R_OUT])\n\t" \
  1182. "20:\n\t" \
  1183. ".machine pop" \
  1184. : /* outputs */ [R_OUT] "+a" (outptr) \
  1185. , [R_IN] "+a" (inptr) \
  1186. , [R_LI] "+d" (loop_count) \
  1187. , [R_I] "=a" (tmp2) \
  1188. , [R_TMP] "=d" (tmp) \
  1189. : /* inputs */ \
  1190. : /* clobber list*/ "memory", "cc" \
  1191. ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
  1192. ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
  1193. ASM_CLOBBER_VR ("v20") ASM_CLOBBER_VR ("v21") \
  1194. ASM_CLOBBER_VR ("v22") ASM_CLOBBER_VR ("v23") \
  1195. ASM_CLOBBER_VR ("v24") \
  1196. ); \
  1197. if (loop_count > 0) \
  1198. { \
  1199. /* Found an invalid character at next character. */ \
  1200. BODY_ORIG \
  1201. } \
  1202. } \
  1203. }
  1204. #define LOOP_NEED_FLAGS
  1205. #include <iconv/loop.c>
  1206. #include <iconv/skeleton.c>
  1207. # undef BODY_ORIG
  1208. ICONV_VX_IFUNC (__gconv_transform_internal_ucs2reverse)
  1209. #else
  1210. /* Generate the internal transformations without ifunc if build environment
  1211. lacks vector support. Instead simply include the common version. */
  1212. # include <iconv/gconv_simple.c>
  1213. #endif /* !defined HAVE_S390_VX_ASM_SUPPORT */