Skip to content

Instantly share code, notes, and snippets.

@alexbowe
Created April 4, 2011 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbowe/901242 to your computer and use it in GitHub Desktop.
Save alexbowe/901242 to your computer and use it in GitHub Desktop.
Popcount table generated at compile-time using ct_expand. See https://gist.github.com/901235
-module(popcount).
-compile({parse_transform, ct_expand}).
-compile({popcount_table}).
-export([popcount16/1, popcount32/1]).
-define(TABLE(B), ct_expand:term( popcount_table:gen_table(B) )).
-define(TABLE16, ct_expand:term( ?TABLE(16) )).
popcount16(V) -> erlang:element(V+1, ?TABLE16).
popcount32(V) -> popcount16( V band 16#ffff )
+ popcount16( (V bsr 16) band 16#ffff).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment