Optimization works for Word but not Word32 or Word64
GHC 7.6.1 fails to properly optimize some code when explicitly specifying word size, that optimizes properly for Word.
I have also tested this in 7.4.2 and it works properly.
An example program:
import Criterion.Main
import Control.Monad.Writer
import Data.Bits
import Data.List
import Data.Word
bitByBitCopy :: Word -> Word
bitByBitCopy w = foldl' f 0 [0 .. bitSize w - 1]
where f acc n = if w `testBit` n then acc `setBit` n else acc
bitByBitCopy32 :: Word32 -> Word32
bitByBitCopy32 w = foldl' f 0 [0 .. bitSize w - 1]
where f acc n = if w `testBit` n then acc `setBit` n else acc
bitByBitCopy64 :: Word64 -> Word64
bitByBitCopy64 w = foldl' f 0 [0 .. bitSize w - 1]
where f acc n = if w `testBit` n then acc `setBit` n else acc
-- Bench
wbench :: Benchmarkable b => String -> b -> Writer [Benchmark] ()
wbench s b = tell [bench s b]
main = defaultMain . execWriter $ do
wbench "Word" $ nf bitByBitCopy 0
when (bitSize (0::Word) == 32) $
wbench "Word32" $ nf bitByBitCopy32 0
when (bitSize (0::Word) == 64) $
wbench "Word64" $ nf bitByBitCopy64 0
Trac metadata
| Trac field | Value |
|---|---|
| Version | 7.6.1 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Compiler |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | |
| Architecture |
Edited by zuserm