Skip to content

[Clang][RISCV] Use __builtin_popcount in __riscv_cpop_32/64 #76286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/lib/Headers/riscv_bitmanip.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ __riscv_ctz_32(uint32_t __x) {

static __inline__ unsigned __attribute__((__always_inline__, __nodebug__))
__riscv_cpop_32(uint32_t __x) {
return __builtin_riscv_cpop_32(__x);
return __builtin_popcount(__x);
}

#if __riscv_xlen == 64
Expand All @@ -55,7 +55,7 @@ __riscv_ctz_64(uint64_t __x) {

static __inline__ unsigned __attribute__((__always_inline__, __nodebug__))
__riscv_cpop_64(uint64_t __x) {
return __builtin_riscv_cpop_64(__x);
return __builtin_popcountll(__x);
}
#endif
#endif // defined(__riscv_zbb)
Expand Down
34 changes: 30 additions & 4 deletions clang/test/CodeGen/RISCV/rvb-intrinsics/zbb.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ unsigned int clz_32(uint32_t a) {
// RV64ZBB-LABEL: @clz_64(
// RV64ZBB-NEXT: entry:
// RV64ZBB-NEXT: [[TMP0:%.*]] = call i64 @llvm.ctlz.i64(i64 [[A:%.*]], i1 false)
// RV64ZBB-NEXT: [[CAST:%.*]] = trunc i64 [[TMP0]] to i32
// RV64ZBB-NEXT: ret i32 [[CAST]]
// RV64ZBB-NEXT: [[CAST_I:%.*]] = trunc i64 [[TMP0]] to i32
// RV64ZBB-NEXT: ret i32 [[CAST_I]]
//
unsigned int clz_64(uint64_t a) {
return __riscv_clz_64(a);
Expand All @@ -77,10 +77,36 @@ unsigned int ctz_32(uint32_t a) {
// RV64ZBB-LABEL: @ctz_64(
// RV64ZBB-NEXT: entry:
// RV64ZBB-NEXT: [[TMP0:%.*]] = call i64 @llvm.cttz.i64(i64 [[A:%.*]], i1 false)
// RV64ZBB-NEXT: [[CAST:%.*]] = trunc i64 [[TMP0]] to i32
// RV64ZBB-NEXT: ret i32 [[CAST]]
// RV64ZBB-NEXT: [[CAST_I:%.*]] = trunc i64 [[TMP0]] to i32
// RV64ZBB-NEXT: ret i32 [[CAST_I]]
//
unsigned int ctz_64(uint64_t a) {
return __riscv_ctz_64(a);
}
#endif

// RV32ZBB-LABEL: @cpop_32(
// RV32ZBB-NEXT: entry:
// RV32ZBB-NEXT: [[TMP0:%.*]] = call i32 @llvm.ctpop.i32(i32 [[A:%.*]])
// RV32ZBB-NEXT: ret i32 [[TMP0]]
//
// RV64ZBB-LABEL: @cpop_32(
// RV64ZBB-NEXT: entry:
// RV64ZBB-NEXT: [[TMP0:%.*]] = call i32 @llvm.ctpop.i32(i32 [[A:%.*]])
// RV64ZBB-NEXT: ret i32 [[TMP0]]
//
unsigned int cpop_32(uint32_t a) {
return __riscv_cpop_32(a);
}

#if __riscv_xlen == 64
// RV64ZBB-LABEL: @cpop_64(
// RV64ZBB-NEXT: entry:
// RV64ZBB-NEXT: [[TMP0:%.*]] = call i64 @llvm.ctpop.i64(i64 [[A:%.*]])
// RV64ZBB-NEXT: [[CAST_I:%.*]] = trunc i64 [[TMP0]] to i32
// RV64ZBB-NEXT: ret i32 [[CAST_I]]
//
unsigned int cpop_64(uint64_t a) {
return __riscv_cpop_64(a);
}
#endif