8bit-generic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* Generic conversion to and from 8bit charsets - 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. # if defined HAVE_S390_VX_GCC_SUPPORT
  17. # define ASM_CLOBBER_VR(NR) , NR
  18. # else
  19. # define ASM_CLOBBER_VR(NR)
  20. # endif
  21. /* Generate the conversion loop routines without vector instructions as
  22. fallback, if vector instructions aren't available at runtime. */
  23. # define IGNORE_ICONV_SKELETON
  24. # define from_generic __from_generic_c
  25. # define to_generic __to_generic_c
  26. # include "iconvdata/8bit-generic.c"
  27. # undef IGNORE_ICONV_SKELETON
  28. # undef from_generic
  29. # undef to_generic
  30. /* Generate the conversion routines with vector instructions. The vector
  31. routines can only be used with charsets where the maximum UCS4 value
  32. fits in 1 byte size. Then the hardware translate-instruction is used
  33. to translate between multiple generic characters and "1 byte UCS4"
  34. characters at once. The vector instructions are used to convert between
  35. the "1 byte UCS4" and UCS4. */
  36. # include <ifunc-resolve.h>
  37. # undef FROM_LOOP
  38. # undef TO_LOOP
  39. # define FROM_LOOP __from_generic_vx
  40. # define TO_LOOP __to_generic_vx
  41. # define MIN_NEEDED_FROM 1
  42. # define MIN_NEEDED_TO 4
  43. # define ONE_DIRECTION 0
  44. /* First define the conversion function from the 8bit charset to UCS4. */
  45. # define MIN_NEEDED_INPUT MIN_NEEDED_FROM
  46. # define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
  47. # define LOOPFCT FROM_LOOP
  48. # define BODY_FROM_ORIG \
  49. { \
  50. uint32_t ch = to_ucs4[*inptr]; \
  51. \
  52. if (HAS_HOLES && __builtin_expect (ch == L'\0', 0) && *inptr != '\0') \
  53. { \
  54. /* This is an illegal character. */ \
  55. STANDARD_FROM_LOOP_ERR_HANDLER (1); \
  56. } \
  57. \
  58. put32 (outptr, ch); \
  59. outptr += 4; \
  60. ++inptr; \
  61. }
  62. # define BODY \
  63. { \
  64. if (__builtin_expect (inend - inptr < 16, 1) \
  65. || outend - outptr < 64) \
  66. /* Convert remaining bytes with c code. */ \
  67. BODY_FROM_ORIG \
  68. else \
  69. { \
  70. /* Convert 16 ... 256 bytes at once with tr-instruction. */ \
  71. size_t index; \
  72. char buf[256]; \
  73. size_t loop_count = (inend - inptr) / 16; \
  74. if (loop_count > (outend - outptr) / 64) \
  75. loop_count = (outend - outptr) / 64; \
  76. if (loop_count > 16) \
  77. loop_count = 16; \
  78. __asm__ volatile (".machine push\n\t" \
  79. ".machine \"z13\"\n\t" \
  80. ".machinemode \"zarch_nohighgprs\"\n\t" \
  81. " sllk %[R_I],%[R_LI],4\n\t" \
  82. " ahi %[R_I],-1\n\t" \
  83. /* Execute mvc and tr with correct len. */ \
  84. " exrl %[R_I],21f\n\t" \
  85. " exrl %[R_I],22f\n\t" \
  86. /* Post-processing. */ \
  87. " lghi %[R_I],0\n\t" \
  88. " vzero %%v0\n\t" \
  89. "0: \n\t" \
  90. /* Find invalid character - value is zero. */ \
  91. " vl %%v16,0(%[R_I],%[R_BUF])\n\t" \
  92. " vceqbs %%v23,%%v0,%%v16\n\t" \
  93. " jle 10f\n\t" \
  94. "1: \n\t" \
  95. /* Enlarge to UCS4. */ \
  96. " vuplhb %%v17,%%v16\n\t" \
  97. " vupllb %%v18,%%v16\n\t" \
  98. " vuplhh %%v19,%%v17\n\t" \
  99. " vupllh %%v20,%%v17\n\t" \
  100. " vuplhh %%v21,%%v18\n\t" \
  101. " vupllh %%v22,%%v18\n\t" \
  102. /* Store 64bytes to buf_out. */ \
  103. " vstm %%v19,%%v22,0(%[R_OUT])\n\t" \
  104. " aghi %[R_I],16\n\t" \
  105. " la %[R_OUT],64(%[R_OUT])\n\t" \
  106. " brct %[R_LI],0b\n\t" \
  107. " la %[R_IN],0(%[R_I],%[R_IN])\n\t" \
  108. " j 20f\n\t" \
  109. "21: mvc 0(1,%[R_BUF]),0(%[R_IN])\n\t" \
  110. "22: tr 0(1,%[R_BUF]),0(%[R_TBL])\n\t" \
  111. /* Possibly invalid character found. */ \
  112. "10: \n\t" \
  113. /* Test if input was zero, too. */ \
  114. " vl %%v24,0(%[R_I],%[R_IN])\n\t" \
  115. " vceqb %%v24,%%v0,%%v24\n\t" \
  116. /* Zeros in buf (v23) and inptr (v24) are marked \
  117. with one bits. After xor, invalid characters \
  118. are marked as one bits. Proceed, if no \
  119. invalid characters are found. */ \
  120. " vx %%v24,%%v23,%%v24\n\t" \
  121. " vfenebs %%v24,%%v24,%%v0\n\t" \
  122. " jo 1b\n\t" \
  123. /* Found an invalid translation. \
  124. Store the preceding chars. */ \
  125. " la %[R_IN],0(%[R_I],%[R_IN])\n\t" \
  126. " vlgvb %[R_I],%%v24,7\n\t" \
  127. " la %[R_IN],0(%[R_I],%[R_IN])\n\t" \
  128. " sll %[R_I],2\n\t" \
  129. " ahi %[R_I],-1\n\t" \
  130. " jl 20f\n\t" \
  131. " lgr %[R_LI],%[R_I]\n\t" \
  132. " vuplhb %%v17,%%v16\n\t" \
  133. " vuplhh %%v19,%%v17\n\t" \
  134. " vstl %%v19,%[R_I],0(%[R_OUT])\n\t" \
  135. " ahi %[R_I],-16\n\t" \
  136. " jl 11f\n\t" \
  137. " vupllh %%v20,%%v17\n\t" \
  138. " vstl %%v20,%[R_I],16(%[R_OUT])\n\t" \
  139. " ahi %[R_I],-16\n\t" \
  140. " jl 11f\n\t" \
  141. " vupllb %%v18,%%v16\n\t" \
  142. " vuplhh %%v21,%%v18\n\t" \
  143. " vstl %%v21,%[R_I],32(%[R_OUT])\n\t" \
  144. " ahi %[R_I],-16\n\t" \
  145. " jl 11f\n\t" \
  146. " vupllh %%v22,%%v18\n\t" \
  147. " vstl %%v22,%[R_I],48(%[R_OUT])\n\t" \
  148. "11: \n\t" \
  149. " la %[R_OUT],1(%[R_LI],%[R_OUT])\n\t" \
  150. "20: \n\t" \
  151. ".machine pop" \
  152. : /* outputs */ [R_IN] "+a" (inptr) \
  153. , [R_OUT] "+a" (outptr), [R_I] "=&a" (index) \
  154. , [R_LI] "+a" (loop_count) \
  155. : /* inputs */ [R_BUF] "a" (buf) \
  156. , [R_TBL] "a" (to_ucs1) \
  157. : /* clobber list*/ "memory", "cc" \
  158. ASM_CLOBBER_VR ("v0") ASM_CLOBBER_VR ("v16") \
  159. ASM_CLOBBER_VR ("v17") ASM_CLOBBER_VR ("v18") \
  160. ASM_CLOBBER_VR ("v19") ASM_CLOBBER_VR ("v20") \
  161. ASM_CLOBBER_VR ("v21") ASM_CLOBBER_VR ("v22") \
  162. ASM_CLOBBER_VR ("v23") ASM_CLOBBER_VR ("v24") \
  163. ); \
  164. /* Error occurred? */ \
  165. if (loop_count != 0) \
  166. { \
  167. /* Found an invalid character! */ \
  168. STANDARD_FROM_LOOP_ERR_HANDLER (1); \
  169. } \
  170. } \
  171. }
  172. # define LOOP_NEED_FLAGS
  173. # include <iconv/loop.c>
  174. /* Next, define the other direction - from UCS4 to 8bit charset. */
  175. # define MIN_NEEDED_INPUT MIN_NEEDED_TO
  176. # define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
  177. # define LOOPFCT TO_LOOP
  178. # define BODY_TO_ORIG \
  179. { \
  180. uint32_t ch = get32 (inptr); \
  181. \
  182. if (__builtin_expect (ch >= sizeof (from_ucs4) / sizeof (from_ucs4[0]), 0)\
  183. || (__builtin_expect (from_ucs4[ch], '\1') == '\0' && ch != 0)) \
  184. { \
  185. UNICODE_TAG_HANDLER (ch, 4); \
  186. \
  187. /* This is an illegal character. */ \
  188. STANDARD_TO_LOOP_ERR_HANDLER (4); \
  189. } \
  190. \
  191. *outptr++ = from_ucs4[ch]; \
  192. inptr += 4; \
  193. }
  194. # define BODY \
  195. { \
  196. if (__builtin_expect (inend - inptr < 64, 1) \
  197. || outend - outptr < 16) \
  198. /* Convert remaining bytes with c code. */ \
  199. BODY_TO_ORIG \
  200. else \
  201. { \
  202. /* Convert 64 ... 1024 bytes at once with tr-instruction. */ \
  203. size_t index, tmp; \
  204. char buf[256]; \
  205. size_t loop_count = (inend - inptr) / 64; \
  206. uint32_t max = sizeof (from_ucs4) / sizeof (from_ucs4[0]); \
  207. if (loop_count > (outend - outptr) / 16) \
  208. loop_count = (outend - outptr) / 16; \
  209. if (loop_count > 16) \
  210. loop_count = 16; \
  211. size_t remaining_loop_count = loop_count; \
  212. /* Step 1: Check for ch>=max, ch == 0 and shorten to bytes. \
  213. (ch == 0 is no error, but is handled differently) */ \
  214. __asm__ volatile (".machine push\n\t" \
  215. ".machine \"z13\"\n\t" \
  216. ".machinemode \"zarch_nohighgprs\"\n\t" \
  217. /* Setup to check for ch >= max. */ \
  218. " vzero %%v21\n\t" \
  219. " vleih %%v21,-24576,0\n\t" /* element 0: > */ \
  220. " vleih %%v21,-8192,2\n\t" /* element 1: =<> */ \
  221. " vlvgf %%v20,%[R_MAX],0\n\t" /* element 0: val */ \
  222. /* Process in 64byte - 16 characters blocks. */ \
  223. " lghi %[R_I],0\n\t" \
  224. " lghi %[R_TMP],0\n\t" \
  225. "0: \n\t" \
  226. " vlm %%v16,%%v19,0(%[R_IN])\n\t" \
  227. /* Test for ch >= max and ch == 0. */ \
  228. " vstrczfs %%v22,%%v16,%%v20,%%v21\n\t" \
  229. " jno 10f\n\t" \
  230. " vstrczfs %%v22,%%v17,%%v20,%%v21\n\t" \
  231. " jno 11f\n\t" \
  232. " vstrczfs %%v22,%%v18,%%v20,%%v21\n\t" \
  233. " jno 12f\n\t" \
  234. " vstrczfs %%v22,%%v19,%%v20,%%v21\n\t" \
  235. " jno 13f\n\t" \
  236. /* Shorten to byte values. */ \
  237. " vpkf %%v16,%%v16,%%v17\n\t" \
  238. " vpkf %%v18,%%v18,%%v19\n\t" \
  239. " vpkh %%v16,%%v16,%%v18\n\t" \
  240. /* Store 16bytes to buf. */ \
  241. " vst %%v16,0(%[R_I],%[R_BUF])\n\t" \
  242. /* Loop until all blocks are processed. */ \
  243. " la %[R_IN],64(%[R_IN])\n\t" \
  244. " aghi %[R_I],16\n\t" \
  245. " brct %[R_LI],0b\n\t" \
  246. " j 20f\n\t" \
  247. /* Found error ch >= max or ch == 0. */ \
  248. "13: aghi %[R_TMP],4\n\t" \
  249. "12: aghi %[R_TMP],4\n\t" \
  250. "11: aghi %[R_TMP],4\n\t" \
  251. "10: vlgvb %[R_I],%%v22,7\n\t" \
  252. " srlg %[R_I],%[R_I],2\n\t" \
  253. " agr %[R_I],%[R_TMP]\n\t" \
  254. "20: \n\t" \
  255. ".machine pop" \
  256. : /* outputs */ [R_IN] "+a" (inptr) \
  257. , [R_I] "=&a" (index) \
  258. , [R_TMP] "=d" (tmp) \
  259. , [R_LI] "+d" (remaining_loop_count) \
  260. : /* inputs */ [R_BUF] "a" (buf) \
  261. , [R_MAX] "d" (max) \
  262. : /* clobber list*/ "memory", "cc" \
  263. ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17") \
  264. ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19") \
  265. ASM_CLOBBER_VR ("v20") ASM_CLOBBER_VR ("v21") \
  266. ASM_CLOBBER_VR ("v22") \
  267. ); \
  268. /* Error occurred in step 1? An error (ch >= max || ch == 0) \
  269. occurred, if remaining_loop_count > 0. The error occurred \
  270. at character-index (index) after already processed blocks. */ \
  271. loop_count -= remaining_loop_count; \
  272. if (loop_count > 0) \
  273. { \
  274. /* Step 2: Translate already processed blocks in buf and \
  275. check for errors (from_ucs4[ch] == 0). */ \
  276. __asm__ volatile (".machine push\n\t" \
  277. ".machine \"z13\"\n\t" \
  278. ".machinemode \"zarch_nohighgprs\"\n\t" \
  279. " sllk %[R_I],%[R_LI],4\n\t" \
  280. " ahi %[R_I],-1\n\t" \
  281. /* Execute tr with correct len. */ \
  282. " exrl %[R_I],21f\n\t" \
  283. /* Post-processing. */ \
  284. " lghi %[R_I],0\n\t" \
  285. "0: \n\t" \
  286. /* Find invalid character - value == 0. */ \
  287. " vl %%v16,0(%[R_I],%[R_BUF])\n\t" \
  288. " vfenezbs %%v17,%%v16,%%v16\n\t" \
  289. " je 10f\n\t" \
  290. /* Store 16bytes to buf_out. */ \
  291. " vst %%v16,0(%[R_I],%[R_OUT])\n\t" \
  292. " aghi %[R_I],16\n\t" \
  293. " brct %[R_LI],0b\n\t" \
  294. " la %[R_OUT],0(%[R_I],%[R_OUT])\n\t" \
  295. " j 20f\n\t" \
  296. "21: tr 0(1,%[R_BUF]),0(%[R_TBL])\n\t" \
  297. /* Found an error: from_ucs4[ch] == 0. */ \
  298. "10: la %[R_OUT],0(%[R_I],%[R_OUT])\n\t" \
  299. " vlgvb %[R_I],%%v17,7\n\t" \
  300. "20: \n\t" \
  301. ".machine pop" \
  302. : /* outputs */ [R_OUT] "+a" (outptr) \
  303. , [R_I] "=&a" (tmp) \
  304. , [R_LI] "+d" (loop_count) \
  305. : /* inputs */ [R_BUF] "a" (buf) \
  306. , [R_TBL] "a" (from_ucs4) \
  307. : /* clobber list*/ "memory", "cc" \
  308. ASM_CLOBBER_VR ("v16") \
  309. ASM_CLOBBER_VR ("v17") \
  310. ); \
  311. /* Error occurred in processed bytes of step 2? \
  312. Thus possible error in step 1 is obsolete.*/ \
  313. if (tmp < 16) \
  314. { \
  315. index = tmp; \
  316. inptr -= loop_count * 64; \
  317. } \
  318. } \
  319. /* Error occurred in step 1/2? */ \
  320. if (index < 16) \
  321. { \
  322. /* Found an invalid character (see step 2) or zero \
  323. (see step 1) at index! Convert the chars before index \
  324. manually. If there is a zero at index detected by step 1, \
  325. there could be invalid characters before this zero. */ \
  326. int i; \
  327. uint32_t ch; \
  328. for (i = 0; i < index; i++) \
  329. { \
  330. ch = get32 (inptr); \
  331. if (__builtin_expect (from_ucs4[ch], '\1') == '\0') \
  332. break; \
  333. *outptr++ = from_ucs4[ch]; \
  334. inptr += 4; \
  335. } \
  336. if (i == index) \
  337. { \
  338. ch = get32 (inptr); \
  339. if (ch == 0) \
  340. { \
  341. /* This is no error, but handled differently. */ \
  342. *outptr++ = from_ucs4[ch]; \
  343. inptr += 4; \
  344. continue; \
  345. } \
  346. } \
  347. \
  348. /* iconv/loop.c disables -Wmaybe-uninitialized for a false \
  349. positive warning in this code with -Os and has a \
  350. comment referencing this code accordingly. Updates in \
  351. one place may require updates in the other. */ \
  352. UNICODE_TAG_HANDLER (ch, 4); \
  353. \
  354. /* This is an illegal character. */ \
  355. STANDARD_TO_LOOP_ERR_HANDLER (4); \
  356. } \
  357. } \
  358. }
  359. # define LOOP_NEED_FLAGS
  360. # include <iconv/loop.c>
  361. /* Generate ifunc'ed loop function. */
  362. s390_libc_ifunc_expr (__from_generic_c, __from_generic,
  363. (sizeof (from_ucs4) / sizeof (from_ucs4[0]) <= 256
  364. && hwcap & HWCAP_S390_VX)
  365. ? __from_generic_vx
  366. : __from_generic_c);
  367. s390_libc_ifunc_expr (__to_generic_c, __to_generic,
  368. (sizeof (from_ucs4) / sizeof (from_ucs4[0]) <= 256
  369. && hwcap & HWCAP_S390_VX)
  370. ? __to_generic_vx
  371. : __to_generic_c);
  372. strong_alias (__to_generic_c_single, __to_generic_single)
  373. # undef FROM_LOOP
  374. # undef TO_LOOP
  375. # define FROM_LOOP __from_generic
  376. # define TO_LOOP __to_generic
  377. # include <iconv/skeleton.c>
  378. #else
  379. /* Generate this module without ifunc if build environment lacks vector
  380. support. Instead the common 8bit-generic.c is used. */
  381. # include "iconvdata/8bit-generic.c"
  382. #endif /* !defined HAVE_S390_VX_ASM_SUPPORT */