Opened 7 years ago

Closed 7 years ago

#5413 closed feature request (fixed)

Add population count primop

Reported by: tibbe Owned by: simonmar
Priority: normal Milestone:
Component: Compiler Version: 7.2.1
Keywords: Cc: johan.tibell@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Modern CPUs have a POPCNT instruction for efficient population count. This instruction can be used to implement various data structures.

I propose we add the following set of primops

popCnt8# :: Word# -> Word#
popCnt16# :: Word# -> Word#
popCnt32# :: Word# -> Word#
popCnt64# :: Word64# -> Word#
popCnt# :: Word# -> Word#

(We use Word# for all functions except the 64 bit version as there are no Word8, Word16# and Word32# types).

Each primop compiles into either a single POPCNT instruction or a call to some fallback function, implemented in C.

Attachments (3)

0001-Add-fallbacks-for-processor-specific-instructions-e..patch (3.5 KB) - added by tibbe 7 years ago.
0001-Add-test-for-popCnt-primop.patch (3.6 KB) - added by tibbe 7 years ago.
0001-Add-popCnt-primop.patch (12.5 KB) - added by tibbe 7 years ago.

Download all attachments as: .zip

Change History (8)

Changed 7 years ago by tibbe

comment:1 Changed 7 years ago by tibbe

Status: newpatch

comment:2 Changed 7 years ago by tibbe

Owner: set to simonmar

I've implemented the primops. Optionally we might want to create a small static library in ghc-prim containing the C fallbacks, to avoid the overhead of dynamic linking for these "fat machine instructions".

Changed 7 years ago by tibbe

comment:3 Changed 7 years ago by simonmar

Shouldn't -msse4.2 imply -msse2?

comment:4 in reply to:  3 Changed 7 years ago by tibbe

Cc: johan.tibell@… added

Replying to simonmar:

Shouldn't -msse4.2 imply -msse2?

Yes and I think it does. Check the helpers in the native code gen.

comment:5 Changed 7 years ago by tibbe

Resolution: fixed
Status: patchclosed
Note: See TracTickets for help on using tickets.