Git Product home page Git Product logo

count-bits's Introduction

count-bits

PostgreSQL extension providing simple functions for counting bits:

  • common_1bits:

    Counts the common 1-bits in two 64 bit integers.

    Examples:

    common_1bits(5,7) = 2
    
    5: 0000000000000000000000000000000000000000000000000000000000000101
    7: 0000000000000000000000000000000000000000000000000000000000000111
    
    common_1bits(7,8) = 0
    
    7: 0000000000000000000000000000000000000000000000000000000000000111
    8: 0000000000000000000000000000000000000000000000000000000000001000
    
  • unique_1bits:

    Counts the unique 1-bits in two 64 bit integers. This is also known as the Hamming distance.

    Examples:

    unique_1bits(5,7) = 1
    unique_1bits(7,8) = 4
    
  • leading_common_1bits:

    Counts the leading common 1-bits in two 64 bit integers.

    Examples:

    leading_common_1bits(5,7) = 0
    leading_common_1bits(7,8) = 0
    
  • trailing_common_1bits:

    Counts the trailing common 1-bits in two 64 bit integers.

    Examples:

    trailing_common_1bits(5,7) = 1
    trailing_common_1bits(7,8) = 0
    
  • leading_common_bits:

    Counts the leading common bits in two 64 bit integers.

    Examples:

    leading_common_bits(5,7) = 62
    leading_common_bits(7,8) = 60
    
  • leading_unique_bits:

    Counts the leading unique bits in two 64 bit integers.

    Examples:

    leading_unique_bits(5,7) = 0
    leading_unique_bits(7,8) = 0
    
  • trailing_common_bits:

    Counts the trailing common bits in two 64 bit integers.

    Examples:

    trailing_common_bits(5,7) = 1
    trailing_common_bits(7,8) = 0
    
  • trailing_unique_bits:

    Counts the trailing unique bits in two 64 bit integers.

    Examples:

    trailing_unique_bits(5,7) = 0
    trailing_unique_bits(7,8) = 4
    

Implementation

The implementation relies on the __builtin_popcountll and __builtin_ctzll intrinsics available in gcc and clang.

__builtin_popcountll will use the efficient popcntq (popcount quick) instruction if available on your target architecture and __builtin_ctzll will use bsfq (bit scan forward quick).

Installation

The PostgreSQL headers postgres.h and fmgr.h must be available for compilation. They can be found in the libpq-dev package on Debian/Ubuntu or in postgresql-libs on RedHat/CentOS. Also the pg_config binary must be available.

Compile the shared library:

make

You may want to run objdump -d count_bits.so to make sure that the popcntq instruction was used for best performance. If it wasn't, try compiling with make CFLAGS='-march=native'. Caution: The code may not run on other CPUs.

Next, install it to your PostgreSQL location for dynamically loadable modules. You'll most likely need to be root to do this.

make install

Run the following to make all functions available in your database. You'll most likely need to do this as the postgres user or as a user with the necessary rights to access the database.

PGDATABASE=<database name> make create_functions

count-bits's People

Contributors

sldab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

swackgit

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.