@samitouri / QOSamiQemu / commits / 288d39e26d

tcg: Simplify bswap/hswap expansion using bitswap

Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

Richard Henderson committed Aug 10, 2026 at 09:08 UTC 288d39e26d48ed26e4f786ddb3a25eef0bca9f56
1 file changed +8 -64
tcg/tcg-op.c
+8 -64
@@ -1252,23 +1252,8 @@ void tcg_gen_bswap32_i32(TCGv_i32 ret, TCGv_i32 arg)
1252 if (tcg_op_supported(INDEX_op_bswap32, TCG_TYPE_I32, 0)) {
1253 tcg_gen_op3i_i32(INDEX_op_bswap32, ret, arg, 0);
1254 } else {
1255 - TCGv_i32 t0 = tcg_temp_ebb_new_i32();
1256 - TCGv_i32 t1 = tcg_temp_ebb_new_i32();
1257 - TCGv_i32 t2 = tcg_constant_i32(0x00ff00ff);
1258 -
1259 - /* arg = abcd */
1260 - tcg_gen_shri_i32(t0, arg, 8); /* t0 = .abc */
1261 - tcg_gen_and_i32(t1, arg, t2); /* t1 = .b.d */
1262 - tcg_gen_and_i32(t0, t0, t2); /* t0 = .a.c */
1263 - tcg_gen_shli_i32(t1, t1, 8); /* t1 = b.d. */
1264 - tcg_gen_or_i32(ret, t0, t1); /* ret = badc */
1265 -
1266 - tcg_gen_shri_i32(t0, ret, 16); /* t0 = ..ba */
1267 - tcg_gen_shli_i32(t1, ret, 16); /* t1 = dc.. */
1268 - tcg_gen_or_i32(ret, t0, t1); /* ret = dcba */
1269 -
1270 - tcg_temp_free_i32(t0);
1271 - tcg_temp_free_i32(t1);
1255 + gen_bitswap_i32(ret, arg, 0x00ff00ff);
1256 + tcg_gen_hswap_i32(ret, ret);
1257 }
1258 }
1259
@@ -1823,14 +1808,9 @@ void tcg_gen_bswap32_i64(TCGv_i64 ret, TCGv_i64 arg, int flags)
1808 } else {
1809 TCGv_i64 t0 = tcg_temp_ebb_new_i64();
1810 TCGv_i64 t1 = tcg_temp_ebb_new_i64();
1826 - TCGv_i64 t2 = tcg_constant_i64(0x00ff00ff);
1811
1828 - /* arg = xxxxabcd */
1829 - tcg_gen_shri_i64(t0, arg, 8); /* t0 = .xxxxabc */
1830 - tcg_gen_and_i64(t1, arg, t2); /* t1 = .....b.d */
1831 - tcg_gen_and_i64(t0, t0, t2); /* t0 = .....a.c */
1832 - tcg_gen_shli_i64(t1, t1, 8); /* t1 = ....b.d. */
1833 - tcg_gen_or_i64(ret, t0, t1); /* ret = ....badc */
1812 + /* arg = xxxxabcd */
1813 + gen_bitswap_i64(ret, arg, 0x00ff00ff); /* ret = ....badc */
1814
1815 tcg_gen_shli_i64(t1, ret, 48); /* t1 = dc...... */
1816 tcg_gen_shri_i64(t0, ret, 16); /* t0 = ......ba */
@@ -1857,32 +1837,8 @@ void tcg_gen_bswap64_i64(TCGv_i64 ret, TCGv_i64 arg)
1837 if (tcg_op_supported(INDEX_op_bswap64, TCG_TYPE_I64, 0)) {
1838 tcg_gen_op3i_i64(INDEX_op_bswap64, ret, arg, 0);
1839 } else {
1860 - TCGv_i64 t0 = tcg_temp_ebb_new_i64();
1861 - TCGv_i64 t1 = tcg_temp_ebb_new_i64();
1862 - TCGv_i64 t2 = tcg_temp_ebb_new_i64();
1863 -
1864 - /* arg = abcdefgh */
1865 - tcg_gen_movi_i64(t2, 0x00ff00ff00ff00ffull);
1866 - tcg_gen_shri_i64(t0, arg, 8); /* t0 = .abcdefg */
1867 - tcg_gen_and_i64(t1, arg, t2); /* t1 = .b.d.f.h */
1868 - tcg_gen_and_i64(t0, t0, t2); /* t0 = .a.c.e.g */
1869 - tcg_gen_shli_i64(t1, t1, 8); /* t1 = b.d.f.h. */
1870 - tcg_gen_or_i64(ret, t0, t1); /* ret = badcfehg */
1871 -
1872 - tcg_gen_movi_i64(t2, 0x0000ffff0000ffffull);
1873 - tcg_gen_shri_i64(t0, ret, 16); /* t0 = ..badcfe */
1874 - tcg_gen_and_i64(t1, ret, t2); /* t1 = ..dc..hg */
1875 - tcg_gen_and_i64(t0, t0, t2); /* t0 = ..ba..fe */
1876 - tcg_gen_shli_i64(t1, t1, 16); /* t1 = dc..hg.. */
1877 - tcg_gen_or_i64(ret, t0, t1); /* ret = dcbahgfe */
1878 -
1879 - tcg_gen_shri_i64(t0, ret, 32); /* t0 = ....dcba */
1880 - tcg_gen_shli_i64(t1, ret, 32); /* t1 = hgfe.... */
1881 - tcg_gen_or_i64(ret, t0, t1); /* ret = hgfedcba */
1882 -
1883 - tcg_temp_free_i64(t0);
1884 - tcg_temp_free_i64(t1);
1885 - tcg_temp_free_i64(t2);
1840 + gen_bitswap_i64(ret, arg, 0x00ff00ff00ff00ffull);
1841 + tcg_gen_hswap_i64(ret, ret);
1842 }
1843 }
1844
@@ -1894,20 +1850,8 @@ void tcg_gen_bswap64_i64(TCGv_i64 ret, TCGv_i64 arg)
1850 */
1851 void tcg_gen_hswap_i64(TCGv_i64 ret, TCGv_i64 arg)
1852 {
1897 - uint64_t m = 0x0000ffff0000ffffull;
1898 - TCGv_i64 t0 = tcg_temp_ebb_new_i64();
1899 - TCGv_i64 t1 = tcg_temp_ebb_new_i64();
1900 -
1901 - /* arg = abcdefgh */
1902 - tcg_gen_rotli_i64(t1, arg, 32); /* t1 = efghabcd */
1903 - tcg_gen_andi_i64(t0, t1, m); /* t0 = ..gh..cd */
1904 - tcg_gen_shli_i64(t0, t0, 16); /* t0 = gh..cd.. */
1905 - tcg_gen_shri_i64(t1, t1, 16); /* t1 = ..efghab */
1906 - tcg_gen_andi_i64(t1, t1, m); /* t1 = ..ef..ab */
1907 - tcg_gen_or_i64(ret, t0, t1); /* ret = ghefcdab */
1908 -
1909 - tcg_temp_free_i64(t0);
1910 - tcg_temp_free_i64(t1);
1853 + gen_bitswap_i64(ret, ret, 0x0000ffff0000ffffull);
1854 + tcg_gen_wswap_i64(ret, ret);
1855 }
1856
1857 /*