15f757f3fSDimitry Andric /*===---- riscv_bitmanip.h - RISC-V Zb* intrinsics --------------------------=== 25f757f3fSDimitry Andric * 35f757f3fSDimitry Andric * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 45f757f3fSDimitry Andric * See https://llvm.org/LICENSE.txt for license information. 55f757f3fSDimitry Andric * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65f757f3fSDimitry Andric * 75f757f3fSDimitry Andric *===-----------------------------------------------------------------------=== 85f757f3fSDimitry Andric */ 95f757f3fSDimitry Andric 105f757f3fSDimitry Andric #ifndef __RISCV_BITMANIP_H 115f757f3fSDimitry Andric #define __RISCV_BITMANIP_H 125f757f3fSDimitry Andric 135f757f3fSDimitry Andric #include <stdint.h> 145f757f3fSDimitry Andric 155f757f3fSDimitry Andric #if defined(__cplusplus) 165f757f3fSDimitry Andric extern "C" { 175f757f3fSDimitry Andric #endif 185f757f3fSDimitry Andric 195f757f3fSDimitry Andric #if defined(__riscv_zbb) 205f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 215f757f3fSDimitry Andric __riscv_orc_b_32(uint32_t __x) { 225f757f3fSDimitry Andric return __builtin_riscv_orc_b_32(__x); 235f757f3fSDimitry Andric } 245f757f3fSDimitry Andric 255f757f3fSDimitry Andric static __inline__ unsigned __attribute__((__always_inline__, __nodebug__)) 265f757f3fSDimitry Andric __riscv_clz_32(uint32_t __x) { 275f757f3fSDimitry Andric return __builtin_riscv_clz_32(__x); 285f757f3fSDimitry Andric } 295f757f3fSDimitry Andric 305f757f3fSDimitry Andric static __inline__ unsigned __attribute__((__always_inline__, __nodebug__)) 315f757f3fSDimitry Andric __riscv_ctz_32(uint32_t __x) { 325f757f3fSDimitry Andric return __builtin_riscv_ctz_32(__x); 335f757f3fSDimitry Andric } 345f757f3fSDimitry Andric 355f757f3fSDimitry Andric static __inline__ unsigned __attribute__((__always_inline__, __nodebug__)) 365f757f3fSDimitry Andric __riscv_cpop_32(uint32_t __x) { 37*cb14a3feSDimitry Andric return __builtin_popcount(__x); 385f757f3fSDimitry Andric } 395f757f3fSDimitry Andric 405f757f3fSDimitry Andric #if __riscv_xlen == 64 415f757f3fSDimitry Andric static __inline__ uint64_t __attribute__((__always_inline__, __nodebug__)) 425f757f3fSDimitry Andric __riscv_orc_b_64(uint64_t __x) { 435f757f3fSDimitry Andric return __builtin_riscv_orc_b_64(__x); 445f757f3fSDimitry Andric } 455f757f3fSDimitry Andric 465f757f3fSDimitry Andric static __inline__ unsigned __attribute__((__always_inline__, __nodebug__)) 475f757f3fSDimitry Andric __riscv_clz_64(uint64_t __x) { 485f757f3fSDimitry Andric return __builtin_riscv_clz_64(__x); 495f757f3fSDimitry Andric } 505f757f3fSDimitry Andric 515f757f3fSDimitry Andric static __inline__ unsigned __attribute__((__always_inline__, __nodebug__)) 525f757f3fSDimitry Andric __riscv_ctz_64(uint64_t __x) { 535f757f3fSDimitry Andric return __builtin_riscv_ctz_64(__x); 545f757f3fSDimitry Andric } 555f757f3fSDimitry Andric 565f757f3fSDimitry Andric static __inline__ unsigned __attribute__((__always_inline__, __nodebug__)) 575f757f3fSDimitry Andric __riscv_cpop_64(uint64_t __x) { 58*cb14a3feSDimitry Andric return __builtin_popcountll(__x); 595f757f3fSDimitry Andric } 605f757f3fSDimitry Andric #endif 615f757f3fSDimitry Andric #endif // defined(__riscv_zbb) 625f757f3fSDimitry Andric 635f757f3fSDimitry Andric #if defined(__riscv_zbb) || defined(__riscv_zbkb) 645f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 655f757f3fSDimitry Andric __riscv_rev8_32(uint32_t __x) { 665f757f3fSDimitry Andric return __builtin_bswap32(__x); 675f757f3fSDimitry Andric } 685f757f3fSDimitry Andric 695f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 705f757f3fSDimitry Andric __riscv_rol_32(uint32_t __x, uint32_t __y) { 715f757f3fSDimitry Andric return __builtin_rotateleft32(__x, __y); 725f757f3fSDimitry Andric } 735f757f3fSDimitry Andric 745f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 755f757f3fSDimitry Andric __riscv_ror_32(uint32_t __x, uint32_t __y) { 765f757f3fSDimitry Andric return __builtin_rotateright32(__x, __y); 775f757f3fSDimitry Andric } 785f757f3fSDimitry Andric 795f757f3fSDimitry Andric #if __riscv_xlen == 64 805f757f3fSDimitry Andric static __inline__ uint64_t __attribute__((__always_inline__, __nodebug__)) 815f757f3fSDimitry Andric __riscv_rev8_64(uint64_t __x) { 825f757f3fSDimitry Andric return __builtin_bswap64(__x); 835f757f3fSDimitry Andric } 845f757f3fSDimitry Andric 855f757f3fSDimitry Andric static __inline__ uint64_t __attribute__((__always_inline__, __nodebug__)) 865f757f3fSDimitry Andric __riscv_rol_64(uint64_t __x, uint32_t __y) { 875f757f3fSDimitry Andric return __builtin_rotateleft64(__x, __y); 885f757f3fSDimitry Andric } 895f757f3fSDimitry Andric 905f757f3fSDimitry Andric static __inline__ uint64_t __attribute__((__always_inline__, __nodebug__)) 915f757f3fSDimitry Andric __riscv_ror_64(uint64_t __x, uint32_t __y) { 925f757f3fSDimitry Andric return __builtin_rotateright64(__x, __y); 935f757f3fSDimitry Andric } 945f757f3fSDimitry Andric #endif 955f757f3fSDimitry Andric #endif // defined(__riscv_zbb) || defined(__riscv_zbkb) 965f757f3fSDimitry Andric 975f757f3fSDimitry Andric #if defined(__riscv_zbkb) 985f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 995f757f3fSDimitry Andric __riscv_brev8_32(uint32_t __x) { 1005f757f3fSDimitry Andric return __builtin_riscv_brev8_32(__x); 1015f757f3fSDimitry Andric } 1025f757f3fSDimitry Andric 1035f757f3fSDimitry Andric #if __riscv_xlen == 64 1045f757f3fSDimitry Andric static __inline__ uint64_t __attribute__((__always_inline__, __nodebug__)) 1055f757f3fSDimitry Andric __riscv_brev8_64(uint64_t __x) { 1065f757f3fSDimitry Andric return __builtin_riscv_brev8_64(__x); 1075f757f3fSDimitry Andric } 1085f757f3fSDimitry Andric #endif 1095f757f3fSDimitry Andric 1105f757f3fSDimitry Andric #if __riscv_xlen == 32 1115f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 1125f757f3fSDimitry Andric __riscv_unzip_32(uint32_t __x) { 1135f757f3fSDimitry Andric return __builtin_riscv_unzip_32(__x); 1145f757f3fSDimitry Andric } 1155f757f3fSDimitry Andric 1165f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 1175f757f3fSDimitry Andric __riscv_zip_32(uint32_t __x) { 1185f757f3fSDimitry Andric return __builtin_riscv_zip_32(__x); 1195f757f3fSDimitry Andric } 1205f757f3fSDimitry Andric #endif 1215f757f3fSDimitry Andric #endif // defined(__riscv_zbkb) 1225f757f3fSDimitry Andric 123*cb14a3feSDimitry Andric #if defined(__riscv_zbc) 124*cb14a3feSDimitry Andric #if __riscv_xlen == 32 125*cb14a3feSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 126*cb14a3feSDimitry Andric __riscv_clmulr_32(uint32_t __x, uint32_t __y) { 127*cb14a3feSDimitry Andric return __builtin_riscv_clmulr_32(__x, __y); 128*cb14a3feSDimitry Andric } 129*cb14a3feSDimitry Andric #endif 130*cb14a3feSDimitry Andric 131*cb14a3feSDimitry Andric #if __riscv_xlen == 64 132*cb14a3feSDimitry Andric static __inline__ uint64_t __attribute__((__always_inline__, __nodebug__)) 133*cb14a3feSDimitry Andric __riscv_clmulr_64(uint64_t __x, uint64_t __y) { 134*cb14a3feSDimitry Andric return __builtin_riscv_clmulr_64(__x, __y); 135*cb14a3feSDimitry Andric } 136*cb14a3feSDimitry Andric #endif 137*cb14a3feSDimitry Andric #endif // defined(__riscv_zbc) 138*cb14a3feSDimitry Andric 139*cb14a3feSDimitry Andric #if defined(__riscv_zbkc) || defined(__riscv_zbc) 1405f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 1415f757f3fSDimitry Andric __riscv_clmul_32(uint32_t __x, uint32_t __y) { 1425f757f3fSDimitry Andric return __builtin_riscv_clmul_32(__x, __y); 1435f757f3fSDimitry Andric } 1445f757f3fSDimitry Andric 1455f757f3fSDimitry Andric #if __riscv_xlen == 32 1465f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 1475f757f3fSDimitry Andric __riscv_clmulh_32(uint32_t __x, uint32_t __y) { 1485f757f3fSDimitry Andric return __builtin_riscv_clmulh_32(__x, __y); 1495f757f3fSDimitry Andric } 1505f757f3fSDimitry Andric #endif 1515f757f3fSDimitry Andric 1525f757f3fSDimitry Andric #if __riscv_xlen == 64 1535f757f3fSDimitry Andric static __inline__ uint64_t __attribute__((__always_inline__, __nodebug__)) 1545f757f3fSDimitry Andric __riscv_clmul_64(uint64_t __x, uint64_t __y) { 1555f757f3fSDimitry Andric return __builtin_riscv_clmul_64(__x, __y); 1565f757f3fSDimitry Andric } 1575f757f3fSDimitry Andric 1585f757f3fSDimitry Andric static __inline__ uint64_t __attribute__((__always_inline__, __nodebug__)) 1595f757f3fSDimitry Andric __riscv_clmulh_64(uint64_t __x, uint64_t __y) { 1605f757f3fSDimitry Andric return __builtin_riscv_clmulh_64(__x, __y); 1615f757f3fSDimitry Andric } 1625f757f3fSDimitry Andric #endif 163*cb14a3feSDimitry Andric #endif // defined(__riscv_zbkc) || defined(__riscv_zbc) 1645f757f3fSDimitry Andric 1655f757f3fSDimitry Andric #if defined(__riscv_zbkx) 1665f757f3fSDimitry Andric #if __riscv_xlen == 32 1675f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 1685f757f3fSDimitry Andric __riscv_xperm4_32(uint32_t __x, uint32_t __y) { 1695f757f3fSDimitry Andric return __builtin_riscv_xperm4_32(__x, __y); 1705f757f3fSDimitry Andric } 1715f757f3fSDimitry Andric 1725f757f3fSDimitry Andric static __inline__ uint32_t __attribute__((__always_inline__, __nodebug__)) 1735f757f3fSDimitry Andric __riscv_xperm8_32(uint32_t __x, uint32_t __y) { 1745f757f3fSDimitry Andric return __builtin_riscv_xperm8_32(__x, __y); 1755f757f3fSDimitry Andric } 1765f757f3fSDimitry Andric #endif 1775f757f3fSDimitry Andric 1785f757f3fSDimitry Andric #if __riscv_xlen == 64 1795f757f3fSDimitry Andric static __inline__ uint64_t __attribute__((__always_inline__, __nodebug__)) 1805f757f3fSDimitry Andric __riscv_xperm4_64(uint64_t __x, uint64_t __y) { 1815f757f3fSDimitry Andric return __builtin_riscv_xperm4_64(__x, __y); 1825f757f3fSDimitry Andric } 1835f757f3fSDimitry Andric 1845f757f3fSDimitry Andric static __inline__ uint64_t __attribute__((__always_inline__, __nodebug__)) 1855f757f3fSDimitry Andric __riscv_xperm8_64(uint64_t __x, uint64_t __y) { 1865f757f3fSDimitry Andric return __builtin_riscv_xperm8_64(__x, __y); 1875f757f3fSDimitry Andric } 1885f757f3fSDimitry Andric #endif 1895f757f3fSDimitry Andric #endif // defined(__riscv_zbkx) 1905f757f3fSDimitry Andric 1915f757f3fSDimitry Andric #if defined(__cplusplus) 1925f757f3fSDimitry Andric } 1935f757f3fSDimitry Andric #endif 1945f757f3fSDimitry Andric 1955f757f3fSDimitry Andric #endif 196