Implement new CLZ and CTZ primops (re #9340)
authorHerbert Valerio Riedel <hvr@gnu.org>
Mon, 11 Aug 2014 16:56:57 +0000 (18:56 +0200)
committerHerbert Valerio Riedel <hvr@gnu.org>
Thu, 14 Aug 2014 09:34:23 +0000 (11:34 +0200)
commite0c1767d0ea8d12e0a4badf43682a08784e379c6
tree6662fe33cd7e803253458f91307b1b5826e30b0f
parent6b5ea617dcd162e682886d5843df51a2866218d3
Implement new CLZ and CTZ primops (re #9340)

This implements the new primops

  clz#, clz32#, clz64#,
  ctz#, ctz32#, ctz64#

which provide efficient implementations of the popular
count-leading-zero and count-trailing-zero respectively
(see testcase for a pure Haskell reference implementation).

On x86, NCG as well as LLVM generates code based on the BSF/BSR
instructions (which need extra logic to make the 0-case well-defined).

Test Plan: validate and succesful tests on i686 and amd64

Reviewers: rwbarton, simonmar, ezyang, austin

Subscribers: simonmar, relrod, ezyang, carter

Differential Revision: https://phabricator.haskell.org/D144

GHC Trac Issues: #9340
17 files changed:
compiler/cmm/CmmMachOp.hs
compiler/cmm/PprC.hs
compiler/codeGen/StgCmmPrim.hs
compiler/llvmGen/LlvmCodeGen/CodeGen.hs
compiler/nativeGen/CPrim.hs
compiler/nativeGen/PPC/CodeGen.hs
compiler/nativeGen/SPARC/CodeGen.hs
compiler/nativeGen/X86/CodeGen.hs
compiler/prelude/primops.txt.pp
includes/stg/Prim.h
libraries/ghc-prim/cbits/clz.c [new file with mode: 0644]
libraries/ghc-prim/cbits/ctz.c [new file with mode: 0644]
libraries/ghc-prim/ghc-prim.cabal
testsuite/.gitignore
testsuite/tests/codeGen/should_run/T9340.hs [new file with mode: 0644]
testsuite/tests/codeGen/should_run/T9340.stdout [new file with mode: 0644]
testsuite/tests/codeGen/should_run/all.T