Skip to content

Commit

Permalink
Replace __builtin_popcountl in armv7, since long is 32-bit here
Browse files Browse the repository at this point in the history
  • Loading branch information
daquexian committed May 28, 2019
1 parent 275a855 commit b47929a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .daq_pm/configs/bgemm_test_v7
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim)
name binary-nn
type cpp
build_dir build_test_v7
target bgemm_test
cmake_options -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-28 -DANDROID_ABI=armeabi-v7a -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release -GNinja
binary ~/adb_push_and_run.sh tests/bgemm_test
7 changes: 7 additions & 0 deletions .daq_pm/configs/bgemm_test_x86
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# It is a configuration file for [project_manager.vim](https://github.com/daquexian/project_manager.vim)
name binary-nn
type cpp
build_dir build_test_x86
target bgemm_test
cmake_options -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBNN_BUILD_MAIN_LIB=ON -GNinja
binary tests/bgemm_test
4 changes: 4 additions & 0 deletions common/baseline.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
#include <dabnn/mat.h>

inline int bitcount(uint64_t x) {
#ifdef __aarch64__
return __builtin_popcountl(x);
#else
std::bitset<64> bs(x);
return bs.count();
#endif
}

inline void baseline_pack_mat(const bnn::Mat &float_mat, bnn::Mat &binary_mat) {
Expand Down
9 changes: 5 additions & 4 deletions dabnn/bgemm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#if __ARM_NEON
#include <arm_neon.h>
#endif // __ARM_NEON
#include <common/baseline.h>
#include <common/helper.h>

#define P 8
Expand Down Expand Up @@ -86,7 +87,7 @@ inline void inner_kernel(const int m, const int n, const int k,
FOR(_j, 0, j) {
FOR(_i, i, m) {
FORZ(_k, k) {
C(_i, _j) += __builtin_popcountl(A(_i, _k) ^ B(_k, _j));
C(_i, _j) += bitcount(A(_i, _k) ^ B(_k, _j));
}
}
}
Expand All @@ -95,7 +96,7 @@ inline void inner_kernel(const int m, const int n, const int k,
FOR(_j, j, n) {
FOR(_i, 0, i) {
FORZ(_k, k) {
C(_i, _j) += __builtin_popcountl(A(_i, _k) ^ B(_k, _j));
C(_i, _j) += bitcount(A(_i, _k) ^ B(_k, _j));
}
}
}
Expand All @@ -104,7 +105,7 @@ inline void inner_kernel(const int m, const int n, const int k,
FOR(_j, j, n) {
FOR(_i, i, m) {
FORZ(_k, k) {
C(_i, _j) += __builtin_popcountl(A(_i, _k) ^ B(_k, _j));
C(_i, _j) += bitcount(A(_i, _k) ^ B(_k, _j));
}
}
}
Expand Down Expand Up @@ -430,7 +431,7 @@ inline void bgemm_naive(const int m, const int n, const int k,
FORZ(j, n) {
FORZ(h, k) {
C(i, j) += static_cast<float>(
__builtin_popcountl((A(i, h) ^ B(h, j))));
bitcount((A(i, h) ^ B(h, j))));
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/bitpack_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ TEST(bitpack, pack_mat_fallback) {

ASSERT_EQ(a_binary, expected);
}

TEST(bitpack, addv_v7) {
uint64_t data[2];
fill_rand_uint64(data, 2);
uint8x16_t v = vld1q_u8(reinterpret_cast<unsigned char *>(data));
auto v1 = vcntq_u8(v);
auto v2 = vpaddlq_u8(v1);
auto v3 = vpaddlq_u16(v2);
auto v4 = vpaddlq_u32(v3);

auto res = vgetq_lane_u64(v4, 0) + vgetq_lane_u64(v4, 1);

ASSERT_EQ(res, __builtin_popcountl(data[0]) + __builtin_popcountl(data[1]));
}

0 comments on commit b47929a

Please sign in to comment.