]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Add __builtin_bitreverse128 [PR50481]
authorJakub Jelinek <jakub@redhat.com>
Sat, 16 May 2026 08:50:57 +0000 (10:50 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Sat, 16 May 2026 08:50:57 +0000 (10:50 +0200)
We already have __builtin_bswap{16,32,64,128}, the last one has been
added ~6 years ago.  So, I think we should have also
__builtin_bitreverse128.

The following patch does that.

Note, we don't have __builtin_bswapg and I don't think we should, one can
only byteswap something which has number of bits divisible by CHAR_BIT.
For __builtin_bitreverseg that isn't a problem, but am not sure I want to
spend time handling it on say unsigned _BitInt(357).  Perhaps only if there
is some real-world use-case.

2026-05-16  Jakub Jelinek  <jakub@redhat.com>

PR target/50481
* doc/extend.texi (__builtin_bitreverse32, __builtin_bitreverse64):
Tweak wording for consistency with __builtin_bswap*.
(__builtin_bitreverse128): Document.
* builtins.def (BUILT_IN_BITREVERSE128): New.
* builtins.cc (expand_builtin): Handle also BUILT_IN_BITREVERSE128.
(is_inexpensive_builtin): Likewise.
* fold-const-call.cc (fold_const_call_ss): Handle also
CFN_BUILT_IN_BITREVERSE128.
* fold-const.cc (tree_call_nonnegative_warnv_p): Likewise.
* tree-ssa-ccp.cc (evaluate_stmt): Handle also BUILT_IN_BITREVERSE128.
* tree-ssa-phiopt.cc (empty_bb_or_one_feeding_into_p): Handle also
CFN_BUILT_IN_BITREVERSE128.
(cond_removal_in_builtin_zero_pattern): Likewise.

* gcc.dg/builtin-bitreverse-1.c: Add __builtin_bitreverse128 tests.
* gcc.dg/builtin-bitreverse-2.c: Likewise.

Reviewed-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
gcc/builtins.cc
gcc/builtins.def
gcc/doc/extend.texi
gcc/fold-const-call.cc
gcc/fold-const.cc
gcc/testsuite/gcc.dg/builtin-bitreverse-1.c
gcc/testsuite/gcc.dg/builtin-bitreverse-2.c
gcc/tree-ssa-ccp.cc
gcc/tree-ssa-phiopt.cc

index 0475bb88a6179783b63d4211052d4c2b100c6096..72310f1c928e0c2eb3bfeef47d89c8c23c5fc6d6 100644 (file)
@@ -8188,6 +8188,7 @@ expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
     case BUILT_IN_BITREVERSE16:
     case BUILT_IN_BITREVERSE32:
     case BUILT_IN_BITREVERSE64:
+    case BUILT_IN_BITREVERSE128:
       target = expand_builtin_unop (target_mode, exp, target, subtarget,
                                    bitreverse_optab);
       if (target)
@@ -12357,6 +12358,7 @@ is_inexpensive_builtin (tree decl)
       case BUILT_IN_BITREVERSE16:
       case BUILT_IN_BITREVERSE32:
       case BUILT_IN_BITREVERSE64:
+      case BUILT_IN_BITREVERSE128:
       case BUILT_IN_CLZ:
       case BUILT_IN_CLZIMAX:
       case BUILT_IN_CLZL:
index 68838de55f1340c8b316dfc1169f9081af6a3bc2..785c1c99c3fe60e9e3a9a179b1ff90a45da27783 100644 (file)
@@ -1028,6 +1028,7 @@ DEF_GCC_BUILTIN        (BUILT_IN_BITREVERSE8, "bitreverse8", BT_FN_UINT8_UINT8,
 DEF_GCC_BUILTIN        (BUILT_IN_BITREVERSE16, "bitreverse16", BT_FN_UINT16_UINT16, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_BITREVERSE32, "bitreverse32", BT_FN_UINT32_UINT32, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_BITREVERSE64, "bitreverse64", BT_FN_UINT64_UINT64, ATTR_CONST_NOTHROW_LEAF_LIST)
+DEF_GCC_BUILTIN        (BUILT_IN_BITREVERSE128, "bitreverse128", BT_FN_UINT128_UINT128, ATTR_CONST_NOTHROW_LEAF_LIST)
 
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_CLEAR_CACHE, "__clear_cache", BT_FN_VOID_PTR_PTR, ATTR_NOTHROW_LEAF_LIST)
 /* [trans-mem]: Adjust BUILT_IN_TM_CALLOC if BUILT_IN_CALLOC is changed.  */
index 96ed8b45f04f9f9d10af4af481c8bebf2cbd23bf..01ffe30c441006fdc666e4fbe0a1318821fdb115 100644 (file)
@@ -16032,15 +16032,20 @@ are 16-bit.
 @enddefbuiltin
 
 @defbuiltin{uint32_t __builtin_bitreverse32 (uint32_t @var{x})}
-Similar to @code{__builtin_bitreverse8}, except the argument and return types
+Similar to @code{__builtin_bitreverse16}, except the argument and return types
 are 32-bit.
 @enddefbuiltin
 
 @defbuiltin{uint64_t __builtin_bitreverse64 (uint64_t @var{x})}
-Similar to @code{__builtin_bitreverse8}, except the argument and return types
+Similar to @code{__builtin_bitreverse32}, except the argument and return types
 are 64-bit.
 @enddefbuiltin
 
+@defbuiltin{uint128_t __builtin_bitreverse128 (uint128_t @var{x})}
+Similar to @code{__builtin_bitreverse64}, except the argument and return types
+are 128-bit.  Only supported on targets when 128-bit types are supported.
+@enddefbuiltin
+
 @node CRC Builtins
 @subsection CRC Builtins
 @cindex CRC builtins
index d473b0dd42800715328e3885072e7e9c593c5141..f982983dba6a0b0187a19fd87c41ba316462318c 100644 (file)
@@ -1106,6 +1106,7 @@ fold_const_call_ss (wide_int *result, combined_fn fn, const wide_int_ref &arg,
     case CFN_BUILT_IN_BITREVERSE16:
     case CFN_BUILT_IN_BITREVERSE32:
     case CFN_BUILT_IN_BITREVERSE64:
+    case CFN_BUILT_IN_BITREVERSE128:
       *result = wi::bitreverse (wide_int::from (arg, precision,
                                                TYPE_SIGN (arg_type)));
       return true;
index 3bc82a108dbc066fbc8a52bce40821db75b17201..a7351445eacb1f3cd44138308fe11d9e9e2c7b55 100644 (file)
@@ -14988,6 +14988,7 @@ tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
     case CFN_BUILT_IN_BITREVERSE16:
     case CFN_BUILT_IN_BITREVERSE32:
     case CFN_BUILT_IN_BITREVERSE64:
+    case CFN_BUILT_IN_BITREVERSE128:
       /* Always true.  */
       return true;
 
index 0047e8d9e22d1cab9d7a4cf15b0b23cbc813600a..81c104fe9d41ab9eeaa553e8cb42736eab5ac3a5 100644 (file)
@@ -25,6 +25,14 @@ br64 (unsigned long long x)
   return __builtin_bitreverse64 (x);
 }
 
+#if __SIZEOF_INT128__ == 16
+[[gnu::noipa]] static unsigned __int128
+br128 (unsigned __int128 x)
+{
+  return __builtin_bitreverse128 (x);
+}
+#endif
+
 int
 main ()
 {
@@ -67,5 +75,18 @@ main ()
   if (br64 (0xffffffffffffffffull) != 0xffffffffffffffffull)
     __builtin_abort ();
 #endif
+#if __SIZEOF_INT128__ == 16
+  if (br128 (0) != 0)
+    __builtin_abort ();
+  if (br128 (1) != (unsigned __int128) 1 << 127)
+    __builtin_abort ();
+  if (br128 (((unsigned __int128) 0x0123456789abcdefull << 64)
+            | 0x2468ace013579bdfull)
+      != (((unsigned __int128) 0xfbd9eac807351624ull << 64)
+         | 0xf7b3d591e6a2c480ull))
+    __builtin_abort ();
+  if (br128 (~(unsigned __int128) 0) != ~(unsigned __int128) 0)
+    __builtin_abort ();
+#endif
 #endif
 }
index 42a677cae4b16bcfd14d848f35c8307421730648..86076bed10cc42717d2c0f301db00700d3889a78 100644 (file)
@@ -12,6 +12,13 @@ _Static_assert (__builtin_bitreverse32 (0x12345678u) == 0x1e6a2c48u,
 #endif
 #if __SIZEOF_LONG_LONG__ == 8
 _Static_assert (__builtin_bitreverse64 (0x0123456789abcdefull)
-                == 0xf7b3d591e6a2c480ull, "bitreverse64");
+               == 0xf7b3d591e6a2c480ull, "bitreverse64");
+#endif
+#if __SIZEOF_INT128__ == 16
+_Static_assert (__builtin_bitreverse128 (((unsigned __int128)
+                                         0x0123456789abcdefull << 64)
+                                        | 0x2468ace013579bdfull)
+               == (((unsigned __int128) 0xfbd9eac807351624ull << 64)
+                   | 0xf7b3d591e6a2c480ull), "bitreverse128");
 #endif
 #endif
index 891fcc12cafc3be21cce59b96410992d1a0fbbe2..d9a41c2e135989e53810e1686a5383e7595b5243 100644 (file)
@@ -2466,6 +2466,7 @@ evaluate_stmt (gimple *stmt)
            case BUILT_IN_BITREVERSE16:
            case BUILT_IN_BITREVERSE32:
            case BUILT_IN_BITREVERSE64:
+           case BUILT_IN_BITREVERSE128:
              val = get_value_for_expr (gimple_call_arg (stmt, 0), true);
              if (val.lattice_val == UNDEFINED)
                break;
index d9e1edb9b144542245ff1514aa866e7afa15c71c..e654e82363606c04d5f98e1a64522a9c9f695960 100644 (file)
@@ -823,6 +823,7 @@ empty_bb_or_one_feeding_into_p (basic_block bb,
        case CFN_BUILT_IN_BITREVERSE16:
        case CFN_BUILT_IN_BITREVERSE32:
        case CFN_BUILT_IN_BITREVERSE64:
+       case CFN_BUILT_IN_BITREVERSE128:
        CASE_CFN_FFS:
        CASE_CFN_PARITY:
        CASE_CFN_POPCOUNT:
@@ -2586,6 +2587,7 @@ cond_removal_in_builtin_zero_pattern (basic_block cond_bb,
     case CFN_BUILT_IN_BITREVERSE16:
     case CFN_BUILT_IN_BITREVERSE32:
     case CFN_BUILT_IN_BITREVERSE64:
+    case CFN_BUILT_IN_BITREVERSE128:
     CASE_CFN_FFS:
     CASE_CFN_PARITY:
     CASE_CFN_POPCOUNT: