Giter Club home page Giter Club logo

popcnt's Introduction

popcnt

Improve popcnt performance by using simd instructions.

使用SIMD指令提升popcnt性能。

Desc

对于一个int64数字来说可以直接使用bits.OnesCount64或者popcnt指令来获取其bit位为1的数量,但如果对于一个[...]int64这样的数组(比如bitmap)来说只能遍历这个数组挨个计算最后取和,性能比较差。

我们可以利用SIMD指令并行同时计算多个int64数字的bit位为1的个数,理论上可以提高N倍性能。

Benchmark

# go test -bench=. -v
=== RUN   TestSimdPopcntQuad
--- PASS: TestSimdPopcntQuad (0.00s)
goos: linux
goarch: amd64
pkg: github.com/Orlion/popcnt
cpu: Intel Core Processor (Broadwell, no TSX)
BenchmarkSimdPopcntQuad
BenchmarkSimdPopcntQuad-8        3693530               330.8 ns/op
BenchmarkSerial
BenchmarkSerial-8               539924296                2.232 ns/op
PASS
ok      github.com/Orlion/popcnt        2.993s

可以看到使用SIMD指令优化之后性能下降了约150倍,与预期严重不符。

进一步优化思路

  1. popcnt.s中汇编代码中可以看到逻辑较为啰嗦,还有优化空间。
  2. 对于一个int64切片仍然要循环调用SimdPopcntQuad函数来计算,SimdPopcntQuad函数中又有许多寄存器初始化的代码,这样大量时间浪费寄存器初始化的工作上,可以改造下SimdPopcntQuad函数,使其接收一个int64切片,在一个汇编函数中直接计算出结果。

popcnt's People

Contributors

orlion avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.